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.

functions.js 2.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. // @flow
  2. import { VIDEO_QUALITY_LEVELS } from '../base/conference';
  3. import { CFG_LVL_TO_APP_QUALITY_LVL } from './constants';
  4. /**
  5. * Selects {@code VIDEO_QUALITY_LEVELS} for the given {@link availableHeight} and threshold to quality mapping.
  6. *
  7. * @param {number} availableHeight - The height to which a matching video quality level should be found.
  8. * @param {Map<number, number>} heightToLevel - The threshold to quality level mapping. The keys are sorted in the
  9. * ascending order.
  10. * @returns {number} The matching value from {@code VIDEO_QUALITY_LEVELS}.
  11. */
  12. export function getReceiverVideoQualityLevel(availableHeight: number, heightToLevel: Map<number, number>): number {
  13. let selectedLevel = VIDEO_QUALITY_LEVELS.LOW;
  14. for (const [ levelThreshold, level ] of heightToLevel.entries()) {
  15. if (availableHeight >= levelThreshold) {
  16. selectedLevel = level;
  17. }
  18. }
  19. return selectedLevel;
  20. }
  21. /**
  22. * Converts {@code Object} passed in the config which represents height thresholds to vide quality level mapping to
  23. * a {@code Map}.
  24. *
  25. * @param {Object} minHeightForQualityLvl - The 'config.videoQuality.minHeightForQualityLvl' Object from
  26. * the configuration. See config.js for more details.
  27. * @returns {Map<number, number>|undefined} - A mapping of minimal thumbnail height required for given quality level or
  28. * {@code undefined} if the map contains invalid values.
  29. */
  30. export function validateMinHeightForQualityLvl(minHeightForQualityLvl: Object): ?Map<number, number> {
  31. if (typeof minHeightForQualityLvl !== 'object'
  32. || Object.keys(minHeightForQualityLvl).map(lvl => Number(lvl))
  33. .find(lvl => lvl === null || isNaN(lvl) || lvl < 0)) {
  34. return undefined;
  35. }
  36. const levelsSorted
  37. = Object.keys(minHeightForQualityLvl)
  38. .map(k => Number(k))
  39. .sort((a, b) => a - b);
  40. const map = new Map();
  41. for (const level of levelsSorted) {
  42. const configQuality = minHeightForQualityLvl[level];
  43. const appQuality = CFG_LVL_TO_APP_QUALITY_LVL[configQuality];
  44. if (!appQuality) {
  45. return undefined;
  46. }
  47. map.set(level, appQuality);
  48. }
  49. return map;
  50. }