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

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