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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650
  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 { createThumbnailOffsetParentIsNullEvent, sendAnalytics } from '../../../react/features/analytics';
  9. import { i18next } from '../../../react/features/base/i18n';
  10. import {
  11. JitsiParticipantConnectionStatus
  12. } from '../../../react/features/base/lib-jitsi-meet';
  13. import {
  14. getPinnedParticipant,
  15. pinParticipant
  16. } from '../../../react/features/base/participants';
  17. import { PresenceLabel } from '../../../react/features/presence-status';
  18. import {
  19. REMOTE_CONTROL_MENU_STATES,
  20. RemoteVideoMenuTriggerButton
  21. } from '../../../react/features/remote-video-menu';
  22. import {
  23. LAYOUTS,
  24. getCurrentLayout
  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.updateDisplayName();
  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;
  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. // hide volume when in silent mode
  142. const onVolumeChange = APP.store.getState()['features/base/config'].startSilent
  143. ? undefined : this._setAudioVolume;
  144. const { isModerator } = APP.conference;
  145. const participantID = this.id;
  146. const currentLayout = getCurrentLayout(APP.store.getState());
  147. let remoteMenuPosition;
  148. if (currentLayout === LAYOUTS.TILE_VIEW) {
  149. remoteMenuPosition = 'left top';
  150. } else if (currentLayout === LAYOUTS.VERTICAL_FILMSTRIP_VIEW) {
  151. remoteMenuPosition = 'left bottom';
  152. } else {
  153. remoteMenuPosition = 'top center';
  154. }
  155. ReactDOM.render(
  156. <Provider store = { APP.store }>
  157. <I18nextProvider i18n = { i18next }>
  158. <AtlasKitThemeProvider mode = 'dark'>
  159. <RemoteVideoMenuTriggerButton
  160. initialVolumeValue = { initialVolumeValue }
  161. isAudioMuted = { this.isAudioMuted }
  162. isModerator = { isModerator }
  163. menuPosition = { remoteMenuPosition }
  164. onMenuDisplay
  165. = {this._onRemoteVideoMenuDisplay.bind(this)}
  166. onRemoteControlToggle = { onRemoteControlToggle }
  167. onVolumeChange = { onVolumeChange }
  168. participantID = { participantID }
  169. remoteControlState = { remoteControlState } />
  170. </AtlasKitThemeProvider>
  171. </I18nextProvider>
  172. </Provider>,
  173. remoteVideoMenuContainer);
  174. };
  175. RemoteVideo.prototype._onRemoteVideoMenuDisplay = function() {
  176. this.updateRemoteVideoMenu();
  177. };
  178. /**
  179. * Sets the remote control active status for the remote video.
  180. *
  181. * @param {boolean} isActive - The new remote control active status.
  182. * @returns {void}
  183. */
  184. RemoteVideo.prototype.setRemoteControlActiveStatus = function(isActive) {
  185. this._isRemoteControlSessionActive = isActive;
  186. this.updateRemoteVideoMenu();
  187. };
  188. /**
  189. * Sets the remote control supported value and initializes or updates the menu
  190. * depending on the remote control is supported or not.
  191. * @param {boolean} isSupported
  192. */
  193. RemoteVideo.prototype.setRemoteControlSupport = function(isSupported = false) {
  194. if (this._supportsRemoteControl === isSupported) {
  195. return;
  196. }
  197. this._supportsRemoteControl = isSupported;
  198. this.updateRemoteVideoMenu();
  199. };
  200. /**
  201. * Requests permissions for remote control session.
  202. */
  203. RemoteVideo.prototype._requestRemoteControlPermissions = function() {
  204. APP.remoteControl.controller.requestPermissions(
  205. this.id, this.VideoLayout.getLargeVideoWrapper()).then(result => {
  206. if (result === null) {
  207. return;
  208. }
  209. this.updateRemoteVideoMenu();
  210. APP.UI.messageHandler.notify(
  211. 'dialog.remoteControlTitle',
  212. result === false ? 'dialog.remoteControlDeniedMessage'
  213. : 'dialog.remoteControlAllowedMessage',
  214. { user: this.user.getDisplayName()
  215. || interfaceConfig.DEFAULT_REMOTE_DISPLAY_NAME }
  216. );
  217. if (result === true) {
  218. // the remote control permissions has been granted
  219. // pin the controlled participant
  220. const pinnedParticipant
  221. = getPinnedParticipant(APP.store.getState()) || {};
  222. const pinnedId = pinnedParticipant.id;
  223. if (pinnedId !== this.id) {
  224. APP.store.dispatch(pinParticipant(this.id));
  225. }
  226. }
  227. }, error => {
  228. logger.error(error);
  229. this.updateRemoteVideoMenu();
  230. APP.UI.messageHandler.notify(
  231. 'dialog.remoteControlTitle',
  232. 'dialog.remoteControlErrorMessage',
  233. { user: this.user.getDisplayName()
  234. || interfaceConfig.DEFAULT_REMOTE_DISPLAY_NAME }
  235. );
  236. });
  237. this.updateRemoteVideoMenu();
  238. };
  239. /**
  240. * Stops remote control session.
  241. */
  242. RemoteVideo.prototype._stopRemoteControl = function() {
  243. // send message about stopping
  244. APP.remoteControl.controller.stop();
  245. this.updateRemoteVideoMenu();
  246. };
  247. /**
  248. * Change the remote participant's volume level.
  249. *
  250. * @param {int} newVal - The value to set the slider to.
  251. */
  252. RemoteVideo.prototype._setAudioVolume = function(newVal) {
  253. if (this._audioStreamElement) {
  254. this._audioStreamElement.volume = newVal;
  255. }
  256. };
  257. /**
  258. * Updates the remote video menu.
  259. *
  260. * @param isMuted the new muted state to update to
  261. */
  262. RemoteVideo.prototype.updateRemoteVideoMenu = function(isMuted) {
  263. if (typeof isMuted !== 'undefined') {
  264. this.isAudioMuted = isMuted;
  265. }
  266. this._generatePopupContent();
  267. };
  268. /**
  269. * @inheritDoc
  270. * @override
  271. */
  272. RemoteVideo.prototype.setVideoMutedView = function(isMuted) {
  273. SmallVideo.prototype.setVideoMutedView.call(this, isMuted);
  274. // Update 'mutedWhileDisconnected' flag
  275. this._figureOutMutedWhileDisconnected();
  276. };
  277. /**
  278. * Figures out the value of {@link #mutedWhileDisconnected} flag by taking into
  279. * account remote participant's network connectivity and video muted status.
  280. *
  281. * @private
  282. */
  283. RemoteVideo.prototype._figureOutMutedWhileDisconnected = function() {
  284. const isActive = this.isConnectionActive();
  285. if (!isActive && this.isVideoMuted) {
  286. this.mutedWhileDisconnected = true;
  287. } else if (isActive && !this.isVideoMuted) {
  288. this.mutedWhileDisconnected = false;
  289. }
  290. };
  291. /**
  292. * Removes the remote stream element corresponding to the given stream and
  293. * parent container.
  294. *
  295. * @param stream the MediaStream
  296. * @param isVideo <tt>true</tt> if given <tt>stream</tt> is a video one.
  297. */
  298. RemoteVideo.prototype.removeRemoteStreamElement = function(stream) {
  299. if (!this.container) {
  300. return false;
  301. }
  302. const isVideo = stream.isVideoTrack();
  303. const elementID = SmallVideo.getStreamElementID(stream);
  304. const select = $(`#${elementID}`);
  305. select.remove();
  306. if (isVideo) {
  307. this.wasVideoPlayed = false;
  308. }
  309. logger.info(`${isVideo ? 'Video' : 'Audio'
  310. } removed ${this.id}`, select);
  311. if (stream === this.videoStream) {
  312. this.videoStream = null;
  313. }
  314. this.updateView();
  315. };
  316. /**
  317. * Checks whether the remote user associated with this <tt>RemoteVideo</tt>
  318. * has connectivity issues.
  319. *
  320. * @return {boolean} <tt>true</tt> if the user's connection is fine or
  321. * <tt>false</tt> otherwise.
  322. */
  323. RemoteVideo.prototype.isConnectionActive = function() {
  324. return this.user.getConnectionStatus()
  325. === JitsiParticipantConnectionStatus.ACTIVE;
  326. };
  327. /**
  328. * The remote video is considered "playable" once the stream has started
  329. * according to the {@link #hasVideoStarted} result.
  330. * It will be allowed to display video also in
  331. * {@link JitsiParticipantConnectionStatus.INTERRUPTED} if the video was ever
  332. * played and was not muted while not in ACTIVE state. This basically means
  333. * that there is stalled video image cached that could be displayed. It's used
  334. * to show "grey video image" in user's thumbnail when there are connectivity
  335. * issues.
  336. *
  337. * @inheritdoc
  338. * @override
  339. */
  340. RemoteVideo.prototype.isVideoPlayable = function() {
  341. const connectionState
  342. = APP.conference.getParticipantConnectionStatus(this.id);
  343. return SmallVideo.prototype.isVideoPlayable.call(this)
  344. && this.hasVideoStarted()
  345. && (connectionState === JitsiParticipantConnectionStatus.ACTIVE
  346. || (connectionState === JitsiParticipantConnectionStatus.INTERRUPTED
  347. && !this.mutedWhileDisconnected));
  348. };
  349. /**
  350. * @inheritDoc
  351. */
  352. RemoteVideo.prototype.updateView = function() {
  353. this.$container.toggleClass('audio-only', APP.conference.isAudioOnly());
  354. this.updateConnectionStatusIndicator();
  355. // This must be called after 'updateConnectionStatusIndicator' because it
  356. // affects the display mode by modifying 'mutedWhileDisconnected' flag
  357. SmallVideo.prototype.updateView.call(this);
  358. };
  359. /**
  360. * Updates the UI to reflect user's connectivity status.
  361. */
  362. RemoteVideo.prototype.updateConnectionStatusIndicator = function() {
  363. const connectionStatus = this.user.getConnectionStatus();
  364. logger.debug(`${this.id} thumbnail connection status: ${connectionStatus}`);
  365. // FIXME rename 'mutedWhileDisconnected' to 'mutedWhileNotRendering'
  366. // Update 'mutedWhileDisconnected' flag
  367. this._figureOutMutedWhileDisconnected();
  368. this.updateConnectionStatus(connectionStatus);
  369. const isInterrupted
  370. = connectionStatus === JitsiParticipantConnectionStatus.INTERRUPTED;
  371. // Toggle thumbnail video problem filter
  372. this.selectVideoElement().toggleClass(
  373. 'videoThumbnailProblemFilter', isInterrupted);
  374. this.$avatar().toggleClass(
  375. 'videoThumbnailProblemFilter', isInterrupted);
  376. };
  377. /**
  378. * Removes RemoteVideo from the page.
  379. */
  380. RemoteVideo.prototype.remove = function() {
  381. SmallVideo.prototype.remove.call(this);
  382. this.removePresenceLabel();
  383. this.removeRemoteVideoMenu();
  384. };
  385. RemoteVideo.prototype.waitForPlayback = function(streamElement, stream) {
  386. const webRtcStream = stream.getOriginalStream();
  387. const isVideo = stream.isVideoTrack();
  388. if (!isVideo || webRtcStream.id === 'mixedmslabel') {
  389. return;
  390. }
  391. const self = this;
  392. // Triggers when video playback starts
  393. const onPlayingHandler = function() {
  394. self.wasVideoPlayed = true;
  395. self.VideoLayout.remoteVideoActive(streamElement, self.id);
  396. streamElement.onplaying = null;
  397. // Refresh to show the video
  398. self.updateView();
  399. };
  400. streamElement.onplaying = onPlayingHandler;
  401. };
  402. /**
  403. * Checks whether the video stream has started for this RemoteVideo instance.
  404. *
  405. * @returns {boolean} true if this RemoteVideo has a video stream for which
  406. * the playback has been started.
  407. */
  408. RemoteVideo.prototype.hasVideoStarted = function() {
  409. return this.wasVideoPlayed;
  410. };
  411. RemoteVideo.prototype.addRemoteStreamElement = function(stream) {
  412. if (!this.container) {
  413. logger.debug('Not attaching remote stream due to no container');
  414. return;
  415. }
  416. const isVideo = stream.isVideoTrack();
  417. isVideo ? this.videoStream = stream : this.audioStream = stream;
  418. if (isVideo) {
  419. this.setVideoType(stream.videoType);
  420. }
  421. if (!stream.getOriginalStream()) {
  422. logger.debug('Remote video stream has no original stream');
  423. return;
  424. }
  425. const streamElement = SmallVideo.createStreamElement(stream);
  426. // Put new stream element always in front
  427. UIUtils.prependChild(this.container, streamElement);
  428. $(streamElement).hide();
  429. this.waitForPlayback(streamElement, stream);
  430. stream.attach(streamElement);
  431. // TODO: Remove once we verify that this.container.offsetParent === null was the reason for not attached video
  432. // streams to the thumbnail.
  433. if (isVideo && this.container.offsetParent === null) {
  434. sendAnalytics(createThumbnailOffsetParentIsNullEvent(this.id));
  435. const parentNodesDisplayProps = [
  436. '#filmstripRemoteVideosContainer',
  437. '#filmstripRemoteVideos',
  438. '#remoteVideos',
  439. '.filmstrip',
  440. '#videospace',
  441. '#videoconference_page',
  442. '#react'
  443. ].map(selector => `${selector} - ${$(selector).css('display')}`);
  444. const videoConferencePageParent = $('#videoconference_page').parent();
  445. const reactDiv = document.getElementById('react');
  446. parentNodesDisplayProps.push(
  447. `${videoConferencePageParent.attr('class')} - ${videoConferencePageParent.css('display')}`);
  448. parentNodesDisplayProps.push(`this.container - ${this.$container.css('display')}`);
  449. logger.debug(`this.container.offsetParent is null [user: ${this.id}, ${
  450. parentNodesDisplayProps.join(', ')}, #react.offsetParent - ${
  451. reactDiv && reactDiv.offsetParent !== null ? 'not null' : 'null'}]`);
  452. }
  453. if (!isVideo) {
  454. this._audioStreamElement = streamElement;
  455. // If the remote video menu was created before the audio stream was
  456. // attached we need to update the menu in order to show the volume
  457. // slider.
  458. this.updateRemoteVideoMenu();
  459. }
  460. };
  461. /**
  462. * Triggers re-rendering of the display name using current instance state.
  463. *
  464. * @returns {void}
  465. */
  466. RemoteVideo.prototype.updateDisplayName = function() {
  467. if (!this.container) {
  468. logger.warn(`Unable to set displayName - ${this.videoSpanId
  469. } does not exist`);
  470. return;
  471. }
  472. this._renderDisplayName({
  473. elementID: `${this.videoSpanId}_name`,
  474. participantID: this.id
  475. });
  476. };
  477. /**
  478. * Removes remote video menu element from video element identified by
  479. * given <tt>videoElementId</tt>.
  480. *
  481. * @param videoElementId the id of local or remote video element.
  482. */
  483. RemoteVideo.prototype.removeRemoteVideoMenu = function() {
  484. const menuSpan = this.$container.find('.remotevideomenu');
  485. if (menuSpan.length) {
  486. ReactDOM.unmountComponentAtNode(menuSpan.get(0));
  487. menuSpan.remove();
  488. }
  489. };
  490. /**
  491. * Mounts the {@code PresenceLabel} for displaying the participant's current
  492. * presence status.
  493. *
  494. * @return {void}
  495. */
  496. RemoteVideo.prototype.addPresenceLabel = function() {
  497. const presenceLabelContainer
  498. = this.container.querySelector('.presence-label-container');
  499. if (presenceLabelContainer) {
  500. ReactDOM.render(
  501. <Provider store = { APP.store }>
  502. <I18nextProvider i18n = { i18next }>
  503. <PresenceLabel
  504. participantID = { this.id }
  505. className = 'presence-label' />
  506. </I18nextProvider>
  507. </Provider>,
  508. presenceLabelContainer);
  509. }
  510. };
  511. /**
  512. * Unmounts the {@code PresenceLabel} component.
  513. *
  514. * @return {void}
  515. */
  516. RemoteVideo.prototype.removePresenceLabel = function() {
  517. const presenceLabelContainer
  518. = this.container.querySelector('.presence-label-container');
  519. if (presenceLabelContainer) {
  520. ReactDOM.unmountComponentAtNode(presenceLabelContainer);
  521. }
  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;