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

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