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

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