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

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