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

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