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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import { VIDEO_QUALITY_LEVELS } from '../../video-quality/constants';
  2. /**
  3. * Determines the lastN value to be used for the conference based on the video quality selected.
  4. *
  5. * @param {string} qualityLevel - Quality level (height) selected.
  6. * @param {number} channelLastN - LastN value set for the whole conference.
  7. * @returns {number} LastN value applicable to the quality level specified.
  8. */
  9. export function getLastNForQualityLevel(qualityLevel, channelLastN) {
  10. let lastN = channelLastN;
  11. const videoQualityLevels = Object.values(VIDEO_QUALITY_LEVELS);
  12. for (const lvl in videoQualityLevels) {
  13. if (videoQualityLevels.hasOwnProperty(lvl)
  14. && qualityLevel === videoQualityLevels[lvl]
  15. && lvl > 1) {
  16. lastN = Math.floor(channelLastN / Math.pow(2, lvl - 1));
  17. }
  18. }
  19. return lastN;
  20. }
  21. /**
  22. * Checks if the given Object is a correct last N limit mapping, coverts both keys and values to numbers and sorts
  23. * the keys in ascending order.
  24. *
  25. * @param {Object} lastNLimits - The Object to be verified.
  26. * @returns {undefined|Map<number, number>}
  27. */
  28. export function validateLastNLimits(lastNLimits) {
  29. // Checks if only numbers are used
  30. if (typeof lastNLimits !== 'object'
  31. || !Object.keys(lastNLimits).length
  32. || Object.keys(lastNLimits)
  33. .find(limit => limit === null || isNaN(Number(limit))
  34. || lastNLimits[limit] === null || isNaN(Number(lastNLimits[limit])))) {
  35. return undefined;
  36. }
  37. // Converts to numbers and sorts the keys
  38. const sortedMapping = new Map();
  39. const orderedLimits = Object.keys(lastNLimits)
  40. .map(n => Number(n))
  41. .sort((n1, n2) => n1 - n2);
  42. for (const limit of orderedLimits) {
  43. sortedMapping.set(limit, Number(lastNLimits[limit]));
  44. }
  45. return sortedMapping;
  46. }
  47. /**
  48. * Returns "last N" value which corresponds to a level defined in the {@code lastNLimits} mapping. See
  49. * {@code config.js} for more detailed explanation on how the mapping is defined.
  50. *
  51. * @param {number} participantsCount - The current number of participants in the conference.
  52. * @param {Map<number, number>} [lastNLimits] - The mapping of number of participants to "last N" values. NOTE that
  53. * this function expects a Map that has been preprocessed by {@link validateLastNLimits}, because the keys must be
  54. * sorted in ascending order and both keys and values should be numbers.
  55. * @returns {number|undefined} - A "last N" number if there was a corresponding "last N" value matched with the number
  56. * of participants or {@code undefined} otherwise.
  57. */
  58. export function limitLastN(participantsCount, lastNLimits) {
  59. if (!lastNLimits || !lastNLimits.keys) {
  60. return undefined;
  61. }
  62. let selectedLimit;
  63. for (const participantsN of lastNLimits.keys()) {
  64. if (participantsCount >= participantsN) {
  65. selectedLimit = participantsN;
  66. }
  67. }
  68. return selectedLimit ? lastNLimits.get(selectedLimit) : undefined;
  69. }