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.ts 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import debounce from 'lodash/debounce';
  2. import { IStore } from '../../app/types';
  3. import { SET_FILMSTRIP_ENABLED } from '../../filmstrip/actionTypes';
  4. import { APP_STATE_CHANGED } from '../../mobile/background/actionTypes';
  5. import {
  6. SET_CAR_MODE,
  7. VIRTUAL_SCREENSHARE_REMOTE_PARTICIPANTS_UPDATED
  8. } from '../../video-layout/actionTypes';
  9. import { SET_AUDIO_ONLY } from '../audio-only/actionTypes';
  10. import { CONFERENCE_JOINED } from '../conference/actionTypes';
  11. import { getParticipantById } from '../participants/functions';
  12. import MiddlewareRegistry from '../redux/MiddlewareRegistry';
  13. import { isLocalVideoTrackDesktop } from '../tracks/functions';
  14. import { setLastN } from './actions';
  15. import logger from './logger';
  16. /**
  17. * Updates the last N value in the conference based on the current state of the redux store.
  18. *
  19. * @param {Store} store - The redux store.
  20. * @private
  21. * @returns {void}
  22. */
  23. const _updateLastN = debounce(({ dispatch, getState }: IStore) => {
  24. const state = getState();
  25. const { conference } = state['features/base/conference'];
  26. if (!conference) {
  27. logger.debug('There is no active conference, not updating last N');
  28. return;
  29. }
  30. const { enabled: audioOnly } = state['features/base/audio-only'];
  31. const { appState } = state['features/background'] || {};
  32. const { enabled: filmStripEnabled } = state['features/filmstrip'];
  33. const config = state['features/base/config'];
  34. const { carMode } = state['features/video-layout'];
  35. // Select the (initial) lastN value based on the following preference order.
  36. // 1. The last-n value from 'startLastN' if it is specified in config.js
  37. // 2. The last-n value from 'channelLastN' if specified in config.js.
  38. // 3. -1 as the default value.
  39. let lastNSelected = config.startLastN ?? (config.channelLastN ?? -1);
  40. if (typeof appState !== 'undefined' && appState !== 'active') {
  41. lastNSelected = isLocalVideoTrackDesktop(state) ? 1 : 0;
  42. } else if (carMode) {
  43. lastNSelected = 0;
  44. } else if (audioOnly) {
  45. const { remoteScreenShares, tileViewEnabled } = state['features/video-layout'];
  46. const largeVideoParticipantId = state['features/large-video'].participantId;
  47. const largeVideoParticipant
  48. = largeVideoParticipantId ? getParticipantById(state, largeVideoParticipantId) : undefined;
  49. // Use tileViewEnabled state from redux here instead of determining if client should be in tile
  50. // view since we make an exception only for screenshare when in audio-only mode. If the user unpins
  51. // the screenshare, lastN will be set to 0 here. It will be set to 1 if screenshare has been auto pinned.
  52. if (!tileViewEnabled && largeVideoParticipant && !largeVideoParticipant.local) {
  53. lastNSelected = (remoteScreenShares || []).includes(largeVideoParticipantId ?? '') ? 1 : 0;
  54. } else {
  55. lastNSelected = 0;
  56. }
  57. } else if (!filmStripEnabled) {
  58. lastNSelected = 1;
  59. }
  60. const { lastN } = state['features/base/lastn'];
  61. if (lastN !== lastNSelected) {
  62. dispatch(setLastN(lastNSelected));
  63. }
  64. }, 1000); /* Don't send this more often than once a second. */
  65. MiddlewareRegistry.register(store => next => action => {
  66. const result = next(action);
  67. switch (action.type) {
  68. case APP_STATE_CHANGED:
  69. case CONFERENCE_JOINED:
  70. case SET_AUDIO_ONLY:
  71. case SET_CAR_MODE:
  72. case SET_FILMSTRIP_ENABLED:
  73. case VIRTUAL_SCREENSHARE_REMOTE_PARTICIPANTS_UPDATED:
  74. _updateLastN(store);
  75. break;
  76. }
  77. return result;
  78. });