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

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