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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712
  1. /* global $, APP, JitsiMeetJS */
  2. /* eslint-disable no-unused-vars */
  3. import React from 'react';
  4. import ReactDOM from 'react-dom';
  5. import { Provider } from 'react-redux';
  6. import { PresenceLabel } from '../../../react/features/presence-status';
  7. /* eslint-enable no-unused-vars */
  8. const logger = require('jitsi-meet-logger').getLogger(__filename);
  9. import {
  10. JitsiParticipantConnectionStatus
  11. } from '../../../react/features/base/lib-jitsi-meet';
  12. import {
  13. getAvatarURLByParticipantId
  14. } from '../../../react/features/base/participants';
  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. * The time interval in milliseconds to check the video resolution of the video
  26. * being displayed.
  27. *
  28. * @type {number}
  29. */
  30. const VIDEO_RESOLUTION_POLL_INTERVAL = 2000;
  31. /**
  32. * Manager for all Large containers.
  33. */
  34. export default class LargeVideoManager {
  35. /**
  36. * Checks whether given container is a {@link VIDEO_CONTAINER_TYPE}.
  37. * FIXME currently this is a workaround for the problem where video type is
  38. * mixed up with container type.
  39. * @param {string} containerType
  40. * @return {boolean}
  41. */
  42. static isVideoContainer(containerType) {
  43. return containerType === VIDEO_CONTAINER_TYPE
  44. || containerType === DESKTOP_CONTAINER_TYPE;
  45. }
  46. /**
  47. *
  48. */
  49. constructor(emitter) {
  50. /**
  51. * The map of <tt>LargeContainer</tt>s where the key is the video
  52. * container type.
  53. * @type {Object.<string, LargeContainer>}
  54. */
  55. this.containers = {};
  56. this.eventEmitter = emitter;
  57. this.state = VIDEO_CONTAINER_TYPE;
  58. // FIXME: We are passing resizeContainer as parameter which is calling
  59. // Container.resize. Probably there's better way to implement this.
  60. this.videoContainer = new VideoContainer(
  61. () => this.resizeContainer(VIDEO_CONTAINER_TYPE), emitter);
  62. this.addContainer(VIDEO_CONTAINER_TYPE, this.videoContainer);
  63. // use the same video container to handle desktop tracks
  64. this.addContainer(DESKTOP_CONTAINER_TYPE, this.videoContainer);
  65. this.width = 0;
  66. this.height = 0;
  67. /**
  68. * Cache the aspect ratio of the video displayed to detect changes to
  69. * the aspect ratio on video resize events.
  70. *
  71. * @type {number}
  72. */
  73. this._videoAspectRatio = 0;
  74. this.$container = $('#largeVideoContainer');
  75. this.$container.css({
  76. display: 'inline-block'
  77. });
  78. this.$container.hover(
  79. e => this.onHoverIn(e),
  80. e => this.onHoverOut(e)
  81. );
  82. // Bind event handler so it is only bound once for every instance.
  83. this._onVideoResolutionUpdate
  84. = this._onVideoResolutionUpdate.bind(this);
  85. this.videoContainer.addResizeListener(this._onVideoResolutionUpdate);
  86. if (!JitsiMeetJS.util.RTCUIHelper.isResizeEventSupported()) {
  87. /**
  88. * An interval for polling if the displayed video resolution is or
  89. * is not high-definition. For browsers that do not support video
  90. * resize events, polling is the fallback.
  91. *
  92. * @private
  93. * @type {timeoutId}
  94. */
  95. this._updateVideoResolutionInterval = window.setInterval(
  96. this._onVideoResolutionUpdate,
  97. VIDEO_RESOLUTION_POLL_INTERVAL);
  98. }
  99. }
  100. /**
  101. * Stops any polling intervals on the instance and removes any
  102. * listeners registered on child components, including React Components.
  103. *
  104. * @returns {void}
  105. */
  106. destroy() {
  107. window.clearInterval(this._updateVideoResolutionInterval);
  108. this.videoContainer.removeResizeListener(
  109. this._onVideoResolutionUpdate);
  110. this.removePresenceLabel();
  111. }
  112. /**
  113. *
  114. */
  115. onHoverIn(e) {
  116. if (!this.state) {
  117. return;
  118. }
  119. const container = this.getCurrentContainer();
  120. container.onHoverIn(e);
  121. }
  122. /**
  123. *
  124. */
  125. onHoverOut(e) {
  126. if (!this.state) {
  127. return;
  128. }
  129. const container = this.getCurrentContainer();
  130. container.onHoverOut(e);
  131. }
  132. /**
  133. * Called when the media connection has been interrupted.
  134. */
  135. onVideoInterrupted() {
  136. this.enableLocalConnectionProblemFilter(true);
  137. this._setLocalConnectionMessage('connection.RECONNECTING');
  138. // Show the message only if the video is currently being displayed
  139. this.showLocalConnectionMessage(
  140. LargeVideoManager.isVideoContainer(this.state));
  141. }
  142. /**
  143. * Called when the media connection has been restored.
  144. */
  145. onVideoRestored() {
  146. this.enableLocalConnectionProblemFilter(false);
  147. this.showLocalConnectionMessage(false);
  148. }
  149. /**
  150. *
  151. */
  152. get id() {
  153. const container = this.getCurrentContainer();
  154. return container.id;
  155. }
  156. /**
  157. *
  158. */
  159. scheduleLargeVideoUpdate() {
  160. if (this.updateInProcess || !this.newStreamData) {
  161. return;
  162. }
  163. this.updateInProcess = true;
  164. // Include hide()/fadeOut only if we're switching between users
  165. // eslint-disable-next-line eqeqeq
  166. const isUserSwitch = this.newStreamData.id != this.id;
  167. const container = this.getCurrentContainer();
  168. const preUpdate = isUserSwitch ? container.hide() : Promise.resolve();
  169. preUpdate.then(() => {
  170. const { id, stream, videoType, resolve } = this.newStreamData;
  171. // FIXME this does not really make sense, because the videoType
  172. // (camera or desktop) is a completely different thing than
  173. // the video container type (Etherpad, SharedVideo, VideoContainer).
  174. const isVideoContainer
  175. = LargeVideoManager.isVideoContainer(videoType);
  176. this.newStreamData = null;
  177. logger.info('hover in %s', id);
  178. this.state = videoType;
  179. // eslint-disable-next-line no-shadow
  180. const container = this.getCurrentContainer();
  181. container.setStream(id, stream, videoType);
  182. // change the avatar url on large
  183. this.updateAvatar(
  184. getAvatarURLByParticipantId(APP.store.getState(), id));
  185. // If the user's connection is disrupted then the avatar will be
  186. // displayed in case we have no video image cached. That is if
  187. // there was a user switch (image is lost on stream detach) or if
  188. // the video was not rendered, before the connection has failed.
  189. const wasUsersImageCached
  190. = !isUserSwitch && container.wasVideoRendered;
  191. const isVideoMuted = !stream || stream.isMuted();
  192. const connectionStatus
  193. = APP.conference.getParticipantConnectionStatus(id);
  194. const isVideoRenderable
  195. = !isVideoMuted
  196. && (APP.conference.isLocalId(id)
  197. || connectionStatus
  198. === JitsiParticipantConnectionStatus.ACTIVE
  199. || wasUsersImageCached);
  200. const showAvatar
  201. = isVideoContainer
  202. && (APP.conference.isAudioOnly() || !isVideoRenderable);
  203. let promise;
  204. // do not show stream if video is muted
  205. // but we still should show watermark
  206. if (showAvatar) {
  207. this.showWatermark(true);
  208. // If the intention of this switch is to show the avatar
  209. // we need to make sure that the video is hidden
  210. promise = container.hide();
  211. } else {
  212. promise = container.show();
  213. }
  214. // show the avatar on large if needed
  215. container.showAvatar(showAvatar);
  216. // Clean up audio level after previous speaker.
  217. if (showAvatar) {
  218. this.updateLargeVideoAudioLevel(0);
  219. }
  220. const isConnectionInterrupted
  221. = APP.conference.getParticipantConnectionStatus(id)
  222. === JitsiParticipantConnectionStatus.INTERRUPTED;
  223. let messageKey = null;
  224. if (isConnectionInterrupted) {
  225. messageKey = 'connection.USER_CONNECTION_INTERRUPTED';
  226. } else if (connectionStatus
  227. === JitsiParticipantConnectionStatus.INACTIVE) {
  228. messageKey = 'connection.LOW_BANDWIDTH';
  229. }
  230. // Make sure no notification about remote failure is shown as
  231. // its UI conflicts with the one for local connection interrupted.
  232. // For the purposes of UI indicators, audio only is considered as
  233. // an "active" connection.
  234. const overrideAndHide
  235. = APP.conference.isAudioOnly()
  236. || APP.conference.isConnectionInterrupted();
  237. this.updateParticipantConnStatusIndication(
  238. id,
  239. !overrideAndHide && isConnectionInterrupted,
  240. !overrideAndHide && messageKey);
  241. // Change the participant id the presence label is listening to.
  242. this.updatePresenceLabel(id);
  243. this.videoContainer.positionRemoteStatusMessages();
  244. // resolve updateLargeVideo promise after everything is done
  245. promise.then(resolve);
  246. return promise;
  247. }).then(() => {
  248. // after everything is done check again if there are any pending
  249. // new streams.
  250. this.updateInProcess = false;
  251. this.eventEmitter.emit(UIEvents.LARGE_VIDEO_ID_CHANGED, this.id);
  252. this.scheduleLargeVideoUpdate();
  253. });
  254. }
  255. /**
  256. * Shows/hides notification about participant's connectivity issues to be
  257. * shown on the large video area.
  258. *
  259. * @param {string} id the id of remote participant(MUC nickname)
  260. * @param {boolean} showProblemsIndication
  261. * @param {string|null} messageKey the i18n key of the message which will be
  262. * displayed on the large video or <tt>null</tt> to hide it.
  263. *
  264. * @private
  265. */
  266. updateParticipantConnStatusIndication(
  267. id,
  268. showProblemsIndication,
  269. messageKey) {
  270. // Apply grey filter on the large video
  271. this.videoContainer.showRemoteConnectionProblemIndicator(
  272. showProblemsIndication);
  273. if (messageKey) {
  274. // Get user's display name
  275. const displayName
  276. = APP.conference.getParticipantDisplayName(id);
  277. this._setRemoteConnectionMessage(
  278. messageKey,
  279. { displayName });
  280. // Show it now only if the VideoContainer is on top
  281. this.showRemoteConnectionMessage(
  282. LargeVideoManager.isVideoContainer(this.state));
  283. } else {
  284. // Hide the message
  285. this.showRemoteConnectionMessage(false);
  286. }
  287. }
  288. /**
  289. * Update large video.
  290. * Switches to large video even if previously other container was visible.
  291. * @param userID the userID of the participant associated with the stream
  292. * @param {JitsiTrack?} stream new stream
  293. * @param {string?} videoType new video type
  294. * @returns {Promise}
  295. */
  296. updateLargeVideo(userID, stream, videoType) {
  297. if (this.newStreamData) {
  298. this.newStreamData.reject();
  299. }
  300. this.newStreamData = createDeferred();
  301. this.newStreamData.id = userID;
  302. this.newStreamData.stream = stream;
  303. this.newStreamData.videoType = videoType;
  304. this.scheduleLargeVideoUpdate();
  305. return this.newStreamData.promise;
  306. }
  307. /**
  308. * Update container size.
  309. */
  310. updateContainerSize() {
  311. this.width = UIUtil.getAvailableVideoWidth();
  312. this.height = window.innerHeight;
  313. }
  314. /**
  315. * Resize Large container of specified type.
  316. * @param {string} type type of container which should be resized.
  317. * @param {boolean} [animate=false] if resize process should be animated.
  318. */
  319. resizeContainer(type, animate = false) {
  320. const container = this.getContainer(type);
  321. container.resize(this.width, this.height, animate);
  322. }
  323. /**
  324. * Resize all Large containers.
  325. * @param {boolean} animate if resize process should be animated.
  326. */
  327. resize(animate) {
  328. // resize all containers
  329. Object.keys(this.containers)
  330. .forEach(type => this.resizeContainer(type, animate));
  331. this.$container.animate({
  332. width: this.width,
  333. height: this.height
  334. }, {
  335. queue: false,
  336. duration: animate ? 500 : 0
  337. });
  338. }
  339. /**
  340. * Enables/disables the filter indicating a video problem to the user caused
  341. * by the problems with local media connection.
  342. *
  343. * @param enable <tt>true</tt> to enable, <tt>false</tt> to disable
  344. */
  345. enableLocalConnectionProblemFilter(enable) {
  346. this.videoContainer.enableLocalConnectionProblemFilter(enable);
  347. }
  348. /**
  349. * Updates the src of the dominant speaker avatar
  350. */
  351. updateAvatar(avatarUrl) {
  352. $('#dominantSpeakerAvatar').attr('src', avatarUrl);
  353. }
  354. /**
  355. * Updates the audio level indicator of the large video.
  356. *
  357. * @param lvl the new audio level to set
  358. */
  359. updateLargeVideoAudioLevel(lvl) {
  360. AudioLevels.updateLargeVideoAudioLevel('dominantSpeaker', lvl);
  361. }
  362. /**
  363. * Displays a message of the passed in participant id's presence status. The
  364. * message will not display if the remote connection message is displayed.
  365. *
  366. * @param {string} id - The participant ID whose associated user's presence
  367. * status should be displayed.
  368. * @returns {void}
  369. */
  370. updatePresenceLabel(id) {
  371. const isConnectionMessageVisible
  372. = $('#remoteConnectionMessage').is(':visible');
  373. if (isConnectionMessageVisible) {
  374. this.removePresenceLabel();
  375. return;
  376. }
  377. const presenceLabelContainer = $('#remotePresenceMessage');
  378. if (presenceLabelContainer.length) {
  379. ReactDOM.render(
  380. <Provider store = { APP.store }>
  381. <PresenceLabel participantID = { id } />
  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. }