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

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