@blockcast/mmt-container 0.4.0-main.d6f2737828fc → 0.4.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.
Files changed (102) hide show
  1. package/README.md +12 -9
  2. package/dist/browser-runtime.js +14219 -13716
  3. package/dist/browser-runtime.js.map +4 -4
  4. package/dist/carp-catalog.d.ts +1 -12
  5. package/dist/carp-catalog.d.ts.map +1 -1
  6. package/dist/carp-catalog.js +14 -6
  7. package/dist/carp-catalog.js.map +4 -4
  8. package/dist/catalog-fec-params.d.ts +5 -2
  9. package/dist/catalog-fec-params.d.ts.map +1 -1
  10. package/dist/catalog-fec-params.js +101 -42
  11. package/dist/catalog-fec-params.js.map +4 -4
  12. package/dist/catalog-network-source.js +4 -6
  13. package/dist/catalog-network-source.js.map +4 -4
  14. package/dist/cmcd-producer.d.ts.map +1 -1
  15. package/dist/cmcd-producer.js +0 -4
  16. package/dist/cmcd-producer.js.map +2 -2
  17. package/dist/delivery-catalog.d.ts +124 -0
  18. package/dist/delivery-catalog.d.ts.map +1 -0
  19. package/dist/delivery-catalog.js +1867 -0
  20. package/dist/delivery-catalog.js.map +7 -0
  21. package/dist/fec-manager.d.ts +17 -4
  22. package/dist/fec-manager.d.ts.map +1 -1
  23. package/dist/fec-manager.js +41 -20
  24. package/dist/fec-manager.js.map +4 -4
  25. package/dist/fec-state.d.ts +3 -2
  26. package/dist/fec-state.d.ts.map +1 -1
  27. package/dist/fec-state.js +17 -15
  28. package/dist/fec-state.js.map +4 -4
  29. package/dist/fec-worker-setup.d.ts +2 -8
  30. package/dist/fec-worker-setup.d.ts.map +1 -1
  31. package/dist/fec-worker-setup.js +12 -62
  32. package/dist/fec-worker-setup.js.map +4 -4
  33. package/dist/index.d.ts +2 -1
  34. package/dist/index.d.ts.map +1 -1
  35. package/dist/index.js +10 -0
  36. package/dist/index.js.map +2 -2
  37. package/dist/iso-block-locator.js +4 -6
  38. package/dist/iso-block-locator.js.map +4 -4
  39. package/dist/mfu-reassembler.js +27 -29
  40. package/dist/mfu-reassembler.js.map +4 -4
  41. package/dist/mmtp-container-parser.d.ts +9 -11
  42. package/dist/mmtp-container-parser.d.ts.map +1 -1
  43. package/dist/mmtp-container-parser.js +107 -98
  44. package/dist/mmtp-container-parser.js.map +4 -4
  45. package/dist/mmtp-container-parser.no-modules.js +133 -108
  46. package/dist/mmtp-container-parser.no-modules.js.map +4 -4
  47. package/dist/mmtp-decode-pipeline.js +107 -98
  48. package/dist/mmtp-decode-pipeline.js.map +4 -4
  49. package/dist/mmtp-router.d.ts +34 -14
  50. package/dist/mmtp-router.d.ts.map +1 -1
  51. package/dist/mmtp-router.js +143 -113
  52. package/dist/mmtp-router.js.map +4 -4
  53. package/dist/multicast-input.js +4 -6
  54. package/dist/multicast-input.js.map +4 -4
  55. package/dist/multicast-manager.d.ts +39 -3
  56. package/dist/multicast-manager.d.ts.map +1 -1
  57. package/dist/multicast-manager.js +264 -156
  58. package/dist/multicast-manager.js.map +4 -4
  59. package/dist/multicast-transport.js +14 -14
  60. package/dist/multicast-transport.js.map +4 -4
  61. package/dist/multicast-unwrap.js +32 -34
  62. package/dist/multicast-unwrap.js.map +4 -4
  63. package/dist/no-modules.js +130 -107
  64. package/dist/no-modules.js.map +4 -4
  65. package/dist/repair-fetch.d.ts +86 -2
  66. package/dist/repair-fetch.d.ts.map +1 -1
  67. package/dist/repair-fetch.js +86 -17
  68. package/dist/repair-fetch.js.map +4 -4
  69. package/dist/repair-layers.js +4 -6
  70. package/dist/repair-layers.js.map +4 -4
  71. package/dist/signaling.js +5 -7
  72. package/dist/signaling.js.map +4 -4
  73. package/dist/timestamp-codec.js +27 -29
  74. package/dist/timestamp-codec.js.map +4 -4
  75. package/dist/wasm-mfu-reassembler.js +4 -6
  76. package/dist/wasm-mfu-reassembler.js.map +4 -4
  77. package/dist/wasm-mmtp-container-parser.js +107 -98
  78. package/dist/wasm-mmtp-container-parser.js.map +4 -4
  79. package/package.json +5 -6
  80. package/src/carp-catalog.ts +16 -11
  81. package/src/catalog-fec-params.ts +38 -17
  82. package/src/cmcd-producer.ts +0 -6
  83. package/src/delivery-catalog.ts +498 -0
  84. package/src/fec-manager.ts +44 -17
  85. package/src/fec-state.ts +4 -9
  86. package/src/fec-worker-setup.ts +4 -35
  87. package/src/index.ts +27 -0
  88. package/src/mmtp-container-parser.ts +80 -20
  89. package/src/mmtp-router.ts +71 -29
  90. package/src/multicast-manager.ts +93 -26
  91. package/src/repair-fetch.ts +228 -17
  92. package/test-vectors/catalog/README.md +10 -1
  93. package/test-vectors/catalog/negative/multicast-endpoint-relay-address.json +39 -0
  94. package/test-vectors/catalog/negative/raptorq-unaligned-symbol.json +1 -0
  95. package/test-vectors/catalog/positive/multicast-amt-manual-relay.json +38 -0
  96. package/test-vectors/catalog/section-codes.json +33 -0
  97. package/test-vectors/delivery-catalog/README.md +11 -0
  98. package/test-vectors/delivery-catalog/negative/missing-block-profile.json +58 -0
  99. package/test-vectors/delivery-catalog/negative/packet-id-signaling.json +59 -0
  100. package/test-vectors/delivery-catalog/negative/retired-k-padded-block-profile.json +59 -0
  101. package/test-vectors/delivery-catalog/positive/dash.json +56 -0
  102. package/test-vectors/delivery-catalog/positive/mmtp-source-only.json +43 -0
@@ -4,7 +4,7 @@
4
4
  * Wires a FecWorkerClient into a MulticastManager for a specific track.
5
5
  * Both moq-lib (`hang-mmt-fec/js/watch/src/element.ts`) and moqtail
6
6
  * (`moqtail-private/apps/client-js/src/lib/player.ts`) call this from their
7
- * SessionManager `fec-setup` handler so the Worker+ALTA setup lives in one
7
+ * SessionManager `fec-setup` handler so the Worker setup lives in one
8
8
  * place instead of being duplicated across ~80 lines per player.
9
9
  *
10
10
  * Callers are responsible for holding the returned `dispose()` and calling it
@@ -29,8 +29,6 @@ export interface FecWorkerAssetUrls {
29
29
  fecWorker: string
30
30
  /** URL to the RaptorQ WASM module (compiled via compileStreaming). */
31
31
  rqWasm: string
32
- /** URL to the ALTA WASM module. Fetched only when an ALTA key is provided. */
33
- altaWasm: string
34
32
  }
35
33
 
36
34
  export interface FecWorkerSetupOptions {
@@ -55,7 +53,7 @@ export interface FecWorkerSetupOptions {
55
53
  * block distance from below; it never strides SS_IDs into blocks.
56
54
  */
57
55
  fecK: number
58
- /** Symbol size in bytes (T) — the ALTA-adjusted value if ALTA is signed. */
56
+ /** Symbol size in bytes (T). */
59
57
  fecSymbolSize: number
60
58
  /** Repair symbols per block (P). */
61
59
  fecRepairCount: number
@@ -85,8 +83,6 @@ export interface FecWorkerSetupOptions {
85
83
  codec?: string
86
84
  /** Video framerate (optional metadata forwarded to the worker). */
87
85
  framerate?: number
88
- /** Base64 SPKI public key (optional). Accepts padded/unpadded, std or url-safe alphabet. */
89
- altaPublicKeySpki?: string
90
86
  /** Absolute or origin-relative asset URLs. */
91
87
  assetUrls: FecWorkerAssetUrls
92
88
  /** Optional FecWorkerClient factory — main use is test injection. */
@@ -96,8 +92,6 @@ export interface FecWorkerSetupOptions {
96
92
  export interface FecWorkerSetupResult {
97
93
  /** The configured client (already registered with MulticastManager). */
98
94
  client: FecWorkerClient
99
- /** True when the ALTA public key decoded and the ALTA WASM compiled. */
100
- altaEnabled: boolean
101
95
  /** Removes the Worker from MulticastManager routing, then releases it. */
102
96
  dispose: () => void
103
97
  }
@@ -234,32 +228,9 @@ export async function setupFecWorkerForTrack(
234
228
  ? opts.clientFactory(workerUrl)
235
229
  : new FecWorkerCtor.FecWorkerClient(workerUrl)
236
230
 
237
- // Compile RaptorQ WASM once; ALTA WASM only when we have a key to decode.
231
+ // Compile RaptorQ WASM once.
238
232
  const rqWasm = await WebAssembly.compileStreaming(fetch(opts.assetUrls.rqWasm))
239
233
 
240
- let altaKeyBuffer: ArrayBuffer | undefined
241
- let altaWasmModule: WebAssembly.Module | undefined
242
- if (opts.altaPublicKeySpki) {
243
- const b64 = opts.altaPublicKeySpki.replace(/-/g, '+').replace(/_/g, '/')
244
- const padded = b64.padEnd(Math.ceil(b64.length / 4) * 4, '=')
245
- try {
246
- const keyBytes = Uint8Array.from(atob(padded), c => c.charCodeAt(0))
247
- // SPKI size range: Ed25519=44, P-256=91; tolerate 32..=256 for forward-compat.
248
- if (keyBytes.length >= 32 && keyBytes.length <= 256) {
249
- altaKeyBuffer = keyBytes.buffer
250
- altaWasmModule = await WebAssembly.compileStreaming(fetch(opts.assetUrls.altaWasm))
251
- } else {
252
- console.warn(
253
- `[fec-worker-setup] ALTA SPKI wrong length for "${opts.trackName}": ` +
254
- `${keyBytes.length} (expected 32..256) — ALTA disabled`,
255
- )
256
- }
257
- } catch (e) {
258
- console.warn(`[fec-worker-setup] ALTA key decode / WASM compile failed for "${opts.trackName}":`, e)
259
- }
260
- }
261
-
262
- const altaEnabled = !!(altaKeyBuffer && altaWasmModule)
263
234
  const fecTrackConfig = {
264
235
  codec: opts.codec ?? 'unknown',
265
236
  trackName: opts.trackName,
@@ -278,11 +249,10 @@ export async function setupFecWorkerForTrack(
278
249
  deliveryWindowMs,
279
250
  fecMode: 'subframe' as const,
280
251
  repairTrack: opts.repairTrackName,
281
- altaEnabled,
282
252
  relayBlockSigEnabled: false,
283
253
  } as unknown as FecTrackConfig
284
254
 
285
- client.configure(fecTrackConfig, rqWasm, altaKeyBuffer, altaWasmModule)
255
+ client.configure(fecTrackConfig, rqWasm)
286
256
  try {
287
257
  opts.multicastManager.setFecWorker(opts.packetId, client, opts.trackType, {
288
258
  blockProfile,
@@ -310,7 +280,6 @@ export async function setupFecWorkerForTrack(
310
280
  let disposed = false
311
281
  return {
312
282
  client,
313
- altaEnabled,
314
283
  dispose: () => {
315
284
  if (disposed) return
316
285
  disposed = true
package/src/index.ts CHANGED
@@ -150,6 +150,28 @@ export {
150
150
  archiveOrigin,
151
151
  } from './carp-catalog.js'
152
152
 
153
+ // Strict v2 multicast delivery catalog (DASH/HLS/MMTP routes), moved from
154
+ // moqtail-private so players share one contract (BLO-47003).
155
+ export {
156
+ normalizeDeliveryCatalog,
157
+ type DeliveryPackaging,
158
+ type DeliveryContentType,
159
+ type DeliveryEndpoint,
160
+ type DeliveryPolicy,
161
+ type DeliveryObjectMapping,
162
+ type DeliveryFec,
163
+ type DeliveryRouteBase,
164
+ type DashDeliveryRoute,
165
+ type HlsDeliveryRoute,
166
+ type MmtpDeliveryRoute,
167
+ type DashDeliveryConfig,
168
+ type HlsDeliveryConfig,
169
+ type MmtpDeliveryConfig,
170
+ type DeliveryMulticastBase,
171
+ type DeliveryMulticastV2,
172
+ type DeliveryCatalogV2,
173
+ } from './delivery-catalog.js'
174
+
153
175
  // Multicast input contract — validator + builder for publisher-emitted
154
176
  // catalogs (draft-ramadan-moq-multicast-00 §4.1, BLO-3323 A.1).
155
177
  // `multicastFromEnv` is the env-var helper used by FFmpeg and the
@@ -530,6 +552,11 @@ export {
530
552
  type TrackFecParams,
531
553
  type TrackFecUnavailable,
532
554
  catalogFecUnavailable,
555
+ catalogFecAvailable,
556
+ catalogFecDeclaresUnconfigurableInstance,
557
+ locateCatalogFecSource,
558
+ type CatalogFecRole,
559
+ type CatalogFecSource,
533
560
  catalogGroupDurationMs,
534
561
  catalogKeyframeCadenceMs,
535
562
  catalogFecMediaUnitsPerSecond,
@@ -2,7 +2,6 @@ import {
2
2
  parseMMTPHeader,
3
3
  extractMFUPayloadWithHeader,
4
4
  sourceFecPayloadIdOf,
5
- extractAltaTrailer,
6
5
  PACKET_TYPE_MPU,
7
6
  PACKET_TYPE_REPAIR,
8
7
  type MMTPHeader,
@@ -453,16 +452,6 @@ export function isMfuKeyframeMetadata(rapFlag: boolean, priority?: number): bool
453
452
  return rapFlag || (priority ?? 0) > 0x80
454
453
  }
455
454
 
456
- /** Per-packet detached ALTA authenticator trailer (08-07 Path 2).
457
- * - `auth`: the authenticator bytes (length-prefixed at packet end).
458
- * - `payloadEnd`: byte offset within the source packet where signed
459
- * payload ends — consumers reconstruct `payload = packet[0..payloadEnd]`
460
- * for verify_detached. */
461
- export interface ParsedAltaTrailer {
462
- auth: Uint8Array
463
- payloadEnd: number
464
- }
465
-
466
455
  /** Source-symbol exposure for FEC consumers.
467
456
  *
468
457
  * Fires from {@link MmtpContainerParser.feed} once per FEC-protected source
@@ -475,7 +464,6 @@ export interface ParsedAltaTrailer {
475
464
  * - `rawSymbol` – packet bytes minus the 4-byte fec_payload_id trailer.
476
465
  * NOT zero-padded to FEC symbol size — consumer pads if needed
477
466
  * (FEC symbol size is FEC-config, not parser-config).
478
- * - `alta` – detached ALTA authenticator if present, else undefined
479
467
  *
480
468
  * Designed so consumers (player, Shaka shim, upstream moqtail) can wire
481
469
  * source-symbol delivery to whatever FEC pipeline they own, without depending
@@ -484,7 +472,6 @@ type SourceSymbolCallback = (
484
472
  packetId: number,
485
473
  ssId: number,
486
474
  rawSymbol: Uint8Array,
487
- alta: ParsedAltaTrailer | undefined,
488
475
  ) => void
489
476
 
490
477
  export interface ParseHeaderOptions {
@@ -758,6 +745,48 @@ class FecReleaseSequencer {
758
745
  return total
759
746
  }
760
747
 
748
+ #deferDepth = 0
749
+ #deferredStreams = new Set<GatedStream>()
750
+
751
+ /**
752
+ * Run `work` with the drains its pushes would run deferred to its end. A
753
+ * recovered FEC block is routed one frame at a time, and its block is
754
+ * already resolved when the first frame is pushed: draining on that push
755
+ * would release past the gap before the block's other recovered frames
756
+ * arrive (deciding a conceal or stall for media that is about to land),
757
+ * and those frames would then be dropped as stale. Nests.
758
+ */
759
+ defer(work: () => void): void {
760
+ this.#deferDepth++
761
+ let threw = true
762
+ try {
763
+ work()
764
+ threw = false
765
+ } finally {
766
+ this.#deferDepth--
767
+ if (this.#deferDepth === 0 && this.#deferredStreams.size > 0) {
768
+ const streams = [...this.#deferredStreams]
769
+ this.#deferredStreams.clear()
770
+ if (!threw) {
771
+ for (const stream of streams) this.#drain(stream)
772
+ } else {
773
+ // `work` is unwinding: still release what it pushed, but never let
774
+ // a drain failure replace the exception that is propagating.
775
+ try {
776
+ for (const stream of streams) this.#drain(stream)
777
+ } catch {
778
+ // The original exception wins; the next push or poke drains again.
779
+ }
780
+ }
781
+ }
782
+ }
783
+ }
784
+
785
+ #drainUnlessDeferred(stream: GatedStream): void {
786
+ if (this.#deferDepth > 0) this.#deferredStreams.add(stream)
787
+ else this.#drain(stream)
788
+ }
789
+
761
790
  push(streamKey: string, data: Uint8Array, meta: FrameMeta, seq: number): void {
762
791
  const frame: GatedFrame = { data, meta: { ...meta }, seq, arrivedAtMs: performance.now() }
763
792
  const stream = this.#streamFor(streamKey, meta)
@@ -781,7 +810,7 @@ class FecReleaseSequencer {
781
810
  return
782
811
  }
783
812
  this.#release(stream, frame, undefined)
784
- this.#drain(stream)
813
+ this.#drainUnlessDeferred(stream)
785
814
  return
786
815
  }
787
816
  let lo = 0
@@ -792,7 +821,24 @@ class FecReleaseSequencer {
792
821
  else hi = mid
793
822
  }
794
823
  stream.buf.splice(lo, 0, frame)
795
- this.#drain(stream)
824
+ this.#drainUnlessDeferred(stream)
825
+ }
826
+
827
+ /**
828
+ * Arrival time of the earliest frame a stream of `packetId` holds behind a
829
+ * gap (the gate forces it out `holdLimitMs` after that), or undefined when
830
+ * none is held. Without `catalogTrackName`, every stream of the packetId.
831
+ */
832
+ earliestHeldArrivalMs(packetId: number, catalogTrackName?: string): number | undefined {
833
+ let earliest: number | undefined
834
+ for (const stream of this.#streams.values()) {
835
+ if (stream.packetId !== packetId) continue
836
+ if (catalogTrackName !== undefined && stream.catalogTrackName !== catalogTrackName) continue
837
+ for (const frame of stream.buf) {
838
+ if (earliest === undefined || frame.arrivedAtMs < earliest) earliest = frame.arrivedAtMs
839
+ }
840
+ }
841
+ return earliest
796
842
  }
797
843
 
798
844
  /** Re-evaluate held gaps after FEC state changed. */
@@ -1266,6 +1312,24 @@ export class MmtpContainerParser {
1266
1312
  : null
1267
1313
  }
1268
1314
 
1315
+ /**
1316
+ * Run `work` with the FEC release gate's drains deferred to its end, so
1317
+ * every frame it pushes is in place before any held gap is decided (see
1318
+ * FecReleaseSequencer.defer).
1319
+ */
1320
+ deferFecRelease(work: () => void): void {
1321
+ if (!this.#fecSequencer) {
1322
+ work()
1323
+ return
1324
+ }
1325
+ this.#fecSequencer.defer(work)
1326
+ }
1327
+
1328
+ /** See FecReleaseSequencer.earliestHeldArrivalMs. */
1329
+ fecReleaseEarliestHeldAtMs(packetId: number, catalogTrackName?: string): number | undefined {
1330
+ return this.#fecSequencer?.earliestHeldArrivalMs(packetId, catalogTrackName)
1331
+ }
1332
+
1269
1333
  /** Re-evaluate held gaps after FEC block state changed. */
1270
1334
  pokeFecRelease(packetId?: number, catalogTrackName?: string): void {
1271
1335
  if (!this.#fecSequencer) return
@@ -1528,12 +1592,8 @@ export class MmtpContainerParser {
1528
1592
  // Symbol-size zero-padding is the consumer's responsibility — FEC
1529
1593
  // symbol size is a FEC-config concern, not a parser concern.
1530
1594
  const rawSymbol = packet.subarray(0, packet.length - 4)
1531
- const altaTrailer = extractAltaTrailer(packet)
1532
- const alta = altaTrailer
1533
- ? { auth: altaTrailer.auth, payloadEnd: altaTrailer.signedEnd }
1534
- : undefined
1535
1595
  try {
1536
- this.#sourceSymbolCb(info.packetId, info.fecPayloadId.ssId, rawSymbol, alta)
1596
+ this.#sourceSymbolCb(info.packetId, info.fecPayloadId.ssId, rawSymbol)
1537
1597
  } catch {
1538
1598
  // Source-symbol consumer threw — never block MFU extraction on
1539
1599
  // FEC-pipeline errors. The frame path stays viable; the consumer
@@ -423,6 +423,24 @@ export type FrameCallback = (data: Uint8Array, meta: RoutedFrameMeta) => void;
423
423
  export type RoutedPacketMeta = {
424
424
  packetId: number;
425
425
  packetSequenceNumber: number;
426
+ /**
427
+ * Raw MMTP header `timestamp`: the packet's UTC sending instant in NTP short
428
+ * format (ISO/IEC 23008-1 §9.2.2), which wraps every 2^16 s. Same unit as
429
+ * `RoutedFrameMeta.mmtpTimestamp`, and unlike `RoutedFrameMeta.timestamp`
430
+ * it is not an authored media time. Order it only with serial arithmetic
431
+ * (`compareNtpShort`). `MulticastOrchestrator.onTierPacket` takes it, as a
432
+ * number, as its `timestamp`.
433
+ */
434
+ mmtpTimestamp: bigint;
435
+ /**
436
+ * The MMTP header RAP flag: the packet starts a random access point. On a
437
+ * `recovered` packet it is read from the FEC-reconstructed header, which is
438
+ * byte-exact, so it is the flag the publisher sent. The router's own gap
439
+ * decisions deliberately ignore it for recovered frames (`#isRapForGap`
440
+ * judges those by bitstream only); a host using it as a tier-switch commit
441
+ * point should decide explicitly whether a recovered RAP qualifies.
442
+ */
443
+ rap: boolean;
426
444
  /** True when FEC reconstructed this source packet. */
427
445
  recovered: boolean;
428
446
  /** True when the packet has no trailing Source FEC Payload ID. */
@@ -531,21 +549,17 @@ export interface FECManagerLike {
531
549
  * `symbolData` is the complete MMTP packet without its Source FEC Payload
532
550
  * ID, unpadded and possibly a view into the received packet. The decoder
533
551
  * builds the ssbg_mode1 symbol (`u16 len || packet || 00h`, zero-filled to
534
- * the packet's own symbol size T) from it; nothing pads it on the way.
535
- *
536
- * Optional `alta` carries the detached ALTA authenticator extracted from
537
- * the MMTP header-extension trailer (08-07 Path 2) — present on signed
538
- * source packets, absent on relay-reassembled / unsigned streams. When
539
- * provided, the fec-worker runs verify_detached instead of counting the
540
- * symbol as `altaPending`. */
552
+ * the packet's own symbol size T) from it; nothing pads it on the way. */
541
553
  feedSourceSymbol(
542
554
  packetId: number,
543
555
  ssId: number,
544
556
  symbolData: Uint8Array,
545
- alta?: { auth: Uint8Array; payloadEnd: number },
546
557
  catalogTrackName?: string,
547
558
  ): Uint8Array | null;
548
559
 
560
+ /** A block's RSB_length, from one of its repair symbols (optional). */
561
+ noteRepairGroupLength?(sbn: number, packetId: number, rsbLength: number, catalogTrackName?: string): void;
562
+
549
563
  /** Feed a repair symbol to the FEC decoder for a given track.
550
564
  * Returns recovered block bytes if recovery completes, else null. */
551
565
  feedRepairSymbol(
@@ -956,6 +970,8 @@ export class MmtpRouter {
956
970
  // ── Keyframe loss detection state ───────────────────────────────
957
971
  /** Catalog-derived keyframe loss timeout (ms), or a reader evaluated at each arm. null = not configured (warns on video track registration; Phase-13 keyframe repair stays off). */
958
972
  #keyframeLostTimeoutMs: number | (() => number) | null;
973
+ /** packetIds already warned for running without Phase 13 (non-FEC video only). */
974
+ readonly #warnedNoKeyframeLostTimeout = new Set<number>();
959
975
  /** Why the last reader evaluation left the timer unarmed; null while arming. Dedups the warning. */
960
976
  #keyframeLostUnarmedReason: string | null = null;
961
977
  /** Active keyframe loss timer handle. Single timer per video track — cleared on each rapFlag and on dispose(). */
@@ -1462,18 +1478,14 @@ export class MmtpRouter {
1462
1478
 
1463
1479
  /** Register MMTP packetId → track type mapping.
1464
1480
  * Call BEFORE packets arrive to ensure correct routing from the first packet.
1465
- * If keyframeLostTimeoutMs is not provided, Phase 13 keyframe repair is
1466
- * disabled (video still routes/decodes normally). framerate is OPTIONAL per
1467
- * draft-ietf-moq-catalogformat-01 §3.2.23, so we can't require the timeout. */
1481
+ * Without keyframeLostTimeoutMs a track WITHOUT FEC has no Phase 13
1482
+ * keyframe repair (video still routes/decodes normally); that is warned at
1483
+ * its first keyframe, once the FEC configuration is known (see
1484
+ * #noteEmittedKeyframe). Nothing is warned here: an FEC track never uses
1485
+ * the timer, and at registration the FEC configuration may not be applied
1486
+ * yet. framerate is OPTIONAL per draft-ietf-moq-catalogformat-01 §3.2.23,
1487
+ * so we can't require the timeout. */
1468
1488
  setPacketIdType(packetId: number, trackType: "video" | "audio"): void {
1469
- if (trackType === "video" && this.#keyframeLostTimeoutMs === null) {
1470
- console.warn(
1471
- "[MmtpRouter] keyframeLostTimeoutMs not configured — Phase 13 keyframe " +
1472
- "repair disabled for this track. Set it via setKeyframeLostTimeoutMs() " +
1473
- "or provide in constructor config to enable: " +
1474
- "Math.ceil(groupDurationMs * KEYFRAME_LOST_TIMEOUT_SLACK).",
1475
- );
1476
- }
1477
1489
  if (trackType === "video") this.#videoPacketIds.add(packetId);
1478
1490
  else this.#audioPacketIds.add(packetId);
1479
1491
  // Sync track info to parser for codec-aware keyframe detection.
@@ -1993,7 +2005,7 @@ export class MmtpRouter {
1993
2005
  #handleIsoRepair(
1994
2006
  ownerPacketId: number,
1995
2007
  catalogTrackName: string | undefined,
1996
- repair: { ssStart: number; ssbLength: number; rsId: number; symbolData: Uint8Array },
2008
+ repair: { ssStart: number; ssbLength: number; rsbLength: number; rsId: number; symbolData: Uint8Array },
1997
2009
  source: "multicast" | "moq",
1998
2010
  ): void {
1999
2011
  const fec = this.#fecManager;
@@ -2019,6 +2031,7 @@ export class MmtpRouter {
2019
2031
  catalogTrackName,
2020
2032
  )) return;
2021
2033
  fec.noteBlockActivity?.(blockStart, ownerPacketId, catalogTrackName);
2034
+ fec.noteRepairGroupLength?.(blockStart, ownerPacketId, repair.rsbLength, catalogTrackName);
2022
2035
  const alreadyCompleted = fec.isBlockCompleted(blockStart, ownerPacketId, catalogTrackName);
2023
2036
  const recovered = fec.feedRepairSymbol(
2024
2037
  ownerPacketId,
@@ -2066,7 +2079,7 @@ export class MmtpRouter {
2066
2079
  }
2067
2080
  if (payload.oversized) continue;
2068
2081
  const alreadyCompleted = fec.isBlockCompleted(blockStart, ownerPacketId, catalogTrackName);
2069
- const recovered = fec.feedSourceSymbol(ownerPacketId, ssId, payload.symbolData, undefined, catalogTrackName);
2082
+ const recovered = fec.feedSourceSymbol(ownerPacketId, ssId, payload.symbolData, catalogTrackName);
2070
2083
  if (!recovered || alreadyCompleted) continue;
2071
2084
  fec.cancelBlockTimeout(blockStart, ownerPacketId, catalogTrackName);
2072
2085
  fec.countRecovery(ownerPacketId, blockStart, catalogTrackName);
@@ -2110,7 +2123,9 @@ export class MmtpRouter {
2110
2123
  packetId: number,
2111
2124
  catalogTrackName?: string,
2112
2125
  ): void {
2113
- this.#routeRecoveredBlock(blockId, data, packetId, catalogTrackName);
2126
+ // A Worker-recovered block arrives here whole: defer the gate's drains
2127
+ // until all its frames are in, as the in-band recovery path does.
2128
+ this.#withDeferredFecPokes(() => this.#routeRecoveredBlock(blockId, data, packetId, catalogTrackName));
2114
2129
  }
2115
2130
 
2116
2131
  /** Route a single recovered source symbol through the parser + dedup pipeline.
@@ -2586,10 +2601,21 @@ export class MmtpRouter {
2586
2601
  #noteEmittedKeyframe(packetId: number, keyframe: boolean): void {
2587
2602
  if (!keyframe) return;
2588
2603
  if (!this.#videoPacketIds.has(packetId)) return;
2589
- if (this.#keyframeLostTimeoutMs === null) return;
2590
- // FEC tracks resolve lost keyframes through block deadlines and the
2591
- // release gate's stall decision, never through this timer.
2604
+ // FEC tracks resolve lost keyframes through block deadlines, the D-28
2605
+ // repair FETCH and the release gate's stall decision, never through this
2606
+ // timer, so an unconfigured timer is no loss there and is not warned.
2592
2607
  if (this.#isFecTrack(packetId)) return;
2608
+ if (this.#keyframeLostTimeoutMs === null) {
2609
+ if (!this.#warnedNoKeyframeLostTimeout.has(packetId)) {
2610
+ this.#warnedNoKeyframeLostTimeout.add(packetId);
2611
+ console.warn(
2612
+ `[MmtpRouter] keyframeLostTimeoutMs not configured — Phase 13 keyframe ` +
2613
+ `repair disabled for packetId=${packetId}, a video track without FEC. ` +
2614
+ "Set it via setKeyframeLostTimeoutMs() or provide in constructor config to enable.",
2615
+ );
2616
+ }
2617
+ return;
2618
+ }
2593
2619
  this.#lastKeyframeGroupId++;
2594
2620
  this.#armKeyframeLostTimer();
2595
2621
  }
@@ -2604,7 +2630,10 @@ export class MmtpRouter {
2604
2630
  #withDeferredFecPokes(work: () => void): void {
2605
2631
  this.#fecPokeDepth++;
2606
2632
  try {
2607
- work();
2633
+ // The gate's own drains wait for the packet too: a recovered block's
2634
+ // frames are pushed one at a time, and a drain on the first would
2635
+ // release past the others before they arrive.
2636
+ this.#parser.deferFecRelease(work);
2608
2637
  } finally {
2609
2638
  this.#fecPokeDepth--;
2610
2639
  if (this.#fecPokeDepth === 0 && this.#pendingFecPokes.size > 0 && !this.#disposed) {
@@ -2617,6 +2646,14 @@ export class MmtpRouter {
2617
2646
  }
2618
2647
  }
2619
2648
 
2649
+ /**
2650
+ * Arrival time of the earliest frame the FEC release gate holds for this
2651
+ * stream (see FecReleaseSequencer.earliestHeldArrivalMs), or undefined.
2652
+ */
2653
+ fecReleaseEarliestHeldAtMs(packetId: number, catalogTrackName?: string): number | undefined {
2654
+ return this.#parser.fecReleaseEarliestHeldAtMs(packetId, catalogTrackName);
2655
+ }
2656
+
2620
2657
  #requestFecPoke(packetId: number, catalogTrackName: string | undefined): void {
2621
2658
  if (this.#disposed) return;
2622
2659
  if (this.#fecPokeDepth > 0) {
@@ -3182,6 +3219,8 @@ export class MmtpRouter {
3182
3219
  catalogTrackName,
3183
3220
  );
3184
3221
  this.#routeTrackPacket(packet, info.packetId, info.sequenceNumber, {
3222
+ mmtpTimestamp: parsed.header.timestamp,
3223
+ rap: info.rapFlag,
3185
3224
  recovered: false,
3186
3225
  skipFecPayloadId: false,
3187
3226
  catalogTrackName,
@@ -3251,13 +3290,11 @@ export class MmtpRouter {
3251
3290
  info.packetId,
3252
3291
  sourceFec.ssId,
3253
3292
  sourceFec.symbolData,
3254
- undefined,
3255
3293
  )
3256
3294
  : this.#fecManager.feedSourceSymbol(
3257
3295
  info.packetId,
3258
3296
  sourceFec.ssId,
3259
3297
  sourceFec.symbolData,
3260
- undefined,
3261
3298
  catalogTrackName,
3262
3299
  );
3263
3300
  if (recovered) {
@@ -3462,7 +3499,7 @@ export class MmtpRouter {
3462
3499
  packetSequenceNumber: number,
3463
3500
  meta: Pick<
3464
3501
  RoutedPacketMeta,
3465
- "recovered" | "skipFecPayloadId" | "catalogTrackName" | "deliveryProvenance"
3502
+ "mmtpTimestamp" | "rap" | "recovered" | "skipFecPayloadId" | "catalogTrackName" | "deliveryProvenance"
3466
3503
  >,
3467
3504
  ): void {
3468
3505
  if (!Number.isFinite(packetSequenceNumber)) return;
@@ -3474,6 +3511,8 @@ export class MmtpRouter {
3474
3511
  callback(packet, {
3475
3512
  packetId,
3476
3513
  packetSequenceNumber,
3514
+ mmtpTimestamp: meta.mmtpTimestamp,
3515
+ rap: meta.rap,
3477
3516
  recovered: meta.recovered,
3478
3517
  skipFecPayloadId: meta.skipFecPayloadId,
3479
3518
  ...(meta.catalogTrackName === undefined
@@ -4061,7 +4100,10 @@ export class MmtpRouter {
4061
4100
  ) ?? 'unknown';
4062
4101
 
4063
4102
  for (const sym of result.symbols) {
4103
+ // The block processor read these from the recovered packet's own header.
4064
4104
  this.#routeTrackPacket(sym.rawSymbol, ownerPacketId, sym.packetSequenceNumber, {
4105
+ mmtpTimestamp: sym.fragment.timestamp,
4106
+ rap: sym.fragment.rapFlag,
4065
4107
  recovered: true,
4066
4108
  skipFecPayloadId: true,
4067
4109
  catalogTrackName,