You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

RemoteVideo.js 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672
  1. /* global $, APP, interfaceConfig */
  2. /* eslint-disable no-unused-vars */
  3. import React from 'react';
  4. import ReactDOM from 'react-dom';
  5. import { Provider } from 'react-redux';
  6. import { I18nextProvider } from 'react-i18next';
  7. import { AtlasKitThemeProvider } from '@atlaskit/theme';
  8. import { i18next } from '../../../react/features/base/i18n';
  9. import {
  10. JitsiParticipantConnectionStatus
  11. } from '../../../react/features/base/lib-jitsi-meet';
  12. import {
  13. getPinnedParticipant,
  14. pinParticipant
  15. } from '../../../react/features/base/participants';
  16. import { PresenceLabel } from '../../../react/features/presence-status';
  17. import {
  18. REMOTE_CONTROL_MENU_STATES,
  19. RemoteVideoMenuTriggerButton
  20. } from '../../../react/features/remote-video-menu';
  21. import {
  22. LAYOUTS,
  23. getCurrentLayout,
  24. shouldDisplayTileView
  25. } from '../../../react/features/video-layout';
  26. /* eslint-enable no-unused-vars */
  27. const logger = require('jitsi-meet-logger').getLogger(__filename);
  28. import SmallVideo from './SmallVideo';
  29. import UIUtils from '../util/UIUtil';
  30. /**
  31. * Creates new instance of the <tt>RemoteVideo</tt>.
  32. * @param user {JitsiParticipant} the user for whom remote video instance will
  33. * be created.
  34. * @param {VideoLayout} VideoLayout the video layout instance.
  35. * @param {EventEmitter} emitter the event emitter which will be used by
  36. * the new instance to emit events.
  37. * @constructor
  38. */
  39. function RemoteVideo(user, VideoLayout, emitter) {
  40. this.user = user;
  41. this.id = user.getId();
  42. this.emitter = emitter;
  43. this.videoSpanId = `participant_${this.id}`;
  44. SmallVideo.call(this, VideoLayout);
  45. this._audioStreamElement = null;
  46. this._supportsRemoteControl = false;
  47. this.statsPopoverLocation = interfaceConfig.VERTICAL_FILMSTRIP
  48. ? 'left bottom' : 'top center';
  49. this.addRemoteVideoContainer();
  50. this.updateIndicators();
  51. this.setDisplayName();
  52. this.bindHoverHandler();
  53. this.flipX = false;
  54. this.isLocal = false;
  55. this.popupMenuIsHovered = false;
  56. this._isRemoteControlSessionActive = false;
  57. /**
  58. * The flag is set to <tt>true</tt> after the 'onplay' event has been
  59. * triggered on the current video element. It goes back to <tt>false</tt>
  60. * when the stream is removed. It is used to determine whether the video
  61. * playback has ever started.
  62. * @type {boolean}
  63. */
  64. this.wasVideoPlayed = false;
  65. /**
  66. * The flag is set to <tt>true</tt> if remote participant's video gets muted
  67. * during his media connection disruption. This is to prevent black video
  68. * being render on the thumbnail, because even though once the video has
  69. * been played the image usually remains on the video element it seems that
  70. * after longer period of the video element being hidden this image can be
  71. * lost.
  72. * @type {boolean}
  73. */
  74. this.mutedWhileDisconnected = false;
  75. // Bind event handlers so they are only bound once for every instance.
  76. // TODO The event handlers should be turned into actions so changes can be
  77. // handled through reducers and middleware.
  78. this._requestRemoteControlPermissions
  79. = this._requestRemoteControlPermissions.bind(this);
  80. this._setAudioVolume = this._setAudioVolume.bind(this);
  81. this._stopRemoteControl = this._stopRemoteControl.bind(this);
  82. this.container.onclick = this._onContainerClick.bind(this);
  83. }
  84. RemoteVideo.prototype = Object.create(SmallVideo.prototype);
  85. RemoteVideo.prototype.constructor = RemoteVideo;
  86. RemoteVideo.prototype.addRemoteVideoContainer = function() {
  87. this.container = RemoteVideo.createContainer(this.videoSpanId);
  88. this.$container = $(this.container);
  89. this.initBrowserSpecificProperties();
  90. this.updateRemoteVideoMenu();
  91. this.VideoLayout.resizeThumbnails(true);
  92. this.addAudioLevelIndicator();
  93. this.addPresenceLabel();
  94. return this.container;
  95. };
  96. /**
  97. * Checks whether current video is considered hovered. Currently it is hovered
  98. * if the mouse is over the video, or if the connection indicator or the popup
  99. * menu is shown(hovered).
  100. * @private
  101. * NOTE: extends SmallVideo's method
  102. */
  103. RemoteVideo.prototype._isHovered = function() {
  104. const isHovered = SmallVideo.prototype._isHovered.call(this)
  105. || this.popupMenuIsHovered;
  106. return isHovered;
  107. };
  108. /**
  109. * Generates the popup menu content.
  110. *
  111. * @returns {Element|*} the constructed element, containing popup menu items
  112. * @private
  113. */
  114. RemoteVideo.prototype._generatePopupContent = function() {
  115. if (interfaceConfig.filmStripOnly) {
  116. return;
  117. }
  118. const remoteVideoMenuContainer
  119. = this.container.querySelector('.remotevideomenu');
  120. if (!remoteVideoMenuContainer) {
  121. return;
  122. }
  123. const { controller } = APP.remoteControl;
  124. let remoteControlState = null;
  125. let onRemoteControlToggle;
  126. if (this._supportsRemoteControl
  127. && ((!APP.remoteControl.active && !this._isRemoteControlSessionActive)
  128. || APP.remoteControl.controller.activeParticipant === this.id)) {
  129. if (controller.getRequestedParticipant() === this.id) {
  130. remoteControlState = REMOTE_CONTROL_MENU_STATES.REQUESTING;
  131. } else if (controller.isStarted()) {
  132. onRemoteControlToggle = this._stopRemoteControl;
  133. remoteControlState = REMOTE_CONTROL_MENU_STATES.STARTED;
  134. } else {
  135. onRemoteControlToggle = this._requestRemoteControlPermissions;
  136. remoteControlState = REMOTE_CONTROL_MENU_STATES.NOT_STARTED;
  137. }
  138. }
  139. const initialVolumeValue
  140. = this._audioStreamElement && this._audioStreamElement.volume;
  141. const onVolumeChange = this._setAudioVolume;
  142. const { isModerator } = APP.conference;
  143. const participantID = this.id;
  144. const currentLayout = getCurrentLayout(APP.store.getState());
  145. let remoteMenuPosition;
  146. if (currentLayout === LAYOUTS.TILE_VIEW) {
  147. remoteMenuPosition = 'left top';
  148. } else if (currentLayout === LAYOUTS.VERTICAL_FILMSTRIP_VIEW) {
  149. remoteMenuPosition = 'left bottom';
  150. } else {
  151. remoteMenuPosition = 'top center';
  152. }
  153. ReactDOM.render(
  154. <Provider store = { APP.store }>
  155. <I18nextProvider i18n = { i18next }>
  156. <AtlasKitThemeProvider mode = 'dark'>
  157. <RemoteVideoMenuTriggerButton
  158. initialVolumeValue = { initialVolumeValue }
  159. isAudioMuted = { this.isAudioMuted }
  160. isModerator = { isModerator }
  161. menuPosition = { remoteMenuPosition }
  162. onMenuDisplay
  163. = {this._onRemoteVideoMenuDisplay.bind(this)}
  164. onRemoteControlToggle = { onRemoteControlToggle }
  165. onVolumeChange = { onVolumeChange }
  166. participantID = { participantID }
  167. remoteControlState = { remoteControlState } />
  168. </AtlasKitThemeProvider>
  169. </I18nextProvider>
  170. </Provider>,
  171. remoteVideoMenuContainer);
  172. };
  173. RemoteVideo.prototype._onRemoteVideoMenuDisplay = function() {
  174. this.updateRemoteVideoMenu();
  175. };
  176. /**
  177. * Sets the remote control active status for the remote video.
  178. *
  179. * @param {boolean} isActive - The new remote control active status.
  180. * @returns {void}
  181. */
  182. RemoteVideo.prototype.setRemoteControlActiveStatus = function(isActive) {
  183. this._isRemoteControlSessionActive = isActive;
  184. this.updateRemoteVideoMenu();
  185. };
  186. /**
  187. * Sets the remote control supported value and initializes or updates the menu
  188. * depending on the remote control is supported or not.
  189. * @param {boolean} isSupported
  190. */
  191. RemoteVideo.prototype.setRemoteControlSupport = function(isSupported = false) {
  192. if (this._supportsRemoteControl === isSupported) {
  193. return;
  194. }
  195. this._supportsRemoteControl = isSupported;
  196. this.updateRemoteVideoMenu();
  197. };
  198. /**
  199. * Requests permissions for remote control session.
  200. */
  201. RemoteVideo.prototype._requestRemoteControlPermissions = function() {
  202. APP.remoteControl.controller.requestPermissions(
  203. this.id, this.VideoLayout.getLargeVideoWrapper()).then(result => {
  204. if (result === null) {
  205. return;
  206. }
  207. this.updateRemoteVideoMenu();
  208. APP.UI.messageHandler.notify(
  209. 'dialog.remoteControlTitle',
  210. result === false ? 'dialog.remoteControlDeniedMessage'
  211. : 'dialog.remoteControlAllowedMessage',
  212. { user: this.user.getDisplayName()
  213. || interfaceConfig.DEFAULT_REMOTE_DISPLAY_NAME }
  214. );
  215. if (result === true) {
  216. // the remote control permissions has been granted
  217. // pin the controlled participant
  218. const pinnedParticipant
  219. = getPinnedParticipant(APP.store.getState()) || {};
  220. const pinnedId = pinnedParticipant.id;
  221. if (pinnedId !== this.id) {
  222. APP.store.dispatch(pinParticipant(this.id));
  223. }
  224. }
  225. }, error => {
  226. logger.error(error);
  227. this.updateRemoteVideoMenu();
  228. APP.UI.messageHandler.notify(
  229. 'dialog.remoteControlTitle',
  230. 'dialog.remoteControlErrorMessage',
  231. { user: this.user.getDisplayName()
  232. || interfaceConfig.DEFAULT_REMOTE_DISPLAY_NAME }
  233. );
  234. });
  235. this.updateRemoteVideoMenu();
  236. };
  237. /**
  238. * Stops remote control session.
  239. */
  240. RemoteVideo.prototype._stopRemoteControl = function() {
  241. // send message about stopping
  242. APP.remoteControl.controller.stop();
  243. this.updateRemoteVideoMenu();
  244. };
  245. /**
  246. * Change the remote participant's volume level.
  247. *
  248. * @param {int} newVal - The value to set the slider to.
  249. */
  250. RemoteVideo.prototype._setAudioVolume = function(newVal) {
  251. if (this._audioStreamElement) {
  252. this._audioStreamElement.volume = newVal;
  253. }
  254. };
  255. /**
  256. * Updates the remote video menu.
  257. *
  258. * @param isMuted the new muted state to update to
  259. */
  260. RemoteVideo.prototype.updateRemoteVideoMenu = function(isMuted) {
  261. if (typeof isMuted !== 'undefined') {
  262. this.isAudioMuted = isMuted;
  263. }
  264. this._generatePopupContent();
  265. };
  266. /**
  267. * @inheritDoc
  268. * @override
  269. */
  270. RemoteVideo.prototype.setVideoMutedView = function(isMuted) {
  271. SmallVideo.prototype.setVideoMutedView.call(this, isMuted);
  272. // Update 'mutedWhileDisconnected' flag
  273. this._figureOutMutedWhileDisconnected();
  274. };
  275. /**
  276. * Figures out the value of {@link #mutedWhileDisconnected} flag by taking into
  277. * account remote participant's network connectivity and video muted status.
  278. *
  279. * @private
  280. */
  281. RemoteVideo.prototype._figureOutMutedWhileDisconnected = function() {
  282. const isActive = this.isConnectionActive();
  283. if (!isActive && this.isVideoMuted) {
  284. this.mutedWhileDisconnected = true;
  285. } else if (isActive && !this.isVideoMuted) {
  286. this.mutedWhileDisconnected = false;
  287. }
  288. };
  289. /**
  290. * Removes the remote stream element corresponding to the given stream and
  291. * parent container.
  292. *
  293. * @param stream the MediaStream
  294. * @param isVideo <tt>true</tt> if given <tt>stream</tt> is a video one.
  295. */
  296. RemoteVideo.prototype.removeRemoteStreamElement = function(stream) {
  297. if (!this.container) {
  298. return false;
  299. }
  300. const isVideo = stream.isVideoTrack();
  301. const elementID = SmallVideo.getStreamElementID(stream);
  302. const select = $(`#${elementID}`);
  303. select.remove();
  304. if (isVideo) {
  305. this.wasVideoPlayed = false;
  306. }
  307. logger.info(`${isVideo ? 'Video' : 'Audio'
  308. } removed ${this.id}`, select);
  309. this.updateView();
  310. };
  311. /**
  312. * Checks whether the remote user associated with this <tt>RemoteVideo</tt>
  313. * has connectivity issues.
  314. *
  315. * @return {boolean} <tt>true</tt> if the user's connection is fine or
  316. * <tt>false</tt> otherwise.
  317. */
  318. RemoteVideo.prototype.isConnectionActive = function() {
  319. return this.user.getConnectionStatus()
  320. === JitsiParticipantConnectionStatus.ACTIVE;
  321. };
  322. /**
  323. * The remote video is considered "playable" once the stream has started
  324. * according to the {@link #hasVideoStarted} result.
  325. * It will be allowed to display video also in
  326. * {@link JitsiParticipantConnectionStatus.INTERRUPTED} if the video was ever
  327. * played and was not muted while not in ACTIVE state. This basically means
  328. * that there is stalled video image cached that could be displayed. It's used
  329. * to show "grey video image" in user's thumbnail when there are connectivity
  330. * issues.
  331. *
  332. * @inheritdoc
  333. * @override
  334. */
  335. RemoteVideo.prototype.isVideoPlayable = function() {
  336. const connectionState
  337. = APP.conference.getParticipantConnectionStatus(this.id);
  338. return SmallVideo.prototype.isVideoPlayable.call(this)
  339. && this.hasVideoStarted()
  340. && (connectionState === JitsiParticipantConnectionStatus.ACTIVE
  341. || (connectionState === JitsiParticipantConnectionStatus.INTERRUPTED
  342. && !this.mutedWhileDisconnected));
  343. };
  344. /**
  345. * @inheritDoc
  346. */
  347. RemoteVideo.prototype.updateView = function() {
  348. this.$container.toggleClass('audio-only', APP.conference.isAudioOnly());
  349. this.updateConnectionStatusIndicator();
  350. // This must be called after 'updateConnectionStatusIndicator' because it
  351. // affects the display mode by modifying 'mutedWhileDisconnected' flag
  352. SmallVideo.prototype.updateView.call(this);
  353. };
  354. /**
  355. * Updates the UI to reflect user's connectivity status.
  356. */
  357. RemoteVideo.prototype.updateConnectionStatusIndicator = function() {
  358. const connectionStatus = this.user.getConnectionStatus();
  359. logger.debug(`${this.id} thumbnail connection status: ${connectionStatus}`);
  360. // FIXME rename 'mutedWhileDisconnected' to 'mutedWhileNotRendering'
  361. // Update 'mutedWhileDisconnected' flag
  362. this._figureOutMutedWhileDisconnected();
  363. this.updateConnectionStatus(connectionStatus);
  364. const isInterrupted
  365. = connectionStatus === JitsiParticipantConnectionStatus.INTERRUPTED;
  366. // Toggle thumbnail video problem filter
  367. this.selectVideoElement().toggleClass(
  368. 'videoThumbnailProblemFilter', isInterrupted);
  369. this.$avatar().toggleClass(
  370. 'videoThumbnailProblemFilter', isInterrupted);
  371. };
  372. /**
  373. * Removes RemoteVideo from the page.
  374. */
  375. RemoteVideo.prototype.remove = function() {
  376. logger.log('Remove thumbnail', this.id);
  377. this.removeAudioLevelIndicator();
  378. const toolbarContainer
  379. = this.container.querySelector('.videocontainer__toolbar');
  380. if (toolbarContainer) {
  381. ReactDOM.unmountComponentAtNode(toolbarContainer);
  382. }
  383. this.removeConnectionIndicator();
  384. this.removeDisplayName();
  385. this.removeAvatar();
  386. this.removePresenceLabel();
  387. this._unmountIndicators();
  388. this.removeRemoteVideoMenu();
  389. // Remove whole container
  390. if (this.container.parentNode) {
  391. this.container.parentNode.removeChild(this.container);
  392. }
  393. };
  394. RemoteVideo.prototype.waitForPlayback = function(streamElement, stream) {
  395. const webRtcStream = stream.getOriginalStream();
  396. const isVideo = stream.isVideoTrack();
  397. if (!isVideo || webRtcStream.id === 'mixedmslabel') {
  398. return;
  399. }
  400. const self = this;
  401. // Triggers when video playback starts
  402. const onPlayingHandler = function() {
  403. self.wasVideoPlayed = true;
  404. self.VideoLayout.remoteVideoActive(streamElement, self.id);
  405. streamElement.onplaying = null;
  406. // Refresh to show the video
  407. self.updateView();
  408. };
  409. streamElement.onplaying = onPlayingHandler;
  410. };
  411. /**
  412. * Checks whether the video stream has started for this RemoteVideo instance.
  413. *
  414. * @returns {boolean} true if this RemoteVideo has a video stream for which
  415. * the playback has been started.
  416. */
  417. RemoteVideo.prototype.hasVideoStarted = function() {
  418. return this.wasVideoPlayed;
  419. };
  420. RemoteVideo.prototype.addRemoteStreamElement = function(stream) {
  421. if (!this.container) {
  422. return;
  423. }
  424. const isVideo = stream.isVideoTrack();
  425. isVideo ? this.videoStream = stream : this.audioStream = stream;
  426. if (isVideo) {
  427. this.setVideoType(stream.videoType);
  428. }
  429. if (!stream.getOriginalStream()) {
  430. return;
  431. }
  432. const streamElement = SmallVideo.createStreamElement(stream);
  433. // Put new stream element always in front
  434. UIUtils.prependChild(this.container, streamElement);
  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. stream.attach(streamElement);
  441. }
  442. if (!isVideo) {
  443. this._audioStreamElement = streamElement;
  444. // If the remote video menu was created before the audio stream was
  445. // attached we need to update the menu in order to show the volume
  446. // slider.
  447. this.updateRemoteVideoMenu();
  448. }
  449. };
  450. /**
  451. * Sets the display name for the given video span id.
  452. *
  453. * @param displayName the display name to set
  454. */
  455. RemoteVideo.prototype.setDisplayName = function(displayName) {
  456. if (!this.container) {
  457. logger.warn(`Unable to set displayName - ${this.videoSpanId
  458. } does not exist`);
  459. return;
  460. }
  461. this.updateDisplayName({
  462. displayName: displayName || interfaceConfig.DEFAULT_REMOTE_DISPLAY_NAME,
  463. elementID: `${this.videoSpanId}_name`,
  464. participantID: this.id
  465. });
  466. };
  467. /**
  468. * Removes remote video menu element from video element identified by
  469. * given <tt>videoElementId</tt>.
  470. *
  471. * @param videoElementId the id of local or remote video element.
  472. */
  473. RemoteVideo.prototype.removeRemoteVideoMenu = function() {
  474. const menuSpan = this.$container.find('.remotevideomenu');
  475. if (menuSpan.length) {
  476. ReactDOM.unmountComponentAtNode(menuSpan.get(0));
  477. menuSpan.remove();
  478. }
  479. };
  480. /**
  481. * Mounts the {@code PresenceLabel} for displaying the participant's current
  482. * presence status.
  483. *
  484. * @return {void}
  485. */
  486. RemoteVideo.prototype.addPresenceLabel = function() {
  487. const presenceLabelContainer
  488. = this.container.querySelector('.presence-label-container');
  489. if (presenceLabelContainer) {
  490. ReactDOM.render(
  491. <Provider store = { APP.store }>
  492. <I18nextProvider i18n = { i18next }>
  493. <PresenceLabel
  494. participantID = { this.id }
  495. className = 'presence-label' />
  496. </I18nextProvider>
  497. </Provider>,
  498. presenceLabelContainer);
  499. }
  500. };
  501. /**
  502. * Unmounts the {@code PresenceLabel} component.
  503. *
  504. * @return {void}
  505. */
  506. RemoteVideo.prototype.removePresenceLabel = function() {
  507. const presenceLabelContainer
  508. = this.container.querySelector('.presence-label-container');
  509. if (presenceLabelContainer) {
  510. ReactDOM.unmountComponentAtNode(presenceLabelContainer);
  511. }
  512. };
  513. /**
  514. * Callback invoked when the thumbnail is clicked. Will directly call
  515. * VideoLayout to handle thumbnail click if certain elements have not been
  516. * clicked.
  517. *
  518. * @param {MouseEvent} event - The click event to intercept.
  519. * @private
  520. * @returns {void}
  521. */
  522. RemoteVideo.prototype._onContainerClick = function(event) {
  523. const $source = $(event.target || event.srcElement);
  524. const { classList } = event.target;
  525. const ignoreClick = $source.parents('.popover').length > 0
  526. || classList.contains('popover');
  527. if (!ignoreClick) {
  528. this._togglePin();
  529. }
  530. // On IE we need to populate this handler on video <object> and it does not
  531. // give event instance as an argument, so we check here for methods.
  532. if (event.stopPropagation && event.preventDefault && !ignoreClick) {
  533. event.stopPropagation();
  534. event.preventDefault();
  535. }
  536. return false;
  537. };
  538. RemoteVideo.createContainer = function(spanId) {
  539. const container = document.createElement('span');
  540. container.id = spanId;
  541. container.className = 'videocontainer';
  542. container.innerHTML = `
  543. <div class = 'videocontainer__background'></div>
  544. <div class = 'videocontainer__toptoolbar'></div>
  545. <div class = 'videocontainer__toolbar'></div>
  546. <div class = 'videocontainer__hoverOverlay'></div>
  547. <div class = 'displayNameContainer'></div>
  548. <div class = 'avatar-container'></div>
  549. <div class ='presence-label-container'></div>
  550. <span class = 'remotevideomenu'></span>`;
  551. const remoteVideosContainer
  552. = document.getElementById('filmstripRemoteVideosContainer');
  553. const localVideoContainer
  554. = document.getElementById('localVideoTileViewContainer');
  555. remoteVideosContainer.insertBefore(container, localVideoContainer);
  556. return container;
  557. };
  558. export default RemoteVideo;