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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  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. let container = this.getContainer(this.state);
  97. // Include hide()/fadeOut only if we're switching between users
  98. let preUpdate;
  99. let isUserSwitch = this.newStreamData.id != this.id;
  100. if (isUserSwitch) {
  101. preUpdate = container.hide();
  102. } else {
  103. preUpdate = Promise.resolve();
  104. }
  105. preUpdate.then(() => {
  106. let {id, stream, videoType, resolve} = this.newStreamData;
  107. this.newStreamData = null;
  108. console.info("hover in %s", id);
  109. this.state = videoType;
  110. let container = this.getContainer(this.state);
  111. container.setStream(stream, videoType);
  112. // change the avatar url on large
  113. this.updateAvatar(Avatar.getAvatarUrl(id));
  114. // FIXME that does not really make sense, because the videoType
  115. // (camera or desktop) is a completely different thing than
  116. // the video container type (Etherpad, SharedVideo, VideoContainer).
  117. // ----------------------------------------------------------------
  118. // If we the continer is VIDEO_CONTAINER_TYPE, we need to check
  119. // its stream whether exist and is muted to set isVideoMuted
  120. // in rest of the cases it is false
  121. let showAvatar = false;
  122. if (videoType == VIDEO_CONTAINER_TYPE)
  123. showAvatar = stream ? stream.isMuted() : true;
  124. // If the user's connection is disrupted then the avatar will be
  125. // displayed in case we have no video image cached. That is if
  126. // there was a user switch(image is lost on stream detach) or if
  127. // the video was not rendered, before the connection has failed.
  128. let isHavingConnectivityIssues
  129. = APP.conference.isParticipantConnectionActive(id) === false;
  130. if (isHavingConnectivityIssues
  131. && (isUserSwitch | !container.wasVideoRendered)) {
  132. showAvatar = true;
  133. }
  134. let promise;
  135. // do not show stream if video is muted
  136. // but we still should show watermark
  137. if (showAvatar) {
  138. this.showWatermark(true);
  139. // If the intention of this switch is to show the avatar
  140. // we need to make sure that the video is hidden
  141. promise = container.hide();
  142. } else {
  143. promise = container.show();
  144. }
  145. // show the avatar on large if needed
  146. container.showAvatar(showAvatar);
  147. // Make sure no notification about remote failure is shown as
  148. // it's UI conflicts with the one for local connection interrupted.
  149. if (APP.conference.isConnectionInterrupted()) {
  150. this.updateParticipantConnStatusIndication(id, true);
  151. } else {
  152. this.updateParticipantConnStatusIndication(
  153. id, !isHavingConnectivityIssues);
  154. }
  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. if (show) {
  285. $('#localConnectionMessage').css({display: "block"});
  286. // Avatar message conflicts with 'videoConnectionMessage',
  287. // so it must be hidden
  288. this.showRemoteConnectionMessage(false);
  289. } else {
  290. $('#localConnectionMessage').css({display: "none"});
  291. }
  292. }
  293. /**
  294. * Shows hides the "avatar" message which is to be displayed either in
  295. * the middle of the screen or below the avatar image.
  296. *
  297. * @param {null|boolean} show (optional) <tt>true</tt> to show the avatar
  298. * message or <tt>false</tt> to hide it. If not provided then the connection
  299. * status of the user currently on the large video will be obtained form
  300. * "APP.conference" and the message will be displayed if the user's
  301. * connection is interrupted.
  302. */
  303. showRemoteConnectionMessage (show) {
  304. if (typeof show !== 'boolean') {
  305. show = APP.conference.isParticipantConnectionActive(this.id);
  306. }
  307. if (show) {
  308. $('#remoteConnectionMessage').css({display: "block"});
  309. // 'videoConnectionMessage' message conflicts with 'avatarMessage',
  310. // so it must be hidden
  311. this.showLocalConnectionMessage(false);
  312. } else {
  313. $('#remoteConnectionMessage').hide();
  314. }
  315. }
  316. /**
  317. * Updates the text which describes that the remote user is having
  318. * connectivity issues.
  319. *
  320. * @param {string} msgKey the translation key which will be used to get
  321. * the message text.
  322. * @param {object} msgOptions translation options object.
  323. *
  324. * @private
  325. */
  326. _setRemoteConnectionMessage (msgKey, msgOptions) {
  327. if (msgKey) {
  328. let text = APP.translation.translateString(msgKey, msgOptions);
  329. $('#remoteConnectionMessage')
  330. .attr("data-i18n", msgKey).text(text);
  331. }
  332. this.videoContainer.positionRemoteConnectionMessage();
  333. }
  334. /**
  335. * Updated the text which is to be shown on the top of large video, when
  336. * local media connection is interrupted.
  337. *
  338. * @param {string} msgKey the translation key which will be used to get
  339. * the message text to be displayed on the large video.
  340. * @param {object} msgOptions translation options object
  341. *
  342. * @private
  343. */
  344. _setLocalConnectionMessage (msgKey, msgOptions) {
  345. $('#localConnectionMessage')
  346. .attr("data-i18n", msgKey)
  347. .text(APP.translation.translateString(msgKey, msgOptions));
  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. }