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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626
  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. this.container.ondblclick = this._onContainerDoubleClick;
  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. return;
  414. }
  415. const isVideo = stream.isVideoTrack();
  416. isVideo ? this.videoStream = stream : this.audioStream = stream;
  417. if (isVideo) {
  418. this.setVideoType(stream.videoType);
  419. }
  420. if (!stream.getOriginalStream()) {
  421. return;
  422. }
  423. const streamElement = SmallVideo.createStreamElement(stream);
  424. // Put new stream element always in front
  425. UIUtils.prependChild(this.container, streamElement);
  426. $(streamElement).hide();
  427. // If the container is currently visible
  428. // we attach the stream to the element.
  429. if (!isVideo || (this.container.offsetParent !== null && isVideo)) {
  430. this.waitForPlayback(streamElement, stream);
  431. stream.attach(streamElement);
  432. }
  433. if (!isVideo) {
  434. this._audioStreamElement = streamElement;
  435. // If the remote video menu was created before the audio stream was
  436. // attached we need to update the menu in order to show the volume
  437. // slider.
  438. this.updateRemoteVideoMenu();
  439. }
  440. };
  441. /**
  442. * Triggers re-rendering of the display name using current instance state.
  443. *
  444. * @returns {void}
  445. */
  446. RemoteVideo.prototype.updateDisplayName = function() {
  447. if (!this.container) {
  448. logger.warn(`Unable to set displayName - ${this.videoSpanId
  449. } does not exist`);
  450. return;
  451. }
  452. this._renderDisplayName({
  453. elementID: `${this.videoSpanId}_name`,
  454. participantID: this.id
  455. });
  456. };
  457. /**
  458. * Removes remote video menu element from video element identified by
  459. * given <tt>videoElementId</tt>.
  460. *
  461. * @param videoElementId the id of local or remote video element.
  462. */
  463. RemoteVideo.prototype.removeRemoteVideoMenu = function() {
  464. const menuSpan = this.$container.find('.remotevideomenu');
  465. if (menuSpan.length) {
  466. ReactDOM.unmountComponentAtNode(menuSpan.get(0));
  467. menuSpan.remove();
  468. }
  469. };
  470. /**
  471. * Mounts the {@code PresenceLabel} for displaying the participant's current
  472. * presence status.
  473. *
  474. * @return {void}
  475. */
  476. RemoteVideo.prototype.addPresenceLabel = function() {
  477. const presenceLabelContainer
  478. = this.container.querySelector('.presence-label-container');
  479. if (presenceLabelContainer) {
  480. ReactDOM.render(
  481. <Provider store = { APP.store }>
  482. <I18nextProvider i18n = { i18next }>
  483. <PresenceLabel
  484. participantID = { this.id }
  485. className = 'presence-label' />
  486. </I18nextProvider>
  487. </Provider>,
  488. presenceLabelContainer);
  489. }
  490. };
  491. /**
  492. * Unmounts the {@code PresenceLabel} component.
  493. *
  494. * @return {void}
  495. */
  496. RemoteVideo.prototype.removePresenceLabel = function() {
  497. const presenceLabelContainer
  498. = this.container.querySelector('.presence-label-container');
  499. if (presenceLabelContainer) {
  500. ReactDOM.unmountComponentAtNode(presenceLabelContainer);
  501. }
  502. };
  503. RemoteVideo.createContainer = function(spanId) {
  504. const container = document.createElement('span');
  505. container.id = spanId;
  506. container.className = 'videocontainer';
  507. container.innerHTML = `
  508. <div class = 'videocontainer__background'></div>
  509. <div class = 'videocontainer__toptoolbar'></div>
  510. <div class = 'videocontainer__toolbar'></div>
  511. <div class = 'videocontainer__hoverOverlay'></div>
  512. <div class = 'displayNameContainer'></div>
  513. <div class = 'avatar-container'></div>
  514. <div class ='presence-label-container'></div>
  515. <span class = 'remotevideomenu'></span>`;
  516. const remoteVideosContainer
  517. = document.getElementById('filmstripRemoteVideosContainer');
  518. const localVideoContainer
  519. = document.getElementById('localVideoTileViewContainer');
  520. remoteVideosContainer.insertBefore(container, localVideoContainer);
  521. return container;
  522. };
  523. export default RemoteVideo;