Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

middleware.js 9.4KB

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