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

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