Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

LargeVideoManager.js 23KB

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