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

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