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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /* @flow */
  2. import {
  3. createStartAudioOnlyEvent,
  4. createStartMutedConfigurationEvent,
  5. createSyncTrackStateEvent,
  6. sendAnalytics
  7. } from '../../analytics';
  8. import { SET_ROOM, setAudioOnly } from '../conference';
  9. import { parseURLParams } from '../config';
  10. import JitsiMeetJS from '../lib-jitsi-meet';
  11. import { MiddlewareRegistry } from '../redux';
  12. import { setTrackMuted, TRACK_ADDED } from '../tracks';
  13. import { setAudioMuted, setCameraFacingMode, setVideoMuted } from './actions';
  14. import { CAMERA_FACING_MODE } from './constants';
  15. const logger = require('jitsi-meet-logger').getLogger(__filename);
  16. /**
  17. * Implements the entry point of the middleware of the feature base/media.
  18. *
  19. * @param {Store} store - The redux store.
  20. * @returns {Function}
  21. */
  22. MiddlewareRegistry.register(store => next => action => {
  23. switch (action.type) {
  24. case SET_ROOM:
  25. return _setRoom(store, next, action);
  26. case TRACK_ADDED: {
  27. const result = next(action);
  28. action.track.local && _syncTrackMutedState(store, action.track);
  29. return result;
  30. }
  31. }
  32. return next(action);
  33. });
  34. /**
  35. * Notifies the feature base/media that the action {@link SET_ROOM} is being
  36. * dispatched within a specific redux {@code store}.
  37. *
  38. * @param {Store} store - The redux store in which the specified {@code action}
  39. * is being dispatched.
  40. * @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
  41. * specified {@code action} to the specified {@code store}.
  42. * @param {Action} action - The redux action, {@code SET_ROOM}, which is being
  43. * dispatched in the specified {@code store}.
  44. * @private
  45. * @returns {Object} The new state that is the result of the reduction of the
  46. * specified {@code action}.
  47. */
  48. function _setRoom({ dispatch, getState }, next, action) {
  49. const { room } = action;
  50. // Read the config.
  51. const state = getState();
  52. let urlParams;
  53. let audioMuted;
  54. let videoMuted;
  55. if (room) {
  56. // The Jitsi Meet client may override the Jitsi Meet deployment in the
  57. // (location) URL on the subject of the following:
  58. // - startAudioOnly
  59. // - startWithAudioMuted
  60. // - startWithVideoMuted
  61. urlParams
  62. = parseURLParams(state['features/base/connection'].locationURL);
  63. audioMuted = urlParams['config.startWithAudioMuted'];
  64. videoMuted = urlParams['config.startWithVideoMuted'];
  65. }
  66. // Of course, the Jitsi Meet deployment defines config.js which should be
  67. // respected if the client did not override it.
  68. const config = state['features/base/config'];
  69. typeof audioMuted === 'undefined'
  70. && (audioMuted = config.startWithAudioMuted);
  71. typeof videoMuted === 'undefined'
  72. && (videoMuted = config.startWithVideoMuted);
  73. audioMuted = Boolean(audioMuted);
  74. videoMuted = Boolean(videoMuted);
  75. sendAnalytics(createStartMutedConfigurationEvent(
  76. 'local', audioMuted, videoMuted));
  77. logger.log(`Start muted: ${audioMuted ? 'audio, ' : ''}${
  78. videoMuted ? 'video' : ''}`);
  79. // Unconditionally express the desires/expectations/intents of the app and
  80. // the user i.e. the state of base/media. Eventually, practice/reality i.e.
  81. // the state of base/tracks will or will not agree with the desires.
  82. dispatch(setAudioMuted(audioMuted));
  83. dispatch(setCameraFacingMode(CAMERA_FACING_MODE.USER));
  84. dispatch(setVideoMuted(videoMuted));
  85. // config.startAudioOnly
  86. //
  87. // FIXME Technically, the audio-only feature is owned by base/conference,
  88. // not base/media so the following should be in base/conference.
  89. // Practically, I presume it was easier to write the source code here
  90. // because it looks like config.startWithAudioMuted and
  91. // config.startWithVideoMuted.
  92. if (room) {
  93. let audioOnly;
  94. if (JitsiMeetJS.mediaDevices.supportsVideo()) {
  95. audioOnly = urlParams && urlParams['config.startAudioOnly'];
  96. typeof audioOnly === 'undefined'
  97. && (audioOnly = config.startAudioOnly);
  98. audioOnly = Boolean(audioOnly);
  99. } else {
  100. // Always default to being audio only if the current environment
  101. // does not support sending or receiving video.
  102. audioOnly = true;
  103. }
  104. sendAnalytics(createStartAudioOnlyEvent(audioOnly));
  105. logger.log(`Start audio only set to ${audioOnly.toString()}`);
  106. dispatch(setAudioOnly(audioOnly));
  107. }
  108. return next(action);
  109. }
  110. /**
  111. * Syncs muted state of local media track with muted state from media state.
  112. *
  113. * @param {Store} store - The redux store.
  114. * @param {Track} track - The local media track.
  115. * @private
  116. * @returns {void}
  117. */
  118. function _syncTrackMutedState({ getState }, track) {
  119. const state = getState()['features/base/media'];
  120. const muted = Boolean(state[track.mediaType].muted);
  121. // XXX If muted state of track when it was added is different from our media
  122. // muted state, we need to mute track and explicitly modify 'muted' property
  123. // on track. This is because though TRACK_ADDED action was dispatched it's
  124. // not yet in redux state and JitsiTrackEvents.TRACK_MUTE_CHANGED may be
  125. // fired before track gets to state.
  126. if (track.muted !== muted) {
  127. sendAnalytics(createSyncTrackStateEvent(track.mediaType, muted));
  128. logger.log(`Sync ${track.mediaType} track muted state to ${
  129. muted ? 'muted' : 'unmuted'}`);
  130. track.muted = muted;
  131. setTrackMuted(track.jitsiTrack, muted);
  132. }
  133. }