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

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