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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. import { combineReducers } from 'redux';
  2. import { CONFERENCE_FAILED, CONFERENCE_LEFT } from '../conference';
  3. import { ReducerRegistry } from '../redux';
  4. import { TRACK_REMOVED } from '../tracks';
  5. import {
  6. SET_AUDIO_AVAILABLE,
  7. SET_AUDIO_MUTED,
  8. SET_CAMERA_FACING_MODE,
  9. SET_VIDEO_AVAILABLE,
  10. SET_VIDEO_MUTED,
  11. STORE_VIDEO_TRANSFORM,
  12. TOGGLE_CAMERA_FACING_MODE
  13. } from './actionTypes';
  14. import { CAMERA_FACING_MODE } from './constants';
  15. /**
  16. * Media state object for local audio.
  17. *
  18. * @typedef {Object} AudioMediaState
  19. * @property {boolean} muted=false - Audio muted state.
  20. */
  21. /**
  22. * Initial state for local audio.
  23. *
  24. * @type {AudioMediaState}
  25. */
  26. const AUDIO_INITIAL_MEDIA_STATE = {
  27. available: true,
  28. muted: false
  29. };
  30. /**
  31. * Reducer for audio media state.
  32. *
  33. * @param {AudioMediaState} state - Media state of local audio.
  34. * @param {Object} action - Action object.
  35. * @param {string} action.type - Type of action.
  36. * @private
  37. * @returns {AudioMediaState}
  38. */
  39. function _audio(state = AUDIO_INITIAL_MEDIA_STATE, action) {
  40. switch (action.type) {
  41. case SET_AUDIO_AVAILABLE:
  42. return {
  43. ...state,
  44. available: action.available
  45. };
  46. case SET_AUDIO_MUTED:
  47. return {
  48. ...state,
  49. muted: action.muted
  50. };
  51. default:
  52. return state;
  53. }
  54. }
  55. /**
  56. * Media state object for local video.
  57. *
  58. * @typedef {Object} VideoMediaState
  59. * @property {CAMERA_FACING_MODE} facingMode='user' - Camera facing mode.
  60. * @property {boolean} muted=false - Video muted state.
  61. */
  62. /**
  63. * Initial state for video.
  64. *
  65. * @type {VideoMediaState}
  66. */
  67. const VIDEO_INITIAL_MEDIA_STATE = {
  68. available: true,
  69. facingMode: CAMERA_FACING_MODE.USER,
  70. muted: 0,
  71. /**
  72. * The video {@link Transform}s applied to {@code MediaStream}s by
  73. * {@code id} i.e. "pinch to zoom".
  74. */
  75. transforms: {}
  76. };
  77. /**
  78. * Reducer for camera media state.
  79. *
  80. * @param {VideoMediaState} state - Media state of local video.
  81. * @param {Object} action - Action object.
  82. * @param {string} action.type - Type of action.
  83. * @private
  84. * @returns {VideoMediaState}
  85. */
  86. function _video(state = VIDEO_INITIAL_MEDIA_STATE, action) {
  87. switch (action.type) {
  88. case CONFERENCE_FAILED:
  89. case CONFERENCE_LEFT:
  90. return _clearAllVideoTransforms(state);
  91. case SET_CAMERA_FACING_MODE:
  92. return {
  93. ...state,
  94. facingMode: action.cameraFacingMode
  95. };
  96. case SET_VIDEO_AVAILABLE:
  97. return {
  98. ...state,
  99. available: action.available
  100. };
  101. case SET_VIDEO_MUTED:
  102. return {
  103. ...state,
  104. muted: action.muted
  105. };
  106. case STORE_VIDEO_TRANSFORM:
  107. return _storeVideoTransform(state, action);
  108. case TOGGLE_CAMERA_FACING_MODE: {
  109. let cameraFacingMode = state.facingMode;
  110. cameraFacingMode
  111. = cameraFacingMode === CAMERA_FACING_MODE.USER
  112. ? CAMERA_FACING_MODE.ENVIRONMENT
  113. : CAMERA_FACING_MODE.USER;
  114. return {
  115. ...state,
  116. facingMode: cameraFacingMode
  117. };
  118. }
  119. case TRACK_REMOVED:
  120. return _trackRemoved(state, action);
  121. default:
  122. return state;
  123. }
  124. }
  125. /**
  126. * Listen for various actions related to media devices.
  127. *
  128. * @param {Object} state - State of media devices.
  129. * @param {Object} action - Action object.
  130. * @param {string} action.type - Type of action.
  131. * @param {Object} action.media - Information about media devices to be
  132. * modified.
  133. * @returns {Object}
  134. */
  135. ReducerRegistry.register('features/base/media', combineReducers({
  136. audio: _audio,
  137. video: _video
  138. }));
  139. /**
  140. * Removes all stored video {@link Transform}s.
  141. *
  142. * @param {Object} state - The {@code video} state of the feature base/media.
  143. * @private
  144. * @returns {Object}
  145. */
  146. function _clearAllVideoTransforms(state) {
  147. return {
  148. ...state,
  149. transforms: VIDEO_INITIAL_MEDIA_STATE.transforms
  150. };
  151. }
  152. /**
  153. * Stores the last applied transform to a stream.
  154. *
  155. * @param {Object} state - The {@code video} state of the feature base/media.
  156. * @param {Object} action - The redux action {@link STORE_VIDEO_TRANSFORM}.
  157. * @private
  158. * @returns {Object}
  159. */
  160. function _storeVideoTransform(state, { streamId, transform }) {
  161. return {
  162. ...state,
  163. transforms: {
  164. ...state.transforms,
  165. [streamId]: transform
  166. }
  167. };
  168. }
  169. /**
  170. * Removes the stored video {@link Transform} associated with a
  171. * {@code MediaStream} when its respective track is removed.
  172. *
  173. * @param {Object} state - The {@code video} state of the feature base/media.
  174. * @param {Object} action - The redux action {@link TRACK_REMOVED}.
  175. * @private
  176. * @returns {Object}
  177. */
  178. function _trackRemoved(state, { track: { jitsiTrack } }) {
  179. if (jitsiTrack) {
  180. const streamId = jitsiTrack.getStreamId();
  181. if (streamId && streamId in state.transforms) {
  182. const nextTransforms = {
  183. ...state.transforms
  184. };
  185. delete nextTransforms[streamId];
  186. return {
  187. ...state,
  188. transforms: nextTransforms
  189. };
  190. }
  191. }
  192. return state;
  193. }