@velion-la/onboarding-sdk-react-native 1.2.0-beta.10 → 1.2.0-beta.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -15,7 +15,7 @@
15
15
  */
16
16
 
17
17
  import { useCallback, useEffect, useRef, useState } from 'react';
18
- import { ScrollView, StyleSheet, Text, View } from 'react-native';
18
+ import { Image, ScrollView, StyleSheet, Text, View } from 'react-native';
19
19
  // `expo-file-system` v19's `File` class wraps a `file://` path with a Blob-
20
20
  // compatible API (`arrayBuffer`, `size`, `delete`). We read the recorded mp4
21
21
  // to bytes in JS and PUT them with the global `fetch`. For the typical
@@ -41,7 +41,13 @@ import { StepIcon } from '../ui/icons/StepIcon';
41
41
  import { mixHex, useTheme } from '../ui/theme';
42
42
  import { getStrings } from '../i18n';
43
43
  import { log } from '../utils/log';
44
- import { readBytesFromUri, readJpegFromUri } from '../utils/jpegFromUri';
44
+ import {
45
+ byteLengthOfUri,
46
+ readBytesFromUri,
47
+ readJpegFromUri,
48
+ type JpegFromUri,
49
+ } from '../utils/jpegFromUri';
50
+ import { bytesToBase64 } from '../utils/base64';
45
51
  import {
46
52
  buildSignedFrameEnvelope,
47
53
  importHmacKey,
@@ -59,6 +65,8 @@ import {
59
65
  type LivenessCameraHandle,
60
66
  type LivenessOvalCue,
61
67
  type LivenessVideoFile,
68
+ type CapturedFrame,
69
+ ACTIVE_LIVENESS_CAMERA_VARIANT,
62
70
  } from '../camera/LivenessCamera';
63
71
 
64
72
  import type { ApiCaller } from '../transport/api';
@@ -67,6 +75,21 @@ import type { StepDeps, StepModule } from './types';
67
75
  // Capture loop defaults tuned for the Iris orchestrator's frame cadence.
68
76
  const FRAME_MS_DEFAULT = 350;
69
77
  const FRAME_MS_TURN = 220;
78
+ /**
79
+ * BLINK needs a frame to land while the eyes are closed, a ~150–300 ms window
80
+ * (OSDKR-24). The web SDK paces it at 150 ms; at the default 350 ms an Edge 30
81
+ * Neo sent 12 frames in the 8 s window and the blink never registered. The
82
+ * capture pipeline still caps the real rate — this only stops the timer from
83
+ * adding idle gaps on top of it.
84
+ */
85
+ const FRAME_MS_BLINK = 150;
86
+ /**
87
+ * No frame is sent during the first second after SESSION_READY (OSDKR-24).
88
+ * Iris gates positioning on the mean sharpness of the whole session, so the
89
+ * frames taken while the user is still settling into the oval weigh on every
90
+ * later decision. The 1–2 s before the first CHALLENGE absorbs the wait.
91
+ */
92
+ const CAPTURE_WARMUP_MS = 1000;
70
93
  const PROGRESS_WATCHDOG_MS = 25_000;
71
94
  /**
72
95
  * Hard ceiling for the POST /liveness-verify call that runs after
@@ -81,6 +104,161 @@ const FRAME_MAX_LONG_EDGE = 720;
81
104
  // was the other half of the spoof-false-positive we saw after the v5
82
105
  // migration: ZSL fallback JPEGs re-compressed this hard scored `screen ≈ 0.95`.
83
106
  const FRAME_QUALITY = 0.85;
107
+
108
+ /** A captured frame waiting for the processor, and how long the camera took on it. */
109
+ interface QueuedFrame {
110
+ frame: CapturedFrame;
111
+ captureMs: number;
112
+ }
113
+
114
+ /** A frame scaled to the wire width and encoded as JPEG. */
115
+ interface EncodedFrame {
116
+ jpeg: JpegFromUri;
117
+ wireWidth: number;
118
+ wireHeight: number;
119
+ /** Upright capture size; only known up front for in-memory frames. */
120
+ captureSize?: { width: number; height: number } | undefined;
121
+ /** Orientation and mirroring `toImage()` applied (in-memory frames only). */
122
+ photoOrientation?: string | undefined;
123
+ photoMirrored?: boolean | undefined;
124
+ }
125
+
126
+ /**
127
+ * Releases the camera buffer an in-memory frame holds. A frame that is
128
+ * dropped instead of encoded must go through here, or CameraX runs out of
129
+ * buffers and the next capture stalls.
130
+ */
131
+ function releaseFrame(frame: CapturedFrame): void {
132
+ if (frame.kind !== 'memory') return;
133
+ try {
134
+ frame.photo.dispose();
135
+ } catch {
136
+ // Already released.
137
+ }
138
+ }
139
+
140
+ /**
141
+ * Scales a captured frame to the wire width and encodes it as JPEG. An
142
+ * in-memory frame's camera buffer is released as soon as its pixels are
143
+ * copied out, whether encoding then succeeds or not.
144
+ */
145
+ async function encodeFrame(frame: CapturedFrame): Promise<EncodedFrame> {
146
+ // Iris reads yaw off UN-mirrored frames (OSDKR-23): a mirrored frame turns
147
+ // TURN_LEFT into TURN_RIGHT. The file path already delivers them
148
+ // un-mirrored — do not flip it. The in-memory path must undo the mirroring
149
+ // `toImage()` applies to front-camera photos (see below).
150
+ if (frame.kind === 'file') {
151
+ const compressed = await ImageManipulator.manipulateAsync(
152
+ frame.filePath.startsWith('file://')
153
+ ? frame.filePath
154
+ : `file://${frame.filePath}`,
155
+ [{ resize: { width: FRAME_MAX_LONG_EDGE } }],
156
+ { compress: FRAME_QUALITY, format: ImageManipulator.SaveFormat.JPEG },
157
+ );
158
+ const jpeg = await readJpegFromUri(compressed.uri);
159
+ return { jpeg, wireWidth: compressed.width, wireHeight: compressed.height };
160
+ }
161
+ const { photo } = frame;
162
+ const photoOrientation = photo.orientation;
163
+ const photoMirrored = photo.isMirrored;
164
+ let image: Awaited<ReturnType<typeof photo.toImageAsync>>;
165
+ try {
166
+ image = await photo.toImageAsync();
167
+ } finally {
168
+ releaseFrame(frame);
169
+ }
170
+ const captureSize = { width: image.width, height: image.height };
171
+ const wireWidth = FRAME_MAX_LONG_EDGE;
172
+ const wireHeight = Math.round((image.height * wireWidth) / image.width);
173
+ let wireImage: Awaited<ReturnType<typeof image.resizeAsync>>;
174
+ try {
175
+ wireImage = await image.resizeAsync(wireWidth, wireHeight);
176
+ } finally {
177
+ image.dispose();
178
+ }
179
+ // `toImage()` mirrors front-camera photos (`isMirrored`), which the file
180
+ // path never did. Left as is, an Edge 30 Neo user turning left moved the yaw
181
+ // by -36° and Iris scored TURN_LEFT at 0 for the whole window. Undo it on
182
+ // the small image, where it is cheap.
183
+ if (photoMirrored) {
184
+ const mirrored = wireImage;
185
+ try {
186
+ wireImage = await mirrored.mirrorHorizontallyAsync();
187
+ } finally {
188
+ mirrored.dispose();
189
+ }
190
+ }
191
+ let encoded: Awaited<ReturnType<typeof wireImage.toEncodedImageDataAsync>>;
192
+ try {
193
+ // Same encoder as the file path (`Bitmap.compress`), on its 0–100 scale.
194
+ encoded = await wireImage.toEncodedImageDataAsync(
195
+ 'jpg',
196
+ Math.round(FRAME_QUALITY * 100),
197
+ );
198
+ } finally {
199
+ wireImage.dispose();
200
+ }
201
+ // Copy out of the native-backed buffer before the image objects go away.
202
+ const bytes = new Uint8Array(encoded.buffer.slice(0));
203
+ return {
204
+ jpeg: { bytes, base64: bytesToBase64(bytes), byteLength: bytes.byteLength },
205
+ wireWidth,
206
+ wireHeight,
207
+ captureSize,
208
+ photoOrientation,
209
+ photoMirrored,
210
+ };
211
+ }
212
+
213
+ /**
214
+ * DIAGNOSTIC (OSDKR-24): logs the capture size CameraX negotiated next to the
215
+ * frame that went to Iris, and how long the camera and the processing took
216
+ * on it. Fire-and-forget — a failure here must never cost a frame.
217
+ */
218
+ async function logCaptureDiagnostics(sample: {
219
+ encoded: EncodedFrame;
220
+ source: CapturedFrame['kind'];
221
+ capturePath?: string | undefined;
222
+ captureMs: number;
223
+ processMs: number;
224
+ challenge: unknown;
225
+ }): Promise<void> {
226
+ try {
227
+ const { encoded } = sample;
228
+ let captureSize = encoded.captureSize;
229
+ let captureBytes: number | null = null;
230
+ if (!captureSize && sample.capturePath) {
231
+ const captureUri = sample.capturePath.startsWith('file://')
232
+ ? sample.capturePath
233
+ : `file://${sample.capturePath}`;
234
+ captureSize = await Image.getSize(captureUri);
235
+ captureBytes = byteLengthOfUri(captureUri);
236
+ }
237
+ if (!captureSize) return;
238
+ log.warn('[Liveness][diag] capture', {
239
+ variant: ACTIVE_LIVENESS_CAMERA_VARIANT,
240
+ source: sample.source,
241
+ captureWidth: captureSize.width,
242
+ captureHeight: captureSize.height,
243
+ captureBytes,
244
+ wireWidth: encoded.wireWidth,
245
+ wireHeight: encoded.wireHeight,
246
+ wireBytes: encoded.jpeg.byteLength,
247
+ // Short edge of the capture over the wire width. Below 1 the resize
248
+ // UPSCALED the capture.
249
+ captureToWire: Number(
250
+ (Math.min(captureSize.width, captureSize.height) / FRAME_MAX_LONG_EDGE).toFixed(2),
251
+ ),
252
+ captureMs: sample.captureMs,
253
+ processMs: sample.processMs,
254
+ photoOrientation: encoded.photoOrientation,
255
+ photoMirrored: encoded.photoMirrored,
256
+ challenge: sample.challenge,
257
+ });
258
+ } catch (err) {
259
+ log.warn('[Liveness][diag] capture size unavailable', err);
260
+ }
261
+ }
84
262
  /**
85
263
  * Hard ceiling for the video upload PUT to GCS that runs between SESSION_COMPLETE
86
264
  * and POST /liveness-verify. Long enough for a 5 MB upload on a slow mobile link
@@ -210,7 +388,23 @@ function LivenessMatchView({
210
388
  );
211
389
  const frameMsRef = useRef<number>(FRAME_MS_DEFAULT);
212
390
  const captureTimerRef = useRef<ReturnType<typeof setInterval> | null>(null);
213
- const inFlightRef = useRef(false);
391
+ // Capture and processing run as a two-stage pipeline (OSDKR-24): the camera
392
+ // takes frame N+1 while frame N is resized, signed and sent. On an Edge 30
393
+ // Neo the camera takes ~320 ms of a ~730 ms cycle and our processing the
394
+ // rest; running them back to back capped the loop at ~1.35 fps, against the
395
+ // 2.8–4.5 fps the challenges are paced for.
396
+ const captureInFlightRef = useRef(false);
397
+ const processingRef = useRef(false);
398
+ // At most one captured frame waits for the processor, and no new capture
399
+ // starts while one waits, so frames never queue up and go stale. An
400
+ // in-memory frame holds a camera buffer while it waits: dropping one must
401
+ // release it.
402
+ const pendingFrameRef = useRef<QueuedFrame | null>(null);
403
+ // Bumped by `stopCapture` so a capture or a processing pass still running
404
+ // when the loop stops is dropped instead of sent.
405
+ const captureGenerationRef = useRef(0);
406
+ // No capture starts before this timestamp. See CAPTURE_WARMUP_MS.
407
+ const captureNotBeforeRef = useRef(0);
214
408
  const framesSentRef = useRef(0);
215
409
  const watchdogRef = useRef<ReturnType<typeof setTimeout> | null>(null);
216
410
  const finalizingRef = useRef(false);
@@ -296,14 +490,25 @@ function LivenessMatchView({
296
490
  }
297
491
  }, []);
298
492
 
299
- const stopCapture = useCallback(() => {
493
+ const clearCaptureTimer = useCallback(() => {
300
494
  if (captureTimerRef.current) {
301
495
  clearInterval(captureTimerRef.current);
302
496
  captureTimerRef.current = null;
303
497
  }
304
- inFlightRef.current = false;
305
498
  }, []);
306
499
 
500
+ const stopCapture = useCallback(() => {
501
+ clearCaptureTimer();
502
+ captureGenerationRef.current += 1;
503
+ const pending = pendingFrameRef.current;
504
+ pendingFrameRef.current = null;
505
+ if (pending) releaseFrame(pending.frame);
506
+ // Also recovers from a camera or processing call that never settled: the
507
+ // next loop start must not stay blocked on it.
508
+ captureInFlightRef.current = false;
509
+ processingRef.current = false;
510
+ }, [clearCaptureTimer]);
511
+
307
512
  // ── Video upload pipeline (OSDKR-4) ─────────────────────────────────────
308
513
  // Everything below is best-effort: never throws into the caller; never
309
514
  // blocks liveness completion. The worst case is `videoOutcomeRef = 'failed'`
@@ -606,75 +811,117 @@ function LivenessMatchView({
606
811
  [deleteVideoTemp, onCancel, stopCapture, stopVideoRecordingIfActive, stopWatchdog],
607
812
  );
608
813
 
609
- const captureAndSend = useCallback(async () => {
610
- if (inFlightRef.current) return;
814
+ const processAndSendFrame = useCallback(
815
+ async (queued: QueuedFrame, generation: number): Promise<void> => {
816
+ processingRef.current = true;
817
+ const processStart = Date.now();
818
+ try {
819
+ const ws = wsRef.current;
820
+ if (!ws || !ws.isOpen()) {
821
+ releaseFrame(queued.frame);
822
+ return;
823
+ }
824
+ const encoded = await encodeFrame(queued.frame);
825
+ const { jpeg } = encoded;
826
+ // The loop stopped (or the socket closed) while this frame was being
827
+ // processed: drop it rather than send a frame from before the stop.
828
+ if (generation !== captureGenerationRef.current || !ws.isOpen()) return;
829
+ // DIAGNOSTIC (OSDKR-24): one line every 10th frame comparing what the
830
+ // camera captured with what went on the wire, plus how long each
831
+ // pipeline stage took. `warn` on purpose: `info` is silent in the
832
+ // release builds testers run.
833
+ if (framesSentRef.current % 10 === 0) {
834
+ void logCaptureDiagnostics({
835
+ encoded,
836
+ source: queued.frame.kind,
837
+ capturePath:
838
+ queued.frame.kind === 'file' ? queued.frame.filePath : undefined,
839
+ captureMs: queued.captureMs,
840
+ processMs: Date.now() - processStart,
841
+ challenge: currentChallengeRef.current ?? null,
842
+ });
843
+ }
844
+ const key = hmacRef.current;
845
+ if (key) {
846
+ const envelope = buildSignedFrameEnvelope({
847
+ key,
848
+ sessionId: livenessSessionIdRef.current ?? sessionId,
849
+ frameId: framesSentRef.current,
850
+ jpegBytes: jpeg.bytes,
851
+ jpegBase64: jpeg.base64,
852
+ });
853
+ ws.sendJson(envelope.json);
854
+ } else {
855
+ // Vision-camera + RN's WebSocket need an ArrayBuffer for binary frames.
856
+ // `Uint8Array.buffer` is a `SharedArrayBuffer` on some Hermes versions
857
+ // — slice into a fresh ArrayBuffer to be safe.
858
+ const ab = jpeg.bytes.buffer.slice(
859
+ jpeg.bytes.byteOffset,
860
+ jpeg.bytes.byteOffset + jpeg.bytes.byteLength,
861
+ ) as ArrayBuffer;
862
+ ws.sendBinary(ab);
863
+ }
864
+ framesSentRef.current += 1;
865
+ } catch (err) {
866
+ log.warn('[Liveness] frame send failed', err);
867
+ } finally {
868
+ // After a stop, `stopCapture` already reset the pipeline; a stale pass
869
+ // must not touch the state of the one that replaced it.
870
+ if (generation === captureGenerationRef.current) {
871
+ processingRef.current = false;
872
+ const next = pendingFrameRef.current;
873
+ if (next) {
874
+ pendingFrameRef.current = null;
875
+ void processAndSendFrame(next, generation);
876
+ }
877
+ }
878
+ }
879
+ },
880
+ [sessionId],
881
+ );
882
+
883
+ const captureTick = useCallback(async () => {
884
+ if (captureInFlightRef.current || pendingFrameRef.current) return;
885
+ if (Date.now() < captureNotBeforeRef.current) return;
611
886
  const ws = wsRef.current;
612
887
  if (!ws || !ws.isOpen()) return;
613
- inFlightRef.current = true;
888
+ const generation = captureGenerationRef.current;
889
+ captureInFlightRef.current = true;
890
+ const captureStart = Date.now();
891
+ let frame: CapturedFrame | null | undefined;
614
892
  try {
615
- const photo = await cameraRef.current?.takeFrame();
616
- if (!photo || !ws.isOpen()) return;
617
- // DO NOT add a horizontal flip here (OSDKR-23). v5 photo capture is
618
- // already un-mirrored; flipping inverted TURN_LEFT / TURN_RIGHT.
619
- const compressed = await ImageManipulator.manipulateAsync(
620
- photo.filePath.startsWith('file://')
621
- ? photo.filePath
622
- : `file://${photo.filePath}`,
623
- [{ resize: { width: FRAME_MAX_LONG_EDGE } }],
624
- { compress: FRAME_QUALITY, format: ImageManipulator.SaveFormat.JPEG },
625
- );
626
- const jpegUri = compressed.uri;
627
- if (!ws.isOpen()) return;
628
- const jpeg = await readJpegFromUri(jpegUri);
629
- // SPIKE (spike/debug-liveness-stall-1): one line every 10th frame saying
630
- // what actually went on the wire. Iris rejecting frames and Iris never
631
- // receiving usable frames look identical from here otherwise — the run
632
- // that produced 14 WARNINGs and zero CHALLENGEs could not be told apart
633
- // from a positioning problem. Pair it with the WARNING `code` now logged
634
- // by irisWebSocket: source + bytes on one side, the reason on the other.
635
- if (framesSentRef.current % 10 === 0) {
636
- log.info('[Liveness] frame on the wire', {
637
- bytes: jpeg.byteLength,
638
- challenge: currentChallengeRef.current ?? null,
639
- });
640
- }
641
- const key = hmacRef.current;
642
- if (key) {
643
- const envelope = buildSignedFrameEnvelope({
644
- key,
645
- sessionId: livenessSessionIdRef.current ?? sessionId,
646
- frameId: framesSentRef.current,
647
- jpegBytes: jpeg.bytes,
648
- jpegBase64: jpeg.base64,
649
- });
650
- ws.sendJson(envelope.json);
651
- } else {
652
- // Vision-camera + RN's WebSocket need an ArrayBuffer for binary frames.
653
- // `Uint8Array.buffer` is a `SharedArrayBuffer` on some Hermes versions
654
- // — slice into a fresh ArrayBuffer to be safe.
655
- const ab = jpeg.bytes.buffer.slice(
656
- jpeg.bytes.byteOffset,
657
- jpeg.bytes.byteOffset + jpeg.bytes.byteLength,
658
- ) as ArrayBuffer;
659
- ws.sendBinary(ab);
660
- }
661
- framesSentRef.current += 1;
893
+ frame = await cameraRef.current?.takeFrame();
662
894
  } catch (err) {
663
- log.warn('[Liveness] frame send failed', err);
895
+ log.warn('[Liveness] frame capture failed', err);
664
896
  } finally {
665
- inFlightRef.current = false;
897
+ if (generation === captureGenerationRef.current) {
898
+ captureInFlightRef.current = false;
899
+ }
900
+ }
901
+ if (!frame) return;
902
+ if (generation !== captureGenerationRef.current) {
903
+ releaseFrame(frame);
904
+ return;
905
+ }
906
+ const queued: QueuedFrame = { frame, captureMs: Date.now() - captureStart };
907
+ if (processingRef.current) {
908
+ pendingFrameRef.current = queued;
909
+ } else {
910
+ void processAndSendFrame(queued, generation);
666
911
  }
667
- }, [sessionId]);
912
+ }, [processAndSendFrame]);
668
913
 
669
914
  const startCaptureLoop = useCallback(
670
915
  (ms: number) => {
671
- stopCapture();
916
+ // Only the pacing changes here: frames already in the pipeline keep
917
+ // going, so a challenge switching the fps does not cost a frame.
918
+ clearCaptureTimer();
672
919
  frameMsRef.current = ms;
673
920
  captureTimerRef.current = setInterval(() => {
674
- void captureAndSend();
921
+ void captureTick();
675
922
  }, ms);
676
923
  },
677
- [captureAndSend, stopCapture],
924
+ [captureTick, clearCaptureTimer],
678
925
  );
679
926
 
680
927
  const adjustFps = useCallback(
@@ -748,6 +995,7 @@ function LivenessMatchView({
748
995
  // failed before the user understood what was being asked.
749
996
  setChallengeText(t.liveness.centerFace);
750
997
  showToast('success', t.liveness.getReady);
998
+ captureNotBeforeRef.current = Date.now() + CAPTURE_WARMUP_MS;
751
999
  startCaptureLoop(FRAME_MS_DEFAULT);
752
1000
  // Do not start the evidence recorder yet. MiniFAS and positioning
753
1001
  // run on these first seconds; a photo+video session on Android
@@ -775,7 +1023,9 @@ function LivenessMatchView({
775
1023
  adjustFps(
776
1024
  ch === 'TURN_LEFT' || ch === 'TURN_RIGHT'
777
1025
  ? FRAME_MS_TURN
778
- : FRAME_MS_DEFAULT,
1026
+ : ch === 'BLINK'
1027
+ ? FRAME_MS_BLINK
1028
+ : FRAME_MS_DEFAULT,
779
1029
  );
780
1030
  setChallengeText(challengeLabel(ch));
781
1031
  // Map distance challenges to an oval-size cue so the viewfinder
@@ -850,11 +1100,23 @@ function LivenessMatchView({
850
1100
  // bar, neutral oval, persistent hint — and let the SAME challenge
851
1101
  // re-arrive on its own once the user is well positioned again.
852
1102
  if (msg.code === 'CHALLENGE_REPOSITION') {
1103
+ const abortedChallenge = currentChallengeRef.current;
853
1104
  currentChallengeRef.current = null;
854
1105
  adjustFps(FRAME_MS_DEFAULT);
855
1106
  setProgress(null);
856
1107
  setOvalCue('NEUTRAL');
857
- const repositionText = msg.message ?? t.liveness.repositioning;
1108
+ // A MOVE_FARTHER abort is the one case where the orchestrator's
1109
+ // generic "center your face and improve the lighting" sends the
1110
+ // user the wrong way (OSDKR-24). Moving away shrinks the face, and
1111
+ // with it the sharpness Iris measures, until the mid-challenge
1112
+ // check aborts; the user then has to come back closer before
1113
+ // positioning reopens. On an Edge 30 Neo they stayed at the far
1114
+ // distance for 43 s. Our own copy overrides the server text here
1115
+ // until IRIS-31 stops that abort at the source.
1116
+ const repositionText =
1117
+ abortedChallenge === 'MOVE_FARTHER'
1118
+ ? t.liveness.repositioningAfterMoveFarther
1119
+ : msg.message ?? t.liveness.repositioning;
858
1120
  setChallengeText(repositionText);
859
1121
  showToast('warning', repositionText);
860
1122
  resetWatchdog();