Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

middleware.js 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /* @flow */
  2. import { sendEvent } from '../../analytics';
  3. import { SET_ROOM, setAudioOnly } from '../conference';
  4. import { parseURLParams } from '../config';
  5. import { MiddlewareRegistry } from '../redux';
  6. import { setTrackMuted, TRACK_ADDED } from '../tracks';
  7. import { setAudioMuted, setCameraFacingMode, setVideoMuted } from './actions';
  8. import { CAMERA_FACING_MODE } from './constants';
  9. const logger = require('jitsi-meet-logger').getLogger(__filename);
  10. /**
  11. * Implements the entry point of the middleware of the feature base/media.
  12. *
  13. * @param {Store} store - The redux store.
  14. * @returns {Function}
  15. */
  16. MiddlewareRegistry.register(store => next => action => {
  17. switch (action.type) {
  18. case SET_ROOM:
  19. return _setRoom(store, next, action);
  20. case TRACK_ADDED: {
  21. const result = next(action);
  22. action.track.local && _syncTrackMutedState(store, action.track);
  23. return result;
  24. }
  25. }
  26. return next(action);
  27. });
  28. /**
  29. * Notifies the feature base/media that the action {@link SET_ROOM} is being
  30. * dispatched within a specific redux {@code store}.
  31. *
  32. * @param {Store} store - The redux store in which the specified {@code action}
  33. * is being dispatched.
  34. * @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
  35. * specified {@code action} to the specified {@code store}.
  36. * @param {Action} action - The redux action, {@code SET_ROOM}, which is being
  37. * dispatched in the specified {@code store}.
  38. * @private
  39. * @returns {Object} The new state that is the result of the reduction of the
  40. * specified {@code action}.
  41. */
  42. function _setRoom({ dispatch, getState }, next, action) {
  43. const { room } = action;
  44. // Read the config.
  45. const state = getState();
  46. let urlParams;
  47. let audioMuted;
  48. let videoMuted;
  49. if (room) {
  50. // The Jitsi Meet client may override the Jitsi Meet deployment in the
  51. // (location) URL on the subject of the following:
  52. // - startAudioOnly
  53. // - startWithAudioMuted
  54. // - startWithVideoMuted
  55. urlParams
  56. = parseURLParams(state['features/base/connection'].locationURL);
  57. audioMuted = urlParams['config.startWithAudioMuted'];
  58. videoMuted = urlParams['config.startWithVideoMuted'];
  59. }
  60. // Of course, the Jitsi Meet deployment defines config.js which should be
  61. // respected if the client did not override it.
  62. const config = state['features/base/config'];
  63. typeof audioMuted === 'undefined'
  64. && (audioMuted = config.startWithAudioMuted);
  65. typeof videoMuted === 'undefined'
  66. && (videoMuted = config.startWithVideoMuted);
  67. audioMuted = Boolean(audioMuted);
  68. videoMuted = Boolean(videoMuted);
  69. // Apply the config.
  70. sendEvent(`startmuted.client.audio.${audioMuted ? 'muted' : 'unmuted'}`);
  71. sendEvent(`startmuted.client.video.${videoMuted ? 'muted' : 'unmuted'}`);
  72. logger.log(`Start muted: ${audioMuted ? 'audio, ' : ''}${
  73. videoMuted ? 'video' : ''}`);
  74. // Unconditionally express the desires/expectations/intents of the app and
  75. // the user i.e. the state of base/media. Eventually, practice/reality i.e.
  76. // the state of base/tracks will or will not agree with the desires.
  77. dispatch(setAudioMuted(audioMuted));
  78. dispatch(setCameraFacingMode(CAMERA_FACING_MODE.USER));
  79. dispatch(setVideoMuted(videoMuted));
  80. // config.startAudioOnly
  81. //
  82. // FIXME Technically, the audio-only feature is owned by base/conference,
  83. // not base/media so the following should be in base/conference.
  84. // Practically, I presume it was easier to write the source code here
  85. // because it looks like config.startWithAudioMuted and
  86. // config.startWithVideoMuted.
  87. if (room) {
  88. let audioOnly = urlParams && urlParams['config.startAudioOnly'];
  89. typeof audioOnly === 'undefined' && (audioOnly = config.startAudioOnly);
  90. audioOnly = Boolean(audioOnly);
  91. sendEvent(`startaudioonly.${audioOnly ? 'enabled' : 'disabled'}`);
  92. logger.log(`Start audio only set to ${audioOnly.toString()}`);
  93. dispatch(setAudioOnly(audioOnly));
  94. }
  95. return next(action);
  96. }
  97. /**
  98. * Syncs muted state of local media track with muted state from media state.
  99. *
  100. * @param {Store} store - The redux store.
  101. * @param {Track} track - The local media track.
  102. * @private
  103. * @returns {void}
  104. */
  105. function _syncTrackMutedState({ getState }, track) {
  106. const state = getState()['features/base/media'];
  107. const muted = Boolean(state[track.mediaType].muted);
  108. // XXX If muted state of track when it was added is different from our media
  109. // muted state, we need to mute track and explicitly modify 'muted' property
  110. // on track. This is because though TRACK_ADDED action was dispatched it's
  111. // not yet in redux state and JitsiTrackEvents.TRACK_MUTE_CHANGED may be
  112. // fired before track gets to state.
  113. if (track.muted !== muted) {
  114. sendEvent(
  115. `synctrackstate.${track.mediaType}.${muted ? 'muted' : 'unmuted'}`);
  116. logger.log(`Sync ${track.mediaType} track muted state to ${
  117. muted ? 'muted' : 'unmuted'}`);
  118. track.muted = muted;
  119. setTrackMuted(track.jitsiTrack, muted);
  120. }
  121. }