您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

RemoteVideo.js 19KB

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