您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

reducer.ts 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. import { combineReducers } from 'redux';
  2. import { CONFERENCE_FAILED, CONFERENCE_LEFT } from '../conference/actionTypes';
  3. import ReducerRegistry from '../redux/ReducerRegistry';
  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: IAudioState = _AUDIO_INITIAL_MEDIA_STATE, action: any) {
  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: IScreenshareState = _SCREENSHARE_INITIAL_MEDIA_STATE, action: any) {
  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: IVideoState = _VIDEO_INITIAL_MEDIA_STATE, action: any) {
  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. interface IAudioState {
  192. available: boolean;
  193. muted: boolean;
  194. unmuteBlocked: boolean;
  195. }
  196. interface IScreenshareState {
  197. available: boolean;
  198. muted: number;
  199. unmuteBlocked: boolean;
  200. }
  201. interface IVideoState {
  202. available: boolean;
  203. facingMode: string;
  204. muted: number;
  205. transforms: Object;
  206. unmuteBlocked: boolean;
  207. }
  208. export interface IMediaState {
  209. audio: IAudioState;
  210. screenshare: IScreenshareState;
  211. video: IVideoState;
  212. }
  213. /**
  214. * Listen for various actions related to media devices.
  215. *
  216. * @param {Object} state - State of media devices.
  217. * @param {Object} action - Action object.
  218. * @param {string} action.type - Type of action.
  219. * @param {Object} action.media - Information about media devices to be
  220. * modified.
  221. * @returns {Object}
  222. */
  223. ReducerRegistry.register('features/base/media', combineReducers({
  224. audio: _audio,
  225. screenshare: _screenshare,
  226. video: _video
  227. }));
  228. /**
  229. * Removes all stored video {@link Transform}s.
  230. *
  231. * @param {Object} state - The {@code video} state of the feature base/media.
  232. * @private
  233. * @returns {Object}
  234. */
  235. function _clearAllVideoTransforms(state: IVideoState) {
  236. return {
  237. ...state,
  238. transforms: _VIDEO_INITIAL_MEDIA_STATE.transforms
  239. };
  240. }
  241. /**
  242. * Stores the last applied transform to a stream.
  243. *
  244. * @param {Object} state - The {@code video} state of the feature base/media.
  245. * @param {Object} action - The redux action {@link STORE_VIDEO_TRANSFORM}.
  246. * @private
  247. * @returns {Object}
  248. */
  249. function _storeVideoTransform(state: IVideoState, { streamId, transform }: { streamId: string, transform: string }) {
  250. return {
  251. ...state,
  252. transforms: {
  253. ...state.transforms,
  254. [streamId]: transform
  255. }
  256. };
  257. }
  258. /**
  259. * Removes the stored video {@link Transform} associated with a
  260. * {@code MediaStream} when its respective track is removed.
  261. *
  262. * @param {Object} state - The {@code video} state of the feature base/media.
  263. * @param {Object} action - The redux action {@link TRACK_REMOVED}.
  264. * @private
  265. * @returns {Object}
  266. */
  267. function _trackRemoved(state: IVideoState, { track: { jitsiTrack } } : {track: {jitsiTrack: any}}) {
  268. if (jitsiTrack) {
  269. const streamId = jitsiTrack.getStreamId();
  270. if (streamId && streamId in state.transforms) {
  271. const nextTransforms: any = {
  272. ...state.transforms
  273. };
  274. delete nextTransforms[streamId];
  275. return {
  276. ...state,
  277. transforms: nextTransforms
  278. };
  279. }
  280. }
  281. return state;
  282. }