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 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. import { AnyAction } from 'redux';
  2. import { IStore } from '../app/types';
  3. import {
  4. CONFERENCE_JOINED,
  5. CONFERENCE_WILL_LEAVE,
  6. ENDPOINT_MESSAGE_RECEIVED
  7. } from '../base/conference/actionTypes';
  8. import { getCurrentConference } from '../base/conference/functions';
  9. import { getLocalParticipant, getParticipantCount } from '../base/participants/functions';
  10. import MiddlewareRegistry from '../base/redux/MiddlewareRegistry';
  11. import { TRACK_ADDED, TRACK_REMOVED, TRACK_UPDATED } from '../base/tracks/actionTypes';
  12. import FaceLandmarksDetector from './FaceLandmarksDetector';
  13. import { ADD_FACE_LANDMARKS, NEW_FACE_COORDINATES, UPDATE_FACE_COORDINATES } from './actionTypes';
  14. import { FACE_BOX_EVENT_TYPE } from './constants';
  15. import { sendFaceBoxToParticipants, sendFaceExpressionToParticipants } from './functions';
  16. MiddlewareRegistry.register((store: IStore) => (next: Function) => (action: AnyAction) => {
  17. const { dispatch, getState } = store;
  18. const { faceLandmarks: faceLandmarksConfig } = getState()['features/base/config'];
  19. const isEnabled = faceLandmarksConfig?.enableFaceCentering || faceLandmarksConfig?.enableFaceExpressionsDetection;
  20. if (action.type === CONFERENCE_JOINED) {
  21. if (isEnabled) {
  22. FaceLandmarksDetector.init(store);
  23. }
  24. return next(action);
  25. } else if (action.type === ENDPOINT_MESSAGE_RECEIVED) {
  26. // Allow using remote face centering data when local face centering is not enabled.
  27. const { participant, data } = action;
  28. if (data?.type === FACE_BOX_EVENT_TYPE) {
  29. dispatch({
  30. type: UPDATE_FACE_COORDINATES,
  31. faceBox: data.faceBox,
  32. id: participant.getId()
  33. });
  34. }
  35. return next(action);
  36. }
  37. if (!isEnabled) {
  38. return next(action);
  39. }
  40. switch (action.type) {
  41. case CONFERENCE_WILL_LEAVE : {
  42. FaceLandmarksDetector.stopDetection(store);
  43. break;
  44. }
  45. case TRACK_ADDED: {
  46. const { jitsiTrack: { isLocal, videoType }, muted } = action.track;
  47. if (videoType === 'camera' && isLocal() && !muted) {
  48. // need to pass this since the track is not yet added in the store
  49. FaceLandmarksDetector.startDetection(store, action.track);
  50. }
  51. break;
  52. }
  53. case TRACK_UPDATED: {
  54. const { jitsiTrack: { isLocal, videoType } } = action.track;
  55. if (videoType !== 'camera' || !isLocal()) {
  56. break;
  57. }
  58. const { muted } = action.track;
  59. if (typeof muted !== 'undefined') {
  60. // addresses video mute state changes
  61. if (muted) {
  62. FaceLandmarksDetector.stopDetection(store);
  63. } else {
  64. FaceLandmarksDetector.startDetection(store);
  65. }
  66. }
  67. break;
  68. }
  69. case TRACK_REMOVED: {
  70. const { jitsiTrack: { isLocal, videoType } } = action.track;
  71. if (videoType === 'camera' && isLocal()) {
  72. FaceLandmarksDetector.stopDetection(store);
  73. }
  74. break;
  75. }
  76. case ADD_FACE_LANDMARKS: {
  77. const state = getState();
  78. const { faceLandmarks } = action;
  79. const conference = getCurrentConference(state);
  80. if (getParticipantCount(state) > 1) {
  81. sendFaceExpressionToParticipants(conference, faceLandmarks);
  82. }
  83. // Disabling for now as there is no value of having the data in speakerstats at the server
  84. // sendFaceExpressionToServer(conference, faceLandmarks);
  85. break;
  86. }
  87. case NEW_FACE_COORDINATES: {
  88. const state = getState();
  89. const { faceBox } = action;
  90. const conference = getCurrentConference(state);
  91. const localParticipant = getLocalParticipant(state);
  92. if (getParticipantCount(state) > 1) {
  93. sendFaceBoxToParticipants(conference, faceBox);
  94. }
  95. dispatch({
  96. type: UPDATE_FACE_COORDINATES,
  97. faceBox,
  98. id: localParticipant?.id
  99. });
  100. break;
  101. }
  102. }
  103. return next(action);
  104. });