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 22KB

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