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 21KB

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