Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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