@blockcast/mmt-container 2.2.0-main.c4fa878f4ff4 → 2.2.0-main.caa271d80062

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 (111) hide show
  1. package/README.md +34 -19
  2. package/dist/block-policy.d.ts +88 -16
  3. package/dist/block-policy.d.ts.map +1 -1
  4. package/dist/block-policy.js +164 -56
  5. package/dist/block-policy.js.map +3 -3
  6. package/dist/browser-runtime.js +2064 -388
  7. package/dist/browser-runtime.js.map +3 -3
  8. package/dist/carp-catalog.js.map +2 -2
  9. package/dist/catalog-fec-params.d.ts +18 -2
  10. package/dist/catalog-fec-params.d.ts.map +1 -1
  11. package/dist/catalog-fec-params.js +29 -1
  12. package/dist/catalog-fec-params.js.map +2 -2
  13. package/dist/catalog-network-source.js.map +2 -2
  14. package/dist/cmaf-block-assembler.js +92 -1
  15. package/dist/cmaf-block-assembler.js.map +2 -2
  16. package/dist/cmcd-producer.d.ts +60 -47
  17. package/dist/cmcd-producer.d.ts.map +1 -1
  18. package/dist/cmcd-producer.js +138 -109
  19. package/dist/cmcd-producer.js.map +2 -2
  20. package/dist/codec-utils.d.ts.map +1 -1
  21. package/dist/codec-utils.js +92 -1
  22. package/dist/codec-utils.js.map +2 -2
  23. package/dist/delivery-catalog.js +26 -16
  24. package/dist/delivery-catalog.js.map +2 -2
  25. package/dist/delta-fallback-config.js +5 -1
  26. package/dist/delta-fallback-config.js.map +3 -3
  27. package/dist/fec-manager.d.ts +163 -20
  28. package/dist/fec-manager.d.ts.map +1 -1
  29. package/dist/fec-manager.js +863 -110
  30. package/dist/fec-manager.js.map +3 -3
  31. package/dist/fec-state.js +5 -1
  32. package/dist/fec-state.js.map +2 -2
  33. package/dist/fec-worker-setup.d.ts +1 -1
  34. package/dist/fec-worker-setup.js +1 -1
  35. package/dist/fec-worker-setup.js.map +2 -2
  36. package/dist/held-layer-admission.js.map +2 -2
  37. package/dist/index.d.ts +4 -4
  38. package/dist/index.d.ts.map +1 -1
  39. package/dist/index.js +8 -2
  40. package/dist/index.js.map +2 -2
  41. package/dist/iso-block-locator.d.ts +64 -13
  42. package/dist/iso-block-locator.d.ts.map +1 -1
  43. package/dist/iso-block-locator.js +125 -25
  44. package/dist/iso-block-locator.js.map +2 -2
  45. package/dist/iso-bmff-timing.d.ts +10 -1
  46. package/dist/iso-bmff-timing.d.ts.map +1 -1
  47. package/dist/iso-bmff-timing.js +36 -2
  48. package/dist/iso-bmff-timing.js.map +2 -2
  49. package/dist/media-router.js.map +1 -1
  50. package/dist/mfu-reassembler.js.map +2 -2
  51. package/dist/mmtp-container-parser.d.ts +109 -12
  52. package/dist/mmtp-container-parser.d.ts.map +1 -1
  53. package/dist/mmtp-container-parser.js +312 -27
  54. package/dist/mmtp-container-parser.js.map +2 -2
  55. package/dist/mmtp-container-parser.no-modules.js +333 -32
  56. package/dist/mmtp-container-parser.no-modules.js.map +4 -4
  57. package/dist/mmtp-decode-pipeline.js +311 -27
  58. package/dist/mmtp-decode-pipeline.js.map +2 -2
  59. package/dist/mmtp-fec-session.js.map +2 -2
  60. package/dist/mmtp-router.d.ts +51 -12
  61. package/dist/mmtp-router.d.ts.map +1 -1
  62. package/dist/mmtp-router.js +723 -83
  63. package/dist/mmtp-router.js.map +3 -3
  64. package/dist/multicast-auth-catalog.js.map +2 -2
  65. package/dist/multicast-input.js.map +2 -2
  66. package/dist/multicast-manager.d.ts +31 -3
  67. package/dist/multicast-manager.d.ts.map +1 -1
  68. package/dist/multicast-manager.js +1698 -231
  69. package/dist/multicast-manager.js.map +3 -3
  70. package/dist/multicast-transport.js +28 -18
  71. package/dist/multicast-transport.js.map +2 -2
  72. package/dist/multicast-unwrap.js.map +2 -2
  73. package/dist/no-modules.d.ts +1 -0
  74. package/dist/no-modules.d.ts.map +1 -1
  75. package/dist/no-modules.js +333 -31
  76. package/dist/no-modules.js.map +4 -4
  77. package/dist/repair-fetch.d.ts +38 -8
  78. package/dist/repair-fetch.d.ts.map +1 -1
  79. package/dist/repair-fetch.js +286 -27
  80. package/dist/repair-fetch.js.map +3 -3
  81. package/dist/repair-layer-subscriptions.d.ts +6 -2
  82. package/dist/repair-layer-subscriptions.d.ts.map +1 -1
  83. package/dist/repair-layer-subscriptions.js +6 -3
  84. package/dist/repair-layer-subscriptions.js.map +2 -2
  85. package/dist/signaling.js.map +2 -2
  86. package/dist/timestamp-codec.js.map +2 -2
  87. package/dist/track-session.d.ts +1 -1
  88. package/dist/track-session.d.ts.map +1 -1
  89. package/dist/track-session.js +1 -0
  90. package/dist/track-session.js.map +2 -2
  91. package/dist/wasm-mfu-reassembler.js.map +2 -2
  92. package/dist/wasm-mmtp-container-parser.js +311 -27
  93. package/dist/wasm-mmtp-container-parser.js.map +2 -2
  94. package/package.json +8 -5
  95. package/schemas/catalog.schema.json +4 -0
  96. package/src/block-policy.ts +324 -83
  97. package/src/catalog-fec-params.ts +37 -1
  98. package/src/cmcd-producer.ts +189 -179
  99. package/src/codec-utils.ts +121 -1
  100. package/src/fec-manager.ts +818 -42
  101. package/src/fec-worker-setup.ts +2 -2
  102. package/src/index.ts +11 -2
  103. package/src/iso-block-locator.ts +167 -31
  104. package/src/iso-bmff-timing.ts +40 -1
  105. package/src/mmtp-container-parser.ts +355 -32
  106. package/src/mmtp-router.ts +665 -50
  107. package/src/multicast-manager.ts +145 -16
  108. package/src/no-modules.ts +3 -0
  109. package/src/repair-fetch.ts +366 -50
  110. package/src/repair-layer-subscriptions.ts +13 -5
  111. package/src/track-session.ts +4 -1
@@ -173,6 +173,15 @@ export interface FrameMeta {
173
173
  * on FEC-gated streams.
174
174
  */
175
175
  fecSbn?: number
176
+ /**
177
+ * Set on a frame the keyframe overlay rebuilt (draft-ramadan-moq-mmt-02
178
+ * 7.4) whose base SS_IDs are determined: the lowest base SS_ID of its
179
+ * MPU's packets in that decode, its FT=0 when the decode holds it. The
180
+ * release gate waits only for the blocks before it, and stops waiting once
181
+ * a base frame after this one arrived. `fecSbn` stays the SS_ID the frame
182
+ * completed in.
183
+ */
184
+ fecSbnFirst?: number
176
185
  /**
177
186
  * Set on the first frame released after a gap: the blocks between it and
178
187
  * the previous release resolved with lost media, or authored decode time
@@ -197,8 +206,10 @@ export interface SourcePacketIdentity {
197
206
  /**
198
207
  * In-order release policy for FEC-protected streams. For such a stream a
199
208
  * frame after a gap is released only once every FEC block that could hold
200
- * the missing frames has resolved (completed or failed); the frame-count
201
- * reorder window applies only to streams without FEC.
209
+ * the missing frames has resolved (completed or failed), or only a late
210
+ * source or the keyframe overlay could still fill the gap and the frame has
211
+ * waited for them (`unrepairableWaitMs`); the frame-count reorder window
212
+ * applies only to streams without FEC.
202
213
  */
203
214
  export interface FecReleaseGate {
204
215
  /** True when this physical stream's frames are FEC-gated. */
@@ -225,6 +236,24 @@ export interface FecReleaseGate {
225
236
  * It must not exceed how long a resolved block stays readable as resolved.
226
237
  */
227
238
  holdLimitMs(packetId: number, catalogTrackName?: string): number
239
+ /**
240
+ * How long a frame held behind unresolved blocks waits, from its arrival,
241
+ * once no repair but the keyframe overlay can still fill the gap before
242
+ * it: the late-symbol allowance when the gap holds no RAP that overlay
243
+ * could rebuild, else as long as its repair may still come. Undefined
244
+ * while another repair may still fill the gap, or the overlay's repair
245
+ * may come at any time. Behind a run no repair has delimited, until the
246
+ * run's FEC deadline, since a repair may delimit it until then.
247
+ * `fromSbn` is the anchor of the frame released before the gap, `toSbn`
248
+ * this frame's, `after.arrivedAtMs` its arrival (performance.now()). Past
249
+ * the wait the frame goes, decided, as it would once those blocks failed.
250
+ */
251
+ unrepairableWaitMs?(
252
+ before: { data: Uint8Array; meta: FrameMeta },
253
+ after: { data: Uint8Array; meta: FrameMeta; arrivedAtMs?: number },
254
+ fromSbn: number,
255
+ toSbn: number,
256
+ ): number | undefined
228
257
  /** Decide stall/conceal for the first frame released after a gap. */
229
258
  decide(
230
259
  before: { data: Uint8Array; meta: FrameMeta },
@@ -242,13 +271,28 @@ export interface FecReleaseGate {
242
271
  isRap?(data: Uint8Array, meta: FrameMeta): boolean
243
272
  /** A complete RAP entered the gate at SS_ID `ssId` (see `isRap`). */
244
273
  onRap?(packetId: number, catalogTrackName: string | undefined, ssId: number): void
274
+ /**
275
+ * Whether what reads this stream decides on when its frames arrive (a
276
+ * staged stream switch): the gate then releases a held overlay-rebuilt
277
+ * frame when it did unanchored (#cursorPassed).
278
+ */
279
+ releaseUnanchored?(packetId: number, catalogTrackName: string | undefined): boolean
280
+ /**
281
+ * A frame held only for its `unrepairableWaitMs` may go at `atMs`
282
+ * (performance.now()): poke the stream (pokeFecRelease) then. Absent, it
283
+ * goes at the next push or poke after that.
284
+ */
285
+ wakeAt?(packetId: number, catalogTrackName: string | undefined, atMs: number): void
245
286
  }
246
287
 
247
288
  /** Typed ISO-BMFF timing-sample budget counters. See `timingBudgetStats`. */
248
289
  export interface MmtpTimingBudgetStats {
249
290
  /** Slots currently charged to declared-but-undelivered samples. */
250
291
  pending: number
251
- /** Total slots available; `pending === capacity` rejects every new moof. */
292
+ /**
293
+ * Slots each stream's window holds (`maxBufferedMfus`). A stream at
294
+ * capacity displaces its own oldest timing; `pending` sums every stream.
295
+ */
252
296
  capacity: number
253
297
  /** Slots returned by samples the combiner abandoned (evicted or timed out). */
254
298
  reclaimed: number
@@ -259,7 +303,13 @@ export interface MmtpTimingBudgetStats {
259
303
  * up, `evicted` is loss that was never observable at all.
260
304
  */
261
305
  evicted: number
262
- /** Movie fragments refused because the budget had no room for their samples. */
306
+ /**
307
+ * Movie fragments refused because the budget had no room for their samples.
308
+ * Stays 0 with a window per stream (BLO-52085): a stream at capacity
309
+ * displaces its own oldest timing to make room, and a moof declaring more
310
+ * samples than a whole window holds is refused as invalid before any budget
311
+ * is consulted. Kept as a guard, and because it is public.
312
+ */
263
313
  rejectedMovieFragments: number
264
314
  }
265
315
 
@@ -365,10 +415,22 @@ export type MmtpFeedDropReason =
365
415
  /** No timing resolver, or it refused the initialization segment. */
366
416
  | 'init-timing-refused'
367
417
  | 'fragment-unexpected-fragmentation'
418
+ /** FT=1 before an FT=0 has bound the stream's timing track. */
368
419
  | 'fragment-no-resolver'
369
- /** addMovieFragment refused — includes the pending-timing-sample budget. */
420
+ /** addMovieFragment refused: the moof does not parse against the stream's
421
+ * track, conflicts with a pending copy of itself, or repeats a pending
422
+ * copy whose samples are partly consumed. Never the timing budget: a full
423
+ * window displaces the stream's own oldest timing. */
370
424
  | 'fragment-timing-refused'
371
425
  | 'unknown-fragment-type'
426
+ /** Stable code, no longer produced: an FT=2 with no timing (before the
427
+ * stream's first FT=0, or after setTrack dropped it) now waits for its
428
+ * timing like one whose FT=1 has not arrived. A head counts as
429
+ * `drop:mfu-sample-timing-missing` (parked) and a FI=2/FI=3 continuation
430
+ * as the `pending` record. A continuation that completes a head the
431
+ * reassembler buffered before the timing was dropped counts as
432
+ * `drop:assembled-timing-missing`. A panel keyed on this code reads zero:
433
+ * a re-attribution, not lost coverage. Re-key on both drop codes. */
372
434
  | 'mfu-no-resolver'
373
435
  | 'mfu-empty-payload'
374
436
  | 'mfu-du-header-missing'
@@ -499,6 +561,8 @@ export interface ParseHeaderOptions {
499
561
  deliveryProvenance?: MmtpDeliveryProvenance
500
562
  /** FEC source block this packet belongs to (FEC-protected packets only). */
501
563
  fecSbn?: number
564
+ /** See FrameMeta.fecSbnFirst. */
565
+ fecSbnFirst?: number
502
566
  }
503
567
 
504
568
  // ── FrameSequencer ──────────────────────────────────────────────────
@@ -679,7 +743,10 @@ class FrameSequencer {
679
743
  // ── FecReleaseSequencer ─────────────────────────────────────────────
680
744
 
681
745
  /** Authored decode-time adjacency: the next frame starts where this one ends. */
682
- function authoredAdjacent(before: FrameMeta, after: FrameMeta): boolean {
746
+ export function authoredAdjacent(
747
+ before: Pick<FrameMeta, 'authoredTiming'>,
748
+ after: Pick<FrameMeta, 'authoredTiming'>,
749
+ ): boolean {
683
750
  const a = before.authoredTiming
684
751
  const b = after.authoredTiming
685
752
  if (!a || !b || a.mediaTimescale !== b.mediaTimescale) return false
@@ -709,9 +776,19 @@ function ssIdAfter(a: number, b: number): boolean {
709
776
  return ((a - b) | 0) > 0
710
777
  }
711
778
 
779
+ /** The stream holds a frame the keyframe overlay rebuilt (FrameMeta.fecSbnFirst). */
780
+ function holdsRebuilt(stream: GatedStream): boolean {
781
+ return stream.buf.some(({ meta }) => meta.fecSbnFirst !== undefined)
782
+ }
783
+
712
784
  interface GatedFrame extends PendingFrame {
713
785
  /** performance.now() at push; measures the hold against the gate's limit. */
714
786
  arrivedAtMs: number
787
+ /**
788
+ * The unanchored path's gate is done with this frame (see #cursorPassed):
789
+ * it released it, or dropped it as stale.
790
+ */
791
+ passed?: boolean
715
792
  }
716
793
 
717
794
  interface GatedStream {
@@ -723,6 +800,10 @@ interface GatedStream {
723
800
  discontinuity: boolean
724
801
  /** Latest (serial order) SS_ID of a complete RAP that entered the gate since the last reset. */
725
802
  rapSsId: number | undefined
803
+ /** A frame that was not FEC-recovered was released since the last reset. */
804
+ sourced: boolean
805
+ /** Drains of this stream in progress: a release calls out, and back in (a stream switch's onCommit). */
806
+ draining: number
726
807
  }
727
808
 
728
809
  /**
@@ -731,16 +812,24 @@ interface GatedStream {
731
812
  * to the last released frame is released immediately. A frame after a gap
732
813
  * waits until every block between the last released frame's block and its
733
814
  * own has resolved; a D-28 repair or a late in-band recovery can therefore
734
- * still fill the gap. When the resolved blocks lost media, or authored decode
815
+ * still fill the gap. Once only a late source or the keyframe overlay could
816
+ * fill it, the frame waits only the gate's `unrepairableWaitMs` from its
817
+ * arrival: the late-symbol allowance, or longer when the gap may hold a RAP
818
+ * the overlay could still rebuild. A frame the keyframe overlay rebuilt
819
+ * waits only for the blocks before its MPU (FrameMeta.fecSbnFirst), and no
820
+ * longer than the source cursor would hold it unanchored
821
+ * (#cursorPassed). When the resolved blocks lost media, or authored decode
735
822
  * time shows a missing sample, the release carries the gate's stall/conceal
736
823
  * decision as `gapBefore`.
737
824
  *
738
825
  * The wait is bounded here, not by the blocks: a frame held longer than the
739
826
  * gate's `holdLimitMs` since it arrived is released with the gate's
740
827
  * discontinuity decision (a stall), so a block that never resolves cannot
741
- * grow the buffer without limit. There is no timer: the check runs on the
742
- * next push or poke, so after a total outage held frames are released by the
743
- * first packet that arrives, not at the ceiling itself.
828
+ * grow the buffer without limit. That ceiling has no timer: the check runs
829
+ * on the next push or poke, so after a total outage held frames are released
830
+ * by the first packet that arrives, not at the ceiling itself. Only the
831
+ * `unrepairableWaitMs` wait is timed: the gate is asked to poke the stream
832
+ * when it ends (`wakeAt`), since a burst may be the last traffic for a while.
744
833
  */
745
834
  class FecReleaseSequencer {
746
835
  #streams = new Map<string, GatedStream>()
@@ -817,6 +906,7 @@ class FecReleaseSequencer {
817
906
  stream.lastSbn = undefined
818
907
  stream.discontinuity = false
819
908
  stream.rapSsId = undefined
909
+ stream.sourced = false
820
910
  }
821
911
  if (stream.last !== null && compareFrameOrder(frame, stream.last) <= 0) {
822
912
  this.#recordStat('droppedStale')
@@ -845,6 +935,8 @@ class FecReleaseSequencer {
845
935
  if (compareFrameOrder(stream.buf[mid]!, frame) < 0) lo = mid + 1
846
936
  else hi = mid
847
937
  }
938
+ // In front of a frame the unanchored path released, it was stale there.
939
+ if (stream.buf[lo]?.passed === true) frame.passed = true
848
940
  stream.buf.splice(lo, 0, frame)
849
941
  this.#drainUnlessDeferred(stream)
850
942
  }
@@ -883,6 +975,54 @@ class FecReleaseSequencer {
883
975
  return false
884
976
  }
885
977
 
978
+ /**
979
+ * These streams' sink is being replaced by one that has seen no source
980
+ * frame yet. First, from a stream that holds a frame the overlay rebuilt,
981
+ * release to the sink being replaced the frames the unanchored path had
982
+ * already released to it (#cursorPassed): there they waited in its queue,
983
+ * which its replacement flushes. A stream that holds none is not drained:
984
+ * it holds nothing the anchor kept back, and a drain would still force out
985
+ * a frame held past the hold limit, which before the anchor waited for the
986
+ * next push. Then treat the streams as unsourced until a source frame is
987
+ * released.
988
+ */
989
+ unsource(packetId: number, catalogTrackName: string | undefined): void {
990
+ for (const stream of this.#streams.values()) {
991
+ if (stream.packetId === packetId && (catalogTrackName === undefined || stream.catalogTrackName === catalogTrackName)) {
992
+ // From inside this stream's own drain (a sink replaced from a frame
993
+ // callback, as a stream switch's onCommit can), the drain under way
994
+ // releases the rest, in order, once the frame it is delivering is
995
+ // done (a commit delivers its held GOP and RAP after onCommit):
996
+ // released here, they would go ahead of them. Inside a deferral
997
+ // scope (a protected flow's record admits frames of several streams
998
+ // in one), the scope's drain does, for the same reason, into the
999
+ // replacement. With the stream unsourced, that drain lets a rebuilt
1000
+ // frame go as unanchored did until it releases a source frame; a
1001
+ // rebuilt frame behind that one waits for the blocks before it, as an
1002
+ // anchored frame does (the record may hold the base frames it needs).
1003
+ if (stream.draining === 0 && holdsRebuilt(stream)) {
1004
+ if (this.#deferDepth > 0) this.#deferredStreams.add(stream)
1005
+ else this.#drain(stream, true)
1006
+ }
1007
+ stream.sourced = false
1008
+ }
1009
+ }
1010
+ }
1011
+
1012
+ /**
1013
+ * Whether a stream of `packetId` holds a frame the keyframe overlay rebuilt
1014
+ * (FrameMeta.fecSbnFirst). Without `catalogTrackName`, every stream of the
1015
+ * packetId.
1016
+ */
1017
+ holdsRebuilt(packetId: number, catalogTrackName: string | undefined): boolean {
1018
+ for (const stream of this.#streams.values()) {
1019
+ if (stream.packetId !== packetId) continue
1020
+ if (catalogTrackName !== undefined && stream.catalogTrackName !== catalogTrackName) continue
1021
+ if (holdsRebuilt(stream)) return true
1022
+ }
1023
+ return false
1024
+ }
1025
+
886
1026
  /** Re-evaluate held gaps after FEC state changed. */
887
1027
  poke(streamKey?: string): void {
888
1028
  if (streamKey !== undefined) {
@@ -989,16 +1129,34 @@ class FecReleaseSequencer {
989
1129
  lastSbn: undefined,
990
1130
  discontinuity: false,
991
1131
  rapSsId: undefined,
1132
+ sourced: false,
1133
+ draining: 0,
992
1134
  }
993
1135
  this.#streams.set(streamKey, stream)
994
1136
  }
995
1137
  return stream
996
1138
  }
997
1139
 
998
- #drain(stream: GatedStream): void {
1140
+ /** `asUnanchored`: release overlay-rebuilt frames when unanchored did (#cursorPassed). */
1141
+ #drain(stream: GatedStream, asUnanchored = false): void {
1142
+ stream.draining++
1143
+ try {
1144
+ this.#drainLoop(stream, asUnanchored)
1145
+ } finally {
1146
+ stream.draining--
1147
+ }
1148
+ }
1149
+
1150
+ #drainLoop(stream: GatedStream, asUnanchored: boolean): void {
999
1151
  while (stream.buf.length > 0 && stream.last !== null) {
1000
1152
  const head = stream.buf[0]!
1001
1153
  const last = stream.last
1154
+ // A second held copy of the frame just released waits on nothing.
1155
+ if (compareFrameOrder(head, last) <= 0) {
1156
+ stream.buf.shift()
1157
+ this.#release(stream, head, undefined)
1158
+ continue
1159
+ }
1002
1160
  if (stream.discontinuity) {
1003
1161
  stream.discontinuity = false
1004
1162
  stream.buf.shift()
@@ -1012,13 +1170,19 @@ class FecReleaseSequencer {
1012
1170
  }
1013
1171
  const headSbn = head.meta.fecSbn
1014
1172
  const from = stream.lastSbn ?? headSbn
1015
- const to = headSbn ?? stream.lastSbn
1173
+ // An overlay-rebuilt frame waits only for the blocks before its MPU's
1174
+ // first packet in that decode (fecSbnFirst): the overlay delivered the
1175
+ // packets from there on.
1176
+ const to = head.meta.fecSbnFirst ?? headSbn ?? stream.lastSbn
1016
1177
  let lost: readonly number[] = []
1017
1178
  // Neither side carries a Source FEC Payload ID: on a catalog-declared
1018
1179
  // FEC track that is the trailer-stripped profile violation, and there
1019
1180
  // are no blocks to wait on. The gap is still a gap: never release across
1020
1181
  // it undecided (fail closed), let the gate decide with no known blocks.
1021
1182
  const unanchored = from === undefined && to === undefined
1183
+ // The source cursor released it (#cursorPassed) across blocks that have
1184
+ // not resolved: nothing sized that gap either, so it is decided too.
1185
+ let unresolved = false
1022
1186
  if (from !== undefined && to !== undefined) {
1023
1187
  const resolved = this.#gate.resolvedLoss(
1024
1188
  stream.packetId,
@@ -1027,7 +1191,8 @@ class FecReleaseSequencer {
1027
1191
  Math.max(from, to),
1028
1192
  from,
1029
1193
  )
1030
- if (resolved === null) {
1194
+ unresolved = resolved === null
1195
+ if (unresolved && !this.#cursorPassed(stream, asUnanchored) && !this.#unrepairableWaitOver(stream, last, head, from, to)) {
1031
1196
  const heldMs = performance.now() - head.arrivedAtMs
1032
1197
  if (heldMs <= this.#gate.holdLimitMs(stream.packetId, stream.catalogTrackName)) return
1033
1198
  // Past the point where these blocks must have resolved: nothing
@@ -1037,7 +1202,7 @@ class FecReleaseSequencer {
1037
1202
  this.#release(stream, head, this.#gate.decide(last, head, [], true))
1038
1203
  continue
1039
1204
  }
1040
- lost = resolved
1205
+ lost = resolved ?? []
1041
1206
  }
1042
1207
  stream.buf.shift()
1043
1208
  // Every gap in the frame sequence gets a decision, not only one whose
@@ -1048,19 +1213,81 @@ class FecReleaseSequencer {
1048
1213
  this.#release(
1049
1214
  stream,
1050
1215
  head,
1051
- lost.length > 0 || unanchored || authoredGap(last.meta, head.meta)
1216
+ lost.length > 0 || unanchored || unresolved || authoredGap(last.meta, head.meta)
1052
1217
  ? this.#gate.decide(last, head, lost, false)
1053
1218
  : undefined,
1054
1219
  )
1055
1220
  }
1056
1221
  }
1057
1222
 
1223
+ /**
1224
+ * The head has waited out a gap only a late source can still fill
1225
+ * (FecReleaseGate.unrepairableWaitMs). Not yet: the gate is asked to poke
1226
+ * the stream when it has (`wakeAt`), since no packet may come by then.
1227
+ */
1228
+ #unrepairableWaitOver(stream: GatedStream, last: PendingFrame, head: GatedFrame, from: number, to: number): boolean {
1229
+ const waitMs = this.#gate.unrepairableWaitMs?.(last, head, from, to)
1230
+ if (waitMs === undefined) return false
1231
+ const atMs = head.arrivedAtMs + waitMs
1232
+ if (performance.now() >= atMs) return true
1233
+ this.#gate.wakeAt?.(stream.packetId, stream.catalogTrackName, atMs)
1234
+ return false
1235
+ }
1236
+
1237
+ /**
1238
+ * An overlay-rebuilt frame (fecSbnFirst) waits no longer than the BLO-53342
1239
+ * source cursor holds it unanchored, and neither does a later frame behind
1240
+ * it. Unanchored, a frame that waits on no block (fecSbnFirst set, or no
1241
+ * fecSbn) left the gate once every frame ahead of it had: mark it `passed`,
1242
+ * like each frame that arrived in front of it since (stale there). The head
1243
+ * then goes, decided as it would be unanchored, once a frame the base path
1244
+ * delivered (a source frame, or one base FEC recovered) is buffered after a
1245
+ * marked one: the base path has passed that one, so a frame still missing
1246
+ * before it can only come from a repair. It also goes while no source frame
1247
+ * has been released, when the direct path shows a recovered frame at once.
1248
+ * And it goes at once while a staged switch decides on when frames arrive
1249
+ * (FecReleaseGate.releaseUnanchored), and when unsource releases into a
1250
+ * sink being replaced: unanchored, it had already reached the sink.
1251
+ * The scan also writes: the `passed` marks it sets stay on the frames,
1252
+ * and push marks a frame it inserts in front of a marked one from them.
1253
+ */
1254
+ #cursorPassed(stream: GatedStream, asUnanchored: boolean): boolean {
1255
+ let at = -1
1256
+ for (let i = 0; i < stream.buf.length; i++) {
1257
+ const frame = stream.buf[i]!
1258
+ if (frame.meta.fecSbnFirst !== undefined || frame.meta.fecSbn === undefined) {
1259
+ frame.passed = true
1260
+ if (at < 0) at = i
1261
+ } else if (frame.passed !== true) break
1262
+ }
1263
+ if (at < 0) return false
1264
+ return asUnanchored || !stream.sourced ||
1265
+ this.#gate.releaseUnanchored?.(stream.packetId, stream.catalogTrackName) === true ||
1266
+ stream.buf.some(({ meta }, i) =>
1267
+ i > at && meta.fecSbnFirst === undefined && (meta.recovered !== true || meta.fecSbn !== undefined))
1268
+ }
1269
+
1058
1270
  #release(stream: GatedStream, frame: PendingFrame, decision: FrameGapDecision | undefined): void {
1271
+ // Two copies of one frame were held (its base copy and the overlay's
1272
+ // rebuild) and the other went out: this one is stale, as a copy pushed
1273
+ // after its frame was released is. It still moves the state as a release
1274
+ // does: only the second emission is dropped.
1275
+ const stale = stream.last !== null && compareFrameOrder(frame, stream.last) <= 0
1276
+ if (stale) this.#recordStat('droppedStale')
1059
1277
  if (decision) frame.meta.gapBefore = decision
1060
1278
  stream.last = frame
1061
- if (frame.meta.fecSbn !== undefined) stream.lastSbn = frame.meta.fecSbn
1279
+ // An overlay-rebuilt frame (fecSbnFirst) released before any base frame
1280
+ // since the last reset leaves lastSbn unset, as it did unanchored: the
1281
+ // base frame after it then waits for nothing, not for the frames between.
1282
+ if (frame.meta.fecSbn !== undefined && (frame.meta.fecSbnFirst === undefined || stream.lastSbn !== undefined)) {
1283
+ stream.lastSbn = frame.meta.fecSbn
1284
+ }
1285
+ if (frame.meta.recovered !== true) stream.sourced = true
1286
+ // A stale copy of the frame just released (equal order) repeats an
1287
+ // onReleased call already made: noteReleasedRap ignores a second RAP of
1288
+ // an MPU it has anchored (the same MPU and presentation time).
1062
1289
  this.#gate.onReleased?.(frame.data, frame.meta)
1063
- this.#emit(frame.data, frame.meta)
1290
+ if (!stale) this.#emit(frame.data, frame.meta)
1064
1291
  }
1065
1292
  }
1066
1293
 
@@ -1089,9 +1316,30 @@ interface AwaitingTimingHead {
1089
1316
  mpuSequenceNumber: number
1090
1317
  movieFragmentSequenceNumber: number
1091
1318
  sampleNumber: number
1319
+ /** MMTP packet sequence number; a recovered copy carries its source's. */
1320
+ sequenceNumber: number
1092
1321
  arrivedAt: number
1093
1322
  }
1094
1323
 
1324
+ /** FEC-recovered heads that went through the awaiting-timing queue. Every
1325
+ * count but `pending` is cumulative. A held head is pending or has left the
1326
+ * queue exactly once, so `held - expired - discarded - superseded - pending`
1327
+ * is the number fed again once their FT=1 landed. That feed can still refuse
1328
+ * one (size, offset, dispose from a frame callback); the refusal is counted in
1329
+ * `feedStats.deferredOutcomes`, not here. */
1330
+ export interface MmtpRecoveredHeadTimingStats {
1331
+ /** Held because their sample's FT=1 had not arrived yet. */
1332
+ held: number
1333
+ /** Left without timing: the reassembler-timeout deadline, or pushed out by the queue's capacity. */
1334
+ expired: number
1335
+ /** Dropped with their stream (track reconfigure, reset or retire) or by dispose(). */
1336
+ discarded: number
1337
+ /** Gave way to their arriving source copy, which takes their place in the queue. */
1338
+ superseded: number
1339
+ /** In the queue now; current occupancy, not cumulative. */
1340
+ pending: number
1341
+ }
1342
+
1095
1343
  const MAX_AWAITING_TIMING_HEADS = 8
1096
1344
 
1097
1345
  // ── MmtpContainerParser ─────────────────────────────────────────────
@@ -1129,6 +1377,9 @@ export class MmtpContainerParser {
1129
1377
  #reclaimedTimingSamples = 0
1130
1378
  #evictedTimingSamples = 0
1131
1379
  #rejectedMovieFragments = 0
1380
+ #recoveredHeadsAwaitingTiming: Omit<MmtpRecoveredHeadTimingStats, 'pending'> = {
1381
+ held: 0, expired: 0, discarded: 0, superseded: 0,
1382
+ }
1132
1383
  #sequencers = new Map<string, { packetId: number; sequencer: FrameSequencer }>()
1133
1384
  #fecGate: FecReleaseGate | null = null
1134
1385
  #fecSequencer: FecReleaseSequencer | null = null
@@ -1330,11 +1581,12 @@ export class MmtpContainerParser {
1330
1581
  /**
1331
1582
  * Typed view of the ISO-BMFF timing-sample budget.
1332
1583
  *
1333
- * This is the acceptance signal for the budget ratchet: `pending` walking
1334
- * to `capacity` while `rejectedMovieFragments` climbs is the stream wedging,
1335
- * and it is observable here rather than only as media silently stopping.
1336
- * Per repo policy this is a producer-owned counter -- never infer budget
1337
- * state by parsing logs.
1584
+ * Each stream admits timing against its own window of `capacity` slots
1585
+ * (BLO-52085). A stream at capacity displaces its own oldest timing to
1586
+ * admit the moof in hand, and `evicted` counts the slots so displaced:
1587
+ * timing a stream declared and never delivered shows up there. `pending`
1588
+ * sums every stream, so it can exceed `capacity`. Per repo policy this is
1589
+ * a producer-owned counter -- never infer budget state by parsing logs.
1338
1590
  */
1339
1591
  get timingBudgetStats(): Readonly<MmtpTimingBudgetStats> {
1340
1592
  return {
@@ -1346,6 +1598,15 @@ export class MmtpContainerParser {
1346
1598
  }
1347
1599
  }
1348
1600
 
1601
+ /** Recovered heads parked for FT=1 timing (BLO-53369). Survives dispose(). */
1602
+ get recoveredHeadsAwaitingTiming(): Readonly<MmtpRecoveredHeadTimingStats> {
1603
+ let pending = 0
1604
+ for (const { awaitingTiming } of this.#timingStreams.values()) {
1605
+ for (const head of awaitingTiming) if (head.options?.recovered) pending++
1606
+ }
1607
+ return { ...this.#recoveredHeadsAwaitingTiming, pending }
1608
+ }
1609
+
1349
1610
  get reorderStats(): Readonly<MmtpReorderStats> {
1350
1611
  let bufferedFrames = this.#fecSequencer?.buffered ?? 0
1351
1612
  for (const { sequencer } of this.#sequencers.values()) bufferedFrames += sequencer.buffered
@@ -1394,6 +1655,16 @@ export class MmtpContainerParser {
1394
1655
  return this.#fecSequencer?.rapAbove(packetId, catalogTrackName, ssId) ?? false
1395
1656
  }
1396
1657
 
1658
+ /** See FecReleaseSequencer.unsource. */
1659
+ unsourceFecRelease(packetId: number, catalogTrackName?: string): void {
1660
+ this.#fecSequencer?.unsource(packetId, catalogTrackName)
1661
+ }
1662
+
1663
+ /** See FecReleaseSequencer.holdsRebuilt. */
1664
+ fecReleaseHoldsRebuilt(packetId: number, catalogTrackName?: string): boolean {
1665
+ return this.#fecSequencer?.holdsRebuilt(packetId, catalogTrackName) ?? false
1666
+ }
1667
+
1397
1668
  /** Re-evaluate held gaps after FEC block state changed. */
1398
1669
  pokeFecRelease(packetId?: number, catalogTrackName?: string): void {
1399
1670
  if (!this.#fecSequencer) return
@@ -1541,7 +1812,7 @@ export class MmtpContainerParser {
1541
1812
  const now = Date.now()
1542
1813
  for (const { awaitingTiming } of this.#timingStreams.values()) {
1543
1814
  while (awaitingTiming.length > 0 && now - awaitingTiming[0]!.arrivedAt > timeoutMs) {
1544
- awaitingTiming.shift()
1815
+ this.#leaveAwaitingTiming(awaitingTiming.shift()!, 'expired')
1545
1816
  }
1546
1817
  }
1547
1818
  }
@@ -1711,17 +1982,27 @@ export class MmtpContainerParser {
1711
1982
  if (mpu.fragmentationIndicator !== 0 || mpu.fragmentCounter !== 0)
1712
1983
  return this.#drop('fragment-unexpected-fragmentation')
1713
1984
  const resolver = this.#timingResolver(streamKey, info.packetId, false)
1714
- if (!resolver) return this.#drop('fragment-no-resolver')
1985
+ if (!resolver?.initialized) return this.#drop('fragment-no-resolver')
1715
1986
  const pendingBefore = resolver.pendingSampleCount
1716
1987
  const evictedBefore = resolver.evictedSampleCount
1717
- const remaining = this.#maxPendingTimingSamples - this.#pendingTimingSamples
1718
- const admitted = resolver.addMovieFragment(mpu.mpuSequenceNumber, data, remaining)
1988
+ // Each stream's window is the whole budget. A resolver can displace only
1989
+ // its own timing, so a pool shared across streams let one stream's
1990
+ // unconsumed slots refuse another stream's moofs with nothing to
1991
+ // displace: BLO-52085 G7b, overlay-rebuilt video keyframes refusing audio.
1992
+ const remaining = this.#maxPendingTimingSamples - resolver.pendingSampleCount
1993
+ const admitted = resolver.addMovieFragment(
1994
+ mpu.mpuSequenceNumber,
1995
+ data,
1996
+ remaining,
1997
+ options?.recovered === true,
1998
+ )
1719
1999
  // Sync on BOTH paths: the resolver can displace stale timing and still
1720
- // refuse the fragment in hand, and a global count that missed those
1721
- // freed slots would keep refusing on a budget that is no longer full.
2000
+ // refuse the fragment in hand, and the total must count those freed slots.
1722
2001
  this.#pendingTimingSamples += resolver.pendingSampleCount - pendingBefore
1723
2002
  this.#evictedTimingSamples += resolver.evictedSampleCount - evictedBefore
1724
2003
  if (!admitted) {
2004
+ // Never 'budget' with a window per stream (see
2005
+ // `rejectedMovieFragments`); kept as a guard.
1725
2006
  if (resolver.lastMovieFragmentRejection === 'budget') {
1726
2007
  this.#rejectedMovieFragments++
1727
2008
  }
@@ -1739,7 +2020,11 @@ export class MmtpContainerParser {
1739
2020
  }
1740
2021
  if (fragmentType !== 2) return this.#drop('unknown-fragment-type')
1741
2022
 
1742
- const resolver = this.#timingResolver(streamKey, info.packetId, false)
2023
+ // An FT=2 before the stream's first FT=0 (a join, a track reset) is taken
2024
+ // like one whose FT=1 has not arrived: a head waits for its timing, a
2025
+ // continuation parks in the reassembler, and a later or FEC-recovered
2026
+ // FT=0/FT=1 can still time them.
2027
+ const resolver = this.#timingResolver(streamKey, info.packetId, true)
1743
2028
  if (!resolver) return this.#drop('mfu-no-resolver')
1744
2029
  if (!Number.isSafeInteger(data.byteLength) || data.byteLength <= 0)
1745
2030
  return this.#drop('mfu-empty-payload')
@@ -1761,6 +2046,7 @@ export class MmtpContainerParser {
1761
2046
  mpuSequenceNumber: mpu.mpuSequenceNumber,
1762
2047
  movieFragmentSequenceNumber: mfuDuHeader.movieFragmentSequenceNumber,
1763
2048
  sampleNumber: mfuDuHeader.sampleNumber,
2049
+ sequenceNumber: info.sequenceNumber,
1764
2050
  arrivedAt: Date.now(),
1765
2051
  })
1766
2052
  return this.#drop('mfu-sample-timing-missing')
@@ -1803,6 +2089,7 @@ export class MmtpContainerParser {
1803
2089
  resolver,
1804
2090
  options?.recovered === true,
1805
2091
  options?.fecSbn,
2092
+ options?.fecSbnFirst,
1806
2093
  )
1807
2094
  if (deferred.record === undefined) this.#countDeferredOutcome(deferred.reason)
1808
2095
  }
@@ -1914,6 +2201,7 @@ export class MmtpContainerParser {
1914
2201
  resolver,
1915
2202
  options?.recovered === true,
1916
2203
  options?.fecSbn,
2204
+ options?.fecSbnFirst,
1917
2205
  )
1918
2206
  if (outcome.record !== undefined) return outcome.record
1919
2207
  return this.#drop(outcome.reason)
@@ -1940,6 +2228,7 @@ export class MmtpContainerParser {
1940
2228
  resolver: IsoBmffTimingResolver,
1941
2229
  recovered: boolean,
1942
2230
  fecSbn: number | undefined,
2231
+ fecSbnFirst: number | undefined,
1943
2232
  ): CombinedSampleOutcome {
1944
2233
  const timing = resolver.sampleTiming(
1945
2234
  combined.mpuSequenceNumber,
@@ -1961,6 +2250,7 @@ export class MmtpContainerParser {
1961
2250
  )
1962
2251
  if (!meta) return { reason: 'combined-meta-build-failed' }
1963
2252
  if (fecSbn !== undefined) meta.fecSbn = fecSbn
2253
+ if (fecSbnFirst !== undefined) meta.fecSbnFirst = fecSbnFirst
1964
2254
  if (!resolver.consumeSample(
1965
2255
  combined.mpuSequenceNumber,
1966
2256
  combined.movieFragmentSequenceNumber,
@@ -2146,6 +2436,18 @@ export class MmtpContainerParser {
2146
2436
  this.#emitFrame(data, meta)
2147
2437
  }
2148
2438
 
2439
+ /** Whether FT=1 metadata already names this sample, without claiming it. */
2440
+ hasAuthoredTimingFor(
2441
+ packetId: number,
2442
+ mpuSequenceNumber: number,
2443
+ movieFragmentSequenceNumber: number,
2444
+ sampleNumber: number,
2445
+ catalogTrackName?: string,
2446
+ ): boolean {
2447
+ return this.#timingResolver(this.#streamKey(packetId, catalogTrackName), packetId, false)
2448
+ ?.sampleTiming(mpuSequenceNumber, movieFragmentSequenceNumber, sampleNumber) != null
2449
+ }
2450
+
2149
2451
  /**
2150
2452
  * Claim and return the authored timing for a recovered sample. Recovery does
2151
2453
  * not pass through the ordinary `feed()` completion path, so it must retire
@@ -2216,6 +2518,9 @@ export class MmtpContainerParser {
2216
2518
  for (const { sequencer } of this.#sequencers.values()) sequencer.discard()
2217
2519
  this.#sequencers.clear()
2218
2520
  this.#fecSequencer?.discard()
2521
+ for (const { awaitingTiming } of this.#timingStreams.values()) {
2522
+ for (const head of awaitingTiming) this.#leaveAwaitingTiming(head, 'discarded')
2523
+ }
2219
2524
  this.#timingStreams.clear()
2220
2525
  this.#pendingTimingSamples = 0
2221
2526
  this.#reliableMoqStartupGate.clear()
@@ -2241,10 +2546,27 @@ export class MmtpContainerParser {
2241
2546
  }
2242
2547
 
2243
2548
  #awaitTiming(streamKey: string, head: AwaitingTimingHead): void {
2244
- const awaitingTiming = this.#timingStreams.get(streamKey)?.awaitingTiming
2245
- if (!awaitingTiming) return
2549
+ const state = this.#timingStreams.get(streamKey)
2550
+ if (!state) return
2551
+ // The source copy of a held recovered head takes its place, so the frame
2552
+ // is delivered with source provenance once FT=1 lands.
2553
+ if (!head.options?.recovered) {
2554
+ const kept = state.awaitingTiming.filter(queued =>
2555
+ !queued.options?.recovered || queued.sequenceNumber !== head.sequenceNumber)
2556
+ this.#recoveredHeadsAwaitingTiming.superseded += state.awaitingTiming.length - kept.length
2557
+ state.awaitingTiming = kept
2558
+ }
2559
+ const awaitingTiming = state.awaitingTiming
2246
2560
  awaitingTiming.push(head)
2247
- if (awaitingTiming.length > MAX_AWAITING_TIMING_HEADS) awaitingTiming.shift()
2561
+ if (head.options?.recovered) this.#recoveredHeadsAwaitingTiming.held++
2562
+ if (awaitingTiming.length > MAX_AWAITING_TIMING_HEADS) {
2563
+ this.#leaveAwaitingTiming(awaitingTiming.shift()!, 'expired')
2564
+ }
2565
+ }
2566
+
2567
+ /** Count a recovered head leaving the queue other than by replay or its source copy. */
2568
+ #leaveAwaitingTiming(head: AwaitingTimingHead, outcome: 'expired' | 'discarded'): void {
2569
+ if (head.options?.recovered) this.#recoveredHeadsAwaitingTiming[outcome]++
2248
2570
  }
2249
2571
 
2250
2572
  /**
@@ -2274,6 +2596,7 @@ export class MmtpContainerParser {
2274
2596
  const state = this.#timingStreams.get(streamKey)
2275
2597
  if (!state) return
2276
2598
  this.#timingStreams.delete(streamKey)
2599
+ for (const head of state.awaitingTiming) this.#leaveAwaitingTiming(head, 'discarded')
2277
2600
  this.#pendingTimingSamples -= state.resolver.pendingSampleCount
2278
2601
  if (this.#pendingTimingSamples < 0) {
2279
2602
  throw new Error('[MmtpContainerParser] pending timing sample accounting underflow')