Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

middleware.js 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /* @flow */
  2. import { SET_ROOM, setAudioOnly } 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 { room } = action;
  42. // Read the config.
  43. const state = getState();
  44. let urlParams;
  45. let audioMuted;
  46. let videoMuted;
  47. if (room) {
  48. // The Jitsi Meet client may override the Jitsi Meet deployment in the
  49. // (location) URL on the subject of the following:
  50. // - startAudioOnly
  51. // - startWithAudioMuted
  52. // - startWithVideoMuted
  53. urlParams
  54. = parseURLParams(state['features/base/connection'].locationURL);
  55. audioMuted = urlParams['config.startWithAudioMuted'];
  56. videoMuted = urlParams['config.startWithVideoMuted'];
  57. }
  58. // Of course, the Jitsi Meet deployment defines config.js which should be
  59. // respected if the client did not override it.
  60. const config = state['features/base/config'];
  61. typeof audioMuted === 'undefined'
  62. && (audioMuted = config.startWithAudioMuted);
  63. typeof videoMuted === 'undefined'
  64. && (videoMuted = config.startWithVideoMuted);
  65. // Apply the config.
  66. // Unconditionally express the desires/expectations/intents of the app and
  67. // the user i.e. the state of base/media. Eventually, practice/reality i.e.
  68. // the state of base/tracks will or will not agree with the desires.
  69. dispatch(setAudioMuted(Boolean(audioMuted)));
  70. dispatch(setCameraFacingMode(CAMERA_FACING_MODE.USER));
  71. dispatch(setVideoMuted(Boolean(videoMuted)));
  72. // config.startAudioOnly
  73. //
  74. // FIXME Technically, the audio-only feature is owned by base/conference,
  75. // not base/media so the following should be in base/conference.
  76. // Practically, I presume it was easier to write the source code here
  77. // because it looks like config.startWithAudioMuted and
  78. // config.startWithVideoMuted.
  79. if (room) {
  80. let audioOnly = urlParams && urlParams['config.startAudioOnly'];
  81. typeof audioOnly === 'undefined' && (audioOnly = config.startAudioOnly);
  82. dispatch(setAudioOnly(Boolean(audioOnly)));
  83. }
  84. return next(action);
  85. }
  86. /**
  87. * Syncs muted state of local media track with muted state from media state.
  88. *
  89. * @param {Store} store - The redux store.
  90. * @param {Track} track - The local media track.
  91. * @private
  92. * @returns {void}
  93. */
  94. function _syncTrackMutedState({ getState }, track) {
  95. const state = getState()['features/base/media'];
  96. const muted = Boolean(state[track.mediaType].muted);
  97. // XXX If muted state of track when it was added is different from our media
  98. // muted state, we need to mute track and explicitly modify 'muted' property
  99. // on track. This is because though TRACK_ADDED action was dispatched it's
  100. // not yet in redux state and JitsiTrackEvents.TRACK_MUTE_CHANGED may be
  101. // fired before track gets to state.
  102. if (track.muted !== muted) {
  103. track.muted = muted;
  104. setTrackMuted(track.jitsiTrack, muted);
  105. }
  106. }