@blockcast/mmt-container 2.2.0-main.2c4d96d3ffc2 → 2.2.0-main.34b53d33bace

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 (60) hide show
  1. package/dist/browser-runtime.js +954 -157
  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/delta-fallback-config.js.map +1 -1
  9. package/dist/fec-manager.d.ts +71 -14
  10. package/dist/fec-manager.d.ts.map +1 -1
  11. package/dist/fec-manager.js +300 -60
  12. package/dist/fec-manager.js.map +2 -2
  13. package/dist/fec-worker-setup.js.map +1 -1
  14. package/dist/index.d.ts +1 -1
  15. package/dist/index.d.ts.map +1 -1
  16. package/dist/index.js.map +2 -2
  17. package/dist/iso-block-locator.d.ts +39 -13
  18. package/dist/iso-block-locator.d.ts.map +1 -1
  19. package/dist/iso-block-locator.js +80 -25
  20. package/dist/iso-block-locator.js.map +2 -2
  21. package/dist/media-router.js.map +1 -1
  22. package/dist/mmtp-container-parser.d.ts +63 -10
  23. package/dist/mmtp-container-parser.d.ts.map +1 -1
  24. package/dist/mmtp-container-parser.js +218 -18
  25. package/dist/mmtp-container-parser.js.map +2 -2
  26. package/dist/mmtp-container-parser.no-modules.js +217 -19
  27. package/dist/mmtp-container-parser.no-modules.js.map +3 -3
  28. package/dist/mmtp-decode-pipeline.js +218 -18
  29. package/dist/mmtp-decode-pipeline.js.map +2 -2
  30. package/dist/mmtp-router.d.ts +22 -9
  31. package/dist/mmtp-router.d.ts.map +1 -1
  32. package/dist/mmtp-router.js +384 -51
  33. package/dist/mmtp-router.js.map +3 -3
  34. package/dist/multicast-manager.d.ts +26 -3
  35. package/dist/multicast-manager.d.ts.map +1 -1
  36. package/dist/multicast-manager.js +774 -130
  37. package/dist/multicast-manager.js.map +3 -3
  38. package/dist/multicast-transport.js +2 -2
  39. package/dist/multicast-transport.js.map +2 -2
  40. package/dist/no-modules.js +218 -18
  41. package/dist/no-modules.js.map +3 -3
  42. package/dist/repair-fetch.d.ts +38 -8
  43. package/dist/repair-fetch.d.ts.map +1 -1
  44. package/dist/repair-fetch.js +286 -27
  45. package/dist/repair-fetch.js.map +3 -3
  46. package/dist/track-session.d.ts.map +1 -1
  47. package/dist/track-session.js +1 -0
  48. package/dist/track-session.js.map +2 -2
  49. package/dist/wasm-mmtp-container-parser.js +218 -18
  50. package/dist/wasm-mmtp-container-parser.js.map +2 -2
  51. package/package.json +5 -5
  52. package/src/codec-utils.ts +121 -1
  53. package/src/fec-manager.ts +316 -34
  54. package/src/index.ts +2 -0
  55. package/src/iso-block-locator.ts +114 -31
  56. package/src/mmtp-container-parser.ts +238 -21
  57. package/src/mmtp-router.ts +271 -58
  58. package/src/multicast-manager.ts +136 -16
  59. package/src/repair-fetch.ts +366 -50
  60. package/src/track-session.ts +3 -0
@@ -27,6 +27,7 @@ import {
27
27
  serial32Precedes,
28
28
  ssbg1FitsSymbol,
29
29
  type FecBlockProfile,
30
+ type FecInstanceGeometry,
30
31
  admitMmtpRepairObject,
31
32
  parseMoqRepairFrameWithHeader,
32
33
  type MMTPHeader,
@@ -213,6 +214,25 @@ export interface UndelimitedIsoLoss {
213
214
  readonly previousBlockStart?: number
214
215
  }
215
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
+
216
236
  /**
217
237
  * How the multicast leg hands its symbols to this manager.
218
238
  *
@@ -522,7 +542,14 @@ export class FECManager {
522
542
  // 'pending' forever, which would park every later frame of its stream in
523
543
  // the in-order release gate. Each entry owns its handle; the callback
524
544
  // checks it still owns the key, so a settled or reset key is never touched.
525
- #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
+ >()
526
553
  // Source symbol count per track-scoped block — marks complete when K symbols arrive.
527
554
  #blockSourceCounts = new Map<string, number>()
528
555
  #blockSourceEsis = new Map<string, Set<number>>()
@@ -538,10 +565,14 @@ export class FECManager {
538
565
  // SSB_length source arrivals and recoverable from SSB_length received
539
566
  // symbols; waiting for K fails every one of them.
540
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>()
541
571
  #ssbLengthConflictCount = 0
542
572
  #blockRepairRsIds = new Map<string, Set<number>>()
543
573
  // RSB_length per block (C.5.3 Repair FEC Payload ID): how many repair
544
- // 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.
545
576
  #blockRsbLengths = new Map<string, number>()
546
577
  /** fec-01 6.3.9 anchor state per stream key, one entry per held layer. */
547
578
  #heldLayerStates = new Map<string, HeldLayerAdmission>()
@@ -745,13 +776,17 @@ export class FECManager {
745
776
  onBlockComplete:
746
777
  | ((sbn: number, packetId: number, catalogTrackName?: string) => void)
747
778
  | null = null
748
- /** 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
+ */
749
783
  onBlockFailed:
750
784
  | ((
751
785
  sbn: number,
752
786
  packetId: number,
753
787
  catalogTrackName: string | undefined,
754
788
  blockClass: BlockClass,
789
+ span?: IsoSpanFailure,
755
790
  ) => void)
756
791
  | null = null
757
792
  /**
@@ -835,6 +870,8 @@ export class FECManager {
835
870
  * for it, which it must be.
836
871
  */
837
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
838
875
  /** A block every symbol of which vanished was created by SBN evidence. */
839
876
  onBlockVanished:
840
877
  | ((sbn: number, packetId: number, catalogTrackName: string | undefined) => void)
@@ -847,6 +884,21 @@ export class FECManager {
847
884
  onSourceDiscontinuity:
848
885
  | ((packetId: number, catalogTrackName: string | undefined) => void)
849
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
850
902
 
851
903
  // Worker-path callbacks — async recovery and stats from FecWorkerClient.
852
904
  /**
@@ -1489,7 +1541,12 @@ export class FECManager {
1489
1541
  timer.handle = null
1490
1542
  this.#requestRepair(sbn, packetId, catalogTrackName)
1491
1543
  }
1492
- 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
+ )
1493
1550
  this.#repairDeadlines.set(key, timer)
1494
1551
  }
1495
1552
 
@@ -1683,6 +1740,12 @@ export class FECManager {
1683
1740
  * Vanished blocks are not enumerated here: their starts are unknown until a
1684
1741
  * repair states them. The SS_IDs between two stated blocks become one
1685
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).
1686
1749
  */
1687
1750
  #noteSbnSeen(
1688
1751
  sbn: number,
@@ -1691,6 +1754,7 @@ export class FECManager {
1691
1754
  symbol: 'source' | 'repair',
1692
1755
  packetSequenceNumber?: number,
1693
1756
  ssId = sbn,
1757
+ held = false,
1694
1758
  ): void {
1695
1759
  const streamKey = this.#streamKey(packetId, catalogTrackName)
1696
1760
  // Arrival at FEC, not receipt (withReceiptTime): this bounds how far SS_ID
@@ -1747,7 +1811,7 @@ export class FECManager {
1747
1811
  }
1748
1812
  if (!this.#blockPrecedes(previous.sbn, sbn)) {
1749
1813
  if (symbol === 'source' && window !== null && this.#blockPrecedes(sbn, previous.sbn) && (
1750
- this.#blocksBehind(sbn, previous.sbn, packetId) > this.#isoRetentionBlocks(packetId) ||
1814
+ (!held && this.#blocksBehind(sbn, previous.sbn, packetId) > this.#isoRetentionBlocks(packetId)) ||
1751
1815
  (psn !== undefined && anchor !== undefined && serial32Precedes(ssId, anchor.ssId) &&
1752
1816
  psn !== anchor.psn && !sentInOrder({ ssId, psn }, anchor))
1753
1817
  )) {
@@ -2101,21 +2165,28 @@ export class FECManager {
2101
2165
  this.#blockWaitDeadlinesMs.delete(key)
2102
2166
  this.#repairHoldUntilMs.delete(key)
2103
2167
  this.#traceLifecycle('timeout-queued', sbn, packetId, catalogTrackName)
2104
- this.#finalizeExpiredBlock(sbn, packetId, catalogTrackName)
2168
+ this.#finalizeExpiredBlock(sbn, packetId, catalogTrackName, currentDeadlineMs ?? firedAtMs)
2105
2169
  }
2106
- 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
+ )
2107
2176
  this.#activeBlocks.set(key, timer)
2108
2177
  this.#indexBlockKey(key, sbn, packetId)
2109
2178
  }
2110
2179
 
2111
2180
  /**
2112
2181
  * Hand a block whose delivery is over to its decoder for finalization: the
2113
- * 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.
2114
2184
  */
2115
2185
  #finalizeExpiredBlock(
2116
2186
  sbn: number,
2117
2187
  packetId: number,
2118
2188
  catalogTrackName: string | undefined,
2189
+ dueAtMs = performance.now(),
2119
2190
  ): void {
2120
2191
  const key = this.#blockKey(sbn, packetId, catalogTrackName)
2121
2192
  // A vanished block that never received a symbol holds no decoder state
@@ -2139,7 +2210,7 @@ export class FECManager {
2139
2210
  if (workerClient) {
2140
2211
  this.#pendingWorkerCleanups.add(key)
2141
2212
  this.#pendingWorkerCleanupClients.set(key, workerClient)
2142
- this.#boundOwedCleanup(key, packetId, workerClient)
2213
+ this.#boundOwedCleanup(key, packetId, workerClient, dueAtMs)
2143
2214
  workerClient.cleanup(sbn)
2144
2215
  } else {
2145
2216
  const decoder = this.#decoderForPacket(packetId, catalogTrackName)?.client
@@ -2558,6 +2629,7 @@ export class FECManager {
2558
2629
  resetMap(this.#blockSourceEsis)
2559
2630
  resetMap(this.#blockRebuiltEsis)
2560
2631
  resetMap(this.#blockAuthoredLengths)
2632
+ resetSet(this.#isoSpanKeys)
2561
2633
  resetMap(this.#blockRepairRsIds)
2562
2634
  resetMap(this.#blockRsbLengths)
2563
2635
  resetMap(this.#heldLayerStates)
@@ -2691,6 +2763,7 @@ export class FECManager {
2691
2763
  dropMap(this.#blockSourceEsis)
2692
2764
  dropMap(this.#blockRebuiltEsis)
2693
2765
  dropMap(this.#blockAuthoredLengths)
2766
+ dropSet(this.#isoSpanKeys)
2694
2767
  dropMap(this.#blockRepairRsIds)
2695
2768
  dropMap(this.#blockRsbLengths)
2696
2769
  dropMap(this.#blockPathCounts)
@@ -3038,8 +3111,10 @@ export class FECManager {
3038
3111
  }
3039
3112
  /**
3040
3113
  * An open block's accepted sources, distinct repair RS_IDs, authored K'
3041
- * (SSB_length) and RSB_length (each undefined until a repair stated it).
3042
- * 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.
3043
3118
  */
3044
3119
  blockSymbolCounts(
3045
3120
  sbn: number,
@@ -3122,8 +3197,9 @@ export class FECManager {
3122
3197
  return true
3123
3198
  }
3124
3199
  /**
3125
- * Drop a block's repair hold: its FETCH ended without delivering. The block
3126
- * 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.
3127
3203
  */
3128
3204
  releaseRepairHold(sbn: number, packetId: number, catalogTrackName?: string): void {
3129
3205
  const key = this.#blockKey(sbn, packetId, catalogTrackName)
@@ -3285,6 +3361,7 @@ export class FECManager {
3285
3361
  const locator = this.#growingIsoLocatorFor(packetId, catalogTrackName)
3286
3362
  const evictedBefore = locator.heldEvicted
3287
3363
  const held = locator.hold(ssId, source)
3364
+ this.#settleExpiredIsoHoles(locator, packetId, catalogTrackName)
3288
3365
  if (held && source.source === 'multicast') this.#noteMulticastSource(locator, ssId, packetId, catalogTrackName)
3289
3366
  const evicted = locator.heldEvicted - evictedBefore
3290
3367
  if (evicted > 0) {
@@ -3313,17 +3390,26 @@ export class FECManager {
3313
3390
  ssStart: number,
3314
3391
  ssbLength: number,
3315
3392
  ): IsoRepairPlacement<IsoHeldSource> {
3316
- 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)
3317
3396
  if (placement.status !== 'placed') {
3318
3397
  this.#isoRepairRefusedCount++
3319
3398
  if (this.#isoRepairRefusedCount <= 5 || this.#isoRepairRefusedCount % 100 === 0) {
3320
3399
  console.warn(`[FECManager] packetId=${packetId} iso-ssbg1-v1 repair refused (${placement.status}): ${placement.reason}`)
3321
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)
3322
3406
  return placement
3323
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))
3324
3411
  if (placement.span !== undefined) this.#openIsoSpan(placement.span, packetId, catalogTrackName)
3325
3412
  if (placement.hole !== undefined) {
3326
- const locator = this.#isoLocatorFor(packetId, catalogTrackName)
3327
3413
  const hole = placement.hole.block
3328
3414
  const previous = locator.blockEndingAt(hole.start)
3329
3415
  const loss: UndelimitedIsoLoss = {
@@ -3345,18 +3431,38 @@ export class FECManager {
3345
3431
  * `handler`, instead of a repair-less span; null restores spans. Set by the
3346
3432
  * range repair scheduler; applies to every stream from its next gap. The
3347
3433
  * handler and the deferral are one setting, so no hole is ever created with
3348
- * 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.
3349
3438
  */
3350
- setUndelimitedLossHandler(handler: ((loss: UndelimitedIsoLoss) => void) | null): void {
3439
+ setUndelimitedLossHandler(
3440
+ handler: ((loss: UndelimitedIsoLoss) => void) | null,
3441
+ onExpired: ((hole: ExpiredIsoHole) => boolean) | null = null,
3442
+ ): void {
3351
3443
  this.#onUndelimitedLoss = handler
3444
+ this.#onIsoHoleExpired = handler === null ? null : onExpired
3352
3445
  this.#deferIsoGaps = handler !== null
3353
3446
  for (const locator of this.#isoLocators.values()) locator.deferIncompleteGaps = this.#deferIsoGaps
3354
3447
  }
3355
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
+
3356
3461
  /**
3357
- * The range repair for a hole ended: every hole still pending inside
3358
- * `[start, start + length)` becomes a repair-less span. Returns those spans
3359
- * 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).
3360
3466
  */
3361
3467
  closeIsoHoles(
3362
3468
  packetId: number,
@@ -3378,6 +3484,17 @@ export class FECManager {
3378
3484
  * its placed sources exactly as on the wire (feedRepairSymbol). A span every
3379
3485
  * SS_ID of which vanished resolves through its deadline like any vanished
3380
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).
3381
3498
  */
3382
3499
  #openIsoSpan(
3383
3500
  span: { readonly block: IsoBlockRange; readonly placed: readonly unknown[] },
@@ -3385,7 +3502,16 @@ export class FECManager {
3385
3502
  catalogTrackName: string | undefined,
3386
3503
  ): void {
3387
3504
  const key = this.#blockKey(span.block.start, packetId, catalogTrackName)
3388
- 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
+ }
3389
3515
  if (span.placed.length === 0) this.#materializeBlock(span.block.start, packetId, catalogTrackName, true)
3390
3516
  }
3391
3517
 
@@ -3433,6 +3559,59 @@ export class FECManager {
3433
3559
  return lost
3434
3560
  }
3435
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), and a multicast source at or
3568
+ * past `toSsId` arrived, so each is lost or reordered behind it (a MoQ
3569
+ * source shows nothing: MoQ streams overtake one another). The span itself
3570
+ * keeps its whole wait.
3571
+ *
3572
+ * The wait is the late-symbol allowance, unless `rapMayHide`: the gap may
3573
+ * hold a RAP packet an installed keyframe overlay can still rebuild (its
3574
+ * decision stalls, or it ends at a RAP that may lie past a whole MPU).
3575
+ * Then the frame waits as a stated block its group cannot complete does
3576
+ * (#overlayLateWaitMs): the allowance alone with no overlay installed, the
3577
+ * overlay's span plus the larger of the allowance and its tolerance, or,
3578
+ * for an overlay that states no span, the span's whole wait (undefined).
3579
+ */
3580
+ isoRepairlessWaitMs(
3581
+ packetId: number,
3582
+ catalogTrackName: string | undefined,
3583
+ fromSsId: number,
3584
+ toSsId: number,
3585
+ rapMayHide = false,
3586
+ ): number | undefined {
3587
+ const config = this.#trackConfigs.get(packetId)
3588
+ if (config === undefined || !FECManager.#hasCatalogTiming(config)) return undefined
3589
+ const locator = this.#isoLocators.get(this.#streamKey(packetId, catalogTrackName))
3590
+ if (locator === undefined || !locator.coversBetween(fromSsId, toSsId)) return undefined
3591
+ const newestSource = locator.newestSource
3592
+ if (newestSource === undefined || serial32Precedes(newestSource, toSsId)) return undefined
3593
+ let repairless = false
3594
+ for (const block of locator.blocksBetween(fromSsId, toSsId)) {
3595
+ const key = this.#blockKey(block.start, packetId, catalogTrackName)
3596
+ if (this.#completedBlocks.has(key)) continue
3597
+ if (block.span && block.length > locator.kMax) {
3598
+ repairless = true
3599
+ continue
3600
+ }
3601
+ const received = this.#blockSourceEsis.get(key)
3602
+ for (let esi = 0; esi < block.length; esi++) {
3603
+ const ssId = serial32Add(block.start, esi)
3604
+ if (!serial32Precedes(fromSsId, ssId) || !serial32Precedes(ssId, toSsId)) continue
3605
+ if (!received?.has(esi) && !locator.isHeld(ssId)) return undefined
3606
+ }
3607
+ }
3608
+ if (!repairless) return undefined
3609
+ const waitMs = rapMayHide
3610
+ ? this.#overlayLateWaitMs(packetId, catalogTrackName, config)
3611
+ : this.#multicastWaitMs(config) - config.interleaveDepthMs
3612
+ return waitMs === undefined ? undefined : Math.max(0, waitMs)
3613
+ }
3614
+
3436
3615
  /**
3437
3616
  * iso-ssbg1-v1 block location: repair symbols it refused, oversize sources,
3438
3617
  * the sources held now for a block no repair has stated yet, and the held
@@ -3864,13 +4043,27 @@ export class FECManager {
3864
4043
 
3865
4044
  client.onCleanupComplete((sbn: number) => {
3866
4045
  if (this.#releasedWorkerClients.has(client)) return
3867
- const cleanupKeys = [...this.#pendingWorkerCleanups].filter((key) => {
4046
+ const owed = [...this.#pendingWorkerCleanups].filter((key) => this.#pendingWorkerCleanupClients.get(key) === client)
4047
+ const cleanupKeys = owed.filter((key) => {
3868
4048
  const block = this.#parseBlockKey(key)
3869
- return block.packetId === packetId &&
3870
- block.sbn === sbn &&
3871
- this.#pendingWorkerCleanupClients.get(key) === client
4049
+ return block.packetId === packetId && block.sbn === sbn
3872
4050
  })
3873
- for (const key of cleanupKeys) this.#settleOwedCleanup(key, 'cleanup-ack')
4051
+ // A cleanup is a queue-drain barrier: the Worker took every symbol fed
4052
+ // before it was asked for first, so every block already due by then
4053
+ // is settled by this acknowledgement too, not one barrier later. The
4054
+ // client sends one barrier at a time, and the blocks a repair walk
4055
+ // stated in one span are due together (BLO-52085). That holds only
4056
+ // while no block is fed a symbol after its dueAtMs, which is its
4057
+ // receipt-scoped deadline, not when feeding stopped: a symbol fed
4058
+ // between that deadline and the block's timer, after another block's
4059
+ // barrier was asked for, would find the block settled (failed) one
4060
+ // barrier early. Walks end before D-28 (walkDeadlineAt), so that window
4061
+ // is the timer's lateness alone; whatever feeds a block past its dueAtMs
4062
+ // must leave it to its own barrier.
4063
+ const askedAtMs = Math.max(...cleanupKeys.map((key) => this.#pendingWorkerCleanupBounds.get(key)?.askedAtMs ?? -Infinity))
4064
+ const due = owed.filter((key) =>
4065
+ !cleanupKeys.includes(key) && (this.#pendingWorkerCleanupBounds.get(key)?.dueAtMs ?? Infinity) <= askedAtMs)
4066
+ for (const key of [...cleanupKeys, ...due]) this.#settleOwedCleanup(key, 'cleanup-ack')
3874
4067
  })
3875
4068
 
3876
4069
  client.onError((msg: string, code?: FecWorkerErrorCode) => {
@@ -3952,9 +4145,11 @@ export class FECManager {
3952
4145
  * fail deadline already passed, so failing it is the decision that deadline
3953
4146
  * made. A late ack then finds nothing owed and is a no-op.
3954
4147
  */
3955
- #boundOwedCleanup(key: string, packetId: number, client: FecWorkerClient): void {
4148
+ #boundOwedCleanup(key: string, packetId: number, client: FecWorkerClient, dueAtMs: number): void {
3956
4149
  this.#clearOwedCleanupBound(key)
3957
4150
  const bound = {
4151
+ dueAtMs,
4152
+ askedAtMs: performance.now(),
3958
4153
  handle: setTimeout(() => {
3959
4154
  if (this.#pendingWorkerCleanupBounds.get(key) !== bound) return
3960
4155
  this.#pendingWorkerCleanupBounds.delete(key)
@@ -4185,9 +4380,25 @@ export class FECManager {
4185
4380
  rebuilt = new Set()
4186
4381
  this.#blockRebuiltEsis.set(key, rebuilt)
4187
4382
  }
4383
+ const config = this.#trackConfigs.get(packetId)
4384
+ const wasShort = config !== undefined &&
4385
+ this.#lateSourceWaitMs(key, sbn, packetId, catalogTrackName, config) !== undefined
4188
4386
  rebuilt.add(located.esi)
4189
4387
  const recovered = this.#feedDecoderSource(packetId, ssId, symbolData, catalogTrackName, sbn)
4190
- if (recovered === null) this.#completeWhenSourcesPresent(key, sbn, located.block.length, packetId, catalogTrackName)
4388
+ if (recovered === null && !this.#completeWhenSourcesPresent(key, sbn, located.block.length, packetId, catalogTrackName)) {
4389
+ // The late-source cut assumed only a late source could complete this
4390
+ // block; with this source its group can, so it gets a full multicast
4391
+ // wait again, counted from now.
4392
+ // ponytail: from now, not from its sources, so it can close up to one
4393
+ // multicast wait after its uncut deadline; at most once per block, since
4394
+ // a rebuilt source only adds to what the block holds and the cut cannot
4395
+ // apply to it again. Keep the uncut deadline and re-arm to it if a lab
4396
+ // run shows that wait.
4397
+ if (wasShort && this.#activeBlocks.has(key) &&
4398
+ this.#lateSourceWaitMs(key, sbn, packetId, catalogTrackName, config!) === undefined) {
4399
+ this.#armBlockTimeout(sbn, packetId, catalogTrackName, this.#multicastWaitMs(config!))
4400
+ }
4401
+ }
4191
4402
  return { taken: true, recovered }
4192
4403
  }
4193
4404
 
@@ -4502,7 +4713,7 @@ export class FECManager {
4502
4713
  // The blocks whose last SS_ID lies in [previous, ssId): this source
4503
4714
  // passed them and the previous newest one had not.
4504
4715
  for (const block of locator.blocksBetween((previous - 1) >>> 0, ssId)) {
4505
- if (block.span || !serial32Precedes(serial32Add(block.start, block.length - 1), ssId)) continue
4716
+ if (!serial32Precedes(serial32Add(block.start, block.length - 1), ssId)) continue
4506
4717
  this.#shortenUnrecoverableWait(
4507
4718
  this.#blockKey(block.start, packetId, catalogTrackName),
4508
4719
  block.start,
@@ -4521,7 +4732,13 @@ export class FECManager {
4521
4732
  * covers repair symbols paced after its sources, buys it nothing: it waits
4522
4733
  * the late-symbol allowance alone, the multicast wait less the span (the
4523
4734
  * catalog tolerance or the measured delivery gap). Undefined for any other
4524
- * block.
4735
+ * block. While a keyframe overlay is installed, the block waits for its
4736
+ * repair too (#overlayLateWaitMs). A repair-less span keeps its whole
4737
+ * wait, even one longer than K whose group is empty (#openIsoSpan): its
4738
+ * resolution is what releases the frames behind it, and failing it early
4739
+ * released them past a keyframe the overlay was still rebuilding, which
4740
+ * then arrived stale. The release gate lets them go instead
4741
+ * (isoRepairlessWaitMs).
4525
4742
  */
4526
4743
  #lateSourceWaitMs(
4527
4744
  key: string,
@@ -4530,15 +4747,56 @@ export class FECManager {
4530
4747
  catalogTrackName: string | undefined,
4531
4748
  config: ResolvedFECTrackConfig,
4532
4749
  ): number | undefined {
4750
+ if (this.#isoSpanKeys.has(key)) return undefined
4533
4751
  const authoredLength = this.#blockAuthoredLengths.get(key)
4534
4752
  const rsbLength = this.#blockRsbLengths.get(key)
4535
4753
  if (authoredLength === undefined || rsbLength === undefined) return undefined
4536
- if ((this.#blockSourceCounts.get(key) ?? 0) + rsbLength >= authoredLength) return undefined
4754
+ // A source another instance rebuilt (feedRebuiltSourceSymbol) is one the
4755
+ // decoder holds: the group need only cover what neither delivered.
4756
+ let present = this.#blockSourceCounts.get(key) ?? 0
4757
+ const received = this.#blockSourceEsis.get(key)
4758
+ for (const esi of this.#blockRebuiltEsis.get(key) ?? []) if (!received?.has(esi)) present++
4759
+ if (present + rsbLength >= authoredLength) return undefined
4537
4760
  const newestSource = this.#isoLocators.get(this.#streamKey(packetId, catalogTrackName))?.newestSource
4538
4761
  if (newestSource === undefined || !serial32Precedes(serial32Add(sbn, authoredLength - 1), newestSource)) {
4539
4762
  return undefined
4540
4763
  }
4541
- return this.#multicastWaitMs(config) - config.interleaveDepthMs
4764
+ return this.#overlayLateWaitMs(packetId, catalogTrackName, config)
4765
+ }
4766
+
4767
+ /**
4768
+ * How long a loss only a late source or an installed keyframe overlay can
4769
+ * still fill waits once a later source passed it: the late-symbol
4770
+ * allowance alone (the multicast wait less the span) while no overlay is
4771
+ * installed. An installed one (keyframeOverlayTiming) may still rebuild a
4772
+ * RAP packet the loss took: a second FEC instance over the same packets
4773
+ * (draft-ramadan-moq-mmt-02 7.4), whose repair is paced across its own
4774
+ * span, unrelated to the base's. That packet can complete a stated block
4775
+ * (feedRebuiltSourceSymbol), or play behind a span; giving the loss up
4776
+ * first lets the release gate pass the keyframe, whose rebuild then lands
4777
+ * stale. The overlay repair of a packet the loss took goes out within the
4778
+ * overlay's span of that packet (fec-core-00 9.3), and the later source was
4779
+ * sent after it, so the loss waits that span plus the larger of the
4780
+ * allowance and the overlay's own tolerance. An overlay that states no
4781
+ * span (its block may then span keyframes), or one whose wait is not
4782
+ * shorter than the whole multicast wait, leaves the whole wait: undefined.
4783
+ */
4784
+ #overlayLateWaitMs(
4785
+ packetId: number,
4786
+ catalogTrackName: string | undefined,
4787
+ config: ResolvedFECTrackConfig,
4788
+ ): number | undefined {
4789
+ const waitMs = this.#multicastWaitMs(config)
4790
+ const allowanceMs = waitMs - config.interleaveDepthMs
4791
+ // Read live. The late-source cut only ever shortens a block's deadline
4792
+ // (#shortenUnrecoverableWait), so a block it armed at the bare allowance
4793
+ // before an overlay was installed, or between a dispose and a reinstall,
4794
+ // keeps that deadline until a source of its own re-arms it.
4795
+ const overlay = this.keyframeOverlayTiming?.(packetId, catalogTrackName)
4796
+ if (overlay === undefined) return allowanceMs
4797
+ const overlaySpanMs = overlay.interleaveDepthMs ?? 0
4798
+ const overlayWaitMs = overlaySpanMs + Math.max(allowanceMs, overlay.reorderToleranceMs ?? 0)
4799
+ return overlaySpanMs > 0 && overlayWaitMs < waitMs ? overlayWaitMs : undefined
4542
4800
  }
4543
4801
 
4544
4802
  /**
@@ -4705,7 +4963,8 @@ export class FECManager {
4705
4963
  * the inactivity deadline on each symbol, records the symbol's in-band
4706
4964
  * classification evidence, and marks the block clean when all of its
4707
4965
  * authored source symbols arrived: the SSB_length a repair symbol declared,
4708
- * otherwise K.
4966
+ * otherwise K. `held` marks a source held unplaced since it arrived and
4967
+ * placed only now (see #noteSbnSeen).
4709
4968
  *
4710
4969
  * Throws for a packetId that was never configured, so a classified block
4711
4970
  * never reads the manager-global K or timeout.
@@ -4716,6 +4975,7 @@ export class FECManager {
4716
4975
  catalogTrackName: string | undefined,
4717
4976
  esi: number,
4718
4977
  evidence: SourceSymbolEvidence,
4978
+ held = false,
4719
4979
  ): void {
4720
4980
  const trackConfig = this.#trackConfigs.get(packetId)
4721
4981
  if (trackConfig === undefined) {
@@ -4737,6 +4997,7 @@ export class FECManager {
4737
4997
  'source',
4738
4998
  evidence.packetSequenceNumber,
4739
4999
  Number.isInteger(esi) ? (sbn + esi) >>> 0 : sbn,
5000
+ held,
4740
5001
  )
4741
5002
 
4742
5003
  // A source ESI at or past the block's authored SSB_length contradicts it;
@@ -4816,6 +5077,7 @@ export class FECManager {
4816
5077
  this.#blockSourceEsis.delete(key)
4817
5078
  this.#blockRebuiltEsis.delete(key)
4818
5079
  this.#blockAuthoredLengths.delete(key)
5080
+ this.#isoSpanKeys.delete(key)
4819
5081
  this.#blockRepairRsIds.delete(key)
4820
5082
  this.#blockRsbLengths.delete(key)
4821
5083
  // BLO-3323 L15: track which SBNs reached K source-symbol arrivals so the
@@ -4857,6 +5119,20 @@ export class FECManager {
4857
5119
  return this.#receiptAtMs ?? performance.now()
4858
5120
  }
4859
5121
 
5122
+ /**
5123
+ * The whole-ms delay of a timer for `deadlineMs` armed inside a receipt
5124
+ * scope: counted from the receipt time, as the timer of a symbol fed when
5125
+ * it was received was, less the whole ms since. So it fires on the tick
5126
+ * that timer would have, never later than a delay counted from now, and
5127
+ * its handler still re-arms a timer that fires before its deadline.
5128
+ * Counted from now, a deadline set from the receipt time is rounded up
5129
+ * from another fraction of a ms (BLO-52085).
5130
+ */
5131
+ #receiptTimerDelayMs(deadlineMs: number, minMs: number): number {
5132
+ const receivedAtMs = this.#receiptAtMs!
5133
+ return Math.max(minMs, Math.ceil(deadlineMs - receivedAtMs) - Math.ceil(performance.now() - receivedAtMs))
5134
+ }
5135
+
4860
5136
  /**
4861
5137
  * A held packet admitted late arms from its (earlier) receipt time; that
4862
5138
  * never pulls in a deadline later activity already armed. Outside a
@@ -4948,6 +5224,9 @@ export class FECManager {
4948
5224
  catalogTrackName?: string,
4949
5225
  ): void {
4950
5226
  const key = this.#blockKey(sbn, packetId, catalogTrackName)
5227
+ const span: IsoSpanFailure | undefined = this.#isoSpanKeys.delete(key)
5228
+ ? { length: this.#blockAuthoredLengths.get(key) ?? 0, sources: this.#blockSourceCounts.get(key) ?? 0 }
5229
+ : undefined
4951
5230
  this.#traceLifecycle('failed', sbn, packetId, catalogTrackName)
4952
5231
  this.#clearRepairDeadline(key)
4953
5232
  this.#clearBlockTimer(key)
@@ -4967,7 +5246,8 @@ export class FECManager {
4967
5246
  this.#markPathBlockComplete(sbn, packetId, catalogTrackName)
4968
5247
  this.#fecStats.blocksFailed++
4969
5248
  this.#statsForPacket(packetId).blocksFailed++
4970
- this.onBlockFailed?.(sbn, packetId, catalogTrackName, blockClass)
5249
+ if (span === undefined) this.onBlockFailed?.(sbn, packetId, catalogTrackName, blockClass)
5250
+ else this.onBlockFailed?.(sbn, packetId, catalogTrackName, blockClass, span)
4971
5251
  this.onBlockResolved?.(sbn, packetId, catalogTrackName)
4972
5252
  }
4973
5253
 
@@ -4999,6 +5279,7 @@ export class FECManager {
4999
5279
  this.#blockSourceEsis.delete(key)
5000
5280
  this.#blockRebuiltEsis.delete(key)
5001
5281
  this.#blockAuthoredLengths.delete(key)
5282
+ this.#isoSpanKeys.delete(key)
5002
5283
  this.#blockRepairRsIds.delete(key)
5003
5284
  this.#blockRsbLengths.delete(key)
5004
5285
  this.#clearBlockTimer(key)
@@ -5108,6 +5389,7 @@ export class FECManager {
5108
5389
  this.#blockSourceEsis.clear()
5109
5390
  this.#blockRebuiltEsis.clear()
5110
5391
  this.#blockAuthoredLengths.clear()
5392
+ this.#isoSpanKeys.clear()
5111
5393
  this.#blockRepairRsIds.clear()
5112
5394
  this.#blockRsbLengths.clear()
5113
5395
  this.#heldLayerStates.clear()
package/src/index.ts CHANGED
@@ -260,6 +260,8 @@ export {
260
260
  type FecStats,
261
261
  type MulticastSymbolDelivery,
262
262
  type UndelimitedIsoLoss,
263
+ type ExpiredIsoHole,
264
+ type IsoSpanFailure,
263
265
  type FecBlockResolution,
264
266
  type RepairVia,
265
267
  type FecRepairLayer,