Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

LargeVideoManager.js 20KB

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