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

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