您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

middleware.js 9.3KB

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