Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

RemoteVideo.js 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  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 state = APP.store.getState();
  288. const participant = getParticipantById(state, this.id);
  289. const connectionState = participant?.connectionStatus;
  290. const isActive = connectionState === JitsiParticipantConnectionStatus.ACTIVE;
  291. const isVideoMuted = isRemoteTrackMuted(state['features/base/tracks'], MEDIA_TYPE.VIDEO, this.id);
  292. if (!isActive && isVideoMuted) {
  293. this.mutedWhileDisconnected = true;
  294. } else if (isActive && !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. * The remote video is considered "playable" once the can play event has been received. It will be allowed to
  324. * display video also in {@link JitsiParticipantConnectionStatus.INTERRUPTED} if the video has received the canplay
  325. * event and was not muted while not in ACTIVE state. This basically means that there is stalled video image cached
  326. * that could be displayed. It's used to show "grey video image" in user's thumbnail when there are connectivity
  327. * issues.
  328. *
  329. * @inheritdoc
  330. * @override
  331. */
  332. isVideoPlayable() {
  333. const participant = getParticipantById(APP.store.getState(), this.id);
  334. const connectionState = participant?.connectionStatus;
  335. return super.isVideoPlayable()
  336. && this._canPlayEventReceived
  337. && (connectionState === JitsiParticipantConnectionStatus.ACTIVE
  338. || (connectionState === JitsiParticipantConnectionStatus.INTERRUPTED && !this.mutedWhileDisconnected));
  339. }
  340. /**
  341. * @inheritDoc
  342. */
  343. updateView() {
  344. this.$container.toggleClass('audio-only', APP.conference.isAudioOnly());
  345. this._figureOutMutedWhileDisconnected();
  346. super.updateView();
  347. }
  348. /**
  349. * Removes RemoteVideo from the page.
  350. */
  351. remove() {
  352. super.remove();
  353. this.removePresenceLabel();
  354. this.removeRemoteVideoMenu();
  355. }
  356. /**
  357. *
  358. * @param {*} streamElement
  359. * @param {*} stream
  360. */
  361. waitForPlayback(streamElement, stream) {
  362. const webRtcStream = stream.getOriginalStream();
  363. const isVideo = stream.isVideoTrack();
  364. if (!isVideo || webRtcStream.id === 'mixedmslabel') {
  365. return;
  366. }
  367. const listener = () => {
  368. this._canPlayEventReceived = true;
  369. this.VideoLayout.remoteVideoActive(streamElement, this.id);
  370. streamElement.removeEventListener('canplay', listener);
  371. // Refresh to show the video
  372. this.updateView();
  373. };
  374. streamElement.addEventListener('canplay', listener);
  375. }
  376. /**
  377. *
  378. * @param {*} stream
  379. */
  380. addRemoteStreamElement(stream) {
  381. if (!this.container) {
  382. logger.debug('Not attaching remote stream due to no container');
  383. return;
  384. }
  385. const isVideo = stream.isVideoTrack();
  386. if (isVideo) {
  387. this.videoStream = stream;
  388. } else {
  389. this.audioStream = stream;
  390. }
  391. if (!stream.getOriginalStream()) {
  392. logger.debug('Remote video stream has no original stream');
  393. return;
  394. }
  395. let streamElement = SmallVideo.createStreamElement(stream);
  396. // Put new stream element always in front
  397. streamElement = UIUtils.prependChild(this.container, streamElement);
  398. $(streamElement).hide();
  399. this.waitForPlayback(streamElement, stream);
  400. stream.attach(streamElement);
  401. if (!isVideo) {
  402. this._audioStreamElement = streamElement;
  403. // If the remote video menu was created before the audio stream was
  404. // attached we need to update the menu in order to show the volume
  405. // slider.
  406. this.updateRemoteVideoMenu();
  407. }
  408. }
  409. /**
  410. * Triggers re-rendering of the display name using current instance state.
  411. *
  412. * @returns {void}
  413. */
  414. updateDisplayName() {
  415. if (!this.container) {
  416. logger.warn(`Unable to set displayName - ${this.videoSpanId} does not exist`);
  417. return;
  418. }
  419. this._renderDisplayName({
  420. elementID: `${this.videoSpanId}_name`,
  421. participantID: this.id
  422. });
  423. }
  424. /**
  425. * Removes remote video menu element from video element identified by
  426. * given <tt>videoElementId</tt>.
  427. *
  428. * @param videoElementId the id of local or remote video element.
  429. */
  430. removeRemoteVideoMenu() {
  431. const menuSpan = this.$container.find('.remotevideomenu');
  432. if (menuSpan.length) {
  433. ReactDOM.unmountComponentAtNode(menuSpan.get(0));
  434. menuSpan.remove();
  435. }
  436. }
  437. /**
  438. * Mounts the {@code PresenceLabel} for displaying the participant's current
  439. * presence status.
  440. *
  441. * @return {void}
  442. */
  443. addPresenceLabel() {
  444. const presenceLabelContainer = this.container.querySelector('.presence-label-container');
  445. if (presenceLabelContainer) {
  446. ReactDOM.render(
  447. <Provider store = { APP.store }>
  448. <I18nextProvider i18n = { i18next }>
  449. <PresenceLabel
  450. participantID = { this.id }
  451. className = 'presence-label' />
  452. </I18nextProvider>
  453. </Provider>,
  454. presenceLabelContainer);
  455. }
  456. }
  457. /**
  458. * Unmounts the {@code PresenceLabel} component.
  459. *
  460. * @return {void}
  461. */
  462. removePresenceLabel() {
  463. const presenceLabelContainer = this.container.querySelector('.presence-label-container');
  464. if (presenceLabelContainer) {
  465. ReactDOM.unmountComponentAtNode(presenceLabelContainer);
  466. }
  467. }
  468. }