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

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