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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /* @flow */
  2. import {
  3. createStartAudioOnlyEvent,
  4. createStartMutedConfigurationEvent,
  5. createSyncTrackStateEvent,
  6. sendAnalytics
  7. } from '../../analytics';
  8. import { SET_ROOM, setAudioOnly } from '../conference';
  9. import JitsiMeetJS from '../lib-jitsi-meet';
  10. import { getPropertyValue } from '../profile';
  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. const audioMuted = Boolean(getPropertyValue(state, 'startWithAudioMuted', {
  53. ignoreUrlParams: !room
  54. }));
  55. const videoMuted = Boolean(getPropertyValue(state, 'startWithVideoMuted', {
  56. ignoreUrlParams: !room
  57. }));
  58. sendAnalytics(createStartMutedConfigurationEvent(
  59. 'local', audioMuted, videoMuted));
  60. logger.log(`Start muted: ${audioMuted ? 'audio, ' : ''}${
  61. videoMuted ? 'video' : ''}`);
  62. // Unconditionally express the desires/expectations/intents of the app and
  63. // the user i.e. the state of base/media. Eventually, practice/reality i.e.
  64. // the state of base/tracks will or will not agree with the desires.
  65. dispatch(setAudioMuted(audioMuted));
  66. dispatch(setCameraFacingMode(CAMERA_FACING_MODE.USER));
  67. dispatch(setVideoMuted(videoMuted));
  68. // config.startAudioOnly
  69. //
  70. // FIXME Technically, the audio-only feature is owned by base/conference,
  71. // not base/media so the following should be in base/conference.
  72. // Practically, I presume it was easier to write the source code here
  73. // because it looks like config.startWithAudioMuted and
  74. // config.startWithVideoMuted.
  75. if (room) {
  76. let audioOnly;
  77. if (JitsiMeetJS.mediaDevices.supportsVideo()) {
  78. audioOnly = Boolean(getPropertyValue(state, 'startAudioOnly'));
  79. } else {
  80. // Always default to being audio only if the current environment
  81. // does not support sending or receiving video.
  82. audioOnly = true;
  83. }
  84. sendAnalytics(createStartAudioOnlyEvent(audioOnly));
  85. logger.log(`Start audio only set to ${audioOnly.toString()}`);
  86. dispatch(setAudioOnly(audioOnly));
  87. }
  88. return next(action);
  89. }
  90. /**
  91. * Syncs muted state of local media track with muted state from media state.
  92. *
  93. * @param {Store} store - The redux store.
  94. * @param {Track} track - The local media track.
  95. * @private
  96. * @returns {void}
  97. */
  98. function _syncTrackMutedState({ getState }, track) {
  99. const state = getState()['features/base/media'];
  100. const muted = Boolean(state[track.mediaType].muted);
  101. // XXX If muted state of track when it was added is different from our media
  102. // muted state, we need to mute track and explicitly modify 'muted' property
  103. // on track. This is because though TRACK_ADDED action was dispatched it's
  104. // not yet in redux state and JitsiTrackEvents.TRACK_MUTE_CHANGED may be
  105. // fired before track gets to state.
  106. if (track.muted !== muted) {
  107. sendAnalytics(createSyncTrackStateEvent(track.mediaType, muted));
  108. logger.log(`Sync ${track.mediaType} track muted state to ${
  109. muted ? 'muted' : 'unmuted'}`);
  110. track.muted = muted;
  111. setTrackMuted(track.jitsiTrack, muted);
  112. }
  113. }