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

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