2022-09-14 12:42:46 +00:00
|
|
|
/* eslint-disable lines-around-comment */
|
|
|
|
import { IStore } from '../app/types';
|
2021-11-17 14:33:03 +00:00
|
|
|
import {
|
|
|
|
CONFERENCE_JOINED,
|
2022-09-14 12:42:46 +00:00
|
|
|
CONFERENCE_WILL_LEAVE
|
|
|
|
} from '../base/conference/actionTypes';
|
|
|
|
// @ts-ignore
|
|
|
|
import { getCurrentConference } from '../base/conference/functions';
|
2022-04-04 13:09:14 +00:00
|
|
|
import { JitsiConferenceEvents } from '../base/lib-jitsi-meet';
|
2022-09-14 12:42:46 +00:00
|
|
|
import { getLocalParticipant, getParticipantCount } from '../base/participants/functions';
|
|
|
|
import { Participant } from '../base/participants/types';
|
|
|
|
import MiddlewareRegistry from '../base/redux/MiddlewareRegistry';
|
|
|
|
import { TRACK_UPDATED, TRACK_ADDED, TRACK_REMOVED } from '../base/tracks/actionTypes';
|
2021-11-17 14:33:03 +00:00
|
|
|
|
2022-09-14 12:42:46 +00:00
|
|
|
import FaceLandmarksDetector from './FaceLandmarksDetector';
|
|
|
|
import { ADD_FACE_EXPRESSION, NEW_FACE_COORDINATES, UPDATE_FACE_COORDINATES } from './actionTypes';
|
2021-11-17 14:33:03 +00:00
|
|
|
import {
|
2022-09-14 12:42:46 +00:00
|
|
|
addToFaceExpressionsBuffer
|
2021-11-17 14:33:03 +00:00
|
|
|
} from './actions';
|
2022-04-04 13:09:14 +00:00
|
|
|
import { FACE_BOX_EVENT_TYPE } from './constants';
|
2022-09-14 12:42:46 +00:00
|
|
|
import { sendFaceBoxToParticipants, sendFaceExpressionToParticipants, sendFaceExpressionToServer } from './functions';
|
2021-11-17 14:33:03 +00:00
|
|
|
|
|
|
|
|
2022-09-14 12:42:46 +00:00
|
|
|
MiddlewareRegistry.register((store: IStore) => (next: Function) => (action: any) => {
|
|
|
|
const { dispatch, getState } = store;
|
2022-04-06 09:10:31 +00:00
|
|
|
const { faceLandmarks } = getState()['features/base/config'];
|
|
|
|
const isEnabled = faceLandmarks?.enableFaceCentering || faceLandmarks?.enableFaceExpressionsDetection;
|
2021-11-17 14:33:03 +00:00
|
|
|
|
|
|
|
if (action.type === CONFERENCE_JOINED) {
|
2022-04-04 13:09:14 +00:00
|
|
|
if (isEnabled) {
|
2022-09-14 12:42:46 +00:00
|
|
|
FaceLandmarksDetector.init(store);
|
2022-04-04 13:09:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// allow using remote face centering data when local face centering is not enabled
|
|
|
|
action.conference.on(
|
|
|
|
JitsiConferenceEvents.ENDPOINT_MESSAGE_RECEIVED,
|
2022-09-14 12:42:46 +00:00
|
|
|
(participant: Participant | undefined, eventData: any) => {
|
|
|
|
if (!participant || !eventData || !participant.getId) {
|
2022-04-04 13:09:14 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (eventData.type === FACE_BOX_EVENT_TYPE) {
|
|
|
|
dispatch({
|
|
|
|
type: UPDATE_FACE_COORDINATES,
|
|
|
|
faceBox: eventData.faceBox,
|
|
|
|
id: participant.getId()
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2021-11-17 14:33:03 +00:00
|
|
|
|
|
|
|
return next(action);
|
|
|
|
}
|
|
|
|
|
2022-04-04 13:09:14 +00:00
|
|
|
|
|
|
|
if (!isEnabled) {
|
2021-11-17 14:33:03 +00:00
|
|
|
return next(action);
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (action.type) {
|
|
|
|
case CONFERENCE_WILL_LEAVE : {
|
2022-09-14 12:42:46 +00:00
|
|
|
FaceLandmarksDetector.stopDetection(store);
|
2021-11-17 14:33:03 +00:00
|
|
|
|
|
|
|
return next(action);
|
|
|
|
}
|
2022-04-04 13:09:14 +00:00
|
|
|
case TRACK_ADDED: {
|
2022-09-23 17:45:51 +00:00
|
|
|
const { jitsiTrack: { isLocal, muted, videoType } } = action.track;
|
2021-11-17 14:33:03 +00:00
|
|
|
|
2022-09-23 17:45:51 +00:00
|
|
|
if (videoType === 'camera' && isLocal() && !muted) {
|
2022-04-04 13:09:14 +00:00
|
|
|
// need to pass this since the track is not yet added in the store
|
2022-09-14 12:42:46 +00:00
|
|
|
FaceLandmarksDetector.startDetection(store, action.track);
|
2021-11-17 14:33:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return next(action);
|
|
|
|
}
|
2022-04-04 13:09:14 +00:00
|
|
|
case TRACK_UPDATED: {
|
|
|
|
const { jitsiTrack: { isLocal, videoType } } = action.track;
|
|
|
|
|
|
|
|
if (videoType !== 'camera' || !isLocal()) {
|
|
|
|
return next(action);
|
|
|
|
}
|
2021-11-17 14:33:03 +00:00
|
|
|
|
2022-04-04 13:09:14 +00:00
|
|
|
const { muted } = action.track;
|
|
|
|
|
|
|
|
if (muted !== undefined) {
|
|
|
|
// addresses video mute state changes
|
|
|
|
if (muted) {
|
2022-09-14 12:42:46 +00:00
|
|
|
FaceLandmarksDetector.stopDetection(store);
|
2022-04-04 13:09:14 +00:00
|
|
|
} else {
|
2022-09-14 12:42:46 +00:00
|
|
|
FaceLandmarksDetector.startDetection(store);
|
2022-04-04 13:09:14 +00:00
|
|
|
}
|
2021-11-17 14:33:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return next(action);
|
|
|
|
}
|
|
|
|
case TRACK_REMOVED: {
|
2022-04-04 13:09:14 +00:00
|
|
|
const { jitsiTrack: { isLocal, videoType } } = action.track;
|
2021-11-17 14:33:03 +00:00
|
|
|
|
2022-04-04 13:09:14 +00:00
|
|
|
if (videoType === 'camera' && isLocal()) {
|
2022-09-14 12:42:46 +00:00
|
|
|
FaceLandmarksDetector.stopDetection(store);
|
2021-11-17 14:33:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return next(action);
|
|
|
|
}
|
2022-04-06 09:10:31 +00:00
|
|
|
case ADD_FACE_EXPRESSION: {
|
2021-11-17 14:33:03 +00:00
|
|
|
const state = getState();
|
2022-09-14 12:42:46 +00:00
|
|
|
const { faceExpression, duration, timestamp } = action;
|
2021-11-17 14:33:03 +00:00
|
|
|
const conference = getCurrentConference(state);
|
|
|
|
|
|
|
|
if (getParticipantCount(state) > 1) {
|
2022-09-14 12:42:46 +00:00
|
|
|
sendFaceExpressionToParticipants(conference, faceExpression, duration);
|
2021-11-17 14:33:03 +00:00
|
|
|
}
|
2022-09-14 12:42:46 +00:00
|
|
|
sendFaceExpressionToServer(conference, faceExpression, duration);
|
2022-04-06 09:10:31 +00:00
|
|
|
dispatch(addToFaceExpressionsBuffer({
|
2022-09-14 12:42:46 +00:00
|
|
|
emotion: faceExpression,
|
|
|
|
timestamp
|
2021-12-21 11:46:54 +00:00
|
|
|
}));
|
2021-11-17 14:33:03 +00:00
|
|
|
|
|
|
|
return next(action);
|
|
|
|
}
|
2022-09-14 12:42:46 +00:00
|
|
|
case NEW_FACE_COORDINATES: {
|
|
|
|
const state = getState();
|
|
|
|
const { faceBox } = action;
|
|
|
|
const conference = getCurrentConference(state);
|
|
|
|
const localParticipant = getLocalParticipant(state);
|
|
|
|
|
|
|
|
if (getParticipantCount(state) > 1) {
|
|
|
|
sendFaceBoxToParticipants(conference, faceBox);
|
|
|
|
}
|
|
|
|
|
|
|
|
dispatch({
|
|
|
|
type: UPDATE_FACE_COORDINATES,
|
|
|
|
faceBox,
|
|
|
|
id: localParticipant?.id
|
|
|
|
});
|
|
|
|
}
|
2021-11-17 14:33:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return next(action);
|
|
|
|
});
|