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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657
  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. /**
  294. * If chat state is open, we re-compute the container width by subtracting the default width of
  295. * the chat. We re-compute the width again after the chat window is closed. This is needed when
  296. * custom styling is configured on the large video container through the iFrame API.
  297. */
  298. if (isOpen && !this.resizedForChat) {
  299. widthToUse -= CHAT_SIZE;
  300. this.resizedForChat = true;
  301. } else if (this.resizedForChat) {
  302. this.resizedForChat = false;
  303. widthToUse += CHAT_SIZE;
  304. }
  305. this.width = widthToUse;
  306. this.height = this.preferredHeight || window.innerHeight;
  307. }
  308. /**
  309. * Resize Large container of specified type.
  310. * @param {string} type type of container which should be resized.
  311. * @param {boolean} [animate=false] if resize process should be animated.
  312. */
  313. resizeContainer(type, animate = false) {
  314. const container = this.getContainer(type);
  315. container.resize(this.width, this.height, animate);
  316. }
  317. /**
  318. * Resize all Large containers.
  319. * @param {boolean} animate if resize process should be animated.
  320. */
  321. resize(animate) {
  322. // resize all containers
  323. Object.keys(this.containers)
  324. .forEach(type => this.resizeContainer(type, animate));
  325. }
  326. /**
  327. * Updates the src of the dominant speaker avatar
  328. */
  329. updateAvatar() {
  330. ReactDOM.render(
  331. <Provider store = { APP.store }>
  332. <Avatar
  333. id = "dominantSpeakerAvatar"
  334. participantId = { this.id }
  335. size = { 200 } />
  336. </Provider>,
  337. this._dominantSpeakerAvatarContainer
  338. );
  339. }
  340. /**
  341. * Updates the audio level indicator of the large video.
  342. *
  343. * @param lvl the new audio level to set
  344. */
  345. updateLargeVideoAudioLevel(lvl) {
  346. AudioLevels.updateLargeVideoAudioLevel('dominantSpeaker', lvl);
  347. }
  348. /**
  349. * Displays a message of the passed in participant id's presence status. The
  350. * message will not display if the remote connection message is displayed.
  351. *
  352. * @param {string} id - The participant ID whose associated user's presence
  353. * status should be displayed.
  354. * @returns {void}
  355. */
  356. updatePresenceLabel(id) {
  357. const isConnectionMessageVisible
  358. = $('#remoteConnectionMessage').is(':visible');
  359. if (isConnectionMessageVisible) {
  360. this.removePresenceLabel();
  361. return;
  362. }
  363. const presenceLabelContainer = $('#remotePresenceMessage');
  364. if (presenceLabelContainer.length) {
  365. ReactDOM.render(
  366. <Provider store = { APP.store }>
  367. <I18nextProvider i18n = { i18next }>
  368. <PresenceLabel
  369. participantID = { id }
  370. className = 'presence-label' />
  371. </I18nextProvider>
  372. </Provider>,
  373. presenceLabelContainer.get(0));
  374. }
  375. }
  376. /**
  377. * Removes the messages about the displayed participant's presence status.
  378. *
  379. * @returns {void}
  380. */
  381. removePresenceLabel() {
  382. const presenceLabelContainer = $('#remotePresenceMessage');
  383. if (presenceLabelContainer.length) {
  384. ReactDOM.unmountComponentAtNode(presenceLabelContainer.get(0));
  385. }
  386. }
  387. /**
  388. * Show or hide watermark.
  389. * @param {boolean} show
  390. */
  391. showWatermark(show) {
  392. $('.watermark').css('visibility', show ? 'visible' : 'hidden');
  393. }
  394. /**
  395. * Shows hides the "avatar" message which is to be displayed either in
  396. * the middle of the screen or below the avatar image.
  397. *
  398. * @param {boolean|undefined} [show=undefined] <tt>true</tt> to show
  399. * the avatar message or <tt>false</tt> to hide it. If not provided then
  400. * the connection status of the user currently on the large video will be
  401. * obtained form "APP.conference" and the message will be displayed if
  402. * the user's connection is either interrupted or inactive.
  403. */
  404. showRemoteConnectionMessage(show) {
  405. if (typeof show !== 'boolean') {
  406. const connStatus
  407. = APP.conference.getParticipantConnectionStatus(this.id);
  408. // eslint-disable-next-line no-param-reassign
  409. show = !APP.conference.isLocalId(this.id)
  410. && (connStatus === JitsiParticipantConnectionStatus.INTERRUPTED
  411. || connStatus
  412. === JitsiParticipantConnectionStatus.INACTIVE);
  413. }
  414. if (show) {
  415. $('#remoteConnectionMessage').css({ display: 'block' });
  416. } else {
  417. $('#remoteConnectionMessage').hide();
  418. }
  419. }
  420. /**
  421. * Updates the text which describes that the remote user is having
  422. * connectivity issues.
  423. *
  424. * @param {string} msgKey the translation key which will be used to get
  425. * the message text.
  426. * @param {object} msgOptions translation options object.
  427. *
  428. * @private
  429. */
  430. _setRemoteConnectionMessage(msgKey, msgOptions) {
  431. if (msgKey) {
  432. $('#remoteConnectionMessage')
  433. .attr('data-i18n', msgKey)
  434. .attr('data-i18n-options', JSON.stringify(msgOptions));
  435. APP.translation.translateElement(
  436. $('#remoteConnectionMessage'), msgOptions);
  437. }
  438. }
  439. /**
  440. * Add container of specified type.
  441. * @param {string} type container type
  442. * @param {LargeContainer} container container to add.
  443. */
  444. addContainer(type, container) {
  445. if (this.containers[type]) {
  446. throw new Error(`container of type ${type} already exist`);
  447. }
  448. this.containers[type] = container;
  449. this.resizeContainer(type);
  450. }
  451. /**
  452. * Get Large container of specified type.
  453. * @param {string} type container type.
  454. * @returns {LargeContainer}
  455. */
  456. getContainer(type) {
  457. const container = this.containers[type];
  458. if (!container) {
  459. throw new Error(`container of type ${type} doesn't exist`);
  460. }
  461. return container;
  462. }
  463. /**
  464. * Returns {@link LargeContainer} for the current {@link state}
  465. *
  466. * @return {LargeContainer}
  467. *
  468. * @throws an <tt>Error</tt> if there is no container for the current
  469. * {@link state}.
  470. */
  471. getCurrentContainer() {
  472. return this.getContainer(this.state);
  473. }
  474. /**
  475. * Returns type of the current {@link LargeContainer}
  476. * @return {string}
  477. */
  478. getCurrentContainerType() {
  479. return this.state;
  480. }
  481. /**
  482. * Remove Large container of specified type.
  483. * @param {string} type container type.
  484. */
  485. removeContainer(type) {
  486. if (!this.containers[type]) {
  487. throw new Error(`container of type ${type} doesn't exist`);
  488. }
  489. delete this.containers[type];
  490. }
  491. /**
  492. * Show Large container of specified type.
  493. * Does nothing if such container is already visible.
  494. * @param {string} type container type.
  495. * @returns {Promise}
  496. */
  497. showContainer(type) {
  498. if (this.state === type) {
  499. return Promise.resolve();
  500. }
  501. const oldContainer = this.containers[this.state];
  502. // FIXME when video is being replaced with other content we need to hide
  503. // companion icons/messages. It would be best if the container would
  504. // be taking care of it by itself, but that is a bigger refactoring
  505. if (LargeVideoManager.isVideoContainer(this.state)) {
  506. this.showWatermark(false);
  507. this.showRemoteConnectionMessage(false);
  508. }
  509. oldContainer.hide();
  510. this.state = type;
  511. const container = this.getContainer(type);
  512. return container.show().then(() => {
  513. if (LargeVideoManager.isVideoContainer(type)) {
  514. // FIXME when video appears on top of other content we need to
  515. // show companion icons/messages. It would be best if
  516. // the container would be taking care of it by itself, but that
  517. // is a bigger refactoring
  518. this.showWatermark(true);
  519. // "avatar" and "video connection" can not be displayed both
  520. // at the same time, but the latter is of higher priority and it
  521. // will hide the avatar one if will be displayed.
  522. this.showRemoteConnectionMessage(/* fetch the current state */);
  523. }
  524. });
  525. }
  526. /**
  527. * Changes the flipX state of the local video.
  528. * @param val {boolean} true if flipped.
  529. */
  530. onLocalFlipXChange(val) {
  531. this.videoContainer.setLocalFlipX(val);
  532. }
  533. /**
  534. * Dispatches an action to update the known resolution state of the large video and adjusts container sizes when the
  535. * resolution changes.
  536. *
  537. * @private
  538. * @returns {void}
  539. */
  540. _onVideoResolutionUpdate() {
  541. const { height, width } = this.videoContainer.getStreamSize();
  542. const { resolution } = APP.store.getState()['features/large-video'];
  543. if (height !== resolution) {
  544. APP.store.dispatch(updateKnownLargeVideoResolution(height));
  545. }
  546. const currentAspectRatio = height === 0 ? 0 : width / height;
  547. if (this._videoAspectRatio !== currentAspectRatio) {
  548. this._videoAspectRatio = currentAspectRatio;
  549. this.resize();
  550. }
  551. }
  552. }