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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. // @flow
  2. import {
  3. createStartAudioOnlyEvent,
  4. createStartMutedConfigurationEvent,
  5. createSyncTrackStateEvent,
  6. sendAnalytics
  7. } from '../../analytics';
  8. import { isRoomValid, SET_ROOM, setAudioOnly } from '../conference';
  9. import JitsiMeetJS from '../lib-jitsi-meet';
  10. import { MiddlewareRegistry } from '../redux';
  11. import { getPropertyValue } from '../settings';
  12. import { setTrackMuted, TRACK_ADDED } from '../tracks';
  13. import { setAudioMuted, setCameraFacingMode, setVideoMuted } from './actions';
  14. import { CAMERA_FACING_MODE } from './constants';
  15. import {
  16. _AUDIO_INITIAL_MEDIA_STATE,
  17. _VIDEO_INITIAL_MEDIA_STATE
  18. } from './reducer';
  19. const logger = require('jitsi-meet-logger').getLogger(__filename);
  20. /**
  21. * Implements the entry point of the middleware of the feature base/media.
  22. *
  23. * @param {Store} store - The redux store.
  24. * @returns {Function}
  25. */
  26. MiddlewareRegistry.register(store => next => action => {
  27. switch (action.type) {
  28. case SET_ROOM:
  29. return _setRoom(store, next, action);
  30. case TRACK_ADDED: {
  31. const result = next(action);
  32. const { track } = action;
  33. track.local && _syncTrackMutedState(store, track);
  34. return result;
  35. }
  36. }
  37. return next(action);
  38. });
  39. /**
  40. * Notifies the feature base/media that the action {@link SET_ROOM} is being
  41. * dispatched within a specific redux {@code store}.
  42. *
  43. * @param {Store} store - The redux store in which the specified {@code action}
  44. * is being dispatched.
  45. * @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
  46. * specified {@code action} to the specified {@code store}.
  47. * @param {Action} action - The redux action, {@code SET_ROOM}, which is being
  48. * dispatched in the specified {@code store}.
  49. * @private
  50. * @returns {Object} The new state that is the result of the reduction of the
  51. * specified {@code action}.
  52. */
  53. function _setRoom({ dispatch, getState }, next, action) {
  54. // Figure out the desires/intents i.e. the state of base/media. There are
  55. // multiple desires/intents ordered by precedence such as server-side
  56. // config, config overrides in the user-supplied URL, user's own app
  57. // settings, etc.
  58. const state = getState();
  59. const { room } = action;
  60. const roomIsValid = isRoomValid(room);
  61. // XXX The configurations/preferences/settings startWithAudioMuted,
  62. // startWithVideoMuted, and startAudioOnly were introduced for
  63. // conferences/meetings. So it makes sense for these to not be considered
  64. // outside of conferences/meetings (e.g. WelcomePage). Later on, though, we
  65. // introduced a "Video <-> Voice" toggle on the WelcomePage which utilizes
  66. // startAudioOnly outside of conferences/meetings so that particular
  67. // configuration/preference/setting employs slightly exclusive logic.
  68. const mutedSources = {
  69. // We have startWithAudioMuted and startWithVideoMuted here:
  70. config: true,
  71. settings: true,
  72. // XXX We've already overwritten base/config with urlParams. However,
  73. // settings are more important than the server-side config.
  74. // Consequently, we need to read from urlParams anyway:
  75. urlParams: true,
  76. // We don't have startWithAudioMuted and startWithVideoMuted here:
  77. jwt: false
  78. };
  79. const audioMuted
  80. = roomIsValid
  81. ? Boolean(
  82. getPropertyValue(state, 'startWithAudioMuted', mutedSources))
  83. : _AUDIO_INITIAL_MEDIA_STATE.muted;
  84. const videoMuted
  85. = roomIsValid
  86. ? Boolean(
  87. getPropertyValue(state, 'startWithVideoMuted', mutedSources))
  88. : _VIDEO_INITIAL_MEDIA_STATE.muted;
  89. sendAnalytics(
  90. createStartMutedConfigurationEvent('local', audioMuted, videoMuted));
  91. logger.log(
  92. `Start muted: ${audioMuted ? 'audio, ' : ''}${
  93. videoMuted ? 'video' : ''}`);
  94. // Unconditionally express the desires/expectations/intents of the app and
  95. // the user i.e. the state of base/media. Eventually, practice/reality i.e.
  96. // the state of base/tracks will or will not agree with the desires.
  97. dispatch(setAudioMuted(audioMuted));
  98. dispatch(setCameraFacingMode(CAMERA_FACING_MODE.USER));
  99. dispatch(setVideoMuted(videoMuted));
  100. // startAudioOnly
  101. //
  102. // FIXME Technically, the audio-only feature is owned by base/conference,
  103. // not base/media so the following should be in base/conference.
  104. // Practically, I presume it was easier to write the source code here
  105. // because it looks like startWithAudioMuted and startWithVideoMuted.
  106. //
  107. // XXX After the introduction of the "Video <-> Voice" toggle on the
  108. // WelcomePage, startAudioOnly is utilized even outside of
  109. // conferences/meetings.
  110. let audioOnly;
  111. if (JitsiMeetJS.mediaDevices.supportsVideo()) {
  112. audioOnly
  113. = Boolean(
  114. getPropertyValue(
  115. state,
  116. 'startAudioOnly',
  117. /* sources */ {
  118. // FIXME Practically, base/config is (really) correct
  119. // only if roomIsValid. At the time of this writing,
  120. // base/config is overwritten by URL params which leaves
  121. // base/config incorrect on the WelcomePage after
  122. // leaving a conference which explicitly overwrites
  123. // base/config with URL params.
  124. config: roomIsValid,
  125. // XXX We've already overwritten base/config with
  126. // urlParams if roomIsValid. However, settings are more
  127. // important than the server-side config. Consequently,
  128. // we need to read from urlParams anyway. We also
  129. // probably want to read from urlParams when
  130. // !roomIsValid.
  131. urlParams: true,
  132. // The following don't have complications around whether
  133. // they are defined or not:
  134. jwt: false,
  135. settings: true
  136. }));
  137. } else {
  138. // Default to audio-only if the (execution) environment does not
  139. // support (sending and/or receiving) video.
  140. audioOnly = true;
  141. }
  142. sendAnalytics(createStartAudioOnlyEvent(audioOnly));
  143. logger.log(`Start audio only set to ${audioOnly.toString()}`);
  144. dispatch(setAudioOnly(audioOnly));
  145. return next(action);
  146. }
  147. /**
  148. * Syncs muted state of local media track with muted state from media state.
  149. *
  150. * @param {Store} store - The redux store.
  151. * @param {Track} track - The local media track.
  152. * @private
  153. * @returns {void}
  154. */
  155. function _syncTrackMutedState({ getState }, track) {
  156. const state = getState()['features/base/media'];
  157. const muted = Boolean(state[track.mediaType].muted);
  158. // XXX If muted state of track when it was added is different from our media
  159. // muted state, we need to mute track and explicitly modify 'muted' property
  160. // on track. This is because though TRACK_ADDED action was dispatched it's
  161. // not yet in redux state and JitsiTrackEvents.TRACK_MUTE_CHANGED may be
  162. // fired before track gets to state.
  163. if (track.muted !== muted) {
  164. sendAnalytics(createSyncTrackStateEvent(track.mediaType, muted));
  165. logger.log(
  166. `Sync ${track.mediaType} track muted state to ${
  167. muted ? 'muted' : 'unmuted'}`);
  168. track.muted = muted;
  169. setTrackMuted(track.jitsiTrack, muted);
  170. }
  171. }