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

LargeVideoManager.js 22KB

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