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 18KB

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