Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

LargeVideoManager.js 19KB

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