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

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