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

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