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 19KB

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