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

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