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

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