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.any.ts 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. import { AnyAction } from 'redux';
  2. import {
  3. createStartAudioOnlyEvent,
  4. createStartMutedConfigurationEvent,
  5. createSyncTrackStateEvent,
  6. createTrackMutedEvent
  7. } from '../../analytics/AnalyticsEvents';
  8. import { sendAnalytics } from '../../analytics/functions';
  9. import { IStore } from '../../app/types';
  10. import { APP_STATE_CHANGED } from '../../mobile/background/actionTypes';
  11. import { showWarningNotification } from '../../notifications/actions';
  12. import { NOTIFICATION_TIMEOUT_TYPE } from '../../notifications/constants';
  13. import { isForceMuted } from '../../participants-pane/functions';
  14. import { isScreenMediaShared } from '../../screen-share/functions';
  15. import { SET_AUDIO_ONLY } from '../audio-only/actionTypes';
  16. import { setAudioOnly } from '../audio-only/actions';
  17. import { SET_ROOM } from '../conference/actionTypes';
  18. import { isRoomValid } from '../conference/functions';
  19. import { getMultipleVideoSendingSupportFeatureFlag } from '../config/functions.any';
  20. import { getLocalParticipant } from '../participants/functions';
  21. import MiddlewareRegistry from '../redux/MiddlewareRegistry';
  22. import { getPropertyValue } from '../settings/functions.any';
  23. import { TRACK_ADDED } from '../tracks/actionTypes';
  24. import { destroyLocalTracks } from '../tracks/actions.any';
  25. import {
  26. getCameraFacingMode,
  27. isLocalTrackMuted,
  28. isLocalVideoTrackDesktop,
  29. setTrackMuted
  30. } from '../tracks/functions.any';
  31. import { ITrack } from '../tracks/types';
  32. import {
  33. SET_AUDIO_MUTED,
  34. SET_AUDIO_UNMUTE_PERMISSIONS,
  35. SET_SCREENSHARE_MUTED,
  36. SET_VIDEO_MUTED,
  37. SET_VIDEO_UNMUTE_PERMISSIONS
  38. } from './actionTypes';
  39. import {
  40. setAudioMuted,
  41. setCameraFacingMode,
  42. setScreenshareMuted,
  43. setVideoMuted
  44. } from './actions';
  45. import {
  46. MEDIA_TYPE,
  47. SCREENSHARE_MUTISM_AUTHORITY,
  48. VIDEO_MUTISM_AUTHORITY
  49. } from './constants';
  50. import { getStartWithAudioMuted, getStartWithVideoMuted } from './functions';
  51. import logger from './logger';
  52. import {
  53. _AUDIO_INITIAL_MEDIA_STATE,
  54. _VIDEO_INITIAL_MEDIA_STATE
  55. } from './reducer';
  56. /**
  57. * Implements the entry point of the middleware of the feature base/media.
  58. *
  59. * @param {Store} store - The redux store.
  60. * @returns {Function}
  61. */
  62. MiddlewareRegistry.register(store => next => action => {
  63. switch (action.type) {
  64. case APP_STATE_CHANGED:
  65. return _appStateChanged(store, next, action);
  66. case SET_AUDIO_ONLY:
  67. return _setAudioOnly(store, next, action);
  68. case SET_ROOM:
  69. return _setRoom(store, next, action);
  70. case TRACK_ADDED: {
  71. const result = next(action);
  72. const { track } = action;
  73. // Don't sync track mute state with the redux store for screenshare
  74. // since video mute state represents local camera mute state only.
  75. track.local && track.videoType !== 'desktop'
  76. && _syncTrackMutedState(store, track);
  77. return result;
  78. }
  79. case SET_AUDIO_MUTED: {
  80. const state = store.getState();
  81. const participant = getLocalParticipant(state);
  82. if (!action.muted && isForceMuted(participant, MEDIA_TYPE.AUDIO, state)) {
  83. return;
  84. }
  85. break;
  86. }
  87. case SET_AUDIO_UNMUTE_PERMISSIONS: {
  88. const { blocked, skipNotification } = action;
  89. const state = store.getState();
  90. const tracks = state['features/base/tracks'];
  91. const isAudioMuted = isLocalTrackMuted(tracks, MEDIA_TYPE.AUDIO);
  92. if (blocked && isAudioMuted && !skipNotification) {
  93. store.dispatch(showWarningNotification({
  94. descriptionKey: 'notify.audioUnmuteBlockedDescription',
  95. titleKey: 'notify.audioUnmuteBlockedTitle'
  96. }, NOTIFICATION_TIMEOUT_TYPE.MEDIUM));
  97. }
  98. break;
  99. }
  100. case SET_SCREENSHARE_MUTED: {
  101. const state = store.getState();
  102. const participant = getLocalParticipant(state);
  103. if (!action.muted && isForceMuted(participant, MEDIA_TYPE.SCREENSHARE, state)) {
  104. return;
  105. }
  106. break;
  107. }
  108. case SET_VIDEO_MUTED: {
  109. const state = store.getState();
  110. const participant = getLocalParticipant(state);
  111. if (!action.muted && isForceMuted(participant, MEDIA_TYPE.VIDEO, state)) {
  112. return;
  113. }
  114. break;
  115. }
  116. case SET_VIDEO_UNMUTE_PERMISSIONS: {
  117. const { blocked, skipNotification } = action;
  118. const state = store.getState();
  119. const tracks = state['features/base/tracks'];
  120. const isVideoMuted = isLocalTrackMuted(tracks, MEDIA_TYPE.VIDEO);
  121. const isMediaShared = isScreenMediaShared(state);
  122. if (blocked && isVideoMuted && !isMediaShared && !skipNotification) {
  123. store.dispatch(showWarningNotification({
  124. descriptionKey: 'notify.videoUnmuteBlockedDescription',
  125. titleKey: 'notify.videoUnmuteBlockedTitle'
  126. }, NOTIFICATION_TIMEOUT_TYPE.MEDIUM));
  127. }
  128. break;
  129. }
  130. }
  131. return next(action);
  132. });
  133. /**
  134. * Adjusts the video muted state based on the app state.
  135. *
  136. * @param {Store} store - The redux store in which the specified {@code action}
  137. * is being dispatched.
  138. * @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
  139. * specified {@code action} to the specified {@code store}.
  140. * @param {Action} action - The redux action {@code APP_STATE_CHANGED} which is
  141. * being dispatched in the specified {@code store}.
  142. * @private
  143. * @returns {Object} The value returned by {@code next(action)}.
  144. */
  145. function _appStateChanged({ dispatch, getState }: IStore, next: Function, action: AnyAction) {
  146. if (navigator.product === 'ReactNative') {
  147. const { appState } = action;
  148. const mute = appState !== 'active' && !isLocalVideoTrackDesktop(getState());
  149. sendAnalytics(createTrackMutedEvent('video', 'background mode', mute));
  150. dispatch(setVideoMuted(mute, VIDEO_MUTISM_AUTHORITY.BACKGROUND));
  151. }
  152. return next(action);
  153. }
  154. /**
  155. * Adjusts the video muted state based on the audio-only state.
  156. *
  157. * @param {Store} store - The redux store in which the specified {@code action}
  158. * is being dispatched.
  159. * @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
  160. * specified {@code action} to the specified {@code store}.
  161. * @param {Action} action - The redux action {@code SET_AUDIO_ONLY} which is
  162. * being dispatched in the specified {@code store}.
  163. * @private
  164. * @returns {Object} The value returned by {@code next(action)}.
  165. */
  166. function _setAudioOnly({ dispatch, getState }: IStore, next: Function, action: AnyAction) {
  167. const { audioOnly } = action;
  168. const state = getState();
  169. sendAnalytics(createTrackMutedEvent('video', 'audio-only mode', audioOnly));
  170. // Make sure we mute both the desktop and video tracks.
  171. dispatch(setVideoMuted(audioOnly, VIDEO_MUTISM_AUTHORITY.AUDIO_ONLY));
  172. if (getMultipleVideoSendingSupportFeatureFlag(state)) {
  173. dispatch(setScreenshareMuted(audioOnly, SCREENSHARE_MUTISM_AUTHORITY.AUDIO_ONLY));
  174. }
  175. return next(action);
  176. }
  177. /**
  178. * Notifies the feature base/media that the action {@link SET_ROOM} is being
  179. * dispatched within a specific redux {@code store}.
  180. *
  181. * @param {Store} store - The redux store in which the specified {@code action}
  182. * is being dispatched.
  183. * @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
  184. * specified {@code action} to the specified {@code store}.
  185. * @param {Action} action - The redux action, {@code SET_ROOM}, which is being
  186. * dispatched in the specified {@code store}.
  187. * @private
  188. * @returns {Object} The new state that is the result of the reduction of the
  189. * specified {@code action}.
  190. */
  191. function _setRoom({ dispatch, getState }: IStore, next: Function, action: AnyAction) {
  192. // Figure out the desires/intents i.e. the state of base/media. There are
  193. // multiple desires/intents ordered by precedence such as server-side
  194. // config, config overrides in the user-supplied URL, user's own app
  195. // settings, etc.
  196. const state = getState();
  197. const { room } = action;
  198. const roomIsValid = isRoomValid(room);
  199. // when going to welcomepage on web(room is not valid) we want to skip resetting the values of startWithA/V
  200. if (roomIsValid || navigator.product === 'ReactNative') {
  201. const audioMuted = roomIsValid ? getStartWithAudioMuted(state) : _AUDIO_INITIAL_MEDIA_STATE.muted;
  202. const videoMuted = roomIsValid ? getStartWithVideoMuted(state) : _VIDEO_INITIAL_MEDIA_STATE.muted;
  203. sendAnalytics(createStartMutedConfigurationEvent('local', audioMuted, Boolean(videoMuted)));
  204. logger.log(`Start muted: ${audioMuted ? 'audio, ' : ''}${videoMuted ? 'video' : ''}`);
  205. // Unconditionally express the desires/expectations/intents of the app and
  206. // the user i.e. the state of base/media. Eventually, practice/reality i.e.
  207. // the state of base/tracks will or will not agree with the desires.
  208. dispatch(setAudioMuted(audioMuted));
  209. dispatch(setCameraFacingMode(getCameraFacingMode(state)));
  210. dispatch(setVideoMuted(videoMuted));
  211. }
  212. // startAudioOnly
  213. //
  214. // FIXME Technically, the audio-only feature is owned by base/conference,
  215. // not base/media so the following should be in base/conference.
  216. // Practically, I presume it was easier to write the source code here
  217. // because it looks like startWithAudioMuted and startWithVideoMuted.
  218. //
  219. // XXX After the introduction of the "Video <-> Voice" toggle on the
  220. // WelcomePage, startAudioOnly is utilized even outside of
  221. // conferences/meetings.
  222. const audioOnly
  223. = Boolean(
  224. getPropertyValue(
  225. state,
  226. 'startAudioOnly',
  227. /* sources */ {
  228. // FIXME Practically, base/config is (really) correct
  229. // only if roomIsValid. At the time of this writing,
  230. // base/config is overwritten by URL params which leaves
  231. // base/config incorrect on the WelcomePage after
  232. // leaving a conference which explicitly overwrites
  233. // base/config with URL params.
  234. config: roomIsValid,
  235. // XXX We've already overwritten base/config with
  236. // urlParams if roomIsValid. However, settings are more
  237. // important than the server-side config. Consequently,
  238. // we need to read from urlParams anyway. We also
  239. // probably want to read from urlParams when
  240. // !roomIsValid.
  241. urlParams: true,
  242. // The following don't have complications around whether
  243. // they are defined or not:
  244. jwt: false,
  245. // We need to look for 'startAudioOnly' in settings only for react native clients. Otherwise, the
  246. // default value from ISettingsState (false) will override the value set in config for web clients.
  247. settings: typeof APP === 'undefined'
  248. }));
  249. sendAnalytics(createStartAudioOnlyEvent(audioOnly));
  250. logger.log(`Start audio only set to ${audioOnly.toString()}`);
  251. dispatch(setAudioOnly(audioOnly));
  252. if (!roomIsValid) {
  253. dispatch(destroyLocalTracks());
  254. }
  255. return next(action);
  256. }
  257. /**
  258. * Syncs muted state of local media track with muted state from media state.
  259. *
  260. * @param {Store} store - The redux store.
  261. * @param {Track} track - The local media track.
  262. * @private
  263. * @returns {void}
  264. */
  265. function _syncTrackMutedState({ getState, dispatch }: IStore, track: ITrack) {
  266. const state = getState()['features/base/media'];
  267. const mediaType = track.mediaType;
  268. const muted = Boolean(state[mediaType].muted);
  269. // XXX If muted state of track when it was added is different from our media
  270. // muted state, we need to mute track and explicitly modify 'muted' property
  271. // on track. This is because though TRACK_ADDED action was dispatched it's
  272. // not yet in redux state and JitsiTrackEvents.TRACK_MUTE_CHANGED may be
  273. // fired before track gets to state.
  274. if (track.muted !== muted) {
  275. sendAnalytics(createSyncTrackStateEvent(mediaType, muted));
  276. logger.log(`Sync ${mediaType} track muted state to ${muted ? 'muted' : 'unmuted'}`);
  277. track.muted = muted;
  278. setTrackMuted(track.jitsiTrack, muted, state, dispatch);
  279. }
  280. }