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

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