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.any.js 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. // @flow
  2. import { getCurrentConference } from '../base/conference';
  3. import { VIDEO_TYPE } from '../base/media';
  4. import {
  5. PARTICIPANT_LEFT,
  6. PIN_PARTICIPANT,
  7. pinParticipant,
  8. getParticipantById,
  9. getPinnedParticipant
  10. } from '../base/participants';
  11. import { MiddlewareRegistry, StateListenerRegistry } from '../base/redux';
  12. import { TRACK_REMOVED } from '../base/tracks';
  13. import { SET_DOCUMENT_EDITING_STATUS } from '../etherpad';
  14. import { isStageFilmstripEnabled } from '../filmstrip/functions';
  15. import { isFollowMeActive } from '../follow-me';
  16. import { SET_TILE_VIEW } from './actionTypes';
  17. import { setRemoteParticipantsWithScreenShare, setTileView } from './actions';
  18. import { getAutoPinSetting, updateAutoPinnedParticipant } from './functions';
  19. import './subscriber';
  20. let previousTileViewEnabled;
  21. /**
  22. * Middleware which intercepts actions and updates tile view related state.
  23. *
  24. * @param {Store} store - The redux store.
  25. * @returns {Function}
  26. */
  27. MiddlewareRegistry.register(store => next => action => {
  28. // we want to extract the leaving participant and check its type before actually the participant being removed.
  29. let shouldUpdateAutoPin = false;
  30. switch (action.type) {
  31. case PARTICIPANT_LEFT: {
  32. if (!getAutoPinSetting() || isFollowMeActive(store)) {
  33. break;
  34. }
  35. shouldUpdateAutoPin = getParticipantById(store.getState(), action.participant.id)?.isFakeParticipant;
  36. break;
  37. }
  38. }
  39. const result = next(action);
  40. switch (action.type) {
  41. // Actions that temporarily clear the user preferred state of tile view,
  42. // then re-set it when needed.
  43. case PIN_PARTICIPANT: {
  44. const pinnedParticipant = getPinnedParticipant(store.getState());
  45. if (pinnedParticipant) {
  46. _storeTileViewStateAndClear(store);
  47. } else {
  48. _restoreTileViewState(store);
  49. }
  50. break;
  51. }
  52. case SET_DOCUMENT_EDITING_STATUS:
  53. if (action.editing) {
  54. _storeTileViewStateAndClear(store);
  55. } else {
  56. _restoreTileViewState(store);
  57. }
  58. break;
  59. // Things to update when tile view state changes
  60. case SET_TILE_VIEW: {
  61. const state = store.getState();
  62. const stageFilmstrip = isStageFilmstripEnabled(state);
  63. if (action.enabled && !stageFilmstrip && getPinnedParticipant(state)) {
  64. store.dispatch(pinParticipant(null));
  65. }
  66. break;
  67. }
  68. // Update the remoteScreenShares.
  69. // Because of the debounce in the subscriber which updates the remoteScreenShares we need to handle
  70. // removal of screen shares separatelly here. Otherwise it is possible to have screen sharing
  71. // participant that has already left in the remoteScreenShares array. This can lead to rendering
  72. // a thumbnails for already left participants since the remoteScreenShares array is used for
  73. // building the ordered list of remote participants.
  74. case TRACK_REMOVED: {
  75. const { jitsiTrack } = action.track;
  76. if (jitsiTrack && jitsiTrack.isVideoTrack() && jitsiTrack.getVideoType() === VIDEO_TYPE.DESKTOP) {
  77. const participantId = jitsiTrack.getParticipantId();
  78. const oldScreenShares = store.getState()['features/video-layout'].remoteScreenShares || [];
  79. const newScreenShares = oldScreenShares.filter(id => id !== participantId);
  80. if (oldScreenShares.length !== newScreenShares.length) { // the participant was removed
  81. store.dispatch(setRemoteParticipantsWithScreenShare(newScreenShares));
  82. updateAutoPinnedParticipant(oldScreenShares, store);
  83. }
  84. }
  85. break;
  86. }
  87. }
  88. if (shouldUpdateAutoPin) {
  89. const screenShares = store.getState()['features/video-layout'].remoteScreenShares || [];
  90. updateAutoPinnedParticipant(screenShares, store);
  91. }
  92. return result;
  93. });
  94. /**
  95. * Set up state change listener to perform maintenance tasks when the conference
  96. * is left or failed.
  97. */
  98. StateListenerRegistry.register(
  99. state => getCurrentConference(state),
  100. (conference, { dispatch }, previousConference) => {
  101. if (conference !== previousConference) {
  102. // conference changed, left or failed...
  103. // Clear tile view state.
  104. dispatch(setTileView());
  105. }
  106. });
  107. /**
  108. * Restores tile view state, if it wasn't updated since then.
  109. *
  110. * @param {Object} store - The Redux Store.
  111. * @returns {void}
  112. */
  113. function _restoreTileViewState({ dispatch, getState }) {
  114. const { tileViewEnabled } = getState()['features/video-layout'];
  115. if (tileViewEnabled === undefined && previousTileViewEnabled !== undefined) {
  116. dispatch(setTileView(previousTileViewEnabled));
  117. }
  118. previousTileViewEnabled = undefined;
  119. }
  120. /**
  121. * Stores the current tile view state and clears it.
  122. *
  123. * @param {Object} store - The Redux Store.
  124. * @returns {void}
  125. */
  126. function _storeTileViewStateAndClear({ dispatch, getState }) {
  127. const { tileViewEnabled } = getState()['features/video-layout'];
  128. if (tileViewEnabled !== undefined) {
  129. previousTileViewEnabled = tileViewEnabled;
  130. dispatch(setTileView(undefined));
  131. }
  132. }