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

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