選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

middleware.js 9.0KB

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