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.

Filmstrip.js 18KB

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