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 20KB

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