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

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