您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

RemoteVideo.js 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. /* global $, APP, interfaceConfig */
  2. import ConnectionIndicator from './ConnectionIndicator';
  3. import SmallVideo from "./SmallVideo";
  4. import AudioLevels from "../audio_levels/AudioLevels";
  5. import UIUtils from "../util/UIUtil";
  6. import UIEvents from '../../../service/UI/UIEvents';
  7. import JitsiPopover from "../util/JitsiPopover";
  8. function RemoteVideo(id, VideoLayout, emitter) {
  9. this.id = id;
  10. this.emitter = emitter;
  11. this.videoSpanId = `participant_${id}`;
  12. SmallVideo.call(this, VideoLayout);
  13. this.hasRemoteVideoMenu = false;
  14. this.addRemoteVideoContainer();
  15. this.connectionIndicator = new ConnectionIndicator(this, id);
  16. this.setDisplayName();
  17. this.flipX = false;
  18. this.isLocal = false;
  19. }
  20. RemoteVideo.prototype = Object.create(SmallVideo.prototype);
  21. RemoteVideo.prototype.constructor = RemoteVideo;
  22. RemoteVideo.prototype.addRemoteVideoContainer = function() {
  23. this.container = RemoteVideo.createContainer(this.videoSpanId);
  24. this.initBrowserSpecificProperties();
  25. if (APP.conference.isModerator) {
  26. this.addRemoteVideoMenu();
  27. }
  28. let { remoteVideo } = this.VideoLayout.resizeThumbnails();
  29. let { thumbHeight, thumbWidth } = remoteVideo;
  30. AudioLevels.createAudioLevelCanvas(this.id, thumbWidth, thumbHeight);
  31. return this.container;
  32. };
  33. /**
  34. * Initializes the remote participant popup menu, by specifying previously
  35. * constructed popupMenuElement, containing all the menu items.
  36. *
  37. * @param popupMenuElement a pre-constructed element, containing the menu items
  38. * to display in the popup
  39. */
  40. RemoteVideo.prototype._initPopupMenu = function (popupMenuElement) {
  41. this.popover = new JitsiPopover(
  42. $("#" + this.videoSpanId + " .remotevideomenu"),
  43. { content: popupMenuElement.outerHTML,
  44. skin: "black"});
  45. // override popover show method to make sure we will update the content
  46. // before showing the popover
  47. var origShowFunc = this.popover.show;
  48. this.popover.show = function () {
  49. // update content by forcing it, to finish even if popover
  50. // is not visible
  51. this.updateRemoteVideoMenu(this.isAudioMuted, true);
  52. // call the original show, passing its actual this
  53. origShowFunc.call(this.popover);
  54. }.bind(this);
  55. };
  56. /**
  57. * Generates the popup menu content.
  58. *
  59. * @returns {Element|*} the constructed element, containing popup menu items
  60. * @private
  61. */
  62. RemoteVideo.prototype._generatePopupContent = function () {
  63. var popupmenuElement = document.createElement('ul');
  64. popupmenuElement.className = 'popupmenu';
  65. popupmenuElement.id = `remote_popupmenu_${this.id}`;
  66. var muteMenuItem = document.createElement('li');
  67. var muteLinkItem = document.createElement('a');
  68. var mutedIndicator = "<i class='icon-mic-disabled'></i>";
  69. var doMuteHTML = mutedIndicator +
  70. " <div " +
  71. "data-i18n='videothumbnail.domute'>" +
  72. APP.translation.translateString("videothumbnail.domute") +
  73. "</div>";
  74. var mutedHTML = mutedIndicator +
  75. " <div " +
  76. "data-i18n='videothumbnail.muted'>" +
  77. APP.translation.translateString("videothumbnail.muted") +
  78. "</div>";
  79. muteLinkItem.id = "mutelink_" + this.id;
  80. if (this.isAudioMuted) {
  81. muteLinkItem.innerHTML = mutedHTML;
  82. muteLinkItem.className = 'mutelink disabled';
  83. }
  84. else {
  85. muteLinkItem.innerHTML = doMuteHTML;
  86. muteLinkItem.className = 'mutelink';
  87. }
  88. // Delegate event to the document.
  89. $(document).on("click", "#mutelink_" + this.id, function(){
  90. if (this.isAudioMuted)
  91. return;
  92. this.emitter.emit(UIEvents.REMOTE_AUDIO_MUTED, this.id);
  93. this.popover.forceHide();
  94. }.bind(this));
  95. muteMenuItem.appendChild(muteLinkItem);
  96. popupmenuElement.appendChild(muteMenuItem);
  97. var ejectIndicator = "<i style='float:left;' class='icon-kick'></i>";
  98. var ejectMenuItem = document.createElement('li');
  99. var ejectLinkItem = document.createElement('a');
  100. var ejectText = "<div " +
  101. "data-i18n='videothumbnail.kick'>" +
  102. APP.translation.translateString("videothumbnail.kick") +
  103. "</div>";
  104. ejectLinkItem.className = 'ejectlink';
  105. ejectLinkItem.innerHTML = ejectIndicator + ' ' + ejectText;
  106. ejectLinkItem.id = "ejectlink_" + this.id;
  107. $(document).on("click", "#ejectlink_" + this.id, function(){
  108. this.emitter.emit(UIEvents.USER_KICKED, this.id);
  109. this.popover.forceHide();
  110. }.bind(this));
  111. ejectMenuItem.appendChild(ejectLinkItem);
  112. popupmenuElement.appendChild(ejectMenuItem);
  113. return popupmenuElement;
  114. };
  115. /**
  116. * Updates the remote video menu.
  117. *
  118. * @param isMuted the new muted state to update to
  119. * @param force to work even if popover is not visible
  120. */
  121. RemoteVideo.prototype.updateRemoteVideoMenu = function (isMuted, force) {
  122. this.isAudioMuted = isMuted;
  123. // generate content, translate it and add it to document only if
  124. // popover is visible or we force to do so.
  125. if(this.popover.popoverShown || force) {
  126. this.popover.updateContent(this._generatePopupContent());
  127. }
  128. };
  129. /**
  130. * Adds the remote video menu element for the given <tt>id</tt> in the
  131. * given <tt>parentElement</tt>.
  132. *
  133. * @param id the id indicating the video for which we're adding a menu.
  134. * @param parentElement the parent element where this menu will be added
  135. */
  136. if (!interfaceConfig.filmStripOnly) {
  137. RemoteVideo.prototype.addRemoteVideoMenu = function () {
  138. var spanElement = document.createElement('span');
  139. spanElement.className = 'remotevideomenu toolbar-icon right';
  140. this.container
  141. .querySelector('.videocontainer__toolbar')
  142. .appendChild(spanElement);
  143. var menuElement = document.createElement('i');
  144. menuElement.className = 'icon-menu-up';
  145. menuElement.title = 'Remote user controls';
  146. spanElement.appendChild(menuElement);
  147. this._initPopupMenu(this._generatePopupContent());
  148. this.hasRemoteVideoMenu = true;
  149. };
  150. } else {
  151. RemoteVideo.prototype.addRemoteVideoMenu = function() {};
  152. }
  153. /**
  154. * Removes the remote stream element corresponding to the given stream and
  155. * parent container.
  156. *
  157. * @param stream the MediaStream
  158. * @param isVideo <tt>true</tt> if given <tt>stream</tt> is a video one.
  159. */
  160. RemoteVideo.prototype.removeRemoteStreamElement = function (stream) {
  161. if (!this.container)
  162. return false;
  163. var isVideo = stream.isVideoTrack();
  164. var elementID = SmallVideo.getStreamElementID(stream);
  165. var select = $('#' + elementID);
  166. select.remove();
  167. console.info((isVideo ? "Video" : "Audio") +
  168. " removed " + this.id, select);
  169. // when removing only the video element and we are on stage
  170. // update the stage
  171. if (isVideo && this.VideoLayout.isCurrentlyOnLarge(this.id))
  172. this.VideoLayout.updateLargeVideo(this.id);
  173. };
  174. /**
  175. * Removes RemoteVideo from the page.
  176. */
  177. RemoteVideo.prototype.remove = function () {
  178. console.log("Remove thumbnail", this.id);
  179. this.removeConnectionIndicator();
  180. // Make sure that the large video is updated if are removing its
  181. // corresponding small video.
  182. this.VideoLayout.updateAfterThumbRemoved(this.id);
  183. // Remove whole container
  184. if (this.container.parentNode) {
  185. this.container.parentNode.removeChild(this.container);
  186. }
  187. };
  188. RemoteVideo.prototype.waitForPlayback = function (streamElement, stream) {
  189. var webRtcStream = stream.getOriginalStream();
  190. var isVideo = stream.isVideoTrack();
  191. if (!isVideo || webRtcStream.id === 'mixedmslabel') {
  192. return;
  193. }
  194. var self = this;
  195. // Register 'onplaying' listener to trigger 'videoactive' on VideoLayout
  196. // when video playback starts
  197. var onPlayingHandler = function () {
  198. self.VideoLayout.videoactive(streamElement, self.id);
  199. streamElement.onplaying = null;
  200. };
  201. streamElement.onplaying = onPlayingHandler;
  202. };
  203. /**
  204. * Checks whether or not video stream exists and has started for this
  205. * RemoteVideo instance. This is checked by trying to select video element in
  206. * this container and checking if 'currentTime' field's value is greater than 0.
  207. *
  208. * @returns {*|boolean} true if this RemoteVideo has active video stream running
  209. */
  210. RemoteVideo.prototype.hasVideoStarted = function () {
  211. var videoSelector = this.selectVideoElement();
  212. return videoSelector.length && videoSelector[0].currentTime > 0;
  213. };
  214. RemoteVideo.prototype.addRemoteStreamElement = function (stream) {
  215. if (!this.container) {
  216. return;
  217. }
  218. let isVideo = stream.isVideoTrack();
  219. isVideo ? this.videoStream = stream : this.audioStream = stream;
  220. if (isVideo)
  221. this.setVideoType(stream.videoType);
  222. // Add click handler.
  223. let onClickHandler = (event) => {
  224. let source = event.target || event.srcElement;
  225. // ignore click if it was done in popup menu
  226. if ($(source).parents('.popupmenu').length === 0) {
  227. this.VideoLayout.handleVideoThumbClicked(this.id);
  228. }
  229. // On IE we need to populate this handler on video <object>
  230. // and it does not give event instance as an argument,
  231. // so we check here for methods.
  232. if (event.stopPropagation && event.preventDefault) {
  233. event.stopPropagation();
  234. event.preventDefault();
  235. }
  236. return false;
  237. };
  238. this.container.onclick = onClickHandler;
  239. if(!stream.getOriginalStream())
  240. return;
  241. let streamElement = SmallVideo.createStreamElement(stream);
  242. let newElementId = streamElement.id;
  243. // Put new stream element always in front
  244. UIUtils.prependChild(this.container, streamElement);
  245. // If we hide element when Temasys plugin is used then
  246. // we'll never receive 'onplay' event and other logic won't work as expected
  247. // NOTE: hiding will not have effect when Temasys plugin is in use, as
  248. // calling attach will show it back
  249. $(streamElement).hide();
  250. // If the container is currently visible
  251. // we attach the stream to the element.
  252. if (!isVideo || (this.container.offsetParent !== null && isVideo)) {
  253. this.waitForPlayback(streamElement, stream);
  254. streamElement = stream.attach(streamElement);
  255. }
  256. $(streamElement).click(onClickHandler);
  257. },
  258. /**
  259. * Show/hide peer container for the given id.
  260. */
  261. RemoteVideo.prototype.showPeerContainer = function (state) {
  262. if (!this.container)
  263. return;
  264. var isHide = state === 'hide';
  265. var resizeThumbnails = false;
  266. if (!isHide) {
  267. if (!$(this.container).is(':visible')) {
  268. resizeThumbnails = true;
  269. $(this.container).show();
  270. }
  271. // Call updateView, so that we'll figure out if avatar
  272. // should be displayed based on video muted status and whether or not
  273. // it's in the lastN set
  274. this.updateView();
  275. }
  276. else if ($(this.container).is(':visible') && isHide)
  277. {
  278. resizeThumbnails = true;
  279. $(this.container).hide();
  280. if(this.connectionIndicator)
  281. this.connectionIndicator.hide();
  282. }
  283. if (resizeThumbnails) {
  284. this.VideoLayout.resizeThumbnails();
  285. }
  286. // We want to be able to pin a participant from the contact list, even
  287. // if he's not in the lastN set!
  288. // ContactList.setClickable(id, !isHide);
  289. };
  290. RemoteVideo.prototype.updateResolution = function (resolution) {
  291. if (this.connectionIndicator) {
  292. this.connectionIndicator.updateResolution(resolution);
  293. }
  294. };
  295. RemoteVideo.prototype.removeConnectionIndicator = function () {
  296. if (this.connectionIndicator)
  297. this.connectionIndicator.remove();
  298. };
  299. RemoteVideo.prototype.hideConnectionIndicator = function () {
  300. if (this.connectionIndicator)
  301. this.connectionIndicator.hide();
  302. };
  303. /**
  304. * Sets the display name for the given video span id.
  305. */
  306. RemoteVideo.prototype.setDisplayName = function(displayName, key) {
  307. if (!this.container) {
  308. console.warn( "Unable to set displayName - " + this.videoSpanId +
  309. " does not exist");
  310. return;
  311. }
  312. var nameSpan = $('#' + this.videoSpanId + ' .displayname');
  313. // If we already have a display name for this video.
  314. if (nameSpan.length > 0) {
  315. if (displayName && displayName.length > 0) {
  316. var displaynameSpan = $('#' + this.videoSpanId + '_name');
  317. if (displaynameSpan.text() !== displayName)
  318. displaynameSpan.text(displayName);
  319. }
  320. else if (key && key.length > 0) {
  321. var nameHtml = APP.translation.generateTranslationHTML(key);
  322. $('#' + this.videoSpanId + '_name').html(nameHtml);
  323. }
  324. else
  325. $('#' + this.videoSpanId + '_name').text(
  326. interfaceConfig.DEFAULT_REMOTE_DISPLAY_NAME);
  327. } else {
  328. nameSpan = document.createElement('span');
  329. nameSpan.className = 'displayname';
  330. $('#' + this.videoSpanId)[0]
  331. .querySelector('.videocontainer__toolbar')
  332. .appendChild(nameSpan);
  333. if (displayName && displayName.length > 0) {
  334. $(nameSpan).text(displayName);
  335. } else {
  336. nameSpan.innerHTML = interfaceConfig.DEFAULT_REMOTE_DISPLAY_NAME;
  337. }
  338. nameSpan.id = this.videoSpanId + '_name';
  339. }
  340. };
  341. /**
  342. * Removes remote video menu element from video element identified by
  343. * given <tt>videoElementId</tt>.
  344. *
  345. * @param videoElementId the id of local or remote video element.
  346. */
  347. RemoteVideo.prototype.removeRemoteVideoMenu = function() {
  348. var menuSpan = $('#' + this.videoSpanId + '> .remotevideomenu');
  349. if (menuSpan.length) {
  350. this.popover.forceHide();
  351. menuSpan.remove();
  352. this.hasRemoteVideoMenu = false;
  353. }
  354. };
  355. RemoteVideo.createContainer = function (spanId) {
  356. let container = document.createElement('span');
  357. container.id = spanId;
  358. container.className = 'videocontainer';
  359. let toolbar = document.createElement('div');
  360. toolbar.className = "videocontainer__toolbar";
  361. container.appendChild(toolbar);
  362. var remotes = document.getElementById('remoteVideos');
  363. return remotes.appendChild(container);
  364. };
  365. export default RemoteVideo;