@blockcast/mmt-container 2.2.0-main.e75bbd7576a6 → 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 (60) hide show
  1. package/dist/browser-runtime.js +994 -162
  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 +75 -14
  10. package/dist/fec-manager.d.ts.map +1 -1
  11. package/dist/fec-manager.js +315 -65
  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 +69 -10
  23. package/dist/mmtp-container-parser.d.ts.map +1 -1
  24. package/dist/mmtp-container-parser.js +226 -18
  25. package/dist/mmtp-container-parser.js.map +2 -2
  26. package/dist/mmtp-container-parser.no-modules.js +225 -19
  27. package/dist/mmtp-container-parser.no-modules.js.map +3 -3
  28. package/dist/mmtp-decode-pipeline.js +226 -18
  29. package/dist/mmtp-decode-pipeline.js.map +2 -2
  30. package/dist/mmtp-router.d.ts +20 -9
  31. package/dist/mmtp-router.d.ts.map +1 -1
  32. package/dist/mmtp-router.js +409 -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 +814 -135
  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 +226 -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 +226 -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 +329 -39
  54. package/src/index.ts +2 -0
  55. package/src/iso-block-locator.ts +114 -31
  56. package/src/mmtp-container-parser.ts +257 -24
  57. package/src/mmtp-router.ts +287 -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)
@@ -3173,7 +3249,7 @@ export class FECManager {
3173
3249
  }
3174
3250
  const trackConfig = this.#trackConfigs.get(packetId)
3175
3251
  if (trackConfig === undefined) return undefined
3176
- return Math.max(1, trackConfig.timeoutMs - this.#repairDeadlineMs(trackConfig))
3252
+ return Math.max(1, this.#repairGraceMs(trackConfig, this.#multicastWaitMs(trackConfig)))
3177
3253
  }
3178
3254
 
3179
3255
  /** True once configureTrack/setFecDecoder/setFecWorker configured this packetId. */
@@ -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,80 @@ 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), 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
+
3436
3636
  /**
3437
3637
  * iso-ssbg1-v1 block location: repair symbols it refused, oversize sources,
3438
3638
  * the sources held now for a block no repair has stated yet, and the held
@@ -3864,13 +4064,27 @@ export class FECManager {
3864
4064
 
3865
4065
  client.onCleanupComplete((sbn: number) => {
3866
4066
  if (this.#releasedWorkerClients.has(client)) return
3867
- const cleanupKeys = [...this.#pendingWorkerCleanups].filter((key) => {
4067
+ const owed = [...this.#pendingWorkerCleanups].filter((key) => this.#pendingWorkerCleanupClients.get(key) === client)
4068
+ const cleanupKeys = owed.filter((key) => {
3868
4069
  const block = this.#parseBlockKey(key)
3869
- return block.packetId === packetId &&
3870
- block.sbn === sbn &&
3871
- this.#pendingWorkerCleanupClients.get(key) === client
4070
+ return block.packetId === packetId && block.sbn === sbn
3872
4071
  })
3873
- 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')
3874
4088
  })
3875
4089
 
3876
4090
  client.onError((msg: string, code?: FecWorkerErrorCode) => {
@@ -3952,9 +4166,11 @@ export class FECManager {
3952
4166
  * fail deadline already passed, so failing it is the decision that deadline
3953
4167
  * made. A late ack then finds nothing owed and is a no-op.
3954
4168
  */
3955
- #boundOwedCleanup(key: string, packetId: number, client: FecWorkerClient): void {
4169
+ #boundOwedCleanup(key: string, packetId: number, client: FecWorkerClient, dueAtMs: number): void {
3956
4170
  this.#clearOwedCleanupBound(key)
3957
4171
  const bound = {
4172
+ dueAtMs,
4173
+ askedAtMs: performance.now(),
3958
4174
  handle: setTimeout(() => {
3959
4175
  if (this.#pendingWorkerCleanupBounds.get(key) !== bound) return
3960
4176
  this.#pendingWorkerCleanupBounds.delete(key)
@@ -4518,7 +4734,7 @@ export class FECManager {
4518
4734
  // The blocks whose last SS_ID lies in [previous, ssId): this source
4519
4735
  // passed them and the previous newest one had not.
4520
4736
  for (const block of locator.blocksBetween((previous - 1) >>> 0, ssId)) {
4521
- if (block.span || !serial32Precedes(serial32Add(block.start, block.length - 1), ssId)) continue
4737
+ if (!serial32Precedes(serial32Add(block.start, block.length - 1), ssId)) continue
4522
4738
  this.#shortenUnrecoverableWait(
4523
4739
  this.#blockKey(block.start, packetId, catalogTrackName),
4524
4740
  block.start,
@@ -4537,7 +4753,13 @@ export class FECManager {
4537
4753
  * covers repair symbols paced after its sources, buys it nothing: it waits
4538
4754
  * the late-symbol allowance alone, the multicast wait less the span (the
4539
4755
  * catalog tolerance or the measured delivery gap). Undefined for any other
4540
- * block.
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).
4541
4763
  */
4542
4764
  #lateSourceWaitMs(
4543
4765
  key: string,
@@ -4546,20 +4768,64 @@ export class FECManager {
4546
4768
  catalogTrackName: string | undefined,
4547
4769
  config: ResolvedFECTrackConfig,
4548
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 {
4549
4785
  const authoredLength = this.#blockAuthoredLengths.get(key)
4550
4786
  const rsbLength = this.#blockRsbLengths.get(key)
4551
- if (authoredLength === undefined || rsbLength === undefined) return undefined
4787
+ if (authoredLength === undefined || rsbLength === undefined) return false
4552
4788
  // A source another instance rebuilt (feedRebuiltSourceSymbol) is one the
4553
4789
  // decoder holds: the group need only cover what neither delivered.
4554
4790
  let present = this.#blockSourceCounts.get(key) ?? 0
4555
4791
  const received = this.#blockSourceEsis.get(key)
4556
4792
  for (const esi of this.#blockRebuiltEsis.get(key) ?? []) if (!received?.has(esi)) present++
4557
- if (present + rsbLength >= authoredLength) return undefined
4558
- const newestSource = this.#isoLocators.get(this.#streamKey(packetId, catalogTrackName))?.newestSource
4559
- if (newestSource === undefined || !serial32Precedes(serial32Add(sbn, authoredLength - 1), newestSource)) {
4560
- return undefined
4561
- }
4562
- return this.#multicastWaitMs(config) - config.interleaveDepthMs
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
4563
4829
  }
4564
4830
 
4565
4831
  /**
@@ -4726,7 +4992,8 @@ export class FECManager {
4726
4992
  * the inactivity deadline on each symbol, records the symbol's in-band
4727
4993
  * classification evidence, and marks the block clean when all of its
4728
4994
  * authored source symbols arrived: the SSB_length a repair symbol declared,
4729
- * otherwise K.
4995
+ * otherwise K. `held` marks a source held unplaced since it arrived and
4996
+ * placed only now (see #noteSbnSeen).
4730
4997
  *
4731
4998
  * Throws for a packetId that was never configured, so a classified block
4732
4999
  * never reads the manager-global K or timeout.
@@ -4737,6 +5004,7 @@ export class FECManager {
4737
5004
  catalogTrackName: string | undefined,
4738
5005
  esi: number,
4739
5006
  evidence: SourceSymbolEvidence,
5007
+ held = false,
4740
5008
  ): void {
4741
5009
  const trackConfig = this.#trackConfigs.get(packetId)
4742
5010
  if (trackConfig === undefined) {
@@ -4758,6 +5026,7 @@ export class FECManager {
4758
5026
  'source',
4759
5027
  evidence.packetSequenceNumber,
4760
5028
  Number.isInteger(esi) ? (sbn + esi) >>> 0 : sbn,
5029
+ held,
4761
5030
  )
4762
5031
 
4763
5032
  // A source ESI at or past the block's authored SSB_length contradicts it;
@@ -4837,6 +5106,7 @@ export class FECManager {
4837
5106
  this.#blockSourceEsis.delete(key)
4838
5107
  this.#blockRebuiltEsis.delete(key)
4839
5108
  this.#blockAuthoredLengths.delete(key)
5109
+ this.#isoSpanKeys.delete(key)
4840
5110
  this.#blockRepairRsIds.delete(key)
4841
5111
  this.#blockRsbLengths.delete(key)
4842
5112
  // BLO-3323 L15: track which SBNs reached K source-symbol arrivals so the
@@ -4878,6 +5148,20 @@ export class FECManager {
4878
5148
  return this.#receiptAtMs ?? performance.now()
4879
5149
  }
4880
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
+
4881
5165
  /**
4882
5166
  * A held packet admitted late arms from its (earlier) receipt time; that
4883
5167
  * never pulls in a deadline later activity already armed. Outside a
@@ -4969,6 +5253,9 @@ export class FECManager {
4969
5253
  catalogTrackName?: string,
4970
5254
  ): void {
4971
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
4972
5259
  this.#traceLifecycle('failed', sbn, packetId, catalogTrackName)
4973
5260
  this.#clearRepairDeadline(key)
4974
5261
  this.#clearBlockTimer(key)
@@ -4988,7 +5275,8 @@ export class FECManager {
4988
5275
  this.#markPathBlockComplete(sbn, packetId, catalogTrackName)
4989
5276
  this.#fecStats.blocksFailed++
4990
5277
  this.#statsForPacket(packetId).blocksFailed++
4991
- 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)
4992
5280
  this.onBlockResolved?.(sbn, packetId, catalogTrackName)
4993
5281
  }
4994
5282
 
@@ -5020,6 +5308,7 @@ export class FECManager {
5020
5308
  this.#blockSourceEsis.delete(key)
5021
5309
  this.#blockRebuiltEsis.delete(key)
5022
5310
  this.#blockAuthoredLengths.delete(key)
5311
+ this.#isoSpanKeys.delete(key)
5023
5312
  this.#blockRepairRsIds.delete(key)
5024
5313
  this.#blockRsbLengths.delete(key)
5025
5314
  this.#clearBlockTimer(key)
@@ -5129,6 +5418,7 @@ export class FECManager {
5129
5418
  this.#blockSourceEsis.clear()
5130
5419
  this.#blockRebuiltEsis.clear()
5131
5420
  this.#blockAuthoredLengths.clear()
5421
+ this.#isoSpanKeys.clear()
5132
5422
  this.#blockRepairRsIds.clear()
5133
5423
  this.#blockRsbLengths.clear()
5134
5424
  this.#heldLayerStates.clear()