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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680
  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. /* eslint-enable no-unused-vars */
  22. const logger = require('jitsi-meet-logger').getLogger(__filename);
  23. import SmallVideo from './SmallVideo';
  24. import UIUtils from '../util/UIUtil';
  25. /**
  26. * Creates new instance of the <tt>RemoteVideo</tt>.
  27. * @param user {JitsiParticipant} the user for whom remote video instance will
  28. * be created.
  29. * @param {VideoLayout} VideoLayout the video layout instance.
  30. * @param {EventEmitter} emitter the event emitter which will be used by
  31. * the new instance to emit events.
  32. * @constructor
  33. */
  34. function RemoteVideo(user, VideoLayout, emitter) {
  35. this.user = user;
  36. this.id = user.getId();
  37. this.emitter = emitter;
  38. this.videoSpanId = `participant_${this.id}`;
  39. SmallVideo.call(this, VideoLayout);
  40. this._audioStreamElement = null;
  41. this._supportsRemoteControl = false;
  42. this.statsPopoverLocation = interfaceConfig.VERTICAL_FILMSTRIP
  43. ? 'left bottom' : 'top center';
  44. this.addRemoteVideoContainer();
  45. this.updateIndicators();
  46. this.setDisplayName();
  47. this.bindHoverHandler();
  48. this.flipX = false;
  49. this.isLocal = false;
  50. this.popupMenuIsHovered = false;
  51. this._isRemoteControlSessionActive = false;
  52. /**
  53. * The flag is set to <tt>true</tt> after the 'onplay' event has been
  54. * triggered on the current video element. It goes back to <tt>false</tt>
  55. * when the stream is removed. It is used to determine whether the video
  56. * playback has ever started.
  57. * @type {boolean}
  58. */
  59. this.wasVideoPlayed = false;
  60. /**
  61. * The flag is set to <tt>true</tt> if remote participant's video gets muted
  62. * during his media connection disruption. This is to prevent black video
  63. * being render on the thumbnail, because even though once the video has
  64. * been played the image usually remains on the video element it seems that
  65. * after longer period of the video element being hidden this image can be
  66. * lost.
  67. * @type {boolean}
  68. */
  69. this.mutedWhileDisconnected = false;
  70. // Bind event handlers so they are only bound once for every instance.
  71. // TODO The event handlers should be turned into actions so changes can be
  72. // handled through reducers and middleware.
  73. this._requestRemoteControlPermissions
  74. = this._requestRemoteControlPermissions.bind(this);
  75. this._setAudioVolume = this._setAudioVolume.bind(this);
  76. this._stopRemoteControl = this._stopRemoteControl.bind(this);
  77. this.container.onclick = this._onContainerClick.bind(this);
  78. }
  79. RemoteVideo.prototype = Object.create(SmallVideo.prototype);
  80. RemoteVideo.prototype.constructor = RemoteVideo;
  81. RemoteVideo.prototype.addRemoteVideoContainer = function() {
  82. this.container = RemoteVideo.createContainer(this.videoSpanId);
  83. this.$container = $(this.container);
  84. this.initBrowserSpecificProperties();
  85. this.updateRemoteVideoMenu();
  86. this.VideoLayout.resizeThumbnails(true);
  87. this.addAudioLevelIndicator();
  88. this.addPresenceLabel();
  89. return this.container;
  90. };
  91. /**
  92. * Checks whether current video is considered hovered. Currently it is hovered
  93. * if the mouse is over the video, or if the connection indicator or the popup
  94. * menu is shown(hovered).
  95. * @private
  96. * NOTE: extends SmallVideo's method
  97. */
  98. RemoteVideo.prototype._isHovered = function() {
  99. const isHovered = SmallVideo.prototype._isHovered.call(this)
  100. || this.popupMenuIsHovered;
  101. return isHovered;
  102. };
  103. /**
  104. * Generates the popup menu content.
  105. *
  106. * @returns {Element|*} the constructed element, containing popup menu items
  107. * @private
  108. */
  109. RemoteVideo.prototype._generatePopupContent = function() {
  110. if (interfaceConfig.filmStripOnly) {
  111. return;
  112. }
  113. const remoteVideoMenuContainer
  114. = this.container.querySelector('.remotevideomenu');
  115. if (!remoteVideoMenuContainer) {
  116. return;
  117. }
  118. const { controller } = APP.remoteControl;
  119. let remoteControlState = null;
  120. let onRemoteControlToggle;
  121. if (this._supportsRemoteControl
  122. && ((!APP.remoteControl.active && !this._isRemoteControlSessionActive)
  123. || APP.remoteControl.controller.activeParticipant === this.id)) {
  124. if (controller.getRequestedParticipant() === this.id) {
  125. remoteControlState = REMOTE_CONTROL_MENU_STATES.REQUESTING;
  126. } else if (controller.isStarted()) {
  127. onRemoteControlToggle = this._stopRemoteControl;
  128. remoteControlState = REMOTE_CONTROL_MENU_STATES.STARTED;
  129. } else {
  130. onRemoteControlToggle = this._requestRemoteControlPermissions;
  131. remoteControlState = REMOTE_CONTROL_MENU_STATES.NOT_STARTED;
  132. }
  133. }
  134. let initialVolumeValue, onVolumeChange;
  135. // Feature check for volume setting as temasys objects cannot adjust volume.
  136. if (this._canSetAudioVolume()) {
  137. initialVolumeValue = this._getAudioElement().volume;
  138. onVolumeChange = this._setAudioVolume;
  139. }
  140. const { isModerator } = APP.conference;
  141. const participantID = this.id;
  142. ReactDOM.render(
  143. <Provider store = { APP.store }>
  144. <I18nextProvider i18n = { i18next }>
  145. <AtlasKitThemeProvider mode = 'dark'>
  146. <RemoteVideoMenuTriggerButton
  147. initialVolumeValue = { initialVolumeValue }
  148. isAudioMuted = { this.isAudioMuted }
  149. isModerator = { isModerator }
  150. onMenuDisplay
  151. = {this._onRemoteVideoMenuDisplay.bind(this)}
  152. onRemoteControlToggle = { onRemoteControlToggle }
  153. onVolumeChange = { onVolumeChange }
  154. participantID = { participantID }
  155. remoteControlState = { remoteControlState } />
  156. </AtlasKitThemeProvider>
  157. </I18nextProvider>
  158. </Provider>,
  159. remoteVideoMenuContainer);
  160. };
  161. RemoteVideo.prototype._onRemoteVideoMenuDisplay = function() {
  162. this.updateRemoteVideoMenu();
  163. };
  164. /**
  165. * Sets the remote control active status for the remote video.
  166. *
  167. * @param {boolean} isActive - The new remote control active status.
  168. * @returns {void}
  169. */
  170. RemoteVideo.prototype.setRemoteControlActiveStatus = function(isActive) {
  171. this._isRemoteControlSessionActive = isActive;
  172. this.updateRemoteVideoMenu();
  173. };
  174. /**
  175. * Sets the remote control supported value and initializes or updates the menu
  176. * depending on the remote control is supported or not.
  177. * @param {boolean} isSupported
  178. */
  179. RemoteVideo.prototype.setRemoteControlSupport = function(isSupported = false) {
  180. if (this._supportsRemoteControl === isSupported) {
  181. return;
  182. }
  183. this._supportsRemoteControl = isSupported;
  184. this.updateRemoteVideoMenu();
  185. };
  186. /**
  187. * Requests permissions for remote control session.
  188. */
  189. RemoteVideo.prototype._requestRemoteControlPermissions = function() {
  190. APP.remoteControl.controller.requestPermissions(
  191. this.id, this.VideoLayout.getLargeVideoWrapper()).then(result => {
  192. if (result === null) {
  193. return;
  194. }
  195. this.updateRemoteVideoMenu();
  196. APP.UI.messageHandler.notify(
  197. 'dialog.remoteControlTitle',
  198. result === false ? 'dialog.remoteControlDeniedMessage'
  199. : 'dialog.remoteControlAllowedMessage',
  200. { user: this.user.getDisplayName()
  201. || interfaceConfig.DEFAULT_REMOTE_DISPLAY_NAME }
  202. );
  203. if (result === true) {
  204. // the remote control permissions has been granted
  205. // pin the controlled participant
  206. const pinnedParticipant
  207. = getPinnedParticipant(APP.store.getState()) || {};
  208. const pinnedId = pinnedParticipant.id;
  209. if (pinnedId !== this.id) {
  210. APP.store.dispatch(pinParticipant(this.id));
  211. }
  212. }
  213. }, error => {
  214. logger.error(error);
  215. this.updateRemoteVideoMenu();
  216. APP.UI.messageHandler.notify(
  217. 'dialog.remoteControlTitle',
  218. 'dialog.remoteControlErrorMessage',
  219. { user: this.user.getDisplayName()
  220. || interfaceConfig.DEFAULT_REMOTE_DISPLAY_NAME }
  221. );
  222. });
  223. this.updateRemoteVideoMenu();
  224. };
  225. /**
  226. * Stops remote control session.
  227. */
  228. RemoteVideo.prototype._stopRemoteControl = function() {
  229. // send message about stopping
  230. APP.remoteControl.controller.stop();
  231. this.updateRemoteVideoMenu();
  232. };
  233. /**
  234. * Get the remote participant's audio element.
  235. *
  236. * @returns {Element} audio element
  237. */
  238. RemoteVideo.prototype._getAudioElement = function() {
  239. return this._audioStreamElement;
  240. };
  241. /**
  242. * Check if the remote participant's audio can have its volume adjusted.
  243. *
  244. * @returns {boolean} true if the volume can be adjusted.
  245. */
  246. RemoteVideo.prototype._canSetAudioVolume = function() {
  247. const audioElement = this._getAudioElement();
  248. return audioElement && audioElement.volume !== undefined;
  249. };
  250. /**
  251. * Change the remote participant's volume level.
  252. *
  253. * @param {int} newVal - The value to set the slider to.
  254. */
  255. RemoteVideo.prototype._setAudioVolume = function(newVal) {
  256. if (this._canSetAudioVolume()) {
  257. this._getAudioElement().volume = newVal;
  258. }
  259. };
  260. /**
  261. * Updates the remote video menu.
  262. *
  263. * @param isMuted the new muted state to update to
  264. */
  265. RemoteVideo.prototype.updateRemoteVideoMenu = function(isMuted) {
  266. if (typeof isMuted !== 'undefined') {
  267. this.isAudioMuted = isMuted;
  268. }
  269. this._generatePopupContent();
  270. };
  271. /**
  272. * @inheritDoc
  273. * @override
  274. */
  275. RemoteVideo.prototype.setVideoMutedView = function(isMuted) {
  276. SmallVideo.prototype.setVideoMutedView.call(this, isMuted);
  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. RemoteVideo.prototype._figureOutMutedWhileDisconnected = function() {
  287. const isActive = this.isConnectionActive();
  288. if (!isActive && this.isVideoMuted) {
  289. this.mutedWhileDisconnected = true;
  290. } else if (isActive && !this.isVideoMuted) {
  291. this.mutedWhileDisconnected = false;
  292. }
  293. };
  294. /**
  295. * Removes the remote stream element corresponding to the given stream and
  296. * parent container.
  297. *
  298. * @param stream the MediaStream
  299. * @param isVideo <tt>true</tt> if given <tt>stream</tt> is a video one.
  300. */
  301. RemoteVideo.prototype.removeRemoteStreamElement = function(stream) {
  302. if (!this.container) {
  303. return false;
  304. }
  305. const isVideo = stream.isVideoTrack();
  306. const elementID = SmallVideo.getStreamElementID(stream);
  307. const select = $(`#${elementID}`);
  308. select.remove();
  309. if (isVideo) {
  310. this.wasVideoPlayed = false;
  311. }
  312. logger.info(`${isVideo ? 'Video' : 'Audio'
  313. } removed ${this.id}`, select);
  314. this.updateView();
  315. };
  316. /**
  317. * Checks whether the remote user associated with this <tt>RemoteVideo</tt>
  318. * has connectivity issues.
  319. *
  320. * @return {boolean} <tt>true</tt> if the user's connection is fine or
  321. * <tt>false</tt> otherwise.
  322. */
  323. RemoteVideo.prototype.isConnectionActive = function() {
  324. return this.user.getConnectionStatus()
  325. === JitsiParticipantConnectionStatus.ACTIVE;
  326. };
  327. /**
  328. * The remote video is considered "playable" once the stream has started
  329. * according to the {@link #hasVideoStarted} result.
  330. * It will be allowed to display video also in
  331. * {@link JitsiParticipantConnectionStatus.INTERRUPTED} if the video was ever
  332. * played and was not muted while not in ACTIVE state. This basically means
  333. * that there is stalled video image cached that could be displayed. It's used
  334. * to show "grey video image" in user's thumbnail when there are connectivity
  335. * issues.
  336. *
  337. * @inheritdoc
  338. * @override
  339. */
  340. RemoteVideo.prototype.isVideoPlayable = function() {
  341. const connectionState
  342. = APP.conference.getParticipantConnectionStatus(this.id);
  343. return SmallVideo.prototype.isVideoPlayable.call(this)
  344. && this.hasVideoStarted()
  345. && (connectionState === JitsiParticipantConnectionStatus.ACTIVE
  346. || (connectionState === JitsiParticipantConnectionStatus.INTERRUPTED
  347. && !this.mutedWhileDisconnected));
  348. };
  349. /**
  350. * @inheritDoc
  351. */
  352. RemoteVideo.prototype.updateView = function() {
  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. SmallVideo.prototype.updateView.call(this);
  358. };
  359. /**
  360. * Updates the UI to reflect user's connectivity status.
  361. */
  362. RemoteVideo.prototype.updateConnectionStatusIndicator = function() {
  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. const isInterrupted
  370. = connectionStatus === JitsiParticipantConnectionStatus.INTERRUPTED;
  371. // Toggle thumbnail video problem filter
  372. this.selectVideoElement().toggleClass(
  373. 'videoThumbnailProblemFilter', isInterrupted);
  374. this.$avatar().toggleClass(
  375. 'videoThumbnailProblemFilter', isInterrupted);
  376. };
  377. /**
  378. * Removes RemoteVideo from the page.
  379. */
  380. RemoteVideo.prototype.remove = function() {
  381. logger.log('Remove thumbnail', this.id);
  382. this.removeAudioLevelIndicator();
  383. const toolbarContainer
  384. = this.container.querySelector('.videocontainer__toolbar');
  385. if (toolbarContainer) {
  386. ReactDOM.unmountComponentAtNode(toolbarContainer);
  387. }
  388. this.removeConnectionIndicator();
  389. this.removeDisplayName();
  390. this.removeAvatar();
  391. this.removePresenceLabel();
  392. this._unmountIndicators();
  393. this.removeRemoteVideoMenu();
  394. // Remove whole container
  395. if (this.container.parentNode) {
  396. this.container.parentNode.removeChild(this.container);
  397. }
  398. };
  399. RemoteVideo.prototype.waitForPlayback = function(streamElement, stream) {
  400. const webRtcStream = stream.getOriginalStream();
  401. const isVideo = stream.isVideoTrack();
  402. if (!isVideo || webRtcStream.id === 'mixedmslabel') {
  403. return;
  404. }
  405. const self = this;
  406. // Triggers when video playback starts
  407. const onPlayingHandler = function() {
  408. self.wasVideoPlayed = true;
  409. self.VideoLayout.remoteVideoActive(streamElement, self.id);
  410. streamElement.onplaying = null;
  411. // Refresh to show the video
  412. self.updateView();
  413. };
  414. streamElement.onplaying = onPlayingHandler;
  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. RemoteVideo.prototype.hasVideoStarted = function() {
  423. return this.wasVideoPlayed;
  424. };
  425. RemoteVideo.prototype.addRemoteStreamElement = function(stream) {
  426. if (!this.container) {
  427. return;
  428. }
  429. const isVideo = stream.isVideoTrack();
  430. isVideo ? this.videoStream = stream : this.audioStream = stream;
  431. if (isVideo) {
  432. this.setVideoType(stream.videoType);
  433. }
  434. if (!stream.getOriginalStream()) {
  435. return;
  436. }
  437. let streamElement = SmallVideo.createStreamElement(stream);
  438. // Put new stream element always in front
  439. UIUtils.prependChild(this.container, streamElement);
  440. // If we hide element when Temasys plugin is used then
  441. // we'll never receive 'onplay' event and other logic won't work as expected
  442. // NOTE: hiding will not have effect when Temasys plugin is in use, as
  443. // calling attach will show it back
  444. $(streamElement).hide();
  445. // If the container is currently visible
  446. // we attach the stream to the element.
  447. if (!isVideo || (this.container.offsetParent !== null && isVideo)) {
  448. this.waitForPlayback(streamElement, stream);
  449. streamElement = stream.attach(streamElement);
  450. }
  451. if (!isVideo) {
  452. this._audioStreamElement = streamElement;
  453. // If the remote video menu was created before the audio stream was
  454. // attached we need to update the menu in order to show the volume
  455. // slider.
  456. this.updateRemoteVideoMenu();
  457. }
  458. };
  459. /**
  460. * Sets the display name for the given video span id.
  461. *
  462. * @param displayName the display name to set
  463. */
  464. RemoteVideo.prototype.setDisplayName = function(displayName) {
  465. if (!this.container) {
  466. logger.warn(`Unable to set displayName - ${this.videoSpanId
  467. } does not exist`);
  468. return;
  469. }
  470. this.updateDisplayName({
  471. displayName: displayName || interfaceConfig.DEFAULT_REMOTE_DISPLAY_NAME,
  472. elementID: `${this.videoSpanId}_name`,
  473. participantID: this.id
  474. });
  475. };
  476. /**
  477. * Removes remote video menu element from video element identified by
  478. * given <tt>videoElementId</tt>.
  479. *
  480. * @param videoElementId the id of local or remote video element.
  481. */
  482. RemoteVideo.prototype.removeRemoteVideoMenu = function() {
  483. const menuSpan = this.$container.find('.remotevideomenu');
  484. if (menuSpan.length) {
  485. ReactDOM.unmountComponentAtNode(menuSpan.get(0));
  486. menuSpan.remove();
  487. }
  488. };
  489. /**
  490. * Mounts the {@code PresenceLabel} for displaying the participant's current
  491. * presence status.
  492. *
  493. * @return {void}
  494. */
  495. RemoteVideo.prototype.addPresenceLabel = function() {
  496. const presenceLabelContainer
  497. = this.container.querySelector('.presence-label-container');
  498. if (presenceLabelContainer) {
  499. ReactDOM.render(
  500. <Provider store = { APP.store }>
  501. <I18nextProvider i18n = { i18next }>
  502. <PresenceLabel participantID = { this.id } />
  503. </I18nextProvider>
  504. </Provider>,
  505. presenceLabelContainer);
  506. }
  507. };
  508. /**
  509. * Unmounts the {@code PresenceLabel} component.
  510. *
  511. * @return {void}
  512. */
  513. RemoteVideo.prototype.removePresenceLabel = function() {
  514. const presenceLabelContainer
  515. = this.container.querySelector('.presence-label-container');
  516. if (presenceLabelContainer) {
  517. ReactDOM.unmountComponentAtNode(presenceLabelContainer);
  518. }
  519. };
  520. /**
  521. * Callback invoked when the thumbnail is clicked. Will directly call
  522. * VideoLayout to handle thumbnail click if certain elements have not been
  523. * clicked.
  524. *
  525. * @param {MouseEvent} event - The click event to intercept.
  526. * @private
  527. * @returns {void}
  528. */
  529. RemoteVideo.prototype._onContainerClick = function(event) {
  530. const $source = $(event.target || event.srcElement);
  531. const { classList } = event.target;
  532. const ignoreClick = $source.parents('.popover').length > 0
  533. || classList.contains('popover');
  534. if (!ignoreClick) {
  535. this._togglePin();
  536. }
  537. // On IE we need to populate this handler on video <object> and it does not
  538. // give event instance as an argument, so we check here for methods.
  539. if (event.stopPropagation && event.preventDefault && !ignoreClick) {
  540. event.stopPropagation();
  541. event.preventDefault();
  542. }
  543. return false;
  544. };
  545. RemoteVideo.createContainer = function(spanId) {
  546. const container = document.createElement('span');
  547. container.id = spanId;
  548. container.className = 'videocontainer';
  549. container.innerHTML = `
  550. <div class = 'videocontainer__background'></div>
  551. <div class = 'videocontainer__toptoolbar'></div>
  552. <div class = 'videocontainer__toolbar'></div>
  553. <div class = 'videocontainer__hoverOverlay'></div>
  554. <div class = 'displayNameContainer'></div>
  555. <div class = 'avatar-container'></div>
  556. <div class ='presence-label-container'></div>
  557. <span class = 'remotevideomenu'></span>`;
  558. const remotes = document.getElementById('filmstripRemoteVideosContainer');
  559. return remotes.appendChild(container);
  560. };
  561. export default RemoteVideo;