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.

LargeVideoManager.js 21KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668
  1. /* global $, APP */
  2. /* eslint-disable no-unused-vars */
  3. import Logger from 'jitsi-meet-logger';
  4. import React from 'react';
  5. import ReactDOM from 'react-dom';
  6. import { I18nextProvider } from 'react-i18next';
  7. import { Provider } from 'react-redux';
  8. import { createScreenSharingIssueEvent, sendAnalytics } from '../../../react/features/analytics';
  9. import { Avatar } from '../../../react/features/base/avatar';
  10. import { i18next } from '../../../react/features/base/i18n';
  11. import {
  12. JitsiParticipantConnectionStatus
  13. } from '../../../react/features/base/lib-jitsi-meet';
  14. import { MEDIA_TYPE, VIDEO_TYPE } from '../../../react/features/base/media';
  15. import { getParticipantById } from '../../../react/features/base/participants';
  16. import { getTrackByMediaTypeAndParticipant } from '../../../react/features/base/tracks';
  17. import { CHAT_SIZE } from '../../../react/features/chat';
  18. import {
  19. updateKnownLargeVideoResolution
  20. } from '../../../react/features/large-video/actions';
  21. import { PresenceLabel } from '../../../react/features/presence-status';
  22. import { shouldDisplayTileView } from '../../../react/features/video-layout';
  23. /* eslint-enable no-unused-vars */
  24. import UIEvents from '../../../service/UI/UIEvents';
  25. import { createDeferred } from '../../util/helpers';
  26. import AudioLevels from '../audio_levels/AudioLevels';
  27. import { VideoContainer, VIDEO_CONTAINER_TYPE } from './VideoContainer';
  28. const logger = Logger.getLogger(__filename);
  29. const DESKTOP_CONTAINER_TYPE = 'desktop';
  30. /**
  31. * Manager for all Large containers.
  32. */
  33. export default class LargeVideoManager {
  34. /**
  35. * Checks whether given container is a {@link VIDEO_CONTAINER_TYPE}.
  36. * FIXME currently this is a workaround for the problem where video type is
  37. * mixed up with container type.
  38. * @param {string} containerType
  39. * @return {boolean}
  40. */
  41. static isVideoContainer(containerType) {
  42. return containerType === VIDEO_CONTAINER_TYPE
  43. || containerType === DESKTOP_CONTAINER_TYPE;
  44. }
  45. /**
  46. *
  47. */
  48. constructor(emitter) {
  49. /**
  50. * The map of <tt>LargeContainer</tt>s where the key is the video
  51. * container type.
  52. * @type {Object.<string, LargeContainer>}
  53. */
  54. this.containers = {};
  55. this.eventEmitter = emitter;
  56. this.state = VIDEO_CONTAINER_TYPE;
  57. // FIXME: We are passing resizeContainer as parameter which is calling
  58. // Container.resize. Probably there's better way to implement this.
  59. this.videoContainer = new VideoContainer(
  60. () => this.resizeContainer(VIDEO_CONTAINER_TYPE), emitter);
  61. this.addContainer(VIDEO_CONTAINER_TYPE, this.videoContainer);
  62. // use the same video container to handle desktop tracks
  63. this.addContainer(DESKTOP_CONTAINER_TYPE, this.videoContainer);
  64. /**
  65. * The preferred width passed as an argument to {@link updateContainerSize}.
  66. *
  67. * @type {number|undefined}
  68. */
  69. this.preferredWidth = undefined;
  70. /**
  71. * The preferred height passed as an argument to {@link updateContainerSize}.
  72. *
  73. * @type {number|undefined}
  74. */
  75. this.preferredHeight = undefined;
  76. /**
  77. * The calculated width that will be used for the large video.
  78. * @type {number}
  79. */
  80. this.width = 0;
  81. /**
  82. * The calculated height that will be used for the large video.
  83. * @type {number}
  84. */
  85. this.height = 0;
  86. /**
  87. * Cache the aspect ratio of the video displayed to detect changes to
  88. * the aspect ratio on video resize events.
  89. *
  90. * @type {number}
  91. */
  92. this._videoAspectRatio = 0;
  93. this.$container = $('#largeVideoContainer');
  94. this.$container.css({
  95. display: 'inline-block'
  96. });
  97. this.$container.hover(
  98. e => this.onHoverIn(e),
  99. e => this.onHoverOut(e)
  100. );
  101. // Bind event handler so it is only bound once for every instance.
  102. this._onVideoResolutionUpdate
  103. = this._onVideoResolutionUpdate.bind(this);
  104. this.videoContainer.addResizeListener(this._onVideoResolutionUpdate);
  105. this._dominantSpeakerAvatarContainer
  106. = document.getElementById('dominantSpeakerAvatarContainer');
  107. }
  108. /**
  109. * Removes any listeners registered on child components, including
  110. * React Components.
  111. *
  112. * @returns {void}
  113. */
  114. destroy() {
  115. this.videoContainer.removeResizeListener(
  116. this._onVideoResolutionUpdate);
  117. this.removePresenceLabel();
  118. ReactDOM.unmountComponentAtNode(this._dominantSpeakerAvatarContainer);
  119. this.$container.css({ display: 'none' });
  120. }
  121. /**
  122. *
  123. */
  124. onHoverIn(e) {
  125. if (!this.state) {
  126. return;
  127. }
  128. const container = this.getCurrentContainer();
  129. container.onHoverIn(e);
  130. }
  131. /**
  132. *
  133. */
  134. onHoverOut(e) {
  135. if (!this.state) {
  136. return;
  137. }
  138. const container = this.getCurrentContainer();
  139. container.onHoverOut(e);
  140. }
  141. /**
  142. *
  143. */
  144. get id() {
  145. const container = this.getCurrentContainer();
  146. // If a user switch for large video is in progress then provide what
  147. // will be the end result of the update.
  148. if (this.updateInProcess
  149. && this.newStreamData
  150. && this.newStreamData.id !== container.id) {
  151. return this.newStreamData.id;
  152. }
  153. return container.id;
  154. }
  155. /**
  156. *
  157. */
  158. scheduleLargeVideoUpdate() {
  159. if (this.updateInProcess || !this.newStreamData) {
  160. return;
  161. }
  162. this.updateInProcess = true;
  163. // Include hide()/fadeOut only if we're switching between users
  164. // eslint-disable-next-line eqeqeq
  165. const container = this.getCurrentContainer();
  166. const isUserSwitch = this.newStreamData.id !== container.id;
  167. const preUpdate = isUserSwitch ? container.hide() : Promise.resolve();
  168. preUpdate.then(() => {
  169. const { id, stream, videoType, resolve } = this.newStreamData;
  170. // FIXME this does not really make sense, because the videoType
  171. // (camera or desktop) is a completely different thing than
  172. // the video container type (Etherpad, SharedVideo, VideoContainer).
  173. const isVideoContainer = LargeVideoManager.isVideoContainer(videoType);
  174. this.newStreamData = null;
  175. logger.info(`hover in ${id}`);
  176. this.state = videoType;
  177. // eslint-disable-next-line no-shadow
  178. const container = this.getCurrentContainer();
  179. container.setStream(id, stream, videoType);
  180. // change the avatar url on large
  181. this.updateAvatar();
  182. const isVideoMuted = !stream || stream.isMuted();
  183. const state = APP.store.getState();
  184. const participant = getParticipantById(state, id);
  185. const connectionStatus = participant?.connectionStatus;
  186. const isVideoRenderable = !isVideoMuted
  187. && (APP.conference.isLocalId(id) || connectionStatus === JitsiParticipantConnectionStatus.ACTIVE);
  188. const isAudioOnly = APP.conference.isAudioOnly();
  189. const showAvatar
  190. = isVideoContainer
  191. && ((isAudioOnly && videoType !== VIDEO_TYPE.DESKTOP) || !isVideoRenderable);
  192. let promise;
  193. // do not show stream if video is muted
  194. // but we still should show watermark
  195. if (showAvatar) {
  196. this.showWatermark(true);
  197. // If the intention of this switch is to show the avatar
  198. // we need to make sure that the video is hidden
  199. promise = container.hide();
  200. if ((!shouldDisplayTileView(state) || participant?.pinned) // In theory the tile view may not be
  201. // enabled yet when we auto pin the participant.
  202. && participant && !participant.local && !participant.isFakeParticipant) {
  203. // remote participant only
  204. const track = getTrackByMediaTypeAndParticipant(
  205. state['features/base/tracks'], MEDIA_TYPE.VIDEO, id);
  206. const isScreenSharing = track?.videoType === 'desktop';
  207. if (isScreenSharing) {
  208. // send the event
  209. sendAnalytics(createScreenSharingIssueEvent({
  210. source: 'large-video',
  211. connectionStatus,
  212. isVideoMuted,
  213. isAudioOnly,
  214. isVideoContainer,
  215. videoType
  216. }));
  217. }
  218. }
  219. } else {
  220. promise = container.show();
  221. }
  222. // show the avatar on large if needed
  223. container.showAvatar(showAvatar);
  224. // Clean up audio level after previous speaker.
  225. if (showAvatar) {
  226. this.updateLargeVideoAudioLevel(0);
  227. }
  228. const messageKey
  229. = connectionStatus === JitsiParticipantConnectionStatus.INACTIVE ? 'connection.LOW_BANDWIDTH' : null;
  230. // Do not show connection status message in the audio only mode,
  231. // because it's based on the video playback status.
  232. const overrideAndHide = APP.conference.isAudioOnly();
  233. this.updateParticipantConnStatusIndication(
  234. id,
  235. !overrideAndHide && messageKey);
  236. // Change the participant id the presence label is listening to.
  237. this.updatePresenceLabel(id);
  238. this.videoContainer.positionRemoteStatusMessages();
  239. // resolve updateLargeVideo promise after everything is done
  240. promise.then(resolve);
  241. return promise;
  242. }).then(() => {
  243. // after everything is done check again if there are any pending
  244. // new streams.
  245. this.updateInProcess = false;
  246. this.eventEmitter.emit(UIEvents.LARGE_VIDEO_ID_CHANGED, this.id);
  247. this.scheduleLargeVideoUpdate();
  248. });
  249. }
  250. /**
  251. * Shows/hides notification about participant's connectivity issues to be
  252. * shown on the large video area.
  253. *
  254. * @param {string} id the id of remote participant(MUC nickname)
  255. * @param {string|null} messageKey the i18n key of the message which will be
  256. * displayed on the large video or <tt>null</tt> to hide it.
  257. *
  258. * @private
  259. */
  260. updateParticipantConnStatusIndication(id, messageKey) {
  261. if (messageKey) {
  262. // Get user's display name
  263. const displayName
  264. = APP.conference.getParticipantDisplayName(id);
  265. this._setRemoteConnectionMessage(
  266. messageKey,
  267. { displayName });
  268. // Show it now only if the VideoContainer is on top
  269. this.showRemoteConnectionMessage(
  270. LargeVideoManager.isVideoContainer(this.state));
  271. } else {
  272. // Hide the message
  273. this.showRemoteConnectionMessage(false);
  274. }
  275. }
  276. /**
  277. * Update large video.
  278. * Switches to large video even if previously other container was visible.
  279. * @param userID the userID of the participant associated with the stream
  280. * @param {JitsiTrack?} stream new stream
  281. * @param {string?} videoType new video type
  282. * @returns {Promise}
  283. */
  284. updateLargeVideo(userID, stream, videoType) {
  285. if (this.newStreamData) {
  286. this.newStreamData.reject();
  287. }
  288. this.newStreamData = createDeferred();
  289. this.newStreamData.id = userID;
  290. this.newStreamData.stream = stream;
  291. this.newStreamData.videoType = videoType;
  292. this.scheduleLargeVideoUpdate();
  293. return this.newStreamData.promise;
  294. }
  295. /**
  296. * Update container size.
  297. */
  298. updateContainerSize(width, height) {
  299. if (typeof width === 'number') {
  300. this.preferredWidth = width;
  301. }
  302. if (typeof height === 'number') {
  303. this.preferredHeight = height;
  304. }
  305. let widthToUse = this.preferredWidth || window.innerWidth;
  306. const { isOpen } = APP.store.getState()['features/chat'];
  307. if (isOpen && window.innerWidth > 580) {
  308. /**
  309. * If chat state is open, we re-compute the container width
  310. * by subtracting the default width of the chat.
  311. */
  312. widthToUse -= CHAT_SIZE;
  313. }
  314. this.width = widthToUse;
  315. this.height = this.preferredHeight || window.innerHeight;
  316. }
  317. /**
  318. * Resize Large container of specified type.
  319. * @param {string} type type of container which should be resized.
  320. * @param {boolean} [animate=false] if resize process should be animated.
  321. */
  322. resizeContainer(type, animate = false) {
  323. const container = this.getContainer(type);
  324. container.resize(this.width, this.height, animate);
  325. }
  326. /**
  327. * Resize all Large containers.
  328. * @param {boolean} animate if resize process should be animated.
  329. */
  330. resize(animate) {
  331. // resize all containers
  332. Object.keys(this.containers)
  333. .forEach(type => this.resizeContainer(type, animate));
  334. }
  335. /**
  336. * Updates the src of the dominant speaker avatar
  337. */
  338. updateAvatar() {
  339. ReactDOM.render(
  340. <Provider store = { APP.store }>
  341. <Avatar
  342. id = "dominantSpeakerAvatar"
  343. participantId = { this.id }
  344. size = { 200 } />
  345. </Provider>,
  346. this._dominantSpeakerAvatarContainer
  347. );
  348. }
  349. /**
  350. * Updates the audio level indicator of the large video.
  351. *
  352. * @param lvl the new audio level to set
  353. */
  354. updateLargeVideoAudioLevel(lvl) {
  355. AudioLevels.updateLargeVideoAudioLevel('dominantSpeaker', lvl);
  356. }
  357. /**
  358. * Displays a message of the passed in participant id's presence status. The
  359. * message will not display if the remote connection message is displayed.
  360. *
  361. * @param {string} id - The participant ID whose associated user's presence
  362. * status should be displayed.
  363. * @returns {void}
  364. */
  365. updatePresenceLabel(id) {
  366. const isConnectionMessageVisible
  367. = $('#remoteConnectionMessage').is(':visible');
  368. if (isConnectionMessageVisible) {
  369. this.removePresenceLabel();
  370. return;
  371. }
  372. const presenceLabelContainer = $('#remotePresenceMessage');
  373. if (presenceLabelContainer.length) {
  374. ReactDOM.render(
  375. <Provider store = { APP.store }>
  376. <I18nextProvider i18n = { i18next }>
  377. <PresenceLabel
  378. participantID = { id }
  379. className = 'presence-label' />
  380. </I18nextProvider>
  381. </Provider>,
  382. presenceLabelContainer.get(0));
  383. }
  384. }
  385. /**
  386. * Removes the messages about the displayed participant's presence status.
  387. *
  388. * @returns {void}
  389. */
  390. removePresenceLabel() {
  391. const presenceLabelContainer = $('#remotePresenceMessage');
  392. if (presenceLabelContainer.length) {
  393. ReactDOM.unmountComponentAtNode(presenceLabelContainer.get(0));
  394. }
  395. }
  396. /**
  397. * Show or hide watermark.
  398. * @param {boolean} show
  399. */
  400. showWatermark(show) {
  401. $('.watermark').css('visibility', show ? 'visible' : 'hidden');
  402. }
  403. /**
  404. * Shows hides the "avatar" message which is to be displayed either in
  405. * the middle of the screen or below the avatar image.
  406. *
  407. * @param {boolean|undefined} [show=undefined] <tt>true</tt> to show
  408. * the avatar message or <tt>false</tt> to hide it. If not provided then
  409. * the connection status of the user currently on the large video will be
  410. * obtained form "APP.conference" and the message will be displayed if
  411. * the user's connection is either interrupted or inactive.
  412. */
  413. showRemoteConnectionMessage(show) {
  414. if (typeof show !== 'boolean') {
  415. const participant = getParticipantById(APP.store.getState(), this.id);
  416. const connStatus = participant?.connectionStatus;
  417. // eslint-disable-next-line no-param-reassign
  418. show = !APP.conference.isLocalId(this.id)
  419. && (connStatus === JitsiParticipantConnectionStatus.INTERRUPTED
  420. || connStatus
  421. === JitsiParticipantConnectionStatus.INACTIVE);
  422. }
  423. if (show) {
  424. $('#remoteConnectionMessage').css({ display: 'block' });
  425. } else {
  426. $('#remoteConnectionMessage').hide();
  427. }
  428. }
  429. /**
  430. * Updates the text which describes that the remote user is having
  431. * connectivity issues.
  432. *
  433. * @param {string} msgKey the translation key which will be used to get
  434. * the message text.
  435. * @param {object} msgOptions translation options object.
  436. *
  437. * @private
  438. */
  439. _setRemoteConnectionMessage(msgKey, msgOptions) {
  440. if (msgKey) {
  441. $('#remoteConnectionMessage')
  442. .attr('data-i18n', msgKey)
  443. .attr('data-i18n-options', JSON.stringify(msgOptions));
  444. APP.translation.translateElement(
  445. $('#remoteConnectionMessage'), msgOptions);
  446. }
  447. }
  448. /**
  449. * Add container of specified type.
  450. * @param {string} type container type
  451. * @param {LargeContainer} container container to add.
  452. */
  453. addContainer(type, container) {
  454. if (this.containers[type]) {
  455. throw new Error(`container of type ${type} already exist`);
  456. }
  457. this.containers[type] = container;
  458. this.resizeContainer(type);
  459. }
  460. /**
  461. * Get Large container of specified type.
  462. * @param {string} type container type.
  463. * @returns {LargeContainer}
  464. */
  465. getContainer(type) {
  466. const container = this.containers[type];
  467. if (!container) {
  468. throw new Error(`container of type ${type} doesn't exist`);
  469. }
  470. return container;
  471. }
  472. /**
  473. * Returns {@link LargeContainer} for the current {@link state}
  474. *
  475. * @return {LargeContainer}
  476. *
  477. * @throws an <tt>Error</tt> if there is no container for the current
  478. * {@link state}.
  479. */
  480. getCurrentContainer() {
  481. return this.getContainer(this.state);
  482. }
  483. /**
  484. * Returns type of the current {@link LargeContainer}
  485. * @return {string}
  486. */
  487. getCurrentContainerType() {
  488. return this.state;
  489. }
  490. /**
  491. * Remove Large container of specified type.
  492. * @param {string} type container type.
  493. */
  494. removeContainer(type) {
  495. if (!this.containers[type]) {
  496. throw new Error(`container of type ${type} doesn't exist`);
  497. }
  498. delete this.containers[type];
  499. }
  500. /**
  501. * Show Large container of specified type.
  502. * Does nothing if such container is already visible.
  503. * @param {string} type container type.
  504. * @returns {Promise}
  505. */
  506. showContainer(type) {
  507. if (this.state === type) {
  508. return Promise.resolve();
  509. }
  510. const oldContainer = this.containers[this.state];
  511. // FIXME when video is being replaced with other content we need to hide
  512. // companion icons/messages. It would be best if the container would
  513. // be taking care of it by itself, but that is a bigger refactoring
  514. if (LargeVideoManager.isVideoContainer(this.state)) {
  515. this.showWatermark(false);
  516. this.showRemoteConnectionMessage(false);
  517. }
  518. oldContainer.hide();
  519. this.state = type;
  520. const container = this.getContainer(type);
  521. return container.show().then(() => {
  522. if (LargeVideoManager.isVideoContainer(type)) {
  523. // FIXME when video appears on top of other content we need to
  524. // show companion icons/messages. It would be best if
  525. // the container would be taking care of it by itself, but that
  526. // is a bigger refactoring
  527. this.showWatermark(true);
  528. // "avatar" and "video connection" can not be displayed both
  529. // at the same time, but the latter is of higher priority and it
  530. // will hide the avatar one if will be displayed.
  531. this.showRemoteConnectionMessage(/* fetch the current state */);
  532. }
  533. });
  534. }
  535. /**
  536. * Changes the flipX state of the local video.
  537. * @param val {boolean} true if flipped.
  538. */
  539. onLocalFlipXChange(val) {
  540. this.videoContainer.setLocalFlipX(val);
  541. }
  542. /**
  543. * Dispatches an action to update the known resolution state of the large video and adjusts container sizes when the
  544. * resolution changes.
  545. *
  546. * @private
  547. * @returns {void}
  548. */
  549. _onVideoResolutionUpdate() {
  550. const { height, width } = this.videoContainer.getStreamSize();
  551. const { resolution } = APP.store.getState()['features/large-video'];
  552. if (height !== resolution) {
  553. APP.store.dispatch(updateKnownLargeVideoResolution(height));
  554. }
  555. const currentAspectRatio = height === 0 ? 0 : width / height;
  556. if (this._videoAspectRatio !== currentAspectRatio) {
  557. this._videoAspectRatio = currentAspectRatio;
  558. this.resize();
  559. }
  560. }
  561. }