Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

RemoteVideo.js 21KB

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