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

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