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

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