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

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