@blockcast/mmt-container 2.2.0-main.da2e7cd1dd5d → 2.2.0-main.e1bebe787bc9

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 (97) hide show
  1. package/README.md +5 -2
  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 +1573 -214
  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 +1 -1
  10. package/dist/catalog-fec-params.d.ts.map +1 -1
  11. package/dist/catalog-fec-params.js +14 -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/codec-utils.d.ts.map +1 -1
  17. package/dist/codec-utils.js +92 -1
  18. package/dist/codec-utils.js.map +2 -2
  19. package/dist/delivery-catalog.js +2 -1
  20. package/dist/delivery-catalog.js.map +2 -2
  21. package/dist/delta-fallback-config.js +5 -1
  22. package/dist/delta-fallback-config.js.map +3 -3
  23. package/dist/fec-manager.d.ts +111 -14
  24. package/dist/fec-manager.d.ts.map +1 -1
  25. package/dist/fec-manager.js +613 -91
  26. package/dist/fec-manager.js.map +2 -2
  27. package/dist/fec-state.js +5 -1
  28. package/dist/fec-state.js.map +2 -2
  29. package/dist/fec-worker-setup.js.map +2 -2
  30. package/dist/held-layer-admission.js.map +2 -2
  31. package/dist/index.d.ts +3 -3
  32. package/dist/index.d.ts.map +1 -1
  33. package/dist/index.js +3 -1
  34. package/dist/index.js.map +2 -2
  35. package/dist/iso-block-locator.d.ts +48 -13
  36. package/dist/iso-block-locator.d.ts.map +1 -1
  37. package/dist/iso-block-locator.js +97 -25
  38. package/dist/iso-block-locator.js.map +2 -2
  39. package/dist/iso-bmff-timing.d.ts +10 -1
  40. package/dist/iso-bmff-timing.d.ts.map +1 -1
  41. package/dist/iso-bmff-timing.js +36 -2
  42. package/dist/iso-bmff-timing.js.map +2 -2
  43. package/dist/media-router.js.map +1 -1
  44. package/dist/mfu-reassembler.js.map +2 -2
  45. package/dist/mmtp-container-parser.d.ts +104 -12
  46. package/dist/mmtp-container-parser.d.ts.map +1 -1
  47. package/dist/mmtp-container-parser.js +308 -27
  48. package/dist/mmtp-container-parser.js.map +2 -2
  49. package/dist/mmtp-container-parser.no-modules.js +329 -32
  50. package/dist/mmtp-container-parser.no-modules.js.map +4 -4
  51. package/dist/mmtp-decode-pipeline.js +307 -27
  52. package/dist/mmtp-decode-pipeline.js.map +2 -2
  53. package/dist/mmtp-fec-session.js.map +2 -2
  54. package/dist/mmtp-router.d.ts +36 -8
  55. package/dist/mmtp-router.d.ts.map +1 -1
  56. package/dist/mmtp-router.js +676 -76
  57. package/dist/mmtp-router.js.map +3 -3
  58. package/dist/multicast-auth-catalog.js.map +2 -2
  59. package/dist/multicast-input.js.map +2 -2
  60. package/dist/multicast-manager.d.ts +26 -3
  61. package/dist/multicast-manager.d.ts.map +1 -1
  62. package/dist/multicast-manager.js +1384 -187
  63. package/dist/multicast-manager.js.map +3 -3
  64. package/dist/multicast-transport.js +4 -3
  65. package/dist/multicast-transport.js.map +2 -2
  66. package/dist/multicast-unwrap.js.map +2 -2
  67. package/dist/no-modules.d.ts +1 -0
  68. package/dist/no-modules.d.ts.map +1 -1
  69. package/dist/no-modules.js +329 -31
  70. package/dist/no-modules.js.map +4 -4
  71. package/dist/repair-fetch.d.ts +38 -8
  72. package/dist/repair-fetch.d.ts.map +1 -1
  73. package/dist/repair-fetch.js +286 -27
  74. package/dist/repair-fetch.js.map +3 -3
  75. package/dist/repair-layer-subscriptions.js.map +2 -2
  76. package/dist/signaling.js.map +2 -2
  77. package/dist/timestamp-codec.js.map +2 -2
  78. package/dist/track-session.d.ts.map +1 -1
  79. package/dist/track-session.js +1 -0
  80. package/dist/track-session.js.map +2 -2
  81. package/dist/wasm-mfu-reassembler.js.map +2 -2
  82. package/dist/wasm-mmtp-container-parser.js +307 -27
  83. package/dist/wasm-mmtp-container-parser.js.map +2 -2
  84. package/package.json +5 -5
  85. package/src/block-policy.ts +216 -52
  86. package/src/catalog-fec-params.ts +1 -0
  87. package/src/codec-utils.ts +121 -1
  88. package/src/fec-manager.ts +593 -33
  89. package/src/index.ts +5 -0
  90. package/src/iso-block-locator.ts +133 -31
  91. package/src/iso-bmff-timing.ts +40 -1
  92. package/src/mmtp-container-parser.ts +341 -32
  93. package/src/mmtp-router.ts +614 -61
  94. package/src/multicast-manager.ts +136 -16
  95. package/src/no-modules.ts +3 -0
  96. package/src/repair-fetch.ts +366 -50
  97. package/src/track-session.ts +3 -0
@@ -22,10 +22,12 @@ import {
22
22
  isoBlockContains,
23
23
  isoBlocksBehindLowerBound,
24
24
  parseFecBlockProfile,
25
+ serial32Add,
25
26
  serial32Distance,
26
27
  serial32Precedes,
27
28
  ssbg1FitsSymbol,
28
29
  type FecBlockProfile,
30
+ type FecInstanceGeometry,
29
31
  admitMmtpRepairObject,
30
32
  parseMoqRepairFrameWithHeader,
31
33
  type MMTPHeader,
@@ -136,6 +138,12 @@ export interface FecStats {
136
138
  workers: Array<FecTrackStats & { packetId: number }>
137
139
  /** Main-thread repair symbols rejected before dispatch because their size did not match T. */
138
140
  repairSymbolSizeMismatches: number
141
+ /**
142
+ * Repair packets discarded because they arrived on a repair track the
143
+ * catalog does not list for their instance (fec-01 6.3.9): they delimit no
144
+ * block and anchor nothing.
145
+ */
146
+ unlistedRepairTrackRefusals: number
139
147
  /**
140
148
  * Repair objects discarded on admission (draft-ramadan-moq-fec-core-00 7.5,
141
149
  * draft-ramadan-moq-mmt-02 7.3), per FEC instance and reason. The instance
@@ -169,6 +177,17 @@ export interface FecPacketStats {
169
177
 
170
178
  export type FecSymbolSource = 'multicast' | 'moq'
171
179
 
180
+ /**
181
+ * What feedRebuiltSourceSymbol did with a rebuilt source: whether its block
182
+ * took it, and the block the synchronous decoder recovered with it, if any.
183
+ */
184
+ export interface RebuiltSourceFeed {
185
+ readonly taken: boolean
186
+ readonly recovered: Uint8Array | null
187
+ }
188
+
189
+ const NOT_TAKEN: RebuiltSourceFeed = Object.freeze({ taken: false, recovered: null })
190
+
172
191
  /**
173
192
  * One iso-ssbg1-v1 first-arrival source symbol MmtpRouter holds until a repair
174
193
  * symbol states its block: the exact protected packet (no SS_ID, unpadded),
@@ -201,6 +220,25 @@ export interface UndelimitedIsoLoss {
201
220
  readonly previousBlockStart?: number
202
221
  }
203
222
 
223
+ /**
224
+ * A pending hole the stream ran more than the locator's horizon past before
225
+ * its range repair ended (BLO-52085): no repair can state its blocks any
226
+ * more, and the release gate no longer waits for it.
227
+ */
228
+ export type ExpiredIsoHole = Pick<UndelimitedIsoLoss, 'packetId' | 'catalogTrackName' | 'holeStart' | 'holeLength'>
229
+
230
+ /**
231
+ * What a failed iso-ssbg1-v1 repair-less span was: the `length` SS_IDs from
232
+ * its start, between two stated blocks, of which `sources` arrived. No repair
233
+ * stated the blocks inside it, so the receiver cannot tell its complete
234
+ * blocks from the ones that lost a source: the span failed, not the block
235
+ * that starts it.
236
+ */
237
+ export interface IsoSpanFailure {
238
+ readonly length: number
239
+ readonly sources: number
240
+ }
241
+
204
242
  /**
205
243
  * How the multicast leg hands its symbols to this manager.
206
244
  *
@@ -510,10 +548,21 @@ export class FECManager {
510
548
  // 'pending' forever, which would park every later frame of its stream in
511
549
  // the in-order release gate. Each entry owns its handle; the callback
512
550
  // checks it still owns the key, so a settled or reset key is never touched.
513
- #pendingWorkerCleanupBounds = new Map<string, { handle: ReturnType<typeof setTimeout> }>()
551
+ /**
552
+ * Per owed Worker cleanup: the bound on its acknowledgement, when its block
553
+ * was due, and when its cleanup was asked for (see `onCleanupComplete`).
554
+ */
555
+ #pendingWorkerCleanupBounds = new Map<
556
+ string,
557
+ { handle: ReturnType<typeof setTimeout>; dueAtMs: number; askedAtMs: number }
558
+ >()
514
559
  // Source symbol count per track-scoped block — marks complete when K symbols arrive.
515
560
  #blockSourceCounts = new Map<string, number>()
516
561
  #blockSourceEsis = new Map<string, Set<number>>()
562
+ // Source ESIs per block that another FEC instance rebuilt and fed in
563
+ // (feedRebuiltSourceSymbol). Kept apart from #blockSourceEsis: they are
564
+ // not network arrivals and never make a block clean.
565
+ #blockRebuiltEsis = new Map<string, Set<number>>()
517
566
  // Authored SSB_length per block (ISO/IEC 23008-1 §C.5.3), learned from the
518
567
  // first repair symbol. The FFmpeg moq_mmt publisher closes a block every D
519
568
  // media units, usually before it holds K symbols; under iso-ssbg1-v1
@@ -522,10 +571,14 @@ export class FECManager {
522
571
  // SSB_length source arrivals and recoverable from SSB_length received
523
572
  // symbols; waiting for K fails every one of them.
524
573
  #blockAuthoredLengths = new Map<string, number>()
574
+ // Keys whose authored length is a repair-less span's (#openIsoSpan), until
575
+ // a repair states the block there or the span resolves.
576
+ #isoSpanKeys = new Set<string>()
525
577
  #ssbLengthConflictCount = 0
526
578
  #blockRepairRsIds = new Map<string, Set<number>>()
527
579
  // RSB_length per block (C.5.3 Repair FEC Payload ID): how many repair
528
- // symbols its repair group carries. Same lifecycle as #blockRepairRsIds.
580
+ // symbols its repair group carries; 0 for a repair-less span no repair can
581
+ // state (#openIsoSpan). Same lifecycle as #blockRepairRsIds.
529
582
  #blockRsbLengths = new Map<string, number>()
530
583
  /** fec-01 6.3.9 anchor state per stream key, one entry per held layer. */
531
584
  #heldLayerStates = new Map<string, HeldLayerAdmission>()
@@ -718,6 +771,7 @@ export class FECManager {
718
771
  #workerErrorCounts = new Map<string, number>()
719
772
  #decoderUnavailableEvents = 0
720
773
  #repairSymbolSizeMismatchCount = 0
774
+ #unlistedRepairTrackRefusals = 0
721
775
 
722
776
  // Event callbacks — block lifecycle notifications for MmtpRouter wiring.
723
777
  // NOTE: For sync WASM path, recovery routing is handled by MmtpRouter from
@@ -729,13 +783,17 @@ export class FECManager {
729
783
  onBlockComplete:
730
784
  | ((sbn: number, packetId: number, catalogTrackName?: string) => void)
731
785
  | null = null
732
- /** A block reached its fail bound. `blockClass` is its class at failure. */
786
+ /**
787
+ * A block reached its fail bound. `blockClass` is its class at failure.
788
+ * `span` is set when what failed is a repair-less span starting at `sbn`.
789
+ */
733
790
  onBlockFailed:
734
791
  | ((
735
792
  sbn: number,
736
793
  packetId: number,
737
794
  catalogTrackName: string | undefined,
738
795
  blockClass: BlockClass,
796
+ span?: IsoSpanFailure,
739
797
  ) => void)
740
798
  | null = null
741
799
  /**
@@ -819,6 +877,8 @@ export class FECManager {
819
877
  * for it, which it must be.
820
878
  */
821
879
  #onUndelimitedLoss: ((loss: UndelimitedIsoLoss) => void) | null = null
880
+ /** Set with the loss handler: true takes an expired hole to close (`closeIsoHoles`). */
881
+ #onIsoHoleExpired: ((hole: ExpiredIsoHole) => boolean) | null = null
822
882
  /** A block every symbol of which vanished was created by SBN evidence. */
823
883
  onBlockVanished:
824
884
  | ((sbn: number, packetId: number, catalogTrackName: string | undefined) => void)
@@ -831,6 +891,21 @@ export class FECManager {
831
891
  onSourceDiscontinuity:
832
892
  | ((packetId: number, catalogTrackName: string | undefined) => void)
833
893
  | null = null
894
+ /**
895
+ * The span and tolerance the catalog states for a keyframe overlay
896
+ * installed over a source track (draft-ramadan-moq-mmt-02 7.4.1: the
897
+ * overlay instance's own interleaveDepthMs and reorderToleranceMs), or
898
+ * undefined while none is installed. A stated block only a late source
899
+ * can complete, and a frame whose gap may hide a RAP behind a span only a
900
+ * late source can fill, also wait for that overlay's repair
901
+ * (#overlayLateWaitMs).
902
+ */
903
+ keyframeOverlayTiming:
904
+ | ((
905
+ packetId: number,
906
+ catalogTrackName: string | undefined,
907
+ ) => Pick<FecInstanceGeometry, 'interleaveDepthMs' | 'reorderToleranceMs'> | undefined)
908
+ | null = null
834
909
 
835
910
  // Worker-path callbacks — async recovery and stats from FecWorkerClient.
836
911
  /**
@@ -1473,7 +1548,12 @@ export class FECManager {
1473
1548
  timer.handle = null
1474
1549
  this.#requestRepair(sbn, packetId, catalogTrackName)
1475
1550
  }
1476
- timer.handle = setTimeout(onFire, Math.max(0, deadlineMs - performance.now()))
1551
+ timer.handle = setTimeout(
1552
+ onFire,
1553
+ this.#receiptAtMs === undefined
1554
+ ? Math.max(0, deadlineMs - performance.now())
1555
+ : this.#receiptTimerDelayMs(deadlineMs, 0),
1556
+ )
1477
1557
  this.#repairDeadlines.set(key, timer)
1478
1558
  }
1479
1559
 
@@ -1667,6 +1747,12 @@ export class FECManager {
1667
1747
  * Vanished blocks are not enumerated here: their starts are unknown until a
1668
1748
  * repair states them. The SS_IDs between two stated blocks become one
1669
1749
  * repair-less span when the later block's repair arrives (placeIsoRepair).
1750
+ *
1751
+ * A `held` source arrived before its block was known and is fed only now
1752
+ * that a repair, a span or a closed hole placed it: how far its block start
1753
+ * then lies behind the frontier measures how long the receiver held it, not
1754
+ * where the stream was, so only its SS_ID and packet_sequence_number judge
1755
+ * it (BLO-52085).
1670
1756
  */
1671
1757
  #noteSbnSeen(
1672
1758
  sbn: number,
@@ -1675,6 +1761,7 @@ export class FECManager {
1675
1761
  symbol: 'source' | 'repair',
1676
1762
  packetSequenceNumber?: number,
1677
1763
  ssId = sbn,
1764
+ held = false,
1678
1765
  ): void {
1679
1766
  const streamKey = this.#streamKey(packetId, catalogTrackName)
1680
1767
  // Arrival at FEC, not receipt (withReceiptTime): this bounds how far SS_ID
@@ -1731,7 +1818,7 @@ export class FECManager {
1731
1818
  }
1732
1819
  if (!this.#blockPrecedes(previous.sbn, sbn)) {
1733
1820
  if (symbol === 'source' && window !== null && this.#blockPrecedes(sbn, previous.sbn) && (
1734
- this.#blocksBehind(sbn, previous.sbn, packetId) > this.#isoRetentionBlocks(packetId) ||
1821
+ (!held && this.#blocksBehind(sbn, previous.sbn, packetId) > this.#isoRetentionBlocks(packetId)) ||
1735
1822
  (psn !== undefined && anchor !== undefined && serial32Precedes(ssId, anchor.ssId) &&
1736
1823
  psn !== anchor.psn && !sentInOrder({ ssId, psn }, anchor))
1737
1824
  )) {
@@ -2085,21 +2172,28 @@ export class FECManager {
2085
2172
  this.#blockWaitDeadlinesMs.delete(key)
2086
2173
  this.#repairHoldUntilMs.delete(key)
2087
2174
  this.#traceLifecycle('timeout-queued', sbn, packetId, catalogTrackName)
2088
- this.#finalizeExpiredBlock(sbn, packetId, catalogTrackName)
2175
+ this.#finalizeExpiredBlock(sbn, packetId, catalogTrackName, currentDeadlineMs ?? firedAtMs)
2089
2176
  }
2090
- timer.handle = setTimeout(onFire, Math.max(1, Math.ceil(deadlineMs - nowMs)))
2177
+ timer.handle = setTimeout(
2178
+ onFire,
2179
+ this.#receiptAtMs === undefined
2180
+ ? Math.max(1, Math.ceil(deadlineMs - nowMs))
2181
+ : this.#receiptTimerDelayMs(deadlineMs, 1),
2182
+ )
2091
2183
  this.#activeBlocks.set(key, timer)
2092
2184
  this.#indexBlockKey(key, sbn, packetId)
2093
2185
  }
2094
2186
 
2095
2187
  /**
2096
2188
  * Hand a block whose delivery is over to its decoder for finalization: the
2097
- * datagram deadline fired, or the ordered relay moved past it.
2189
+ * datagram deadline fired, or the ordered relay moved past it. `dueAtMs`
2190
+ * is when it was due: its deadline, or now.
2098
2191
  */
2099
2192
  #finalizeExpiredBlock(
2100
2193
  sbn: number,
2101
2194
  packetId: number,
2102
2195
  catalogTrackName: string | undefined,
2196
+ dueAtMs = performance.now(),
2103
2197
  ): void {
2104
2198
  const key = this.#blockKey(sbn, packetId, catalogTrackName)
2105
2199
  // A vanished block that never received a symbol holds no decoder state
@@ -2123,7 +2217,7 @@ export class FECManager {
2123
2217
  if (workerClient) {
2124
2218
  this.#pendingWorkerCleanups.add(key)
2125
2219
  this.#pendingWorkerCleanupClients.set(key, workerClient)
2126
- this.#boundOwedCleanup(key, packetId, workerClient)
2220
+ this.#boundOwedCleanup(key, packetId, workerClient, dueAtMs)
2127
2221
  workerClient.cleanup(sbn)
2128
2222
  } else {
2129
2223
  const decoder = this.#decoderForPacket(packetId, catalogTrackName)?.client
@@ -2421,6 +2515,7 @@ export class FECManager {
2421
2515
  packetId,
2422
2516
  })),
2423
2517
  repairSymbolSizeMismatches: this.#repairSymbolSizeMismatchCount,
2518
+ unlistedRepairTrackRefusals: this.#unlistedRepairTrackRefusals,
2424
2519
  repairObjectRejects: [...this.#repairObjectRejects.values()].map((r) => ({ ...r })),
2425
2520
  workerErrors: Object.fromEntries(this.#workerErrorCounts),
2426
2521
  }
@@ -2540,7 +2635,9 @@ export class FECManager {
2540
2635
  }
2541
2636
  resetMap(this.#blockSourceCounts)
2542
2637
  resetMap(this.#blockSourceEsis)
2638
+ resetMap(this.#blockRebuiltEsis)
2543
2639
  resetMap(this.#blockAuthoredLengths)
2640
+ resetSet(this.#isoSpanKeys)
2544
2641
  resetMap(this.#blockRepairRsIds)
2545
2642
  resetMap(this.#blockRsbLengths)
2546
2643
  resetMap(this.#heldLayerStates)
@@ -2672,7 +2769,9 @@ export class FECManager {
2672
2769
  dropMap(this.#pendingWorkerCleanupClients)
2673
2770
  dropMap(this.#blockSourceCounts)
2674
2771
  dropMap(this.#blockSourceEsis)
2772
+ dropMap(this.#blockRebuiltEsis)
2675
2773
  dropMap(this.#blockAuthoredLengths)
2774
+ dropSet(this.#isoSpanKeys)
2676
2775
  dropMap(this.#blockRepairRsIds)
2677
2776
  dropMap(this.#blockRsbLengths)
2678
2777
  dropMap(this.#blockPathCounts)
@@ -2891,6 +2990,10 @@ export class FECManager {
2891
2990
  }
2892
2991
  }
2893
2992
 
2993
+ if (isSourceSymbol && source === 'multicast') {
2994
+ this.#noteMulticastSource(this.#isoLocatorFor(packetId, catalogTrackName), (sbn + esi) >>> 0, packetId, catalogTrackName)
2995
+ }
2996
+
2894
2997
  return true
2895
2998
  }
2896
2999
 
@@ -3016,8 +3119,10 @@ export class FECManager {
3016
3119
  }
3017
3120
  /**
3018
3121
  * An open block's accepted sources, distinct repair RS_IDs, authored K'
3019
- * (SSB_length) and RSB_length (each undefined until a repair stated it).
3020
- * Undefined for a block that is not open.
3122
+ * (SSB_length) and RSB_length (each undefined until a repair stated it; a
3123
+ * repair-less span states its length, and RSB_length 0 when it is longer
3124
+ * than K, since no repair can reach it). Undefined for a block that is not
3125
+ * open.
3021
3126
  */
3022
3127
  blockSymbolCounts(
3023
3128
  sbn: number,
@@ -3100,8 +3205,9 @@ export class FECManager {
3100
3205
  return true
3101
3206
  }
3102
3207
  /**
3103
- * Drop a block's repair hold: its FETCH ended without delivering. The block
3104
- * falls back to its multicast wait, which may already have passed.
3208
+ * Drop a block's repair hold: its FETCH ended without delivering, or brought
3209
+ * back only a repair that cannot complete it. The block falls back to its
3210
+ * multicast wait, which may already have passed.
3105
3211
  */
3106
3212
  releaseRepairHold(sbn: number, packetId: number, catalogTrackName?: string): void {
3107
3213
  const key = this.#blockKey(sbn, packetId, catalogTrackName)
@@ -3151,7 +3257,7 @@ export class FECManager {
3151
3257
  }
3152
3258
  const trackConfig = this.#trackConfigs.get(packetId)
3153
3259
  if (trackConfig === undefined) return undefined
3154
- return Math.max(1, trackConfig.timeoutMs - this.#repairDeadlineMs(trackConfig))
3260
+ return Math.max(1, this.#repairGraceMs(trackConfig, this.#multicastWaitMs(trackConfig)))
3155
3261
  }
3156
3262
 
3157
3263
  /** True once configureTrack/setFecDecoder/setFecWorker configured this packetId. */
@@ -3263,6 +3369,8 @@ export class FECManager {
3263
3369
  const locator = this.#growingIsoLocatorFor(packetId, catalogTrackName)
3264
3370
  const evictedBefore = locator.heldEvicted
3265
3371
  const held = locator.hold(ssId, source)
3372
+ this.#settleExpiredIsoHoles(locator, packetId, catalogTrackName)
3373
+ if (held && source.source === 'multicast') this.#noteMulticastSource(locator, ssId, packetId, catalogTrackName)
3266
3374
  const evicted = locator.heldEvicted - evictedBefore
3267
3375
  if (evicted > 0) {
3268
3376
  const before = this.#isoHeldEvictedCount
@@ -3290,17 +3398,26 @@ export class FECManager {
3290
3398
  ssStart: number,
3291
3399
  ssbLength: number,
3292
3400
  ): IsoRepairPlacement<IsoHeldSource> {
3293
- const placement = this.#growingIsoLocatorFor(packetId, catalogTrackName).placeRepair(ssStart, ssbLength)
3401
+ const locator = this.#growingIsoLocatorFor(packetId, catalogTrackName)
3402
+ const placement = locator.placeRepair(ssStart, ssbLength)
3403
+ this.#settleExpiredIsoHoles(locator, packetId, catalogTrackName)
3294
3404
  if (placement.status !== 'placed') {
3295
3405
  this.#isoRepairRefusedCount++
3296
3406
  if (this.#isoRepairRefusedCount <= 5 || this.#isoRepairRefusedCount % 100 === 0) {
3297
3407
  console.warn(`[FECManager] packetId=${packetId} iso-ssbg1-v1 repair refused (${placement.status}): ${placement.reason}`)
3298
3408
  }
3409
+ // The repair FETCH of a span's first group brought back a shorter block:
3410
+ // the span holds more than one, so that group cannot complete it, and
3411
+ // holding the span (and every frame behind it) for the FETCH buys
3412
+ // nothing. It falls back to its multicast wait.
3413
+ if (placement.ofSpan !== undefined) this.releaseRepairHold(placement.ofSpan.start, packetId, catalogTrackName)
3299
3414
  return placement
3300
3415
  }
3416
+ // A repair states the block at its SS_Start: a span the locator kept
3417
+ // there is now that one block (IsoBlockLocator.placeRepair).
3418
+ this.#isoSpanKeys.delete(this.#blockKey(placement.block.start, packetId, catalogTrackName))
3301
3419
  if (placement.span !== undefined) this.#openIsoSpan(placement.span, packetId, catalogTrackName)
3302
3420
  if (placement.hole !== undefined) {
3303
- const locator = this.#isoLocatorFor(packetId, catalogTrackName)
3304
3421
  const hole = placement.hole.block
3305
3422
  const previous = locator.blockEndingAt(hole.start)
3306
3423
  const loss: UndelimitedIsoLoss = {
@@ -3322,18 +3439,38 @@ export class FECManager {
3322
3439
  * `handler`, instead of a repair-less span; null restores spans. Set by the
3323
3440
  * range repair scheduler; applies to every stream from its next gap. The
3324
3441
  * handler and the deferral are one setting, so no hole is ever created with
3325
- * nobody to close it.
3442
+ * nobody to close it. `onExpired` is told of each pending hole the stream
3443
+ * runs past the locator's horizon (BLO-52085): returning true, it closes
3444
+ * the hole (`closeIsoHoles`), so the span it leaves still fails and is
3445
+ * reported; otherwise the hole is discarded with its sources.
3326
3446
  */
3327
- setUndelimitedLossHandler(handler: ((loss: UndelimitedIsoLoss) => void) | null): void {
3447
+ setUndelimitedLossHandler(
3448
+ handler: ((loss: UndelimitedIsoLoss) => void) | null,
3449
+ onExpired: ((hole: ExpiredIsoHole) => boolean) | null = null,
3450
+ ): void {
3328
3451
  this.#onUndelimitedLoss = handler
3452
+ this.#onIsoHoleExpired = handler === null ? null : onExpired
3329
3453
  this.#deferIsoGaps = handler !== null
3330
3454
  for (const locator of this.#isoLocators.values()) locator.deferIncompleteGaps = this.#deferIsoGaps
3331
3455
  }
3332
3456
 
3457
+ /** Hand each hole the locator's horizon passed to its owner, or discard it (setUndelimitedLossHandler). */
3458
+ #settleExpiredIsoHoles(
3459
+ locator: IsoBlockLocator<IsoHeldSource>,
3460
+ packetId: number,
3461
+ catalogTrackName: string | undefined,
3462
+ ): void {
3463
+ for (const hole of locator.takeExpiredHoles()) {
3464
+ const expired: ExpiredIsoHole = { packetId, catalogTrackName, holeStart: hole.start, holeLength: hole.length }
3465
+ if (this.#onIsoHoleExpired?.(expired) !== true) locator.closeHolesWithin(hole.start, hole.length)
3466
+ }
3467
+ }
3468
+
3333
3469
  /**
3334
- * The range repair for a hole ended: every hole still pending inside
3335
- * `[start, start + length)` becomes a repair-less span. Returns those spans
3336
- * with the held sources they placed, for the caller to feed (MmtpRouter).
3470
+ * The range repair for a hole ended: every hole inside `[start, start +
3471
+ * length)`, pending or expired, becomes a repair-less span. Returns those
3472
+ * spans with the held sources they placed, for the caller to feed
3473
+ * (MmtpRouter).
3337
3474
  */
3338
3475
  closeIsoHoles(
3339
3476
  packetId: number,
@@ -3355,6 +3492,17 @@ export class FECManager {
3355
3492
  * its placed sources exactly as on the wire (feedRepairSymbol). A span every
3356
3493
  * SS_ID of which vanished resolves through its deadline like any vanished
3357
3494
  * block.
3495
+ *
3496
+ * A span longer than K holds more than one block (SSB_length <= K), and the
3497
+ * locator states no block inside a span, only one exactly its range
3498
+ * (IsoBlockLocator.placeRepair): no repair, multicast or FETCHed, can ever
3499
+ * reach it. Its repair group is empty, so the repair scheduler FETCHes no
3500
+ * Group of it (group-cannot-complete). Every span still keeps its whole
3501
+ * wait (#lateSourceWaitMs): failing it early released the frames behind it
3502
+ * past a keyframe the overlay was still rebuilding (BLO-52085). The release
3503
+ * gate lets those frames go once only a late source can fill the gap
3504
+ * before them, as long after they arrived as the gap's decision allows
3505
+ * (isoRepairlessWaitMs).
3358
3506
  */
3359
3507
  #openIsoSpan(
3360
3508
  span: { readonly block: IsoBlockRange; readonly placed: readonly unknown[] },
@@ -3362,7 +3510,16 @@ export class FECManager {
3362
3510
  catalogTrackName: string | undefined,
3363
3511
  ): void {
3364
3512
  const key = this.#blockKey(span.block.start, packetId, catalogTrackName)
3365
- if (!this.#blockAuthoredLengths.has(key)) this.#blockAuthoredLengths.set(key, span.block.length)
3513
+ // A span's key is always new here. An authored length is set only here or
3514
+ // from a repair stating that block (#noteAuthoredLength), and the locator
3515
+ // opens a span only where no repair stated a block, so none can precede
3516
+ // it. Were one to, the guard would skip the RSB_length 0 that keeps the
3517
+ // repair scheduler from FETCHing a long span's first Group.
3518
+ if (!this.#blockAuthoredLengths.has(key)) {
3519
+ this.#blockAuthoredLengths.set(key, span.block.length)
3520
+ this.#isoSpanKeys.add(key)
3521
+ if (span.block.length > this.#isoLocatorFor(packetId, catalogTrackName).kMax) this.#blockRsbLengths.set(key, 0)
3522
+ }
3366
3523
  if (span.placed.length === 0) this.#materializeBlock(span.block.start, packetId, catalogTrackName, true)
3367
3524
  }
3368
3525
 
@@ -3410,6 +3567,90 @@ export class FECManager {
3410
3567
  return lost
3411
3568
  }
3412
3569
 
3570
+ /**
3571
+ * The release gate's wait, from a frame's arrival, for the SS_IDs strictly
3572
+ * between two released frames' SS_IDs once only a late source can still
3573
+ * fill them, else undefined. That is when every one missing (neither
3574
+ * arrived nor held) lies in a repair-less span longer than K, which no
3575
+ * repair can state a block of (#openIsoSpan), or in a stated block its
3576
+ * own repair group cannot complete every SS_ID of which it misses lies
3577
+ * between them (so the gap's decision covers all it lost), and a multicast
3578
+ * source at or past `toSsId` arrived, so each is lost or reordered behind
3579
+ * it (a MoQ source shows nothing: MoQ streams overtake one another). The
3580
+ * span or block itself keeps its own wait.
3581
+ *
3582
+ * The wait is the late-symbol allowance, unless `rapMayHide`: the gap may
3583
+ * hold a RAP packet an installed keyframe overlay can still rebuild (its
3584
+ * decision stalls, or it ends at a RAP that may lie past a whole MPU).
3585
+ * Then the frame waits as a stated block its group cannot complete does
3586
+ * (#overlayLateWaitMs): the allowance alone with no overlay installed, the
3587
+ * overlay's span plus the larger of the allowance and its tolerance, or,
3588
+ * for an overlay that states no span, the span's whole wait (undefined).
3589
+ * So does `rapNext` (the gap ends at the RAP that opens the next MPU) when
3590
+ * the gap holds a stated block's loss.
3591
+ */
3592
+ isoRepairlessWaitMs(
3593
+ packetId: number,
3594
+ catalogTrackName: string | undefined,
3595
+ fromSsId: number,
3596
+ toSsId: number,
3597
+ rapMayHide = false,
3598
+ rapNext = false,
3599
+ ): number | undefined {
3600
+ const config = this.#trackConfigs.get(packetId)
3601
+ if (config === undefined || !FECManager.#hasCatalogTiming(config)) return undefined
3602
+ const locator = this.#isoLocators.get(this.#streamKey(packetId, catalogTrackName))
3603
+ if (locator === undefined || !locator.coversBetween(fromSsId, toSsId)) return undefined
3604
+ const newestSource = locator.newestSource
3605
+ if (newestSource === undefined || serial32Precedes(newestSource, toSsId)) return undefined
3606
+ let repairless = false
3607
+ let stated = false
3608
+ for (const block of locator.blocksBetween(fromSsId, toSsId)) {
3609
+ const key = this.#blockKey(block.start, packetId, catalogTrackName)
3610
+ if (this.#completedBlocks.has(key)) continue
3611
+ if (block.span && block.length > locator.kMax) {
3612
+ repairless = true
3613
+ continue
3614
+ }
3615
+ const received = this.#blockSourceEsis.get(key)
3616
+ const rebuilt = this.#blockRebuiltEsis.get(key)
3617
+ let missesInGap = false
3618
+ let missesOutside = false
3619
+ // A rebuilt source is present outside the gap but missed inside it.
3620
+ // Outside, the question is what the block's decoder lacks beyond this
3621
+ // gap, and it holds a rebuilt source (#groupCannotComplete). Inside, it
3622
+ // is what the base path lost between the two frames. A base recovery
3623
+ // does not route a rebuilt packet again (MmtpRouter
3624
+ // #routeVerifiedFecSymbols), so only the overlay route delivers it, and
3625
+ // its frame may not have reached the gate yet (a head held for its FT=1,
3626
+ // BLO-53369). The two differ only when every SS_ID a block misses in the
3627
+ // gap was rebuilt: the later frame then waits for the gap's blocks to
3628
+ // resolve (undefined), which is conservative.
3629
+ for (let esi = 0; esi < block.length; esi++) {
3630
+ const ssId = serial32Add(block.start, esi)
3631
+ if (received?.has(esi) || locator.isHeld(ssId)) continue
3632
+ if (serial32Precedes(fromSsId, ssId) && serial32Precedes(ssId, toSsId)) missesInGap = true
3633
+ else if (!rebuilt?.has(esi)) missesOutside = true
3634
+ }
3635
+ if (!missesInGap) continue
3636
+ // A stated block its own group cannot complete (#lateSourceWaitMs) is
3637
+ // one too, when every SS_ID it misses lies in this gap: the gap's
3638
+ // decision then covers all it lost, so the overlay can rebuild none of
3639
+ // it unless `rapMayHide` or `rapNext`.
3640
+ if (missesOutside || block.span || !this.#groupCannotComplete(key)) return undefined
3641
+ repairless = stated = true
3642
+ }
3643
+ if (!repairless) return undefined
3644
+ // A gap into the next MPU (`rapNext`) may hold that MPU's FT=0, an overlay
3645
+ // member; rebuilt, it is one more source of a stated block, which its group
3646
+ // may then complete. Such a block keeps #741's overlay wait; a span longer
3647
+ // than K has no group to complete and keeps #742's allowance.
3648
+ const waitMs = rapMayHide || (stated && rapNext)
3649
+ ? this.#overlayLateWaitMs(packetId, catalogTrackName, config)
3650
+ : this.#multicastWaitMs(config) - config.interleaveDepthMs
3651
+ return waitMs === undefined ? undefined : Math.max(0, waitMs)
3652
+ }
3653
+
3413
3654
  /**
3414
3655
  * iso-ssbg1-v1 block location: repair symbols it refused, oversize sources,
3415
3656
  * the sources held now for a block no repair has stated yet, and the held
@@ -3841,13 +4082,27 @@ export class FECManager {
3841
4082
 
3842
4083
  client.onCleanupComplete((sbn: number) => {
3843
4084
  if (this.#releasedWorkerClients.has(client)) return
3844
- const cleanupKeys = [...this.#pendingWorkerCleanups].filter((key) => {
4085
+ const owed = [...this.#pendingWorkerCleanups].filter((key) => this.#pendingWorkerCleanupClients.get(key) === client)
4086
+ const cleanupKeys = owed.filter((key) => {
3845
4087
  const block = this.#parseBlockKey(key)
3846
- return block.packetId === packetId &&
3847
- block.sbn === sbn &&
3848
- this.#pendingWorkerCleanupClients.get(key) === client
4088
+ return block.packetId === packetId && block.sbn === sbn
3849
4089
  })
3850
- for (const key of cleanupKeys) this.#settleOwedCleanup(key, 'cleanup-ack')
4090
+ // A cleanup is a queue-drain barrier: the Worker took every symbol fed
4091
+ // before it was asked for first, so every block already due by then
4092
+ // is settled by this acknowledgement too, not one barrier later. The
4093
+ // client sends one barrier at a time, and the blocks a repair walk
4094
+ // stated in one span are due together (BLO-52085). That holds only
4095
+ // while no block is fed a symbol after its dueAtMs, which is its
4096
+ // receipt-scoped deadline, not when feeding stopped: a symbol fed
4097
+ // between that deadline and the block's timer, after another block's
4098
+ // barrier was asked for, would find the block settled (failed) one
4099
+ // barrier early. Walks end before D-28 (walkDeadlineAt), so that window
4100
+ // is the timer's lateness alone; whatever feeds a block past its dueAtMs
4101
+ // must leave it to its own barrier.
4102
+ const askedAtMs = Math.max(...cleanupKeys.map((key) => this.#pendingWorkerCleanupBounds.get(key)?.askedAtMs ?? -Infinity))
4103
+ const due = owed.filter((key) =>
4104
+ !cleanupKeys.includes(key) && (this.#pendingWorkerCleanupBounds.get(key)?.dueAtMs ?? Infinity) <= askedAtMs)
4105
+ for (const key of [...cleanupKeys, ...due]) this.#settleOwedCleanup(key, 'cleanup-ack')
3851
4106
  })
3852
4107
 
3853
4108
  client.onError((msg: string, code?: FecWorkerErrorCode) => {
@@ -3929,9 +4184,11 @@ export class FECManager {
3929
4184
  * fail deadline already passed, so failing it is the decision that deadline
3930
4185
  * made. A late ack then finds nothing owed and is a no-op.
3931
4186
  */
3932
- #boundOwedCleanup(key: string, packetId: number, client: FecWorkerClient): void {
4187
+ #boundOwedCleanup(key: string, packetId: number, client: FecWorkerClient, dueAtMs: number): void {
3933
4188
  this.#clearOwedCleanupBound(key)
3934
4189
  const bound = {
4190
+ dueAtMs,
4191
+ askedAtMs: performance.now(),
3935
4192
  handle: setTimeout(() => {
3936
4193
  if (this.#pendingWorkerCleanupBounds.get(key) !== bound) return
3937
4194
  this.#pendingWorkerCleanupBounds.delete(key)
@@ -4062,7 +4319,22 @@ export class FECManager {
4062
4319
  this.#isoSourceOversizeCount++
4063
4320
  return null
4064
4321
  }
4065
- const sbn = located.block.start
4322
+ return this.#feedDecoderSource(packetId, ssId, symbolData, catalogTrackName, located.block.start)
4323
+ }
4324
+
4325
+ /**
4326
+ * Hand one placed source symbol to the stream's decoder: its Worker, which
4327
+ * answers through onFrame (null here), or its synchronous decoder, whose
4328
+ * recovered block is returned. `sbn` is the SS_Start of the block a repair
4329
+ * stated for `ssId`.
4330
+ */
4331
+ #feedDecoderSource(
4332
+ packetId: number,
4333
+ ssId: number,
4334
+ symbolData: Uint8Array,
4335
+ catalogTrackName: string | undefined,
4336
+ sbn: number,
4337
+ ): Uint8Array | null {
4066
4338
  const blockKey = this.#blockKey(sbn, packetId, catalogTrackName)
4067
4339
  const syncBlockOwner = this.#syncDecoderBlockOwners.get(blockKey)
4068
4340
 
@@ -4107,6 +4379,97 @@ export class FECManager {
4107
4379
  return recovered
4108
4380
  }
4109
4381
 
4382
+ /**
4383
+ * Feed a source symbol of this stream that another FEC instance over the
4384
+ * same packets rebuilt: a keyframe overlay recovery whose base SS_ID the
4385
+ * caller determined (draft-ramadan-moq-mmt-02 7.1, 7.4.4; joint decoding,
4386
+ * BLO-52085). It joins its block's decoder as a received source would, so
4387
+ * it reduces the erasures the block still has to solve, but it is not a
4388
+ * network arrival: no transport accounting, no classification evidence,
4389
+ * no deadline refresh, and it never makes a block clean.
4390
+ *
4391
+ * Only a block a repair stated takes one (a repair-less span has no
4392
+ * decoder block). A block that is terminal, or already holds that source,
4393
+ * does not. The symbol is the exact protected packet, unpadded, as
4394
+ * feedSourceSymbol takes it.
4395
+ *
4396
+ * `recovered` is the block the synchronous decoder completed with it; the
4397
+ * caller routes it and completes the block, as for any recovery. When
4398
+ * every source of the block is then present, received or rebuilt, and no
4399
+ * decoder returned it (a Worker answers asynchronously, and reports such a
4400
+ * block as clean on its own side), the block is completed here as
4401
+ * recovered, with nothing left to route.
4402
+ */
4403
+ feedRebuiltSourceSymbol(
4404
+ packetId: number,
4405
+ ssId: number,
4406
+ symbolData: Uint8Array,
4407
+ catalogTrackName?: string,
4408
+ ): RebuiltSourceFeed {
4409
+ if (!this.#hasBlockProfile(packetId)) return NOT_TAKEN
4410
+ const located = this.#isoLocatorFor(packetId, catalogTrackName).locate(ssId)
4411
+ if (located === null || located.block.span) return NOT_TAKEN
4412
+ if (!ssbg1FitsSymbol(symbolData.byteLength, this.#trackConfig(packetId).symbolSize)) return NOT_TAKEN
4413
+ const sbn = located.block.start
4414
+ const key = this.#blockKey(sbn, packetId, catalogTrackName)
4415
+ if (this.#completedBlocks.has(key) || this.#blockSourceEsis.get(key)?.has(located.esi)) return NOT_TAKEN
4416
+ let rebuilt = this.#blockRebuiltEsis.get(key)
4417
+ if (rebuilt?.has(located.esi)) return NOT_TAKEN
4418
+ if (rebuilt === undefined) {
4419
+ rebuilt = new Set()
4420
+ this.#blockRebuiltEsis.set(key, rebuilt)
4421
+ }
4422
+ const config = this.#trackConfigs.get(packetId)
4423
+ const wasShort = config !== undefined &&
4424
+ this.#lateSourceWaitMs(key, sbn, packetId, catalogTrackName, config) !== undefined
4425
+ rebuilt.add(located.esi)
4426
+ const recovered = this.#feedDecoderSource(packetId, ssId, symbolData, catalogTrackName, sbn)
4427
+ if (recovered === null && !this.#completeWhenSourcesPresent(key, sbn, located.block.length, packetId, catalogTrackName)) {
4428
+ // The late-source cut assumed only a late source could complete this
4429
+ // block; with this source its group can, so it gets a full multicast
4430
+ // wait again, counted from now.
4431
+ // ponytail: from now, not from its sources, so it can close up to one
4432
+ // multicast wait after its uncut deadline; at most once per block, since
4433
+ // a rebuilt source only adds to what the block holds and the cut cannot
4434
+ // apply to it again. Keep the uncut deadline and re-arm to it if a lab
4435
+ // run shows that wait.
4436
+ if (wasShort && this.#activeBlocks.has(key) &&
4437
+ this.#lateSourceWaitMs(key, sbn, packetId, catalogTrackName, config!) === undefined) {
4438
+ this.#armBlockTimeout(sbn, packetId, catalogTrackName, this.#multicastWaitMs(config!))
4439
+ }
4440
+ }
4441
+ return { taken: true, recovered }
4442
+ }
4443
+
4444
+ /**
4445
+ * A block that holds every one of its `length` sources, received or
4446
+ * rebuilt by another instance, with at least one rebuilt, is recovered:
4447
+ * every packet of it was delivered, by its own arrival or by the instance
4448
+ * that rebuilt it, so nothing is routed. Returns whether it completed it.
4449
+ */
4450
+ #completeWhenSourcesPresent(
4451
+ key: string,
4452
+ sbn: number,
4453
+ length: number,
4454
+ packetId: number,
4455
+ catalogTrackName: string | undefined,
4456
+ ): boolean {
4457
+ const rebuilt = this.#blockRebuiltEsis.get(key)
4458
+ if (rebuilt === undefined || this.#completedBlocks.has(key)) return false
4459
+ let present = rebuilt.size
4460
+ for (const esi of this.#blockSourceEsis.get(key) ?? []) if (!rebuilt.has(esi)) present++
4461
+ if (present < length) return false
4462
+ this.#traceLifecycle('recovered', sbn, packetId, catalogTrackName)
4463
+ this.countRecovery(packetId, sbn, catalogTrackName)
4464
+ this.cancelBlockTimeout(sbn, packetId, catalogTrackName)
4465
+ if (catalogTrackName === undefined) {
4466
+ this.onBlockComplete?.(sbn, packetId)
4467
+ } else {
4468
+ this.onBlockComplete?.(sbn, packetId, catalogTrackName)
4469
+ }
4470
+ return true
4471
+ }
4472
+
4110
4473
  /**
4111
4474
  * fec-01 4.5: a reed-solomon repair packet whose SSB_length + RSB_length
4112
4475
  * exceeds 255, or whose RS_ID is not below RSB_length, is discarded with the
@@ -4187,7 +4550,10 @@ export class FECManager {
4187
4550
  this.#layerAnchorHorizon(packetId),
4188
4551
  this.#sourceFrontiers.get(streamKey)?.sbn,
4189
4552
  )
4190
- if (verdict === 'discard') return false
4553
+ if (verdict === 'discard') {
4554
+ this.#unlistedRepairTrackRefusals++
4555
+ return false
4556
+ }
4191
4557
  if (verdict !== undefined) {
4192
4558
  this.#failClosed(streamKey, verdict, packetId, catalogTrackName)
4193
4559
  return false
@@ -4364,9 +4730,150 @@ export class FECManager {
4364
4730
  this.#completeCleanBlock(blockKey, sbn, packetId, catalogTrackName)
4365
4731
  return null
4366
4732
  }
4733
+ // Its K' is known now: a block this repair shows short of it waits less.
4734
+ if (recovered === null) this.#shortenUnrecoverableWait(blockKey, sbn, packetId, catalogTrackName)
4367
4735
  return recovered
4368
4736
  }
4369
4737
 
4738
+ /**
4739
+ * A first-arrival multicast source, held or placed. A datagram flow keeps
4740
+ * its sources in send order, so one that moves the stream's newest source
4741
+ * position past a block leaves the sources that block still misses
4742
+ * reordered behind it at best: it shortens the wait of every block it
4743
+ * passed that only a late source can complete. MoQ is left out: its streams
4744
+ * can overtake one another, so a MoQ source shows nothing about the sources
4745
+ * sent before it.
4746
+ */
4747
+ #noteMulticastSource(
4748
+ locator: IsoBlockLocator<IsoHeldSource>,
4749
+ ssId: number,
4750
+ packetId: number,
4751
+ catalogTrackName: string | undefined,
4752
+ ): void {
4753
+ const previous = locator.newestSource
4754
+ if (!locator.noteSource(ssId) || previous === undefined) return
4755
+ // The blocks whose last SS_ID lies in [previous, ssId): this source
4756
+ // passed them and the previous newest one had not.
4757
+ for (const block of locator.blocksBetween((previous - 1) >>> 0, ssId)) {
4758
+ if (!serial32Precedes(serial32Add(block.start, block.length - 1), ssId)) continue
4759
+ this.#shortenUnrecoverableWait(
4760
+ this.#blockKey(block.start, packetId, catalogTrackName),
4761
+ block.start,
4762
+ packetId,
4763
+ catalogTrackName,
4764
+ )
4765
+ }
4766
+ }
4767
+
4768
+ /**
4769
+ * The wait of a block that only a late source can complete (BLO-52085): a
4770
+ * repair stated it, its received sources plus every symbol its repair group
4771
+ * carries (RSB_length) are short of K' (the repair scheduler's
4772
+ * group-cannot-complete), and a later source of the stream has arrived, so
4773
+ * a source it misses is lost or reordered behind that one. The span, which
4774
+ * covers repair symbols paced after its sources, buys it nothing: it waits
4775
+ * the late-symbol allowance alone, the multicast wait less the span (the
4776
+ * catalog tolerance or the measured delivery gap). Undefined for any other
4777
+ * block. While a keyframe overlay is installed, the block waits for its
4778
+ * repair too (#overlayLateWaitMs). A repair-less span keeps its whole
4779
+ * wait, even one longer than K whose group is empty (#openIsoSpan): its
4780
+ * resolution is what releases the frames behind it, and failing it early
4781
+ * released them past a keyframe the overlay was still rebuilding, which
4782
+ * then arrived stale. The release gate lets them go instead
4783
+ * (isoRepairlessWaitMs).
4784
+ */
4785
+ #lateSourceWaitMs(
4786
+ key: string,
4787
+ sbn: number,
4788
+ packetId: number,
4789
+ catalogTrackName: string | undefined,
4790
+ config: ResolvedFECTrackConfig,
4791
+ ): number | undefined {
4792
+ if (this.#isoSpanKeys.has(key) || !this.#groupCannotComplete(key)) return undefined
4793
+ const authoredLength = this.#blockAuthoredLengths.get(key)!
4794
+ const newestSource = this.#isoLocators.get(this.#streamKey(packetId, catalogTrackName))?.newestSource
4795
+ if (newestSource === undefined || !serial32Precedes(serial32Add(sbn, authoredLength - 1), newestSource)) {
4796
+ return undefined
4797
+ }
4798
+ return this.#overlayLateWaitMs(packetId, catalogTrackName, config)
4799
+ }
4800
+
4801
+ /**
4802
+ * A repair stated the block, and its sources plus every symbol its repair
4803
+ * group carries (RSB_length) are short of its SSB_length.
4804
+ */
4805
+ #groupCannotComplete(key: string): boolean {
4806
+ const authoredLength = this.#blockAuthoredLengths.get(key)
4807
+ const rsbLength = this.#blockRsbLengths.get(key)
4808
+ if (authoredLength === undefined || rsbLength === undefined) return false
4809
+ // A source another instance rebuilt (feedRebuiltSourceSymbol) is one the
4810
+ // decoder holds: the group need only cover what neither delivered.
4811
+ let present = this.#blockSourceCounts.get(key) ?? 0
4812
+ const received = this.#blockSourceEsis.get(key)
4813
+ for (const esi of this.#blockRebuiltEsis.get(key) ?? []) if (!received?.has(esi)) present++
4814
+ return present + rsbLength < authoredLength
4815
+ }
4816
+
4817
+ /**
4818
+ * How long a loss only a late source or an installed keyframe overlay can
4819
+ * still fill waits once a later source passed it: the late-symbol
4820
+ * allowance alone (the multicast wait less the span) while no overlay is
4821
+ * installed. An installed one (keyframeOverlayTiming) may still rebuild a
4822
+ * RAP packet the loss took: a second FEC instance over the same packets
4823
+ * (draft-ramadan-moq-mmt-02 7.4), whose repair is paced across its own
4824
+ * span, unrelated to the base's. That packet can complete a stated block
4825
+ * (feedRebuiltSourceSymbol), or play behind a span; giving the loss up
4826
+ * first lets the release gate pass the keyframe, whose rebuild then lands
4827
+ * stale. The overlay repair of a packet the loss took goes out within the
4828
+ * overlay's span of that packet (fec-core-00 9.3), and the later source was
4829
+ * sent after it, so the loss waits that span plus the larger of the
4830
+ * allowance and the overlay's own tolerance. An overlay that states no
4831
+ * span (its block may then span keyframes), or one whose wait is not
4832
+ * shorter than the whole multicast wait, leaves the whole wait: undefined.
4833
+ */
4834
+ #overlayLateWaitMs(
4835
+ packetId: number,
4836
+ catalogTrackName: string | undefined,
4837
+ config: ResolvedFECTrackConfig,
4838
+ ): number | undefined {
4839
+ const waitMs = this.#multicastWaitMs(config)
4840
+ const allowanceMs = waitMs - config.interleaveDepthMs
4841
+ // Read live. The late-source cut only ever shortens a block's deadline
4842
+ // (#shortenUnrecoverableWait), so a block it armed at the bare allowance
4843
+ // before an overlay was installed, or between a dispose and a reinstall,
4844
+ // keeps that deadline until a source of its own re-arms it.
4845
+ const overlay = this.keyframeOverlayTiming?.(packetId, catalogTrackName)
4846
+ if (overlay === undefined) return allowanceMs
4847
+ const overlaySpanMs = overlay.interleaveDepthMs ?? 0
4848
+ const overlayWaitMs = overlaySpanMs + Math.max(allowanceMs, overlay.reorderToleranceMs ?? 0)
4849
+ return overlaySpanMs > 0 && overlayWaitMs < waitMs ? overlayWaitMs : undefined
4850
+ }
4851
+
4852
+ /**
4853
+ * Bring an open block's wait in to its late-source wait, when it has one.
4854
+ * Never later than it is armed: only a source can complete such a block,
4855
+ * so a repair of its group (paced across the span) must not keep it open.
4856
+ */
4857
+ #shortenUnrecoverableWait(
4858
+ key: string,
4859
+ sbn: number,
4860
+ packetId: number,
4861
+ catalogTrackName: string | undefined,
4862
+ ): void {
4863
+ if (!this.#activeBlocks.has(key)) return
4864
+ const config = this.#trackConfigs.get(packetId)
4865
+ if (config === undefined) return
4866
+ const waitMs = this.#lateSourceWaitMs(key, sbn, packetId, catalogTrackName, config)
4867
+ if (waitMs === undefined) return
4868
+ const armedAtMs = this.#blockWaitDeadlinesMs.get(key)
4869
+ this.#armBlockTimeout(
4870
+ sbn,
4871
+ packetId,
4872
+ catalogTrackName,
4873
+ armedAtMs === undefined ? waitMs : Math.min(waitMs, armedAtMs - this.#receiptNow()),
4874
+ )
4875
+ }
4876
+
4370
4877
  /**
4371
4878
  * Record a block's authored SSB_length from a repair symbol. Returns false,
4372
4879
  * leaving the block unchanged, when the symbol contradicts the block: a
@@ -4506,7 +5013,8 @@ export class FECManager {
4506
5013
  * the inactivity deadline on each symbol, records the symbol's in-band
4507
5014
  * classification evidence, and marks the block clean when all of its
4508
5015
  * authored source symbols arrived: the SSB_length a repair symbol declared,
4509
- * otherwise K.
5016
+ * otherwise K. `held` marks a source held unplaced since it arrived and
5017
+ * placed only now (see #noteSbnSeen).
4510
5018
  *
4511
5019
  * Throws for a packetId that was never configured, so a classified block
4512
5020
  * never reads the manager-global K or timeout.
@@ -4517,6 +5025,7 @@ export class FECManager {
4517
5025
  catalogTrackName: string | undefined,
4518
5026
  esi: number,
4519
5027
  evidence: SourceSymbolEvidence,
5028
+ held = false,
4520
5029
  ): void {
4521
5030
  const trackConfig = this.#trackConfigs.get(packetId)
4522
5031
  if (trackConfig === undefined) {
@@ -4538,6 +5047,7 @@ export class FECManager {
4538
5047
  'source',
4539
5048
  evidence.packetSequenceNumber,
4540
5049
  Number.isInteger(esi) ? (sbn + esi) >>> 0 : sbn,
5050
+ held,
4541
5051
  )
4542
5052
 
4543
5053
  // A source ESI at or past the block's authored SSB_length contradicts it;
@@ -4582,6 +5092,20 @@ export class FECManager {
4582
5092
  this.#completeCleanBlock(key, sbn, packetId, catalogTrackName)
4583
5093
  return
4584
5094
  }
5095
+ // A late arrival can be the last source a block lacked besides those
5096
+ // another instance rebuilt (feedRebuiltSourceSymbol).
5097
+ if (
5098
+ this.#blockRebuiltEsis.has(key) &&
5099
+ this.#completeWhenSourcesPresent(
5100
+ key,
5101
+ sbn,
5102
+ this.#isoLocatorFor(packetId, catalogTrackName).blockAt(sbn)?.length ?? authoredLength,
5103
+ packetId,
5104
+ catalogTrackName,
5105
+ )
5106
+ ) {
5107
+ return
5108
+ }
4585
5109
 
4586
5110
  this.noteBlockActivity(sbn, packetId, catalogTrackName, true)
4587
5111
 
@@ -4601,7 +5125,9 @@ export class FECManager {
4601
5125
  this.cancelBlockTimeout(sbn, packetId, catalogTrackName)
4602
5126
  this.#blockSourceCounts.delete(key)
4603
5127
  this.#blockSourceEsis.delete(key)
5128
+ this.#blockRebuiltEsis.delete(key)
4604
5129
  this.#blockAuthoredLengths.delete(key)
5130
+ this.#isoSpanKeys.delete(key)
4605
5131
  this.#blockRepairRsIds.delete(key)
4606
5132
  this.#blockRsbLengths.delete(key)
4607
5133
  // BLO-3323 L15: track which SBNs reached K source-symbol arrivals so the
@@ -4643,6 +5169,20 @@ export class FECManager {
4643
5169
  return this.#receiptAtMs ?? performance.now()
4644
5170
  }
4645
5171
 
5172
+ /**
5173
+ * The whole-ms delay of a timer for `deadlineMs` armed inside a receipt
5174
+ * scope: counted from the receipt time, as the timer of a symbol fed when
5175
+ * it was received was, less the whole ms since. So it fires on the tick
5176
+ * that timer would have, never later than a delay counted from now, and
5177
+ * its handler still re-arms a timer that fires before its deadline.
5178
+ * Counted from now, a deadline set from the receipt time is rounded up
5179
+ * from another fraction of a ms (BLO-52085).
5180
+ */
5181
+ #receiptTimerDelayMs(deadlineMs: number, minMs: number): number {
5182
+ const receivedAtMs = this.#receiptAtMs!
5183
+ return Math.max(minMs, Math.ceil(deadlineMs - receivedAtMs) - Math.ceil(performance.now() - receivedAtMs))
5184
+ }
5185
+
4646
5186
  /**
4647
5187
  * A held packet admitted late arms from its (earlier) receipt time; that
4648
5188
  * never pulls in a deadline later activity already armed. Outside a
@@ -4714,7 +5254,18 @@ export class FECManager {
4714
5254
  )
4715
5255
  }
4716
5256
 
4717
- this.#armBlockTimeout(sbn, packetId, catalogTrackName, timeoutMs)
5257
+ // A block only a late source can complete waits the late-symbol
5258
+ // allowance from its own sources' activity; nothing else moves it later.
5259
+ if (!sourceProgress && this.#lateSourceWaitMs(key, sbn, packetId, catalogTrackName, trackConfig) !== undefined) {
5260
+ this.#shortenUnrecoverableWait(key, sbn, packetId, catalogTrackName)
5261
+ return
5262
+ }
5263
+ this.#armBlockTimeout(
5264
+ sbn,
5265
+ packetId,
5266
+ catalogTrackName,
5267
+ this.#lateSourceWaitMs(key, sbn, packetId, catalogTrackName, trackConfig) ?? timeoutMs,
5268
+ )
4718
5269
  }
4719
5270
 
4720
5271
  #failBlock(
@@ -4723,6 +5274,9 @@ export class FECManager {
4723
5274
  catalogTrackName?: string,
4724
5275
  ): void {
4725
5276
  const key = this.#blockKey(sbn, packetId, catalogTrackName)
5277
+ const span: IsoSpanFailure | undefined = this.#isoSpanKeys.delete(key)
5278
+ ? { length: this.#blockAuthoredLengths.get(key) ?? 0, sources: this.#blockSourceCounts.get(key) ?? 0 }
5279
+ : undefined
4726
5280
  this.#traceLifecycle('failed', sbn, packetId, catalogTrackName)
4727
5281
  this.#clearRepairDeadline(key)
4728
5282
  this.#clearBlockTimer(key)
@@ -4732,6 +5286,7 @@ export class FECManager {
4732
5286
  }
4733
5287
  this.#blockSourceCounts.delete(key)
4734
5288
  this.#blockSourceEsis.delete(key)
5289
+ this.#blockRebuiltEsis.delete(key)
4735
5290
  this.#blockAuthoredLengths.delete(key)
4736
5291
  this.#blockRepairRsIds.delete(key)
4737
5292
  this.#blockRsbLengths.delete(key)
@@ -4741,7 +5296,8 @@ export class FECManager {
4741
5296
  this.#markPathBlockComplete(sbn, packetId, catalogTrackName)
4742
5297
  this.#fecStats.blocksFailed++
4743
5298
  this.#statsForPacket(packetId).blocksFailed++
4744
- this.onBlockFailed?.(sbn, packetId, catalogTrackName, blockClass)
5299
+ if (span === undefined) this.onBlockFailed?.(sbn, packetId, catalogTrackName, blockClass)
5300
+ else this.onBlockFailed?.(sbn, packetId, catalogTrackName, blockClass, span)
4745
5301
  this.onBlockResolved?.(sbn, packetId, catalogTrackName)
4746
5302
  }
4747
5303
 
@@ -4771,7 +5327,9 @@ export class FECManager {
4771
5327
  this.#retireBlockClass(key, block.sbn, block.packetId, block.catalogTrackName)
4772
5328
  this.#blockSourceCounts.delete(key)
4773
5329
  this.#blockSourceEsis.delete(key)
5330
+ this.#blockRebuiltEsis.delete(key)
4774
5331
  this.#blockAuthoredLengths.delete(key)
5332
+ this.#isoSpanKeys.delete(key)
4775
5333
  this.#blockRepairRsIds.delete(key)
4776
5334
  this.#blockRsbLengths.delete(key)
4777
5335
  this.#clearBlockTimer(key)
@@ -4879,7 +5437,9 @@ export class FECManager {
4879
5437
  this.#blockKeysBySbn.clear()
4880
5438
  this.#blockSourceCounts.clear()
4881
5439
  this.#blockSourceEsis.clear()
5440
+ this.#blockRebuiltEsis.clear()
4882
5441
  this.#blockAuthoredLengths.clear()
5442
+ this.#isoSpanKeys.clear()
4883
5443
  this.#blockRepairRsIds.clear()
4884
5444
  this.#blockRsbLengths.clear()
4885
5445
  this.#heldLayerStates.clear()