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

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