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

RemoteVideo.js 14KB

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