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

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