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

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