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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. // @flow
  2. import {
  3. createStartAudioOnlyEvent,
  4. createStartMutedConfigurationEvent,
  5. createSyncTrackStateEvent,
  6. createTrackMutedEvent,
  7. sendAnalytics
  8. } from '../../analytics';
  9. import { APP_STATE_CHANGED } from '../../mobile/background';
  10. import { SET_AUDIO_ONLY, setAudioOnly } from '../audio-only';
  11. import { isRoomValid, SET_ROOM } from '../conference';
  12. import JitsiMeetJS from '../lib-jitsi-meet';
  13. import { MiddlewareRegistry } from '../redux';
  14. import { getPropertyValue } from '../settings';
  15. import { setTrackMuted, TRACK_ADDED } from '../tracks';
  16. import { setAudioMuted, setCameraFacingMode, setVideoMuted } from './actions';
  17. import {
  18. CAMERA_FACING_MODE,
  19. MEDIA_TYPE,
  20. VIDEO_MUTISM_AUTHORITY
  21. } from './constants';
  22. import logger from './logger';
  23. import {
  24. _AUDIO_INITIAL_MEDIA_STATE,
  25. _VIDEO_INITIAL_MEDIA_STATE
  26. } from './reducer';
  27. /**
  28. * Implements the entry point of the middleware of the feature base/media.
  29. *
  30. * @param {Store} store - The redux store.
  31. * @returns {Function}
  32. */
  33. MiddlewareRegistry.register(store => next => action => {
  34. switch (action.type) {
  35. case APP_STATE_CHANGED:
  36. return _appStateChanged(store, next, action);
  37. case SET_AUDIO_ONLY:
  38. return _setAudioOnly(store, next, action);
  39. case SET_ROOM:
  40. return _setRoom(store, next, action);
  41. case TRACK_ADDED: {
  42. const result = next(action);
  43. const { track } = action;
  44. // Don't sync track mute state with the redux store for screenshare
  45. // since video mute state represents local camera mute state only.
  46. track.local && track.videoType !== 'desktop'
  47. && _syncTrackMutedState(store, track);
  48. return result;
  49. }
  50. }
  51. return next(action);
  52. });
  53. /**
  54. * Adjusts the video muted state based on the app state.
  55. *
  56. * @param {Store} store - The redux store in which the specified {@code action}
  57. * is being dispatched.
  58. * @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
  59. * specified {@code action} to the specified {@code store}.
  60. * @param {Action} action - The redux action {@code APP_STATE_CHANGED} which is
  61. * being dispatched in the specified {@code store}.
  62. * @private
  63. * @returns {Object} The value returned by {@code next(action)}.
  64. */
  65. function _appStateChanged({ dispatch }, next, action) {
  66. const { appState } = action;
  67. const mute = appState !== 'active'; // Note that 'background' and 'inactive' are treated equal.
  68. sendAnalytics(createTrackMutedEvent('video', 'background mode', mute));
  69. dispatch(setVideoMuted(mute, MEDIA_TYPE.VIDEO, VIDEO_MUTISM_AUTHORITY.BACKGROUND));
  70. return next(action);
  71. }
  72. /**
  73. * Adjusts the video muted state based on the audio-only state.
  74. *
  75. * @param {Store} store - The redux store in which the specified {@code action}
  76. * is being dispatched.
  77. * @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
  78. * specified {@code action} to the specified {@code store}.
  79. * @param {Action} action - The redux action {@code SET_AUDIO_ONLY} which is
  80. * being dispatched in the specified {@code store}.
  81. * @private
  82. * @returns {Object} The value returned by {@code next(action)}.
  83. */
  84. function _setAudioOnly({ dispatch }, next, action) {
  85. const { audioOnly, ensureVideoTrack } = action;
  86. sendAnalytics(createTrackMutedEvent('video', 'audio-only mode', audioOnly));
  87. // Make sure we mute both the desktop and video tracks.
  88. dispatch(setVideoMuted(audioOnly, MEDIA_TYPE.VIDEO, VIDEO_MUTISM_AUTHORITY.AUDIO_ONLY, ensureVideoTrack));
  89. if (navigator.product !== 'ReactNative') {
  90. dispatch(setVideoMuted(audioOnly, MEDIA_TYPE.PRESENTER, VIDEO_MUTISM_AUTHORITY.AUDIO_ONLY, ensureVideoTrack));
  91. }
  92. return next(action);
  93. }
  94. /**
  95. * Notifies the feature base/media that the action {@link SET_ROOM} is being
  96. * dispatched within a specific redux {@code store}.
  97. *
  98. * @param {Store} store - The redux store in which the specified {@code action}
  99. * is being dispatched.
  100. * @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
  101. * specified {@code action} to the specified {@code store}.
  102. * @param {Action} action - The redux action, {@code SET_ROOM}, which is being
  103. * dispatched in the specified {@code store}.
  104. * @private
  105. * @returns {Object} The new state that is the result of the reduction of the
  106. * specified {@code action}.
  107. */
  108. function _setRoom({ dispatch, getState }, next, action) {
  109. // Figure out the desires/intents i.e. the state of base/media. There are
  110. // multiple desires/intents ordered by precedence such as server-side
  111. // config, config overrides in the user-supplied URL, user's own app
  112. // settings, etc.
  113. const state = getState();
  114. const { room } = action;
  115. const roomIsValid = isRoomValid(room);
  116. // XXX The configurations/preferences/settings startWithAudioMuted,
  117. // startWithVideoMuted, and startAudioOnly were introduced for
  118. // conferences/meetings. So it makes sense for these to not be considered
  119. // outside of conferences/meetings (e.g. WelcomePage). Later on, though, we
  120. // introduced a "Video <-> Voice" toggle on the WelcomePage which utilizes
  121. // startAudioOnly outside of conferences/meetings so that particular
  122. // configuration/preference/setting employs slightly exclusive logic.
  123. const mutedSources = {
  124. // We have startWithAudioMuted and startWithVideoMuted here:
  125. config: true,
  126. settings: true,
  127. // XXX We've already overwritten base/config with urlParams. However,
  128. // settings are more important than the server-side config.
  129. // Consequently, we need to read from urlParams anyway:
  130. urlParams: true,
  131. // We don't have startWithAudioMuted and startWithVideoMuted here:
  132. jwt: false
  133. };
  134. const audioMuted
  135. = roomIsValid
  136. ? Boolean(
  137. getPropertyValue(state, 'startWithAudioMuted', mutedSources))
  138. : _AUDIO_INITIAL_MEDIA_STATE.muted;
  139. const videoMuted
  140. = roomIsValid
  141. ? Boolean(
  142. getPropertyValue(state, 'startWithVideoMuted', mutedSources))
  143. : _VIDEO_INITIAL_MEDIA_STATE.muted;
  144. sendAnalytics(
  145. createStartMutedConfigurationEvent('local', audioMuted, videoMuted));
  146. logger.log(
  147. `Start muted: ${audioMuted ? 'audio, ' : ''}${
  148. videoMuted ? 'video' : ''}`);
  149. // Unconditionally express the desires/expectations/intents of the app and
  150. // the user i.e. the state of base/media. Eventually, practice/reality i.e.
  151. // the state of base/tracks will or will not agree with the desires.
  152. dispatch(setAudioMuted(audioMuted));
  153. dispatch(setCameraFacingMode(CAMERA_FACING_MODE.USER));
  154. dispatch(setVideoMuted(videoMuted));
  155. // startAudioOnly
  156. //
  157. // FIXME Technically, the audio-only feature is owned by base/conference,
  158. // not base/media so the following should be in base/conference.
  159. // Practically, I presume it was easier to write the source code here
  160. // because it looks like startWithAudioMuted and startWithVideoMuted.
  161. //
  162. // XXX After the introduction of the "Video <-> Voice" toggle on the
  163. // WelcomePage, startAudioOnly is utilized even outside of
  164. // conferences/meetings.
  165. let audioOnly;
  166. if (JitsiMeetJS.mediaDevices.supportsVideo()) {
  167. audioOnly
  168. = Boolean(
  169. getPropertyValue(
  170. state,
  171. 'startAudioOnly',
  172. /* sources */ {
  173. // FIXME Practically, base/config is (really) correct
  174. // only if roomIsValid. At the time of this writing,
  175. // base/config is overwritten by URL params which leaves
  176. // base/config incorrect on the WelcomePage after
  177. // leaving a conference which explicitly overwrites
  178. // base/config with URL params.
  179. config: roomIsValid,
  180. // XXX We've already overwritten base/config with
  181. // urlParams if roomIsValid. However, settings are more
  182. // important than the server-side config. Consequently,
  183. // we need to read from urlParams anyway. We also
  184. // probably want to read from urlParams when
  185. // !roomIsValid.
  186. urlParams: true,
  187. // The following don't have complications around whether
  188. // they are defined or not:
  189. jwt: false,
  190. settings: true
  191. }));
  192. } else {
  193. // Default to audio-only if the (execution) environment does not
  194. // support (sending and/or receiving) video.
  195. audioOnly = true;
  196. }
  197. sendAnalytics(createStartAudioOnlyEvent(audioOnly));
  198. logger.log(`Start audio only set to ${audioOnly.toString()}`);
  199. dispatch(setAudioOnly(audioOnly, false));
  200. return next(action);
  201. }
  202. /**
  203. * Syncs muted state of local media track with muted state from media state.
  204. *
  205. * @param {Store} store - The redux store.
  206. * @param {Track} track - The local media track.
  207. * @private
  208. * @returns {void}
  209. */
  210. function _syncTrackMutedState({ getState }, track) {
  211. const state = getState()['features/base/media'];
  212. const mediaType = track.mediaType === MEDIA_TYPE.PRESENTER
  213. ? MEDIA_TYPE.VIDEO : track.mediaType;
  214. const muted = Boolean(state[mediaType].muted);
  215. // XXX If muted state of track when it was added is different from our media
  216. // muted state, we need to mute track and explicitly modify 'muted' property
  217. // on track. This is because though TRACK_ADDED action was dispatched it's
  218. // not yet in redux state and JitsiTrackEvents.TRACK_MUTE_CHANGED may be
  219. // fired before track gets to state.
  220. if (track.muted !== muted) {
  221. sendAnalytics(createSyncTrackStateEvent(track.mediaType, muted));
  222. logger.log(
  223. `Sync ${track.mediaType} track muted state to ${
  224. muted ? 'muted' : 'unmuted'}`);
  225. track.muted = muted;
  226. setTrackMuted(track.jitsiTrack, muted);
  227. }
  228. }