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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  1. /* global $, APP, config, JitsiMeetJS, interfaceConfig */
  2. import {
  3. setFilmstripRemoteVideosVisibility,
  4. setFilmstripVisibility
  5. } from '../../../react/features/filmstrip';
  6. import UIEvents from "../../../service/UI/UIEvents";
  7. import UIUtil from "../util/UIUtil";
  8. const Filmstrip = {
  9. /**
  10. *
  11. * @param eventEmitter the {EventEmitter} through which {Filmstrip} is to
  12. * emit/fire {UIEvents} (such as {UIEvents.TOGGLED_FILMSTRIP}).
  13. */
  14. init (eventEmitter) {
  15. this.iconMenuDownClassName = 'icon-menu-down';
  16. this.iconMenuUpClassName = 'icon-menu-up';
  17. this.filmstripContainerClassName = 'filmstrip';
  18. this.filmstrip = $('#remoteVideos');
  19. this.filmstripRemoteVideos = $('#filmstripRemoteVideosContainer');
  20. this.eventEmitter = eventEmitter;
  21. // Show the toggle button and add event listeners only when out of
  22. // filmstrip only mode.
  23. if (!interfaceConfig.filmStripOnly) {
  24. this._initFilmstripToolbar();
  25. this.registerListeners();
  26. }
  27. },
  28. /**
  29. * Sets a class on the remote videos container for CSS to adjust visibility
  30. * of the remote videos. Will no-op if config.debug is truthy, as should be
  31. * the case with torture tests.
  32. *
  33. * @param {boolean} shouldHide - True if remote videos should be hidden,
  34. * false if they should be visible.
  35. * @returns {void}
  36. */
  37. setRemoteVideoVisibility(shouldShow) {
  38. // Allow disabling on 1-on-1 UI mode. Used by torture tests.
  39. if (config.disable1On1Mode) {
  40. return;
  41. }
  42. APP.store.dispatch(setFilmstripRemoteVideosVisibility(shouldShow));
  43. this.filmstripRemoteVideos.toggleClass('hide-videos', !shouldShow);
  44. },
  45. /**
  46. * Initializes the filmstrip toolbar.
  47. */
  48. _initFilmstripToolbar() {
  49. let toolbarContainerHTML = this._generateToolbarHTML();
  50. let className = this.filmstripContainerClassName;
  51. let container = document.querySelector(`.${className}`);
  52. UIUtil.prependChild(container, toolbarContainerHTML);
  53. let iconSelector = '#toggleFilmstripButton i';
  54. this.toggleFilmstripIcon = document.querySelector(iconSelector);
  55. },
  56. /**
  57. * Generates HTML layout for filmstrip toggle button and wrapping container.
  58. * @returns {HTMLElement}
  59. * @private
  60. */
  61. _generateToolbarHTML() {
  62. let container = document.createElement('div');
  63. let isVisible = this.isFilmstripVisible();
  64. container.className = 'filmstrip__toolbar';
  65. container.innerHTML = `
  66. <button id="toggleFilmstripButton">
  67. <i class="icon-menu-${isVisible ? 'down' : 'up'}">
  68. </i>
  69. </button>
  70. `;
  71. return container;
  72. },
  73. /**
  74. * Attach 'click' listener to "hide filmstrip" button
  75. */
  76. registerListeners() {
  77. // Important:
  78. // Firing the event instead of executing toggleFilmstrip method because
  79. // it's important to hide the filmstrip by UI.toggleFilmstrip in order
  80. // to correctly resize the video area.
  81. $('#toggleFilmstripButton').on('click',
  82. () => this.eventEmitter.emit(UIEvents.TOGGLE_FILMSTRIP));
  83. this._registerToggleFilmstripShortcut();
  84. },
  85. /**
  86. * Registering toggle filmstrip shortcut
  87. * @private
  88. */
  89. _registerToggleFilmstripShortcut() {
  90. let shortcut = 'F';
  91. let shortcutAttr = 'filmstripPopover';
  92. let description = 'keyboardShortcuts.toggleFilmstrip';
  93. // Important:
  94. // Firing the event instead of executing toggleFilmstrip method because
  95. // it's important to hide the filmstrip by UI.toggleFilmstrip in order
  96. // to correctly resize the video area.
  97. let handler = () => this.eventEmitter.emit(UIEvents.TOGGLE_FILMSTRIP);
  98. APP.keyboardshortcut.registerShortcut(
  99. shortcut,
  100. shortcutAttr,
  101. handler,
  102. description
  103. );
  104. },
  105. /**
  106. * Changes classes of icon for showing down state
  107. */
  108. showMenuDownIcon() {
  109. let icon = this.toggleFilmstripIcon;
  110. if(icon) {
  111. icon.classList.add(this.iconMenuDownClassName);
  112. icon.classList.remove(this.iconMenuUpClassName);
  113. }
  114. },
  115. /**
  116. * Changes classes of icon for showing up state
  117. */
  118. showMenuUpIcon() {
  119. let icon = this.toggleFilmstripIcon;
  120. if(icon) {
  121. icon.classList.add(this.iconMenuUpClassName);
  122. icon.classList.remove(this.iconMenuDownClassName);
  123. }
  124. },
  125. /**
  126. * Toggles the visibility of the filmstrip.
  127. *
  128. * @param visible optional {Boolean} which specifies the desired visibility
  129. * of the filmstrip. If not specified, the visibility will be flipped
  130. * (i.e. toggled); otherwise, the visibility will be set to the specified
  131. * value.
  132. * @param {Boolean} sendAnalytics - True to send an analytics event. The
  133. * default value is true.
  134. *
  135. * Note:
  136. * This method shouldn't be executed directly to hide the filmstrip.
  137. * It's important to hide the filmstrip with UI.toggleFilmstrip in order
  138. * to correctly resize the video area.
  139. */
  140. toggleFilmstrip(visible, sendAnalytics = true) {
  141. const isVisibleDefined = typeof visible === 'boolean';
  142. if (!isVisibleDefined) {
  143. visible = this.isFilmstripVisible();
  144. } else if (this.isFilmstripVisible() === visible) {
  145. return;
  146. }
  147. if (sendAnalytics) {
  148. JitsiMeetJS.analytics.sendEvent('toolbar.filmstrip.toggled');
  149. }
  150. this.filmstrip.toggleClass("hidden");
  151. if (visible) {
  152. this.showMenuUpIcon();
  153. } else {
  154. this.showMenuDownIcon();
  155. }
  156. // Emit/fire UIEvents.TOGGLED_FILMSTRIP.
  157. const eventEmitter = this.eventEmitter;
  158. const isFilmstripVisible = this.isFilmstripVisible();
  159. if (eventEmitter) {
  160. eventEmitter.emit(
  161. UIEvents.TOGGLED_FILMSTRIP,
  162. this.isFilmstripVisible());
  163. }
  164. APP.store.dispatch(setFilmstripVisibility(isFilmstripVisible));
  165. },
  166. /**
  167. * Shows if filmstrip is visible
  168. * @returns {boolean}
  169. */
  170. isFilmstripVisible() {
  171. return !this.filmstrip.hasClass('hidden');
  172. },
  173. /**
  174. * Adjusts styles for filmstrip-only mode.
  175. */
  176. setFilmstripOnly() {
  177. this.filmstrip.addClass('filmstrip__videos-filmstripOnly');
  178. },
  179. /**
  180. * Returns the height of filmstrip
  181. * @returns {number} height
  182. */
  183. getFilmstripHeight() {
  184. // FIXME Make it more clear the getFilmstripHeight check is used in
  185. // horizontal film strip mode for calculating how tall large video
  186. // display should be.
  187. if (this.isFilmstripVisible() && !interfaceConfig.VERTICAL_FILMSTRIP) {
  188. return $(`.${this.filmstripContainerClassName}`).outerHeight();
  189. } else {
  190. return 0;
  191. }
  192. },
  193. /**
  194. * Returns the width of filmstip
  195. * @returns {number} width
  196. */
  197. getFilmstripWidth() {
  198. return this.filmstrip.innerWidth()
  199. - parseInt(this.filmstrip.css('paddingLeft'), 10)
  200. - parseInt(this.filmstrip.css('paddingRight'), 10);
  201. },
  202. /**
  203. * Calculates the size for thumbnails: local and remote one
  204. * @returns {*|{localVideo, remoteVideo}}
  205. */
  206. calculateThumbnailSize() {
  207. let availableSizes = this.calculateAvailableSize();
  208. let width = availableSizes.availableWidth;
  209. let height = availableSizes.availableHeight;
  210. return this.calculateThumbnailSizeFromAvailable(width, height);
  211. },
  212. /**
  213. * Calculates available size for one thumbnail according to
  214. * the current window size.
  215. *
  216. * @returns {{availableWidth: number, availableHeight: number}}
  217. */
  218. calculateAvailableSize() {
  219. let availableHeight = interfaceConfig.FILM_STRIP_MAX_HEIGHT;
  220. let thumbs = this.getThumbs(true);
  221. let numvids = thumbs.remoteThumbs.length;
  222. let localVideoContainer = $("#localVideoContainer");
  223. /**
  224. * If the videoAreaAvailableWidth is set we use this one to calculate
  225. * the filmstrip width, because we're probably in a state where the
  226. * filmstrip size hasn't been updated yet, but it will be.
  227. */
  228. let videoAreaAvailableWidth
  229. = UIUtil.getAvailableVideoWidth()
  230. - this._getFilmstripExtraPanelsWidth()
  231. - UIUtil.parseCssInt(this.filmstrip.css('right'), 10)
  232. - UIUtil.parseCssInt(this.filmstrip.css('paddingLeft'), 10)
  233. - UIUtil.parseCssInt(this.filmstrip.css('paddingRight'), 10)
  234. - UIUtil.parseCssInt(this.filmstrip.css('borderLeftWidth'), 10)
  235. - UIUtil.parseCssInt(this.filmstrip.css('borderRightWidth'), 10)
  236. - 5;
  237. let availableWidth = videoAreaAvailableWidth;
  238. // If local thumb is not hidden
  239. if(thumbs.localThumb) {
  240. availableWidth = Math.floor(
  241. (videoAreaAvailableWidth - (
  242. UIUtil.parseCssInt(
  243. localVideoContainer.css('borderLeftWidth'), 10)
  244. + UIUtil.parseCssInt(
  245. localVideoContainer.css('borderRightWidth'), 10)
  246. + UIUtil.parseCssInt(
  247. localVideoContainer.css('paddingLeft'), 10)
  248. + UIUtil.parseCssInt(
  249. localVideoContainer.css('paddingRight'), 10)
  250. + UIUtil.parseCssInt(
  251. localVideoContainer.css('marginLeft'), 10)
  252. + UIUtil.parseCssInt(
  253. localVideoContainer.css('marginRight'), 10)))
  254. );
  255. }
  256. // If the number of videos is 0 or undefined or we're in vertical
  257. // filmstrip mode we don't need to calculate further any adjustments
  258. // to width based on the number of videos present.
  259. if (numvids && !interfaceConfig.VERTICAL_FILMSTRIP) {
  260. let remoteVideoContainer = thumbs.remoteThumbs.eq(0);
  261. availableWidth = Math.floor(
  262. (videoAreaAvailableWidth - numvids * (
  263. UIUtil.parseCssInt(
  264. remoteVideoContainer.css('borderLeftWidth'), 10)
  265. + UIUtil.parseCssInt(
  266. remoteVideoContainer.css('borderRightWidth'), 10)
  267. + UIUtil.parseCssInt(
  268. remoteVideoContainer.css('paddingLeft'), 10)
  269. + UIUtil.parseCssInt(
  270. remoteVideoContainer.css('paddingRight'), 10)
  271. + UIUtil.parseCssInt(
  272. remoteVideoContainer.css('marginLeft'), 10)
  273. + UIUtil.parseCssInt(
  274. remoteVideoContainer.css('marginRight'), 10)))
  275. );
  276. }
  277. let maxHeight
  278. // If the MAX_HEIGHT property hasn't been specified
  279. // we have the static value.
  280. = Math.min(interfaceConfig.FILM_STRIP_MAX_HEIGHT || 120,
  281. availableHeight);
  282. availableHeight
  283. = Math.min(maxHeight, window.innerHeight - 18);
  284. return { availableWidth, availableHeight };
  285. },
  286. /**
  287. * Traverse all elements inside the filmstrip
  288. * and calculates the sum of all of them except
  289. * remote videos element. Used for calculation of
  290. * available width for video thumbnails.
  291. *
  292. * @returns {number} calculated width
  293. * @private
  294. */
  295. _getFilmstripExtraPanelsWidth() {
  296. let className = this.filmstripContainerClassName;
  297. let width = 0;
  298. $(`.${className}`)
  299. .children()
  300. .each(function () {
  301. if (this.id !== 'remoteVideos') {
  302. width += $(this).outerWidth();
  303. }
  304. });
  305. return width;
  306. },
  307. /**
  308. Calculate the thumbnail size in order to fit all the thumnails in passed
  309. * dimensions.
  310. * NOTE: Here we assume that the remote and local thumbnails are with the
  311. * same height.
  312. * @param {int} availableWidth the maximum width for all thumbnails
  313. * @param {int} availableHeight the maximum height for all thumbnails
  314. * @returns {{localVideo, remoteVideo}}
  315. */
  316. calculateThumbnailSizeFromAvailable(availableWidth, availableHeight) {
  317. /**
  318. * Let:
  319. * lW - width of the local thumbnail
  320. * rW - width of the remote thumbnail
  321. * h - the height of the thumbnails
  322. * remoteRatio - width:height for the remote thumbnail
  323. * localRatio - width:height for the local thumbnail
  324. * remoteThumbsInRow - number of remote thumbnails in a row (we have
  325. * only one local thumbnail) next to the local thumbnail. In vertical
  326. * filmstrip mode, this will always be 0.
  327. *
  328. * Since the height for local thumbnail = height for remote thumbnail
  329. * and we know the ratio (width:height) for the local and for the
  330. * remote thumbnail we can find rW/lW:
  331. * rW / remoteRatio = lW / localRatio then -
  332. * remoteLocalWidthRatio = rW / lW = remoteRatio / localRatio
  333. * and rW = lW * remoteRatio / localRatio = lW * remoteLocalWidthRatio
  334. * And the total width for the thumbnails is:
  335. * totalWidth = rW * remoteThumbsInRow + lW
  336. * = lW * remoteLocalWidthRatio * remoteThumbsInRow + lW =
  337. * lW * (remoteLocalWidthRatio * remoteThumbsInRow + 1)
  338. * and the h = lW/localRatio
  339. *
  340. * In order to fit all the thumbails in the area defined by
  341. * availableWidth * availableHeight we should check one of the
  342. * following options:
  343. * 1) if availableHeight == h - totalWidth should be less than
  344. * availableWidth
  345. * 2) if availableWidth == totalWidth - h should be less than
  346. * availableHeight
  347. *
  348. * 1) or 2) will be true and we are going to use it to calculate all
  349. * sizes.
  350. *
  351. * if 1) is true that means that
  352. * availableHeight/h > availableWidth/totalWidth otherwise 2) is true
  353. */
  354. const remoteThumbsInRow = interfaceConfig.VERTICAL_FILMSTRIP
  355. ? 0 : this.getThumbs(true).remoteThumbs.length;
  356. const remoteLocalWidthRatio = interfaceConfig.REMOTE_THUMBNAIL_RATIO /
  357. interfaceConfig.LOCAL_THUMBNAIL_RATIO;
  358. const lW = Math.min(availableWidth /
  359. (remoteLocalWidthRatio * remoteThumbsInRow + 1), availableHeight *
  360. interfaceConfig.LOCAL_THUMBNAIL_RATIO);
  361. const h = lW / interfaceConfig.LOCAL_THUMBNAIL_RATIO;
  362. const remoteVideoWidth = lW * remoteLocalWidthRatio;
  363. let localVideo;
  364. if (interfaceConfig.VERTICAL_FILMSTRIP) {
  365. localVideo = {
  366. thumbWidth: remoteVideoWidth,
  367. thumbHeight: h * remoteLocalWidthRatio
  368. };
  369. } else {
  370. localVideo = {
  371. thumbWidth: lW,
  372. thumbHeight: h
  373. };
  374. }
  375. return {
  376. localVideo,
  377. remoteVideo: {
  378. thumbWidth: remoteVideoWidth,
  379. thumbHeight: h
  380. }
  381. };
  382. },
  383. /**
  384. * Resizes thumbnails
  385. * @param local
  386. * @param remote
  387. * @param animate
  388. * @param forceUpdate
  389. * @returns {Promise}
  390. */
  391. resizeThumbnails(local, remote, animate = false, forceUpdate = false) {
  392. return new Promise(resolve => {
  393. let thumbs = this.getThumbs(!forceUpdate);
  394. let promises = [];
  395. if(thumbs.localThumb) {
  396. promises.push(new Promise((resolve) => {
  397. thumbs.localThumb.animate({
  398. height: local.thumbHeight,
  399. width: local.thumbWidth
  400. }, this._getAnimateOptions(animate, resolve));
  401. }));
  402. }
  403. if(thumbs.remoteThumbs) {
  404. promises.push(new Promise((resolve) => {
  405. thumbs.remoteThumbs.animate({
  406. height: remote.thumbHeight,
  407. width: remote.thumbWidth
  408. }, this._getAnimateOptions(animate, resolve));
  409. }));
  410. }
  411. promises.push(new Promise((resolve) => {
  412. // Let CSS take care of height in vertical filmstrip mode.
  413. if (interfaceConfig.VERTICAL_FILMSTRIP) {
  414. resolve();
  415. } else {
  416. this.filmstrip.animate({
  417. // adds 2 px because of small video 1px border
  418. height: remote.thumbHeight + 2
  419. }, this._getAnimateOptions(animate, resolve));
  420. }
  421. }));
  422. promises.push(new Promise(() => {
  423. let { localThumb } = this.getThumbs();
  424. let height = localThumb.height();
  425. let fontSize = UIUtil.getIndicatorFontSize(height);
  426. this.filmstrip.find('.indicator').animate({
  427. fontSize
  428. }, this._getAnimateOptions(animate, resolve));
  429. }));
  430. if (!animate) {
  431. resolve();
  432. }
  433. Promise.all(promises).then(resolve);
  434. });
  435. },
  436. /**
  437. * Helper method. Returns options for jQuery animation
  438. * @param animate {Boolean} - animation flag
  439. * @param cb {Function} - complete callback
  440. * @returns {Object} - animation options object
  441. * @private
  442. */
  443. _getAnimateOptions(animate, cb = $.noop) {
  444. return {
  445. queue: false,
  446. duration: animate ? 500 : 0,
  447. complete: cb
  448. };
  449. },
  450. /**
  451. * Returns thumbnails of the filmstrip
  452. * @param only_visible
  453. * @returns {object} thumbnails
  454. */
  455. getThumbs(only_visible = false) {
  456. let selector = 'span';
  457. if (only_visible) {
  458. selector += ':visible';
  459. }
  460. let localThumb = $("#localVideoContainer");
  461. let remoteThumbs = this.filmstripRemoteVideos.children(selector);
  462. // Exclude the local video container if it has been hidden.
  463. if (localThumb.hasClass("hidden")) {
  464. return { remoteThumbs };
  465. } else {
  466. return { remoteThumbs, localThumb };
  467. }
  468. }
  469. };
  470. export default Filmstrip;