biometry-react-components 1.4.0 → 2.0.0
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.
- package/README.md +66 -28
- package/dist/index.d.ts +123 -5
- package/dist/index.esm.js +455 -144
- package/dist/index.js +455 -144
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1,19 +1,60 @@
|
|
|
1
1
|
'use strict';var jsxRuntime=require('react/jsx-runtime'),React=require('react');const DocScan = ({ rectWidth = 640, rectHeight = 400, onConfirmCapture, onCapture, className, style, noShadow = false, }) => {
|
|
2
|
+
var _a;
|
|
2
3
|
const [isConfirming, setIsConfirming] = React.useState(false);
|
|
3
4
|
const [capturedUrl, setCapturedUrl] = React.useState(null);
|
|
4
5
|
const [capturedFile, setCapturedFile] = React.useState(null);
|
|
5
6
|
const videoRef = React.useRef(null);
|
|
6
7
|
const streamRef = React.useRef(null);
|
|
8
|
+
const [devices, setDevices] = React.useState([]);
|
|
9
|
+
const [selectedDeviceId, setSelectedDeviceId] = React.useState(null);
|
|
10
|
+
const [isFrontCamera, setIsFrontCamera] = React.useState(false);
|
|
11
|
+
const [capabilities, setCapabilities] = React.useState({});
|
|
12
|
+
const [torchOn, setTorchOn] = React.useState(false);
|
|
7
13
|
const QUALITY_MULTIPLIER = 3;
|
|
8
14
|
// Crop area (dashed area) as percentages
|
|
9
15
|
const cropTop = 0.075, cropLeft = 0.075, cropWidth = 0.85, cropHeight = 0.85;
|
|
16
|
+
const getDevices = async () => {
|
|
17
|
+
try {
|
|
18
|
+
const all = await navigator.mediaDevices.enumerateDevices();
|
|
19
|
+
return all.filter((d) => d.kind === "videoinput");
|
|
20
|
+
}
|
|
21
|
+
catch (_a) {
|
|
22
|
+
return [];
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
React.useEffect(() => {
|
|
26
|
+
const handleDeviceChange = async () => {
|
|
27
|
+
const vids = await getDevices();
|
|
28
|
+
setDevices(vids);
|
|
29
|
+
if (selectedDeviceId && !vids.find((d) => d.deviceId === selectedDeviceId)) {
|
|
30
|
+
// Active device disconnected, clear it to fallback to default
|
|
31
|
+
setSelectedDeviceId(null);
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
navigator.mediaDevices.addEventListener("devicechange", handleDeviceChange);
|
|
35
|
+
return () => navigator.mediaDevices.removeEventListener("devicechange", handleDeviceChange);
|
|
36
|
+
}, [selectedDeviceId]);
|
|
10
37
|
React.useEffect(() => {
|
|
11
38
|
let isMounted = true;
|
|
12
39
|
if (!isConfirming) {
|
|
13
40
|
(async () => {
|
|
14
41
|
try {
|
|
15
|
-
const
|
|
16
|
-
|
|
42
|
+
const videoConstraints = {
|
|
43
|
+
width: rectWidth,
|
|
44
|
+
height: rectHeight,
|
|
45
|
+
};
|
|
46
|
+
if (selectedDeviceId) {
|
|
47
|
+
videoConstraints.deviceId = { exact: selectedDeviceId };
|
|
48
|
+
}
|
|
49
|
+
else {
|
|
50
|
+
videoConstraints.facingMode = "environment";
|
|
51
|
+
}
|
|
52
|
+
const stream = await navigator.mediaDevices.getUserMedia({ video: videoConstraints });
|
|
53
|
+
if (!isMounted) {
|
|
54
|
+
stream.getTracks().forEach((track) => track.stop());
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
if (videoRef.current) {
|
|
17
58
|
videoRef.current.srcObject = stream;
|
|
18
59
|
videoRef.current.onloadedmetadata = () => {
|
|
19
60
|
var _a;
|
|
@@ -21,6 +62,40 @@
|
|
|
21
62
|
};
|
|
22
63
|
}
|
|
23
64
|
streamRef.current = stream;
|
|
65
|
+
const track = stream.getVideoTracks()[0];
|
|
66
|
+
if (track) {
|
|
67
|
+
if (track.getCapabilities) {
|
|
68
|
+
setCapabilities(track.getCapabilities());
|
|
69
|
+
}
|
|
70
|
+
const settings = track.getSettings();
|
|
71
|
+
let isFront = false;
|
|
72
|
+
if (settings.facingMode) {
|
|
73
|
+
isFront = settings.facingMode === "user";
|
|
74
|
+
}
|
|
75
|
+
else {
|
|
76
|
+
const allVids = await getDevices();
|
|
77
|
+
const currDev = allVids.find(d => d.deviceId === settings.deviceId);
|
|
78
|
+
if (currDev)
|
|
79
|
+
isFront = currDev.label.toLowerCase().includes("front");
|
|
80
|
+
}
|
|
81
|
+
if (isMounted)
|
|
82
|
+
setIsFrontCamera(isFront);
|
|
83
|
+
if (!selectedDeviceId) {
|
|
84
|
+
const currentId = track.getSettings().deviceId;
|
|
85
|
+
if (currentId && isMounted)
|
|
86
|
+
setSelectedDeviceId(currentId);
|
|
87
|
+
}
|
|
88
|
+
track.onended = async () => {
|
|
89
|
+
if (!isMounted)
|
|
90
|
+
return;
|
|
91
|
+
const vids = await getDevices();
|
|
92
|
+
setDevices(vids);
|
|
93
|
+
setSelectedDeviceId(null);
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
const vids = await getDevices();
|
|
97
|
+
if (isMounted)
|
|
98
|
+
setDevices(vids);
|
|
24
99
|
}
|
|
25
100
|
catch (err) {
|
|
26
101
|
console.error(err);
|
|
@@ -34,7 +109,7 @@
|
|
|
34
109
|
streamRef.current = null;
|
|
35
110
|
}
|
|
36
111
|
};
|
|
37
|
-
}, [isConfirming, rectWidth, rectHeight]);
|
|
112
|
+
}, [isConfirming, rectWidth, rectHeight, selectedDeviceId]);
|
|
38
113
|
const handleCapture = () => {
|
|
39
114
|
const video = videoRef.current;
|
|
40
115
|
if (!video)
|
|
@@ -49,18 +124,20 @@
|
|
|
49
124
|
const targetAspect = rectWidth / rectHeight;
|
|
50
125
|
let sx = 0, sy = 0, sw = video.videoWidth, sh = video.videoHeight;
|
|
51
126
|
if (videoAspect > targetAspect) {
|
|
52
|
-
// Video is wider than target: crop sides
|
|
53
127
|
sw = video.videoHeight * targetAspect;
|
|
54
128
|
sx = (video.videoWidth - sw) / 2;
|
|
55
129
|
}
|
|
56
130
|
else {
|
|
57
|
-
// Video is taller than target: crop top/bottom
|
|
58
131
|
sh = video.videoWidth / targetAspect;
|
|
59
132
|
sy = (video.videoHeight - sh) / 2;
|
|
60
133
|
}
|
|
61
134
|
ctx.drawImage(video, sx, sy, sw, sh, 0, 0, canvas.width, canvas.height);
|
|
62
135
|
canvas.toBlob((blob) => {
|
|
63
136
|
if (blob) {
|
|
137
|
+
if (streamRef.current) {
|
|
138
|
+
streamRef.current.getTracks().forEach((track) => track.stop());
|
|
139
|
+
streamRef.current = null;
|
|
140
|
+
}
|
|
64
141
|
const file = new File([blob], "document.jpg", { type: "image/jpeg" });
|
|
65
142
|
setCapturedFile(file);
|
|
66
143
|
setCapturedUrl(URL.createObjectURL(blob));
|
|
@@ -70,7 +147,7 @@
|
|
|
70
147
|
onCapture(file);
|
|
71
148
|
}
|
|
72
149
|
catch (err) {
|
|
73
|
-
console.error(
|
|
150
|
+
console.error(err);
|
|
74
151
|
}
|
|
75
152
|
}
|
|
76
153
|
}
|
|
@@ -90,6 +167,53 @@
|
|
|
90
167
|
setCapturedFile(null);
|
|
91
168
|
setIsConfirming(false);
|
|
92
169
|
};
|
|
170
|
+
const cycleCamera = (e) => {
|
|
171
|
+
e.stopPropagation();
|
|
172
|
+
if (devices.length <= 1)
|
|
173
|
+
return;
|
|
174
|
+
const currentIndex = devices.findIndex((d) => d.deviceId === selectedDeviceId);
|
|
175
|
+
const nextIndex = (currentIndex + 1) % devices.length;
|
|
176
|
+
setSelectedDeviceId(devices[nextIndex].deviceId);
|
|
177
|
+
setTorchOn(false);
|
|
178
|
+
};
|
|
179
|
+
const toggleTorch = async (e) => {
|
|
180
|
+
e.stopPropagation();
|
|
181
|
+
if (!streamRef.current)
|
|
182
|
+
return;
|
|
183
|
+
const track = streamRef.current.getVideoTracks()[0];
|
|
184
|
+
if (track && capabilities.torch) {
|
|
185
|
+
try {
|
|
186
|
+
const nextTorch = !torchOn;
|
|
187
|
+
await track.applyConstraints({ advanced: [{ torch: nextTorch }] });
|
|
188
|
+
setTorchOn(nextTorch);
|
|
189
|
+
}
|
|
190
|
+
catch (err) {
|
|
191
|
+
console.error("Torch toggle failed:", err);
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
};
|
|
195
|
+
const handleCameraAreaClick = async (e) => {
|
|
196
|
+
if (!streamRef.current)
|
|
197
|
+
return;
|
|
198
|
+
const track = streamRef.current.getVideoTracks()[0];
|
|
199
|
+
if (track && capabilities.focusMode) {
|
|
200
|
+
const focusType = capabilities.focusMode.includes("single-shot") ? "single-shot" : capabilities.focusMode.includes("manual") ? "manual" : null;
|
|
201
|
+
if (!focusType)
|
|
202
|
+
return;
|
|
203
|
+
const rect = e.currentTarget.getBoundingClientRect();
|
|
204
|
+
let x = (e.clientX - rect.left) / rect.width;
|
|
205
|
+
const y = (e.clientY - rect.top) / rect.height;
|
|
206
|
+
if (isFrontCamera) {
|
|
207
|
+
x = 1 - x;
|
|
208
|
+
}
|
|
209
|
+
try {
|
|
210
|
+
await track.applyConstraints({ advanced: [{ focusMode: focusType, pointsOfInterest: [{ x, y }] }] });
|
|
211
|
+
}
|
|
212
|
+
catch (err) {
|
|
213
|
+
console.error("Focus failed:", err);
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
};
|
|
93
217
|
const wrapperStyle = {
|
|
94
218
|
background: '#fff',
|
|
95
219
|
borderRadius: noShadow ? 0 : 10,
|
|
@@ -112,6 +236,7 @@
|
|
|
112
236
|
display: 'flex',
|
|
113
237
|
alignItems: 'center',
|
|
114
238
|
justifyContent: 'center',
|
|
239
|
+
cursor: ((_a = capabilities.focusMode) === null || _a === void 0 ? void 0 : _a.includes("manual")) ? "crosshair" : "default"
|
|
115
240
|
};
|
|
116
241
|
const defaultDashedArea = {
|
|
117
242
|
position: 'absolute',
|
|
@@ -127,6 +252,7 @@
|
|
|
127
252
|
display: 'flex',
|
|
128
253
|
flexDirection: 'column',
|
|
129
254
|
alignItems: 'center',
|
|
255
|
+
pointerEvents: 'none'
|
|
130
256
|
};
|
|
131
257
|
const defaultGuidanceText = {
|
|
132
258
|
color: '#fff',
|
|
@@ -178,13 +304,34 @@
|
|
|
178
304
|
transition: 'background 0.2s',
|
|
179
305
|
margin: '0 8px',
|
|
180
306
|
};
|
|
181
|
-
|
|
307
|
+
const controlsOverlayStyle = {
|
|
308
|
+
position: 'absolute',
|
|
309
|
+
top: 8,
|
|
310
|
+
right: 8,
|
|
311
|
+
zIndex: 3,
|
|
312
|
+
display: 'flex',
|
|
313
|
+
flexDirection: 'column',
|
|
314
|
+
gap: 12,
|
|
315
|
+
};
|
|
316
|
+
const overlayBtnStyle = {
|
|
317
|
+
background: 'rgba(0,0,0,0.5)',
|
|
318
|
+
border: 'none',
|
|
319
|
+
borderRadius: '50%',
|
|
320
|
+
width: 36,
|
|
321
|
+
height: 36,
|
|
322
|
+
display: 'flex',
|
|
323
|
+
alignItems: 'center',
|
|
324
|
+
justifyContent: 'center',
|
|
325
|
+
cursor: 'pointer',
|
|
326
|
+
color: '#fff',
|
|
327
|
+
};
|
|
328
|
+
return (jsxRuntime.jsx("div", { className: className, style: wrapperStyle, children: !isConfirming ? (jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [jsxRuntime.jsxs("div", { className: "docscan-camera-area", style: defaultCameraArea, onClick: handleCameraAreaClick, children: [jsxRuntime.jsxs("div", { style: controlsOverlayStyle, children: [devices.length > 1 && (jsxRuntime.jsx("button", { type: "button", onClick: cycleCamera, style: overlayBtnStyle, title: "Switch Camera", children: jsxRuntime.jsxs("svg", { width: "20", height: "20", viewBox: "0 0 24 24", fill: "none", stroke: "currentColor", strokeWidth: "2", strokeLinecap: "round", strokeLinejoin: "round", children: [jsxRuntime.jsx("path", { d: "M21 12A9 9 0 0 0 6 5.3L3 8" }), jsxRuntime.jsx("path", { d: "M21 3v5h-5" }), jsxRuntime.jsx("path", { d: "M3 12a9 9 0 0 0 15 6.7l3-2.7" }), jsxRuntime.jsx("path", { d: "M3 21v-5h5" })] }) })), capabilities.torch && (jsxRuntime.jsx("button", { type: "button", onClick: toggleTorch, style: { ...overlayBtnStyle, color: torchOn ? '#ffd700' : '#fff' }, title: "Toggle Flash", children: jsxRuntime.jsx("svg", { width: "20", height: "20", viewBox: "0 0 24 24", fill: "none", stroke: "currentColor", strokeWidth: "2", strokeLinecap: "round", strokeLinejoin: "round", children: jsxRuntime.jsx("polygon", { points: "13 2 3 14 12 14 11 22 21 10 12 10 13 2" }) }) }))] }), jsxRuntime.jsx("video", { ref: videoRef, width: rectWidth - 40, height: rectHeight - 40, className: "docscan-video", style: {
|
|
182
329
|
width: rectWidth - 40,
|
|
183
330
|
height: rectHeight - 40,
|
|
184
331
|
objectFit: 'cover',
|
|
185
332
|
borderRadius: noShadow ? 0 : 8,
|
|
186
333
|
zIndex: 0,
|
|
187
|
-
transform: 'scaleX(-1)'
|
|
334
|
+
transform: isFrontCamera ? 'scaleX(-1)' : 'none'
|
|
188
335
|
}, playsInline: true, muted: true, autoPlay: true }), jsxRuntime.jsx("div", { className: "docscan-dark-overlay", style: {
|
|
189
336
|
position: 'absolute',
|
|
190
337
|
top: 0,
|
|
@@ -194,6 +341,7 @@
|
|
|
194
341
|
background: 'rgba(0,0,0,0.4)',
|
|
195
342
|
zIndex: 1,
|
|
196
343
|
borderRadius: noShadow ? 0 : 8,
|
|
344
|
+
pointerEvents: 'none'
|
|
197
345
|
} }), jsxRuntime.jsx("div", { className: "docscan-dashed-area", style: defaultDashedArea, children: jsxRuntime.jsx("span", { className: "docscan-guidance-text", style: defaultGuidanceText, children: "Place your document here" }) })] }), jsxRuntime.jsx("button", { type: "button", className: "docscan-capture-btn", style: defaultCaptureButton, onClick: handleCapture, children: "Capture" })] })) : (jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [jsxRuntime.jsx("div", { className: "docscan-preview", style: defaultPreview, children: capturedUrl && (jsxRuntime.jsx("img", { src: capturedUrl, alt: "Captured document", className: "docscan-preview-img", style: { maxWidth: '100%', maxHeight: '100%', borderRadius: 8 } })) }), jsxRuntime.jsx("div", { className: "docscan-confirm-text", style: defaultConfirmText, children: "Do you want to use this photo?" }), jsxRuntime.jsxs("div", { className: "docscan-confirm-btns", style: { display: 'flex', justifyContent: 'center', gap: 32, marginTop: 16, width: '100%' }, children: [jsxRuntime.jsx("button", { type: "button", "aria-label": "Retake", className: "docscan-icon-btn", style: defaultIconButton, onClick: handleDecline, onMouseOver: e => (e.currentTarget.style.background = 'rgba(211,47,47,0.12)'), onMouseOut: e => (e.currentTarget.style.background = 'none'), children: jsxRuntime.jsxs("svg", { width: "36", height: "36", viewBox: "0 0 36 36", fill: "none", xmlns: "http://www.w3.org/2000/svg", children: [jsxRuntime.jsx("circle", { cx: "18", cy: "18", r: "18", fill: "#fff", fillOpacity: "0.15" }), jsxRuntime.jsx("path", { d: "M12 12L24 24", stroke: "#d32f2f", strokeWidth: "3", strokeLinecap: "round" }), jsxRuntime.jsx("path", { d: "M24 12L12 24", stroke: "#d32f2f", strokeWidth: "3", strokeLinecap: "round" })] }) }), jsxRuntime.jsx("button", { type: "button", "aria-label": "Confirm", className: "docscan-icon-btn", style: defaultIconButton, onClick: handleConfirm, onMouseOver: e => (e.currentTarget.style.background = 'rgba(56,142,60,0.12)'), onMouseOut: e => (e.currentTarget.style.background = 'none'), children: jsxRuntime.jsxs("svg", { width: "36", height: "36", viewBox: "0 0 36 36", fill: "none", xmlns: "http://www.w3.org/2000/svg", children: [jsxRuntime.jsx("circle", { cx: "18", cy: "18", r: "18", fill: "#fff", fillOpacity: "0.15" }), jsxRuntime.jsx("path", { d: "M10 19L16 25L26 13", stroke: "#388e3c", strokeWidth: "3", strokeLinecap: "round", strokeLinejoin: "round" })] }) })] })] })) }));
|
|
198
346
|
};const FaceCapture = ({ rectWidth = 360, rectHeight = 576, onConfirmCapture, onCapture, className, style, noShadow = false, }) => {
|
|
199
347
|
const [isConfirming, setIsConfirming] = React.useState(false);
|
|
@@ -208,7 +356,11 @@
|
|
|
208
356
|
(async () => {
|
|
209
357
|
try {
|
|
210
358
|
const stream = await navigator.mediaDevices.getUserMedia({ video: { width: rectWidth, height: rectHeight } });
|
|
211
|
-
if (
|
|
359
|
+
if (!isMounted) {
|
|
360
|
+
stream.getTracks().forEach((track) => track.stop());
|
|
361
|
+
return;
|
|
362
|
+
}
|
|
363
|
+
if (videoRef.current) {
|
|
212
364
|
videoRef.current.srcObject = stream;
|
|
213
365
|
videoRef.current.onloadedmetadata = () => {
|
|
214
366
|
var _a;
|
|
@@ -242,6 +394,11 @@
|
|
|
242
394
|
ctx.drawImage(video, 0, 0, rectWidth, rectHeight, 0, 0, rectWidth * QUALITY_MULTIPLIER, rectHeight * QUALITY_MULTIPLIER);
|
|
243
395
|
canvas.toBlob((blob) => {
|
|
244
396
|
if (blob) {
|
|
397
|
+
// Stop the stream immediately — don't wait for useEffect cleanup
|
|
398
|
+
if (streamRef.current) {
|
|
399
|
+
streamRef.current.getTracks().forEach((track) => track.stop());
|
|
400
|
+
streamRef.current = null;
|
|
401
|
+
}
|
|
245
402
|
const file = new File([blob], "face.jpg", { type: "image/jpeg" });
|
|
246
403
|
setCapturedFile(file);
|
|
247
404
|
setCapturedUrl(URL.createObjectURL(blob));
|
|
@@ -7058,7 +7215,78 @@ function requireRecordRTC () {
|
|
|
7058
7215
|
} (RecordRTC$1));
|
|
7059
7216
|
return RecordRTC$1.exports;
|
|
7060
7217
|
}var RecordRTCExports = /*@__PURE__*/ requireRecordRTC();
|
|
7061
|
-
var RecordRTC = /*@__PURE__*/getDefaultExportFromCjs(RecordRTCExports)
|
|
7218
|
+
var RecordRTC = /*@__PURE__*/getDefaultExportFromCjs(RecordRTCExports);/**
|
|
7219
|
+
* Digit challenge and phrase helpers, kept in step with
|
|
7220
|
+
* biometry-angular-components `src/lib/utils/numbers.ts`. The two packages must
|
|
7221
|
+
* produce the same `phrase` for the same digits: it is compared against what the
|
|
7222
|
+
* voice engine recognised, so a divergence here is a verification failure with no
|
|
7223
|
+
* obvious cause.
|
|
7224
|
+
*/
|
|
7225
|
+
/**
|
|
7226
|
+
* Digits a generated challenge may contain.
|
|
7227
|
+
*
|
|
7228
|
+
* `0` is deliberately absent. A server-issued challenge may still contain one
|
|
7229
|
+
* while the backend rolls out, and numbersToPhrase tolerates that — but nothing
|
|
7230
|
+
* we generate emits it.
|
|
7231
|
+
*/
|
|
7232
|
+
const CHALLENGE_DIGITS = [1, 2, 3, 4, 5, 6, 7, 8, 9];
|
|
7233
|
+
/**
|
|
7234
|
+
* Enrolment prompts, taken verbatim from the vendor guide's published set.
|
|
7235
|
+
*
|
|
7236
|
+
* These are fixed, not random, and that is the point: random prompting is only
|
|
7237
|
+
* required at verification time. Presenting every user the same sequences is
|
|
7238
|
+
* also what makes cross-user calibration accurate — and calibration is what
|
|
7239
|
+
* sets our acceptance threshold (BM-449).
|
|
7240
|
+
*
|
|
7241
|
+
* The guide selects these specifically because they contain no duplicate
|
|
7242
|
+
* 2-digit contexts: biometric performance depends on co-articulation between
|
|
7243
|
+
* adjacent digits, and "321" and "123" are different contexts for "2". Drawing
|
|
7244
|
+
* enrolment prompts at random gives each user an arbitrary, unmeasured slice of
|
|
7245
|
+
* that space.
|
|
7246
|
+
*
|
|
7247
|
+
* Order matters: the first two are count-up and count-down, which the guide
|
|
7248
|
+
* uses to open its enrolment flow because they are the easiest to comply with.
|
|
7249
|
+
*/
|
|
7250
|
+
const ENROLMENT_DIGIT_SEQUENCES = [
|
|
7251
|
+
[1, 2, 3, 4, 5, 6, 7, 8, 9],
|
|
7252
|
+
[9, 8, 7, 6, 5, 4, 3, 2, 1],
|
|
7253
|
+
[7, 1, 5, 2, 6, 3, 8, 4, 9],
|
|
7254
|
+
[6, 1, 8, 2, 9, 3, 5, 7, 4],
|
|
7255
|
+
[4, 7, 5, 9, 1, 6, 2, 8, 3],
|
|
7256
|
+
[5, 1, 9, 2, 7, 3, 6, 4, 8],
|
|
7257
|
+
[8, 1, 7, 2, 5, 3, 9, 4, 6],
|
|
7258
|
+
[2, 4, 1, 3, 7, 9, 6, 8, 5],
|
|
7259
|
+
];
|
|
7260
|
+
/**
|
|
7261
|
+
* Render digits as the spoken words the engine expects in `phrase`.
|
|
7262
|
+
*
|
|
7263
|
+
* Lowercase, matching the Angular package and therefore the only casing that
|
|
7264
|
+
* has been exercised in production. The gateway forwards `phrase` to the engine
|
|
7265
|
+
* untouched, so the client's casing is what gets compared.
|
|
7266
|
+
*
|
|
7267
|
+
* `zero` is retained even though we no longer generate it: a server-issued
|
|
7268
|
+
* challenge may contain a 0, and rendering it as "0" would send a phrase the
|
|
7269
|
+
* engine cannot match at all. A digit outside 0–9 is passed through as-is
|
|
7270
|
+
* rather than silently becoming a word that was never displayed.
|
|
7271
|
+
*/
|
|
7272
|
+
function numbersToPhrase(digits) {
|
|
7273
|
+
const words = ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine'];
|
|
7274
|
+
return digits.map(d => (d >= 0 && d <= 9 ? words[d] : String(d))).join(' ');
|
|
7275
|
+
}
|
|
7276
|
+
/**
|
|
7277
|
+
* `count` distinct digits drawn from CHALLENGE_DIGITS, in random order.
|
|
7278
|
+
*
|
|
7279
|
+
* Used for the face recorder's fallback challenge when the server issued none.
|
|
7280
|
+
* Enrolment does not call this — see ENROLMENT_DIGIT_SEQUENCES.
|
|
7281
|
+
*/
|
|
7282
|
+
function generateRandomDigits(count) {
|
|
7283
|
+
const digits = [...CHALLENGE_DIGITS];
|
|
7284
|
+
for (let i = digits.length - 1; i > 0; i--) {
|
|
7285
|
+
const j = Math.floor(Math.random() * (i + 1));
|
|
7286
|
+
[digits[i], digits[j]] = [digits[j], digits[i]];
|
|
7287
|
+
}
|
|
7288
|
+
return digits.slice(0, count);
|
|
7289
|
+
}function getSupportedMimeType() {
|
|
7062
7290
|
if (MediaRecorder.isTypeSupported('video/webm;codecs=vp9')) {
|
|
7063
7291
|
return 'video/webm;codecs=vp9';
|
|
7064
7292
|
}
|
|
@@ -7071,13 +7299,6 @@ var RecordRTC = /*@__PURE__*/getDefaultExportFromCjs(RecordRTCExports);function
|
|
|
7071
7299
|
else {
|
|
7072
7300
|
console.error("no suitable mimetype found for this device");
|
|
7073
7301
|
}
|
|
7074
|
-
}
|
|
7075
|
-
function numberToWord(num) {
|
|
7076
|
-
const words = ['ZERO', 'ONE', 'TWO', 'THREE', 'FOUR', 'FIVE', 'SIX', 'SEVEN', 'EIGHT', 'NINE'];
|
|
7077
|
-
return words[num] || 'ZERO';
|
|
7078
|
-
}
|
|
7079
|
-
function numbersToPhrase(numbers) {
|
|
7080
|
-
return numbers.map(numberToWord).join(' ');
|
|
7081
7302
|
}const VIDEO_LENGTH_MS = 7000;
|
|
7082
7303
|
/**
|
|
7083
7304
|
* Voice *enrollment* reads all ten digits aloud, which does not fit in the
|
|
@@ -7184,11 +7405,17 @@ const useRecorder = (stream, type, onStopRecording, lengthMs = VIDEO_LENGTH_MS)
|
|
|
7184
7405
|
const [permission, setPermission] = React.useState(false);
|
|
7185
7406
|
const [stream, setStream] = React.useState(null);
|
|
7186
7407
|
const [isLoading, setIsLoading] = React.useState(true);
|
|
7408
|
+
// The stream is mirrored in a ref so stopStreamTracks and the unmount
|
|
7409
|
+
// cleanup act on the live stream rather than a stale closure, and so a
|
|
7410
|
+
// getUserMedia that resolves after unmount can be stopped immediately
|
|
7411
|
+
// (restored from 1.3.5, which 1.4.0 reverted).
|
|
7412
|
+
const streamRef = React.useRef(null);
|
|
7413
|
+
const isMountedRef = React.useRef(false);
|
|
7187
7414
|
const stopStreamTracks = () => {
|
|
7188
|
-
|
|
7189
|
-
|
|
7190
|
-
|
|
7191
|
-
|
|
7415
|
+
const currentStream = streamRef.current;
|
|
7416
|
+
if (currentStream) {
|
|
7417
|
+
currentStream.getTracks().forEach(track => track.stop());
|
|
7418
|
+
streamRef.current = null;
|
|
7192
7419
|
setStream(null);
|
|
7193
7420
|
}
|
|
7194
7421
|
};
|
|
@@ -7204,7 +7431,7 @@ const useRecorder = (stream, type, onStopRecording, lengthMs = VIDEO_LENGTH_MS)
|
|
|
7204
7431
|
noiseSuppression: true,
|
|
7205
7432
|
autoGainControl: true,
|
|
7206
7433
|
sampleRate: 48000,
|
|
7207
|
-
channelCount:
|
|
7434
|
+
channelCount: 1 // Mono: the gateway downmixes with -ac 1 and the engine expects mono (BM-446)
|
|
7208
7435
|
},
|
|
7209
7436
|
video: {
|
|
7210
7437
|
width: { ideal: Math.min(rectWidth, 1920), min: 1280 },
|
|
@@ -7258,19 +7485,36 @@ const useRecorder = (stream, type, onStopRecording, lengthMs = VIDEO_LENGTH_MS)
|
|
|
7258
7485
|
const constraints = getAdaptiveConstraints();
|
|
7259
7486
|
const streamData = await navigator.mediaDevices.getUserMedia(constraints);
|
|
7260
7487
|
console.log('Media stream obtained, active:', streamData.active);
|
|
7488
|
+
// Unmounted while getUserMedia was resolving: release the devices now,
|
|
7489
|
+
// otherwise the camera/mic indicator stays on with nothing holding it.
|
|
7490
|
+
if (!isMountedRef.current) {
|
|
7491
|
+
streamData.getTracks().forEach(track => track.stop());
|
|
7492
|
+
return;
|
|
7493
|
+
}
|
|
7494
|
+
streamRef.current = streamData;
|
|
7261
7495
|
setPermission(true);
|
|
7262
7496
|
setStream(streamData);
|
|
7263
7497
|
}
|
|
7264
7498
|
catch (err) {
|
|
7265
7499
|
console.error('Camera/mic permission denied:', err);
|
|
7266
|
-
|
|
7500
|
+
if (isMountedRef.current)
|
|
7501
|
+
setPermission(false);
|
|
7267
7502
|
}
|
|
7268
7503
|
finally {
|
|
7269
|
-
|
|
7504
|
+
if (isMountedRef.current)
|
|
7505
|
+
setIsLoading(false);
|
|
7270
7506
|
}
|
|
7271
7507
|
};
|
|
7272
7508
|
React.useEffect(() => {
|
|
7509
|
+
isMountedRef.current = true;
|
|
7273
7510
|
void requestPermissions();
|
|
7511
|
+
return () => {
|
|
7512
|
+
isMountedRef.current = false;
|
|
7513
|
+
if (streamRef.current) {
|
|
7514
|
+
streamRef.current.getTracks().forEach(track => track.stop());
|
|
7515
|
+
streamRef.current = null;
|
|
7516
|
+
}
|
|
7517
|
+
};
|
|
7274
7518
|
}, [rectWidth, rectHeight]);
|
|
7275
7519
|
return { permission, stream, stopStreamTracks, requestPermissions, isLoading };
|
|
7276
7520
|
};// spinner animation
|
|
@@ -7291,22 +7535,14 @@ if (typeof window !== 'undefined' && typeof console !== 'undefined') {
|
|
|
7291
7535
|
console.log('MediaRecorder supported:', typeof MediaRecorder !== 'undefined');
|
|
7292
7536
|
console.log('getUserMedia supported:', !!(navigator.mediaDevices && navigator.mediaDevices.getUserMedia));
|
|
7293
7537
|
}
|
|
7294
|
-
const
|
|
7295
|
-
const
|
|
7538
|
+
const DEFAULT_RECORD_SECONDS = VIDEO_LENGTH_MS / 1000;
|
|
7539
|
+
const DEFAULT_PREP_SECONDS = 3;
|
|
7296
7540
|
const DIGITS_NUMBER$1 = 7;
|
|
7297
|
-
|
|
7298
|
-
const digits = Array.from({ length: 10 }, (_, i) => i);
|
|
7299
|
-
for (let i = digits.length - 1; i > 0; i--) {
|
|
7300
|
-
const j = Math.floor(Math.random() * (i + 1));
|
|
7301
|
-
[digits[i], digits[j]] = [digits[j], digits[i]];
|
|
7302
|
-
}
|
|
7303
|
-
return digits.slice(0, count);
|
|
7304
|
-
}
|
|
7305
|
-
const FaceRecorder = ({ rectWidth = 360, rectHeight = 576, onConfirmRecording, onRecorded, className, style, noShadow = false, }) => {
|
|
7541
|
+
const FaceRecorder = ({ rectWidth = 360, rectHeight = 576, challengeDigits, recordingSeconds = DEFAULT_RECORD_SECONDS, countdownSeconds = DEFAULT_PREP_SECONDS, emitAudio = false, onConfirmRecording, onRecorded, className, style, noShadow = false, }) => {
|
|
7306
7542
|
const [state, setState] = React.useState('preparation');
|
|
7307
|
-
const [prepTimer, setPrepTimer] = React.useState(
|
|
7308
|
-
const [recordTimer, setRecordTimer] = React.useState(
|
|
7309
|
-
const [digits, setDigits] = React.useState(generateRandomDigits
|
|
7543
|
+
const [prepTimer, setPrepTimer] = React.useState(countdownSeconds);
|
|
7544
|
+
const [recordTimer, setRecordTimer] = React.useState(recordingSeconds);
|
|
7545
|
+
const [digits, setDigits] = React.useState(generateRandomDigits(DIGITS_NUMBER$1));
|
|
7310
7546
|
const { permission, stream, stopStreamTracks, requestPermissions, isLoading } = usePermissions({ rectWidth, rectHeight });
|
|
7311
7547
|
const [videoBlob, setVideoBlob] = React.useState(null);
|
|
7312
7548
|
const [audioBlob, setAudioBlob] = React.useState(null);
|
|
@@ -7319,47 +7555,39 @@ const FaceRecorder = ({ rectWidth = 360, rectHeight = 576, onConfirmRecording, o
|
|
|
7319
7555
|
const analyserRef = React.useRef(null);
|
|
7320
7556
|
const audioCtxRef = React.useRef(null);
|
|
7321
7557
|
const rafRef = React.useRef(null);
|
|
7322
|
-
// Video recorder
|
|
7558
|
+
// Video recorder. This callback owns the transition to the review screen:
|
|
7559
|
+
// it used to live in the audio recorder's callback, which meant the component
|
|
7560
|
+
// could not reach 'result' at all unless a second recording existed (BM-461).
|
|
7561
|
+
const recordingMs = Math.max(1, Math.round(recordingSeconds * 1000));
|
|
7323
7562
|
const { startRecording: startVideoRecording, cancelRecording: cancelVideoRecording } = useRecorder(stream, "video", (blob) => {
|
|
7324
|
-
if (blob && blob.size > 0) {
|
|
7325
|
-
setVideoBlob(blob);
|
|
7326
|
-
const url = URL.createObjectURL(blob);
|
|
7327
|
-
setVideoUrl(url);
|
|
7328
|
-
}
|
|
7329
|
-
else {
|
|
7330
|
-
const dummyBlob = new Blob(['dummy video data'], { type: 'video/webm' });
|
|
7331
|
-
setVideoBlob(dummyBlob);
|
|
7332
|
-
const url = URL.createObjectURL(dummyBlob);
|
|
7333
|
-
setVideoUrl(url);
|
|
7334
|
-
}
|
|
7335
|
-
});
|
|
7336
|
-
// Audio recorder
|
|
7337
|
-
const { startRecording: startAudioRecording, cancelRecording: cancelAudioRecording } = useRecorder(stream, "audio", (blob) => {
|
|
7338
|
-
if (blob && blob.size > 0) {
|
|
7339
|
-
setAudioBlob(blob);
|
|
7340
|
-
}
|
|
7341
|
-
else {
|
|
7342
|
-
const dummyBlob = new Blob(['dummy audio data'], { type: 'audio/webm' });
|
|
7343
|
-
setAudioBlob(dummyBlob);
|
|
7344
|
-
}
|
|
7345
|
-
// Only show result when both recordings are done
|
|
7346
|
-
// Video callback will be called first, then audio
|
|
7347
7563
|
stopStreamTracks();
|
|
7564
|
+
stopAudioVisualization();
|
|
7565
|
+
// A capture that produced nothing is a failure, not a success with an
|
|
7566
|
+
// empty file. This used to substitute a ~15-byte text Blob named
|
|
7567
|
+
// face-video.webm and carry on, so a camera that failed surfaced later as
|
|
7568
|
+
// an unexplained ML error instead of a retry here.
|
|
7569
|
+
if (!blob || blob.size === 0) {
|
|
7570
|
+
console.warn('face recorder: capture produced no video; returning to preparation');
|
|
7571
|
+
resetAfterFailedCapture();
|
|
7572
|
+
return;
|
|
7573
|
+
}
|
|
7574
|
+
setVideoBlob(blob);
|
|
7575
|
+
setVideoUrl(URL.createObjectURL(blob));
|
|
7348
7576
|
setState('result');
|
|
7349
7577
|
setIsConfirming(true);
|
|
7350
|
-
|
|
7351
|
-
|
|
7578
|
+
}, recordingMs);
|
|
7579
|
+
// Audio recorder — only used when emitAudio is set; see FaceRecorderProps.
|
|
7580
|
+
const { startRecording: startAudioRecording, cancelRecording: cancelAudioRecording } = useRecorder(stream, "audio", (blob) => {
|
|
7581
|
+
setAudioBlob(blob && blob.size > 0 ? blob : null);
|
|
7582
|
+
}, recordingMs);
|
|
7352
7583
|
React.useEffect(() => {
|
|
7353
7584
|
// If we already notified for this recording, skip
|
|
7354
7585
|
if (hasNotifiedRef.current)
|
|
7355
7586
|
return;
|
|
7356
|
-
if (videoBlob &&
|
|
7587
|
+
if (videoBlob && onRecorded) {
|
|
7357
7588
|
hasNotifiedRef.current = true; // prevent double-calls
|
|
7358
|
-
const videoFile = new File([videoBlob], 'face-video.webm', { type: videoBlob.type });
|
|
7359
|
-
const audioFile = new File([audioBlob], 'face-audio.webm', { type: audioBlob.type });
|
|
7360
|
-
const phrase = numbersToPhrase(digits);
|
|
7361
7589
|
try {
|
|
7362
|
-
onRecorded(
|
|
7590
|
+
onRecorded(buildRecording(videoBlob, audioBlob, digits));
|
|
7363
7591
|
}
|
|
7364
7592
|
catch (err) {
|
|
7365
7593
|
// swallow errors from consumer callback to avoid breaking SDK logic
|
|
@@ -7394,10 +7622,10 @@ const FaceRecorder = ({ rectWidth = 360, rectHeight = 576, onConfirmRecording, o
|
|
|
7394
7622
|
}
|
|
7395
7623
|
else {
|
|
7396
7624
|
setState('recording');
|
|
7397
|
-
setPrepTimer(
|
|
7398
|
-
// Start both video and audio recording simultaneously
|
|
7625
|
+
setPrepTimer(countdownSeconds);
|
|
7399
7626
|
startVideoRecording();
|
|
7400
|
-
|
|
7627
|
+
if (emitAudio)
|
|
7628
|
+
startAudioRecording();
|
|
7401
7629
|
}
|
|
7402
7630
|
}
|
|
7403
7631
|
}, [state, prepTimer]);
|
|
@@ -7426,9 +7654,41 @@ const FaceRecorder = ({ rectWidth = 360, rectHeight = 576, onConfirmRecording, o
|
|
|
7426
7654
|
}
|
|
7427
7655
|
}
|
|
7428
7656
|
}, [state, recordTimer]);
|
|
7657
|
+
/**
|
|
7658
|
+
* Prefer the server-issued challenge; fall back to a generated one. Kept in
|
|
7659
|
+
* one place so every reset path applies the same rule.
|
|
7660
|
+
*/
|
|
7661
|
+
const applyChallenge = () => {
|
|
7662
|
+
var _a;
|
|
7663
|
+
const issued = (_a = challengeDigits === null || challengeDigits === void 0 ? void 0 : challengeDigits.filter(d => Number.isInteger(d) && d >= 0 && d <= 9)) !== null && _a !== void 0 ? _a : [];
|
|
7664
|
+
setDigits(issued.length > 0 ? issued : generateRandomDigits(DIGITS_NUMBER$1));
|
|
7665
|
+
};
|
|
7666
|
+
/** Build the emitted payload; `audio` is omitted unless a blob exists. */
|
|
7667
|
+
const buildRecording = (video, audio, challenge) => ({
|
|
7668
|
+
file: new File([video], 'face-video.webm', { type: video.type }),
|
|
7669
|
+
...(audio ? { audio: new File([audio], 'face-audio.webm', { type: audio.type }) } : {}),
|
|
7670
|
+
phrase: numbersToPhrase(challenge),
|
|
7671
|
+
});
|
|
7672
|
+
/** Return to the start after a capture that produced no usable video. */
|
|
7673
|
+
const resetAfterFailedCapture = () => {
|
|
7674
|
+
setVideoBlob(null);
|
|
7675
|
+
setAudioBlob(null);
|
|
7676
|
+
setVideoUrl(null);
|
|
7677
|
+
setIsConfirming(false);
|
|
7678
|
+
hasNotifiedRef.current = false;
|
|
7679
|
+
setState('preparation');
|
|
7680
|
+
applyChallenge();
|
|
7681
|
+
requestPermissions();
|
|
7682
|
+
};
|
|
7683
|
+
// Adopt a challenge that arrives (or changes) while idle; never mid-capture.
|
|
7684
|
+
React.useEffect(() => {
|
|
7685
|
+
if (state === 'preparation')
|
|
7686
|
+
applyChallenge();
|
|
7687
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
7688
|
+
}, [challengeDigits]);
|
|
7429
7689
|
const startRecording = async () => {
|
|
7430
|
-
|
|
7431
|
-
setRecordTimer(
|
|
7690
|
+
applyChallenge();
|
|
7691
|
+
setRecordTimer(recordingSeconds);
|
|
7432
7692
|
setState('countdown');
|
|
7433
7693
|
};
|
|
7434
7694
|
const handleCancel = () => {
|
|
@@ -7445,17 +7705,14 @@ const FaceRecorder = ({ rectWidth = 360, rectHeight = 576, onConfirmRecording, o
|
|
|
7445
7705
|
setVideoBlob(null);
|
|
7446
7706
|
setAudioBlob(null);
|
|
7447
7707
|
// Reset digits for next recording
|
|
7448
|
-
|
|
7708
|
+
applyChallenge();
|
|
7449
7709
|
// Restart the stream for cancellation
|
|
7450
7710
|
requestPermissions();
|
|
7451
7711
|
};
|
|
7452
7712
|
const handleConfirm = () => {
|
|
7453
|
-
if (videoBlob
|
|
7454
|
-
// Create file objects
|
|
7455
|
-
const videoFile = new File([videoBlob], 'face-video.webm', { type: videoBlob.type });
|
|
7456
|
-
const audioFile = new File([audioBlob], 'face-audio.webm', { type: audioBlob.type });
|
|
7713
|
+
if (videoBlob) {
|
|
7457
7714
|
// required callback in parent app
|
|
7458
|
-
onConfirmRecording(
|
|
7715
|
+
onConfirmRecording(buildRecording(videoBlob, audioBlob, digits));
|
|
7459
7716
|
// Clean up and reset state
|
|
7460
7717
|
if (videoUrl)
|
|
7461
7718
|
URL.revokeObjectURL(videoUrl);
|
|
@@ -7467,7 +7724,7 @@ const FaceRecorder = ({ rectWidth = 360, rectHeight = 576, onConfirmRecording, o
|
|
|
7467
7724
|
// Reset notified flag
|
|
7468
7725
|
hasNotifiedRef.current = false;
|
|
7469
7726
|
// Reset digits for next recording
|
|
7470
|
-
|
|
7727
|
+
applyChallenge();
|
|
7471
7728
|
// Restart stream for next recording
|
|
7472
7729
|
requestPermissions();
|
|
7473
7730
|
}
|
|
@@ -7483,7 +7740,7 @@ const FaceRecorder = ({ rectWidth = 360, rectHeight = 576, onConfirmRecording, o
|
|
|
7483
7740
|
// Reset notified flag
|
|
7484
7741
|
hasNotifiedRef.current = false;
|
|
7485
7742
|
// Reset digits for next recording
|
|
7486
|
-
|
|
7743
|
+
applyChallenge();
|
|
7487
7744
|
// Restart the stream for retake
|
|
7488
7745
|
requestPermissions();
|
|
7489
7746
|
};
|
|
@@ -7742,11 +7999,17 @@ const FaceRecorder = ({ rectWidth = 360, rectHeight = 576, onConfirmRecording, o
|
|
|
7742
7999
|
const [permission, setPermission] = React.useState(false);
|
|
7743
8000
|
const [stream, setStream] = React.useState(null);
|
|
7744
8001
|
const [isLoading, setIsLoading] = React.useState(true);
|
|
8002
|
+
// The stream is mirrored in a ref so stopStreamTracks and the unmount
|
|
8003
|
+
// cleanup act on the live stream rather than a stale closure, and so a
|
|
8004
|
+
// getUserMedia that resolves after unmount can be stopped immediately
|
|
8005
|
+
// (restored from 1.3.5, which 1.4.0 reverted).
|
|
8006
|
+
const streamRef = React.useRef(null);
|
|
8007
|
+
const isMountedRef = React.useRef(false);
|
|
7745
8008
|
const stopStreamTracks = () => {
|
|
7746
|
-
|
|
7747
|
-
|
|
7748
|
-
|
|
7749
|
-
|
|
8009
|
+
const currentStream = streamRef.current;
|
|
8010
|
+
if (currentStream) {
|
|
8011
|
+
currentStream.getTracks().forEach(track => track.stop());
|
|
8012
|
+
streamRef.current = null;
|
|
7750
8013
|
setStream(null);
|
|
7751
8014
|
}
|
|
7752
8015
|
};
|
|
@@ -7762,7 +8025,7 @@ const FaceRecorder = ({ rectWidth = 360, rectHeight = 576, onConfirmRecording, o
|
|
|
7762
8025
|
noiseSuppression: true,
|
|
7763
8026
|
autoGainControl: true,
|
|
7764
8027
|
sampleRate: 48000,
|
|
7765
|
-
channelCount:
|
|
8028
|
+
channelCount: 1 // Mono: the gateway downmixes with -ac 1 and the engine expects mono (BM-446)
|
|
7766
8029
|
}
|
|
7767
8030
|
};
|
|
7768
8031
|
}
|
|
@@ -7794,19 +8057,36 @@ const FaceRecorder = ({ rectWidth = 360, rectHeight = 576, onConfirmRecording, o
|
|
|
7794
8057
|
setIsLoading(true);
|
|
7795
8058
|
const constraints = getAudioConstraints();
|
|
7796
8059
|
const streamData = await navigator.mediaDevices.getUserMedia(constraints);
|
|
8060
|
+
// Unmounted while getUserMedia was resolving: release the devices now,
|
|
8061
|
+
// otherwise the camera/mic indicator stays on with nothing holding it.
|
|
8062
|
+
if (!isMountedRef.current) {
|
|
8063
|
+
streamData.getTracks().forEach(track => track.stop());
|
|
8064
|
+
return;
|
|
8065
|
+
}
|
|
8066
|
+
streamRef.current = streamData;
|
|
7797
8067
|
setPermission(true);
|
|
7798
8068
|
setStream(streamData);
|
|
7799
8069
|
}
|
|
7800
8070
|
catch (err) {
|
|
7801
8071
|
console.error('Microphone permission denied:', err);
|
|
7802
|
-
|
|
8072
|
+
if (isMountedRef.current)
|
|
8073
|
+
setPermission(false);
|
|
7803
8074
|
}
|
|
7804
8075
|
finally {
|
|
7805
|
-
|
|
8076
|
+
if (isMountedRef.current)
|
|
8077
|
+
setIsLoading(false);
|
|
7806
8078
|
}
|
|
7807
8079
|
};
|
|
7808
8080
|
React.useEffect(() => {
|
|
8081
|
+
isMountedRef.current = true;
|
|
7809
8082
|
void requestPermissions();
|
|
8083
|
+
return () => {
|
|
8084
|
+
isMountedRef.current = false;
|
|
8085
|
+
if (streamRef.current) {
|
|
8086
|
+
streamRef.current.getTracks().forEach(track => track.stop());
|
|
8087
|
+
streamRef.current = null;
|
|
8088
|
+
}
|
|
8089
|
+
};
|
|
7810
8090
|
}, []);
|
|
7811
8091
|
return { permission, stream, stopStreamTracks, requestPermissions, isLoading };
|
|
7812
8092
|
};// spinner animation
|
|
@@ -7824,25 +8104,30 @@ if (typeof document !== 'undefined') {
|
|
|
7824
8104
|
}
|
|
7825
8105
|
const RECORD_SECONDS = VOICE_ENROLL_LENGTH_MS / 1000;
|
|
7826
8106
|
const PREP_SECONDS = 3;
|
|
7827
|
-
|
|
7828
|
-
|
|
7829
|
-
|
|
7830
|
-
|
|
7831
|
-
|
|
7832
|
-
|
|
7833
|
-
|
|
7834
|
-
|
|
7835
|
-
|
|
7836
|
-
|
|
7837
|
-
|
|
7838
|
-
|
|
7839
|
-
return digits.slice(0, count);
|
|
7840
|
-
}
|
|
7841
|
-
const VoiceRecorder = ({ rectWidth = 360, rectHeight, onConfirmRecording, onRecorded, className, style, noShadow = false, }) => {
|
|
8107
|
+
const DEFAULT_TAKES = 5;
|
|
8108
|
+
/**
|
|
8109
|
+
* Prompts come from ENROLMENT_DIGIT_SEQUENCES, not from a random draw.
|
|
8110
|
+
*
|
|
8111
|
+
* The published sequences are chosen to contain no duplicate 2-digit contexts,
|
|
8112
|
+
* and presenting every user the same set is what makes cross-user calibration
|
|
8113
|
+
* accurate — calibration being what sets the acceptance threshold (BM-449,
|
|
8114
|
+
* BM-450). Random enrolment prompts gave each user an arbitrary, unmeasured
|
|
8115
|
+
* slice of that space.
|
|
8116
|
+
*/
|
|
8117
|
+
const DIGITS_NUMBER = ENROLMENT_DIGIT_SEQUENCES[0].length;
|
|
8118
|
+
const VoiceRecorder = ({ rectWidth = 360, rectHeight, takes = DEFAULT_TAKES, onConfirmRecording, onRecorded, className, style, noShadow = false, }) => {
|
|
7842
8119
|
const [state, setState] = React.useState('preparation');
|
|
7843
8120
|
const [prepTimer, setPrepTimer] = React.useState(PREP_SECONDS);
|
|
7844
8121
|
const [recordTimer, setRecordTimer] = React.useState(RECORD_SECONDS);
|
|
7845
|
-
|
|
8122
|
+
/** Takes confirmed so far; emitted together once `takes` are collected. */
|
|
8123
|
+
const [collected, setCollected] = React.useState([]);
|
|
8124
|
+
/**
|
|
8125
|
+
* The prompt for the take being collected, keyed on how many are already
|
|
8126
|
+
* confirmed — so a retake re-prompts the *same* sequence rather than drawing
|
|
8127
|
+
* a new one: the user is re-recording a take, not being given a different
|
|
8128
|
+
* task. Wraps if a consumer asks for more takes than there are sequences.
|
|
8129
|
+
*/
|
|
8130
|
+
const digits = React.useMemo(() => [...ENROLMENT_DIGIT_SEQUENCES[collected.length % ENROLMENT_DIGIT_SEQUENCES.length]], [collected.length]);
|
|
7846
8131
|
const [audioLevel, setAudioLevel] = React.useState(0);
|
|
7847
8132
|
const [waveformData, setWaveformData] = React.useState([]);
|
|
7848
8133
|
const { permission, stream, stopStreamTracks, requestPermissions, isLoading } = useAudioPermissions();
|
|
@@ -7854,29 +8139,33 @@ const VoiceRecorder = ({ rectWidth = 360, rectHeight, onConfirmRecording, onReco
|
|
|
7854
8139
|
const animationFrameRef = React.useRef(null);
|
|
7855
8140
|
const { startRecording: startHookRecording, cancelRecording } = useRecorder(stream, "audio", (blob) => {
|
|
7856
8141
|
stopStreamTracks();
|
|
7857
|
-
|
|
7858
|
-
|
|
7859
|
-
|
|
7860
|
-
|
|
7861
|
-
|
|
7862
|
-
|
|
7863
|
-
|
|
7864
|
-
|
|
7865
|
-
|
|
7866
|
-
|
|
7867
|
-
|
|
7868
|
-
|
|
7869
|
-
console.error('onRecorded callback threw:', err);
|
|
7870
|
-
}
|
|
7871
|
-
}
|
|
8142
|
+
// A take that produced nothing is a failure, not a success with an empty
|
|
8143
|
+
// file. This used to substitute a ~15-byte text Blob named
|
|
8144
|
+
// voice-recording.webm and present it for confirmation, so a failed
|
|
8145
|
+
// recording went into the voiceprint as garbage (BM-461).
|
|
8146
|
+
if (!blob || blob.size === 0) {
|
|
8147
|
+
console.warn('voice recorder: take produced no audio; returning to preparation');
|
|
8148
|
+
setAudioBlob(null);
|
|
8149
|
+
setAudioUrl(null);
|
|
8150
|
+
setIsConfirming(false);
|
|
8151
|
+
setState('preparation');
|
|
8152
|
+
requestPermissions();
|
|
8153
|
+
return;
|
|
7872
8154
|
}
|
|
7873
|
-
|
|
7874
|
-
|
|
7875
|
-
|
|
7876
|
-
|
|
7877
|
-
|
|
7878
|
-
|
|
7879
|
-
|
|
8155
|
+
setAudioBlob(blob);
|
|
8156
|
+
setAudioUrl(URL.createObjectURL(blob));
|
|
8157
|
+
setState('result');
|
|
8158
|
+
setIsConfirming(true);
|
|
8159
|
+
if (onRecorded) {
|
|
8160
|
+
try {
|
|
8161
|
+
onRecorded({
|
|
8162
|
+
file: new File([blob], `voice-recording-${collected.length + 1}.webm`, { type: blob.type }),
|
|
8163
|
+
phrase: numbersToPhrase(digits),
|
|
8164
|
+
});
|
|
8165
|
+
}
|
|
8166
|
+
catch (err) {
|
|
8167
|
+
console.error('onRecorded callback threw:', err);
|
|
8168
|
+
}
|
|
7880
8169
|
}
|
|
7881
8170
|
}, VOICE_ENROLL_LENGTH_MS);
|
|
7882
8171
|
// Audio visualization setup
|
|
@@ -7938,10 +8227,20 @@ const VoiceRecorder = ({ rectWidth = 360, rectHeight, onConfirmRecording, onReco
|
|
|
7938
8227
|
}
|
|
7939
8228
|
}, [state, recordTimer]);
|
|
7940
8229
|
const startRecording = async () => {
|
|
7941
|
-
|
|
8230
|
+
// The prompt is derived from how many takes are confirmed, so there is
|
|
8231
|
+
// nothing to re-draw here — a retake deliberately re-prompts the same
|
|
8232
|
+
// sequence.
|
|
7942
8233
|
setRecordTimer(RECORD_SECONDS);
|
|
7943
8234
|
setState('countdown');
|
|
7944
8235
|
};
|
|
8236
|
+
/**
|
|
8237
|
+
* Discards the take in flight, not the enrollment.
|
|
8238
|
+
*
|
|
8239
|
+
* Confirmed takes are kept: wiping `collected` here sent the user back to
|
|
8240
|
+
* take 1 with no warning and no undo, so the longer the enrollment the more a
|
|
8241
|
+
* stumble cost. Nothing is mixed across attempts — every take reads the same
|
|
8242
|
+
* `digits`, and the in-flight blob is released below rather than kept.
|
|
8243
|
+
*/
|
|
7945
8244
|
const handleCancel = () => {
|
|
7946
8245
|
// Cancel the recording first
|
|
7947
8246
|
cancelRecording();
|
|
@@ -7957,20 +8256,32 @@ const VoiceRecorder = ({ rectWidth = 360, rectHeight, onConfirmRecording, onReco
|
|
|
7957
8256
|
requestPermissions();
|
|
7958
8257
|
};
|
|
7959
8258
|
const handleConfirm = () => {
|
|
7960
|
-
if (audioBlob)
|
|
7961
|
-
|
|
7962
|
-
|
|
7963
|
-
|
|
7964
|
-
|
|
7965
|
-
|
|
7966
|
-
|
|
7967
|
-
|
|
7968
|
-
|
|
7969
|
-
|
|
7970
|
-
|
|
7971
|
-
|
|
7972
|
-
|
|
8259
|
+
if (!audioBlob)
|
|
8260
|
+
return;
|
|
8261
|
+
const take = {
|
|
8262
|
+
file: new File([audioBlob], `voice-recording-${collected.length + 1}.webm`, { type: audioBlob.type }),
|
|
8263
|
+
phrase: numbersToPhrase(digits),
|
|
8264
|
+
};
|
|
8265
|
+
const confirmed = [...collected, take];
|
|
8266
|
+
if (audioUrl)
|
|
8267
|
+
URL.revokeObjectURL(audioUrl);
|
|
8268
|
+
setAudioUrl(null);
|
|
8269
|
+
setAudioBlob(null);
|
|
8270
|
+
setIsConfirming(false);
|
|
8271
|
+
setState('preparation');
|
|
8272
|
+
// Emit only once every take is in: the consumer posts them together as one
|
|
8273
|
+
// enrollment, so a partial set is worse than none.
|
|
8274
|
+
if (confirmed.length >= takes) {
|
|
8275
|
+
setCollected([]);
|
|
8276
|
+
onConfirmRecording({
|
|
8277
|
+
files: confirmed.map(t => t.file),
|
|
8278
|
+
phrases: confirmed.map(t => t.phrase),
|
|
8279
|
+
});
|
|
8280
|
+
return;
|
|
7973
8281
|
}
|
|
8282
|
+
// More takes to go: re-arm the mic so the next one can start immediately.
|
|
8283
|
+
setCollected(confirmed);
|
|
8284
|
+
requestPermissions();
|
|
7974
8285
|
};
|
|
7975
8286
|
const handleDecline = () => {
|
|
7976
8287
|
if (audioUrl)
|
|
@@ -8151,13 +8462,13 @@ const VoiceRecorder = ({ rectWidth = 360, rectHeight, onConfirmRecording, onReco
|
|
|
8151
8462
|
justifyContent: 'center',
|
|
8152
8463
|
width: '100%',
|
|
8153
8464
|
padding: '20px',
|
|
8154
|
-
}, children: [state === 'preparation' && (jsxRuntime.
|
|
8465
|
+
}, children: [state === 'preparation' && (jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [takes > 1 && (jsxRuntime.jsxs("div", { style: { ...guidanceText, fontWeight: 600 }, children: ["Recording ", Math.min(collected.length + 1, takes), " of ", takes] })), jsxRuntime.jsx("div", { style: guidanceText, children: "Click Start to begin voice recording" })] })), state === 'countdown' && (jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [jsxRuntime.jsx("div", { style: guidanceText, children: "Get ready to speak clearly" }), jsxRuntime.jsx("div", { style: prepTimerStyle, children: prepTimer })] })), state === 'recording' && (jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [jsxRuntime.jsx("div", { style: waveformContainer, children: jsxRuntime.jsx("div", { style: {
|
|
8155
8466
|
display: 'flex',
|
|
8156
8467
|
alignItems: 'center',
|
|
8157
8468
|
justifyContent: 'center',
|
|
8158
8469
|
height: '120px',
|
|
8159
8470
|
gap: '2px',
|
|
8160
|
-
}, children: waveformData.map((value, index) => (jsxRuntime.jsx("div", { style: waveformBar(value / 255) }, index))) }) }), jsxRuntime.jsx("div", { style: timerStyle, children: recordTimer }), jsxRuntime.jsx("div", { style: guidanceText, children: "Speak clearly into your microphone" })] }))] })] }), jsxRuntime.jsx("div", { style: numberRow, children: (state === 'recording' ? digits : Array(DIGITS_NUMBER).fill('*')).map((num, i) => (jsxRuntime.jsx("div", { style: numberBox, children: num }, i))) }), state === 'preparation' && (jsxRuntime.jsx("button", { type: "button", style: actionButton, onClick: startRecording, children: "Start recording" })), state === 'recording' && (jsxRuntime.jsx("button", { type: "button", style: { ...actionButton, background: '#d32f2f' }, onClick: handleCancel, children:
|
|
8471
|
+
}, children: waveformData.map((value, index) => (jsxRuntime.jsx("div", { style: waveformBar(value / 255) }, index))) }) }), jsxRuntime.jsx("div", { style: timerStyle, children: recordTimer }), jsxRuntime.jsx("div", { style: guidanceText, children: "Speak clearly into your microphone" })] }))] })] }), jsxRuntime.jsx("div", { style: numberRow, children: (state === 'recording' ? digits : Array(DIGITS_NUMBER).fill('*')).map((num, i) => (jsxRuntime.jsx("div", { style: numberBox, children: num }, i))) }), state === 'preparation' && (jsxRuntime.jsx("button", { type: "button", style: actionButton, onClick: startRecording, children: "Start recording" })), state === 'recording' && (jsxRuntime.jsx("button", { type: "button", style: { ...actionButton, background: '#d32f2f' }, onClick: handleCancel, children: takes > 1 ? 'Discard this recording' : 'Cancel' }))] })), state === 'processing' && (jsxRuntime.jsx(jsxRuntime.Fragment, { children: jsxRuntime.jsx("div", { className: "voicerecorder-processing", style: preview, children: jsxRuntime.jsxs("div", { style: {
|
|
8161
8472
|
display: 'flex',
|
|
8162
8473
|
flexDirection: 'column',
|
|
8163
8474
|
alignItems: 'center',
|
|
@@ -8184,4 +8495,4 @@ const VoiceRecorder = ({ rectWidth = 360, rectHeight, onConfirmRecording, onReco
|
|
|
8184
8495
|
maxWidth: '300px',
|
|
8185
8496
|
marginBottom: '16px',
|
|
8186
8497
|
} })), jsxRuntime.jsx("div", { className: "voicerecorder-confirm-text", style: confirmText, children: "Do you want to use this recording?" }), jsxRuntime.jsxs("div", { className: "voicerecorder-confirm-btns", style: { display: 'flex', justifyContent: 'center', gap: 32, marginTop: 12, width: '100%' }, children: [jsxRuntime.jsx("button", { type: "button", "aria-label": "Retake", className: "voicerecorder-icon-btn", style: iconButton, onClick: handleDecline, onMouseOver: e => (e.currentTarget.style.background = 'rgba(211,47,47,0.12)'), onMouseOut: e => (e.currentTarget.style.background = 'none'), children: jsxRuntime.jsxs("svg", { width: "36", height: "36", viewBox: "0 0 36 36", fill: "none", xmlns: "http://www.w3.org/2000/svg", children: [jsxRuntime.jsx("circle", { cx: "18", cy: "18", r: "18", fill: "#fff", fillOpacity: "0.15" }), jsxRuntime.jsx("path", { d: "M12 12L24 24", stroke: "#d32f2f", strokeWidth: "3", strokeLinecap: "round" }), jsxRuntime.jsx("path", { d: "M24 12L12 24", stroke: "#d32f2f", strokeWidth: "3", strokeLinecap: "round" })] }) }), jsxRuntime.jsx("button", { type: "button", "aria-label": "Confirm", className: "voicerecorder-icon-btn", style: iconButton, onClick: handleConfirm, onMouseOver: e => (e.currentTarget.style.background = 'rgba(56,142,60,0.12)'), onMouseOut: e => (e.currentTarget.style.background = 'none'), children: jsxRuntime.jsxs("svg", { width: "36", height: "36", viewBox: "0 0 36 36", fill: "none", xmlns: "http://www.w3.org/2000/svg", children: [jsxRuntime.jsx("circle", { cx: "18", cy: "18", r: "18", fill: "#fff", fillOpacity: "0.15" }), jsxRuntime.jsx("path", { d: "M10 19L16 25L26 13", stroke: "#388e3c", strokeWidth: "3", strokeLinecap: "round", strokeLinejoin: "round" })] }) })] })] }) }))] }));
|
|
8187
|
-
};exports.DocScan=DocScan;exports.FaceCapture=FaceCapture;exports.FaceRecorder=FaceRecorder;exports.VoiceRecorder=VoiceRecorder;exports.useAudioPermissions=useAudioPermissions;exports.usePermissions=usePermissions;exports.useRecorder=useRecorder;
|
|
8498
|
+
};exports.CHALLENGE_DIGITS=CHALLENGE_DIGITS;exports.DocScan=DocScan;exports.ENROLMENT_DIGIT_SEQUENCES=ENROLMENT_DIGIT_SEQUENCES;exports.FaceCapture=FaceCapture;exports.FaceRecorder=FaceRecorder;exports.VoiceRecorder=VoiceRecorder;exports.generateRandomDigits=generateRandomDigits;exports.numbersToPhrase=numbersToPhrase;exports.useAudioPermissions=useAudioPermissions;exports.usePermissions=usePermissions;exports.useRecorder=useRecorder;
|