您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

LargeVideoManager.js 21KB

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