Skip to content

Commit

Permalink
chore: add VideoEndTimestamp to client info (#749)
Browse files Browse the repository at this point in the history
* chore: add VideoEndTimestamp to client info
* chore: separate stopVideo and empty chunk dispatch events, update tests
  • Loading branch information
hbuchel committed Feb 27, 2023
1 parent a91e5f4 commit 1fa4e7c
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -195,15 +195,26 @@ describe('LivenessStreamProvider', () => {
});

describe('stopVideo', () => {
test('should stop sending video events and then dispatch an empty video chunk', async () => {
test('should stop sending video events', async () => {
const provider = new LivenessStreamProvider(
'sessionId',
mockVideoMediaStream,
mockVideoEl
);
const response = await provider.stopVideo();

expect(mockVideoRecorder.stop).toHaveBeenCalled();
expect(response).toBeUndefined();
});
});

describe('dispatchStopVideoEvent', () => {
test('should dispatch an empty video chunk', async () => {
const provider = new LivenessStreamProvider(
'sessionId',
mockVideoMediaStream,
mockVideoEl
);
const response = await provider.dispatchStopVideoEvent();
expect(mockVideoRecorder.dispatch).toHaveBeenCalledTimes(2);
expect(response).toBeUndefined();
});
Expand Down
6 changes: 5 additions & 1 deletion packages/ui/src/helpers/liveness/liveness-stream-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,13 +160,17 @@ export class LivenessStreamProvider extends AmazonAIInterpretPredictionsProvider
}

public async stopVideo() {
await this.videoRecorder.stop();
this.videoRecorder.stop();
}

public async dispatchStopVideoEvent() {
this.videoRecorder.dispatch(new Event('stopVideo'));
}

public async endStream() {
if (this.videoRecorder.getState() === 'recording') {
await this.stopVideo();
this.dispatchStopVideoEvent();
}
await this._reader.cancel();
return this._reader.closed;
Expand Down
2 changes: 2 additions & 0 deletions packages/ui/src/helpers/liveness/liveness-test-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export const mockVideoRecorder: any = {
destroy: jest.fn(),
recordingStartApiTimestamp: Date.now(),
recorderStartTimestamp: Date.now(),
recorderEndTimestamp: Date.now(),
firstChunkTimestamp: Date.now(),
getState: () => 'idle',
};
Expand All @@ -77,6 +78,7 @@ export const mockLivenessStreamProvider: any = {
sendClientInfo: jest.fn(),
endStream: jest.fn(),
stopVideo: jest.fn(),
dispatchStopVideoEvent: jest.fn(),
getResponseStream: jest.fn().mockResolvedValue([mockedStream]), // TODO: a following PR after PR634 will be made to have the stream emit the proper mock data.
startRecordingLivenessVideo: jest.fn(),
videoRecorder: mockVideoRecorder,
Expand Down
3 changes: 3 additions & 0 deletions packages/ui/src/machines/liveness/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -649,6 +649,9 @@ describe('Liveness Machine', () => {
uploading: 'waitForDisconnectEvent',
});
expect(mockLivenessStreamProvider.stopVideo).toHaveBeenCalledTimes(1);
expect(
mockLivenessStreamProvider.dispatchStopVideoEvent
).toHaveBeenCalledTimes(1);
expect(mockLivenessStreamProvider.sendClientInfo).toHaveBeenCalledTimes(
2
);
Expand Down
14 changes: 6 additions & 8 deletions packages/ui/src/machines/liveness/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1087,10 +1087,7 @@ export const livenessMachine = createMachine<LivenessContext, LivenessEvent>(
async stopVideo(context) {
const {
challengeId,
videoAssociatedParams: {
videoMediaStream,
recordingStartTimestampMs,
},
videoAssociatedParams: { videoMediaStream },
ovalAssociatedParams: { initialFace, ovalDetails },
faceMatchAssociatedParams: { startFace, endFace },
livenessStreamProvider,
Expand All @@ -1101,6 +1098,8 @@ export const livenessMachine = createMachine<LivenessContext, LivenessEvent>(
const flippedInitialFaceLeft =
width - initialFace.left - initialFace.width;

await livenessStreamProvider.stopVideo();

const livenessActionDocument: ClientSessionInformationEvent = {
DeviceInformation: {
ClientSDKVersion: '1.0.0',
Expand Down Expand Up @@ -1134,16 +1133,15 @@ export const livenessMachine = createMachine<LivenessContext, LivenessEvent>(
left: ovalDetails.centerX - ovalDetails.width / 2,
}),
},
VideoEndTimestamp: undefined,
VideoEndTimestamp:
livenessStreamProvider.videoRecorder.recorderEndTimestamp,
},
},
};

livenessStreamProvider.sendClientInfo(livenessActionDocument);

livenessStreamProvider.stopVideo();

const endStreamLivenessVideoTime = Date.now();
await livenessStreamProvider.dispatchStopVideoEvent();
},
async getLiveness(context) {
const {
Expand Down

0 comments on commit 1fa4e7c

Please sign in to comment.