@blockcast/mmt-container 2.2.0-main.a88c3a0aa4f3 → 2.2.0-main.aee8d33d2cc8

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