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

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