您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

middleware.js 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. // @flow
  2. import {
  3. CONFERENCE_JOINED,
  4. CONFERENCE_WILL_LEAVE,
  5. getCurrentConference
  6. } from '../base/conference';
  7. import { JitsiConferenceEvents } from '../base/lib-jitsi-meet';
  8. import { getParticipantCount } from '../base/participants';
  9. import { MiddlewareRegistry } from '../base/redux';
  10. import { TRACK_UPDATED, TRACK_ADDED, TRACK_REMOVED } from '../base/tracks';
  11. import { ADD_FACE_EXPRESSION, UPDATE_FACE_COORDINATES } from './actionTypes';
  12. import {
  13. addToFaceExpressionsBuffer,
  14. loadWorker,
  15. stopFaceLandmarksDetection,
  16. startFaceLandmarksDetection
  17. } from './actions';
  18. import { FACE_BOX_EVENT_TYPE } from './constants';
  19. import { sendFaceExpressionToParticipants, sendFaceExpressionToServer } from './functions';
  20. MiddlewareRegistry.register(({ dispatch, getState }) => next => action => {
  21. const { faceLandmarks } = getState()['features/base/config'];
  22. const isEnabled = faceLandmarks?.enableFaceCentering || faceLandmarks?.enableFaceExpressionsDetection;
  23. if (action.type === CONFERENCE_JOINED) {
  24. if (isEnabled) {
  25. dispatch(loadWorker());
  26. }
  27. // allow using remote face centering data when local face centering is not enabled
  28. action.conference.on(
  29. JitsiConferenceEvents.ENDPOINT_MESSAGE_RECEIVED,
  30. (participant, eventData) => {
  31. if (!participant || !eventData) {
  32. return;
  33. }
  34. if (eventData.type === FACE_BOX_EVENT_TYPE) {
  35. dispatch({
  36. type: UPDATE_FACE_COORDINATES,
  37. faceBox: eventData.faceBox,
  38. id: participant.getId()
  39. });
  40. }
  41. });
  42. return next(action);
  43. }
  44. if (!isEnabled) {
  45. return next(action);
  46. }
  47. switch (action.type) {
  48. case CONFERENCE_WILL_LEAVE : {
  49. dispatch(stopFaceLandmarksDetection());
  50. return next(action);
  51. }
  52. case TRACK_ADDED: {
  53. const { jitsiTrack: { isLocal, videoType } } = action.track;
  54. if (videoType === 'camera' && isLocal()) {
  55. // need to pass this since the track is not yet added in the store
  56. dispatch(startFaceLandmarksDetection(action.track));
  57. }
  58. return next(action);
  59. }
  60. case TRACK_UPDATED: {
  61. const { jitsiTrack: { isLocal, videoType } } = action.track;
  62. if (videoType !== 'camera' || !isLocal()) {
  63. return next(action);
  64. }
  65. const { muted } = action.track;
  66. if (muted !== undefined) {
  67. // addresses video mute state changes
  68. if (muted) {
  69. dispatch(stopFaceLandmarksDetection());
  70. } else {
  71. dispatch(startFaceLandmarksDetection());
  72. }
  73. }
  74. return next(action);
  75. }
  76. case TRACK_REMOVED: {
  77. const { jitsiTrack: { isLocal, videoType } } = action.track;
  78. if (videoType === 'camera' && isLocal()) {
  79. dispatch(stopFaceLandmarksDetection());
  80. }
  81. return next(action);
  82. }
  83. case ADD_FACE_EXPRESSION: {
  84. const state = getState();
  85. const conference = getCurrentConference(state);
  86. if (getParticipantCount(state) > 1) {
  87. sendFaceExpressionToParticipants(conference, action.faceExpression, action.duration);
  88. }
  89. sendFaceExpressionToServer(conference, action.faceExpression, action.duration);
  90. dispatch(addToFaceExpressionsBuffer({
  91. emotion: action.faceExpression,
  92. timestamp: action.timestamp
  93. }));
  94. return next(action);
  95. }
  96. }
  97. return next(action);
  98. });