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 12KB

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 { isRoomValid, SET_ROOM } 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. destroyLocalTracks,
  24. isLocalTrackMuted,
  25. isLocalVideoTrackDesktop,
  26. setTrackMuted,
  27. TRACK_ADDED
  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, MEDIA_TYPE.VIDEO, 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, ensureVideoTrack } = 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, MEDIA_TYPE.VIDEO, VIDEO_MUTISM_AUTHORITY.AUDIO_ONLY, ensureVideoTrack));
  170. if (getMultipleVideoSendingSupportFeatureFlag(state)) {
  171. dispatch(setScreenshareMuted(audioOnly, MEDIA_TYPE.SCREENSHARE, SCREENSHARE_MUTISM_AUTHORITY.AUDIO_ONLY));
  172. } else if (navigator.product !== 'ReactNative') {
  173. dispatch(setVideoMuted(audioOnly, MEDIA_TYPE.PRESENTER, VIDEO_MUTISM_AUTHORITY.AUDIO_ONLY, ensureVideoTrack));
  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 }, next, action) {
  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. const audioMuted = roomIsValid ? getStartWithAudioMuted(state) : _AUDIO_INITIAL_MEDIA_STATE.muted;
  200. const videoMuted = roomIsValid ? getStartWithVideoMuted(state) : _VIDEO_INITIAL_MEDIA_STATE.muted;
  201. sendAnalytics(
  202. createStartMutedConfigurationEvent('local', audioMuted, videoMuted));
  203. logger.log(
  204. `Start muted: ${audioMuted ? 'audio, ' : ''}${
  205. videoMuted ? 'video' : ''}`);
  206. // Unconditionally express the desires/expectations/intents of the app and
  207. // the user i.e. the state of base/media. Eventually, practice/reality i.e.
  208. // the state of base/tracks will or will not agree with the desires.
  209. dispatch(setAudioMuted(audioMuted));
  210. dispatch(setCameraFacingMode(CAMERA_FACING_MODE.USER));
  211. dispatch(setVideoMuted(videoMuted));
  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. settings: true
  246. }));
  247. sendAnalytics(createStartAudioOnlyEvent(audioOnly));
  248. logger.log(`Start audio only set to ${audioOnly.toString()}`);
  249. dispatch(setAudioOnly(audioOnly, false));
  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 === MEDIA_TYPE.PRESENTER
  266. ? MEDIA_TYPE.VIDEO : track.mediaType;
  267. const muted = Boolean(state[mediaType].muted);
  268. // XXX If muted state of track when it was added is different from our media
  269. // muted state, we need to mute track and explicitly modify 'muted' property
  270. // on track. This is because though TRACK_ADDED action was dispatched it's
  271. // not yet in redux state and JitsiTrackEvents.TRACK_MUTE_CHANGED may be
  272. // fired before track gets to state.
  273. if (track.muted !== muted) {
  274. sendAnalytics(createSyncTrackStateEvent(track.mediaType, muted));
  275. logger.log(`Sync ${track.mediaType} track muted state to ${muted ? 'muted' : 'unmuted'}`);
  276. track.muted = muted;
  277. setTrackMuted(track.jitsiTrack, muted, state);
  278. }
  279. }