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

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