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

RemoteVideo.js 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630
  1. /* global $, APP, interfaceConfig */
  2. import ConnectionIndicator from './ConnectionIndicator';
  3. import SmallVideo from "./SmallVideo";
  4. import UIUtils from "../util/UIUtil";
  5. import UIEvents from '../../../service/UI/UIEvents';
  6. import JitsiPopover from "../util/JitsiPopover";
  7. const MUTED_DIALOG_BUTTON_VALUES = {
  8. cancel: 0,
  9. muted: 1
  10. };
  11. /**
  12. * Creates new instance of the <tt>RemoteVideo</tt>.
  13. * @param user {JitsiParticipant} the user for whom remote video instance will
  14. * be created.
  15. * @param {VideoLayout} VideoLayout the video layout instance.
  16. * @param {EventEmitter} emitter the event emitter which will be used by
  17. * the new instance to emit events.
  18. * @constructor
  19. */
  20. function RemoteVideo(user, VideoLayout, emitter) {
  21. this.user = user;
  22. this.id = user.getId();
  23. this.emitter = emitter;
  24. this.videoSpanId = `participant_${this.id}`;
  25. SmallVideo.call(this, VideoLayout);
  26. this.hasRemoteVideoMenu = false;
  27. this.addRemoteVideoContainer();
  28. this.connectionIndicator = new ConnectionIndicator(this, this.id);
  29. this.setDisplayName();
  30. this.bindHoverHandler();
  31. this.flipX = false;
  32. this.isLocal = false;
  33. /**
  34. * The flag is set to <tt>true</tt> after the 'onplay' event has been
  35. * triggered on the current video element. It goes back to <tt>false</tt>
  36. * when the stream is removed. It is used to determine whether the video
  37. * playback has ever started.
  38. * @type {boolean}
  39. */
  40. this.wasVideoPlayed = false;
  41. /**
  42. * The flag is set to <tt>true</tt> if remote participant's video gets muted
  43. * during his media connection disruption. This is to prevent black video
  44. * being render on the thumbnail, because even though once the video has
  45. * been played the image usually remains on the video element it seems that
  46. * after longer period of the video element being hidden this image can be
  47. * lost.
  48. * @type {boolean}
  49. */
  50. this.mutedWhileDisconnected = false;
  51. }
  52. RemoteVideo.prototype = Object.create(SmallVideo.prototype);
  53. RemoteVideo.prototype.constructor = RemoteVideo;
  54. RemoteVideo.prototype.addRemoteVideoContainer = function() {
  55. this.container = RemoteVideo.createContainer(this.videoSpanId);
  56. this.initBrowserSpecificProperties();
  57. if (APP.conference.isModerator) {
  58. this.addRemoteVideoMenu();
  59. }
  60. this.VideoLayout.resizeThumbnails(false, true);
  61. this.addAudioLevelIndicator();
  62. return this.container;
  63. };
  64. /**
  65. * Initializes the remote participant popup menu, by specifying previously
  66. * constructed popupMenuElement, containing all the menu items.
  67. *
  68. * @param popupMenuElement a pre-constructed element, containing the menu items
  69. * to display in the popup
  70. */
  71. RemoteVideo.prototype._initPopupMenu = function (popupMenuElement) {
  72. let options = {
  73. content: popupMenuElement.outerHTML,
  74. skin: "black",
  75. hasArrow: false,
  76. onBeforePosition: el => APP.translation.translateElement(el)
  77. };
  78. let element = $("#" + this.videoSpanId + " .remotevideomenu");
  79. this.popover = new JitsiPopover(element, options);
  80. // override popover show method to make sure we will update the content
  81. // before showing the popover
  82. let origShowFunc = this.popover.show;
  83. this.popover.show = function () {
  84. // update content by forcing it, to finish even if popover
  85. // is not visible
  86. this.updateRemoteVideoMenu(this.isAudioMuted, true);
  87. // call the original show, passing its actual this
  88. origShowFunc.call(this.popover);
  89. }.bind(this);
  90. };
  91. /**
  92. * Generates the popup menu content.
  93. *
  94. * @returns {Element|*} the constructed element, containing popup menu items
  95. * @private
  96. */
  97. RemoteVideo.prototype._generatePopupContent = function () {
  98. var popupmenuElement = document.createElement('ul');
  99. popupmenuElement.className = 'popupmenu';
  100. popupmenuElement.id = `remote_popupmenu_${this.id}`;
  101. var muteMenuItem = document.createElement('li');
  102. var muteLinkItem = document.createElement('a');
  103. var mutedIndicator = "<i class='icon-mic-disabled'></i>";
  104. var doMuteHTML = mutedIndicator +
  105. " <div data-i18n='videothumbnail.domute'></div>";
  106. var mutedHTML = mutedIndicator +
  107. " <div data-i18n='videothumbnail.muted'></div>";
  108. muteLinkItem.id = "mutelink_" + this.id;
  109. if (this.isAudioMuted) {
  110. muteLinkItem.innerHTML = mutedHTML;
  111. muteLinkItem.className = 'mutelink disabled';
  112. }
  113. else {
  114. muteLinkItem.innerHTML = doMuteHTML;
  115. muteLinkItem.className = 'mutelink';
  116. }
  117. // Delegate event to the document.
  118. $(document).on("click", "#mutelink_" + this.id, () => {
  119. if (this.isAudioMuted)
  120. return;
  121. RemoteVideo.showMuteParticipantDialog().then(reason => {
  122. if(reason === MUTED_DIALOG_BUTTON_VALUES.muted) {
  123. this.emitter.emit(UIEvents.REMOTE_AUDIO_MUTED, this.id);
  124. }
  125. }).catch(e => {
  126. //currently shouldn't be called
  127. console.error(e);
  128. });
  129. this.popover.forceHide();
  130. });
  131. muteMenuItem.appendChild(muteLinkItem);
  132. popupmenuElement.appendChild(muteMenuItem);
  133. var ejectIndicator = "<i style='float:left;' class='icon-kick'></i>";
  134. var ejectMenuItem = document.createElement('li');
  135. var ejectLinkItem = document.createElement('a');
  136. var ejectText = "<div data-i18n='videothumbnail.kick'></div>";
  137. ejectLinkItem.className = 'ejectlink';
  138. ejectLinkItem.innerHTML = ejectIndicator + ' ' + ejectText;
  139. ejectLinkItem.id = "ejectlink_" + this.id;
  140. $(document).on("click", "#ejectlink_" + this.id, function(){
  141. this.emitter.emit(UIEvents.USER_KICKED, this.id);
  142. this.popover.forceHide();
  143. }.bind(this));
  144. ejectMenuItem.appendChild(ejectLinkItem);
  145. popupmenuElement.appendChild(ejectMenuItem);
  146. APP.translation.translateElement($(popupmenuElement));
  147. return popupmenuElement;
  148. };
  149. /**
  150. * Updates the remote video menu.
  151. *
  152. * @param isMuted the new muted state to update to
  153. * @param force to work even if popover is not visible
  154. */
  155. RemoteVideo.prototype.updateRemoteVideoMenu = function (isMuted, force) {
  156. this.isAudioMuted = isMuted;
  157. // generate content, translate it and add it to document only if
  158. // popover is visible or we force to do so.
  159. if(this.popover.popoverShown || force) {
  160. this.popover.updateContent(this._generatePopupContent());
  161. }
  162. };
  163. /**
  164. * @inheritDoc
  165. */
  166. RemoteVideo.prototype.setMutedView = function(isMuted) {
  167. SmallVideo.prototype.setMutedView.call(this, isMuted);
  168. // Update 'mutedWhileDisconnected' flag
  169. this._figureOutMutedWhileDisconnected(this.isConnectionActive() === false);
  170. };
  171. /**
  172. * Figures out the value of {@link #mutedWhileDisconnected} flag by taking into
  173. * account remote participant's network connectivity and video muted status.
  174. *
  175. * @param {boolean} isDisconnected <tt>true</tt> if the remote participant is
  176. * currently having connectivity issues or <tt>false</tt> otherwise.
  177. *
  178. * @private
  179. */
  180. RemoteVideo.prototype._figureOutMutedWhileDisconnected
  181. = function(isDisconnected) {
  182. if (isDisconnected && this.isVideoMuted) {
  183. this.mutedWhileDisconnected = true;
  184. } else if (!isDisconnected && !this.isVideoMuted) {
  185. this.mutedWhileDisconnected = false;
  186. }
  187. };
  188. /**
  189. * Adds the remote video menu element for the given <tt>id</tt> in the
  190. * given <tt>parentElement</tt>.
  191. *
  192. * @param id the id indicating the video for which we're adding a menu.
  193. * @param parentElement the parent element where this menu will be added
  194. */
  195. if (!interfaceConfig.filmStripOnly) {
  196. RemoteVideo.prototype.addRemoteVideoMenu = function () {
  197. var spanElement = document.createElement('span');
  198. spanElement.className = 'remotevideomenu';
  199. this.container.appendChild(spanElement);
  200. var menuElement = document.createElement('i');
  201. menuElement.className = 'icon-menu-up';
  202. menuElement.title = 'Remote user controls';
  203. spanElement.appendChild(menuElement);
  204. this._initPopupMenu(this._generatePopupContent());
  205. this.hasRemoteVideoMenu = true;
  206. };
  207. } else {
  208. RemoteVideo.prototype.addRemoteVideoMenu = function() {};
  209. }
  210. /**
  211. * Removes the remote stream element corresponding to the given stream and
  212. * parent container.
  213. *
  214. * @param stream the MediaStream
  215. * @param isVideo <tt>true</tt> if given <tt>stream</tt> is a video one.
  216. */
  217. RemoteVideo.prototype.removeRemoteStreamElement = function (stream) {
  218. if (!this.container)
  219. return false;
  220. var isVideo = stream.isVideoTrack();
  221. var elementID = SmallVideo.getStreamElementID(stream);
  222. var select = $('#' + elementID);
  223. select.remove();
  224. if (isVideo) {
  225. this.wasVideoPlayed = false;
  226. }
  227. console.info((isVideo ? "Video" : "Audio") +
  228. " removed " + this.id, select);
  229. // when removing only the video element and we are on stage
  230. // update the stage
  231. if (isVideo && this.isCurrentlyOnLargeVideo())
  232. this.VideoLayout.updateLargeVideo(this.id);
  233. else
  234. // Missing video stream will affect display mode
  235. this.updateView();
  236. };
  237. /**
  238. * Checks whether the remote user associated with this <tt>RemoteVideo</tt>
  239. * has connectivity issues.
  240. *
  241. * @return {boolean} <tt>true</tt> if the user's connection is fine or
  242. * <tt>false</tt> otherwise.
  243. */
  244. RemoteVideo.prototype.isConnectionActive = function() {
  245. return this.user.isConnectionActive();
  246. };
  247. /**
  248. * The remote video is considered "playable" once the stream has started
  249. * according to the {@link #hasVideoStarted} result.
  250. *
  251. * @inheritdoc
  252. * @override
  253. */
  254. RemoteVideo.prototype.isVideoPlayable = function () {
  255. return SmallVideo.prototype.isVideoPlayable.call(this)
  256. && this.hasVideoStarted() && !this.mutedWhileDisconnected;
  257. };
  258. /**
  259. * @inheritDoc
  260. */
  261. RemoteVideo.prototype.updateView = function () {
  262. this.updateConnectionStatusIndicator(
  263. null /* will obtain the status from 'conference' */);
  264. // This must be called after 'updateConnectionStatusIndicator' because it
  265. // affects the display mode by modifying 'mutedWhileDisconnected' flag
  266. SmallVideo.prototype.updateView.call(this);
  267. };
  268. /**
  269. * Updates the UI to reflect user's connectivity status.
  270. * @param isActive {boolean|null} 'true' if user's connection is active or
  271. * 'false' when the use is having some connectivity issues and a warning
  272. * should be displayed. When 'null' is passed then the current value will be
  273. * obtained from the conference instance.
  274. */
  275. RemoteVideo.prototype.updateConnectionStatusIndicator = function (isActive) {
  276. // Check for initial value if 'isActive' is not defined
  277. if (typeof isActive !== "boolean") {
  278. isActive = this.isConnectionActive();
  279. if (isActive === null) {
  280. // Cancel processing at this point - no update
  281. return;
  282. }
  283. }
  284. console.debug(this.id + " thumbnail is connection active ? " + isActive);
  285. // Update 'mutedWhileDisconnected' flag
  286. this._figureOutMutedWhileDisconnected(!isActive);
  287. if(this.connectionIndicator)
  288. this.connectionIndicator.updateConnectionStatusIndicator(isActive);
  289. // Toggle thumbnail video problem filter
  290. this.selectVideoElement().toggleClass(
  291. "videoThumbnailProblemFilter", !isActive);
  292. this.$avatar().toggleClass(
  293. "videoThumbnailProblemFilter", !isActive);
  294. };
  295. /**
  296. * Removes RemoteVideo from the page.
  297. */
  298. RemoteVideo.prototype.remove = function () {
  299. console.log("Remove thumbnail", this.id);
  300. this.removeConnectionIndicator();
  301. // Make sure that the large video is updated if are removing its
  302. // corresponding small video.
  303. this.VideoLayout.updateAfterThumbRemoved(this.id);
  304. // Remove whole container
  305. if (this.container.parentNode) {
  306. this.container.parentNode.removeChild(this.container);
  307. }
  308. };
  309. RemoteVideo.prototype.waitForPlayback = function (streamElement, stream) {
  310. var webRtcStream = stream.getOriginalStream();
  311. var isVideo = stream.isVideoTrack();
  312. if (!isVideo || webRtcStream.id === 'mixedmslabel') {
  313. return;
  314. }
  315. var self = this;
  316. // Register 'onplaying' listener to trigger 'videoactive' on VideoLayout
  317. // when video playback starts
  318. var onPlayingHandler = function () {
  319. self.wasVideoPlayed = true;
  320. self.VideoLayout.videoactive(streamElement, self.id);
  321. streamElement.onplaying = null;
  322. // Refresh to show the video
  323. self.updateView();
  324. };
  325. streamElement.onplaying = onPlayingHandler;
  326. };
  327. /**
  328. * Checks whether the video stream has started for this RemoteVideo instance.
  329. *
  330. * @returns {boolean} true if this RemoteVideo has a video stream for which
  331. * the playback has been started.
  332. */
  333. RemoteVideo.prototype.hasVideoStarted = function () {
  334. return this.wasVideoPlayed;
  335. };
  336. RemoteVideo.prototype.addRemoteStreamElement = function (stream) {
  337. if (!this.container) {
  338. return;
  339. }
  340. let isVideo = stream.isVideoTrack();
  341. isVideo ? this.videoStream = stream : this.audioStream = stream;
  342. if (isVideo)
  343. this.setVideoType(stream.videoType);
  344. // Add click handler.
  345. let onClickHandler = (event) => {
  346. let source = event.target || event.srcElement;
  347. // ignore click if it was done in popup menu
  348. if ($(source).parents('.popupmenu').length === 0) {
  349. this.VideoLayout.handleVideoThumbClicked(this.id);
  350. }
  351. // On IE we need to populate this handler on video <object>
  352. // and it does not give event instance as an argument,
  353. // so we check here for methods.
  354. if (event.stopPropagation && event.preventDefault) {
  355. event.stopPropagation();
  356. event.preventDefault();
  357. }
  358. return false;
  359. };
  360. this.container.onclick = onClickHandler;
  361. if(!stream.getOriginalStream())
  362. return;
  363. let streamElement = SmallVideo.createStreamElement(stream);
  364. // Put new stream element always in front
  365. UIUtils.prependChild(this.container, streamElement);
  366. // If we hide element when Temasys plugin is used then
  367. // we'll never receive 'onplay' event and other logic won't work as expected
  368. // NOTE: hiding will not have effect when Temasys plugin is in use, as
  369. // calling attach will show it back
  370. $(streamElement).hide();
  371. // If the container is currently visible
  372. // we attach the stream to the element.
  373. if (!isVideo || (this.container.offsetParent !== null && isVideo)) {
  374. this.waitForPlayback(streamElement, stream);
  375. streamElement = stream.attach(streamElement);
  376. }
  377. $(streamElement).click(onClickHandler);
  378. },
  379. /**
  380. * Show/hide peer container for the given id.
  381. */
  382. RemoteVideo.prototype.showPeerContainer = function (state) {
  383. if (!this.container)
  384. return;
  385. var isHide = state === 'hide';
  386. var resizeThumbnails = false;
  387. if (!isHide) {
  388. if (!$(this.container).is(':visible')) {
  389. resizeThumbnails = true;
  390. $(this.container).show();
  391. }
  392. // Call updateView, so that we'll figure out if avatar
  393. // should be displayed based on video muted status and whether or not
  394. // it's in the lastN set
  395. this.updateView();
  396. }
  397. else if ($(this.container).is(':visible') && isHide)
  398. {
  399. resizeThumbnails = true;
  400. $(this.container).hide();
  401. if(this.connectionIndicator)
  402. this.connectionIndicator.hide();
  403. }
  404. if (resizeThumbnails) {
  405. this.VideoLayout.resizeThumbnails();
  406. }
  407. // We want to be able to pin a participant from the contact list, even
  408. // if he's not in the lastN set!
  409. // ContactList.setClickable(id, !isHide);
  410. };
  411. RemoteVideo.prototype.updateResolution = function (resolution) {
  412. if (this.connectionIndicator) {
  413. this.connectionIndicator.updateResolution(resolution);
  414. }
  415. };
  416. RemoteVideo.prototype.removeConnectionIndicator = function () {
  417. if (this.connectionIndicator)
  418. this.connectionIndicator.remove();
  419. };
  420. RemoteVideo.prototype.hideConnectionIndicator = function () {
  421. if (this.connectionIndicator)
  422. this.connectionIndicator.hide();
  423. };
  424. /**
  425. * Sets the display name for the given video span id.
  426. *
  427. * @param displayName the display name to set
  428. */
  429. RemoteVideo.prototype.setDisplayName = function(displayName) {
  430. if (!this.container) {
  431. console.warn( "Unable to set displayName - " + this.videoSpanId +
  432. " does not exist");
  433. return;
  434. }
  435. var nameSpan = $('#' + this.videoSpanId + ' .displayname');
  436. // If we already have a display name for this video.
  437. if (nameSpan.length > 0) {
  438. if (displayName && displayName.length > 0) {
  439. var displaynameSpan = $('#' + this.videoSpanId + '_name');
  440. if (displaynameSpan.text() !== displayName)
  441. displaynameSpan.text(displayName);
  442. }
  443. else
  444. $('#' + this.videoSpanId + '_name').text(
  445. interfaceConfig.DEFAULT_REMOTE_DISPLAY_NAME);
  446. } else {
  447. nameSpan = document.createElement('span');
  448. nameSpan.className = 'displayname';
  449. $('#' + this.videoSpanId)[0]
  450. .appendChild(nameSpan);
  451. if (displayName && displayName.length > 0) {
  452. $(nameSpan).text(displayName);
  453. } else {
  454. nameSpan.innerHTML = interfaceConfig.DEFAULT_REMOTE_DISPLAY_NAME;
  455. }
  456. nameSpan.id = this.videoSpanId + '_name';
  457. }
  458. };
  459. /**
  460. * Removes remote video menu element from video element identified by
  461. * given <tt>videoElementId</tt>.
  462. *
  463. * @param videoElementId the id of local or remote video element.
  464. */
  465. RemoteVideo.prototype.removeRemoteVideoMenu = function() {
  466. var menuSpan = $('#' + this.videoSpanId + '> .remotevideomenu');
  467. if (menuSpan.length) {
  468. this.popover.forceHide();
  469. menuSpan.remove();
  470. this.hasRemoteVideoMenu = false;
  471. }
  472. };
  473. RemoteVideo.createContainer = function (spanId) {
  474. let container = document.createElement('span');
  475. container.id = spanId;
  476. container.className = 'videocontainer';
  477. let indicatorBar = document.createElement('div');
  478. indicatorBar.className = "videocontainer__toptoolbar";
  479. container.appendChild(indicatorBar);
  480. let toolbar = document.createElement('div');
  481. toolbar.className = "videocontainer__toolbar";
  482. container.appendChild(toolbar);
  483. let overlay = document.createElement('div');
  484. overlay.className = "videocontainer__hoverOverlay";
  485. container.appendChild(overlay);
  486. var remotes = document.getElementById('remoteVideos');
  487. return remotes.appendChild(container);
  488. };
  489. /**
  490. * Shows 2 button dialog for confirmation from the user for muting remote
  491. * participant.
  492. */
  493. RemoteVideo.showMuteParticipantDialog = function () {
  494. //FIXME: don't show again checkbox is implemented very dirty. we should add
  495. // this functionality to MessageHandler class.
  496. if (window.localStorage
  497. && window.localStorage.getItem(
  498. "dontShowMuteParticipantDialog") === "true") {
  499. return Promise.resolve(MUTED_DIALOG_BUTTON_VALUES.muted);
  500. }
  501. let msgString =
  502. `<div data-i18n="dialog.muteParticipantBody"></div>
  503. <br />
  504. <label>
  505. <input type='checkbox' checked id='doNotShowMessageAgain' />
  506. <span data-i18n='dialog.doNotShowMessageAgain'></span>
  507. </label>`;
  508. return new Promise(resolve => {
  509. APP.UI.messageHandler.openTwoButtonDialog({
  510. titleKey : "dialog.muteParticipantTitle",
  511. msgString,
  512. leftButtonKey: 'dialog.muteParticipantButton',
  513. submitFunction: () => {
  514. if(window.localStorage) {
  515. let form = $.prompt.getPrompt();
  516. if (form) {
  517. let input = form.find("#doNotShowMessageAgain");
  518. if (input.length) {
  519. window.localStorage.setItem(
  520. "dontShowMuteParticipantDialog",
  521. input.prop("checked"));
  522. }
  523. }
  524. }
  525. resolve(MUTED_DIALOG_BUTTON_VALUES.muted);
  526. },
  527. closeFunction: () => resolve(MUTED_DIALOG_BUTTON_VALUES.cancel)
  528. });
  529. });
  530. };
  531. export default RemoteVideo;