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.web.js 11KB

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