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

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