You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

RemoteVideo.js 20KB

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