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.

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