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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /* @flow */
  2. import { SET_ROOM } from '../conference';
  3. import { parseURLParams } from '../config';
  4. import { MiddlewareRegistry } from '../redux';
  5. import { setTrackMuted, TRACK_ADDED } from '../tracks';
  6. import { setAudioMuted, setCameraFacingMode, setVideoMuted } from './actions';
  7. import { CAMERA_FACING_MODE } from './constants';
  8. /**
  9. * Implements the entry point of the middleware of the feature base/media.
  10. *
  11. * @param {Store} store - The redux store.
  12. * @returns {Function}
  13. */
  14. MiddlewareRegistry.register(store => next => action => {
  15. switch (action.type) {
  16. case SET_ROOM:
  17. return _setRoom(store, next, action);
  18. case TRACK_ADDED: {
  19. const result = next(action);
  20. action.track.local && _syncTrackMutedState(store, action.track);
  21. return result;
  22. }
  23. }
  24. return next(action);
  25. });
  26. /**
  27. * Notifies the feature base/media that the action {@link SET_ROOM} is being
  28. * dispatched within a specific redux {@code store}.
  29. *
  30. * @param {Store} store - The redux store in which the specified {@code action}
  31. * is being dispatched.
  32. * @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
  33. * specified {@code action} to the specified {@code store}.
  34. * @param {Action} action - The redux action, {@code SET_ROOM}, which is being
  35. * dispatched in the specified {@code store}.
  36. * @private
  37. * @returns {Object} The new state that is the result of the reduction of the
  38. * specified {@code action}.
  39. */
  40. function _setRoom({ dispatch, getState }, next, action) {
  41. const state = getState();
  42. let audioMuted;
  43. let videoMuted;
  44. if (action.room) {
  45. // The Jitsi Meet client may override the Jitsi Meet deployment on the
  46. // subject of startWithAudioMuted and/or startWithVideoMuted in the
  47. // (location) URL.
  48. const urlParams
  49. = parseURLParams(state['features/base/connection'].locationURL);
  50. audioMuted = urlParams['config.startWithAudioMuted'];
  51. videoMuted = urlParams['config.startWithVideoMuted'];
  52. }
  53. // Of course, the Jitsi Meet deployment may define startWithAudioMuted
  54. // and/or startWithVideoMuted through config.js which should be respected if
  55. // the client did not override it.
  56. const config = state['features/base/config'];
  57. typeof audioMuted === 'undefined'
  58. && (audioMuted = config.startWithAudioMuted);
  59. typeof videoMuted === 'undefined'
  60. && (videoMuted = config.startWithVideoMuted);
  61. // Apply startWithAudioMuted and startWithVideoMuted.
  62. const { audio, video } = state['features/base/media'];
  63. audioMuted = Boolean(audioMuted);
  64. videoMuted = Boolean(videoMuted);
  65. (audio.muted !== audioMuted) && dispatch(setAudioMuted(audioMuted));
  66. (video.facingMode !== CAMERA_FACING_MODE.USER)
  67. && dispatch(setCameraFacingMode(CAMERA_FACING_MODE.USER));
  68. (Boolean(video.muted) !== videoMuted)
  69. && dispatch(setVideoMuted(videoMuted));
  70. return next(action);
  71. }
  72. /**
  73. * Syncs muted state of local media track with muted state from media state.
  74. *
  75. * @param {Store} store - The redux store.
  76. * @param {Track} track - The local media track.
  77. * @private
  78. * @returns {void}
  79. */
  80. function _syncTrackMutedState({ dispatch, getState }, track) {
  81. const state = getState()['features/base/media'];
  82. const muted = Boolean(state[track.mediaType].muted);
  83. // XXX If muted state of track when it was added is different from our media
  84. // muted state, we need to mute track and explicitly modify 'muted' property
  85. // on track. This is because though TRACK_ADDED action was dispatched it's
  86. // not yet in redux state and JitsiTrackEvents.TRACK_MUTE_CHANGED may be
  87. // fired before track gets to state.
  88. if (track.muted !== muted) {
  89. track.muted = muted;
  90. dispatch(setTrackMuted(track.jitsiTrack, muted));
  91. }
  92. }