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

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