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

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