Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

middleware.web.ts 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. import { batch } from 'react-redux';
  2. // @ts-expect-error
  3. import VideoLayout from '../../../modules/UI/videolayout/VideoLayout';
  4. import {
  5. DOMINANT_SPEAKER_CHANGED,
  6. PARTICIPANT_JOINED,
  7. PARTICIPANT_LEFT
  8. } from '../base/participants/actionTypes';
  9. import {
  10. getDominantSpeakerParticipant,
  11. getLocalParticipant,
  12. getLocalScreenShareParticipant,
  13. isScreenShareParticipant
  14. } from '../base/participants/functions';
  15. import MiddlewareRegistry from '../base/redux/MiddlewareRegistry';
  16. import { CLIENT_RESIZED } from '../base/responsive-ui/actionTypes';
  17. import { SETTINGS_UPDATED } from '../base/settings/actionTypes';
  18. import { setTileView } from '../video-layout/actions.web';
  19. import { LAYOUTS } from '../video-layout/constants';
  20. import { getCurrentLayout } from '../video-layout/functions.web';
  21. import { WHITEBOARD_ID } from '../whiteboard/constants';
  22. import { isWhiteboardVisible } from '../whiteboard/functions';
  23. import {
  24. ADD_STAGE_PARTICIPANT,
  25. CLEAR_STAGE_PARTICIPANTS,
  26. REMOVE_STAGE_PARTICIPANT,
  27. RESIZE_FILMSTRIP,
  28. SET_USER_FILMSTRIP_WIDTH,
  29. TOGGLE_PIN_STAGE_PARTICIPANT
  30. } from './actionTypes';
  31. import {
  32. addStageParticipant,
  33. removeStageParticipant,
  34. setFilmstripHeight,
  35. setFilmstripWidth,
  36. setScreenshareFilmstripParticipant,
  37. setStageParticipants
  38. } from './actions.web';
  39. import {
  40. ACTIVE_PARTICIPANT_TIMEOUT,
  41. DEFAULT_FILMSTRIP_WIDTH,
  42. MAX_ACTIVE_PARTICIPANTS,
  43. MIN_STAGE_VIEW_HEIGHT,
  44. MIN_STAGE_VIEW_WIDTH,
  45. TOP_FILMSTRIP_HEIGHT
  46. } from './constants';
  47. import {
  48. getActiveParticipantsIds,
  49. getPinnedActiveParticipants,
  50. isFilmstripResizable,
  51. isStageFilmstripAvailable,
  52. isStageFilmstripTopPanel,
  53. updateRemoteParticipants,
  54. updateRemoteParticipantsOnLeave
  55. } from './functions.web';
  56. import './subscriber.web';
  57. /**
  58. * Map of timers.
  59. *
  60. * @type {Map}
  61. */
  62. const timers = new Map();
  63. /**
  64. * The middleware of the feature Filmstrip.
  65. */
  66. MiddlewareRegistry.register(store => next => action => {
  67. if (action.type === PARTICIPANT_LEFT) {
  68. // This has to be executed before we remove the participant from features/base/participants state in order to
  69. // remove the related thumbnail component before we need to re-render it. If we do this after next()
  70. // we will be in situation where the participant exists in the remoteParticipants array in features/filmstrip
  71. // but doesn't exist in features/base/participants state which will lead to rendering a thumbnail for
  72. // non-existing participant.
  73. updateRemoteParticipantsOnLeave(store, action.participant?.id);
  74. }
  75. let result;
  76. switch (action.type) {
  77. case CLIENT_RESIZED: {
  78. const state = store.getState();
  79. if (isFilmstripResizable(state)) {
  80. const { width: filmstripWidth, topPanelHeight } = state['features/filmstrip'];
  81. const { clientWidth, clientHeight } = action;
  82. let height, width;
  83. if ((filmstripWidth.current ?? 0) > clientWidth - MIN_STAGE_VIEW_WIDTH) {
  84. width = Math.max(clientWidth - MIN_STAGE_VIEW_WIDTH, DEFAULT_FILMSTRIP_WIDTH);
  85. } else {
  86. width = Math.min(clientWidth - MIN_STAGE_VIEW_WIDTH, filmstripWidth.userSet ?? 0);
  87. }
  88. if (width !== filmstripWidth.current) {
  89. store.dispatch(setFilmstripWidth(width));
  90. }
  91. if ((topPanelHeight.current ?? 0) > clientHeight - MIN_STAGE_VIEW_HEIGHT) {
  92. height = Math.max(clientHeight - MIN_STAGE_VIEW_HEIGHT, TOP_FILMSTRIP_HEIGHT);
  93. } else {
  94. height = Math.min(clientHeight - MIN_STAGE_VIEW_HEIGHT, topPanelHeight.userSet ?? 0);
  95. }
  96. if (height !== topPanelHeight.current) {
  97. store.dispatch(setFilmstripHeight(height));
  98. }
  99. }
  100. break;
  101. }
  102. case PARTICIPANT_JOINED: {
  103. result = next(action);
  104. if (isScreenShareParticipant(action.participant)) {
  105. break;
  106. }
  107. updateRemoteParticipants(store, action.participant?.id);
  108. break;
  109. }
  110. case SETTINGS_UPDATED: {
  111. if (typeof action.settings?.localFlipX === 'boolean') {
  112. // TODO: This needs to be removed once the large video is Reactified.
  113. VideoLayout.onLocalFlipXChanged(action.settings.localFlipX);
  114. }
  115. if (action.settings?.disableSelfView) {
  116. const state = store.getState();
  117. const local = getLocalParticipant(state);
  118. const localScreenShare = getLocalScreenShareParticipant(state);
  119. const activeParticipantsIds = getActiveParticipantsIds(state);
  120. if (activeParticipantsIds.find(id => id === local?.id)) {
  121. store.dispatch(removeStageParticipant(local?.id ?? ''));
  122. }
  123. if (localScreenShare) {
  124. if (activeParticipantsIds.find(id => id === localScreenShare.id)) {
  125. store.dispatch(removeStageParticipant(localScreenShare.id));
  126. }
  127. }
  128. }
  129. if (action.settings?.maxStageParticipants !== undefined) {
  130. const maxParticipants = action.settings.maxStageParticipants;
  131. const { activeParticipants } = store.getState()['features/filmstrip'];
  132. const newMax = Math.min(MAX_ACTIVE_PARTICIPANTS, maxParticipants);
  133. if (newMax < activeParticipants.length) {
  134. const toRemove = activeParticipants.slice(0, activeParticipants.length - newMax);
  135. batch(() => {
  136. toRemove.forEach(p => store.dispatch(removeStageParticipant(p.participantId)));
  137. });
  138. }
  139. }
  140. break;
  141. }
  142. case SET_USER_FILMSTRIP_WIDTH: {
  143. VideoLayout.refreshLayout();
  144. break;
  145. }
  146. case RESIZE_FILMSTRIP: {
  147. const { width = 0 } = action;
  148. store.dispatch(setFilmstripWidth(width));
  149. break;
  150. }
  151. case ADD_STAGE_PARTICIPANT: {
  152. const { dispatch, getState } = store;
  153. const { participantId, pinned } = action;
  154. const state = getState();
  155. const { activeParticipants } = state['features/filmstrip'];
  156. const { maxStageParticipants } = state['features/base/settings'];
  157. const isWhiteboardActive = isWhiteboardVisible(state);
  158. let queue;
  159. if (activeParticipants.find(p => p.participantId === participantId)) {
  160. queue = activeParticipants.filter(p => p.participantId !== participantId);
  161. queue.push({
  162. participantId,
  163. pinned
  164. });
  165. const tid = timers.get(participantId);
  166. clearTimeout(tid);
  167. timers.delete(participantId);
  168. } else if (activeParticipants.length < (maxStageParticipants ?? 0)) {
  169. queue = [ ...activeParticipants, {
  170. participantId,
  171. pinned
  172. } ];
  173. } else {
  174. const notPinnedIndex = activeParticipants.findIndex(p => !p.pinned);
  175. if (notPinnedIndex === -1) {
  176. if (pinned) {
  177. queue = [ ...activeParticipants, {
  178. participantId,
  179. pinned
  180. } ];
  181. queue.shift();
  182. }
  183. } else {
  184. queue = [ ...activeParticipants, {
  185. participantId,
  186. pinned
  187. } ];
  188. queue.splice(notPinnedIndex, 1);
  189. }
  190. }
  191. if (participantId === WHITEBOARD_ID) {
  192. // If the whiteboard is pinned, this action should clear the other pins.
  193. queue = [ {
  194. participantId,
  195. pinned: true
  196. } ];
  197. } else if (isWhiteboardActive && Array.isArray(queue)) {
  198. // When another participant is pinned, remove the whiteboard from the stage area.
  199. queue = queue.filter(p => p?.participantId !== WHITEBOARD_ID);
  200. }
  201. // If queue is undefined we haven't made any changes to the active participants. This will mostly happen
  202. // if the participant that we are trying to add is not pinned and all slots are currently taken by pinned
  203. // participants.
  204. // IMPORTANT: setting active participants to undefined will crash jitsi-meet.
  205. if (typeof queue !== 'undefined') {
  206. dispatch(setStageParticipants(queue));
  207. if (!pinned) {
  208. const timeoutId = setTimeout(() => dispatch(removeStageParticipant(participantId)),
  209. ACTIVE_PARTICIPANT_TIMEOUT);
  210. timers.set(participantId, timeoutId);
  211. }
  212. }
  213. if (getCurrentLayout(state) === LAYOUTS.TILE_VIEW) {
  214. dispatch(setTileView(false));
  215. }
  216. break;
  217. }
  218. case REMOVE_STAGE_PARTICIPANT: {
  219. const state = store.getState();
  220. const { participantId } = action;
  221. const tid = timers.get(participantId);
  222. clearTimeout(tid);
  223. timers.delete(participantId);
  224. const dominant = getDominantSpeakerParticipant(state);
  225. if (participantId === dominant?.id) {
  226. const timeoutId = setTimeout(() => store.dispatch(removeStageParticipant(participantId)),
  227. ACTIVE_PARTICIPANT_TIMEOUT);
  228. timers.set(participantId, timeoutId);
  229. return;
  230. }
  231. break;
  232. }
  233. case DOMINANT_SPEAKER_CHANGED: {
  234. const { id } = action.participant;
  235. const state = store.getState();
  236. const stageFilmstrip = isStageFilmstripAvailable(state);
  237. const local = getLocalParticipant(state);
  238. const currentLayout = getCurrentLayout(state);
  239. const dominantSpeaker = getDominantSpeakerParticipant(state);
  240. if (dominantSpeaker?.id === id || id === local?.id || currentLayout === LAYOUTS.TILE_VIEW) {
  241. break;
  242. }
  243. if (stageFilmstrip) {
  244. const isPinned = getPinnedActiveParticipants(state).some(p => p.participantId === id);
  245. store.dispatch(addStageParticipant(id, Boolean(isPinned)));
  246. }
  247. break;
  248. }
  249. case PARTICIPANT_LEFT: {
  250. const state = store.getState();
  251. const { id } = action.participant;
  252. const activeParticipantsIds = getActiveParticipantsIds(state);
  253. if (activeParticipantsIds.find(pId => pId === id)) {
  254. const tid = timers.get(id);
  255. const { activeParticipants } = state['features/filmstrip'];
  256. clearTimeout(tid);
  257. timers.delete(id);
  258. store.dispatch(setStageParticipants(activeParticipants.filter(p => p.participantId !== id)));
  259. }
  260. break;
  261. }
  262. case TOGGLE_PIN_STAGE_PARTICIPANT: {
  263. const { dispatch, getState } = store;
  264. const state = getState();
  265. const { participantId } = action;
  266. const pinnedParticipants = getPinnedActiveParticipants(state);
  267. const dominant = getDominantSpeakerParticipant(state);
  268. if (isStageFilmstripTopPanel(state, 2)) {
  269. const screenshares = state['features/video-layout'].remoteScreenShares;
  270. if (screenshares.find(sId => sId === participantId)) {
  271. dispatch(setScreenshareFilmstripParticipant(participantId));
  272. break;
  273. }
  274. }
  275. if (pinnedParticipants.find(p => p.participantId === participantId)) {
  276. if (dominant?.id === participantId) {
  277. const { activeParticipants } = state['features/filmstrip'];
  278. const queue = activeParticipants.map(p => {
  279. if (p.participantId === participantId) {
  280. return {
  281. participantId,
  282. pinned: false
  283. };
  284. }
  285. return p;
  286. });
  287. dispatch(setStageParticipants(queue));
  288. } else {
  289. dispatch(removeStageParticipant(participantId));
  290. }
  291. } else {
  292. dispatch(addStageParticipant(participantId, true));
  293. }
  294. break;
  295. }
  296. case CLEAR_STAGE_PARTICIPANTS: {
  297. const activeParticipants = getActiveParticipantsIds(store.getState());
  298. activeParticipants.forEach(pId => {
  299. const tid = timers.get(pId);
  300. clearTimeout(tid);
  301. timers.delete(pId);
  302. });
  303. }
  304. }
  305. return result ?? next(action);
  306. });