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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683
  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. // override popover hide method so we can cleanup click handlers
  96. let origHideFunc = this.popover.forceHide;
  97. this.popover.forceHide = function () {
  98. $(document).off("click", '#mutelink_' + this.id);
  99. $(document).off("click", '#ejectlink_' + this.id);
  100. origHideFunc.call(this.popover);
  101. }.bind(this);
  102. };
  103. /**
  104. * Checks whether current video is considered hovered. Currently it is hovered
  105. * if the mouse is over the video, or if the connection indicator or the popup
  106. * menu is shown(hovered).
  107. * @private
  108. * NOTE: extends SmallVideo's method
  109. */
  110. RemoteVideo.prototype._isHovered = function () {
  111. let isHovered = SmallVideo.prototype._isHovered.call(this)
  112. || this.popupMenuIsHovered;
  113. return isHovered;
  114. };
  115. /**
  116. * Generates the popup menu content.
  117. *
  118. * @returns {Element|*} the constructed element, containing popup menu items
  119. * @private
  120. */
  121. RemoteVideo.prototype._generatePopupContent = function () {
  122. let popupmenuElement = document.createElement('ul');
  123. popupmenuElement.className = 'popupmenu';
  124. popupmenuElement.id = `remote_popupmenu_${this.id}`;
  125. let muteTranslationKey;
  126. let muteClassName;
  127. if (this.isAudioMuted) {
  128. muteTranslationKey = 'videothumbnail.muted';
  129. muteClassName = 'mutelink disabled';
  130. } else {
  131. muteTranslationKey = 'videothumbnail.domute';
  132. muteClassName = 'mutelink';
  133. }
  134. let muteHandler = this._muteHandler.bind(this);
  135. let kickHandler = this._kickHandler.bind(this);
  136. let menuItems = [
  137. {
  138. id: 'mutelink_' + this.id,
  139. handler: muteHandler,
  140. icon: 'icon-mic-disabled',
  141. className: muteClassName,
  142. data: {
  143. i18n: muteTranslationKey
  144. }
  145. }, {
  146. id: 'ejectlink_' + this.id,
  147. handler: kickHandler,
  148. icon: 'icon-kick',
  149. data: {
  150. i18n: 'videothumbnail.kick'
  151. }
  152. }
  153. ];
  154. menuItems.forEach(el => {
  155. let menuItem = this._generatePopupMenuItem(el);
  156. popupmenuElement.appendChild(menuItem);
  157. });
  158. APP.translation.translateElement($(popupmenuElement));
  159. return popupmenuElement;
  160. };
  161. RemoteVideo.prototype._muteHandler = function () {
  162. if (this.isAudioMuted)
  163. return;
  164. RemoteVideo.showMuteParticipantDialog().then(reason => {
  165. if(reason === MUTED_DIALOG_BUTTON_VALUES.muted) {
  166. this.emitter.emit(UIEvents.REMOTE_AUDIO_MUTED, this.id);
  167. }
  168. }).catch(e => {
  169. //currently shouldn't be called
  170. console.error(e);
  171. });
  172. this.popover.forceHide();
  173. };
  174. RemoteVideo.prototype._kickHandler = function () {
  175. this.emitter.emit(UIEvents.USER_KICKED, this.id);
  176. this.popover.forceHide();
  177. };
  178. RemoteVideo.prototype._generatePopupMenuItem = function (opts = {}) {
  179. let {
  180. id,
  181. handler,
  182. icon,
  183. data,
  184. className
  185. } = opts;
  186. handler = handler || $.noop;
  187. let menuItem = document.createElement('li');
  188. menuItem.className = 'popupmenu__item';
  189. let linkItem = document.createElement('a');
  190. linkItem.className = 'popupmenu__link';
  191. if (className) {
  192. linkItem.className += ` ${className}`;
  193. }
  194. if (icon) {
  195. let indicator = document.createElement('span');
  196. indicator.className = 'popupmenu__icon';
  197. indicator.innerHTML = `<i class="${icon}"></i>`;
  198. linkItem.appendChild(indicator);
  199. }
  200. let textContent = document.createElement('span');
  201. textContent.className = 'popupmenu__text';
  202. if (data) {
  203. let dataKeys = Object.keys(data);
  204. dataKeys.forEach(key => {
  205. textContent.dataset[key] = data[key];
  206. });
  207. }
  208. linkItem.appendChild(textContent);
  209. linkItem.id = id;
  210. // Delegate event to the document.
  211. $(document).on("click", `#${id}`, handler);
  212. menuItem.appendChild(linkItem);
  213. return menuItem;
  214. };
  215. /**
  216. * Updates the remote video menu.
  217. *
  218. * @param isMuted the new muted state to update to
  219. * @param force to work even if popover is not visible
  220. */
  221. RemoteVideo.prototype.updateRemoteVideoMenu = function (isMuted, force) {
  222. this.isAudioMuted = isMuted;
  223. // generate content, translate it and add it to document only if
  224. // popover is visible or we force to do so.
  225. if(this.popover.popoverShown || force) {
  226. this.popover.updateContent(this._generatePopupContent());
  227. }
  228. };
  229. /**
  230. * @inheritDoc
  231. */
  232. RemoteVideo.prototype.setMutedView = function(isMuted) {
  233. SmallVideo.prototype.setMutedView.call(this, isMuted);
  234. // Update 'mutedWhileDisconnected' flag
  235. this._figureOutMutedWhileDisconnected(this.isConnectionActive() === false);
  236. };
  237. /**
  238. * Figures out the value of {@link #mutedWhileDisconnected} flag by taking into
  239. * account remote participant's network connectivity and video muted status.
  240. *
  241. * @param {boolean} isDisconnected <tt>true</tt> if the remote participant is
  242. * currently having connectivity issues or <tt>false</tt> otherwise.
  243. *
  244. * @private
  245. */
  246. RemoteVideo.prototype._figureOutMutedWhileDisconnected
  247. = function(isDisconnected) {
  248. if (isDisconnected && this.isVideoMuted) {
  249. this.mutedWhileDisconnected = true;
  250. } else if (!isDisconnected && !this.isVideoMuted) {
  251. this.mutedWhileDisconnected = false;
  252. }
  253. };
  254. /**
  255. * Adds the remote video menu element for the given <tt>id</tt> in the
  256. * given <tt>parentElement</tt>.
  257. *
  258. * @param id the id indicating the video for which we're adding a menu.
  259. * @param parentElement the parent element where this menu will be added
  260. */
  261. if (!interfaceConfig.filmStripOnly) {
  262. RemoteVideo.prototype.addRemoteVideoMenu = function () {
  263. var spanElement = document.createElement('span');
  264. spanElement.className = 'remotevideomenu';
  265. this.container.appendChild(spanElement);
  266. var menuElement = document.createElement('i');
  267. menuElement.className = 'icon-menu';
  268. menuElement.title = 'Remote user controls';
  269. spanElement.appendChild(menuElement);
  270. this._initPopupMenu(this._generatePopupContent());
  271. this.hasRemoteVideoMenu = true;
  272. };
  273. } else {
  274. RemoteVideo.prototype.addRemoteVideoMenu = function() {};
  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. console.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. console.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. console.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. console.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;