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

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