Skip to content

Commit

Permalink
chore: add debug logs and send them to sample app through hug events (#…
Browse files Browse the repository at this point in the history
…763)

* chore: add debug logs and send them to sample app through hug events

* remove core mocking
  • Loading branch information
thaddmt committed Mar 6, 2023
1 parent 63e7ffe commit 8e71060
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ import { LivenessStreamProvider } from '../liveness-stream-provider';
import { VideoRecorder } from '../video-recorder';
import { mockClientSessionInformationEvent } from '../liveness-test-helpers';

jest.mock('../video-recorder');
jest.mock('@aws-sdk/client-rekognitionstreaming');
jest.mock('@aws-amplify/core');
jest.mock('@aws-amplify/analytics');
jest.mock('aws-amplify');
jest.mock('../video-recorder');
const mockGet = jest.fn().mockImplementation(() => {
return {
accessKeyId: 'accessKeyId',
Expand Down
27 changes: 27 additions & 0 deletions packages/ui/src/helpers/liveness/video-recorder.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import { Hub } from 'aws-amplify';

/**
* The options for the video recorder.
*/
export interface VideoRecorderOptions {
// TODO:: add options
}

const DEBUG = process.env.NEXT_PUBLIC_DEBUG === 'TRUE';

/**
* Helper wrapper class over the native MediaRecorder.
*/
Expand Down Expand Up @@ -45,6 +49,19 @@ export class VideoRecorder {
if (this._chunks.length === 0) {
this.firstChunkTimestamp = Date.now();
}
if (DEBUG) {
console.log(
`chunk sent #${this._chunks.length}: ${JSON.stringify({
size: e.data.size,
time: Date.now(),
})}`
);
Hub.dispatch('LivenessSampleApp', {
event: 'chunkEvent',
data: { size: e.data.size },
message: 'Chunk sent',
});
}
this._chunks.push(e.data);
controller.enqueue(e.data);
}
Expand All @@ -64,6 +81,16 @@ export class VideoRecorder {
'clientSesssionInfo',
(e: MessageEvent) => {
controller.enqueue(e.data.clientInfo);
if (DEBUG) {
console.log(
`Client Info sent: ${JSON.stringify(e.data.clientInfo)}`
);
Hub.dispatch('LivenessSampleApp', {
event: 'clientInfoEvent',
data: { clientInfo: e.data.clientInfo },
message: 'Client info sent',
});
}
}
);

Expand Down
37 changes: 37 additions & 0 deletions packages/ui/src/machines/liveness/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,12 @@ import {
ClientSessionInformationEvent,
LivenessResponseStream,
} from '@aws-sdk/client-rekognitionstreaming';
import { Hub } from 'aws-amplify';

export const MIN_FACE_MATCH_TIME = 500;

const DEBUG = process.env.NEXT_PUBLIC_DEBUG === 'TRUE';

// timer metrics variables
let faceDetectedTimestamp: number;
let ovalDrawnTimestamp: number;
Expand Down Expand Up @@ -918,6 +921,19 @@ export const livenessMachine = createMachine<LivenessContext, LivenessEvent>(
});
}

if (DEBUG) {
const deviceInfo = {
deviceName: realVideoDevices[0].label,
userAgent: navigator.userAgent,
};
console.log(`device info: ${JSON.stringify(deviceInfo)}`);
Hub.dispatch('LivenessSampleApp', {
event: 'deviceEvent',
data: deviceInfo,
message: 'Device info',
});
}

return { stream: realVideoDeviceStream };
},
async openLivenessStreamConnection(context) {
Expand Down Expand Up @@ -1197,11 +1213,32 @@ export const livenessMachine = createMachine<LivenessContext, LivenessEvent>(
async getLiveness(context) {
const {
componentProps: { sessionId, onAnalysisComplete },
videoAssociatedParams: { videoMediaStream },
livenessStreamProvider,
} = context;

livenessStreamProvider.endStream();

if (DEBUG) {
const debugInfo = {
recorderStartTimestamp:
livenessStreamProvider.videoRecorder.recorderStartTimestamp,
recordingStartApiTimestamp:
livenessStreamProvider.videoRecorder.recordingStartApiTimestamp,
recorderEndTimestamp:
livenessStreamProvider.videoRecorder.recorderEndTimestamp,
firstChunkTimestamp:
livenessStreamProvider.videoRecorder.firstChunkTimestamp,
videoStream: videoMediaStream.getVideoTracks()[0].getSettings(),
};
console.log(`debug info: ${JSON.stringify(debugInfo)}`);
Hub.dispatch('LivenessSampleApp', {
event: 'debugEvent',
data: debugInfo,
message: 'Debug info',
});
}

// Get liveness result
await onAnalysisComplete();
},
Expand Down

0 comments on commit 8e71060

Please sign in to comment.