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

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