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

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