Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

LargeVideoManager.js 22KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678
  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,
  247. showProblemsIndication,
  248. messageKey) {
  249. // Apply grey filter on the large video
  250. this.videoContainer.showRemoteConnectionProblemIndicator(
  251. showProblemsIndication);
  252. if (!messageKey) {
  253. // Hide the message
  254. this.showRemoteConnectionMessage(false);
  255. } else {
  256. // Get user's display name
  257. let displayName
  258. = APP.conference.getParticipantDisplayName(id);
  259. this._setRemoteConnectionMessage(
  260. messageKey,
  261. { displayName: displayName });
  262. // Show it now only if the VideoContainer is on top
  263. this.showRemoteConnectionMessage(
  264. LargeVideoManager.isVideoContainer(this.state));
  265. }
  266. }
  267. /**
  268. * Update large video.
  269. * Switches to large video even if previously other container was visible.
  270. * @param userID the userID of the participant associated with the stream
  271. * @param {JitsiTrack?} stream new stream
  272. * @param {string?} videoType new video type
  273. * @returns {Promise}
  274. */
  275. updateLargeVideo (userID, stream, videoType) {
  276. if (this.newStreamData) {
  277. this.newStreamData.reject();
  278. }
  279. this.newStreamData = createDeferred();
  280. this.newStreamData.id = userID;
  281. this.newStreamData.stream = stream;
  282. this.newStreamData.videoType = videoType;
  283. this.scheduleLargeVideoUpdate();
  284. return this.newStreamData.promise;
  285. }
  286. /**
  287. * Update container size.
  288. */
  289. updateContainerSize () {
  290. this.width = UIUtil.getAvailableVideoWidth();
  291. this.height = window.innerHeight;
  292. }
  293. /**
  294. * Resize Large container of specified type.
  295. * @param {string} type type of container which should be resized.
  296. * @param {boolean} [animate=false] if resize process should be animated.
  297. */
  298. resizeContainer (type, animate = false) {
  299. let container = this.getContainer(type);
  300. container.resize(this.width, this.height, animate);
  301. }
  302. /**
  303. * Resize all Large containers.
  304. * @param {boolean} animate if resize process should be animated.
  305. */
  306. resize (animate) {
  307. // resize all containers
  308. Object.keys(this.containers)
  309. .forEach(type => this.resizeContainer(type, animate));
  310. this.$container.animate({
  311. width: this.width,
  312. height: this.height
  313. }, {
  314. queue: false,
  315. duration: animate ? 500 : 0
  316. });
  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. $("#dominantSpeakerAvatar").attr('src', avatarUrl);
  332. }
  333. /**
  334. * Updates the audio level indicator of the large video.
  335. *
  336. * @param lvl the new audio level to set
  337. */
  338. updateLargeVideoAudioLevel (lvl) {
  339. AudioLevels.updateLargeVideoAudioLevel("dominantSpeaker", lvl);
  340. }
  341. /**
  342. * Displays a message of the passed in participant id's presence status. The
  343. * message will not display if the remote connection message is displayed.
  344. *
  345. * @param {string} id - The participant ID whose associated user's presence
  346. * status should be displayed.
  347. * @returns {void}
  348. */
  349. updatePresenceLabel(id) {
  350. const isConnectionMessageVisible
  351. = $('#remoteConnectionMessage').is(':visible');
  352. if (isConnectionMessageVisible) {
  353. this.removePresenceLabel();
  354. return;
  355. }
  356. const presenceLabelContainer = $('#remotePresenceMessage');
  357. if (presenceLabelContainer.length) {
  358. /* jshint ignore:start */
  359. ReactDOM.render(
  360. <Provider store = { APP.store }>
  361. <PresenceLabel participantID = { id } />
  362. </Provider>,
  363. presenceLabelContainer.get(0));
  364. /* jshint ignore:end */
  365. }
  366. }
  367. /**
  368. * Removes the messages about the displayed participant's presence status.
  369. *
  370. * @returns {void}
  371. */
  372. removePresenceLabel() {
  373. const presenceLabelContainer = $('#remotePresenceMessage');
  374. if (presenceLabelContainer.length) {
  375. /* jshint ignore:start */
  376. ReactDOM.unmountComponentAtNode(presenceLabelContainer.get(0));
  377. /* jshint ignore:end */
  378. }
  379. }
  380. /**
  381. * Show or hide watermark.
  382. * @param {boolean} show
  383. */
  384. showWatermark (show) {
  385. $('.watermark').css('visibility', show ? 'visible' : 'hidden');
  386. }
  387. /**
  388. * Shows/hides the message indicating problems with local media connection.
  389. * @param {boolean|null} show(optional) tells whether the message is to be
  390. * displayed or not. If missing the condition will be based on the value
  391. * obtained from {@link APP.conference.isConnectionInterrupted}.
  392. */
  393. showLocalConnectionMessage (show) {
  394. if (typeof show !== 'boolean') {
  395. show = APP.conference.isConnectionInterrupted();
  396. }
  397. let id = 'localConnectionMessage';
  398. UIUtil.setVisible(id, show);
  399. if (show) {
  400. // Avatar message conflicts with 'videoConnectionMessage',
  401. // so it must be hidden
  402. this.showRemoteConnectionMessage(false);
  403. }
  404. }
  405. /**
  406. * Shows hides the "avatar" message which is to be displayed either in
  407. * the middle of the screen or below the avatar image.
  408. *
  409. * @param {boolean|undefined} [show=undefined] <tt>true</tt> to show
  410. * the avatar message or <tt>false</tt> to hide it. If not provided then
  411. * the connection status of the user currently on the large video will be
  412. * obtained form "APP.conference" and the message will be displayed if
  413. * the user's connection is either interrupted or inactive.
  414. */
  415. showRemoteConnectionMessage (show) {
  416. if (typeof show !== 'boolean') {
  417. const connStatus
  418. = APP.conference.getParticipantConnectionStatus(this.id);
  419. show = !APP.conference.isLocalId(this.id)
  420. && (connStatus === ParticipantConnectionStatus.INTERRUPTED
  421. || connStatus === ParticipantConnectionStatus.INACTIVE);
  422. }
  423. if (show) {
  424. $('#remoteConnectionMessage').css({display: "block"});
  425. // 'videoConnectionMessage' message conflicts with 'avatarMessage',
  426. // so it must be hidden
  427. this.showLocalConnectionMessage(false);
  428. } else {
  429. $('#remoteConnectionMessage').hide();
  430. }
  431. }
  432. /**
  433. * Updates the text which describes that the remote user is having
  434. * connectivity issues.
  435. *
  436. * @param {string} msgKey the translation key which will be used to get
  437. * the message text.
  438. * @param {object} msgOptions translation options object.
  439. *
  440. * @private
  441. */
  442. _setRemoteConnectionMessage (msgKey, msgOptions) {
  443. if (msgKey) {
  444. $('#remoteConnectionMessage')
  445. .attr("data-i18n", msgKey)
  446. .attr("data-i18n-options", JSON.stringify(msgOptions));
  447. APP.translation.translateElement(
  448. $('#remoteConnectionMessage'), msgOptions);
  449. }
  450. }
  451. /**
  452. * Updated the text which is to be shown on the top of large video, when
  453. * local media connection is interrupted.
  454. *
  455. * @param {string} msgKey the translation key which will be used to get
  456. * the message text to be displayed on the large video.
  457. *
  458. * @private
  459. */
  460. _setLocalConnectionMessage (msgKey) {
  461. $('#localConnectionMessage')
  462. .attr("data-i18n", msgKey);
  463. APP.translation.translateElement($('#localConnectionMessage'));
  464. }
  465. /**
  466. * Add container of specified type.
  467. * @param {string} type container type
  468. * @param {LargeContainer} container container to add.
  469. */
  470. addContainer (type, container) {
  471. if (this.containers[type]) {
  472. throw new Error(`container of type ${type} already exist`);
  473. }
  474. this.containers[type] = container;
  475. this.resizeContainer(type);
  476. }
  477. /**
  478. * Get Large container of specified type.
  479. * @param {string} type container type.
  480. * @returns {LargeContainer}
  481. */
  482. getContainer (type) {
  483. let container = this.containers[type];
  484. if (!container) {
  485. throw new Error(`container of type ${type} doesn't exist`);
  486. }
  487. return container;
  488. }
  489. /**
  490. * Returns {@link LargeContainer} for the current {@link state}
  491. *
  492. * @return {LargeContainer}
  493. *
  494. * @throws an <tt>Error</tt> if there is no container for the current
  495. * {@link state}.
  496. */
  497. getCurrentContainer() {
  498. return this.getContainer(this.state);
  499. }
  500. /**
  501. * Returns type of the current {@link LargeContainer}
  502. * @return {string}
  503. */
  504. getCurrentContainerType() {
  505. return this.state;
  506. }
  507. /**
  508. * Remove Large container of specified type.
  509. * @param {string} type container type.
  510. */
  511. removeContainer (type) {
  512. if (!this.containers[type]) {
  513. throw new Error(`container of type ${type} doesn't exist`);
  514. }
  515. delete this.containers[type];
  516. }
  517. /**
  518. * Show Large container of specified type.
  519. * Does nothing if such container is already visible.
  520. * @param {string} type container type.
  521. * @returns {Promise}
  522. */
  523. showContainer (type) {
  524. if (this.state === type) {
  525. return Promise.resolve();
  526. }
  527. let oldContainer = this.containers[this.state];
  528. // FIXME when video is being replaced with other content we need to hide
  529. // companion icons/messages. It would be best if the container would
  530. // be taking care of it by itself, but that is a bigger refactoring
  531. if (LargeVideoManager.isVideoContainer(this.state)) {
  532. this.showWatermark(false);
  533. this.showLocalConnectionMessage(false);
  534. this.showRemoteConnectionMessage(false);
  535. }
  536. oldContainer.hide();
  537. this.state = type;
  538. let container = this.getContainer(type);
  539. return container.show().then(() => {
  540. if (LargeVideoManager.isVideoContainer(type)) {
  541. // FIXME when video appears on top of other content we need to
  542. // show companion icons/messages. It would be best if
  543. // the container would be taking care of it by itself, but that
  544. // is a bigger refactoring
  545. this.showWatermark(true);
  546. // "avatar" and "video connection" can not be displayed both
  547. // at the same time, but the latter is of higher priority and it
  548. // will hide the avatar one if will be displayed.
  549. this.showRemoteConnectionMessage(/* fetch the current state */);
  550. this.showLocalConnectionMessage(/* fetch the current state */);
  551. }
  552. });
  553. }
  554. /**
  555. * Changes the flipX state of the local video.
  556. * @param val {boolean} true if flipped.
  557. */
  558. onLocalFlipXChange(val) {
  559. this.videoContainer.setLocalFlipX(val);
  560. }
  561. /**
  562. * Dispatches an action to update the known resolution state of the
  563. * large video and adjusts container sizes when the resolution changes.
  564. *
  565. * @private
  566. * @returns {void}
  567. */
  568. _onVideoResolutionUpdate() {
  569. const { height, width } = this.videoContainer.getStreamSize();
  570. const { resolution } = APP.store.getState()['features/large-video'];
  571. if (height !== resolution) {
  572. APP.store.dispatch(updateKnownLargeVideoResolution(height));
  573. }
  574. const currentAspectRatio = width / height;
  575. if (this._videoAspectRatio !== currentAspectRatio) {
  576. this._videoAspectRatio = currentAspectRatio;
  577. this.resize();
  578. }
  579. }
  580. }