You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

LargeVideoManager.js 22KB

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