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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. // @flow
  2. import { getLogger } from 'jitsi-meet-logger';
  3. import { SET_FILMSTRIP_ENABLED } from '../../filmstrip/actionTypes';
  4. import { APP_STATE_CHANGED } from '../../mobile/background/actionTypes';
  5. import { SET_AUDIO_ONLY } from '../audio-only';
  6. import { CONFERENCE_JOINED } from '../conference/actionTypes';
  7. import { MiddlewareRegistry } from '../redux';
  8. import { setLastN } from './actions';
  9. import { SET_LASTN } from './actionTypes';
  10. declare var APP: Object;
  11. const logger = getLogger('features/base/lastn');
  12. MiddlewareRegistry.register(store => next => action => {
  13. switch (action.type) {
  14. case APP_STATE_CHANGED:
  15. return _appStateChanged(store, next, action);
  16. case CONFERENCE_JOINED:
  17. return _conferenceJoined(store, next, action);
  18. case SET_AUDIO_ONLY:
  19. return _setAudioOnly(store, next, action);
  20. case SET_FILMSTRIP_ENABLED:
  21. return _setFilmstripEnabled(store, next, action);
  22. case SET_LASTN:
  23. return _setLastN(store, next, action);
  24. }
  25. return next(action);
  26. });
  27. /**
  28. * Adjusts the lasN value based on the app state.
  29. *
  30. * @param {Store} store - The redux store in which the specified {@code action}
  31. * is being dispatched.
  32. * @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
  33. * specified {@code action} to the specified {@code store}.
  34. * @param {Action} action - The redux action {@code APP_STATE_CHANGED} which is
  35. * being dispatched in the specified {@code store}.
  36. * @private
  37. * @returns {Object} The value returned by {@code next(action)}.
  38. */
  39. function _appStateChanged({ dispatch, getState }, next, action) {
  40. const { enabled: audioOnly } = getState()['features/base/audio-only'];
  41. if (!audioOnly) {
  42. const { appState } = action;
  43. const lastN = appState === 'active' ? undefined : 0;
  44. dispatch(setLastN(lastN));
  45. logger.log(`App state changed - updated lastN to ${String(lastN)}`);
  46. }
  47. return next(action);
  48. }
  49. /**
  50. * Adjusts the lasN value upon joining a conference.
  51. *
  52. * @param {Store} store - The redux store in which the specified {@code action}
  53. * is being dispatched.
  54. * @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
  55. * specified {@code action} to the specified {@code store}.
  56. * @param {Action} action - The redux action {@code CONFERENCE_JOINED} which is
  57. * being dispatched in the specified {@code store}.
  58. * @private
  59. * @returns {Object} The value returned by {@code next(action)}.
  60. */
  61. function _conferenceJoined({ dispatch, getState }, next, action) {
  62. const { conference } = action;
  63. const { enabled: audioOnly } = getState()['features/base/audio-only'];
  64. audioOnly && conference.getLastN() !== 0 && dispatch(setLastN(0));
  65. return next(action);
  66. }
  67. /**
  68. * Sets the audio-only flag for the current conference. When audio-only is set,
  69. * local video is muted and last N is set to 0 to avoid receiving remote video.
  70. *
  71. * @param {Store} store - The redux store in which the specified {@code action}
  72. * is being dispatched.
  73. * @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
  74. * specified {@code action} to the specified {@code store}.
  75. * @param {Action} action - The redux action {@code SET_AUDIO_ONLY} which is
  76. * being dispatched in the specified {@code store}.
  77. * @private
  78. * @returns {Object} The value returned by {@code next(action)}.
  79. */
  80. function _setAudioOnly({ dispatch }, next, action) {
  81. const { audioOnly } = action;
  82. // Set lastN to 0 in case audio-only is desired; leave it as undefined,
  83. // otherwise, and the default lastN value will be chosen automatically.
  84. dispatch(setLastN(audioOnly ? 0 : undefined));
  85. return next(action);
  86. }
  87. /**
  88. * Notifies the feature filmstrip that the action {@link SET_FILMSTRIP_ENABLED}
  89. * is being dispatched within a specific redux store.
  90. *
  91. * @param {Store} store - The redux store in which the specified action is being
  92. * dispatched.
  93. * @param {Dispatch} next - The redux dispatch function to dispatch the
  94. * specified action to the specified store.
  95. * @param {Action} action - The redux action {@code SET_FILMSTRIP_ENABLED} which
  96. * is being dispatched in the specified store.
  97. * @private
  98. * @returns {Object} The value returned by {@code next(action)}.
  99. */
  100. function _setFilmstripEnabled({ dispatch, getState }, next, action) {
  101. // FIXME This action is not currently dispatched on web.
  102. if (typeof APP === 'undefined') {
  103. const { enabled } = action;
  104. const { enabled: audioOnly } = getState()['features/base/audio-only'];
  105. audioOnly || dispatch(setLastN(enabled ? undefined : 1));
  106. }
  107. return next(action);
  108. }
  109. /**
  110. * Sets the last N (value) of the video channel in the conference.
  111. *
  112. * @param {Store} store - The redux store in which the specified {@code action}
  113. * is being dispatched.
  114. * @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
  115. * specified {@code action} to the specified {@code store}.
  116. * @param {Action} action - The redux action {@code SET_LASTN} which is being
  117. * dispatched in the specified {@code store}.
  118. * @private
  119. * @returns {Object} The value returned by {@code next(action)}.
  120. */
  121. function _setLastN({ getState }, next, action) {
  122. const { conference } = getState()['features/base/conference'];
  123. if (conference) {
  124. try {
  125. conference.setLastN(action.lastN);
  126. } catch (err) {
  127. logger.error(`Failed to set lastN: ${err}`);
  128. }
  129. }
  130. return next(action);
  131. }