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.

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