您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

LargeVideoManager.js 22KB

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