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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. import { combineReducers } from 'redux';
  2. import { ReducerRegistry } from '../redux';
  3. import {
  4. SET_AUDIO_AVAILABLE,
  5. SET_AUDIO_MUTED,
  6. SET_CAMERA_FACING_MODE,
  7. SET_VIDEO_AVAILABLE,
  8. SET_VIDEO_MUTED,
  9. TOGGLE_CAMERA_FACING_MODE
  10. } from './actionTypes';
  11. import { CAMERA_FACING_MODE } from './constants';
  12. /**
  13. * Media state object for local audio.
  14. *
  15. * @typedef {Object} AudioMediaState
  16. * @property {boolean} muted=false - Audio muted state.
  17. */
  18. /**
  19. * Initial state for local audio.
  20. *
  21. * @type {AudioMediaState}
  22. */
  23. const AUDIO_INITIAL_MEDIA_STATE = {
  24. available: true,
  25. muted: false
  26. };
  27. /**
  28. * Reducer for audio media state.
  29. *
  30. * @param {AudioMediaState} state - Media state of local audio.
  31. * @param {Object} action - Action object.
  32. * @param {string} action.type - Type of action.
  33. * @private
  34. * @returns {AudioMediaState}
  35. */
  36. function _audio(state = AUDIO_INITIAL_MEDIA_STATE, action) {
  37. switch (action.type) {
  38. case SET_AUDIO_AVAILABLE:
  39. return {
  40. ...state,
  41. available: action.available
  42. };
  43. case SET_AUDIO_MUTED:
  44. return {
  45. ...state,
  46. muted: action.muted
  47. };
  48. default:
  49. return state;
  50. }
  51. }
  52. /**
  53. * Media state object for local video.
  54. *
  55. * @typedef {Object} VideoMediaState
  56. * @property {CAMERA_FACING_MODE} facingMode='user' - Camera facing mode.
  57. * @property {boolean} muted=false - Video muted state.
  58. */
  59. /**
  60. * Initial state for video.
  61. *
  62. * @type {VideoMediaState}
  63. */
  64. const VIDEO_INITIAL_MEDIA_STATE = {
  65. available: true,
  66. facingMode: CAMERA_FACING_MODE.USER,
  67. muted: 0
  68. };
  69. /**
  70. * Reducer for camera media state.
  71. *
  72. * @param {VideoMediaState} state - Media state of local video.
  73. * @param {Object} action - Action object.
  74. * @param {string} action.type - Type of action.
  75. * @private
  76. * @returns {VideoMediaState}
  77. */
  78. function _video(state = VIDEO_INITIAL_MEDIA_STATE, action) {
  79. switch (action.type) {
  80. case SET_CAMERA_FACING_MODE:
  81. return {
  82. ...state,
  83. facingMode: action.cameraFacingMode
  84. };
  85. case SET_VIDEO_AVAILABLE:
  86. return {
  87. ...state,
  88. available: action.available
  89. };
  90. case SET_VIDEO_MUTED:
  91. return {
  92. ...state,
  93. muted: action.muted
  94. };
  95. case TOGGLE_CAMERA_FACING_MODE: {
  96. let cameraFacingMode = state.facingMode;
  97. cameraFacingMode
  98. = cameraFacingMode === CAMERA_FACING_MODE.USER
  99. ? CAMERA_FACING_MODE.ENVIRONMENT
  100. : CAMERA_FACING_MODE.USER;
  101. return {
  102. ...state,
  103. facingMode: cameraFacingMode
  104. };
  105. }
  106. default:
  107. return state;
  108. }
  109. }
  110. /**
  111. * Listen for various actions related to media devices.
  112. *
  113. * @param {Object} state - State of media devices.
  114. * @param {Object} action - Action object.
  115. * @param {string} action.type - Type of action.
  116. * @param {Object} action.media - Information about media devices to be
  117. * modified.
  118. * @returns {Object}
  119. */
  120. ReducerRegistry.register('features/base/media', combineReducers({
  121. audio: _audio,
  122. video: _video
  123. }));