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

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 (73) hide show
  1. package/README.md +29 -17
  2. package/dist/browser-runtime.js +934 -213
  3. package/dist/browser-runtime.js.map +3 -3
  4. package/dist/catalog-fec-params.d.ts +17 -1
  5. package/dist/catalog-fec-params.d.ts.map +1 -1
  6. package/dist/catalog-fec-params.js +15 -0
  7. package/dist/catalog-fec-params.js.map +2 -2
  8. package/dist/cmcd-producer.d.ts +60 -47
  9. package/dist/cmcd-producer.d.ts.map +1 -1
  10. package/dist/cmcd-producer.js +138 -109
  11. package/dist/cmcd-producer.js.map +2 -2
  12. package/dist/delivery-catalog.js +26 -16
  13. package/dist/delivery-catalog.js.map +2 -2
  14. package/dist/delta-fallback-config.js.map +1 -1
  15. package/dist/fec-manager.d.ts +33 -1
  16. package/dist/fec-manager.d.ts.map +1 -1
  17. package/dist/fec-manager.js +222 -6
  18. package/dist/fec-manager.js.map +2 -2
  19. package/dist/fec-worker-setup.d.ts +1 -1
  20. package/dist/fec-worker-setup.js +1 -1
  21. package/dist/fec-worker-setup.js.map +1 -1
  22. package/dist/index.d.ts +3 -3
  23. package/dist/index.d.ts.map +1 -1
  24. package/dist/index.js +5 -1
  25. package/dist/index.js.map +2 -2
  26. package/dist/iso-block-locator.d.ts +9 -0
  27. package/dist/iso-block-locator.d.ts.map +1 -1
  28. package/dist/iso-block-locator.js +17 -0
  29. package/dist/iso-block-locator.js.map +2 -2
  30. package/dist/iso-bmff-timing.d.ts +10 -1
  31. package/dist/iso-bmff-timing.d.ts.map +1 -1
  32. package/dist/iso-bmff-timing.js +36 -2
  33. package/dist/iso-bmff-timing.js.map +2 -2
  34. package/dist/media-router.js.map +1 -1
  35. package/dist/mmtp-container-parser.d.ts +79 -10
  36. package/dist/mmtp-container-parser.d.ts.map +1 -1
  37. package/dist/mmtp-container-parser.js +204 -26
  38. package/dist/mmtp-container-parser.js.map +2 -2
  39. package/dist/mmtp-container-parser.no-modules.js +206 -29
  40. package/dist/mmtp-container-parser.no-modules.js.map +2 -2
  41. package/dist/mmtp-decode-pipeline.js +203 -26
  42. package/dist/mmtp-decode-pipeline.js.map +2 -2
  43. package/dist/mmtp-router.d.ts +27 -3
  44. package/dist/mmtp-router.d.ts.map +1 -1
  45. package/dist/mmtp-router.js +533 -75
  46. package/dist/mmtp-router.js.map +2 -2
  47. package/dist/multicast-manager.js +757 -83
  48. package/dist/multicast-manager.js.map +2 -2
  49. package/dist/multicast-transport.js +28 -18
  50. package/dist/multicast-transport.js.map +2 -2
  51. package/dist/no-modules.js +203 -26
  52. package/dist/no-modules.js.map +2 -2
  53. package/dist/repair-layer-subscriptions.d.ts +6 -2
  54. package/dist/repair-layer-subscriptions.d.ts.map +1 -1
  55. package/dist/repair-layer-subscriptions.js +6 -3
  56. package/dist/repair-layer-subscriptions.js.map +2 -2
  57. package/dist/track-session.d.ts +1 -1
  58. package/dist/track-session.js.map +1 -1
  59. package/dist/wasm-mmtp-container-parser.js +203 -26
  60. package/dist/wasm-mmtp-container-parser.js.map +2 -2
  61. package/package.json +8 -5
  62. package/schemas/catalog.schema.json +4 -0
  63. package/src/catalog-fec-params.ts +36 -1
  64. package/src/cmcd-producer.ts +189 -179
  65. package/src/fec-manager.ts +277 -6
  66. package/src/fec-worker-setup.ts +2 -2
  67. package/src/index.ts +7 -2
  68. package/src/iso-block-locator.ts +19 -0
  69. package/src/iso-bmff-timing.ts +40 -1
  70. package/src/mmtp-container-parser.ts +298 -29
  71. package/src/mmtp-router.ts +510 -37
  72. package/src/repair-layer-subscriptions.ts +13 -5
  73. package/src/track-session.ts +1 -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
@@ -242,13 +251,22 @@ export interface FecReleaseGate {
242
251
  isRap?(data: Uint8Array, meta: FrameMeta): boolean
243
252
  /** A complete RAP entered the gate at SS_ID `ssId` (see `isRap`). */
244
253
  onRap?(packetId: number, catalogTrackName: string | undefined, ssId: number): void
254
+ /**
255
+ * Whether what reads this stream decides on when its frames arrive (a
256
+ * staged stream switch): the gate then releases a held overlay-rebuilt
257
+ * frame when it did unanchored (#cursorPassed).
258
+ */
259
+ releaseUnanchored?(packetId: number, catalogTrackName: string | undefined): boolean
245
260
  }
246
261
 
247
262
  /** Typed ISO-BMFF timing-sample budget counters. See `timingBudgetStats`. */
248
263
  export interface MmtpTimingBudgetStats {
249
264
  /** Slots currently charged to declared-but-undelivered samples. */
250
265
  pending: number
251
- /** Total slots available; `pending === capacity` rejects every new moof. */
266
+ /**
267
+ * Slots each stream's window holds (`maxBufferedMfus`). A stream at
268
+ * capacity displaces its own oldest timing; `pending` sums every stream.
269
+ */
252
270
  capacity: number
253
271
  /** Slots returned by samples the combiner abandoned (evicted or timed out). */
254
272
  reclaimed: number
@@ -259,7 +277,13 @@ export interface MmtpTimingBudgetStats {
259
277
  * up, `evicted` is loss that was never observable at all.
260
278
  */
261
279
  evicted: number
262
- /** Movie fragments refused because the budget had no room for their samples. */
280
+ /**
281
+ * Movie fragments refused because the budget had no room for their samples.
282
+ * Stays 0 with a window per stream (BLO-52085): a stream at capacity
283
+ * displaces its own oldest timing to make room, and a moof declaring more
284
+ * samples than a whole window holds is refused as invalid before any budget
285
+ * is consulted. Kept as a guard, and because it is public.
286
+ */
263
287
  rejectedMovieFragments: number
264
288
  }
265
289
 
@@ -365,10 +389,22 @@ export type MmtpFeedDropReason =
365
389
  /** No timing resolver, or it refused the initialization segment. */
366
390
  | 'init-timing-refused'
367
391
  | 'fragment-unexpected-fragmentation'
392
+ /** FT=1 before an FT=0 has bound the stream's timing track. */
368
393
  | 'fragment-no-resolver'
369
- /** addMovieFragment refused — includes the pending-timing-sample budget. */
394
+ /** addMovieFragment refused: the moof does not parse against the stream's
395
+ * track, conflicts with a pending copy of itself, or repeats a pending
396
+ * copy whose samples are partly consumed. Never the timing budget: a full
397
+ * window displaces the stream's own oldest timing. */
370
398
  | 'fragment-timing-refused'
371
399
  | 'unknown-fragment-type'
400
+ /** Stable code, no longer produced: an FT=2 with no timing (before the
401
+ * stream's first FT=0, or after setTrack dropped it) now waits for its
402
+ * timing like one whose FT=1 has not arrived. A head counts as
403
+ * `drop:mfu-sample-timing-missing` (parked) and a FI=2/FI=3 continuation
404
+ * as the `pending` record. A continuation that completes a head the
405
+ * reassembler buffered before the timing was dropped counts as
406
+ * `drop:assembled-timing-missing`. A panel keyed on this code reads zero:
407
+ * a re-attribution, not lost coverage. Re-key on both drop codes. */
372
408
  | 'mfu-no-resolver'
373
409
  | 'mfu-empty-payload'
374
410
  | 'mfu-du-header-missing'
@@ -499,6 +535,8 @@ export interface ParseHeaderOptions {
499
535
  deliveryProvenance?: MmtpDeliveryProvenance
500
536
  /** FEC source block this packet belongs to (FEC-protected packets only). */
501
537
  fecSbn?: number
538
+ /** See FrameMeta.fecSbnFirst. */
539
+ fecSbnFirst?: number
502
540
  }
503
541
 
504
542
  // ── FrameSequencer ──────────────────────────────────────────────────
@@ -679,7 +717,10 @@ class FrameSequencer {
679
717
  // ── FecReleaseSequencer ─────────────────────────────────────────────
680
718
 
681
719
  /** Authored decode-time adjacency: the next frame starts where this one ends. */
682
- function authoredAdjacent(before: FrameMeta, after: FrameMeta): boolean {
720
+ export function authoredAdjacent(
721
+ before: Pick<FrameMeta, 'authoredTiming'>,
722
+ after: Pick<FrameMeta, 'authoredTiming'>,
723
+ ): boolean {
683
724
  const a = before.authoredTiming
684
725
  const b = after.authoredTiming
685
726
  if (!a || !b || a.mediaTimescale !== b.mediaTimescale) return false
@@ -709,9 +750,19 @@ function ssIdAfter(a: number, b: number): boolean {
709
750
  return ((a - b) | 0) > 0
710
751
  }
711
752
 
753
+ /** The stream holds a frame the keyframe overlay rebuilt (FrameMeta.fecSbnFirst). */
754
+ function holdsRebuilt(stream: GatedStream): boolean {
755
+ return stream.buf.some(({ meta }) => meta.fecSbnFirst !== undefined)
756
+ }
757
+
712
758
  interface GatedFrame extends PendingFrame {
713
759
  /** performance.now() at push; measures the hold against the gate's limit. */
714
760
  arrivedAtMs: number
761
+ /**
762
+ * The unanchored path's gate is done with this frame (see #cursorPassed):
763
+ * it released it, or dropped it as stale.
764
+ */
765
+ passed?: boolean
715
766
  }
716
767
 
717
768
  interface GatedStream {
@@ -723,6 +774,10 @@ interface GatedStream {
723
774
  discontinuity: boolean
724
775
  /** Latest (serial order) SS_ID of a complete RAP that entered the gate since the last reset. */
725
776
  rapSsId: number | undefined
777
+ /** A frame that was not FEC-recovered was released since the last reset. */
778
+ sourced: boolean
779
+ /** Drains of this stream in progress: a release calls out, and back in (a stream switch's onCommit). */
780
+ draining: number
726
781
  }
727
782
 
728
783
  /**
@@ -731,9 +786,11 @@ interface GatedStream {
731
786
  * to the last released frame is released immediately. A frame after a gap
732
787
  * waits until every block between the last released frame's block and its
733
788
  * 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
735
- * time shows a missing sample, the release carries the gate's stall/conceal
736
- * decision as `gapBefore`.
789
+ * still fill the gap. A frame the keyframe overlay rebuilt waits only for the
790
+ * blocks before its MPU (FrameMeta.fecSbnFirst), and no longer than the
791
+ * source cursor would hold it unanchored (#cursorPassed). When the resolved
792
+ * blocks lost media, or authored decode time shows a missing sample, the
793
+ * release carries the gate's stall/conceal decision as `gapBefore`.
737
794
  *
738
795
  * The wait is bounded here, not by the blocks: a frame held longer than the
739
796
  * gate's `holdLimitMs` since it arrived is released with the gate's
@@ -817,6 +874,7 @@ class FecReleaseSequencer {
817
874
  stream.lastSbn = undefined
818
875
  stream.discontinuity = false
819
876
  stream.rapSsId = undefined
877
+ stream.sourced = false
820
878
  }
821
879
  if (stream.last !== null && compareFrameOrder(frame, stream.last) <= 0) {
822
880
  this.#recordStat('droppedStale')
@@ -845,6 +903,8 @@ class FecReleaseSequencer {
845
903
  if (compareFrameOrder(stream.buf[mid]!, frame) < 0) lo = mid + 1
846
904
  else hi = mid
847
905
  }
906
+ // In front of a frame the unanchored path released, it was stale there.
907
+ if (stream.buf[lo]?.passed === true) frame.passed = true
848
908
  stream.buf.splice(lo, 0, frame)
849
909
  this.#drainUnlessDeferred(stream)
850
910
  }
@@ -883,6 +943,46 @@ class FecReleaseSequencer {
883
943
  return false
884
944
  }
885
945
 
946
+ /**
947
+ * These streams' sink is being replaced by one that has seen no source
948
+ * frame yet. First, from a stream that holds a frame the overlay rebuilt,
949
+ * release to the sink being replaced the frames the unanchored path had
950
+ * already released to it (#cursorPassed): there they waited in its queue,
951
+ * which its replacement flushes. A stream that holds none is not drained:
952
+ * it holds nothing the anchor kept back, and a drain would still force out
953
+ * a frame held past the hold limit, which before the anchor waited for the
954
+ * next push. Then treat the streams as unsourced until a source frame is
955
+ * released.
956
+ */
957
+ unsource(packetId: number, catalogTrackName: string | undefined): void {
958
+ for (const stream of this.#streams.values()) {
959
+ if (stream.packetId === packetId && (catalogTrackName === undefined || stream.catalogTrackName === catalogTrackName)) {
960
+ // From inside this stream's own drain (a sink replaced from a frame
961
+ // callback, as a stream switch's onCommit can), the drain under way
962
+ // releases the rest, in order, once the frame it is delivering is
963
+ // done (a commit delivers its held GOP and RAP after onCommit):
964
+ // released here, they would go ahead of them. With the stream
965
+ // unsourced, that drain lets a rebuilt frame go as unanchored did.
966
+ if (stream.draining === 0 && holdsRebuilt(stream)) this.#drain(stream, true)
967
+ stream.sourced = false
968
+ }
969
+ }
970
+ }
971
+
972
+ /**
973
+ * Whether a stream of `packetId` holds a frame the keyframe overlay rebuilt
974
+ * (FrameMeta.fecSbnFirst). Without `catalogTrackName`, every stream of the
975
+ * packetId.
976
+ */
977
+ holdsRebuilt(packetId: number, catalogTrackName: string | undefined): boolean {
978
+ for (const stream of this.#streams.values()) {
979
+ if (stream.packetId !== packetId) continue
980
+ if (catalogTrackName !== undefined && stream.catalogTrackName !== catalogTrackName) continue
981
+ if (holdsRebuilt(stream)) return true
982
+ }
983
+ return false
984
+ }
985
+
886
986
  /** Re-evaluate held gaps after FEC state changed. */
887
987
  poke(streamKey?: string): void {
888
988
  if (streamKey !== undefined) {
@@ -989,16 +1089,34 @@ class FecReleaseSequencer {
989
1089
  lastSbn: undefined,
990
1090
  discontinuity: false,
991
1091
  rapSsId: undefined,
1092
+ sourced: false,
1093
+ draining: 0,
992
1094
  }
993
1095
  this.#streams.set(streamKey, stream)
994
1096
  }
995
1097
  return stream
996
1098
  }
997
1099
 
998
- #drain(stream: GatedStream): void {
1100
+ /** `asUnanchored`: release overlay-rebuilt frames when unanchored did (#cursorPassed). */
1101
+ #drain(stream: GatedStream, asUnanchored = false): void {
1102
+ stream.draining++
1103
+ try {
1104
+ this.#drainLoop(stream, asUnanchored)
1105
+ } finally {
1106
+ stream.draining--
1107
+ }
1108
+ }
1109
+
1110
+ #drainLoop(stream: GatedStream, asUnanchored: boolean): void {
999
1111
  while (stream.buf.length > 0 && stream.last !== null) {
1000
1112
  const head = stream.buf[0]!
1001
1113
  const last = stream.last
1114
+ // A second held copy of the frame just released waits on nothing.
1115
+ if (compareFrameOrder(head, last) <= 0) {
1116
+ stream.buf.shift()
1117
+ this.#release(stream, head, undefined)
1118
+ continue
1119
+ }
1002
1120
  if (stream.discontinuity) {
1003
1121
  stream.discontinuity = false
1004
1122
  stream.buf.shift()
@@ -1012,13 +1130,19 @@ class FecReleaseSequencer {
1012
1130
  }
1013
1131
  const headSbn = head.meta.fecSbn
1014
1132
  const from = stream.lastSbn ?? headSbn
1015
- const to = headSbn ?? stream.lastSbn
1133
+ // An overlay-rebuilt frame waits only for the blocks before its MPU's
1134
+ // first packet in that decode (fecSbnFirst): the overlay delivered the
1135
+ // packets from there on.
1136
+ const to = head.meta.fecSbnFirst ?? headSbn ?? stream.lastSbn
1016
1137
  let lost: readonly number[] = []
1017
1138
  // Neither side carries a Source FEC Payload ID: on a catalog-declared
1018
1139
  // FEC track that is the trailer-stripped profile violation, and there
1019
1140
  // are no blocks to wait on. The gap is still a gap: never release across
1020
1141
  // it undecided (fail closed), let the gate decide with no known blocks.
1021
1142
  const unanchored = from === undefined && to === undefined
1143
+ // The source cursor released it (#cursorPassed) across blocks that have
1144
+ // not resolved: nothing sized that gap either, so it is decided too.
1145
+ let unresolved = false
1022
1146
  if (from !== undefined && to !== undefined) {
1023
1147
  const resolved = this.#gate.resolvedLoss(
1024
1148
  stream.packetId,
@@ -1027,7 +1151,8 @@ class FecReleaseSequencer {
1027
1151
  Math.max(from, to),
1028
1152
  from,
1029
1153
  )
1030
- if (resolved === null) {
1154
+ unresolved = resolved === null
1155
+ if (unresolved && !this.#cursorPassed(stream, asUnanchored)) {
1031
1156
  const heldMs = performance.now() - head.arrivedAtMs
1032
1157
  if (heldMs <= this.#gate.holdLimitMs(stream.packetId, stream.catalogTrackName)) return
1033
1158
  // Past the point where these blocks must have resolved: nothing
@@ -1037,7 +1162,7 @@ class FecReleaseSequencer {
1037
1162
  this.#release(stream, head, this.#gate.decide(last, head, [], true))
1038
1163
  continue
1039
1164
  }
1040
- lost = resolved
1165
+ lost = resolved ?? []
1041
1166
  }
1042
1167
  stream.buf.shift()
1043
1168
  // Every gap in the frame sequence gets a decision, not only one whose
@@ -1048,19 +1173,67 @@ class FecReleaseSequencer {
1048
1173
  this.#release(
1049
1174
  stream,
1050
1175
  head,
1051
- lost.length > 0 || unanchored || authoredGap(last.meta, head.meta)
1176
+ lost.length > 0 || unanchored || unresolved || authoredGap(last.meta, head.meta)
1052
1177
  ? this.#gate.decide(last, head, lost, false)
1053
1178
  : undefined,
1054
1179
  )
1055
1180
  }
1056
1181
  }
1057
1182
 
1183
+ /**
1184
+ * An overlay-rebuilt frame (fecSbnFirst) waits no longer than the BLO-53342
1185
+ * source cursor holds it unanchored, and neither does a later frame behind
1186
+ * it. Unanchored, a frame that waits on no block (fecSbnFirst set, or no
1187
+ * fecSbn) left the gate once every frame ahead of it had: mark it `passed`,
1188
+ * like each frame that arrived in front of it since (stale there). The head
1189
+ * then goes, decided as it would be unanchored, once a frame the base path
1190
+ * delivered (a source frame, or one base FEC recovered) is buffered after a
1191
+ * marked one: the base path has passed that one, so a frame still missing
1192
+ * before it can only come from a repair. It also goes while no source frame
1193
+ * has been released, when the direct path shows a recovered frame at once.
1194
+ * And it goes at once while a staged switch decides on when frames arrive
1195
+ * (FecReleaseGate.releaseUnanchored), and when unsource releases into a
1196
+ * sink being replaced: unanchored, it had already reached the sink.
1197
+ * The scan also writes: the `passed` marks it sets stay on the frames,
1198
+ * and push marks a frame it inserts in front of a marked one from them.
1199
+ */
1200
+ #cursorPassed(stream: GatedStream, asUnanchored: boolean): boolean {
1201
+ let at = -1
1202
+ for (let i = 0; i < stream.buf.length; i++) {
1203
+ const frame = stream.buf[i]!
1204
+ if (frame.meta.fecSbnFirst !== undefined || frame.meta.fecSbn === undefined) {
1205
+ frame.passed = true
1206
+ if (at < 0) at = i
1207
+ } else if (frame.passed !== true) break
1208
+ }
1209
+ if (at < 0) return false
1210
+ return asUnanchored || !stream.sourced ||
1211
+ this.#gate.releaseUnanchored?.(stream.packetId, stream.catalogTrackName) === true ||
1212
+ stream.buf.some(({ meta }, i) =>
1213
+ i > at && meta.fecSbnFirst === undefined && (meta.recovered !== true || meta.fecSbn !== undefined))
1214
+ }
1215
+
1058
1216
  #release(stream: GatedStream, frame: PendingFrame, decision: FrameGapDecision | undefined): void {
1217
+ // Two copies of one frame were held (its base copy and the overlay's
1218
+ // rebuild) and the other went out: this one is stale, as a copy pushed
1219
+ // after its frame was released is. It still moves the state as a release
1220
+ // does: only the second emission is dropped.
1221
+ const stale = stream.last !== null && compareFrameOrder(frame, stream.last) <= 0
1222
+ if (stale) this.#recordStat('droppedStale')
1059
1223
  if (decision) frame.meta.gapBefore = decision
1060
1224
  stream.last = frame
1061
- if (frame.meta.fecSbn !== undefined) stream.lastSbn = frame.meta.fecSbn
1225
+ // An overlay-rebuilt frame (fecSbnFirst) released before any base frame
1226
+ // since the last reset leaves lastSbn unset, as it did unanchored: the
1227
+ // base frame after it then waits for nothing, not for the frames between.
1228
+ if (frame.meta.fecSbn !== undefined && (frame.meta.fecSbnFirst === undefined || stream.lastSbn !== undefined)) {
1229
+ stream.lastSbn = frame.meta.fecSbn
1230
+ }
1231
+ if (frame.meta.recovered !== true) stream.sourced = true
1232
+ // A stale copy of the frame just released (equal order) repeats an
1233
+ // onReleased call already made: noteReleasedRap ignores a second RAP of
1234
+ // an MPU it has anchored (the same MPU and presentation time).
1062
1235
  this.#gate.onReleased?.(frame.data, frame.meta)
1063
- this.#emit(frame.data, frame.meta)
1236
+ if (!stale) this.#emit(frame.data, frame.meta)
1064
1237
  }
1065
1238
  }
1066
1239
 
@@ -1089,9 +1262,30 @@ interface AwaitingTimingHead {
1089
1262
  mpuSequenceNumber: number
1090
1263
  movieFragmentSequenceNumber: number
1091
1264
  sampleNumber: number
1265
+ /** MMTP packet sequence number; a recovered copy carries its source's. */
1266
+ sequenceNumber: number
1092
1267
  arrivedAt: number
1093
1268
  }
1094
1269
 
1270
+ /** FEC-recovered heads that went through the awaiting-timing queue. Every
1271
+ * count but `pending` is cumulative. A held head is pending or has left the
1272
+ * queue exactly once, so `held - expired - discarded - superseded - pending`
1273
+ * is the number fed again once their FT=1 landed. That feed can still refuse
1274
+ * one (size, offset, dispose from a frame callback); the refusal is counted in
1275
+ * `feedStats.deferredOutcomes`, not here. */
1276
+ export interface MmtpRecoveredHeadTimingStats {
1277
+ /** Held because their sample's FT=1 had not arrived yet. */
1278
+ held: number
1279
+ /** Left without timing: the reassembler-timeout deadline, or pushed out by the queue's capacity. */
1280
+ expired: number
1281
+ /** Dropped with their stream (track reconfigure, reset or retire) or by dispose(). */
1282
+ discarded: number
1283
+ /** Gave way to their arriving source copy, which takes their place in the queue. */
1284
+ superseded: number
1285
+ /** In the queue now; current occupancy, not cumulative. */
1286
+ pending: number
1287
+ }
1288
+
1095
1289
  const MAX_AWAITING_TIMING_HEADS = 8
1096
1290
 
1097
1291
  // ── MmtpContainerParser ─────────────────────────────────────────────
@@ -1129,6 +1323,9 @@ export class MmtpContainerParser {
1129
1323
  #reclaimedTimingSamples = 0
1130
1324
  #evictedTimingSamples = 0
1131
1325
  #rejectedMovieFragments = 0
1326
+ #recoveredHeadsAwaitingTiming: Omit<MmtpRecoveredHeadTimingStats, 'pending'> = {
1327
+ held: 0, expired: 0, discarded: 0, superseded: 0,
1328
+ }
1132
1329
  #sequencers = new Map<string, { packetId: number; sequencer: FrameSequencer }>()
1133
1330
  #fecGate: FecReleaseGate | null = null
1134
1331
  #fecSequencer: FecReleaseSequencer | null = null
@@ -1330,11 +1527,12 @@ export class MmtpContainerParser {
1330
1527
  /**
1331
1528
  * Typed view of the ISO-BMFF timing-sample budget.
1332
1529
  *
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.
1530
+ * Each stream admits timing against its own window of `capacity` slots
1531
+ * (BLO-52085). A stream at capacity displaces its own oldest timing to
1532
+ * admit the moof in hand, and `evicted` counts the slots so displaced:
1533
+ * timing a stream declared and never delivered shows up there. `pending`
1534
+ * sums every stream, so it can exceed `capacity`. Per repo policy this is
1535
+ * a producer-owned counter -- never infer budget state by parsing logs.
1338
1536
  */
1339
1537
  get timingBudgetStats(): Readonly<MmtpTimingBudgetStats> {
1340
1538
  return {
@@ -1346,6 +1544,15 @@ export class MmtpContainerParser {
1346
1544
  }
1347
1545
  }
1348
1546
 
1547
+ /** Recovered heads parked for FT=1 timing (BLO-53369). Survives dispose(). */
1548
+ get recoveredHeadsAwaitingTiming(): Readonly<MmtpRecoveredHeadTimingStats> {
1549
+ let pending = 0
1550
+ for (const { awaitingTiming } of this.#timingStreams.values()) {
1551
+ for (const head of awaitingTiming) if (head.options?.recovered) pending++
1552
+ }
1553
+ return { ...this.#recoveredHeadsAwaitingTiming, pending }
1554
+ }
1555
+
1349
1556
  get reorderStats(): Readonly<MmtpReorderStats> {
1350
1557
  let bufferedFrames = this.#fecSequencer?.buffered ?? 0
1351
1558
  for (const { sequencer } of this.#sequencers.values()) bufferedFrames += sequencer.buffered
@@ -1394,6 +1601,16 @@ export class MmtpContainerParser {
1394
1601
  return this.#fecSequencer?.rapAbove(packetId, catalogTrackName, ssId) ?? false
1395
1602
  }
1396
1603
 
1604
+ /** See FecReleaseSequencer.unsource. */
1605
+ unsourceFecRelease(packetId: number, catalogTrackName?: string): void {
1606
+ this.#fecSequencer?.unsource(packetId, catalogTrackName)
1607
+ }
1608
+
1609
+ /** See FecReleaseSequencer.holdsRebuilt. */
1610
+ fecReleaseHoldsRebuilt(packetId: number, catalogTrackName?: string): boolean {
1611
+ return this.#fecSequencer?.holdsRebuilt(packetId, catalogTrackName) ?? false
1612
+ }
1613
+
1397
1614
  /** Re-evaluate held gaps after FEC block state changed. */
1398
1615
  pokeFecRelease(packetId?: number, catalogTrackName?: string): void {
1399
1616
  if (!this.#fecSequencer) return
@@ -1541,7 +1758,7 @@ export class MmtpContainerParser {
1541
1758
  const now = Date.now()
1542
1759
  for (const { awaitingTiming } of this.#timingStreams.values()) {
1543
1760
  while (awaitingTiming.length > 0 && now - awaitingTiming[0]!.arrivedAt > timeoutMs) {
1544
- awaitingTiming.shift()
1761
+ this.#leaveAwaitingTiming(awaitingTiming.shift()!, 'expired')
1545
1762
  }
1546
1763
  }
1547
1764
  }
@@ -1711,17 +1928,27 @@ export class MmtpContainerParser {
1711
1928
  if (mpu.fragmentationIndicator !== 0 || mpu.fragmentCounter !== 0)
1712
1929
  return this.#drop('fragment-unexpected-fragmentation')
1713
1930
  const resolver = this.#timingResolver(streamKey, info.packetId, false)
1714
- if (!resolver) return this.#drop('fragment-no-resolver')
1931
+ if (!resolver?.initialized) return this.#drop('fragment-no-resolver')
1715
1932
  const pendingBefore = resolver.pendingSampleCount
1716
1933
  const evictedBefore = resolver.evictedSampleCount
1717
- const remaining = this.#maxPendingTimingSamples - this.#pendingTimingSamples
1718
- const admitted = resolver.addMovieFragment(mpu.mpuSequenceNumber, data, remaining)
1934
+ // Each stream's window is the whole budget. A resolver can displace only
1935
+ // its own timing, so a pool shared across streams let one stream's
1936
+ // unconsumed slots refuse another stream's moofs with nothing to
1937
+ // displace: BLO-52085 G7b, overlay-rebuilt video keyframes refusing audio.
1938
+ const remaining = this.#maxPendingTimingSamples - resolver.pendingSampleCount
1939
+ const admitted = resolver.addMovieFragment(
1940
+ mpu.mpuSequenceNumber,
1941
+ data,
1942
+ remaining,
1943
+ options?.recovered === true,
1944
+ )
1719
1945
  // 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.
1946
+ // refuse the fragment in hand, and the total must count those freed slots.
1722
1947
  this.#pendingTimingSamples += resolver.pendingSampleCount - pendingBefore
1723
1948
  this.#evictedTimingSamples += resolver.evictedSampleCount - evictedBefore
1724
1949
  if (!admitted) {
1950
+ // Never 'budget' with a window per stream (see
1951
+ // `rejectedMovieFragments`); kept as a guard.
1725
1952
  if (resolver.lastMovieFragmentRejection === 'budget') {
1726
1953
  this.#rejectedMovieFragments++
1727
1954
  }
@@ -1739,7 +1966,11 @@ export class MmtpContainerParser {
1739
1966
  }
1740
1967
  if (fragmentType !== 2) return this.#drop('unknown-fragment-type')
1741
1968
 
1742
- const resolver = this.#timingResolver(streamKey, info.packetId, false)
1969
+ // An FT=2 before the stream's first FT=0 (a join, a track reset) is taken
1970
+ // like one whose FT=1 has not arrived: a head waits for its timing, a
1971
+ // continuation parks in the reassembler, and a later or FEC-recovered
1972
+ // FT=0/FT=1 can still time them.
1973
+ const resolver = this.#timingResolver(streamKey, info.packetId, true)
1743
1974
  if (!resolver) return this.#drop('mfu-no-resolver')
1744
1975
  if (!Number.isSafeInteger(data.byteLength) || data.byteLength <= 0)
1745
1976
  return this.#drop('mfu-empty-payload')
@@ -1761,6 +1992,7 @@ export class MmtpContainerParser {
1761
1992
  mpuSequenceNumber: mpu.mpuSequenceNumber,
1762
1993
  movieFragmentSequenceNumber: mfuDuHeader.movieFragmentSequenceNumber,
1763
1994
  sampleNumber: mfuDuHeader.sampleNumber,
1995
+ sequenceNumber: info.sequenceNumber,
1764
1996
  arrivedAt: Date.now(),
1765
1997
  })
1766
1998
  return this.#drop('mfu-sample-timing-missing')
@@ -1803,6 +2035,7 @@ export class MmtpContainerParser {
1803
2035
  resolver,
1804
2036
  options?.recovered === true,
1805
2037
  options?.fecSbn,
2038
+ options?.fecSbnFirst,
1806
2039
  )
1807
2040
  if (deferred.record === undefined) this.#countDeferredOutcome(deferred.reason)
1808
2041
  }
@@ -1914,6 +2147,7 @@ export class MmtpContainerParser {
1914
2147
  resolver,
1915
2148
  options?.recovered === true,
1916
2149
  options?.fecSbn,
2150
+ options?.fecSbnFirst,
1917
2151
  )
1918
2152
  if (outcome.record !== undefined) return outcome.record
1919
2153
  return this.#drop(outcome.reason)
@@ -1940,6 +2174,7 @@ export class MmtpContainerParser {
1940
2174
  resolver: IsoBmffTimingResolver,
1941
2175
  recovered: boolean,
1942
2176
  fecSbn: number | undefined,
2177
+ fecSbnFirst: number | undefined,
1943
2178
  ): CombinedSampleOutcome {
1944
2179
  const timing = resolver.sampleTiming(
1945
2180
  combined.mpuSequenceNumber,
@@ -1961,6 +2196,7 @@ export class MmtpContainerParser {
1961
2196
  )
1962
2197
  if (!meta) return { reason: 'combined-meta-build-failed' }
1963
2198
  if (fecSbn !== undefined) meta.fecSbn = fecSbn
2199
+ if (fecSbnFirst !== undefined) meta.fecSbnFirst = fecSbnFirst
1964
2200
  if (!resolver.consumeSample(
1965
2201
  combined.mpuSequenceNumber,
1966
2202
  combined.movieFragmentSequenceNumber,
@@ -2146,6 +2382,18 @@ export class MmtpContainerParser {
2146
2382
  this.#emitFrame(data, meta)
2147
2383
  }
2148
2384
 
2385
+ /** Whether FT=1 metadata already names this sample, without claiming it. */
2386
+ hasAuthoredTimingFor(
2387
+ packetId: number,
2388
+ mpuSequenceNumber: number,
2389
+ movieFragmentSequenceNumber: number,
2390
+ sampleNumber: number,
2391
+ catalogTrackName?: string,
2392
+ ): boolean {
2393
+ return this.#timingResolver(this.#streamKey(packetId, catalogTrackName), packetId, false)
2394
+ ?.sampleTiming(mpuSequenceNumber, movieFragmentSequenceNumber, sampleNumber) != null
2395
+ }
2396
+
2149
2397
  /**
2150
2398
  * Claim and return the authored timing for a recovered sample. Recovery does
2151
2399
  * not pass through the ordinary `feed()` completion path, so it must retire
@@ -2216,6 +2464,9 @@ export class MmtpContainerParser {
2216
2464
  for (const { sequencer } of this.#sequencers.values()) sequencer.discard()
2217
2465
  this.#sequencers.clear()
2218
2466
  this.#fecSequencer?.discard()
2467
+ for (const { awaitingTiming } of this.#timingStreams.values()) {
2468
+ for (const head of awaitingTiming) this.#leaveAwaitingTiming(head, 'discarded')
2469
+ }
2219
2470
  this.#timingStreams.clear()
2220
2471
  this.#pendingTimingSamples = 0
2221
2472
  this.#reliableMoqStartupGate.clear()
@@ -2241,10 +2492,27 @@ export class MmtpContainerParser {
2241
2492
  }
2242
2493
 
2243
2494
  #awaitTiming(streamKey: string, head: AwaitingTimingHead): void {
2244
- const awaitingTiming = this.#timingStreams.get(streamKey)?.awaitingTiming
2245
- if (!awaitingTiming) return
2495
+ const state = this.#timingStreams.get(streamKey)
2496
+ if (!state) return
2497
+ // The source copy of a held recovered head takes its place, so the frame
2498
+ // is delivered with source provenance once FT=1 lands.
2499
+ if (!head.options?.recovered) {
2500
+ const kept = state.awaitingTiming.filter(queued =>
2501
+ !queued.options?.recovered || queued.sequenceNumber !== head.sequenceNumber)
2502
+ this.#recoveredHeadsAwaitingTiming.superseded += state.awaitingTiming.length - kept.length
2503
+ state.awaitingTiming = kept
2504
+ }
2505
+ const awaitingTiming = state.awaitingTiming
2246
2506
  awaitingTiming.push(head)
2247
- if (awaitingTiming.length > MAX_AWAITING_TIMING_HEADS) awaitingTiming.shift()
2507
+ if (head.options?.recovered) this.#recoveredHeadsAwaitingTiming.held++
2508
+ if (awaitingTiming.length > MAX_AWAITING_TIMING_HEADS) {
2509
+ this.#leaveAwaitingTiming(awaitingTiming.shift()!, 'expired')
2510
+ }
2511
+ }
2512
+
2513
+ /** Count a recovered head leaving the queue other than by replay or its source copy. */
2514
+ #leaveAwaitingTiming(head: AwaitingTimingHead, outcome: 'expired' | 'discarded'): void {
2515
+ if (head.options?.recovered) this.#recoveredHeadsAwaitingTiming[outcome]++
2248
2516
  }
2249
2517
 
2250
2518
  /**
@@ -2274,6 +2542,7 @@ export class MmtpContainerParser {
2274
2542
  const state = this.#timingStreams.get(streamKey)
2275
2543
  if (!state) return
2276
2544
  this.#timingStreams.delete(streamKey)
2545
+ for (const head of state.awaitingTiming) this.#leaveAwaitingTiming(head, 'discarded')
2277
2546
  this.#pendingTimingSamples -= state.resolver.pendingSampleCount
2278
2547
  if (this.#pendingTimingSamples < 0) {
2279
2548
  throw new Error('[MmtpContainerParser] pending timing sample accounting underflow')