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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603
  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 {
  22. LAYOUTS,
  23. getCurrentLayout
  24. } from '../../../react/features/video-layout';
  25. /* eslint-enable no-unused-vars */
  26. const logger = require('jitsi-meet-logger').getLogger(__filename);
  27. import SmallVideo from './SmallVideo';
  28. import UIUtils from '../util/UIUtil';
  29. /**
  30. *
  31. * @param {*} spanId
  32. */
  33. function createContainer(spanId) {
  34. const container = document.createElement('span');
  35. container.id = spanId;
  36. container.className = 'videocontainer';
  37. container.innerHTML = `
  38. <div class = 'videocontainer__background'></div>
  39. <div class = 'videocontainer__toptoolbar'></div>
  40. <div class = 'videocontainer__toolbar'></div>
  41. <div class = 'videocontainer__hoverOverlay'></div>
  42. <div class = 'displayNameContainer'></div>
  43. <div class = 'avatar-container'></div>
  44. <div class ='presence-label-container'></div>
  45. <span class = 'remotevideomenu'></span>`;
  46. const remoteVideosContainer
  47. = document.getElementById('filmstripRemoteVideosContainer');
  48. const localVideoContainer
  49. = document.getElementById('localVideoTileViewContainer');
  50. remoteVideosContainer.insertBefore(container, localVideoContainer);
  51. return container;
  52. }
  53. /**
  54. *
  55. */
  56. export default class RemoteVideo extends SmallVideo {
  57. /**
  58. * Creates new instance of the <tt>RemoteVideo</tt>.
  59. * @param user {JitsiParticipant} the user for whom remote video instance will
  60. * be created.
  61. * @param {VideoLayout} VideoLayout the video layout instance.
  62. * @constructor
  63. */
  64. constructor(user, VideoLayout) {
  65. super(VideoLayout);
  66. this.user = user;
  67. this.id = user.getId();
  68. this.videoSpanId = `participant_${this.id}`;
  69. this._audioStreamElement = null;
  70. this._supportsRemoteControl = false;
  71. this.statsPopoverLocation = interfaceConfig.VERTICAL_FILMSTRIP ? 'left bottom' : 'top center';
  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 'onplay' 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.wasVideoPlayed = 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.initBrowserSpecificProperties();
  114. this.updateRemoteVideoMenu();
  115. this.addAudioLevelIndicator();
  116. this.addPresenceLabel();
  117. return this.container;
  118. }
  119. /**
  120. * Checks whether current video is considered hovered. Currently it is hovered
  121. * if the mouse is over the video, or if the connection indicator or the popup
  122. * menu is shown(hovered).
  123. * @private
  124. * NOTE: extends SmallVideo's method
  125. */
  126. _isHovered() {
  127. return super._isHovered() || this.popupMenuIsHovered;
  128. }
  129. /**
  130. * Generates the popup menu content.
  131. *
  132. * @returns {Element|*} the constructed element, containing popup menu items
  133. * @private
  134. */
  135. _generatePopupContent() {
  136. if (interfaceConfig.filmStripOnly) {
  137. return;
  138. }
  139. const remoteVideoMenuContainer
  140. = this.container.querySelector('.remotevideomenu');
  141. if (!remoteVideoMenuContainer) {
  142. return;
  143. }
  144. const { controller } = APP.remoteControl;
  145. let remoteControlState = null;
  146. let onRemoteControlToggle;
  147. if (this._supportsRemoteControl
  148. && ((!APP.remoteControl.active && !this._isRemoteControlSessionActive)
  149. || APP.remoteControl.controller.activeParticipant === this.id)) {
  150. if (controller.getRequestedParticipant() === this.id) {
  151. remoteControlState = REMOTE_CONTROL_MENU_STATES.REQUESTING;
  152. } else if (controller.isStarted()) {
  153. onRemoteControlToggle = this._stopRemoteControl;
  154. remoteControlState = REMOTE_CONTROL_MENU_STATES.STARTED;
  155. } else {
  156. onRemoteControlToggle = this._requestRemoteControlPermissions;
  157. remoteControlState = REMOTE_CONTROL_MENU_STATES.NOT_STARTED;
  158. }
  159. }
  160. const initialVolumeValue = this._audioStreamElement && this._audioStreamElement.volume;
  161. // hide volume when in silent mode
  162. const onVolumeChange
  163. = APP.store.getState()['features/base/config'].startSilent ? undefined : this._setAudioVolume;
  164. const { isModerator } = APP.conference;
  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. isModerator = { isModerator }
  183. menuPosition = { remoteMenuPosition }
  184. onMenuDisplay
  185. = {this._onRemoteVideoMenuDisplay.bind(this)}
  186. onRemoteControlToggle = { onRemoteControlToggle }
  187. onVolumeChange = { onVolumeChange }
  188. participantID = { participantID }
  189. remoteControlState = { remoteControlState } />
  190. </AtlasKitThemeProvider>
  191. </I18nextProvider>
  192. </Provider>,
  193. remoteVideoMenuContainer);
  194. }
  195. /**
  196. *
  197. */
  198. _onRemoteVideoMenuDisplay() {
  199. this.updateRemoteVideoMenu();
  200. }
  201. /**
  202. * Sets the remote control active status for the remote video.
  203. *
  204. * @param {boolean} isActive - The new remote control active status.
  205. * @returns {void}
  206. */
  207. setRemoteControlActiveStatus(isActive) {
  208. this._isRemoteControlSessionActive = isActive;
  209. this.updateRemoteVideoMenu();
  210. }
  211. /**
  212. * Sets the remote control supported value and initializes or updates the menu
  213. * depending on the remote control is supported or not.
  214. * @param {boolean} isSupported
  215. */
  216. setRemoteControlSupport(isSupported = false) {
  217. if (this._supportsRemoteControl === isSupported) {
  218. return;
  219. }
  220. this._supportsRemoteControl = isSupported;
  221. this.updateRemoteVideoMenu();
  222. }
  223. /**
  224. * Requests permissions for remote control session.
  225. */
  226. _requestRemoteControlPermissions() {
  227. APP.remoteControl.controller.requestPermissions(this.id, this.VideoLayout.getLargeVideoWrapper())
  228. .then(result => {
  229. if (result === null) {
  230. return;
  231. }
  232. this.updateRemoteVideoMenu();
  233. APP.UI.messageHandler.notify(
  234. 'dialog.remoteControlTitle',
  235. result === false ? 'dialog.remoteControlDeniedMessage' : 'dialog.remoteControlAllowedMessage',
  236. { user: this.user.getDisplayName() || interfaceConfig.DEFAULT_REMOTE_DISPLAY_NAME }
  237. );
  238. if (result === true) {
  239. // the remote control permissions has been granted
  240. // pin the controlled participant
  241. const pinnedParticipant = getPinnedParticipant(APP.store.getState()) || {};
  242. const pinnedId = pinnedParticipant.id;
  243. if (pinnedId !== this.id) {
  244. APP.store.dispatch(pinParticipant(this.id));
  245. }
  246. }
  247. }, error => {
  248. logger.error(error);
  249. this.updateRemoteVideoMenu();
  250. APP.UI.messageHandler.notify(
  251. 'dialog.remoteControlTitle',
  252. 'dialog.remoteControlErrorMessage',
  253. { user: this.user.getDisplayName() || interfaceConfig.DEFAULT_REMOTE_DISPLAY_NAME }
  254. );
  255. });
  256. this.updateRemoteVideoMenu();
  257. }
  258. /**
  259. * Stops remote control session.
  260. */
  261. _stopRemoteControl() {
  262. // send message about stopping
  263. APP.remoteControl.controller.stop();
  264. this.updateRemoteVideoMenu();
  265. }
  266. /**
  267. * Change the remote participant's volume level.
  268. *
  269. * @param {int} newVal - The value to set the slider to.
  270. */
  271. _setAudioVolume(newVal) {
  272. if (this._audioStreamElement) {
  273. this._audioStreamElement.volume = newVal;
  274. }
  275. }
  276. /**
  277. * Updates the remote video menu.
  278. *
  279. * @param isMuted the new muted state to update to
  280. */
  281. updateRemoteVideoMenu(isMuted) {
  282. if (typeof isMuted !== 'undefined') {
  283. this.isAudioMuted = isMuted;
  284. }
  285. this._generatePopupContent();
  286. }
  287. /**
  288. * @inheritDoc
  289. * @override
  290. */
  291. setVideoMutedView(isMuted) {
  292. super.setVideoMutedView(isMuted);
  293. // Update 'mutedWhileDisconnected' flag
  294. this._figureOutMutedWhileDisconnected();
  295. }
  296. /**
  297. * Figures out the value of {@link #mutedWhileDisconnected} flag by taking into
  298. * account remote participant's network connectivity and video muted status.
  299. *
  300. * @private
  301. */
  302. _figureOutMutedWhileDisconnected() {
  303. const isActive = this.isConnectionActive();
  304. if (!isActive && this.isVideoMuted) {
  305. this.mutedWhileDisconnected = true;
  306. } else if (isActive && !this.isVideoMuted) {
  307. this.mutedWhileDisconnected = false;
  308. }
  309. }
  310. /**
  311. * Removes the remote stream element corresponding to the given stream and
  312. * parent container.
  313. *
  314. * @param stream the MediaStream
  315. * @param isVideo <tt>true</tt> if given <tt>stream</tt> is a video one.
  316. */
  317. removeRemoteStreamElement(stream) {
  318. if (!this.container) {
  319. return false;
  320. }
  321. const isVideo = stream.isVideoTrack();
  322. const elementID = SmallVideo.getStreamElementID(stream);
  323. const select = $(`#${elementID}`);
  324. select.remove();
  325. if (isVideo) {
  326. this.wasVideoPlayed = false;
  327. }
  328. logger.info(`${isVideo ? 'Video' : 'Audio'} removed ${this.id}`, select);
  329. if (stream === this.videoStream) {
  330. this.videoStream = null;
  331. }
  332. this.updateView();
  333. }
  334. /**
  335. * Checks whether the remote user associated with this <tt>RemoteVideo</tt>
  336. * has connectivity issues.
  337. *
  338. * @return {boolean} <tt>true</tt> if the user's connection is fine or
  339. * <tt>false</tt> otherwise.
  340. */
  341. isConnectionActive() {
  342. return this.user.getConnectionStatus() === JitsiParticipantConnectionStatus.ACTIVE;
  343. }
  344. /**
  345. * The remote video is considered "playable" once the stream has started
  346. * according to the {@link #hasVideoStarted} result.
  347. * It will be allowed to display video also in
  348. * {@link JitsiParticipantConnectionStatus.INTERRUPTED} if the video was ever
  349. * played and was not muted while not in ACTIVE state. This basically means
  350. * that there is stalled video image cached that could be displayed. It's used
  351. * 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.hasVideoStarted()
  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. const isInterrupted = connectionStatus === JitsiParticipantConnectionStatus.INTERRUPTED;
  385. // Toggle thumbnail video problem filter
  386. this.selectVideoElement().toggleClass('videoThumbnailProblemFilter', isInterrupted);
  387. this.$avatar().toggleClass('videoThumbnailProblemFilter', isInterrupted);
  388. }
  389. /**
  390. * Removes RemoteVideo from the page.
  391. */
  392. remove() {
  393. super.remove();
  394. this.removePresenceLabel();
  395. this.removeRemoteVideoMenu();
  396. }
  397. /**
  398. *
  399. * @param {*} streamElement
  400. * @param {*} stream
  401. */
  402. waitForPlayback(streamElement, stream) {
  403. const webRtcStream = stream.getOriginalStream();
  404. const isVideo = stream.isVideoTrack();
  405. if (!isVideo || webRtcStream.id === 'mixedmslabel') {
  406. return;
  407. }
  408. streamElement.onplaying = () => {
  409. this.wasVideoPlayed = true;
  410. this.VideoLayout.remoteVideoActive(streamElement, this.id);
  411. streamElement.onplaying = null;
  412. // Refresh to show the video
  413. this.updateView();
  414. };
  415. }
  416. /**
  417. * Checks whether the video stream has started for this RemoteVideo instance.
  418. *
  419. * @returns {boolean} true if this RemoteVideo has a video stream for which
  420. * the playback has been started.
  421. */
  422. hasVideoStarted() {
  423. return this.wasVideoPlayed;
  424. }
  425. /**
  426. *
  427. * @param {*} stream
  428. */
  429. addRemoteStreamElement(stream) {
  430. if (!this.container) {
  431. logger.debug('Not attaching remote stream due to no container');
  432. return;
  433. }
  434. const isVideo = stream.isVideoTrack();
  435. isVideo ? this.videoStream = stream : this.audioStream = stream;
  436. if (isVideo) {
  437. this.setVideoType(stream.videoType);
  438. }
  439. if (!stream.getOriginalStream()) {
  440. logger.debug('Remote video stream has no original stream');
  441. return;
  442. }
  443. const streamElement = SmallVideo.createStreamElement(stream);
  444. // Put new stream element always in front
  445. UIUtils.prependChild(this.container, streamElement);
  446. $(streamElement).hide();
  447. this.waitForPlayback(streamElement, stream);
  448. stream.attach(streamElement);
  449. if (!isVideo) {
  450. this._audioStreamElement = streamElement;
  451. // If the remote video menu was created before the audio stream was
  452. // attached we need to update the menu in order to show the volume
  453. // slider.
  454. this.updateRemoteVideoMenu();
  455. }
  456. }
  457. /**
  458. * Triggers re-rendering of the display name using current instance state.
  459. *
  460. * @returns {void}
  461. */
  462. updateDisplayName() {
  463. if (!this.container) {
  464. logger.warn(`Unable to set displayName - ${this.videoSpanId} does not exist`);
  465. return;
  466. }
  467. this._renderDisplayName({
  468. elementID: `${this.videoSpanId}_name`,
  469. participantID: this.id
  470. });
  471. }
  472. /**
  473. * Removes remote video menu element from video element identified by
  474. * given <tt>videoElementId</tt>.
  475. *
  476. * @param videoElementId the id of local or remote video element.
  477. */
  478. removeRemoteVideoMenu() {
  479. const menuSpan = this.$container.find('.remotevideomenu');
  480. if (menuSpan.length) {
  481. ReactDOM.unmountComponentAtNode(menuSpan.get(0));
  482. menuSpan.remove();
  483. }
  484. }
  485. /**
  486. * Mounts the {@code PresenceLabel} for displaying the participant's current
  487. * presence status.
  488. *
  489. * @return {void}
  490. */
  491. addPresenceLabel() {
  492. const presenceLabelContainer = this.container.querySelector('.presence-label-container');
  493. if (presenceLabelContainer) {
  494. ReactDOM.render(
  495. <Provider store = { APP.store }>
  496. <I18nextProvider i18n = { i18next }>
  497. <PresenceLabel
  498. participantID = { this.id }
  499. className = 'presence-label' />
  500. </I18nextProvider>
  501. </Provider>,
  502. presenceLabelContainer);
  503. }
  504. }
  505. /**
  506. * Unmounts the {@code PresenceLabel} component.
  507. *
  508. * @return {void}
  509. */
  510. removePresenceLabel() {
  511. const presenceLabelContainer = this.container.querySelector('.presence-label-container');
  512. if (presenceLabelContainer) {
  513. ReactDOM.unmountComponentAtNode(presenceLabelContainer);
  514. }
  515. }
  516. }