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

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