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.

reducer.js 2.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import { SET_CONFIG } from '../base/config';
  2. import { PersistenceRegistry, ReducerRegistry, set } from '../base/redux';
  3. import { SET_MAX_RECEIVER_VIDEO_QUALITY, SET_PREFERRED_VIDEO_QUALITY } from './actionTypes';
  4. import { VIDEO_QUALITY_LEVELS } from './constants';
  5. import { validateMinHeightForQualityLvl } from './functions';
  6. import logger from './logger';
  7. const DEFAULT_STATE = {
  8. maxReceiverVideoQuality: VIDEO_QUALITY_LEVELS.ULTRA,
  9. minHeightForQualityLvl: new Map(),
  10. preferredVideoQuality: VIDEO_QUALITY_LEVELS.ULTRA
  11. };
  12. DEFAULT_STATE.minHeightForQualityLvl.set(360, VIDEO_QUALITY_LEVELS.STANDARD);
  13. DEFAULT_STATE.minHeightForQualityLvl.set(720, VIDEO_QUALITY_LEVELS.HIGH);
  14. // When the persisted state is initialized the current state (for example the default state) is erased.
  15. // In order to workaround this issue we need additional state for the persisted properties.
  16. PersistenceRegistry.register('features/video-quality-persistent-storage');
  17. ReducerRegistry.register('features/video-quality-persistent-storage', (state = {}, action) => {
  18. switch (action.type) {
  19. case SET_PREFERRED_VIDEO_QUALITY: {
  20. const { preferredVideoQuality } = action;
  21. return {
  22. ...state,
  23. persistedPrefferedVideoQuality: preferredVideoQuality
  24. };
  25. }
  26. }
  27. return state;
  28. });
  29. ReducerRegistry.register('features/video-quality', (state = DEFAULT_STATE, action) => {
  30. switch (action.type) {
  31. case SET_CONFIG:
  32. return _setConfig(state, action);
  33. case SET_MAX_RECEIVER_VIDEO_QUALITY:
  34. return set(
  35. state,
  36. 'maxReceiverVideoQuality',
  37. action.maxReceiverVideoQuality);
  38. case SET_PREFERRED_VIDEO_QUALITY: {
  39. const { preferredVideoQuality } = action;
  40. return {
  41. ...state,
  42. preferredVideoQuality
  43. };
  44. }
  45. }
  46. return state;
  47. });
  48. /**
  49. * Extracts the height to quality level mapping from the new config.
  50. *
  51. * @param {Object} state - The Redux state of feature base/lastn.
  52. * @param {Action} action - The Redux action SET_CONFIG to reduce.
  53. * @private
  54. * @returns {Object} The new state after the reduction of the specified action.
  55. */
  56. function _setConfig(state, { config }) {
  57. const configuredMap = config?.videoQuality?.minHeightForQualityLvl;
  58. const convertedMap = validateMinHeightForQualityLvl(configuredMap);
  59. if (configuredMap && !convertedMap) {
  60. logger.error('Invalid config value videoQuality.minHeightForQualityLvl');
  61. }
  62. return convertedMap ? set(state, 'minHeightForQualityLvl', convertedMap) : state;
  63. }