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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. import { SET_CONFIG } from '../base/config/actionTypes';
  2. import { IConfig } from '../base/config/configType';
  3. import PersistenceRegistry from '../base/redux/PersistenceRegistry';
  4. import ReducerRegistry from '../base/redux/ReducerRegistry';
  5. import { set } from '../base/redux/functions';
  6. import {
  7. SET_MAX_RECEIVER_VIDEO_QUALITY_FOR_LARGE_VIDEO,
  8. SET_MAX_RECEIVER_VIDEO_QUALITY_FOR_SCREEN_SHARING_FILMSTRIP,
  9. SET_MAX_RECEIVER_VIDEO_QUALITY_FOR_STAGE_FILMSTRIP,
  10. SET_MAX_RECEIVER_VIDEO_QUALITY_FOR_TILE_VIEW,
  11. SET_MAX_RECEIVER_VIDEO_QUALITY_FOR_VERTICAL_FILMSTRIP,
  12. SET_PREFERRED_VIDEO_QUALITY
  13. } from './actionTypes';
  14. import { VIDEO_QUALITY_LEVELS } from './constants';
  15. import { validateMinHeightForQualityLvl } from './functions';
  16. import logger from './logger';
  17. const DEFAULT_STATE = {
  18. maxReceiverVideoQualityForLargeVideo: VIDEO_QUALITY_LEVELS.ULTRA,
  19. maxReceiverVideoQualityForScreenSharingFilmstrip: VIDEO_QUALITY_LEVELS.HIGH,
  20. maxReceiverVideoQualityForStageFilmstrip: VIDEO_QUALITY_LEVELS.HIGH,
  21. maxReceiverVideoQualityForTileView: VIDEO_QUALITY_LEVELS.STANDARD,
  22. maxReceiverVideoQualityForVerticalFilmstrip: VIDEO_QUALITY_LEVELS.LOW,
  23. minHeightForQualityLvl: new Map(),
  24. preferredVideoQuality: VIDEO_QUALITY_LEVELS.ULTRA
  25. };
  26. Object.values(VIDEO_QUALITY_LEVELS).sort()
  27. .forEach(value => {
  28. if (value > VIDEO_QUALITY_LEVELS.NONE) {
  29. DEFAULT_STATE.minHeightForQualityLvl.set(value, value);
  30. }
  31. });
  32. export interface IVideoQualityState {
  33. maxReceiverVideoQualityForLargeVideo: number;
  34. maxReceiverVideoQualityForScreenSharingFilmstrip: number;
  35. maxReceiverVideoQualityForStageFilmstrip: number;
  36. maxReceiverVideoQualityForTileView: number;
  37. maxReceiverVideoQualityForVerticalFilmstrip: number;
  38. minHeightForQualityLvl: Map<number, number>;
  39. preferredVideoQuality: number;
  40. }
  41. export interface IVideoQualityPersistedState {
  42. persistedPrefferedVideoQuality?: number;
  43. }
  44. // When the persisted state is initialized the current state (for example the default state) is erased.
  45. // In order to workaround this issue we need additional state for the persisted properties.
  46. PersistenceRegistry.register('features/video-quality-persistent-storage');
  47. ReducerRegistry.register<IVideoQualityPersistedState>('features/video-quality-persistent-storage',
  48. (state = {}, action): IVideoQualityPersistedState => {
  49. switch (action.type) {
  50. case SET_PREFERRED_VIDEO_QUALITY: {
  51. const { preferredVideoQuality } = action;
  52. return {
  53. ...state,
  54. persistedPrefferedVideoQuality: preferredVideoQuality
  55. };
  56. }
  57. }
  58. return state;
  59. });
  60. ReducerRegistry.register<IVideoQualityState>('features/video-quality',
  61. (state = DEFAULT_STATE, action): IVideoQualityState => {
  62. switch (action.type) {
  63. case SET_CONFIG:
  64. return _setConfig(state, action);
  65. case SET_MAX_RECEIVER_VIDEO_QUALITY_FOR_LARGE_VIDEO:
  66. return set(state,
  67. 'maxReceiverVideoQualityForLargeVideo',
  68. action.maxReceiverVideoQuality);
  69. case SET_MAX_RECEIVER_VIDEO_QUALITY_FOR_SCREEN_SHARING_FILMSTRIP:
  70. return set(state,
  71. 'maxReceiverVideoQualityForScreenSharingFilmstrip',
  72. action.maxReceiverVideoQuality);
  73. case SET_MAX_RECEIVER_VIDEO_QUALITY_FOR_STAGE_FILMSTRIP:
  74. return set(
  75. state,
  76. 'maxReceiverVideoQualityForStageFilmstrip',
  77. action.maxReceiverVideoQuality);
  78. case SET_MAX_RECEIVER_VIDEO_QUALITY_FOR_TILE_VIEW:
  79. return set(
  80. state,
  81. 'maxReceiverVideoQualityForTileView',
  82. action.maxReceiverVideoQuality);
  83. case SET_MAX_RECEIVER_VIDEO_QUALITY_FOR_VERTICAL_FILMSTRIP:
  84. return set(
  85. state,
  86. 'maxReceiverVideoQualityForVerticalFilmstrip',
  87. action.maxReceiverVideoQuality);
  88. case SET_PREFERRED_VIDEO_QUALITY: {
  89. const { preferredVideoQuality } = action;
  90. return {
  91. ...state,
  92. preferredVideoQuality
  93. };
  94. }
  95. }
  96. return state;
  97. });
  98. /**
  99. * Extracts the height to quality level mapping from the new config.
  100. *
  101. * @param {Object} state - The Redux state of feature base/lastn.
  102. * @param {Action} action - The Redux action SET_CONFIG to reduce.
  103. * @private
  104. * @returns {Object} The new state after the reduction of the specified action.
  105. */
  106. function _setConfig(state: IVideoQualityState, { config }: { config: IConfig; }) {
  107. const configuredMap = config?.videoQuality?.minHeightForQualityLvl;
  108. const convertedMap = validateMinHeightForQualityLvl(configuredMap);
  109. if (configuredMap && !convertedMap) {
  110. logger.error('Invalid config value videoQuality.minHeightForQualityLvl');
  111. }
  112. return convertedMap ? set(state, 'minHeightForQualityLvl', convertedMap) : state;
  113. }