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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. import { AnyAction, 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. GUM_PENDING,
  7. SET_AUDIO_AVAILABLE,
  8. SET_AUDIO_MUTED,
  9. SET_AUDIO_UNMUTE_PERMISSIONS,
  10. SET_CAMERA_FACING_MODE,
  11. SET_INITIAL_GUM_PROMISE,
  12. SET_SCREENSHARE_MUTED,
  13. SET_VIDEO_AVAILABLE,
  14. SET_VIDEO_MUTED,
  15. SET_VIDEO_UNMUTE_PERMISSIONS,
  16. STORE_VIDEO_TRANSFORM,
  17. TOGGLE_CAMERA_FACING_MODE
  18. } from './actionTypes';
  19. import { CAMERA_FACING_MODE, MEDIA_TYPE, SCREENSHARE_MUTISM_AUTHORITY } from './constants';
  20. import { IGUMPendingState } from './types';
  21. /**
  22. * Media state object for local audio.
  23. *
  24. * @typedef {Object} AudioMediaState
  25. * @property {boolean} muted=false - Audio muted state.
  26. */
  27. // FIXME Technically, _AUDIO_INITIAL_MEDIA_STATE is a constant internal to the
  28. // feature base/media and used in multiple files so it should be in
  29. // constants.js. Practically though, AudioMediaState would then be used in
  30. // multiple files as well so I don't know where and how to move it.
  31. /**
  32. * Initial state for local audio.
  33. *
  34. * @type {AudioMediaState}
  35. */
  36. export const _AUDIO_INITIAL_MEDIA_STATE = {
  37. available: true,
  38. gumPending: IGUMPendingState.NONE,
  39. unmuteBlocked: false,
  40. muted: false
  41. };
  42. /**
  43. * Reducer for audio media state.
  44. *
  45. * @param {AudioMediaState} state - Media state of local audio.
  46. * @param {Object} action - Action object.
  47. * @param {string} action.type - Type of action.
  48. * @private
  49. * @returns {AudioMediaState}
  50. */
  51. function _audio(state: IAudioState = _AUDIO_INITIAL_MEDIA_STATE, action: AnyAction) {
  52. switch (action.type) {
  53. case SET_AUDIO_AVAILABLE:
  54. return {
  55. ...state,
  56. available: action.available
  57. };
  58. case GUM_PENDING:
  59. if (action.mediaTypes.includes(MEDIA_TYPE.AUDIO)) {
  60. return {
  61. ...state,
  62. gumPending: action.status
  63. };
  64. }
  65. return state;
  66. case SET_AUDIO_MUTED:
  67. return {
  68. ...state,
  69. muted: action.muted
  70. };
  71. case SET_AUDIO_UNMUTE_PERMISSIONS:
  72. return {
  73. ...state,
  74. unmuteBlocked: action.blocked
  75. };
  76. default:
  77. return state;
  78. }
  79. }
  80. // Using a deferred promise here to make sure that once the connection is established even if conference.init and the
  81. // initial track creation haven't been started we would wait for it to finish before starting to join the room.
  82. // NOTE: The previous implementation was using the GUM promise from conference.init. But it turned out that connect
  83. // may finish even before conference.init is executed.
  84. const DEFAULT_INITIAL_PROMISE_STATE = Promise.withResolvers<IInitialGUMPromiseResult>();
  85. /**
  86. * Reducer for the common properties in media state.
  87. *
  88. * @param {ICommonState} state - Common media state.
  89. * @param {Object} action - Action object.
  90. * @param {string} action.type - Type of action.
  91. * @returns {ICommonState}
  92. */
  93. function _initialGUMPromise(state: PromiseWithResolvers<IInitialGUMPromiseResult> | null = DEFAULT_INITIAL_PROMISE_STATE,
  94. action: AnyAction) {
  95. if (action.type === SET_INITIAL_GUM_PROMISE) {
  96. return action.promise ?? null;
  97. }
  98. return state;
  99. }
  100. /**
  101. * Media state object for local screenshare.
  102. *
  103. * @typedef {Object} ScreenshareMediaState
  104. * @property {boolean} available=true - Screenshare available state.
  105. * @property {boolean} muted=true - Screenshare muted state.
  106. * @property {boolean} unmuteBlocked=false - Screenshare unmute blocked state.
  107. */
  108. /**
  109. * Initial state for video.
  110. *
  111. * @type {ScreenshareMediaState}
  112. */
  113. export const _SCREENSHARE_INITIAL_MEDIA_STATE = {
  114. available: true,
  115. muted: SCREENSHARE_MUTISM_AUTHORITY.USER,
  116. unmuteBlocked: false
  117. };
  118. /**
  119. * Reducer for screenshare media state.
  120. *
  121. * @param {VideoMediaState} state - Media state of local screenshare.
  122. * @param {Object} action - Action object.
  123. * @param {string} action.type - Type of action.
  124. * @private
  125. * @returns {ScreenshareMediaState}
  126. */
  127. function _screenshare(state: IScreenshareState = _SCREENSHARE_INITIAL_MEDIA_STATE, action: AnyAction) {
  128. switch (action.type) {
  129. case SET_SCREENSHARE_MUTED:
  130. return {
  131. ...state,
  132. muted: action.muted
  133. };
  134. case SET_VIDEO_UNMUTE_PERMISSIONS:
  135. return {
  136. ...state,
  137. unmuteBlocked: action.blocked
  138. };
  139. default:
  140. return state;
  141. }
  142. }
  143. /**
  144. * Media state object for local video.
  145. *
  146. * @typedef {Object} VideoMediaState
  147. * @property {CAMERA_FACING_MODE} facingMode='user' - Camera facing mode.
  148. * @property {boolean} muted=false - Video muted state.
  149. */
  150. // FIXME Technically, _VIDEO_INITIAL_MEDIA_STATE is a constant internal to the
  151. // feature base/media and used in multiple files so it should be in
  152. // constants.js. Practically though, VideoMediaState would then be used in
  153. // multiple files as well so I don't know where and how to move it.
  154. /**
  155. * Initial state for video.
  156. *
  157. * @type {VideoMediaState}
  158. */
  159. export const _VIDEO_INITIAL_MEDIA_STATE = {
  160. available: true,
  161. gumPending: IGUMPendingState.NONE,
  162. unmuteBlocked: false,
  163. facingMode: CAMERA_FACING_MODE.USER,
  164. muted: 0,
  165. /**
  166. * The video {@link Transform}s applied to {@code MediaStream}s by
  167. * {@code id} i.e. "pinch to zoom".
  168. */
  169. transforms: {}
  170. };
  171. /**
  172. * Reducer for camera media state.
  173. *
  174. * @param {VideoMediaState} state - Media state of local video.
  175. * @param {Object} action - Action object.
  176. * @param {string} action.type - Type of action.
  177. * @private
  178. * @returns {VideoMediaState}
  179. */
  180. function _video(state: IVideoState = _VIDEO_INITIAL_MEDIA_STATE, action: any) {
  181. switch (action.type) {
  182. case CONFERENCE_FAILED:
  183. case CONFERENCE_LEFT:
  184. return _clearAllVideoTransforms(state);
  185. case GUM_PENDING:
  186. if (action.mediaTypes.includes(MEDIA_TYPE.VIDEO)) {
  187. return {
  188. ...state,
  189. gumPending: action.status
  190. };
  191. }
  192. return state;
  193. case SET_CAMERA_FACING_MODE:
  194. return {
  195. ...state,
  196. facingMode: action.cameraFacingMode
  197. };
  198. case SET_VIDEO_AVAILABLE:
  199. return {
  200. ...state,
  201. available: action.available
  202. };
  203. case SET_VIDEO_MUTED:
  204. return {
  205. ...state,
  206. muted: action.muted
  207. };
  208. case SET_VIDEO_UNMUTE_PERMISSIONS:
  209. return {
  210. ...state,
  211. unmuteBlocked: action.blocked
  212. };
  213. case STORE_VIDEO_TRANSFORM:
  214. return _storeVideoTransform(state, action);
  215. case TOGGLE_CAMERA_FACING_MODE: {
  216. let cameraFacingMode = state.facingMode;
  217. cameraFacingMode
  218. = cameraFacingMode === CAMERA_FACING_MODE.USER
  219. ? CAMERA_FACING_MODE.ENVIRONMENT
  220. : CAMERA_FACING_MODE.USER;
  221. return {
  222. ...state,
  223. facingMode: cameraFacingMode
  224. };
  225. }
  226. case TRACK_REMOVED:
  227. return _trackRemoved(state, action);
  228. default:
  229. return state;
  230. }
  231. }
  232. interface IAudioState {
  233. available: boolean;
  234. gumPending: IGUMPendingState;
  235. muted: boolean;
  236. unmuteBlocked: boolean;
  237. }
  238. interface IInitialGUMPromiseResult {
  239. errors?: any;
  240. tracks: Array<any>;
  241. }
  242. interface IScreenshareState {
  243. available: boolean;
  244. muted: number;
  245. unmuteBlocked: boolean;
  246. }
  247. interface IVideoState {
  248. available: boolean;
  249. facingMode: string;
  250. gumPending: IGUMPendingState;
  251. muted: number;
  252. transforms: Object;
  253. unmuteBlocked: boolean;
  254. }
  255. export interface IMediaState {
  256. audio: IAudioState;
  257. initialGUMPromise: PromiseWithResolvers<IInitialGUMPromiseResult> | null;
  258. screenshare: IScreenshareState;
  259. video: IVideoState;
  260. }
  261. /**
  262. * Listen for various actions related to media devices.
  263. *
  264. * @param {Object} state - State of media devices.
  265. * @param {Object} action - Action object.
  266. * @param {string} action.type - Type of action.
  267. * @param {Object} action.media - Information about media devices to be
  268. * modified.
  269. * @returns {Object}
  270. */
  271. ReducerRegistry.register<IMediaState>('features/base/media', combineReducers({
  272. audio: _audio,
  273. initialGUMPromise: _initialGUMPromise,
  274. screenshare: _screenshare,
  275. video: _video
  276. }));
  277. /**
  278. * Removes all stored video {@link Transform}s.
  279. *
  280. * @param {Object} state - The {@code video} state of the feature base/media.
  281. * @private
  282. * @returns {Object}
  283. */
  284. function _clearAllVideoTransforms(state: IVideoState) {
  285. return {
  286. ...state,
  287. transforms: _VIDEO_INITIAL_MEDIA_STATE.transforms
  288. };
  289. }
  290. /**
  291. * Stores the last applied transform to a stream.
  292. *
  293. * @param {Object} state - The {@code video} state of the feature base/media.
  294. * @param {Object} action - The redux action {@link STORE_VIDEO_TRANSFORM}.
  295. * @private
  296. * @returns {Object}
  297. */
  298. function _storeVideoTransform(state: IVideoState, { streamId, transform }: { streamId: string; transform: string; }) {
  299. return {
  300. ...state,
  301. transforms: {
  302. ...state.transforms,
  303. [streamId]: transform
  304. }
  305. };
  306. }
  307. /**
  308. * Removes the stored video {@link Transform} associated with a
  309. * {@code MediaStream} when its respective track is removed.
  310. *
  311. * @param {Object} state - The {@code video} state of the feature base/media.
  312. * @param {Object} action - The redux action {@link TRACK_REMOVED}.
  313. * @private
  314. * @returns {Object}
  315. */
  316. function _trackRemoved(state: IVideoState, { track: { jitsiTrack } }: { track: { jitsiTrack: any; }; }) {
  317. if (jitsiTrack) {
  318. const streamId = jitsiTrack.getStreamId();
  319. if (streamId && streamId in state.transforms) {
  320. const nextTransforms: any = {
  321. ...state.transforms
  322. };
  323. delete nextTransforms[streamId];
  324. return {
  325. ...state,
  326. transforms: nextTransforms
  327. };
  328. }
  329. }
  330. return state;
  331. }