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.2KB

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