@incodetech/core 2.3.0-rc.11 → 2.3.0-rc.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.
Files changed (58) hide show
  1. package/dist/{FaceDetectionProvider-D5FNNkoJ.esm.js → FaceDetectionProvider-BLINJUJ_.esm.js} +5 -5
  2. package/dist/ae-signature.esm.js +1 -1
  3. package/dist/{apiError-BmktKO7R.esm.js → apiError-4S2Z_rSf.esm.js} +1 -1
  4. package/dist/authentication.esm.js +2 -2
  5. package/dist/{authenticationManager-CSCtXfWz.esm.js → authenticationManager-Cds7_1k7.esm.js} +3 -3
  6. package/dist/{authenticationStateMachine-BBPaXbO0.esm.js → authenticationStateMachine-CNb31qDV.esm.js} +2 -2
  7. package/dist/{deepsightLoader-U24VrP2r.esm.js → deepsightLoader-D7aKM2Fr.esm.js} +3 -3
  8. package/dist/dynamic-forms.esm.js +2 -2
  9. package/dist/{dynamicFormsManager-DJ9GQ1HY.esm.js → dynamicFormsManager-C6vQOzNN.esm.js} +1 -1
  10. package/dist/{dynamicFormsStateMachine-D6oLfWyM.esm.js → dynamicFormsStateMachine-CEf-u1Vj.esm.js} +1 -1
  11. package/dist/ekyb.esm.js +3 -3
  12. package/dist/{ekybManager-BDtwhSrQ.esm.js → ekybManager-CvyL5PAV.esm.js} +1 -1
  13. package/dist/{ekybStateMachine-B4qbgC_I.esm.js → ekybStateMachine-BsIr7Uh-.esm.js} +1 -1
  14. package/dist/electronic-signature.esm.js +1 -1
  15. package/dist/{electronicSignatureManager-i9KSHGcI.esm.js → electronicSignatureManager-CnMuiOGf.esm.js} +1 -1
  16. package/dist/extensibility.d.ts +2 -2
  17. package/dist/extensibility.esm.js +10 -10
  18. package/dist/{faceCaptureManagerFactory-Df6oe5Zi.esm.js → faceCaptureManagerFactory-Dyt7CSl-.esm.js} +1 -1
  19. package/dist/{faceCaptureSetup-DrJvjpVf.esm.js → faceCaptureSetup-DhQCy2ex.esm.js} +7 -6
  20. package/dist/{feature-management-CT71kF7K.esm.js → feature-management-DVOpWeB4.esm.js} +1 -1
  21. package/dist/feature-management.esm.js +1 -1
  22. package/dist/flow.esm.js +3 -3
  23. package/dist/{flowCompletionService-DbMQ7zEQ.esm.js → flowCompletionService-BSfi8EEC.esm.js} +1 -1
  24. package/dist/{flowServices-ZXug3wzs.esm.js → flowServices-DoPimvFm.esm.js} +4 -4
  25. package/dist/geolocation.esm.js +2 -2
  26. package/dist/{geolocationManager-DbfRyrLW.esm.js → geolocationManager-CpPHA-m7.esm.js} +1 -1
  27. package/dist/{geolocationStateMachine-CP8_KHBU.esm.js → geolocationStateMachine-5M86MRiv.esm.js} +1 -1
  28. package/dist/id-selfie-flow.d.ts +1 -1
  29. package/dist/id-selfie-flow.esm.js +8 -8
  30. package/dist/id.esm.js +3 -3
  31. package/dist/{idCaptureDefaults-CqdpEg3N.esm.js → idCaptureDefaults-DkNKy4mw.esm.js} +1 -1
  32. package/dist/{idCaptureManager-Bs6p5LKF.esm.js → idCaptureManager-cLKLxge3.esm.js} +3 -3
  33. package/dist/{idCaptureServices-xi9Pthk2.esm.js → idCaptureServices-CP8ZJr8Y.esm.js} +8 -7
  34. package/dist/{idCaptureStateMachine-BYgxfqHv.esm.js → idCaptureStateMachine-5DZ07r8_.esm.js} +10 -10
  35. package/dist/identity-reuse.d.ts +1 -1
  36. package/dist/{identityReuseManager-v2TH5g7e.d.ts → identityReuseManager-BFDI7iFK.d.ts} +4 -4
  37. package/dist/index.esm.js +1 -1
  38. package/dist/mandatory-consent.d.ts +1 -1
  39. package/dist/{mandatoryConsentManager-CJSyhTpi.d.ts → mandatoryConsentManager-Csfl7_XQ.d.ts} +4 -4
  40. package/dist/{multipartRecordingService-YKu0Bd7S.esm.js → multipartRecordingService-mwKFTlhR.esm.js} +689 -8
  41. package/dist/qe-signature.esm.js +1 -1
  42. package/dist/{runChildModule-BhK3islQ.esm.js → runChildModule-DnSpmfi_.esm.js} +1 -1
  43. package/dist/{sdkVersion-B8ok4TX0.esm.js → sdkVersion-D9RnzkVL.esm.js} +1 -1
  44. package/dist/selfie.esm.js +5 -5
  45. package/dist/{selfieManager-CGWnCp26.esm.js → selfieManager-CKG9Ycb8.esm.js} +3 -3
  46. package/dist/{selfieStateMachine-B27MSUWB.esm.js → selfieStateMachine-D4F_8Mmv.esm.js} +2 -2
  47. package/dist/{session-BRsgJ-Ek.esm.js → session-BnAXj1AO.esm.js} +3 -3
  48. package/dist/session.esm.js +4 -4
  49. package/dist/{sessionInitializer-NKOKaYzH.esm.js → sessionInitializer-FZeu_jKW.esm.js} +3 -3
  50. package/dist/{setup-BqZsYdcN.esm.js → setup-DEPf5jcB.esm.js} +4 -4
  51. package/dist/{trueSightService-CvCQ1O8I.esm.js → trueSightService-B8ESMOpB.esm.js} +1 -1
  52. package/dist/video-selfie.d.ts +61 -3
  53. package/dist/video-selfie.esm.js +7 -6
  54. package/dist/{videoSelfieCameraWarmer-DEwDuHEE.esm.js → videoSelfieCameraWarmer-DC3RYtMs.esm.js} +4 -4
  55. package/dist/{videoSelfieStepDetector-Co0Gu2k2.d.ts → videoSelfieStepDetector-CAqAG2G3.d.ts} +49 -0
  56. package/dist/wasm.esm.js +2 -2
  57. package/dist/workflow.esm.js +14 -14
  58. package/package.json +1 -1
@@ -83,6 +83,33 @@ function getAdaptiveMediaRecorderOptions() {
83
83
  bitsPerSecond: 5e5
84
84
  };
85
85
  }
86
+ /**
87
+ * Worker-encoded multipart recording profile (ENG-55397). 2 GB-class devices
88
+ * (`navigator.deviceMemory` ≤ 2) record 360p at 15 fps: on a Galaxy A01 Core,
89
+ * 720p30 plus WASM detection exhausted memory (renderer swapped, GPU process
90
+ * killed), and even the in-worker encoder starved the camera preview to
91
+ * 6 fps. They also upload 2 s parts: every part is one request through the
92
+ * WASM HTTP client on the main thread, and at 0.5 s parts the uploads fell
93
+ * behind during face detection, stretching the final "processing" wait.
94
+ * Other devices, and browsers without `deviceMemory`, keep 720p30 at the
95
+ * MediaRecorder bitrate and 0.5 s parts.
96
+ */
97
+ function getWorkerRecordingProfile(deviceMemoryGb = typeof navigator !== "undefined" ? navigator.deviceMemory : void 0) {
98
+ if (deviceMemoryGb !== void 0 && deviceMemoryGb <= 2) return {
99
+ maxLongEdge: 640,
100
+ maxShortEdge: 360,
101
+ fps: 15,
102
+ bitrate: 6e5,
103
+ timesliceMs: 2e3
104
+ };
105
+ return {
106
+ maxLongEdge: 1280,
107
+ maxShortEdge: 720,
108
+ fps: 30,
109
+ bitrate: 25e5,
110
+ timesliceMs: 500
111
+ };
112
+ }
86
113
  //#endregion
87
114
  //#region ../infra/src/media/cameraCompositorWorker.ts
88
115
  /**
@@ -122,6 +149,9 @@ function cameraCompositorWorkerMain(scope) {
122
149
  let generation = 0;
123
150
  let paused = false;
124
151
  let announceNextFrame = false;
152
+ let minFrameIntervalUs = 0;
153
+ let lastWrittenUs = -Infinity;
154
+ let encodeConfig = null;
125
155
  const boundedSize = (width, height) => {
126
156
  const scale = Math.min(1, maxLongEdge / Math.max(width, height), maxShortEdge / Math.min(width, height));
127
157
  return {
@@ -141,6 +171,423 @@ function cameraCompositorWorkerMain(scope) {
141
171
  frame.close();
142
172
  return out;
143
173
  };
174
+ const video = {
175
+ number: 1,
176
+ offsetUs: null,
177
+ lastUs: -1
178
+ };
179
+ const audio = {
180
+ number: 2,
181
+ offsetUs: null,
182
+ lastUs: -1
183
+ };
184
+ let videoEncoder = null;
185
+ let audioEncoder = null;
186
+ let hasAudioSource = false;
187
+ let audioWanted = false;
188
+ let audioFormat = null;
189
+ let audioInHeader = false;
190
+ /**
191
+ * The Opus encoder numbers its output by sample count from its first input,
192
+ * so track where that count has reached on our timeline (µs).
193
+ */
194
+ let audioClockStartUs = null;
195
+ let audioFedUs = 0;
196
+ let recording = "off";
197
+ let encodedSize = {
198
+ width: 0,
199
+ height: 0
200
+ };
201
+ let originMs = 0;
202
+ let pausedMs = 0;
203
+ let pauseStartedMs = 0;
204
+ /** Pacer: source time the next frame is due (schedule, not last frame). */
205
+ let nextDueUs = -Infinity;
206
+ let lastKeyUs = -Infinity;
207
+ let forceKey = true;
208
+ let headerSent = false;
209
+ let lastFlushMs = 0;
210
+ /** Oldest pending block timestamp (µs); pending is small but hot. */
211
+ let oldestPendingUs = Infinity;
212
+ let pending = [];
213
+ const concat = (parts) => {
214
+ let length = 0;
215
+ for (const part of parts) length += part.length;
216
+ const out = new Uint8Array(length);
217
+ let offset = 0;
218
+ for (const part of parts) {
219
+ out.set(part, offset);
220
+ offset += part.length;
221
+ }
222
+ return out;
223
+ };
224
+ const uint = (value) => {
225
+ const out = [];
226
+ let rest = value;
227
+ do {
228
+ out.unshift(rest % 256);
229
+ rest = Math.floor(rest / 256);
230
+ } while (rest > 0);
231
+ return out;
232
+ };
233
+ /** EBML variable-length size, shortest form. */
234
+ const vsize = (size) => {
235
+ for (let length = 1; length <= 8; length += 1) if (size < 2 ** (7 * length) - 1) {
236
+ const out = uint(size);
237
+ while (out.length < length) out.unshift(0);
238
+ out[0] |= 128 >> length - 1;
239
+ return out;
240
+ }
241
+ throw new Error("EBML element too large");
242
+ };
243
+ const element = (id, ...body) => {
244
+ const data = concat(body);
245
+ return concat([
246
+ id,
247
+ vsize(data.length),
248
+ data
249
+ ]);
250
+ };
251
+ const text = (value) => Array.from(value, (char) => char.charCodeAt(0));
252
+ const float64 = (value) => {
253
+ const view = /* @__PURE__ */ new DataView(/* @__PURE__ */ new ArrayBuffer(8));
254
+ view.setFloat64(0, value);
255
+ return Array.from(new Uint8Array(view.buffer));
256
+ };
257
+ /** RFC 7845 identification header: version 1, pre-skip 312, family 0. */
258
+ const opusHead = (channels, sampleRate) => [
259
+ ...text("OpusHead"),
260
+ 1,
261
+ channels,
262
+ 56,
263
+ 1,
264
+ sampleRate & 255,
265
+ sampleRate >> 8 & 255,
266
+ sampleRate >> 16 & 255,
267
+ sampleRate >> 24 & 255,
268
+ 0,
269
+ 0,
270
+ 0
271
+ ];
272
+ const webmHeader = () => {
273
+ const videoEntry = element([174], element([215], [1]), element([115, 197], [1]), element([131], [1]), element([134], text("V_VP8")), element([224], element([176], uint(encodedSize.width)), element([186], uint(encodedSize.height))));
274
+ const audioEntry = audioInHeader && audioFormat ? element([174], element([215], [2]), element([115, 197], [2]), element([131], [2]), element([134], text("A_OPUS")), element([99, 162], opusHead(audioFormat.channels, audioFormat.sampleRate)), element([225], element([181], float64(48e3)), element([159], [audioFormat.channels]))) : /* @__PURE__ */ new Uint8Array(0);
275
+ return concat([
276
+ element([
277
+ 26,
278
+ 69,
279
+ 223,
280
+ 163
281
+ ], element([66, 134], [1]), element([66, 247], [1]), element([66, 242], [4]), element([66, 243], [8]), element([66, 130], text("webm")), element([66, 135], [4]), element([66, 133], [2])),
282
+ [
283
+ 24,
284
+ 83,
285
+ 128,
286
+ 103,
287
+ 1,
288
+ 255,
289
+ 255,
290
+ 255,
291
+ 255,
292
+ 255,
293
+ 255,
294
+ 255
295
+ ],
296
+ element([
297
+ 21,
298
+ 73,
299
+ 169,
300
+ 102
301
+ ], element([
302
+ 42,
303
+ 215,
304
+ 177
305
+ ], uint(1e6)), element([77, 128], text("incode")), element([87, 65], text("incode"))),
306
+ element([
307
+ 22,
308
+ 84,
309
+ 174,
310
+ 107
311
+ ], videoEntry, audioEntry)
312
+ ]);
313
+ };
314
+ /**
315
+ * Writes every pending block older than `limitUs` as one cluster (one
316
+ * upload part). The first part carries the header, so it waits (up to
317
+ * 3 s) for the microphone's format when a microphone is attached.
318
+ */
319
+ const flushBefore = (limitUs, force = false) => {
320
+ if (pending.length === 0) return;
321
+ const oldestUs = oldestPendingUs;
322
+ const timesliceMs = encodeConfig?.timesliceMs ?? 500;
323
+ if (!force && performance.now() - lastFlushMs < timesliceMs * .8) return;
324
+ if (!force && !headerSent && audioWanted && !audioFormat && limitUs - oldestUs < 3e6) return;
325
+ const ready = pending.filter((block) => block.us < limitUs).sort((a, b) => a.us - b.us || a.track - b.track);
326
+ if (ready.length === 0) return;
327
+ pending = pending.filter((block) => block.us >= limitUs);
328
+ oldestPendingUs = pending.reduce((min, block) => Math.min(min, block.us), Infinity);
329
+ if (!headerSent) audioInHeader = audioFormat !== null;
330
+ const blocks = ready.filter((block) => block.track === 1 || audioInHeader);
331
+ if (blocks.length === 0) return;
332
+ const clusters = [];
333
+ let run = [];
334
+ const closeRun = () => {
335
+ if (run.length === 0) return;
336
+ const clusterMs = Math.floor(run[0].us / 1e3);
337
+ clusters.push(element([
338
+ 31,
339
+ 67,
340
+ 182,
341
+ 117
342
+ ], element([231], uint(clusterMs)), ...run.map((block) => {
343
+ const relative = Math.round(block.us / 1e3) - clusterMs;
344
+ return element([163], [
345
+ 128 | block.track,
346
+ relative >> 8 & 255,
347
+ relative & 255,
348
+ block.key ? 128 : 0
349
+ ], block.data);
350
+ })));
351
+ run = [];
352
+ };
353
+ for (const block of blocks) {
354
+ if (run.length > 0 && block.us - run[0].us > 3e7) closeRun();
355
+ run.push(block);
356
+ }
357
+ closeRun();
358
+ const cluster = concat(clusters);
359
+ const part = headerSent ? cluster : concat([webmHeader(), cluster]);
360
+ headerSent = true;
361
+ lastFlushMs = performance.now();
362
+ scope.postMessage({
363
+ type: "part",
364
+ data: part.buffer
365
+ }, [part.buffer]);
366
+ };
367
+ const onEncoded = (track) => (chunk) => {
368
+ const data = new Uint8Array(chunk.byteLength);
369
+ chunk.copyTo(data);
370
+ pending.push({
371
+ us: chunk.timestamp,
372
+ data,
373
+ key: chunk.type === "key",
374
+ track: track.number
375
+ });
376
+ oldestPendingUs = Math.min(oldestPendingUs, chunk.timestamp);
377
+ const timesliceUs = (encodeConfig?.timesliceMs ?? 500) * 1e3;
378
+ const spanUs = chunk.timestamp - oldestPendingUs;
379
+ if (track === video && chunk.type === "key" && spanUs >= timesliceUs / 2) flushBefore(chunk.timestamp);
380
+ else if (spanUs > timesliceUs * 3) flushBefore(chunk.timestamp - 2e5);
381
+ };
382
+ const timelineNowUs = () => (performance.now() - originMs - pausedMs) * 1e3;
383
+ const anchor = (track, sourceUs) => {
384
+ const nowUs = timelineNowUs();
385
+ if (track === audio && track.offsetUs !== null && nowUs - (sourceUs + track.offsetUs) > 25e4) track.offsetUs = null;
386
+ if (track.offsetUs === null) track.offsetUs = nowUs - sourceUs;
387
+ const us = Math.max(Math.round(sourceUs + track.offsetUs), track.lastUs + 1);
388
+ track.lastUs = us;
389
+ return us;
390
+ };
391
+ const reanchor = () => {
392
+ video.offsetUs = null;
393
+ audio.offsetUs = null;
394
+ nextDueUs = -Infinity;
395
+ forceKey = true;
396
+ };
397
+ const resetRecording = () => {
398
+ videoEncoder = null;
399
+ audioEncoder = null;
400
+ audioWanted = hasAudioSource;
401
+ audioFormat = null;
402
+ audioInHeader = false;
403
+ audioClockStartUs = null;
404
+ audioFedUs = 0;
405
+ originMs = performance.now();
406
+ pausedMs = 0;
407
+ video.lastUs = -1;
408
+ audio.lastUs = -1;
409
+ reanchor();
410
+ lastKeyUs = -Infinity;
411
+ headerSent = false;
412
+ lastFlushMs = 0;
413
+ oldestPendingUs = Infinity;
414
+ pending = [];
415
+ };
416
+ const encodeVideo = (frame) => {
417
+ const config = encodeConfig;
418
+ if (!config) {
419
+ frame.close();
420
+ return;
421
+ }
422
+ if (minFrameIntervalUs > 0) {
423
+ const intervalUs = 1e6 / config.fps;
424
+ if (frame.timestamp < nextDueUs - intervalUs * .2) {
425
+ frame.close();
426
+ return;
427
+ }
428
+ nextDueUs = nextDueUs === -Infinity || frame.timestamp - nextDueUs > intervalUs ? frame.timestamp + intervalUs : nextDueUs + intervalUs;
429
+ }
430
+ if (!videoEncoder) {
431
+ const size = boundedSize(frame.displayWidth, frame.displayHeight);
432
+ encodedSize = {
433
+ width: Math.max(2, size.width - size.width % 2),
434
+ height: Math.max(2, size.height - size.height % 2)
435
+ };
436
+ videoEncoder = new g.VideoEncoder({
437
+ output: onEncoded(video),
438
+ error: (error) => scope.postMessage({
439
+ type: "record-error",
440
+ message: String(error)
441
+ })
442
+ });
443
+ videoEncoder.configure({
444
+ codec: config.codec,
445
+ width: encodedSize.width,
446
+ height: encodedSize.height,
447
+ bitrate: config.bitrate,
448
+ framerate: config.fps,
449
+ latencyMode: "realtime"
450
+ });
451
+ }
452
+ if (videoEncoder.state !== "configured" || videoEncoder.encodeQueueSize > 4) {
453
+ frame.close();
454
+ return;
455
+ }
456
+ const us = anchor(video, frame.timestamp);
457
+ const keyFrame = forceKey || us - lastKeyUs >= config.timesliceMs * 1e3;
458
+ if (keyFrame) {
459
+ lastKeyUs = us;
460
+ forceKey = false;
461
+ }
462
+ const rebased = new g.VideoFrame(frame, { timestamp: us });
463
+ frame.close();
464
+ videoEncoder.encode(rebased, { keyFrame });
465
+ rebased.close();
466
+ };
467
+ /** AudioData cannot be re-timestamped in place; copying 10 ms is cheap. */
468
+ const retimed = (data, us) => {
469
+ const planes = data.format.endsWith("-planar") ? data.numberOfChannels : 1;
470
+ const sizes = [];
471
+ for (let plane = 0; plane < planes; plane += 1) sizes.push(data.allocationSize({ planeIndex: plane }));
472
+ const buffer = new Uint8Array(sizes.reduce((sum, size) => sum + size, 0));
473
+ let offset = 0;
474
+ for (let plane = 0; plane < planes; plane += 1) {
475
+ data.copyTo(buffer.subarray(offset, offset + sizes[plane]), { planeIndex: plane });
476
+ offset += sizes[plane];
477
+ }
478
+ return new g.AudioData({
479
+ format: data.format,
480
+ sampleRate: data.sampleRate,
481
+ numberOfFrames: data.numberOfFrames,
482
+ numberOfChannels: data.numberOfChannels,
483
+ timestamp: us,
484
+ data: buffer
485
+ });
486
+ };
487
+ const bytesPerSample = (format) => format.startsWith("u8") ? 1 : format.startsWith("s16") ? 2 : 4;
488
+ /** Zero-filled AudioData (silence for every signed / float format). */
489
+ const silence = (like, us, durationUs) => {
490
+ const frames = Math.max(1, Math.round(durationUs * like.sampleRate / 1e6));
491
+ return new g.AudioData({
492
+ format: like.format,
493
+ sampleRate: like.sampleRate,
494
+ numberOfFrames: frames,
495
+ numberOfChannels: like.numberOfChannels,
496
+ timestamp: us,
497
+ data: new Uint8Array(frames * like.numberOfChannels * bytesPerSample(like.format))
498
+ });
499
+ };
500
+ const encodeAudio = (data) => {
501
+ const config = encodeConfig;
502
+ if (!config || !audioWanted) {
503
+ data.close();
504
+ return;
505
+ }
506
+ if (!audioEncoder) try {
507
+ const encoderInstance = new g.AudioEncoder({
508
+ output: onEncoded(audio),
509
+ error: (error) => {
510
+ audioWanted = false;
511
+ audioEncoder = null;
512
+ scope.postMessage({
513
+ type: "record-error",
514
+ message: `audio encoder: ${String(error)}`
515
+ });
516
+ }
517
+ });
518
+ encoderInstance.configure({
519
+ codec: "opus",
520
+ sampleRate: data.sampleRate,
521
+ numberOfChannels: data.numberOfChannels,
522
+ bitrate: config.audioBitrate ?? 64e3
523
+ });
524
+ audioEncoder = encoderInstance;
525
+ audioFormat = {
526
+ sampleRate: data.sampleRate,
527
+ channels: data.numberOfChannels
528
+ };
529
+ } catch (error) {
530
+ audioWanted = false;
531
+ scope.postMessage({
532
+ type: "record-error",
533
+ message: `audio configure: ${String(error)}`
534
+ });
535
+ data.close();
536
+ return;
537
+ }
538
+ if (audioEncoder.state !== "configured" || audioEncoder.encodeQueueSize > 50) {
539
+ data.close();
540
+ return;
541
+ }
542
+ const us = anchor(audio, data.timestamp);
543
+ audioClockStartUs ??= us;
544
+ const driftUs = us - (audioClockStartUs + audioFedUs);
545
+ if (driftUs < -2e5) {
546
+ data.close();
547
+ return;
548
+ }
549
+ if (driftUs > 1e5) {
550
+ const fillUs = Math.min(driftUs, 5e6);
551
+ const filler = silence(data, audioClockStartUs + audioFedUs, fillUs);
552
+ audioEncoder.encode(filler);
553
+ filler.close();
554
+ audioFedUs += filler.numberOfFrames * 1e6 / filler.sampleRate;
555
+ }
556
+ audioFedUs += data.numberOfFrames * 1e6 / data.sampleRate;
557
+ const copy = retimed(data, us);
558
+ data.close();
559
+ audioEncoder.encode(copy);
560
+ copy.close();
561
+ };
562
+ const pumpAudio = async (readable) => {
563
+ const reader = readable.getReader();
564
+ try {
565
+ for (;;) {
566
+ const { value, done } = await reader.read();
567
+ if (done) return;
568
+ if (paused || recording !== "on") value.close();
569
+ else encodeAudio(value);
570
+ }
571
+ } catch (error) {
572
+ audioWanted = false;
573
+ scope.postMessage({
574
+ type: "record-error",
575
+ message: `audio source: ${String(error)}`
576
+ });
577
+ }
578
+ };
579
+ const stopRecording = async () => {
580
+ recording = "off";
581
+ for (const current of [videoEncoder, audioEncoder]) try {
582
+ if (current && current.state === "configured") await current.flush();
583
+ } catch {}
584
+ flushBefore(Infinity, true);
585
+ for (const current of [videoEncoder, audioEncoder]) try {
586
+ current?.close();
587
+ } catch {}
588
+ resetRecording();
589
+ scope.postMessage({ type: "record-stopped" });
590
+ };
144
591
  const pump = async (readable, gen) => {
145
592
  const reader = readable.getReader();
146
593
  activeReader = reader;
@@ -155,10 +602,21 @@ function cameraCompositorWorkerMain(scope) {
155
602
  frame.close();
156
603
  return;
157
604
  }
158
- if (paused || !writer) {
605
+ if (encodeConfig) {
606
+ if (paused || recording !== "on") frame.close();
607
+ else encodeVideo(frame);
608
+ if (announceNextFrame && !paused) {
609
+ announceNextFrame = false;
610
+ scope.postMessage({ type: "frame" });
611
+ }
612
+ continue;
613
+ }
614
+ const sinceLastUs = frame.timestamp - lastWrittenUs;
615
+ if (paused || !writer || sinceLastUs >= 0 && sinceLastUs < minFrameIntervalUs) {
159
616
  frame.close();
160
617
  continue;
161
618
  }
619
+ lastWrittenUs = frame.timestamp;
162
620
  await writer.write(scaled(frame));
163
621
  if (announceNextFrame) {
164
622
  announceNextFrame = false;
@@ -174,6 +632,8 @@ function cameraCompositorWorkerMain(scope) {
174
632
  };
175
633
  const switchSource = (readable) => {
176
634
  generation += 1;
635
+ lastWrittenUs = -Infinity;
636
+ reanchor();
177
637
  const previous = activeReader;
178
638
  activeReader = null;
179
639
  previous?.cancel().catch(() => {});
@@ -185,6 +645,12 @@ function cameraCompositorWorkerMain(scope) {
185
645
  case "start":
186
646
  maxLongEdge = message.maxLongEdge;
187
647
  maxShortEdge = message.maxShortEdge;
648
+ minFrameIntervalUs = message.minFrameIntervalUs ?? 0;
649
+ encodeConfig = message.encode ?? null;
650
+ if (encodeConfig && message.audio) {
651
+ hasAudioSource = true;
652
+ pumpAudio(message.audio);
653
+ }
188
654
  canvas = new g.OffscreenCanvas(1, 1);
189
655
  ctx = canvas.getContext("2d");
190
656
  writer = message.writable.getWriter();
@@ -200,6 +666,27 @@ function cameraCompositorWorkerMain(scope) {
200
666
  case "resume":
201
667
  paused = false;
202
668
  announceNextFrame = true;
669
+ reanchor();
670
+ break;
671
+ case "record-start":
672
+ resetRecording();
673
+ recording = "on";
674
+ break;
675
+ case "record-pause":
676
+ if (recording === "on") {
677
+ recording = "paused";
678
+ pauseStartedMs = performance.now();
679
+ }
680
+ break;
681
+ case "record-resume":
682
+ if (recording === "paused") {
683
+ pausedMs += performance.now() - pauseStartedMs;
684
+ recording = "on";
685
+ reanchor();
686
+ }
687
+ break;
688
+ case "record-stop":
689
+ stopRecording();
203
690
  break;
204
691
  default: break;
205
692
  }
@@ -216,6 +703,26 @@ const MAX_COMPOSITE_SHORT_EDGE = 720;
216
703
  const FRAME_INTERVAL_TOLERANCE_RATIO = .9;
217
704
  const DEFAULT_RESUME_TIMEOUT_MS = 2e3;
218
705
  /**
706
+ * Compositor for a multipart recording (ID, Selfie, Video Selfie): encodes
707
+ * inside its worker at the device's {@link getWorkerRecordingProfile} tier.
708
+ * Pair it with
709
+ * `WorkerEncoderRecordingProvider`, which falls back to `MediaRecorder` when
710
+ * in-worker encoding is unavailable.
711
+ */
712
+ function createRecordingCompositor() {
713
+ const profile = getWorkerRecordingProfile();
714
+ return new CameraCompositor({
715
+ fps: profile.fps,
716
+ maxLongEdge: profile.maxLongEdge,
717
+ maxShortEdge: profile.maxShortEdge,
718
+ paceWorkerFrames: profile.fps < 30,
719
+ encodeInWorker: {
720
+ bitrate: profile.bitrate,
721
+ timesliceMs: profile.timesliceMs
722
+ }
723
+ });
724
+ }
725
+ /**
219
726
  * Composites the *active* camera into a single, stable `MediaStream` to
220
727
  * record. Switching cameras (front ↔ back) just repoints the source — the
221
728
  * output stream and its audio track are never replaced — so a single
@@ -233,6 +740,10 @@ const DEFAULT_RESUME_TIMEOUT_MS = 2e3;
233
740
  * `captureStream`, for Safari, Firefox and strict-CSP hosts that block blob
234
741
  * workers.
235
742
  *
743
+ * With `encodeInWorker`, the worker transport also records: it encodes and
744
+ * muxes the WebM itself (see `startWorkerRecording`), so no `MediaRecorder`
745
+ * — which needs the main thread for every frame — is involved.
746
+ *
236
747
  * A worker that dies mid-recording (GPU-process crash, OOM) is released at
237
748
  * once; the composite track stays valid but receives no further frames, and
238
749
  * the next `resume()` reports `timed-out` so the caller reacquires the camera
@@ -243,6 +754,10 @@ const DEFAULT_RESUME_TIMEOUT_MS = 2e3;
243
754
  */
244
755
  var CameraCompositor = class {
245
756
  constructor(options = {}) {
757
+ this.workerEncoding = false;
758
+ this.onWorkerPart = null;
759
+ this.workerRecordingError = null;
760
+ this.workerRecordingStopped = null;
246
761
  this.canvas = null;
247
762
  this.ctx = null;
248
763
  this.video = null;
@@ -257,6 +772,10 @@ var CameraCompositor = class {
257
772
  this.videoTrack = null;
258
773
  this.composite = null;
259
774
  this.fps = options.fps ?? 30;
775
+ this.maxLongEdge = options.maxLongEdge ?? MAX_COMPOSITE_LONG_EDGE;
776
+ this.maxShortEdge = options.maxShortEdge ?? MAX_COMPOSITE_SHORT_EDGE;
777
+ this.paceWorkerFrames = options.paceWorkerFrames ?? false;
778
+ this.encodeInWorker = options.encodeInWorker;
260
779
  this.frameIntervalMs = this.fps > 0 ? 1e3 / this.fps : 0;
261
780
  this.resumeTimeoutMs = options.resumeTimeoutMs ?? DEFAULT_RESUME_TIMEOUT_MS;
262
781
  }
@@ -295,6 +814,66 @@ var CameraCompositor = class {
295
814
  } else this.videoTrack = stream.getVideoTracks()[0] ?? null;
296
815
  if (previousVideoTrack) previousVideoTrack.stop();
297
816
  }
817
+ /**
818
+ * True when this compositor records by itself: the worker transport started
819
+ * with `encodeInWorker` and the browser has `VideoEncoder`. Use the
820
+ * `*WorkerRecording` methods instead of a `MediaRecorder` then; the
821
+ * composite stream receives no frames.
822
+ */
823
+ get encodesInWorker() {
824
+ return this.workerEncoding && this.transport !== null;
825
+ }
826
+ /** Last in-worker encoder error, if any. */
827
+ get workerRecordingErrorMessage() {
828
+ return this.workerRecordingError;
829
+ }
830
+ /**
831
+ * Start a new WebM recording inside the worker. `onPart` receives whole
832
+ * WebM clusters (~`timesliceMs` each, keyframe first; the first part also
833
+ * carries the header), in order. Returns false when not encoding in-worker.
834
+ */
835
+ startWorkerRecording(onPart) {
836
+ if (!this.encodesInWorker) return false;
837
+ this.onWorkerPart = onPart;
838
+ this.workerRecordingError = null;
839
+ this.postToWorker({ type: "record-start" });
840
+ return true;
841
+ }
842
+ pauseWorkerRecording() {
843
+ if (!this.encodesInWorker || !this.onWorkerPart) return false;
844
+ this.postToWorker({ type: "record-pause" });
845
+ return true;
846
+ }
847
+ resumeWorkerRecording() {
848
+ if (!this.encodesInWorker || !this.onWorkerPart) return false;
849
+ this.postToWorker({ type: "record-resume" });
850
+ return true;
851
+ }
852
+ /**
853
+ * Flush the encoder and resolve once every part has reached `onPart`.
854
+ * Resolves at once if the worker is gone (its buffered frames are lost).
855
+ */
856
+ stopWorkerRecording() {
857
+ if (!this.onWorkerPart) return Promise.resolve();
858
+ if (!this.transport) {
859
+ this.onWorkerPart = null;
860
+ return Promise.resolve();
861
+ }
862
+ return new Promise((resolve) => {
863
+ this.workerRecordingStopped = () => {
864
+ this.workerRecordingStopped = null;
865
+ this.onWorkerPart = null;
866
+ resolve();
867
+ };
868
+ this.postToWorker({ type: "record-stop" });
869
+ });
870
+ }
871
+ /** Stop in-worker recording and drop whatever it still emits. */
872
+ cancelWorkerRecording() {
873
+ if (this.onWorkerPart && this.transport) this.postToWorker({ type: "record-stop" });
874
+ this.onWorkerPart = null;
875
+ this.workerRecordingStopped?.();
876
+ }
298
877
  /** The composite stream being recorded, if compositing is active. */
299
878
  get stream() {
300
879
  return this.composite ?? void 0;
@@ -386,6 +965,13 @@ var CameraCompositor = class {
386
965
  let worker = null;
387
966
  let generator = null;
388
967
  let blobUrl = null;
968
+ const codecs = globalThis;
969
+ const encode = this.encodeInWorker && typeof codecs.VideoEncoder === "function" && (!this.audioTrack || typeof codecs.AudioEncoder === "function") ? {
970
+ codec: "vp8",
971
+ bitrate: this.encodeInWorker.bitrate,
972
+ fps: this.fps,
973
+ timesliceMs: this.encodeInWorker.timesliceMs
974
+ } : null;
389
975
  try {
390
976
  const processor = createTrackProcessor(track);
391
977
  generator = createTrackGenerator();
@@ -396,14 +982,22 @@ var CameraCompositor = class {
396
982
  type: "fatal",
397
983
  message: event?.message || "worker error"
398
984
  });
985
+ const audioReadable = encode && this.audioTrack ? createTrackProcessor(this.audioTrack).readable : void 0;
399
986
  const message = {
400
987
  type: "start",
988
+ audio: audioReadable,
401
989
  readable: processor.readable,
402
990
  writable: generator.writable,
403
- maxLongEdge: MAX_COMPOSITE_LONG_EDGE,
404
- maxShortEdge: MAX_COMPOSITE_SHORT_EDGE
991
+ maxLongEdge: this.maxLongEdge,
992
+ maxShortEdge: this.maxShortEdge,
993
+ minFrameIntervalUs: this.paceWorkerFrames && this.frameIntervalMs > 0 ? this.frameIntervalMs * 1e3 * FRAME_INTERVAL_TOLERANCE_RATIO : 0,
994
+ encode: encode ?? void 0
405
995
  };
406
- worker.postMessage(message, [processor.readable, generator.writable]);
996
+ worker.postMessage(message, [
997
+ processor.readable,
998
+ generator.writable,
999
+ ...audioReadable ? [audioReadable] : []
1000
+ ]);
407
1001
  } catch {
408
1002
  worker?.terminate();
409
1003
  generator?.stop();
@@ -416,6 +1010,7 @@ var CameraCompositor = class {
416
1010
  blobUrl
417
1011
  };
418
1012
  this.generator = generator;
1013
+ this.workerEncoding = encode !== null;
419
1014
  return new MediaStream([generator]);
420
1015
  }
421
1016
  /** Terminates the worker and frees its blob; idempotent. */
@@ -424,6 +1019,7 @@ var CameraCompositor = class {
424
1019
  this.transport.worker.terminate();
425
1020
  URL.revokeObjectURL(this.transport.blobUrl);
426
1021
  this.transport = null;
1022
+ this.workerRecordingStopped?.();
427
1023
  }
428
1024
  startMainThread(stream) {
429
1025
  this.canvas = document.createElement("canvas");
@@ -448,6 +1044,15 @@ var CameraCompositor = class {
448
1044
  case "ended":
449
1045
  this.finishResume("video-ended");
450
1046
  break;
1047
+ case "part":
1048
+ this.onWorkerPart?.(new Blob([message.data], { type: "video/webm" }));
1049
+ break;
1050
+ case "record-stopped":
1051
+ this.workerRecordingStopped?.();
1052
+ break;
1053
+ case "record-error":
1054
+ this.workerRecordingError = message.message;
1055
+ break;
451
1056
  case "fatal":
452
1057
  this.releaseWorker();
453
1058
  this.finishResume("timed-out");
@@ -521,7 +1126,7 @@ var CameraCompositor = class {
521
1126
  getBoundedDimensions(width, height) {
522
1127
  const longEdge = Math.max(width, height);
523
1128
  const shortEdge = Math.min(width, height);
524
- const scale = Math.min(1, MAX_COMPOSITE_LONG_EDGE / longEdge, MAX_COMPOSITE_SHORT_EDGE / shortEdge);
1129
+ const scale = Math.min(1, this.maxLongEdge / longEdge, this.maxShortEdge / shortEdge);
525
1130
  return {
526
1131
  width: Math.max(1, Math.round(width * scale)),
527
1132
  height: Math.max(1, Math.round(height * scale))
@@ -673,6 +1278,82 @@ var MultipartRecordingProvider = class {
673
1278
  }
674
1279
  };
675
1280
  //#endregion
1281
+ //#region ../infra/src/providers/browser/WorkerEncoderRecordingProvider.ts
1282
+ /**
1283
+ * Multipart recorder for a `CameraCompositor` that encodes inside its worker
1284
+ * (ENG-55397): emits the worker's WebM clusters as parts, in order. When the
1285
+ * compositor could not start in-worker encoding (no insertable streams or
1286
+ * WebCodecs, strict CSP), it records the composite stream with
1287
+ * `MediaRecorder` instead, so callers wire it unconditionally.
1288
+ *
1289
+ * Why not always `MediaRecorder`: Chrome hands every recorded frame through
1290
+ * the page main thread and drops frames while it is blocked. On a Galaxy A01
1291
+ * Core the ID flow blocks it for 3–5 s (WASM + 1080p readbacks); a
1292
+ * `MediaRecorder` fed by the worker kept 74 of 149 frames under 1.5 s
1293
+ * freezes, the in-worker encoder 150 of 151.
1294
+ */
1295
+ var WorkerEncoderRecordingProvider = class {
1296
+ constructor(compositor) {
1297
+ this.compositor = compositor;
1298
+ this._isRecording = false;
1299
+ this._error = null;
1300
+ this.partNumber = 0;
1301
+ this.fallback = null;
1302
+ }
1303
+ get isRecording() {
1304
+ return this.fallback ? this.fallback.isRecording : this._isRecording;
1305
+ }
1306
+ get hasError() {
1307
+ return this.error !== null;
1308
+ }
1309
+ get error() {
1310
+ if (this.fallback) return this.fallback.error;
1311
+ return this._error ?? this.compositor.workerRecordingErrorMessage;
1312
+ }
1313
+ startRecording(stream, onPart) {
1314
+ this.reset();
1315
+ if (!this.compositor.encodesInWorker) {
1316
+ this.fallback = new MultipartRecordingProvider();
1317
+ this.fallback.startRecording(stream, onPart);
1318
+ return;
1319
+ }
1320
+ this.partNumber = 0;
1321
+ const started = this.compositor.startWorkerRecording((chunk) => {
1322
+ this.partNumber += 1;
1323
+ onPart({
1324
+ chunk,
1325
+ partNumber: this.partNumber
1326
+ });
1327
+ });
1328
+ this._isRecording = started;
1329
+ this._error = started ? null : "Failed to start recording: no encoder";
1330
+ }
1331
+ pauseRecording() {
1332
+ if (this.fallback) return this.fallback.pauseRecording();
1333
+ return this._isRecording && this.compositor.pauseWorkerRecording();
1334
+ }
1335
+ resumeRecording() {
1336
+ if (this.fallback) return this.fallback.resumeRecording();
1337
+ return this._isRecording && this.compositor.resumeWorkerRecording();
1338
+ }
1339
+ async stopRecording() {
1340
+ if (this.fallback) {
1341
+ await this.fallback.stopRecording();
1342
+ return;
1343
+ }
1344
+ if (!this._isRecording) return;
1345
+ this._isRecording = false;
1346
+ await this.compositor.stopWorkerRecording();
1347
+ }
1348
+ reset() {
1349
+ this.fallback?.reset();
1350
+ this.fallback = null;
1351
+ this.compositor.cancelWorkerRecording();
1352
+ this._isRecording = false;
1353
+ this._error = null;
1354
+ }
1355
+ };
1356
+ //#endregion
676
1357
  //#region ../infra/src/providers/browser/BrowserNetworkStatusProvider.ts
677
1358
  var BrowserNetworkStatusProvider = class {
678
1359
  onOnline(listener) {
@@ -957,15 +1638,15 @@ function httpStatusOf(error) {
957
1638
  * (S3 SSE-AES-256).
958
1639
  */
959
1640
  var MultipartRecordingService = class {
960
- constructor(scope = "videoselfie") {
1641
+ constructor(scope = "videoselfie", recorder = new MultipartRecordingProvider()) {
961
1642
  this.scope = scope;
962
- this.recorder = new MultipartRecordingProvider();
963
1643
  this.timer = BrowserTimerProvider.getInstance();
964
1644
  this.session = null;
965
1645
  this.uploader = null;
966
1646
  this.chunksEmitted = 0;
967
1647
  this.stopPromise = null;
968
1648
  this.abandonRequested = false;
1649
+ this.recorder = recorder;
969
1650
  }
970
1651
  get isRecording() {
971
1652
  return this.recorder.isRecording;
@@ -1084,4 +1765,4 @@ var MultipartRecordingService = class {
1084
1765
  }
1085
1766
  };
1086
1767
  //#endregion
1087
- export { StreamCanvasProcessingSession as i, CameraCompositor as n, getAdaptiveMediaRecorderOptions as r, MultipartRecordingService as t };
1768
+ export { getAdaptiveMediaRecorderOptions as a, createRecordingCompositor as i, WorkerEncoderRecordingProvider as n, StreamCanvasProcessingSession as o, CameraCompositor as r, MultipartRecordingService as t };