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

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