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

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