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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716
  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. this.$container.animate({
  334. width: this.width,
  335. height: this.height
  336. }, {
  337. queue: false,
  338. duration: animate ? 500 : 0
  339. });
  340. }
  341. /**
  342. * Enables/disables the filter indicating a video problem to the user caused
  343. * by the problems with local media connection.
  344. *
  345. * @param enable <tt>true</tt> to enable, <tt>false</tt> to disable
  346. */
  347. enableLocalConnectionProblemFilter(enable) {
  348. this.videoContainer.enableLocalConnectionProblemFilter(enable);
  349. }
  350. /**
  351. * Updates the src of the dominant speaker avatar
  352. */
  353. updateAvatar(avatarUrl) {
  354. $('#dominantSpeakerAvatar').attr('src', avatarUrl);
  355. }
  356. /**
  357. * Updates the audio level indicator of the large video.
  358. *
  359. * @param lvl the new audio level to set
  360. */
  361. updateLargeVideoAudioLevel(lvl) {
  362. AudioLevels.updateLargeVideoAudioLevel('dominantSpeaker', lvl);
  363. }
  364. /**
  365. * Displays a message of the passed in participant id's presence status. The
  366. * message will not display if the remote connection message is displayed.
  367. *
  368. * @param {string} id - The participant ID whose associated user's presence
  369. * status should be displayed.
  370. * @returns {void}
  371. */
  372. updatePresenceLabel(id) {
  373. const isConnectionMessageVisible
  374. = $('#remoteConnectionMessage').is(':visible');
  375. if (isConnectionMessageVisible) {
  376. this.removePresenceLabel();
  377. return;
  378. }
  379. const presenceLabelContainer = $('#remotePresenceMessage');
  380. if (presenceLabelContainer.length) {
  381. ReactDOM.render(
  382. <Provider store = { APP.store }>
  383. <I18nextProvider i18n = { i18next }>
  384. <PresenceLabel participantID = { id } />
  385. </I18nextProvider>
  386. </Provider>,
  387. presenceLabelContainer.get(0));
  388. }
  389. }
  390. /**
  391. * Removes the messages about the displayed participant's presence status.
  392. *
  393. * @returns {void}
  394. */
  395. removePresenceLabel() {
  396. const presenceLabelContainer = $('#remotePresenceMessage');
  397. if (presenceLabelContainer.length) {
  398. ReactDOM.unmountComponentAtNode(presenceLabelContainer.get(0));
  399. }
  400. }
  401. /**
  402. * Show or hide watermark.
  403. * @param {boolean} show
  404. */
  405. showWatermark(show) {
  406. $('.watermark').css('visibility', show ? 'visible' : 'hidden');
  407. }
  408. /**
  409. * Shows/hides the message indicating problems with local media connection.
  410. * @param {boolean|null} show(optional) tells whether the message is to be
  411. * displayed or not. If missing the condition will be based on the value
  412. * obtained from {@link APP.conference.isConnectionInterrupted}.
  413. */
  414. showLocalConnectionMessage(show) {
  415. if (typeof show !== 'boolean') {
  416. // eslint-disable-next-line no-param-reassign
  417. show = APP.conference.isConnectionInterrupted();
  418. }
  419. const id = 'localConnectionMessage';
  420. UIUtil.setVisible(id, show);
  421. if (show) {
  422. // Avatar message conflicts with 'videoConnectionMessage',
  423. // so it must be hidden
  424. this.showRemoteConnectionMessage(false);
  425. }
  426. }
  427. /**
  428. * Shows hides the "avatar" message which is to be displayed either in
  429. * the middle of the screen or below the avatar image.
  430. *
  431. * @param {boolean|undefined} [show=undefined] <tt>true</tt> to show
  432. * the avatar message or <tt>false</tt> to hide it. If not provided then
  433. * the connection status of the user currently on the large video will be
  434. * obtained form "APP.conference" and the message will be displayed if
  435. * the user's connection is either interrupted or inactive.
  436. */
  437. showRemoteConnectionMessage(show) {
  438. if (typeof show !== 'boolean') {
  439. const connStatus
  440. = APP.conference.getParticipantConnectionStatus(this.id);
  441. // eslint-disable-next-line no-param-reassign
  442. show = !APP.conference.isLocalId(this.id)
  443. && (connStatus === JitsiParticipantConnectionStatus.INTERRUPTED
  444. || connStatus
  445. === JitsiParticipantConnectionStatus.INACTIVE);
  446. }
  447. if (show) {
  448. $('#remoteConnectionMessage').css({ display: 'block' });
  449. // 'videoConnectionMessage' message conflicts with 'avatarMessage',
  450. // so it must be hidden
  451. this.showLocalConnectionMessage(false);
  452. } else {
  453. $('#remoteConnectionMessage').hide();
  454. }
  455. }
  456. /**
  457. * Updates the text which describes that the remote user is having
  458. * connectivity issues.
  459. *
  460. * @param {string} msgKey the translation key which will be used to get
  461. * the message text.
  462. * @param {object} msgOptions translation options object.
  463. *
  464. * @private
  465. */
  466. _setRemoteConnectionMessage(msgKey, msgOptions) {
  467. if (msgKey) {
  468. $('#remoteConnectionMessage')
  469. .attr('data-i18n', msgKey)
  470. .attr('data-i18n-options', JSON.stringify(msgOptions));
  471. APP.translation.translateElement(
  472. $('#remoteConnectionMessage'), msgOptions);
  473. }
  474. }
  475. /**
  476. * Updated the text which is to be shown on the top of large video, when
  477. * local media connection is interrupted.
  478. *
  479. * @param {string} msgKey the translation key which will be used to get
  480. * the message text to be displayed on the large video.
  481. *
  482. * @private
  483. */
  484. _setLocalConnectionMessage(msgKey) {
  485. $('#localConnectionMessage')
  486. .attr('data-i18n', msgKey);
  487. APP.translation.translateElement($('#localConnectionMessage'));
  488. }
  489. /**
  490. * Add container of specified type.
  491. * @param {string} type container type
  492. * @param {LargeContainer} container container to add.
  493. */
  494. addContainer(type, container) {
  495. if (this.containers[type]) {
  496. throw new Error(`container of type ${type} already exist`);
  497. }
  498. this.containers[type] = container;
  499. this.resizeContainer(type);
  500. }
  501. /**
  502. * Get Large container of specified type.
  503. * @param {string} type container type.
  504. * @returns {LargeContainer}
  505. */
  506. getContainer(type) {
  507. const container = this.containers[type];
  508. if (!container) {
  509. throw new Error(`container of type ${type} doesn't exist`);
  510. }
  511. return container;
  512. }
  513. /**
  514. * Returns {@link LargeContainer} for the current {@link state}
  515. *
  516. * @return {LargeContainer}
  517. *
  518. * @throws an <tt>Error</tt> if there is no container for the current
  519. * {@link state}.
  520. */
  521. getCurrentContainer() {
  522. return this.getContainer(this.state);
  523. }
  524. /**
  525. * Returns type of the current {@link LargeContainer}
  526. * @return {string}
  527. */
  528. getCurrentContainerType() {
  529. return this.state;
  530. }
  531. /**
  532. * Remove Large container of specified type.
  533. * @param {string} type container type.
  534. */
  535. removeContainer(type) {
  536. if (!this.containers[type]) {
  537. throw new Error(`container of type ${type} doesn't exist`);
  538. }
  539. delete this.containers[type];
  540. }
  541. /**
  542. * Show Large container of specified type.
  543. * Does nothing if such container is already visible.
  544. * @param {string} type container type.
  545. * @returns {Promise}
  546. */
  547. showContainer(type) {
  548. if (this.state === type) {
  549. return Promise.resolve();
  550. }
  551. const oldContainer = this.containers[this.state];
  552. // FIXME when video is being replaced with other content we need to hide
  553. // companion icons/messages. It would be best if the container would
  554. // be taking care of it by itself, but that is a bigger refactoring
  555. if (LargeVideoManager.isVideoContainer(this.state)) {
  556. this.showWatermark(false);
  557. this.showLocalConnectionMessage(false);
  558. this.showRemoteConnectionMessage(false);
  559. }
  560. oldContainer.hide();
  561. this.state = type;
  562. const container = this.getContainer(type);
  563. return container.show().then(() => {
  564. if (LargeVideoManager.isVideoContainer(type)) {
  565. // FIXME when video appears on top of other content we need to
  566. // show companion icons/messages. It would be best if
  567. // the container would be taking care of it by itself, but that
  568. // is a bigger refactoring
  569. this.showWatermark(true);
  570. // "avatar" and "video connection" can not be displayed both
  571. // at the same time, but the latter is of higher priority and it
  572. // will hide the avatar one if will be displayed.
  573. this.showRemoteConnectionMessage(/* fetch the current state */);
  574. this.showLocalConnectionMessage(/* fetch the current state */);
  575. }
  576. });
  577. }
  578. /**
  579. * Changes the flipX state of the local video.
  580. * @param val {boolean} true if flipped.
  581. */
  582. onLocalFlipXChange(val) {
  583. this.videoContainer.setLocalFlipX(val);
  584. }
  585. /**
  586. * Dispatches an action to update the known resolution state of the
  587. * large video and adjusts container sizes when the resolution changes.
  588. *
  589. * @private
  590. * @returns {void}
  591. */
  592. _onVideoResolutionUpdate() {
  593. const { height, width } = this.videoContainer.getStreamSize();
  594. const { resolution } = APP.store.getState()['features/large-video'];
  595. if (height !== resolution) {
  596. APP.store.dispatch(updateKnownLargeVideoResolution(height));
  597. }
  598. const currentAspectRatio = width / height;
  599. if (this._videoAspectRatio !== currentAspectRatio) {
  600. this._videoAspectRatio = currentAspectRatio;
  601. this.resize();
  602. }
  603. }
  604. }