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.

middleware.js 2.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. import { CONFERENCE_LEFT } from '../conference';
  2. import { MiddlewareRegistry } from '../redux';
  3. import { setTrackMuted, TRACK_ADDED } from '../tracks';
  4. import {
  5. audioMutedChanged,
  6. cameraFacingModeChanged,
  7. videoMutedChanged
  8. } from './actions';
  9. import { CAMERA_FACING_MODE } from './constants';
  10. /**
  11. * Middleware that captures CONFERENCE_LEFT action and restores initial state
  12. * for media devices. Also captures TRACK_ADDED to sync 'muted' state.
  13. *
  14. * @param {Store} store - Redux store.
  15. * @returns {Function}
  16. */
  17. MiddlewareRegistry.register(store => next => action => {
  18. const result = next(action);
  19. switch (action.type) {
  20. case CONFERENCE_LEFT:
  21. resetInitialMediaState(store);
  22. break;
  23. case TRACK_ADDED:
  24. action.track.local && syncTrackMutedState(store, action.track);
  25. break;
  26. }
  27. return result;
  28. });
  29. /**
  30. * Resets initial media state.
  31. *
  32. * @param {Store} store - Redux store.
  33. * @returns {void}
  34. */
  35. function resetInitialMediaState(store) {
  36. const { dispatch, getState } = store;
  37. const state = getState()['features/base/media'];
  38. state.audio.muted && dispatch(audioMutedChanged(false));
  39. (state.video.facingMode !== CAMERA_FACING_MODE.USER)
  40. && dispatch(cameraFacingModeChanged(CAMERA_FACING_MODE.USER));
  41. state.video.muted && dispatch(videoMutedChanged(false));
  42. }
  43. /**
  44. * Syncs muted state of local media track with muted state from media state.
  45. *
  46. * @param {Store} store - Redux store.
  47. * @param {Track} track - Local media track.
  48. * @returns {void}
  49. */
  50. function syncTrackMutedState(store, track) {
  51. const state = store.getState()['features/base/media'];
  52. const muted = state[track.mediaType].muted;
  53. // XXX If muted state of track when it was added is different from our media
  54. // muted state, we need to mute track and explicitly modify 'muted' property
  55. // on track. This is because though TRACK_ADDED action was dispatched it's
  56. // not yet in Redux state and JitsiTrackEvents.TRACK_MUTE_CHANGED may be
  57. // fired before track gets to state.
  58. if (track.muted !== muted) {
  59. track.muted = muted;
  60. setTrackMuted(track.jitsiTrack, muted);
  61. }
  62. }