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

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 (67) hide show
  1. package/dist/browser-runtime.js +1447 -179
  2. package/dist/browser-runtime.js.map +3 -3
  3. package/dist/cmaf-block-assembler.js +92 -1
  4. package/dist/cmaf-block-assembler.js.map +2 -2
  5. package/dist/codec-utils.d.ts.map +1 -1
  6. package/dist/codec-utils.js +92 -1
  7. package/dist/codec-utils.js.map +2 -2
  8. package/dist/delivery-catalog.js +2 -1
  9. package/dist/delivery-catalog.js.map +2 -2
  10. package/dist/delta-fallback-config.js.map +1 -1
  11. package/dist/fec-manager.d.ts +105 -14
  12. package/dist/fec-manager.d.ts.map +1 -1
  13. package/dist/fec-manager.js +502 -58
  14. package/dist/fec-manager.js.map +2 -2
  15. package/dist/fec-worker-setup.js.map +1 -1
  16. package/dist/index.d.ts +2 -2
  17. package/dist/index.d.ts.map +1 -1
  18. package/dist/index.js.map +2 -2
  19. package/dist/iso-block-locator.d.ts +48 -13
  20. package/dist/iso-block-locator.d.ts.map +1 -1
  21. package/dist/iso-block-locator.js +97 -25
  22. package/dist/iso-block-locator.js.map +2 -2
  23. package/dist/iso-bmff-timing.d.ts +10 -1
  24. package/dist/iso-bmff-timing.d.ts.map +1 -1
  25. package/dist/iso-bmff-timing.js +36 -2
  26. package/dist/iso-bmff-timing.js.map +2 -2
  27. package/dist/media-router.js.map +1 -1
  28. package/dist/mmtp-container-parser.d.ts +104 -12
  29. package/dist/mmtp-container-parser.d.ts.map +1 -1
  30. package/dist/mmtp-container-parser.js +308 -27
  31. package/dist/mmtp-container-parser.js.map +2 -2
  32. package/dist/mmtp-container-parser.no-modules.js +310 -30
  33. package/dist/mmtp-container-parser.no-modules.js.map +3 -3
  34. package/dist/mmtp-decode-pipeline.js +307 -27
  35. package/dist/mmtp-decode-pipeline.js.map +2 -2
  36. package/dist/mmtp-router.d.ts +36 -8
  37. package/dist/mmtp-router.d.ts.map +1 -1
  38. package/dist/mmtp-router.js +675 -75
  39. package/dist/mmtp-router.js.map +3 -3
  40. package/dist/multicast-manager.d.ts +26 -3
  41. package/dist/multicast-manager.d.ts.map +1 -1
  42. package/dist/multicast-manager.js +1267 -152
  43. package/dist/multicast-manager.js.map +3 -3
  44. package/dist/multicast-transport.js +4 -3
  45. package/dist/multicast-transport.js.map +2 -2
  46. package/dist/no-modules.js +307 -27
  47. package/dist/no-modules.js.map +3 -3
  48. package/dist/repair-fetch.d.ts +38 -8
  49. package/dist/repair-fetch.d.ts.map +1 -1
  50. package/dist/repair-fetch.js +286 -27
  51. package/dist/repair-fetch.js.map +3 -3
  52. package/dist/track-session.d.ts.map +1 -1
  53. package/dist/track-session.js +1 -0
  54. package/dist/track-session.js.map +2 -2
  55. package/dist/wasm-mmtp-container-parser.js +307 -27
  56. package/dist/wasm-mmtp-container-parser.js.map +2 -2
  57. package/package.json +5 -5
  58. package/src/codec-utils.ts +121 -1
  59. package/src/fec-manager.ts +571 -32
  60. package/src/index.ts +3 -0
  61. package/src/iso-block-locator.ts +133 -31
  62. package/src/iso-bmff-timing.ts +40 -1
  63. package/src/mmtp-container-parser.ts +341 -32
  64. package/src/mmtp-router.ts +600 -55
  65. package/src/multicast-manager.ts +136 -16
  66. package/src/repair-fetch.ts +366 -50
  67. 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,
@@ -169,6 +171,17 @@ export interface FecPacketStats {
169
171
 
170
172
  export type FecSymbolSource = 'multicast' | 'moq'
171
173
 
174
+ /**
175
+ * What feedRebuiltSourceSymbol did with a rebuilt source: whether its block
176
+ * took it, and the block the synchronous decoder recovered with it, if any.
177
+ */
178
+ export interface RebuiltSourceFeed {
179
+ readonly taken: boolean
180
+ readonly recovered: Uint8Array | null
181
+ }
182
+
183
+ const NOT_TAKEN: RebuiltSourceFeed = Object.freeze({ taken: false, recovered: null })
184
+
172
185
  /**
173
186
  * One iso-ssbg1-v1 first-arrival source symbol MmtpRouter holds until a repair
174
187
  * symbol states its block: the exact protected packet (no SS_ID, unpadded),
@@ -201,6 +214,25 @@ export interface UndelimitedIsoLoss {
201
214
  readonly previousBlockStart?: number
202
215
  }
203
216
 
217
+ /**
218
+ * A pending hole the stream ran more than the locator's horizon past before
219
+ * its range repair ended (BLO-52085): no repair can state its blocks any
220
+ * more, and the release gate no longer waits for it.
221
+ */
222
+ export type ExpiredIsoHole = Pick<UndelimitedIsoLoss, 'packetId' | 'catalogTrackName' | 'holeStart' | 'holeLength'>
223
+
224
+ /**
225
+ * What a failed iso-ssbg1-v1 repair-less span was: the `length` SS_IDs from
226
+ * its start, between two stated blocks, of which `sources` arrived. No repair
227
+ * stated the blocks inside it, so the receiver cannot tell its complete
228
+ * blocks from the ones that lost a source: the span failed, not the block
229
+ * that starts it.
230
+ */
231
+ export interface IsoSpanFailure {
232
+ readonly length: number
233
+ readonly sources: number
234
+ }
235
+
204
236
  /**
205
237
  * How the multicast leg hands its symbols to this manager.
206
238
  *
@@ -510,10 +542,21 @@ export class FECManager {
510
542
  // 'pending' forever, which would park every later frame of its stream in
511
543
  // the in-order release gate. Each entry owns its handle; the callback
512
544
  // checks it still owns the key, so a settled or reset key is never touched.
513
- #pendingWorkerCleanupBounds = new Map<string, { handle: ReturnType<typeof setTimeout> }>()
545
+ /**
546
+ * Per owed Worker cleanup: the bound on its acknowledgement, when its block
547
+ * was due, and when its cleanup was asked for (see `onCleanupComplete`).
548
+ */
549
+ #pendingWorkerCleanupBounds = new Map<
550
+ string,
551
+ { handle: ReturnType<typeof setTimeout>; dueAtMs: number; askedAtMs: number }
552
+ >()
514
553
  // Source symbol count per track-scoped block — marks complete when K symbols arrive.
515
554
  #blockSourceCounts = new Map<string, number>()
516
555
  #blockSourceEsis = new Map<string, Set<number>>()
556
+ // Source ESIs per block that another FEC instance rebuilt and fed in
557
+ // (feedRebuiltSourceSymbol). Kept apart from #blockSourceEsis: they are
558
+ // not network arrivals and never make a block clean.
559
+ #blockRebuiltEsis = new Map<string, Set<number>>()
517
560
  // Authored SSB_length per block (ISO/IEC 23008-1 §C.5.3), learned from the
518
561
  // first repair symbol. The FFmpeg moq_mmt publisher closes a block every D
519
562
  // media units, usually before it holds K symbols; under iso-ssbg1-v1
@@ -522,10 +565,14 @@ export class FECManager {
522
565
  // SSB_length source arrivals and recoverable from SSB_length received
523
566
  // symbols; waiting for K fails every one of them.
524
567
  #blockAuthoredLengths = new Map<string, number>()
568
+ // Keys whose authored length is a repair-less span's (#openIsoSpan), until
569
+ // a repair states the block there or the span resolves.
570
+ #isoSpanKeys = new Set<string>()
525
571
  #ssbLengthConflictCount = 0
526
572
  #blockRepairRsIds = new Map<string, Set<number>>()
527
573
  // RSB_length per block (C.5.3 Repair FEC Payload ID): how many repair
528
- // symbols its repair group carries. Same lifecycle as #blockRepairRsIds.
574
+ // symbols its repair group carries; 0 for a repair-less span no repair can
575
+ // state (#openIsoSpan). Same lifecycle as #blockRepairRsIds.
529
576
  #blockRsbLengths = new Map<string, number>()
530
577
  /** fec-01 6.3.9 anchor state per stream key, one entry per held layer. */
531
578
  #heldLayerStates = new Map<string, HeldLayerAdmission>()
@@ -729,13 +776,17 @@ export class FECManager {
729
776
  onBlockComplete:
730
777
  | ((sbn: number, packetId: number, catalogTrackName?: string) => void)
731
778
  | null = null
732
- /** A block reached its fail bound. `blockClass` is its class at failure. */
779
+ /**
780
+ * A block reached its fail bound. `blockClass` is its class at failure.
781
+ * `span` is set when what failed is a repair-less span starting at `sbn`.
782
+ */
733
783
  onBlockFailed:
734
784
  | ((
735
785
  sbn: number,
736
786
  packetId: number,
737
787
  catalogTrackName: string | undefined,
738
788
  blockClass: BlockClass,
789
+ span?: IsoSpanFailure,
739
790
  ) => void)
740
791
  | null = null
741
792
  /**
@@ -819,6 +870,8 @@ export class FECManager {
819
870
  * for it, which it must be.
820
871
  */
821
872
  #onUndelimitedLoss: ((loss: UndelimitedIsoLoss) => void) | null = null
873
+ /** Set with the loss handler: true takes an expired hole to close (`closeIsoHoles`). */
874
+ #onIsoHoleExpired: ((hole: ExpiredIsoHole) => boolean) | null = null
822
875
  /** A block every symbol of which vanished was created by SBN evidence. */
823
876
  onBlockVanished:
824
877
  | ((sbn: number, packetId: number, catalogTrackName: string | undefined) => void)
@@ -831,6 +884,21 @@ export class FECManager {
831
884
  onSourceDiscontinuity:
832
885
  | ((packetId: number, catalogTrackName: string | undefined) => void)
833
886
  | null = null
887
+ /**
888
+ * The span and tolerance the catalog states for a keyframe overlay
889
+ * installed over a source track (draft-ramadan-moq-mmt-02 7.4.1: the
890
+ * overlay instance's own interleaveDepthMs and reorderToleranceMs), or
891
+ * undefined while none is installed. A stated block only a late source
892
+ * can complete, and a frame whose gap may hide a RAP behind a span only a
893
+ * late source can fill, also wait for that overlay's repair
894
+ * (#overlayLateWaitMs).
895
+ */
896
+ keyframeOverlayTiming:
897
+ | ((
898
+ packetId: number,
899
+ catalogTrackName: string | undefined,
900
+ ) => Pick<FecInstanceGeometry, 'interleaveDepthMs' | 'reorderToleranceMs'> | undefined)
901
+ | null = null
834
902
 
835
903
  // Worker-path callbacks — async recovery and stats from FecWorkerClient.
836
904
  /**
@@ -1473,7 +1541,12 @@ export class FECManager {
1473
1541
  timer.handle = null
1474
1542
  this.#requestRepair(sbn, packetId, catalogTrackName)
1475
1543
  }
1476
- timer.handle = setTimeout(onFire, Math.max(0, deadlineMs - performance.now()))
1544
+ timer.handle = setTimeout(
1545
+ onFire,
1546
+ this.#receiptAtMs === undefined
1547
+ ? Math.max(0, deadlineMs - performance.now())
1548
+ : this.#receiptTimerDelayMs(deadlineMs, 0),
1549
+ )
1477
1550
  this.#repairDeadlines.set(key, timer)
1478
1551
  }
1479
1552
 
@@ -1667,6 +1740,12 @@ export class FECManager {
1667
1740
  * Vanished blocks are not enumerated here: their starts are unknown until a
1668
1741
  * repair states them. The SS_IDs between two stated blocks become one
1669
1742
  * repair-less span when the later block's repair arrives (placeIsoRepair).
1743
+ *
1744
+ * A `held` source arrived before its block was known and is fed only now
1745
+ * that a repair, a span or a closed hole placed it: how far its block start
1746
+ * then lies behind the frontier measures how long the receiver held it, not
1747
+ * where the stream was, so only its SS_ID and packet_sequence_number judge
1748
+ * it (BLO-52085).
1670
1749
  */
1671
1750
  #noteSbnSeen(
1672
1751
  sbn: number,
@@ -1675,6 +1754,7 @@ export class FECManager {
1675
1754
  symbol: 'source' | 'repair',
1676
1755
  packetSequenceNumber?: number,
1677
1756
  ssId = sbn,
1757
+ held = false,
1678
1758
  ): void {
1679
1759
  const streamKey = this.#streamKey(packetId, catalogTrackName)
1680
1760
  // Arrival at FEC, not receipt (withReceiptTime): this bounds how far SS_ID
@@ -1731,7 +1811,7 @@ export class FECManager {
1731
1811
  }
1732
1812
  if (!this.#blockPrecedes(previous.sbn, sbn)) {
1733
1813
  if (symbol === 'source' && window !== null && this.#blockPrecedes(sbn, previous.sbn) && (
1734
- this.#blocksBehind(sbn, previous.sbn, packetId) > this.#isoRetentionBlocks(packetId) ||
1814
+ (!held && this.#blocksBehind(sbn, previous.sbn, packetId) > this.#isoRetentionBlocks(packetId)) ||
1735
1815
  (psn !== undefined && anchor !== undefined && serial32Precedes(ssId, anchor.ssId) &&
1736
1816
  psn !== anchor.psn && !sentInOrder({ ssId, psn }, anchor))
1737
1817
  )) {
@@ -2085,21 +2165,28 @@ export class FECManager {
2085
2165
  this.#blockWaitDeadlinesMs.delete(key)
2086
2166
  this.#repairHoldUntilMs.delete(key)
2087
2167
  this.#traceLifecycle('timeout-queued', sbn, packetId, catalogTrackName)
2088
- this.#finalizeExpiredBlock(sbn, packetId, catalogTrackName)
2168
+ this.#finalizeExpiredBlock(sbn, packetId, catalogTrackName, currentDeadlineMs ?? firedAtMs)
2089
2169
  }
2090
- timer.handle = setTimeout(onFire, Math.max(1, Math.ceil(deadlineMs - nowMs)))
2170
+ timer.handle = setTimeout(
2171
+ onFire,
2172
+ this.#receiptAtMs === undefined
2173
+ ? Math.max(1, Math.ceil(deadlineMs - nowMs))
2174
+ : this.#receiptTimerDelayMs(deadlineMs, 1),
2175
+ )
2091
2176
  this.#activeBlocks.set(key, timer)
2092
2177
  this.#indexBlockKey(key, sbn, packetId)
2093
2178
  }
2094
2179
 
2095
2180
  /**
2096
2181
  * Hand a block whose delivery is over to its decoder for finalization: the
2097
- * datagram deadline fired, or the ordered relay moved past it.
2182
+ * datagram deadline fired, or the ordered relay moved past it. `dueAtMs`
2183
+ * is when it was due: its deadline, or now.
2098
2184
  */
2099
2185
  #finalizeExpiredBlock(
2100
2186
  sbn: number,
2101
2187
  packetId: number,
2102
2188
  catalogTrackName: string | undefined,
2189
+ dueAtMs = performance.now(),
2103
2190
  ): void {
2104
2191
  const key = this.#blockKey(sbn, packetId, catalogTrackName)
2105
2192
  // A vanished block that never received a symbol holds no decoder state
@@ -2123,7 +2210,7 @@ export class FECManager {
2123
2210
  if (workerClient) {
2124
2211
  this.#pendingWorkerCleanups.add(key)
2125
2212
  this.#pendingWorkerCleanupClients.set(key, workerClient)
2126
- this.#boundOwedCleanup(key, packetId, workerClient)
2213
+ this.#boundOwedCleanup(key, packetId, workerClient, dueAtMs)
2127
2214
  workerClient.cleanup(sbn)
2128
2215
  } else {
2129
2216
  const decoder = this.#decoderForPacket(packetId, catalogTrackName)?.client
@@ -2540,7 +2627,9 @@ export class FECManager {
2540
2627
  }
2541
2628
  resetMap(this.#blockSourceCounts)
2542
2629
  resetMap(this.#blockSourceEsis)
2630
+ resetMap(this.#blockRebuiltEsis)
2543
2631
  resetMap(this.#blockAuthoredLengths)
2632
+ resetSet(this.#isoSpanKeys)
2544
2633
  resetMap(this.#blockRepairRsIds)
2545
2634
  resetMap(this.#blockRsbLengths)
2546
2635
  resetMap(this.#heldLayerStates)
@@ -2672,7 +2761,9 @@ export class FECManager {
2672
2761
  dropMap(this.#pendingWorkerCleanupClients)
2673
2762
  dropMap(this.#blockSourceCounts)
2674
2763
  dropMap(this.#blockSourceEsis)
2764
+ dropMap(this.#blockRebuiltEsis)
2675
2765
  dropMap(this.#blockAuthoredLengths)
2766
+ dropSet(this.#isoSpanKeys)
2676
2767
  dropMap(this.#blockRepairRsIds)
2677
2768
  dropMap(this.#blockRsbLengths)
2678
2769
  dropMap(this.#blockPathCounts)
@@ -2891,6 +2982,10 @@ export class FECManager {
2891
2982
  }
2892
2983
  }
2893
2984
 
2985
+ if (isSourceSymbol && source === 'multicast') {
2986
+ this.#noteMulticastSource(this.#isoLocatorFor(packetId, catalogTrackName), (sbn + esi) >>> 0, packetId, catalogTrackName)
2987
+ }
2988
+
2894
2989
  return true
2895
2990
  }
2896
2991
 
@@ -3016,8 +3111,10 @@ export class FECManager {
3016
3111
  }
3017
3112
  /**
3018
3113
  * 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.
3114
+ * (SSB_length) and RSB_length (each undefined until a repair stated it; a
3115
+ * repair-less span states its length, and RSB_length 0 when it is longer
3116
+ * than K, since no repair can reach it). Undefined for a block that is not
3117
+ * open.
3021
3118
  */
3022
3119
  blockSymbolCounts(
3023
3120
  sbn: number,
@@ -3100,8 +3197,9 @@ export class FECManager {
3100
3197
  return true
3101
3198
  }
3102
3199
  /**
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.
3200
+ * Drop a block's repair hold: its FETCH ended without delivering, or brought
3201
+ * back only a repair that cannot complete it. The block falls back to its
3202
+ * multicast wait, which may already have passed.
3105
3203
  */
3106
3204
  releaseRepairHold(sbn: number, packetId: number, catalogTrackName?: string): void {
3107
3205
  const key = this.#blockKey(sbn, packetId, catalogTrackName)
@@ -3151,7 +3249,7 @@ export class FECManager {
3151
3249
  }
3152
3250
  const trackConfig = this.#trackConfigs.get(packetId)
3153
3251
  if (trackConfig === undefined) return undefined
3154
- return Math.max(1, trackConfig.timeoutMs - this.#repairDeadlineMs(trackConfig))
3252
+ return Math.max(1, this.#repairGraceMs(trackConfig, this.#multicastWaitMs(trackConfig)))
3155
3253
  }
3156
3254
 
3157
3255
  /** True once configureTrack/setFecDecoder/setFecWorker configured this packetId. */
@@ -3263,6 +3361,8 @@ export class FECManager {
3263
3361
  const locator = this.#growingIsoLocatorFor(packetId, catalogTrackName)
3264
3362
  const evictedBefore = locator.heldEvicted
3265
3363
  const held = locator.hold(ssId, source)
3364
+ this.#settleExpiredIsoHoles(locator, packetId, catalogTrackName)
3365
+ if (held && source.source === 'multicast') this.#noteMulticastSource(locator, ssId, packetId, catalogTrackName)
3266
3366
  const evicted = locator.heldEvicted - evictedBefore
3267
3367
  if (evicted > 0) {
3268
3368
  const before = this.#isoHeldEvictedCount
@@ -3290,17 +3390,26 @@ export class FECManager {
3290
3390
  ssStart: number,
3291
3391
  ssbLength: number,
3292
3392
  ): IsoRepairPlacement<IsoHeldSource> {
3293
- const placement = this.#growingIsoLocatorFor(packetId, catalogTrackName).placeRepair(ssStart, ssbLength)
3393
+ const locator = this.#growingIsoLocatorFor(packetId, catalogTrackName)
3394
+ const placement = locator.placeRepair(ssStart, ssbLength)
3395
+ this.#settleExpiredIsoHoles(locator, packetId, catalogTrackName)
3294
3396
  if (placement.status !== 'placed') {
3295
3397
  this.#isoRepairRefusedCount++
3296
3398
  if (this.#isoRepairRefusedCount <= 5 || this.#isoRepairRefusedCount % 100 === 0) {
3297
3399
  console.warn(`[FECManager] packetId=${packetId} iso-ssbg1-v1 repair refused (${placement.status}): ${placement.reason}`)
3298
3400
  }
3401
+ // The repair FETCH of a span's first group brought back a shorter block:
3402
+ // the span holds more than one, so that group cannot complete it, and
3403
+ // holding the span (and every frame behind it) for the FETCH buys
3404
+ // nothing. It falls back to its multicast wait.
3405
+ if (placement.ofSpan !== undefined) this.releaseRepairHold(placement.ofSpan.start, packetId, catalogTrackName)
3299
3406
  return placement
3300
3407
  }
3408
+ // A repair states the block at its SS_Start: a span the locator kept
3409
+ // there is now that one block (IsoBlockLocator.placeRepair).
3410
+ this.#isoSpanKeys.delete(this.#blockKey(placement.block.start, packetId, catalogTrackName))
3301
3411
  if (placement.span !== undefined) this.#openIsoSpan(placement.span, packetId, catalogTrackName)
3302
3412
  if (placement.hole !== undefined) {
3303
- const locator = this.#isoLocatorFor(packetId, catalogTrackName)
3304
3413
  const hole = placement.hole.block
3305
3414
  const previous = locator.blockEndingAt(hole.start)
3306
3415
  const loss: UndelimitedIsoLoss = {
@@ -3322,18 +3431,38 @@ export class FECManager {
3322
3431
  * `handler`, instead of a repair-less span; null restores spans. Set by the
3323
3432
  * range repair scheduler; applies to every stream from its next gap. The
3324
3433
  * handler and the deferral are one setting, so no hole is ever created with
3325
- * nobody to close it.
3434
+ * nobody to close it. `onExpired` is told of each pending hole the stream
3435
+ * runs past the locator's horizon (BLO-52085): returning true, it closes
3436
+ * the hole (`closeIsoHoles`), so the span it leaves still fails and is
3437
+ * reported; otherwise the hole is discarded with its sources.
3326
3438
  */
3327
- setUndelimitedLossHandler(handler: ((loss: UndelimitedIsoLoss) => void) | null): void {
3439
+ setUndelimitedLossHandler(
3440
+ handler: ((loss: UndelimitedIsoLoss) => void) | null,
3441
+ onExpired: ((hole: ExpiredIsoHole) => boolean) | null = null,
3442
+ ): void {
3328
3443
  this.#onUndelimitedLoss = handler
3444
+ this.#onIsoHoleExpired = handler === null ? null : onExpired
3329
3445
  this.#deferIsoGaps = handler !== null
3330
3446
  for (const locator of this.#isoLocators.values()) locator.deferIncompleteGaps = this.#deferIsoGaps
3331
3447
  }
3332
3448
 
3449
+ /** Hand each hole the locator's horizon passed to its owner, or discard it (setUndelimitedLossHandler). */
3450
+ #settleExpiredIsoHoles(
3451
+ locator: IsoBlockLocator<IsoHeldSource>,
3452
+ packetId: number,
3453
+ catalogTrackName: string | undefined,
3454
+ ): void {
3455
+ for (const hole of locator.takeExpiredHoles()) {
3456
+ const expired: ExpiredIsoHole = { packetId, catalogTrackName, holeStart: hole.start, holeLength: hole.length }
3457
+ if (this.#onIsoHoleExpired?.(expired) !== true) locator.closeHolesWithin(hole.start, hole.length)
3458
+ }
3459
+ }
3460
+
3333
3461
  /**
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).
3462
+ * The range repair for a hole ended: every hole inside `[start, start +
3463
+ * length)`, pending or expired, becomes a repair-less span. Returns those
3464
+ * spans with the held sources they placed, for the caller to feed
3465
+ * (MmtpRouter).
3337
3466
  */
3338
3467
  closeIsoHoles(
3339
3468
  packetId: number,
@@ -3355,6 +3484,17 @@ export class FECManager {
3355
3484
  * its placed sources exactly as on the wire (feedRepairSymbol). A span every
3356
3485
  * SS_ID of which vanished resolves through its deadline like any vanished
3357
3486
  * block.
3487
+ *
3488
+ * A span longer than K holds more than one block (SSB_length <= K), and the
3489
+ * locator states no block inside a span, only one exactly its range
3490
+ * (IsoBlockLocator.placeRepair): no repair, multicast or FETCHed, can ever
3491
+ * reach it. Its repair group is empty, so the repair scheduler FETCHes no
3492
+ * Group of it (group-cannot-complete). Every span still keeps its whole
3493
+ * wait (#lateSourceWaitMs): failing it early released the frames behind it
3494
+ * past a keyframe the overlay was still rebuilding (BLO-52085). The release
3495
+ * gate lets those frames go once only a late source can fill the gap
3496
+ * before them, as long after they arrived as the gap's decision allows
3497
+ * (isoRepairlessWaitMs).
3358
3498
  */
3359
3499
  #openIsoSpan(
3360
3500
  span: { readonly block: IsoBlockRange; readonly placed: readonly unknown[] },
@@ -3362,7 +3502,16 @@ export class FECManager {
3362
3502
  catalogTrackName: string | undefined,
3363
3503
  ): void {
3364
3504
  const key = this.#blockKey(span.block.start, packetId, catalogTrackName)
3365
- if (!this.#blockAuthoredLengths.has(key)) this.#blockAuthoredLengths.set(key, span.block.length)
3505
+ // A span's key is always new here. An authored length is set only here or
3506
+ // from a repair stating that block (#noteAuthoredLength), and the locator
3507
+ // opens a span only where no repair stated a block, so none can precede
3508
+ // it. Were one to, the guard would skip the RSB_length 0 that keeps the
3509
+ // repair scheduler from FETCHing a long span's first Group.
3510
+ if (!this.#blockAuthoredLengths.has(key)) {
3511
+ this.#blockAuthoredLengths.set(key, span.block.length)
3512
+ this.#isoSpanKeys.add(key)
3513
+ if (span.block.length > this.#isoLocatorFor(packetId, catalogTrackName).kMax) this.#blockRsbLengths.set(key, 0)
3514
+ }
3366
3515
  if (span.placed.length === 0) this.#materializeBlock(span.block.start, packetId, catalogTrackName, true)
3367
3516
  }
3368
3517
 
@@ -3410,6 +3559,80 @@ export class FECManager {
3410
3559
  return lost
3411
3560
  }
3412
3561
 
3562
+ /**
3563
+ * The release gate's wait, from a frame's arrival, for the SS_IDs strictly
3564
+ * between two released frames' SS_IDs once only a late source can still
3565
+ * fill them, else undefined. That is when every one missing (neither
3566
+ * arrived nor held) lies in a repair-less span longer than K, which no
3567
+ * repair can state a block of (#openIsoSpan), or in a stated block its
3568
+ * own repair group cannot complete every SS_ID of which it misses lies
3569
+ * between them (so the gap's decision covers all it lost), and a multicast
3570
+ * source at or past `toSsId` arrived, so each is lost or reordered behind
3571
+ * it (a MoQ source shows nothing: MoQ streams overtake one another). The
3572
+ * span or block itself keeps its own wait.
3573
+ *
3574
+ * The wait is the late-symbol allowance, unless `rapMayHide`: the gap may
3575
+ * hold a RAP packet an installed keyframe overlay can still rebuild (its
3576
+ * decision stalls, or it ends at a RAP that may lie past a whole MPU).
3577
+ * Then the frame waits as a stated block its group cannot complete does
3578
+ * (#overlayLateWaitMs): the allowance alone with no overlay installed, the
3579
+ * overlay's span plus the larger of the allowance and its tolerance, or,
3580
+ * for an overlay that states no span, the span's whole wait (undefined).
3581
+ * So does `rapNext` (the gap ends at the RAP that opens the next MPU) when
3582
+ * the gap holds a stated block's loss.
3583
+ */
3584
+ isoRepairlessWaitMs(
3585
+ packetId: number,
3586
+ catalogTrackName: string | undefined,
3587
+ fromSsId: number,
3588
+ toSsId: number,
3589
+ rapMayHide = false,
3590
+ rapNext = false,
3591
+ ): number | undefined {
3592
+ const config = this.#trackConfigs.get(packetId)
3593
+ if (config === undefined || !FECManager.#hasCatalogTiming(config)) return undefined
3594
+ const locator = this.#isoLocators.get(this.#streamKey(packetId, catalogTrackName))
3595
+ if (locator === undefined || !locator.coversBetween(fromSsId, toSsId)) return undefined
3596
+ const newestSource = locator.newestSource
3597
+ if (newestSource === undefined || serial32Precedes(newestSource, toSsId)) return undefined
3598
+ let repairless = false
3599
+ let stated = false
3600
+ for (const block of locator.blocksBetween(fromSsId, toSsId)) {
3601
+ const key = this.#blockKey(block.start, packetId, catalogTrackName)
3602
+ if (this.#completedBlocks.has(key)) continue
3603
+ if (block.span && block.length > locator.kMax) {
3604
+ repairless = true
3605
+ continue
3606
+ }
3607
+ const received = this.#blockSourceEsis.get(key)
3608
+ const rebuilt = this.#blockRebuiltEsis.get(key)
3609
+ let missesInGap = false
3610
+ let missesOutside = false
3611
+ for (let esi = 0; esi < block.length; esi++) {
3612
+ const ssId = serial32Add(block.start, esi)
3613
+ if (received?.has(esi) || locator.isHeld(ssId)) continue
3614
+ if (serial32Precedes(fromSsId, ssId) && serial32Precedes(ssId, toSsId)) missesInGap = true
3615
+ else if (!rebuilt?.has(esi)) missesOutside = true
3616
+ }
3617
+ if (!missesInGap) continue
3618
+ // A stated block its own group cannot complete (#lateSourceWaitMs) is
3619
+ // one too, when every SS_ID it misses lies in this gap: the gap's
3620
+ // decision then covers all it lost, so the overlay can rebuild none of
3621
+ // it unless `rapMayHide` or `rapNext`.
3622
+ if (missesOutside || block.span || !this.#groupCannotComplete(key)) return undefined
3623
+ repairless = stated = true
3624
+ }
3625
+ if (!repairless) return undefined
3626
+ // A gap into the next MPU (`rapNext`) may hold that MPU's FT=0, an overlay
3627
+ // member; rebuilt, it is one more source of a stated block, which its group
3628
+ // may then complete. Such a block keeps #741's overlay wait; a span longer
3629
+ // than K has no group to complete and keeps #742's allowance.
3630
+ const waitMs = rapMayHide || (stated && rapNext)
3631
+ ? this.#overlayLateWaitMs(packetId, catalogTrackName, config)
3632
+ : this.#multicastWaitMs(config) - config.interleaveDepthMs
3633
+ return waitMs === undefined ? undefined : Math.max(0, waitMs)
3634
+ }
3635
+
3413
3636
  /**
3414
3637
  * iso-ssbg1-v1 block location: repair symbols it refused, oversize sources,
3415
3638
  * the sources held now for a block no repair has stated yet, and the held
@@ -3841,13 +4064,27 @@ export class FECManager {
3841
4064
 
3842
4065
  client.onCleanupComplete((sbn: number) => {
3843
4066
  if (this.#releasedWorkerClients.has(client)) return
3844
- const cleanupKeys = [...this.#pendingWorkerCleanups].filter((key) => {
4067
+ const owed = [...this.#pendingWorkerCleanups].filter((key) => this.#pendingWorkerCleanupClients.get(key) === client)
4068
+ const cleanupKeys = owed.filter((key) => {
3845
4069
  const block = this.#parseBlockKey(key)
3846
- return block.packetId === packetId &&
3847
- block.sbn === sbn &&
3848
- this.#pendingWorkerCleanupClients.get(key) === client
4070
+ return block.packetId === packetId && block.sbn === sbn
3849
4071
  })
3850
- for (const key of cleanupKeys) this.#settleOwedCleanup(key, 'cleanup-ack')
4072
+ // A cleanup is a queue-drain barrier: the Worker took every symbol fed
4073
+ // before it was asked for first, so every block already due by then
4074
+ // is settled by this acknowledgement too, not one barrier later. The
4075
+ // client sends one barrier at a time, and the blocks a repair walk
4076
+ // stated in one span are due together (BLO-52085). That holds only
4077
+ // while no block is fed a symbol after its dueAtMs, which is its
4078
+ // receipt-scoped deadline, not when feeding stopped: a symbol fed
4079
+ // between that deadline and the block's timer, after another block's
4080
+ // barrier was asked for, would find the block settled (failed) one
4081
+ // barrier early. Walks end before D-28 (walkDeadlineAt), so that window
4082
+ // is the timer's lateness alone; whatever feeds a block past its dueAtMs
4083
+ // must leave it to its own barrier.
4084
+ const askedAtMs = Math.max(...cleanupKeys.map((key) => this.#pendingWorkerCleanupBounds.get(key)?.askedAtMs ?? -Infinity))
4085
+ const due = owed.filter((key) =>
4086
+ !cleanupKeys.includes(key) && (this.#pendingWorkerCleanupBounds.get(key)?.dueAtMs ?? Infinity) <= askedAtMs)
4087
+ for (const key of [...cleanupKeys, ...due]) this.#settleOwedCleanup(key, 'cleanup-ack')
3851
4088
  })
3852
4089
 
3853
4090
  client.onError((msg: string, code?: FecWorkerErrorCode) => {
@@ -3929,9 +4166,11 @@ export class FECManager {
3929
4166
  * fail deadline already passed, so failing it is the decision that deadline
3930
4167
  * made. A late ack then finds nothing owed and is a no-op.
3931
4168
  */
3932
- #boundOwedCleanup(key: string, packetId: number, client: FecWorkerClient): void {
4169
+ #boundOwedCleanup(key: string, packetId: number, client: FecWorkerClient, dueAtMs: number): void {
3933
4170
  this.#clearOwedCleanupBound(key)
3934
4171
  const bound = {
4172
+ dueAtMs,
4173
+ askedAtMs: performance.now(),
3935
4174
  handle: setTimeout(() => {
3936
4175
  if (this.#pendingWorkerCleanupBounds.get(key) !== bound) return
3937
4176
  this.#pendingWorkerCleanupBounds.delete(key)
@@ -4062,7 +4301,22 @@ export class FECManager {
4062
4301
  this.#isoSourceOversizeCount++
4063
4302
  return null
4064
4303
  }
4065
- const sbn = located.block.start
4304
+ return this.#feedDecoderSource(packetId, ssId, symbolData, catalogTrackName, located.block.start)
4305
+ }
4306
+
4307
+ /**
4308
+ * Hand one placed source symbol to the stream's decoder: its Worker, which
4309
+ * answers through onFrame (null here), or its synchronous decoder, whose
4310
+ * recovered block is returned. `sbn` is the SS_Start of the block a repair
4311
+ * stated for `ssId`.
4312
+ */
4313
+ #feedDecoderSource(
4314
+ packetId: number,
4315
+ ssId: number,
4316
+ symbolData: Uint8Array,
4317
+ catalogTrackName: string | undefined,
4318
+ sbn: number,
4319
+ ): Uint8Array | null {
4066
4320
  const blockKey = this.#blockKey(sbn, packetId, catalogTrackName)
4067
4321
  const syncBlockOwner = this.#syncDecoderBlockOwners.get(blockKey)
4068
4322
 
@@ -4107,6 +4361,97 @@ export class FECManager {
4107
4361
  return recovered
4108
4362
  }
4109
4363
 
4364
+ /**
4365
+ * Feed a source symbol of this stream that another FEC instance over the
4366
+ * same packets rebuilt: a keyframe overlay recovery whose base SS_ID the
4367
+ * caller determined (draft-ramadan-moq-mmt-02 7.1, 7.4.4; joint decoding,
4368
+ * BLO-52085). It joins its block's decoder as a received source would, so
4369
+ * it reduces the erasures the block still has to solve, but it is not a
4370
+ * network arrival: no transport accounting, no classification evidence,
4371
+ * no deadline refresh, and it never makes a block clean.
4372
+ *
4373
+ * Only a block a repair stated takes one (a repair-less span has no
4374
+ * decoder block). A block that is terminal, or already holds that source,
4375
+ * does not. The symbol is the exact protected packet, unpadded, as
4376
+ * feedSourceSymbol takes it.
4377
+ *
4378
+ * `recovered` is the block the synchronous decoder completed with it; the
4379
+ * caller routes it and completes the block, as for any recovery. When
4380
+ * every source of the block is then present, received or rebuilt, and no
4381
+ * decoder returned it (a Worker answers asynchronously, and reports such a
4382
+ * block as clean on its own side), the block is completed here as
4383
+ * recovered, with nothing left to route.
4384
+ */
4385
+ feedRebuiltSourceSymbol(
4386
+ packetId: number,
4387
+ ssId: number,
4388
+ symbolData: Uint8Array,
4389
+ catalogTrackName?: string,
4390
+ ): RebuiltSourceFeed {
4391
+ if (!this.#hasBlockProfile(packetId)) return NOT_TAKEN
4392
+ const located = this.#isoLocatorFor(packetId, catalogTrackName).locate(ssId)
4393
+ if (located === null || located.block.span) return NOT_TAKEN
4394
+ if (!ssbg1FitsSymbol(symbolData.byteLength, this.#trackConfig(packetId).symbolSize)) return NOT_TAKEN
4395
+ const sbn = located.block.start
4396
+ const key = this.#blockKey(sbn, packetId, catalogTrackName)
4397
+ if (this.#completedBlocks.has(key) || this.#blockSourceEsis.get(key)?.has(located.esi)) return NOT_TAKEN
4398
+ let rebuilt = this.#blockRebuiltEsis.get(key)
4399
+ if (rebuilt?.has(located.esi)) return NOT_TAKEN
4400
+ if (rebuilt === undefined) {
4401
+ rebuilt = new Set()
4402
+ this.#blockRebuiltEsis.set(key, rebuilt)
4403
+ }
4404
+ const config = this.#trackConfigs.get(packetId)
4405
+ const wasShort = config !== undefined &&
4406
+ this.#lateSourceWaitMs(key, sbn, packetId, catalogTrackName, config) !== undefined
4407
+ rebuilt.add(located.esi)
4408
+ const recovered = this.#feedDecoderSource(packetId, ssId, symbolData, catalogTrackName, sbn)
4409
+ if (recovered === null && !this.#completeWhenSourcesPresent(key, sbn, located.block.length, packetId, catalogTrackName)) {
4410
+ // The late-source cut assumed only a late source could complete this
4411
+ // block; with this source its group can, so it gets a full multicast
4412
+ // wait again, counted from now.
4413
+ // ponytail: from now, not from its sources, so it can close up to one
4414
+ // multicast wait after its uncut deadline; at most once per block, since
4415
+ // a rebuilt source only adds to what the block holds and the cut cannot
4416
+ // apply to it again. Keep the uncut deadline and re-arm to it if a lab
4417
+ // run shows that wait.
4418
+ if (wasShort && this.#activeBlocks.has(key) &&
4419
+ this.#lateSourceWaitMs(key, sbn, packetId, catalogTrackName, config!) === undefined) {
4420
+ this.#armBlockTimeout(sbn, packetId, catalogTrackName, this.#multicastWaitMs(config!))
4421
+ }
4422
+ }
4423
+ return { taken: true, recovered }
4424
+ }
4425
+
4426
+ /**
4427
+ * A block that holds every one of its `length` sources, received or
4428
+ * rebuilt by another instance, with at least one rebuilt, is recovered:
4429
+ * every packet of it was delivered, by its own arrival or by the instance
4430
+ * that rebuilt it, so nothing is routed. Returns whether it completed it.
4431
+ */
4432
+ #completeWhenSourcesPresent(
4433
+ key: string,
4434
+ sbn: number,
4435
+ length: number,
4436
+ packetId: number,
4437
+ catalogTrackName: string | undefined,
4438
+ ): boolean {
4439
+ const rebuilt = this.#blockRebuiltEsis.get(key)
4440
+ if (rebuilt === undefined || this.#completedBlocks.has(key)) return false
4441
+ let present = rebuilt.size
4442
+ for (const esi of this.#blockSourceEsis.get(key) ?? []) if (!rebuilt.has(esi)) present++
4443
+ if (present < length) return false
4444
+ this.#traceLifecycle('recovered', sbn, packetId, catalogTrackName)
4445
+ this.countRecovery(packetId, sbn, catalogTrackName)
4446
+ this.cancelBlockTimeout(sbn, packetId, catalogTrackName)
4447
+ if (catalogTrackName === undefined) {
4448
+ this.onBlockComplete?.(sbn, packetId)
4449
+ } else {
4450
+ this.onBlockComplete?.(sbn, packetId, catalogTrackName)
4451
+ }
4452
+ return true
4453
+ }
4454
+
4110
4455
  /**
4111
4456
  * fec-01 4.5: a reed-solomon repair packet whose SSB_length + RSB_length
4112
4457
  * exceeds 255, or whose RS_ID is not below RSB_length, is discarded with the
@@ -4364,9 +4709,150 @@ export class FECManager {
4364
4709
  this.#completeCleanBlock(blockKey, sbn, packetId, catalogTrackName)
4365
4710
  return null
4366
4711
  }
4712
+ // Its K' is known now: a block this repair shows short of it waits less.
4713
+ if (recovered === null) this.#shortenUnrecoverableWait(blockKey, sbn, packetId, catalogTrackName)
4367
4714
  return recovered
4368
4715
  }
4369
4716
 
4717
+ /**
4718
+ * A first-arrival multicast source, held or placed. A datagram flow keeps
4719
+ * its sources in send order, so one that moves the stream's newest source
4720
+ * position past a block leaves the sources that block still misses
4721
+ * reordered behind it at best: it shortens the wait of every block it
4722
+ * passed that only a late source can complete. MoQ is left out: its streams
4723
+ * can overtake one another, so a MoQ source shows nothing about the sources
4724
+ * sent before it.
4725
+ */
4726
+ #noteMulticastSource(
4727
+ locator: IsoBlockLocator<IsoHeldSource>,
4728
+ ssId: number,
4729
+ packetId: number,
4730
+ catalogTrackName: string | undefined,
4731
+ ): void {
4732
+ const previous = locator.newestSource
4733
+ if (!locator.noteSource(ssId) || previous === undefined) return
4734
+ // The blocks whose last SS_ID lies in [previous, ssId): this source
4735
+ // passed them and the previous newest one had not.
4736
+ for (const block of locator.blocksBetween((previous - 1) >>> 0, ssId)) {
4737
+ if (!serial32Precedes(serial32Add(block.start, block.length - 1), ssId)) continue
4738
+ this.#shortenUnrecoverableWait(
4739
+ this.#blockKey(block.start, packetId, catalogTrackName),
4740
+ block.start,
4741
+ packetId,
4742
+ catalogTrackName,
4743
+ )
4744
+ }
4745
+ }
4746
+
4747
+ /**
4748
+ * The wait of a block that only a late source can complete (BLO-52085): a
4749
+ * repair stated it, its received sources plus every symbol its repair group
4750
+ * carries (RSB_length) are short of K' (the repair scheduler's
4751
+ * group-cannot-complete), and a later source of the stream has arrived, so
4752
+ * a source it misses is lost or reordered behind that one. The span, which
4753
+ * covers repair symbols paced after its sources, buys it nothing: it waits
4754
+ * the late-symbol allowance alone, the multicast wait less the span (the
4755
+ * catalog tolerance or the measured delivery gap). Undefined for any other
4756
+ * block. While a keyframe overlay is installed, the block waits for its
4757
+ * repair too (#overlayLateWaitMs). A repair-less span keeps its whole
4758
+ * wait, even one longer than K whose group is empty (#openIsoSpan): its
4759
+ * resolution is what releases the frames behind it, and failing it early
4760
+ * released them past a keyframe the overlay was still rebuilding, which
4761
+ * then arrived stale. The release gate lets them go instead
4762
+ * (isoRepairlessWaitMs).
4763
+ */
4764
+ #lateSourceWaitMs(
4765
+ key: string,
4766
+ sbn: number,
4767
+ packetId: number,
4768
+ catalogTrackName: string | undefined,
4769
+ config: ResolvedFECTrackConfig,
4770
+ ): number | undefined {
4771
+ if (this.#isoSpanKeys.has(key) || !this.#groupCannotComplete(key)) return undefined
4772
+ const authoredLength = this.#blockAuthoredLengths.get(key)!
4773
+ const newestSource = this.#isoLocators.get(this.#streamKey(packetId, catalogTrackName))?.newestSource
4774
+ if (newestSource === undefined || !serial32Precedes(serial32Add(sbn, authoredLength - 1), newestSource)) {
4775
+ return undefined
4776
+ }
4777
+ return this.#overlayLateWaitMs(packetId, catalogTrackName, config)
4778
+ }
4779
+
4780
+ /**
4781
+ * A repair stated the block, and its sources plus every symbol its repair
4782
+ * group carries (RSB_length) are short of its SSB_length.
4783
+ */
4784
+ #groupCannotComplete(key: string): boolean {
4785
+ const authoredLength = this.#blockAuthoredLengths.get(key)
4786
+ const rsbLength = this.#blockRsbLengths.get(key)
4787
+ if (authoredLength === undefined || rsbLength === undefined) return false
4788
+ // A source another instance rebuilt (feedRebuiltSourceSymbol) is one the
4789
+ // decoder holds: the group need only cover what neither delivered.
4790
+ let present = this.#blockSourceCounts.get(key) ?? 0
4791
+ const received = this.#blockSourceEsis.get(key)
4792
+ for (const esi of this.#blockRebuiltEsis.get(key) ?? []) if (!received?.has(esi)) present++
4793
+ return present + rsbLength < authoredLength
4794
+ }
4795
+
4796
+ /**
4797
+ * How long a loss only a late source or an installed keyframe overlay can
4798
+ * still fill waits once a later source passed it: the late-symbol
4799
+ * allowance alone (the multicast wait less the span) while no overlay is
4800
+ * installed. An installed one (keyframeOverlayTiming) may still rebuild a
4801
+ * RAP packet the loss took: a second FEC instance over the same packets
4802
+ * (draft-ramadan-moq-mmt-02 7.4), whose repair is paced across its own
4803
+ * span, unrelated to the base's. That packet can complete a stated block
4804
+ * (feedRebuiltSourceSymbol), or play behind a span; giving the loss up
4805
+ * first lets the release gate pass the keyframe, whose rebuild then lands
4806
+ * stale. The overlay repair of a packet the loss took goes out within the
4807
+ * overlay's span of that packet (fec-core-00 9.3), and the later source was
4808
+ * sent after it, so the loss waits that span plus the larger of the
4809
+ * allowance and the overlay's own tolerance. An overlay that states no
4810
+ * span (its block may then span keyframes), or one whose wait is not
4811
+ * shorter than the whole multicast wait, leaves the whole wait: undefined.
4812
+ */
4813
+ #overlayLateWaitMs(
4814
+ packetId: number,
4815
+ catalogTrackName: string | undefined,
4816
+ config: ResolvedFECTrackConfig,
4817
+ ): number | undefined {
4818
+ const waitMs = this.#multicastWaitMs(config)
4819
+ const allowanceMs = waitMs - config.interleaveDepthMs
4820
+ // Read live. The late-source cut only ever shortens a block's deadline
4821
+ // (#shortenUnrecoverableWait), so a block it armed at the bare allowance
4822
+ // before an overlay was installed, or between a dispose and a reinstall,
4823
+ // keeps that deadline until a source of its own re-arms it.
4824
+ const overlay = this.keyframeOverlayTiming?.(packetId, catalogTrackName)
4825
+ if (overlay === undefined) return allowanceMs
4826
+ const overlaySpanMs = overlay.interleaveDepthMs ?? 0
4827
+ const overlayWaitMs = overlaySpanMs + Math.max(allowanceMs, overlay.reorderToleranceMs ?? 0)
4828
+ return overlaySpanMs > 0 && overlayWaitMs < waitMs ? overlayWaitMs : undefined
4829
+ }
4830
+
4831
+ /**
4832
+ * Bring an open block's wait in to its late-source wait, when it has one.
4833
+ * Never later than it is armed: only a source can complete such a block,
4834
+ * so a repair of its group (paced across the span) must not keep it open.
4835
+ */
4836
+ #shortenUnrecoverableWait(
4837
+ key: string,
4838
+ sbn: number,
4839
+ packetId: number,
4840
+ catalogTrackName: string | undefined,
4841
+ ): void {
4842
+ if (!this.#activeBlocks.has(key)) return
4843
+ const config = this.#trackConfigs.get(packetId)
4844
+ if (config === undefined) return
4845
+ const waitMs = this.#lateSourceWaitMs(key, sbn, packetId, catalogTrackName, config)
4846
+ if (waitMs === undefined) return
4847
+ const armedAtMs = this.#blockWaitDeadlinesMs.get(key)
4848
+ this.#armBlockTimeout(
4849
+ sbn,
4850
+ packetId,
4851
+ catalogTrackName,
4852
+ armedAtMs === undefined ? waitMs : Math.min(waitMs, armedAtMs - this.#receiptNow()),
4853
+ )
4854
+ }
4855
+
4370
4856
  /**
4371
4857
  * Record a block's authored SSB_length from a repair symbol. Returns false,
4372
4858
  * leaving the block unchanged, when the symbol contradicts the block: a
@@ -4506,7 +4992,8 @@ export class FECManager {
4506
4992
  * the inactivity deadline on each symbol, records the symbol's in-band
4507
4993
  * classification evidence, and marks the block clean when all of its
4508
4994
  * authored source symbols arrived: the SSB_length a repair symbol declared,
4509
- * otherwise K.
4995
+ * otherwise K. `held` marks a source held unplaced since it arrived and
4996
+ * placed only now (see #noteSbnSeen).
4510
4997
  *
4511
4998
  * Throws for a packetId that was never configured, so a classified block
4512
4999
  * never reads the manager-global K or timeout.
@@ -4517,6 +5004,7 @@ export class FECManager {
4517
5004
  catalogTrackName: string | undefined,
4518
5005
  esi: number,
4519
5006
  evidence: SourceSymbolEvidence,
5007
+ held = false,
4520
5008
  ): void {
4521
5009
  const trackConfig = this.#trackConfigs.get(packetId)
4522
5010
  if (trackConfig === undefined) {
@@ -4538,6 +5026,7 @@ export class FECManager {
4538
5026
  'source',
4539
5027
  evidence.packetSequenceNumber,
4540
5028
  Number.isInteger(esi) ? (sbn + esi) >>> 0 : sbn,
5029
+ held,
4541
5030
  )
4542
5031
 
4543
5032
  // A source ESI at or past the block's authored SSB_length contradicts it;
@@ -4582,6 +5071,20 @@ export class FECManager {
4582
5071
  this.#completeCleanBlock(key, sbn, packetId, catalogTrackName)
4583
5072
  return
4584
5073
  }
5074
+ // A late arrival can be the last source a block lacked besides those
5075
+ // another instance rebuilt (feedRebuiltSourceSymbol).
5076
+ if (
5077
+ this.#blockRebuiltEsis.has(key) &&
5078
+ this.#completeWhenSourcesPresent(
5079
+ key,
5080
+ sbn,
5081
+ this.#isoLocatorFor(packetId, catalogTrackName).blockAt(sbn)?.length ?? authoredLength,
5082
+ packetId,
5083
+ catalogTrackName,
5084
+ )
5085
+ ) {
5086
+ return
5087
+ }
4585
5088
 
4586
5089
  this.noteBlockActivity(sbn, packetId, catalogTrackName, true)
4587
5090
 
@@ -4601,7 +5104,9 @@ export class FECManager {
4601
5104
  this.cancelBlockTimeout(sbn, packetId, catalogTrackName)
4602
5105
  this.#blockSourceCounts.delete(key)
4603
5106
  this.#blockSourceEsis.delete(key)
5107
+ this.#blockRebuiltEsis.delete(key)
4604
5108
  this.#blockAuthoredLengths.delete(key)
5109
+ this.#isoSpanKeys.delete(key)
4605
5110
  this.#blockRepairRsIds.delete(key)
4606
5111
  this.#blockRsbLengths.delete(key)
4607
5112
  // BLO-3323 L15: track which SBNs reached K source-symbol arrivals so the
@@ -4643,6 +5148,20 @@ export class FECManager {
4643
5148
  return this.#receiptAtMs ?? performance.now()
4644
5149
  }
4645
5150
 
5151
+ /**
5152
+ * The whole-ms delay of a timer for `deadlineMs` armed inside a receipt
5153
+ * scope: counted from the receipt time, as the timer of a symbol fed when
5154
+ * it was received was, less the whole ms since. So it fires on the tick
5155
+ * that timer would have, never later than a delay counted from now, and
5156
+ * its handler still re-arms a timer that fires before its deadline.
5157
+ * Counted from now, a deadline set from the receipt time is rounded up
5158
+ * from another fraction of a ms (BLO-52085).
5159
+ */
5160
+ #receiptTimerDelayMs(deadlineMs: number, minMs: number): number {
5161
+ const receivedAtMs = this.#receiptAtMs!
5162
+ return Math.max(minMs, Math.ceil(deadlineMs - receivedAtMs) - Math.ceil(performance.now() - receivedAtMs))
5163
+ }
5164
+
4646
5165
  /**
4647
5166
  * A held packet admitted late arms from its (earlier) receipt time; that
4648
5167
  * never pulls in a deadline later activity already armed. Outside a
@@ -4714,7 +5233,18 @@ export class FECManager {
4714
5233
  )
4715
5234
  }
4716
5235
 
4717
- this.#armBlockTimeout(sbn, packetId, catalogTrackName, timeoutMs)
5236
+ // A block only a late source can complete waits the late-symbol
5237
+ // allowance from its own sources' activity; nothing else moves it later.
5238
+ if (!sourceProgress && this.#lateSourceWaitMs(key, sbn, packetId, catalogTrackName, trackConfig) !== undefined) {
5239
+ this.#shortenUnrecoverableWait(key, sbn, packetId, catalogTrackName)
5240
+ return
5241
+ }
5242
+ this.#armBlockTimeout(
5243
+ sbn,
5244
+ packetId,
5245
+ catalogTrackName,
5246
+ this.#lateSourceWaitMs(key, sbn, packetId, catalogTrackName, trackConfig) ?? timeoutMs,
5247
+ )
4718
5248
  }
4719
5249
 
4720
5250
  #failBlock(
@@ -4723,6 +5253,9 @@ export class FECManager {
4723
5253
  catalogTrackName?: string,
4724
5254
  ): void {
4725
5255
  const key = this.#blockKey(sbn, packetId, catalogTrackName)
5256
+ const span: IsoSpanFailure | undefined = this.#isoSpanKeys.delete(key)
5257
+ ? { length: this.#blockAuthoredLengths.get(key) ?? 0, sources: this.#blockSourceCounts.get(key) ?? 0 }
5258
+ : undefined
4726
5259
  this.#traceLifecycle('failed', sbn, packetId, catalogTrackName)
4727
5260
  this.#clearRepairDeadline(key)
4728
5261
  this.#clearBlockTimer(key)
@@ -4732,6 +5265,7 @@ export class FECManager {
4732
5265
  }
4733
5266
  this.#blockSourceCounts.delete(key)
4734
5267
  this.#blockSourceEsis.delete(key)
5268
+ this.#blockRebuiltEsis.delete(key)
4735
5269
  this.#blockAuthoredLengths.delete(key)
4736
5270
  this.#blockRepairRsIds.delete(key)
4737
5271
  this.#blockRsbLengths.delete(key)
@@ -4741,7 +5275,8 @@ export class FECManager {
4741
5275
  this.#markPathBlockComplete(sbn, packetId, catalogTrackName)
4742
5276
  this.#fecStats.blocksFailed++
4743
5277
  this.#statsForPacket(packetId).blocksFailed++
4744
- this.onBlockFailed?.(sbn, packetId, catalogTrackName, blockClass)
5278
+ if (span === undefined) this.onBlockFailed?.(sbn, packetId, catalogTrackName, blockClass)
5279
+ else this.onBlockFailed?.(sbn, packetId, catalogTrackName, blockClass, span)
4745
5280
  this.onBlockResolved?.(sbn, packetId, catalogTrackName)
4746
5281
  }
4747
5282
 
@@ -4771,7 +5306,9 @@ export class FECManager {
4771
5306
  this.#retireBlockClass(key, block.sbn, block.packetId, block.catalogTrackName)
4772
5307
  this.#blockSourceCounts.delete(key)
4773
5308
  this.#blockSourceEsis.delete(key)
5309
+ this.#blockRebuiltEsis.delete(key)
4774
5310
  this.#blockAuthoredLengths.delete(key)
5311
+ this.#isoSpanKeys.delete(key)
4775
5312
  this.#blockRepairRsIds.delete(key)
4776
5313
  this.#blockRsbLengths.delete(key)
4777
5314
  this.#clearBlockTimer(key)
@@ -4879,7 +5416,9 @@ export class FECManager {
4879
5416
  this.#blockKeysBySbn.clear()
4880
5417
  this.#blockSourceCounts.clear()
4881
5418
  this.#blockSourceEsis.clear()
5419
+ this.#blockRebuiltEsis.clear()
4882
5420
  this.#blockAuthoredLengths.clear()
5421
+ this.#isoSpanKeys.clear()
4883
5422
  this.#blockRepairRsIds.clear()
4884
5423
  this.#blockRsbLengths.clear()
4885
5424
  this.#heldLayerStates.clear()