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

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