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.7KB

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