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.js 11KB

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