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

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 (111) hide show
  1. package/README.md +34 -19
  2. package/dist/block-policy.d.ts +88 -16
  3. package/dist/block-policy.d.ts.map +1 -1
  4. package/dist/block-policy.js +164 -56
  5. package/dist/block-policy.js.map +3 -3
  6. package/dist/browser-runtime.js +2064 -388
  7. package/dist/browser-runtime.js.map +3 -3
  8. package/dist/carp-catalog.js.map +2 -2
  9. package/dist/catalog-fec-params.d.ts +18 -2
  10. package/dist/catalog-fec-params.d.ts.map +1 -1
  11. package/dist/catalog-fec-params.js +29 -1
  12. package/dist/catalog-fec-params.js.map +2 -2
  13. package/dist/catalog-network-source.js.map +2 -2
  14. package/dist/cmaf-block-assembler.js +92 -1
  15. package/dist/cmaf-block-assembler.js.map +2 -2
  16. package/dist/cmcd-producer.d.ts +60 -47
  17. package/dist/cmcd-producer.d.ts.map +1 -1
  18. package/dist/cmcd-producer.js +138 -109
  19. package/dist/cmcd-producer.js.map +2 -2
  20. package/dist/codec-utils.d.ts.map +1 -1
  21. package/dist/codec-utils.js +92 -1
  22. package/dist/codec-utils.js.map +2 -2
  23. package/dist/delivery-catalog.js +26 -16
  24. package/dist/delivery-catalog.js.map +2 -2
  25. package/dist/delta-fallback-config.js +5 -1
  26. package/dist/delta-fallback-config.js.map +3 -3
  27. package/dist/fec-manager.d.ts +163 -20
  28. package/dist/fec-manager.d.ts.map +1 -1
  29. package/dist/fec-manager.js +863 -110
  30. package/dist/fec-manager.js.map +3 -3
  31. package/dist/fec-state.js +5 -1
  32. package/dist/fec-state.js.map +2 -2
  33. package/dist/fec-worker-setup.d.ts +1 -1
  34. package/dist/fec-worker-setup.js +1 -1
  35. package/dist/fec-worker-setup.js.map +2 -2
  36. package/dist/held-layer-admission.js.map +2 -2
  37. package/dist/index.d.ts +4 -4
  38. package/dist/index.d.ts.map +1 -1
  39. package/dist/index.js +8 -2
  40. package/dist/index.js.map +2 -2
  41. package/dist/iso-block-locator.d.ts +64 -13
  42. package/dist/iso-block-locator.d.ts.map +1 -1
  43. package/dist/iso-block-locator.js +125 -25
  44. package/dist/iso-block-locator.js.map +2 -2
  45. package/dist/iso-bmff-timing.d.ts +10 -1
  46. package/dist/iso-bmff-timing.d.ts.map +1 -1
  47. package/dist/iso-bmff-timing.js +36 -2
  48. package/dist/iso-bmff-timing.js.map +2 -2
  49. package/dist/media-router.js.map +1 -1
  50. package/dist/mfu-reassembler.js.map +2 -2
  51. package/dist/mmtp-container-parser.d.ts +109 -12
  52. package/dist/mmtp-container-parser.d.ts.map +1 -1
  53. package/dist/mmtp-container-parser.js +312 -27
  54. package/dist/mmtp-container-parser.js.map +2 -2
  55. package/dist/mmtp-container-parser.no-modules.js +333 -32
  56. package/dist/mmtp-container-parser.no-modules.js.map +4 -4
  57. package/dist/mmtp-decode-pipeline.js +311 -27
  58. package/dist/mmtp-decode-pipeline.js.map +2 -2
  59. package/dist/mmtp-fec-session.js.map +2 -2
  60. package/dist/mmtp-router.d.ts +51 -12
  61. package/dist/mmtp-router.d.ts.map +1 -1
  62. package/dist/mmtp-router.js +723 -83
  63. package/dist/mmtp-router.js.map +3 -3
  64. package/dist/multicast-auth-catalog.js.map +2 -2
  65. package/dist/multicast-input.js.map +2 -2
  66. package/dist/multicast-manager.d.ts +31 -3
  67. package/dist/multicast-manager.d.ts.map +1 -1
  68. package/dist/multicast-manager.js +1698 -231
  69. package/dist/multicast-manager.js.map +3 -3
  70. package/dist/multicast-transport.js +28 -18
  71. package/dist/multicast-transport.js.map +2 -2
  72. package/dist/multicast-unwrap.js.map +2 -2
  73. package/dist/no-modules.d.ts +1 -0
  74. package/dist/no-modules.d.ts.map +1 -1
  75. package/dist/no-modules.js +333 -31
  76. package/dist/no-modules.js.map +4 -4
  77. package/dist/repair-fetch.d.ts +38 -8
  78. package/dist/repair-fetch.d.ts.map +1 -1
  79. package/dist/repair-fetch.js +286 -27
  80. package/dist/repair-fetch.js.map +3 -3
  81. package/dist/repair-layer-subscriptions.d.ts +6 -2
  82. package/dist/repair-layer-subscriptions.d.ts.map +1 -1
  83. package/dist/repair-layer-subscriptions.js +6 -3
  84. package/dist/repair-layer-subscriptions.js.map +2 -2
  85. package/dist/signaling.js.map +2 -2
  86. package/dist/timestamp-codec.js.map +2 -2
  87. package/dist/track-session.d.ts +1 -1
  88. package/dist/track-session.d.ts.map +1 -1
  89. package/dist/track-session.js +1 -0
  90. package/dist/track-session.js.map +2 -2
  91. package/dist/wasm-mfu-reassembler.js.map +2 -2
  92. package/dist/wasm-mmtp-container-parser.js +311 -27
  93. package/dist/wasm-mmtp-container-parser.js.map +2 -2
  94. package/package.json +8 -5
  95. package/schemas/catalog.schema.json +4 -0
  96. package/src/block-policy.ts +324 -83
  97. package/src/catalog-fec-params.ts +37 -1
  98. package/src/cmcd-producer.ts +189 -179
  99. package/src/codec-utils.ts +121 -1
  100. package/src/fec-manager.ts +818 -42
  101. package/src/fec-worker-setup.ts +2 -2
  102. package/src/index.ts +11 -2
  103. package/src/iso-block-locator.ts +167 -31
  104. package/src/iso-bmff-timing.ts +40 -1
  105. package/src/mmtp-container-parser.ts +355 -32
  106. package/src/mmtp-router.ts +665 -50
  107. package/src/multicast-manager.ts +145 -16
  108. package/src/no-modules.ts +3 -0
  109. package/src/repair-fetch.ts +366 -50
  110. package/src/repair-layer-subscriptions.ts +13 -5
  111. package/src/track-session.ts +4 -1
@@ -327,6 +327,15 @@ var IsoBlockLocator = class _IsoBlockLocator {
327
327
  this.#heldEvicted = 0;
328
328
  /** Pending holes (see `deferIncompleteGaps`): unstated, sources still held. */
329
329
  this.#holes = [];
330
+ /** Holes the horizon passed while pending: sources held until closed. */
331
+ this.#expired = [];
332
+ /** Of `#expired`, those `takeExpiredHoles` has not handed out yet. */
333
+ this.#newlyExpired = [];
334
+ /**
335
+ * Spans closed from expired holes: behind the horizon, so kept only for
336
+ * `locate` while their sources are fed, until the next retirement.
337
+ */
338
+ this.#closedBehind = [];
330
339
  /**
331
340
  * Keep a gap that is missing a source as a pending hole rather than a span,
332
341
  * so a repair FETCHed for it (draft-ramadan-moq-fec §7.5) can still state
@@ -348,7 +357,11 @@ var IsoBlockLocator = class _IsoBlockLocator {
348
357
  #retentionBlocks;
349
358
  #heldEvicted;
350
359
  #newest;
360
+ #newestSource;
351
361
  #holes;
362
+ #expired;
363
+ #newlyExpired;
364
+ #closedBehind;
352
365
  static #checkedRetention(retentionBlocks) {
353
366
  if (!Number.isSafeInteger(retentionBlocks) || retentionBlocks <= 0) {
354
367
  throw new Error(
@@ -385,6 +398,21 @@ var IsoBlockLocator = class _IsoBlockLocator {
385
398
  observe(position) {
386
399
  if (this.#newest === void 0 || serial32Precedes(this.#newest, position)) this.#newest = position;
387
400
  }
401
+ /**
402
+ * The newest source SS_ID noted with `noteSource` (RFC 1982 order). Unlike
403
+ * `newest`, a repair never moves it: a repair can run ahead of its sources,
404
+ * a source cannot run ahead of the sources sent before it except by
405
+ * reordering.
406
+ */
407
+ get newestSource() {
408
+ return this.#newestSource;
409
+ }
410
+ /** Note a received source's SS_ID; true when it is the newest one yet. */
411
+ noteSource(ssId) {
412
+ if (this.#newestSource !== void 0 && !serial32Precedes(this.#newestSource, ssId)) return false;
413
+ this.#newestSource = ssId;
414
+ return true;
415
+ }
388
416
  /** Held sources evicted by `heldCapacity` (never by the horizon itself). */
389
417
  get heldEvicted() {
390
418
  return this.#heldEvicted;
@@ -403,17 +431,29 @@ var IsoBlockLocator = class _IsoBlockLocator {
403
431
  }
404
432
  /** The known block or span containing `ssId`, with its ESI. */
405
433
  locate(ssId) {
406
- for (let i = this.#blocks.length - 1; i >= 0; i--) {
407
- const block = this.#blocks[i];
408
- if (isoBlockContains(block.start, block.length, ssId)) {
409
- return { block, esi: serial32Distance(block.start, ssId) };
434
+ for (const blocks of [this.#blocks, this.#closedBehind]) {
435
+ for (let i = blocks.length - 1; i >= 0; i--) {
436
+ const block = blocks[i];
437
+ if (isoBlockContains(block.start, block.length, ssId)) {
438
+ return { block, esi: serial32Distance(block.start, ssId) };
439
+ }
410
440
  }
411
441
  }
412
442
  return null;
413
443
  }
414
- /** Number of pending holes. */
444
+ /** Number of holes not closed yet, pending or past the horizon. */
415
445
  get holeCount() {
416
- return this.#holes.length;
446
+ return this.#holes.length + this.#expired.length;
447
+ }
448
+ /**
449
+ * The pending holes the horizon has passed since the last call, in the
450
+ * order it passed them. Each is no longer pending: no repair states a block
451
+ * in it (that repair is `stale`, as for a span the horizon retired) and the
452
+ * release gate does not wait for it. Its owner must close it
453
+ * (`closeHolesWithin`), which turns it into a span that is not kept here.
454
+ */
455
+ takeExpiredHoles() {
456
+ return this.#newlyExpired.splice(0);
417
457
  }
418
458
  /** The stated block (never a span) ending exactly at `end`. */
419
459
  blockEndingAt(end) {
@@ -466,12 +506,42 @@ var IsoBlockLocator = class _IsoBlockLocator {
466
506
  isHeld(ssId) {
467
507
  return this.#held.has(ssId);
468
508
  }
509
+ /** The payload held unplaced for `ssId`, if any. */
510
+ heldSource(ssId) {
511
+ return this.#held.get(ssId);
512
+ }
513
+ /**
514
+ * The SS_IDs strictly between `a` and `b` that no repair has delimited a
515
+ * block of yet: those from the coverage end on, or all of them before the
516
+ * first repair. `missing` is the highest of them no source is held for;
517
+ * absent, every one of them arrived (C.5.2: SS_IDs are consecutive). Null
518
+ * when none lies past coverage. `flagged` is whether a source held among
519
+ * them satisfies `flag` (a RAP fragment, say): the held packets read as
520
+ * fec-multicast-00 Block Classification item 1 reads bounding packets.
521
+ */
522
+ undelimitedBetween(a, b, flag) {
523
+ if (!serial32Precedes(a, b) || serial32Distance(a, b) <= 1) return null;
524
+ const first = a + 1 >>> 0;
525
+ const end = this.#coverageEnd;
526
+ const start = end !== void 0 && serial32Precedes(first, end) ? end : first;
527
+ if (!serial32Precedes(start, b)) return null;
528
+ const length = serial32Distance(start, b);
529
+ const missing = this.#highestMissing(start, length);
530
+ let flagged = false;
531
+ for (let i = 0; flag !== void 0 && !flagged && i < length; i++) {
532
+ const payload = this.#held.get(serial32Add(start, i));
533
+ flagged = payload !== void 0 && flag(payload);
534
+ }
535
+ return missing === void 0 ? { start, flagged } : { start, missing, flagged };
536
+ }
469
537
  /**
470
538
  * Apply one repair symbol's `(SS_Start, SSB_length)`. The first repair of a
471
539
  * block states its range and places every held source in it; later repair
472
- * symbols of the same block change nothing. A range that contradicts a
473
- * known block (a different length, or an overlap with another block or a
474
- * span) fails closed: the repair symbol is dropped and no block changes.
540
+ * symbols of the same block change nothing. A repair whose range is exactly
541
+ * a repair-less span states that span's one block. A range that contradicts
542
+ * a known block (a different length, or an overlap with another block or
543
+ * with part of a span) fails closed: the repair symbol is dropped and no
544
+ * block changes.
475
545
  * A newly stated block retires behind its SS_Start (see the class note).
476
546
  * A block so far past `newest` that stating it would retire the position
477
547
  * `newest` is refused as `ahead`. When `newest` is a placed block's SS_Start
@@ -490,7 +560,16 @@ var IsoBlockLocator = class _IsoBlockLocator {
490
560
  const known = this.blockAt(start);
491
561
  if (known !== void 0) {
492
562
  if (known.span) {
493
- return { status: "overlap", reason: `repair block ${start}+${length} falls inside a repair-less span` };
563
+ if (known.length === length) {
564
+ const block2 = { start, length, span: false };
565
+ this.#blocks[this.#blocks.indexOf(known)] = block2;
566
+ return { status: "placed", block: block2, placed: [] };
567
+ }
568
+ return length < known.length ? {
569
+ status: "overlap",
570
+ reason: `repair block ${start}+${length} is only the first block of repair-less span ${known.start}+${known.length}`,
571
+ ofSpan: known
572
+ } : { status: "overlap", reason: `repair block ${start}+${length} runs past repair-less span ${known.start}+${known.length}` };
494
573
  }
495
574
  if (known.length !== length) {
496
575
  return {
@@ -558,18 +637,28 @@ var IsoBlockLocator = class _IsoBlockLocator {
558
637
  return span === void 0 ? { status: "placed", block, placed } : { status: "placed", block, placed, span };
559
638
  }
560
639
  /**
561
- * Turn every pending hole inside `[start, start + length)` into a
562
- * repair-less span, claiming its held sources: its range FETCH ended, so
563
- * nothing more will state its blocks. Returns the spans, in SS_ID order.
640
+ * Turn every hole inside `[start, start + length)` into a repair-less span,
641
+ * claiming its held sources: its range FETCH ended, so nothing more will
642
+ * state its blocks. A hole the horizon passed (`takeExpiredHoles`) leaves a
643
+ * span that is not kept among the blocks, as the horizon would retire it at
644
+ * once; only `locate` finds it, until the next retirement, so its sources
645
+ * can be fed. Returns the spans, in SS_ID order.
564
646
  */
565
647
  closeHolesWithin(start, length) {
566
648
  const closed = [];
567
- for (const hole of [...this.#holes]) {
568
- if (!isoBlockContains(start, length, hole.start)) continue;
569
- this.#holes.splice(this.#holes.indexOf(hole), 1);
570
- const spanBlock = { start: hole.start, length: hole.length, span: true };
571
- closed.push({ block: spanBlock, placed: this.#claim(spanBlock) });
572
- this.#blocks.push(spanBlock);
649
+ for (const holes of [this.#holes, this.#expired]) {
650
+ for (const hole of [...holes]) {
651
+ if (!isoBlockContains(start, length, hole.start)) continue;
652
+ holes.splice(holes.indexOf(hole), 1);
653
+ const spanBlock = { start: hole.start, length: hole.length, span: true };
654
+ closed.push({ block: spanBlock, placed: this.#claim(spanBlock) });
655
+ if (holes === this.#holes) {
656
+ this.#blocks.push(spanBlock);
657
+ } else {
658
+ this.#closedBehind.push(spanBlock);
659
+ if (this.#newlyExpired.includes(hole)) this.#newlyExpired.splice(this.#newlyExpired.indexOf(hole), 1);
660
+ }
661
+ }
573
662
  }
574
663
  return closed.sort((x, y) => serial32Distance(start, x.block.start) - serial32Distance(start, y.block.start));
575
664
  }
@@ -583,15 +672,17 @@ var IsoBlockLocator = class _IsoBlockLocator {
583
672
  return void 0;
584
673
  }
585
674
  /**
586
- * Retire every block and span whose last SS_ID, and every held source,
587
- * lies more than `retentionBlocks` blocks behind `position`, with the shared
588
- * lower-bound metric (`isoBlocksBehindLowerBound`) every ISO interleave
589
- * horizon uses. Returns the retired block starts.
675
+ * Retire every block and span whose last SS_ID, and every held source
676
+ * outside a hole, lies more than `retentionBlocks` blocks behind
677
+ * `position`, with the shared lower-bound metric (`isoBlocksBehindLowerBound`)
678
+ * every ISO interleave horizon uses; a pending hole so far behind expires
679
+ * (`takeExpiredHoles`). Returns the retired block starts.
590
680
  */
591
681
  retireBehind(position) {
592
682
  this.observe(position);
593
683
  const horizon = this.#retentionBlocks;
594
684
  const retired = [];
685
+ this.#closedBehind.length = 0;
595
686
  for (let i = this.#blocks.length - 1; i >= 0; i--) {
596
687
  const block = this.#blocks[i];
597
688
  const last = serial32Add(block.start, block.length - 1);
@@ -603,10 +694,15 @@ var IsoBlockLocator = class _IsoBlockLocator {
603
694
  for (let i = this.#holes.length - 1; i >= 0; i--) {
604
695
  const hole = this.#holes[i];
605
696
  const last = serial32Add(hole.start, hole.length - 1);
606
- if (isoBlocksBehindLowerBound(last, position, this.#kMax) > horizon) this.#holes.splice(i, 1);
697
+ if (isoBlocksBehindLowerBound(last, position, this.#kMax) <= horizon) continue;
698
+ this.#holes.splice(i, 1);
699
+ this.#expired.push(hole);
700
+ this.#newlyExpired.push(hole);
607
701
  }
608
702
  for (const ssId of [...this.#held.keys()]) {
609
- if (isoBlocksBehindLowerBound(ssId, position, this.#kMax) > horizon) this.#held.delete(ssId);
703
+ if (isoBlocksBehindLowerBound(ssId, position, this.#kMax) > horizon && !this.#holes.some((hole) => isoBlockContains(hole.start, hole.length, ssId)) && !this.#expired.some((hole) => isoBlockContains(hole.start, hole.length, ssId))) {
704
+ this.#held.delete(ssId);
705
+ }
610
706
  }
611
707
  return retired;
612
708
  }
@@ -614,9 +710,13 @@ var IsoBlockLocator = class _IsoBlockLocator {
614
710
  reset() {
615
711
  this.#blocks.length = 0;
616
712
  this.#holes.length = 0;
713
+ this.#expired.length = 0;
714
+ this.#newlyExpired.length = 0;
715
+ this.#closedBehind.length = 0;
617
716
  this.#held.clear();
618
717
  this.#coverageEnd = void 0;
619
718
  this.#newest = void 0;
719
+ this.#newestSource = void 0;
620
720
  }
621
721
  #claim(block) {
622
722
  const placed = [];
@@ -649,7 +749,7 @@ var INACTIVE = Object.freeze({
649
749
  // src/block-policy.ts
650
750
  import { deriveFecInterleaveDepth } from "@blockcast/mmt-fec";
651
751
  function keyframeIntervalMediaTicks(geometry, mediaTimescale) {
652
- if (geometry.trackKind !== "video" || geometry.mode !== "classifying") return null;
752
+ if (geometry.trackKind !== "video" || geometry.keyframeInterval === void 0) return null;
653
753
  if (!Number.isSafeInteger(mediaTimescale) || mediaTimescale <= 0) return null;
654
754
  const { ticks, timescale } = geometry.keyframeInterval;
655
755
  if (!Number.isSafeInteger(ticks) || !Number.isSafeInteger(timescale)) return null;
@@ -658,9 +758,15 @@ function keyframeIntervalMediaTicks(geometry, mediaTimescale) {
658
758
  if (scaled % divisor !== 0n) return null;
659
759
  return scaled / divisor;
660
760
  }
761
+ function keyframeInstantBetween(start, interval, from, to) {
762
+ const k = from >= start ? (from - start) / interval + 1n : 1n;
763
+ return start + k * interval <= to;
764
+ }
661
765
  function compareDataUnitStarts(a, b) {
662
- return a.movieFragment - b.movieFragment || a.idx - b.idx || a.offset - b.offset;
766
+ return a.movieFragment - b.movieFragment | 0 || a.idx - b.idx || a.offset - b.offset;
663
767
  }
768
+ var LATE_RAP_MAX_MPUS_BACK = 64;
769
+ var UNPLACED = { rapNotMpuStart: false };
664
770
  var BlockClassifier = class {
665
771
  #geometry;
666
772
  #mode;
@@ -683,8 +789,24 @@ var BlockClassifier = class {
683
789
  #mpuSbnSpans = /* @__PURE__ */ new Map();
684
790
  #dataUnitStarts = /* @__PURE__ */ new Map();
685
791
  #highestMpu = null;
686
- /** MPU → movie_fragment_sequence_number observed in it. */
687
- #mpuFragments = /* @__PURE__ */ new Map();
792
+ /**
793
+ * P3 layout, kept for the MPUs within the classification horizon and valid
794
+ * within one continuous SBN sequence (`noteSourceDiscontinuity`).
795
+ */
796
+ #mpus = /* @__PURE__ */ new Map();
797
+ /** An MPU of this sequence showed two movie fragments: each frame is one. */
798
+ #fragmentPerFrame = false;
799
+ /** A DU header of this sequence stated sample_number above 1. */
800
+ #multiSampleFragment = false;
801
+ /**
802
+ * The anchor received last, whether or not its MPU is still in the
803
+ * horizon. Each new anchor, and the anchor of every DU header's MPU, is
804
+ * checked against it.
805
+ */
806
+ #lastAnchor = null;
807
+ /** Two anchors were exactly G fragments per MPU apart. */
808
+ #fragmentsProven = false;
809
+ /** The first RAP released of the latest MPU a RAP was released in, recorded in every mode. */
688
810
  #lastRap = null;
689
811
  constructor(geometry) {
690
812
  this.#geometry = geometry;
@@ -714,7 +836,7 @@ var BlockClassifier = class {
714
836
  this.#geometry = geometry;
715
837
  this.#blocks.clear();
716
838
  this.#clearContiguityLedger();
717
- this.#mpuFragments.clear();
839
+ this.#clearMpuLayout();
718
840
  this.#lastRap = null;
719
841
  this.#mpuBoundsRaps = true;
720
842
  if (geometry.trackKind === "video" && geometry.mode === "all-protected") {
@@ -748,11 +870,14 @@ var BlockClassifier = class {
748
870
  /**
749
871
  * The stream's SBN sequence restarted (FECManager source discontinuity:
750
872
  * a publisher restart, or the SS_ID space wrapped). Authored order and SBN
751
- * order are comparable only within one continuous sequence, so the P1
752
- * ledger starts again.
873
+ * order are comparable only within one continuous sequence, and a
874
+ * restarted publisher numbers MPUs and movie fragments again, so the P1
875
+ * ledger and the P3 layout start again.
753
876
  */
754
877
  noteSourceDiscontinuity() {
755
878
  this.#clearContiguityLedger();
879
+ this.#clearMpuLayout();
880
+ this.#lastRap = null;
756
881
  }
757
882
  /** Register a block that exists without evidence (a vanished block). */
758
883
  touch(sbn) {
@@ -771,16 +896,16 @@ var BlockClassifier = class {
771
896
  if (this.#geometry.trackKind === "audio") return [];
772
897
  const changes = [];
773
898
  const block = this.#blockFor(sbn);
899
+ const placement = this.#place(evidence);
774
900
  if (evidence.rap) block.rapSeen = true;
775
901
  if (evidence.mpu !== void 0) block.mpus.add(evidence.mpu);
776
- if (evidence.idx !== void 0) {
777
- block.idxMin = block.idxMin === void 0 ? evidence.idx : Math.min(block.idxMin, evidence.idx);
778
- block.idxMax = block.idxMax === void 0 ? evidence.idx : Math.max(block.idxMax, evidence.idx);
779
- }
780
- if (evidence.rap && evidence.idx !== void 0 && evidence.idx !== 0) {
781
- this.#mpuBoundsRaps = false;
902
+ const { position } = placement;
903
+ if (position !== void 0) {
904
+ block.positionMin = block.positionMin === void 0 ? position : Math.min(block.positionMin, position);
905
+ block.positionMax = block.positionMax === void 0 ? position : Math.max(block.positionMax, position);
782
906
  }
783
- const violation = this.#checkInvariants(sbn, block, evidence);
907
+ if (placement.rapNotMpuStart) this.#mpuBoundsRaps = false;
908
+ const violation = this.#checkInvariants(sbn, block, evidence, placement);
784
909
  if (violation !== null) changes.push(...this.#disable(violation));
785
910
  const next = this.#classify(block);
786
911
  if (next !== block.cls && this.#allowed(block.cls, next)) {
@@ -805,18 +930,35 @@ var BlockClassifier = class {
805
930
  * RAP-bearing block is the one a wrong G calls delta), because the next
806
931
  * released RAP then belongs to m+2. Every MPU is one interval long, so the
807
932
  * span to any later released RAP proves or refutes all MPUs in between.
933
+ *
934
+ * In every mode it keeps each MPU's first released RAP, with its decode
935
+ * time, as the start keyframeFreeBetween measures the catalog interval
936
+ * from, and a second RAP released inside one MPU refutes `mpuBoundsRaps`
937
+ * when it lies off the catalog lattice (not a whole number of intervals
938
+ * after the first; draft-ramadan-moq-mmt-02 4.4.3 binds every keyframe to
939
+ * it): with no repair, released RAPs are the only evidence of where they
940
+ * lie. Without a usable interval, or a decode time on either, any second
941
+ * RAP with another presentation time refutes.
808
942
  */
809
- noteReleasedRap(mpu, presentationTime, mediaTimescale) {
810
- if (this.#geometry.trackKind === "audio" || this.#mode === "all-protected") {
811
- return [];
812
- }
813
- const interval = keyframeIntervalMediaTicks(this.#geometry, mediaTimescale);
814
- if (interval === null) return this.#disable("keyframe-interval-not-media-integral");
943
+ noteReleasedRap(mpu, presentationTime, mediaTimescale, decodeTime) {
944
+ if (this.#geometry.trackKind === "audio") return [];
815
945
  const previous = this.#lastRap;
816
946
  if (previous !== null && previous.mpu === mpu && previous.mediaTimescale === mediaTimescale) {
947
+ if (presentationTime === previous.presentationTime) return [];
948
+ const interval2 = keyframeIntervalMediaTicks(this.#geometry, mediaTimescale);
949
+ if (interval2 === null || decodeTime === void 0 || previous.decodeTime === void 0 || (decodeTime - previous.decodeTime) % interval2 !== 0n) {
950
+ this.#mpuBoundsRaps = false;
951
+ }
817
952
  return [];
818
953
  }
819
- this.#lastRap = { mpu, presentationTime, mediaTimescale };
954
+ if (previous !== null && previous.mediaTimescale === mediaTimescale) {
955
+ const back = previous.mpu - mpu >>> 0;
956
+ if (back !== 0 && back <= LATE_RAP_MAX_MPUS_BACK && presentationTime < previous.presentationTime) return [];
957
+ }
958
+ this.#lastRap = { mpu, presentationTime, mediaTimescale, ...decodeTime === void 0 ? {} : { decodeTime } };
959
+ if (this.#mode === "all-protected") return [];
960
+ const interval = keyframeIntervalMediaTicks(this.#geometry, mediaTimescale);
961
+ if (interval === null) return this.#disable("keyframe-interval-not-media-integral");
820
962
  if (previous === null || mpu < previous.mpu || previous.mediaTimescale !== mediaTimescale) {
821
963
  return [];
822
964
  }
@@ -825,6 +967,22 @@ var BlockClassifier = class {
825
967
  }
826
968
  return this.#disable("rap-spacing-mismatch");
827
969
  }
970
+ /**
971
+ * Whether a gap inside MPU `mpu`, between the decode times `from` and `to`
972
+ * of the frames on either side, holds no keyframe: MPU boundaries still
973
+ * bound RAPs, the catalog states an interval exact in this timescale, and
974
+ * none of its instants after the MPU's first released RAP lies in
975
+ * `(from, to]` (fec-multicast-00 Block Classification item 3). Anything
976
+ * unknown counts as a keyframe.
977
+ */
978
+ keyframeFreeBetween(mpu, from, to, mediaTimescale) {
979
+ const rap = this.#lastRap;
980
+ if (!this.#mpuBoundsRaps || rap?.mpu !== mpu || rap.mediaTimescale !== mediaTimescale || rap.decodeTime === void 0) {
981
+ return false;
982
+ }
983
+ const interval = keyframeIntervalMediaTicks(this.#geometry, mediaTimescale);
984
+ return interval !== null && !keyframeInstantBetween(rap.decodeTime, interval, from, to);
985
+ }
828
986
  #blockFor(sbn) {
829
987
  let block = this.#blocks.get(sbn);
830
988
  if (!block) {
@@ -833,35 +991,89 @@ var BlockClassifier = class {
833
991
  }
834
992
  return block;
835
993
  }
836
- #checkInvariants(sbn, block, evidence) {
994
+ #checkInvariants(sbn, block, evidence, placement) {
837
995
  if (this.#mode === "all-protected" || this.#geometry.trackKind !== "video") return null;
838
996
  const geometry = this.#geometry;
839
997
  if (geometry.mode !== "classifying") return null;
840
- if (evidence.rap && evidence.idx !== void 0 && evidence.idx !== 0) {
841
- return "rap-not-mpu-start";
842
- }
843
- if (evidence.idx !== void 0 && evidence.idx >= geometry.gopFrames) {
998
+ if (placement.rapNotMpuStart) return "rap-not-mpu-start";
999
+ if (placement.position !== void 0 && placement.position >= geometry.gopFrames) {
844
1000
  return "mpu-longer-than-gop";
845
1001
  }
846
- if (block.mpus.size === 1 && block.idxMin !== void 0 && block.idxMax !== void 0 && block.idxMax - block.idxMin >= geometry.interleaveUnits) {
1002
+ if (block.mpus.size === 1 && block.positionMin !== void 0 && block.positionMax !== void 0 && block.positionMax - block.positionMin >= geometry.interleaveUnits) {
847
1003
  return "block-wider-than-interleave";
848
1004
  }
849
- if (evidence.mpu !== void 0 && evidence.movieFragment !== void 0) {
850
- const fragment = this.#mpuFragments.get(evidence.mpu);
851
- if (fragment === void 0) {
852
- this.#mpuFragments.set(evidence.mpu, evidence.movieFragment);
853
- for (const mpu of this.#mpuFragments.keys()) {
854
- if (mpu + 1 < evidence.mpu) this.#mpuFragments.delete(mpu);
855
- }
856
- } else if (fragment !== evidence.movieFragment) {
857
- return "mpu-multiple-fragments";
858
- }
859
- }
1005
+ if (placement.violation !== void 0) return placement.violation;
860
1006
  if (evidence.mpu !== void 0 && this.#contradictsAuthoredOrder(sbn, evidence.mpu, evidence)) {
861
1007
  return "block-not-contiguous";
862
1008
  }
863
1009
  return null;
864
1010
  }
1011
+ /**
1012
+ * The MPU position a source symbol's DU header proves (P3), and what it
1013
+ * refutes. Runs in every mode: mpuBoundsRaps needs its RAP verdict.
1014
+ */
1015
+ #place(evidence) {
1016
+ const { mpu, idx, movieFragment: fragment } = evidence;
1017
+ if (idx === void 0) return UNPLACED;
1018
+ const bySample = { position: idx, rapNotMpuStart: evidence.rap && idx !== 0 };
1019
+ if (mpu === void 0 || fragment === void 0) {
1020
+ return this.#fragmentPerFrame && !this.#multiSampleFragment ? { rapNotMpuStart: bySample.rapNotMpuStart } : bySample;
1021
+ }
1022
+ let layout = this.#mpus.get(mpu);
1023
+ if (layout === void 0) {
1024
+ layout = { low: fragment };
1025
+ this.#mpus.set(mpu, layout);
1026
+ for (const other of this.#mpus.keys()) {
1027
+ if (other + 1 < mpu) this.#mpus.delete(other);
1028
+ }
1029
+ } else if (fragment !== layout.low) {
1030
+ this.#fragmentPerFrame = true;
1031
+ if ((fragment - layout.low | 0) < 0) layout.low = fragment;
1032
+ }
1033
+ if (idx !== 0) this.#multiSampleFragment = true;
1034
+ let arrival;
1035
+ if (evidence.rap && layout.anchor === void 0) {
1036
+ layout.anchor = fragment;
1037
+ arrival = this.#anchorDrift(mpu, fragment);
1038
+ if (arrival === 0 && this.#lastAnchor?.mpu !== mpu) this.#fragmentsProven = true;
1039
+ this.#lastAnchor = { mpu, fragment };
1040
+ }
1041
+ if (!this.#fragmentPerFrame) return bySample;
1042
+ if (this.#multiSampleFragment) return { ...bySample, violation: "mpu-multiple-fragments" };
1043
+ const { anchor } = layout;
1044
+ if (anchor === void 0) return UNPLACED;
1045
+ const drift = arrival ?? this.#anchorDrift(mpu, anchor);
1046
+ const offset = fragment - anchor | 0;
1047
+ return {
1048
+ ...offset === 0 || offset > 0 && this.#fragmentsProven ? { position: offset } : {},
1049
+ rapNotMpuStart: evidence.rap && offset !== 0 || (layout.low - anchor | 0) < 0 || // Fragments count frames: an anchor arriving past the spacing is a
1050
+ // RAP inside its MPU, or an earlier MPU outran G.
1051
+ arrival !== void 0 && arrival > 0 && this.#fragmentsProven,
1052
+ ...drift === void 0 || drift === 0 ? {} : { violation: "rap-spacing-mismatch" }
1053
+ };
1054
+ }
1055
+ /**
1056
+ * How many fragments MPU m's anchor `fragment` lies past the start that
1057
+ * the anchor received last predicts for MPU m: one frame per fragment and
1058
+ * G frames per MPU put the anchors of MPUs m and m' exactly G * (m - m')
1059
+ * fragments apart, modulo 2^32. Undefined without a last anchor, a
1060
+ * cadence, or this layout.
1061
+ */
1062
+ #anchorDrift(mpu, fragment) {
1063
+ const geometry = this.#geometry;
1064
+ const last = this.#lastAnchor;
1065
+ if (last === null || !this.#fragmentPerFrame || this.#multiSampleFragment || geometry.trackKind !== "video" || geometry.mode !== "classifying") {
1066
+ return void 0;
1067
+ }
1068
+ return fragment - last.fragment - geometry.gopFrames * (mpu - last.mpu) | 0;
1069
+ }
1070
+ #clearMpuLayout() {
1071
+ this.#mpus.clear();
1072
+ this.#fragmentPerFrame = false;
1073
+ this.#multiSampleFragment = false;
1074
+ this.#lastAnchor = null;
1075
+ this.#fragmentsProven = false;
1076
+ }
865
1077
  #clearContiguityLedger() {
866
1078
  this.#mpuSbnSpans.clear();
867
1079
  this.#dataUnitStarts.clear();
@@ -961,9 +1173,9 @@ var BlockClassifier = class {
961
1173
  if (this.#mode === "all-protected") return "unknown";
962
1174
  const geometry = this.#geometry;
963
1175
  if (geometry.trackKind !== "video" || geometry.mode !== "classifying") return "unknown";
964
- if (block.mpus.size === 1 && block.idxMin !== void 0 && block.idxMax !== void 0) {
1176
+ if (block.mpus.size === 1 && block.positionMin !== void 0 && block.positionMax !== void 0) {
965
1177
  const span = geometry.interleaveUnits - 1;
966
- if (block.idxMax - span >= 1 && block.idxMin + span <= geometry.gopFrames - 1) {
1178
+ if (block.positionMax - span >= 1 && block.positionMin + span <= geometry.gopFrames - 1) {
967
1179
  return "delta";
968
1180
  }
969
1181
  }
@@ -1081,6 +1293,7 @@ var WORKER_ERROR_LOG_CAP = 5;
1081
1293
  var WORKER_ERROR_KIND_CAP = 32;
1082
1294
  var WORKER_ERROR_OVERFLOW_KIND = "other";
1083
1295
  var STARTUP_REPAIR_WAIT_SOURCE_BOUNDARIES = 2;
1296
+ var NOT_TAKEN = Object.freeze({ taken: false, recovered: null });
1084
1297
  function isMulticastSymbolDelivery(value) {
1085
1298
  return value === "datagram" || value === "ordered-relay";
1086
1299
  }
@@ -1174,10 +1387,18 @@ var FECManager = class _FECManager {
1174
1387
  // 'pending' forever, which would park every later frame of its stream in
1175
1388
  // the in-order release gate. Each entry owns its handle; the callback
1176
1389
  // checks it still owns the key, so a settled or reset key is never touched.
1390
+ /**
1391
+ * Per owed Worker cleanup: the bound on its acknowledgement, when its block
1392
+ * was due, and when its cleanup was asked for (see `onCleanupComplete`).
1393
+ */
1177
1394
  this.#pendingWorkerCleanupBounds = /* @__PURE__ */ new Map();
1178
1395
  // Source symbol count per track-scoped block — marks complete when K symbols arrive.
1179
1396
  this.#blockSourceCounts = /* @__PURE__ */ new Map();
1180
1397
  this.#blockSourceEsis = /* @__PURE__ */ new Map();
1398
+ // Source ESIs per block that another FEC instance rebuilt and fed in
1399
+ // (feedRebuiltSourceSymbol). Kept apart from #blockSourceEsis: they are
1400
+ // not network arrivals and never make a block clean.
1401
+ this.#blockRebuiltEsis = /* @__PURE__ */ new Map();
1181
1402
  // Authored SSB_length per block (ISO/IEC 23008-1 §C.5.3), learned from the
1182
1403
  // first repair symbol. The FFmpeg moq_mmt publisher closes a block every D
1183
1404
  // media units, usually before it holds K symbols; under iso-ssbg1-v1
@@ -1186,10 +1407,14 @@ var FECManager = class _FECManager {
1186
1407
  // SSB_length source arrivals and recoverable from SSB_length received
1187
1408
  // symbols; waiting for K fails every one of them.
1188
1409
  this.#blockAuthoredLengths = /* @__PURE__ */ new Map();
1410
+ // Keys whose authored length is a repair-less span's (#openIsoSpan), until
1411
+ // a repair states the block there or the span resolves.
1412
+ this.#isoSpanKeys = /* @__PURE__ */ new Set();
1189
1413
  this.#ssbLengthConflictCount = 0;
1190
1414
  this.#blockRepairRsIds = /* @__PURE__ */ new Map();
1191
1415
  // RSB_length per block (C.5.3 Repair FEC Payload ID): how many repair
1192
- // symbols its repair group carries. Same lifecycle as #blockRepairRsIds.
1416
+ // symbols its repair group carries; 0 for a repair-less span no repair can
1417
+ // state (#openIsoSpan). Same lifecycle as #blockRepairRsIds.
1193
1418
  this.#blockRsbLengths = /* @__PURE__ */ new Map();
1194
1419
  /** fec-01 6.3.9 anchor state per stream key, one entry per held layer. */
1195
1420
  this.#heldLayerStates = /* @__PURE__ */ new Map();
@@ -1306,6 +1531,10 @@ var FECManager = class _FECManager {
1306
1531
  // ── Keyframe-protected repair policy (block-policy.ts) ──
1307
1532
  // One classifier per physical stream, built from that stream's geometry.
1308
1533
  this.#classifiers = /* @__PURE__ */ new Map();
1534
+ // A stream without catalog timing has no classifier, so the last RAP it
1535
+ // released stands in for one: "an MPU boundary bounds its RAPs" holds until
1536
+ // a second RAP is released inside one MPU (BLO-52085).
1537
+ this.#noSpanRaps = /* @__PURE__ */ new Map();
1309
1538
  // Geometry a stream-scoped configuration gave one stream (streamKey). ABR
1310
1539
  // rungs can share a packetId with a different framerate or GOP, so the
1311
1540
  // packet-level geometry is only the geometry of a packet whose streams
@@ -1338,6 +1567,14 @@ var FECManager = class _FECManager {
1338
1567
  // iso-ssbg1-v1 block location per stream (streamKey). A source belongs to a
1339
1568
  // block only once a repair symbol stated the block's (SS_Start, SSB_length).
1340
1569
  this.#isoLocators = /* @__PURE__ */ new Map();
1570
+ // The ts of the run behind the gap the release gate last asked about, per
1571
+ // stream (isoRepairlessWaitMs), keyed on the frame before the gap: kept, as
1572
+ // the source it came from may retire before the run's deadline, and the
1573
+ // earliest found wins, so a late frame that becomes the head restarts
1574
+ // nothing. It stays until the next run gap: a source discontinuity drops
1575
+ // it, and one older than twice the hold ceiling is not this gap's.
1576
+ // `counted` once the deadline passed (onUndelimitedRunGap).
1577
+ this.#runAnchors = /* @__PURE__ */ new Map();
1341
1578
  this.#deferIsoGaps = false;
1342
1579
  this.#isoRepairRefusedCount = 0;
1343
1580
  this.#isoSourceOversizeCount = 0;
@@ -1373,13 +1610,17 @@ var FECManager = class _FECManager {
1373
1610
  this.#workerErrorCounts = /* @__PURE__ */ new Map();
1374
1611
  this.#decoderUnavailableEvents = 0;
1375
1612
  this.#repairSymbolSizeMismatchCount = 0;
1613
+ this.#unlistedRepairTrackRefusals = 0;
1376
1614
  // Event callbacks — block lifecycle notifications for MmtpRouter wiring.
1377
1615
  // NOTE: For sync WASM path, recovery routing is handled by MmtpRouter from
1378
1616
  // feedSourceSymbol/feedRepairSymbol return values. For Worker path, recovery
1379
1617
  // arrives via onBlockRecovered callback.
1380
1618
  this.onBlockStarted = null;
1381
1619
  this.onBlockComplete = null;
1382
- /** A block reached its fail bound. `blockClass` is its class at failure. */
1620
+ /**
1621
+ * A block reached its fail bound. `blockClass` is its class at failure.
1622
+ * `span` is set when what failed is a repair-less span starting at `sbn`.
1623
+ */
1383
1624
  this.onBlockFailed = null;
1384
1625
  /**
1385
1626
  * Fired once per stream when its Worker decoder stops being routable.
@@ -1421,12 +1662,26 @@ var FECManager = class _FECManager {
1421
1662
  * for it, which it must be.
1422
1663
  */
1423
1664
  this.#onUndelimitedLoss = null;
1665
+ /** Set with the loss handler: true takes an expired hole to close (`closeIsoHoles`). */
1666
+ this.#onIsoHoleExpired = null;
1424
1667
  /** A block every symbol of which vanished was created by SBN evidence. */
1425
1668
  this.onBlockVanished = null;
1426
1669
  /** A block reached a terminal state (completed, recovered, or failed). */
1427
1670
  this.onBlockResolved = null;
1428
1671
  /** A stream's SBN went backwards (or jumped implausibly): the source restarted. */
1429
1672
  this.onSourceDiscontinuity = null;
1673
+ /** The FEC deadline of a run no repair delimited passed, releasing the gap behind it (once per gap). */
1674
+ this.onUndelimitedRunGap = null;
1675
+ /**
1676
+ * The span and tolerance the catalog states for a keyframe overlay
1677
+ * installed over a source track (draft-ramadan-moq-mmt-02 7.4.1: the
1678
+ * overlay instance's own interleaveDepthMs and reorderToleranceMs), or
1679
+ * undefined while none is installed. A stated block only a late source
1680
+ * can complete, and a frame whose gap may hide a RAP behind a span only a
1681
+ * late source can fill, also wait for that overlay's repair
1682
+ * (#overlayLateWaitMs).
1683
+ */
1684
+ this.keyframeOverlayTiming = null;
1430
1685
  // Worker-path callbacks — async recovery and stats from FecWorkerClient.
1431
1686
  /**
1432
1687
  * A recovered source symbol. Return false when admission refused the
@@ -1463,7 +1718,9 @@ var FECManager = class _FECManager {
1463
1718
  #pendingWorkerCleanupBounds;
1464
1719
  #blockSourceCounts;
1465
1720
  #blockSourceEsis;
1721
+ #blockRebuiltEsis;
1466
1722
  #blockAuthoredLengths;
1723
+ #isoSpanKeys;
1467
1724
  #ssbLengthConflictCount;
1468
1725
  #blockRepairRsIds;
1469
1726
  #blockRsbLengths;
@@ -1505,6 +1762,7 @@ var FECManager = class _FECManager {
1505
1762
  #orderedFrontiers;
1506
1763
  #orderedPendingBlocks;
1507
1764
  #classifiers;
1765
+ #noSpanRaps;
1508
1766
  #streamGeometries;
1509
1767
  #failedBlocks;
1510
1768
  #resolvedBlockClasses;
@@ -1514,6 +1772,7 @@ var FECManager = class _FECManager {
1514
1772
  #highestSbn;
1515
1773
  #sourceFrontiers;
1516
1774
  #isoLocators;
1775
+ #runAnchors;
1517
1776
  #deferIsoGaps;
1518
1777
  #isoRepairRefusedCount;
1519
1778
  #isoSourceOversizeCount;
@@ -1528,7 +1787,9 @@ var FECManager = class _FECManager {
1528
1787
  #workerErrorCounts;
1529
1788
  #decoderUnavailableEvents;
1530
1789
  #repairSymbolSizeMismatchCount;
1790
+ #unlistedRepairTrackRefusals;
1531
1791
  #onUndelimitedLoss;
1792
+ #onIsoHoleExpired;
1532
1793
  #encodeTrackName(catalogTrackName) {
1533
1794
  let encoded = this.#encodedTrackNames.get(catalogTrackName);
1534
1795
  if (encoded === void 0) {
@@ -2000,7 +2261,10 @@ var FECManager = class _FECManager {
2000
2261
  timer.handle = null;
2001
2262
  this.#requestRepair(sbn, packetId, catalogTrackName);
2002
2263
  };
2003
- timer.handle = setTimeout(onFire, Math.max(0, deadlineMs - performance.now()));
2264
+ timer.handle = setTimeout(
2265
+ onFire,
2266
+ this.#receiptAtMs === void 0 ? Math.max(0, deadlineMs - performance.now()) : this.#receiptTimerDelayMs(deadlineMs, 0)
2267
+ );
2004
2268
  this.#repairDeadlines.set(key, timer);
2005
2269
  }
2006
2270
  #requestRepair(sbn, packetId, catalogTrackName, settledByOrder = false) {
@@ -2144,8 +2408,14 @@ var FECManager = class _FECManager {
2144
2408
  * Vanished blocks are not enumerated here: their starts are unknown until a
2145
2409
  * repair states them. The SS_IDs between two stated blocks become one
2146
2410
  * repair-less span when the later block's repair arrives (placeIsoRepair).
2411
+ *
2412
+ * A `held` source arrived before its block was known and is fed only now
2413
+ * that a repair, a span or a closed hole placed it: how far its block start
2414
+ * then lies behind the frontier measures how long the receiver held it, not
2415
+ * where the stream was, so only its SS_ID and packet_sequence_number judge
2416
+ * it (BLO-52085).
2147
2417
  */
2148
- #noteSbnSeen(sbn, packetId, catalogTrackName, symbol, packetSequenceNumber, ssId = sbn) {
2418
+ #noteSbnSeen(sbn, packetId, catalogTrackName, symbol, packetSequenceNumber, ssId = sbn, held = false) {
2149
2419
  const streamKey = this.#streamKey(packetId, catalogTrackName);
2150
2420
  const now = performance.now();
2151
2421
  const previous = (symbol === "source" ? this.#sourceFrontiers : this.#highestSbn).get(streamKey);
@@ -2166,6 +2436,8 @@ var FECManager = class _FECManager {
2166
2436
  };
2167
2437
  const discontinuity = () => {
2168
2438
  this.#classifiers.get(streamKey)?.noteSourceDiscontinuity();
2439
+ this.#noSpanRaps.delete(streamKey);
2440
+ this.#runAnchors.delete(streamKey);
2169
2441
  this.#highestSbn.set(streamKey, { sbn, atMs: now });
2170
2442
  this.#sourceFrontiers.set(streamKey, { sbn, atMs: now });
2171
2443
  if (psn === void 0) this.#sourceAnchors.delete(streamKey);
@@ -2183,7 +2455,7 @@ var FECManager = class _FECManager {
2183
2455
  return sent <= 2147483647 && serial32Distance(from.ssId, to.ssId) <= sent + config.k;
2184
2456
  };
2185
2457
  if (!this.#blockPrecedes(previous.sbn, sbn)) {
2186
- if (symbol === "source" && window !== null && this.#blockPrecedes(sbn, previous.sbn) && (this.#blocksBehind(sbn, previous.sbn, packetId) > this.#isoRetentionBlocks(packetId) || psn !== void 0 && anchor !== void 0 && serial32Precedes(ssId, anchor.ssId) && psn !== anchor.psn && !sentInOrder({ ssId, psn }, anchor))) {
2458
+ if (symbol === "source" && window !== null && this.#blockPrecedes(sbn, previous.sbn) && (!held && this.#blocksBehind(sbn, previous.sbn, packetId) > this.#isoRetentionBlocks(packetId) || psn !== void 0 && anchor !== void 0 && serial32Precedes(ssId, anchor.ssId) && psn !== anchor.psn && !sentInOrder({ ssId, psn }, anchor))) {
2187
2459
  discontinuity();
2188
2460
  return;
2189
2461
  }
@@ -2445,17 +2717,21 @@ var FECManager = class _FECManager {
2445
2717
  this.#blockWaitDeadlinesMs.delete(key);
2446
2718
  this.#repairHoldUntilMs.delete(key);
2447
2719
  this.#traceLifecycle("timeout-queued", sbn, packetId, catalogTrackName);
2448
- this.#finalizeExpiredBlock(sbn, packetId, catalogTrackName);
2720
+ this.#finalizeExpiredBlock(sbn, packetId, catalogTrackName, currentDeadlineMs ?? firedAtMs);
2449
2721
  };
2450
- timer.handle = setTimeout(onFire, Math.max(1, Math.ceil(deadlineMs - nowMs)));
2722
+ timer.handle = setTimeout(
2723
+ onFire,
2724
+ this.#receiptAtMs === void 0 ? Math.max(1, Math.ceil(deadlineMs - nowMs)) : this.#receiptTimerDelayMs(deadlineMs, 1)
2725
+ );
2451
2726
  this.#activeBlocks.set(key, timer);
2452
2727
  this.#indexBlockKey(key, sbn, packetId);
2453
2728
  }
2454
2729
  /**
2455
2730
  * Hand a block whose delivery is over to its decoder for finalization: the
2456
- * datagram deadline fired, or the ordered relay moved past it.
2731
+ * datagram deadline fired, or the ordered relay moved past it. `dueAtMs`
2732
+ * is when it was due: its deadline, or now.
2457
2733
  */
2458
- #finalizeExpiredBlock(sbn, packetId, catalogTrackName) {
2734
+ #finalizeExpiredBlock(sbn, packetId, catalogTrackName, dueAtMs = performance.now()) {
2459
2735
  const key = this.#blockKey(sbn, packetId, catalogTrackName);
2460
2736
  if (this.#materializedBlocks.has(key) && !this.#blockPathCounts.has(key)) {
2461
2737
  this.#failBlock(sbn, packetId, catalogTrackName);
@@ -2471,7 +2747,7 @@ var FECManager = class _FECManager {
2471
2747
  if (workerClient) {
2472
2748
  this.#pendingWorkerCleanups.add(key);
2473
2749
  this.#pendingWorkerCleanupClients.set(key, workerClient);
2474
- this.#boundOwedCleanup(key, packetId, workerClient);
2750
+ this.#boundOwedCleanup(key, packetId, workerClient, dueAtMs);
2475
2751
  workerClient.cleanup(sbn);
2476
2752
  } else {
2477
2753
  const decoder = this.#decoderForPacket(packetId, catalogTrackName)?.client;
@@ -2578,6 +2854,9 @@ var FECManager = class _FECManager {
2578
2854
  }
2579
2855
  if (policyChanged) {
2580
2856
  this.#bumpEpoch(packetId);
2857
+ for (const key of [...this.#noSpanRaps.keys()]) {
2858
+ if (this.#belongsToPacket(key, packetId)) this.#noSpanRaps.delete(key);
2859
+ }
2581
2860
  for (const [key, classifier] of [...this.#classifiers]) {
2582
2861
  if (!this.#belongsToPacket(key, packetId)) continue;
2583
2862
  const stream = this.#parseStreamKey(key);
@@ -2678,6 +2957,7 @@ var FECManager = class _FECManager {
2678
2957
  packetId
2679
2958
  })),
2680
2959
  repairSymbolSizeMismatches: this.#repairSymbolSizeMismatchCount,
2960
+ unlistedRepairTrackRefusals: this.#unlistedRepairTrackRefusals,
2681
2961
  repairObjectRejects: [...this.#repairObjectRejects.values()].map((r) => ({ ...r })),
2682
2962
  workerErrors: Object.fromEntries(this.#workerErrorCounts)
2683
2963
  };
@@ -2780,7 +3060,9 @@ var FECManager = class _FECManager {
2780
3060
  }
2781
3061
  resetMap(this.#blockSourceCounts);
2782
3062
  resetMap(this.#blockSourceEsis);
3063
+ resetMap(this.#blockRebuiltEsis);
2783
3064
  resetMap(this.#blockAuthoredLengths);
3065
+ resetSet(this.#isoSpanKeys);
2784
3066
  resetMap(this.#blockRepairRsIds);
2785
3067
  resetMap(this.#blockRsbLengths);
2786
3068
  resetMap(this.#heldLayerStates);
@@ -2811,8 +3093,10 @@ var FECManager = class _FECManager {
2811
3093
  resetMap(this.#highestSbn);
2812
3094
  resetMap(this.#sourceFrontiers);
2813
3095
  resetMap(this.#isoLocators);
3096
+ resetMap(this.#runAnchors);
2814
3097
  resetMap(this.#sourceAnchors);
2815
3098
  this.#classifiers.delete(this.#streamKey(packetId, catalogTrackName));
3099
+ this.#noSpanRaps.delete(this.#streamKey(packetId, catalogTrackName));
2816
3100
  this.#bumpEpoch(packetId);
2817
3101
  for (const [sbn, keys] of this.#blockKeysBySbn) {
2818
3102
  for (const key of keys.keys()) {
@@ -2903,7 +3187,9 @@ var FECManager = class _FECManager {
2903
3187
  dropMap(this.#pendingWorkerCleanupClients);
2904
3188
  dropMap(this.#blockSourceCounts);
2905
3189
  dropMap(this.#blockSourceEsis);
3190
+ dropMap(this.#blockRebuiltEsis);
2906
3191
  dropMap(this.#blockAuthoredLengths);
3192
+ dropSet(this.#isoSpanKeys);
2907
3193
  dropMap(this.#blockRepairRsIds);
2908
3194
  dropMap(this.#blockRsbLengths);
2909
3195
  dropMap(this.#blockPathCounts);
@@ -2917,8 +3203,10 @@ var FECManager = class _FECManager {
2917
3203
  dropMap(this.#highestSbn);
2918
3204
  dropMap(this.#sourceFrontiers);
2919
3205
  dropMap(this.#isoLocators);
3206
+ dropMap(this.#runAnchors);
2920
3207
  dropMap(this.#sourceAnchors);
2921
3208
  dropMap(this.#classifiers);
3209
+ dropMap(this.#noSpanRaps);
2922
3210
  dropMap(this.#streamGeometries);
2923
3211
  dropSet(this.#pendingWorkerCleanups);
2924
3212
  dropSet(this.#completedBlocks);
@@ -3064,6 +3352,9 @@ var FECManager = class _FECManager {
3064
3352
  this.#unindexBlockKey(key);
3065
3353
  }
3066
3354
  }
3355
+ if (isSourceSymbol && source === "multicast") {
3356
+ this.#noteMulticastSource(this.#isoLocatorFor(packetId, catalogTrackName), sbn + esi >>> 0, packetId, catalogTrackName);
3357
+ }
3067
3358
  return true;
3068
3359
  }
3069
3360
  #observeBlockCatalogTrackName(blockKey, sbn, packetId, catalogTrackName) {
@@ -3173,8 +3464,10 @@ var FECManager = class _FECManager {
3173
3464
  }
3174
3465
  /**
3175
3466
  * An open block's accepted sources, distinct repair RS_IDs, authored K'
3176
- * (SSB_length) and RSB_length (each undefined until a repair stated it).
3177
- * Undefined for a block that is not open.
3467
+ * (SSB_length) and RSB_length (each undefined until a repair stated it; a
3468
+ * repair-less span states its length, and RSB_length 0 when it is longer
3469
+ * than K, since no repair can reach it). Undefined for a block that is not
3470
+ * open.
3178
3471
  */
3179
3472
  blockSymbolCounts(sbn, packetId, catalogTrackName) {
3180
3473
  const key = this.#blockKey(sbn, packetId, catalogTrackName);
@@ -3242,8 +3535,9 @@ var FECManager = class _FECManager {
3242
3535
  return true;
3243
3536
  }
3244
3537
  /**
3245
- * Drop a block's repair hold: its FETCH ended without delivering. The block
3246
- * falls back to its multicast wait, which may already have passed.
3538
+ * Drop a block's repair hold: its FETCH ended without delivering, or brought
3539
+ * back only a repair that cannot complete it. The block falls back to its
3540
+ * multicast wait, which may already have passed.
3247
3541
  */
3248
3542
  releaseRepairHold(sbn, packetId, catalogTrackName) {
3249
3543
  const key = this.#blockKey(sbn, packetId, catalogTrackName);
@@ -3289,7 +3583,7 @@ var FECManager = class _FECManager {
3289
3583
  }
3290
3584
  const trackConfig = this.#trackConfigs.get(packetId);
3291
3585
  if (trackConfig === void 0) return void 0;
3292
- return Math.max(1, trackConfig.timeoutMs - this.#repairDeadlineMs(trackConfig));
3586
+ return Math.max(1, this.#repairGraceMs(trackConfig, this.#multicastWaitMs(trackConfig)));
3293
3587
  }
3294
3588
  /** True once configureTrack/setFecDecoder/setFecWorker configured this packetId. */
3295
3589
  // -----------------------------------------------------------------------
@@ -3331,6 +3625,9 @@ var FECManager = class _FECManager {
3331
3625
  for (const key of [...this.#isoLocators.keys()]) {
3332
3626
  if (this.#belongsToPacket(key, packetId)) this.#isoLocators.delete(key);
3333
3627
  }
3628
+ for (const key of [...this.#runAnchors.keys()]) {
3629
+ if (this.#belongsToPacket(key, packetId)) this.#runAnchors.delete(key);
3630
+ }
3334
3631
  }
3335
3632
  /**
3336
3633
  * The iso-ssbg1-v1 locator horizon in blocks: every block the manager can
@@ -3380,7 +3677,9 @@ var FECManager = class _FECManager {
3380
3677
  holdIsoSource(packetId, catalogTrackName, ssId, source) {
3381
3678
  const locator = this.#growingIsoLocatorFor(packetId, catalogTrackName);
3382
3679
  const evictedBefore = locator.heldEvicted;
3383
- const held = locator.hold(ssId, source);
3680
+ const held = locator.hold(ssId, source.arrivedAtMs === void 0 ? { ...source, arrivedAtMs: this.#receiptNow() } : source);
3681
+ this.#settleExpiredIsoHoles(locator, packetId, catalogTrackName);
3682
+ if (held && source.source === "multicast") this.#noteMulticastSource(locator, ssId, packetId, catalogTrackName);
3384
3683
  const evicted = locator.heldEvicted - evictedBefore;
3385
3684
  if (evicted > 0) {
3386
3685
  const before = this.#isoHeldEvictedCount;
@@ -3401,17 +3700,20 @@ var FECManager = class _FECManager {
3401
3700
  * contradicts a known block is refused (counted) and must be dropped.
3402
3701
  */
3403
3702
  placeIsoRepair(packetId, catalogTrackName, ssStart, ssbLength) {
3404
- const placement = this.#growingIsoLocatorFor(packetId, catalogTrackName).placeRepair(ssStart, ssbLength);
3703
+ const locator = this.#growingIsoLocatorFor(packetId, catalogTrackName);
3704
+ const placement = locator.placeRepair(ssStart, ssbLength);
3705
+ this.#settleExpiredIsoHoles(locator, packetId, catalogTrackName);
3405
3706
  if (placement.status !== "placed") {
3406
3707
  this.#isoRepairRefusedCount++;
3407
3708
  if (this.#isoRepairRefusedCount <= 5 || this.#isoRepairRefusedCount % 100 === 0) {
3408
3709
  console.warn(`[FECManager] packetId=${packetId} iso-ssbg1-v1 repair refused (${placement.status}): ${placement.reason}`);
3409
3710
  }
3711
+ if (placement.ofSpan !== void 0) this.releaseRepairHold(placement.ofSpan.start, packetId, catalogTrackName);
3410
3712
  return placement;
3411
3713
  }
3714
+ this.#isoSpanKeys.delete(this.#blockKey(placement.block.start, packetId, catalogTrackName));
3412
3715
  if (placement.span !== void 0) this.#openIsoSpan(placement.span, packetId, catalogTrackName);
3413
3716
  if (placement.hole !== void 0) {
3414
- const locator = this.#isoLocatorFor(packetId, catalogTrackName);
3415
3717
  const hole = placement.hole.block;
3416
3718
  const previous = locator.blockEndingAt(hole.start);
3417
3719
  const loss = {
@@ -3432,17 +3734,29 @@ var FECManager = class _FECManager {
3432
3734
  * `handler`, instead of a repair-less span; null restores spans. Set by the
3433
3735
  * range repair scheduler; applies to every stream from its next gap. The
3434
3736
  * handler and the deferral are one setting, so no hole is ever created with
3435
- * nobody to close it.
3737
+ * nobody to close it. `onExpired` is told of each pending hole the stream
3738
+ * runs past the locator's horizon (BLO-52085): returning true, it closes
3739
+ * the hole (`closeIsoHoles`), so the span it leaves still fails and is
3740
+ * reported; otherwise the hole is discarded with its sources.
3436
3741
  */
3437
- setUndelimitedLossHandler(handler) {
3742
+ setUndelimitedLossHandler(handler, onExpired = null) {
3438
3743
  this.#onUndelimitedLoss = handler;
3744
+ this.#onIsoHoleExpired = handler === null ? null : onExpired;
3439
3745
  this.#deferIsoGaps = handler !== null;
3440
3746
  for (const locator of this.#isoLocators.values()) locator.deferIncompleteGaps = this.#deferIsoGaps;
3441
3747
  }
3748
+ /** Hand each hole the locator's horizon passed to its owner, or discard it (setUndelimitedLossHandler). */
3749
+ #settleExpiredIsoHoles(locator, packetId, catalogTrackName) {
3750
+ for (const hole of locator.takeExpiredHoles()) {
3751
+ const expired = { packetId, catalogTrackName, holeStart: hole.start, holeLength: hole.length };
3752
+ if (this.#onIsoHoleExpired?.(expired) !== true) locator.closeHolesWithin(hole.start, hole.length);
3753
+ }
3754
+ }
3442
3755
  /**
3443
- * The range repair for a hole ended: every hole still pending inside
3444
- * `[start, start + length)` becomes a repair-less span. Returns those spans
3445
- * with the held sources they placed, for the caller to feed (MmtpRouter).
3756
+ * The range repair for a hole ended: every hole inside `[start, start +
3757
+ * length)`, pending or expired, becomes a repair-less span. Returns those
3758
+ * spans with the held sources they placed, for the caller to feed
3759
+ * (MmtpRouter).
3446
3760
  */
3447
3761
  closeIsoHoles(packetId, catalogTrackName, start, length) {
3448
3762
  const locator = this.#isoLocators.get(this.#streamKey(packetId, catalogTrackName));
@@ -3458,17 +3772,37 @@ var FECManager = class _FECManager {
3458
3772
  * its placed sources exactly as on the wire (feedRepairSymbol). A span every
3459
3773
  * SS_ID of which vanished resolves through its deadline like any vanished
3460
3774
  * block.
3775
+ *
3776
+ * A span longer than K holds more than one block (SSB_length <= K), and the
3777
+ * locator states no block inside a span, only one exactly its range
3778
+ * (IsoBlockLocator.placeRepair): no repair, multicast or FETCHed, can ever
3779
+ * reach it. Its repair group is empty, so the repair scheduler FETCHes no
3780
+ * Group of it (group-cannot-complete). Every span still keeps its whole
3781
+ * wait (#lateSourceWaitMs): failing it early released the frames behind it
3782
+ * past a keyframe the overlay was still rebuilding (BLO-52085). The release
3783
+ * gate lets those frames go once only a late source can fill the gap
3784
+ * before them, as long after they arrived as the gap's decision allows
3785
+ * (isoRepairlessWaitMs).
3461
3786
  */
3462
3787
  #openIsoSpan(span, packetId, catalogTrackName) {
3463
3788
  const key = this.#blockKey(span.block.start, packetId, catalogTrackName);
3464
- if (!this.#blockAuthoredLengths.has(key)) this.#blockAuthoredLengths.set(key, span.block.length);
3789
+ if (!this.#blockAuthoredLengths.has(key)) {
3790
+ this.#blockAuthoredLengths.set(key, span.block.length);
3791
+ this.#isoSpanKeys.add(key);
3792
+ if (span.block.length > this.#isoLocatorFor(packetId, catalogTrackName).kMax) this.#blockRsbLengths.set(key, 0);
3793
+ }
3465
3794
  if (span.placed.length === 0) this.#materializeBlock(span.block.start, packetId, catalogTrackName, true);
3466
3795
  }
3467
3796
  /**
3468
3797
  * Resolution of the SS_IDs strictly between two released frames' SS_IDs on
3469
3798
  * an iso-ssbg1-v1 stream, for in-order frame release: null while a block
3470
3799
  * that could hold them is pending or not yet stated by a repair, else the
3471
- * starts of the blocks that failed or vanished.
3800
+ * starts of the blocks that failed or vanished. SS_IDs no repair has
3801
+ * delimited yet that all arrived lost nothing and wait for no repair, only
3802
+ * the late-source allowance after the multicast datagram that clocks them
3803
+ * arrived (#lossFreeRunReadyAtMs), and not even that when none of them came
3804
+ * as one; one missing makes a run that waits as one block
3805
+ * (isoRepairlessWaitMs).
3472
3806
  *
3473
3807
  * `untracked` reads as the release gate reads it (MmtpRouter
3474
3808
  * #createFecReleaseGate): a block holding either released frame's SS_ID
@@ -3481,7 +3815,16 @@ var FECManager = class _FECManager {
3481
3815
  isoSpanResolution(packetId, catalogTrackName, fromSsId, toSsId) {
3482
3816
  if (!serial32Precedes(fromSsId, toSsId) || serial32Distance(fromSsId, toSsId) <= 1) return [];
3483
3817
  const locator = this.#isoLocators.get(this.#streamKey(packetId, catalogTrackName));
3484
- if (!locator || !locator.coversBetween(fromSsId, toSsId)) return null;
3818
+ if (!locator) return null;
3819
+ const run = locator.undelimitedBetween(fromSsId, toSsId);
3820
+ if (run?.missing !== void 0) return null;
3821
+ if (run !== null) {
3822
+ const config = this.#trackConfigs.get(packetId);
3823
+ const readyAtMs = config === void 0 ? void 0 : this.#lossFreeRunReadyAtMs(config, locator, run.start, toSsId);
3824
+ if (readyAtMs !== void 0 && this.#receiptNow() < readyAtMs) return null;
3825
+ }
3826
+ if (locator.coverageEnd === void 0) return [];
3827
+ if (!locator.coversBetween(fromSsId, run?.start ?? toSsId)) return null;
3485
3828
  const highest = this.highestSbn(packetId, catalogTrackName);
3486
3829
  const lost = [];
3487
3830
  for (const block of locator.blocksBetween(fromSsId, toSsId)) {
@@ -3499,6 +3842,148 @@ var FECManager = class _FECManager {
3499
3842
  }
3500
3843
  return lost;
3501
3844
  }
3845
+ /**
3846
+ * When undelimited SS_IDs from `runStart` up to `toSsId` that all arrived
3847
+ * stop holding its frame: the late-source allowance after a multicast
3848
+ * datagram receipt. Until then a frame between them may still complete
3849
+ * from a packet with no SS_ID (an untagged FT=0/FT=1 reordered behind the
3850
+ * next frame), and a repair may delimit them. The receipt is `toSsId`'s
3851
+ * own when it is held from a multicast datagram. Otherwise (its packet
3852
+ * came only as an overlap copy or from a MoQ stream, or was evicted or
3853
+ * retired) it is the newest among those SS_IDs, every one of which is
3854
+ * held and states its path. So the time moves at most once, when the
3855
+ * head's own multicast copy arrives while its frame waits or its held
3856
+ * source is dropped, and never on a later ask.
3857
+ *
3858
+ * Undefined, so the range releases at once, when no SS_ID of it came as a
3859
+ * multicast datagram: a MoQ arrival, an ordered relay's and the frame's
3860
+ * own are no clock for multicast reordering (#lossyReceiptMs). Undefined
3861
+ * too on a track with no catalog span, which states no allowance: the
3862
+ * local timeout is a cleanup bound, and isoRepairlessWaitMs arms no wake
3863
+ * there, so a wait would hold the frame until the next packet. The cost,
3864
+ * on multicast too: an untagged FT=0/FT=1 reordered behind the next frame
3865
+ * of such a track is dropped stale.
3866
+ */
3867
+ #lossFreeRunReadyAtMs(config, locator, runStart, toSsId) {
3868
+ if (!_FECManager.#hasCatalogTiming(config)) return void 0;
3869
+ let clockMs = this.#lossyReceiptMs(locator.heldSource(toSsId));
3870
+ if (clockMs === void 0) {
3871
+ for (let ssId = runStart; ssId !== toSsId; ssId = ssId + 1 >>> 0) {
3872
+ const atMs = this.#lossyReceiptMs(locator.heldSource(ssId));
3873
+ if (atMs !== void 0 && (clockMs === void 0 || atMs > clockMs)) clockMs = atMs;
3874
+ }
3875
+ }
3876
+ if (clockMs === void 0) return void 0;
3877
+ return clockMs + Math.max(0, this.#multicastWaitMs(config) - config.interleaveDepthMs);
3878
+ }
3879
+ /**
3880
+ * Receipt time of a held source that came as a multicast datagram, the
3881
+ * one path that loses and reorders packets, else undefined: a MoQ stream
3882
+ * and an ordered relay deliver in order, so their arrivals clock no
3883
+ * reorder allowance and no FEC deadline. The path is the one the feed
3884
+ * stated (IsoHeldSource.source, MulticastSymbolDelivery), never a guess.
3885
+ */
3886
+ #lossyReceiptMs(held) {
3887
+ return held?.source === "multicast" && this.#multicastSymbolDelivery === "datagram" ? held.arrivedAtMs : void 0;
3888
+ }
3889
+ /**
3890
+ * The release gate's wait, from a frame's arrival, for the SS_IDs strictly
3891
+ * between two released frames' SS_IDs once only a late source can still
3892
+ * fill them, else undefined. That is when every one missing (neither
3893
+ * arrived nor held) lies in a repair-less span longer than K, which no
3894
+ * repair can state a block of (#openIsoSpan), or in a stated block its
3895
+ * own repair group cannot complete every SS_ID of which it misses lies
3896
+ * between them (so the gap's decision covers all it lost), and a multicast
3897
+ * source at or past `toSsId` arrived, so each is lost or reordered behind
3898
+ * it (a MoQ source shows nothing: MoQ streams overtake one another). The
3899
+ * span or block itself keeps its own wait.
3900
+ *
3901
+ * The wait is the late-symbol allowance, unless `rapMayHide`: the gap may
3902
+ * hold a RAP packet an installed keyframe overlay can still rebuild (its
3903
+ * decision stalls, or it ends at a RAP that may lie past a whole MPU).
3904
+ * Then the frame waits as a stated block its group cannot complete does
3905
+ * (#overlayLateWaitMs): the allowance alone with no overlay installed, the
3906
+ * overlay's span plus the larger of the allowance and its tolerance, or,
3907
+ * for an overlay that states no span, the span's whole wait (undefined).
3908
+ * So does `rapNext` (the gap ends at the RAP that opens the next MPU) when
3909
+ * the gap holds a stated block's loss.
3910
+ *
3911
+ * A run of SS_IDs no repair has delimited yet, one of which never arrived,
3912
+ * is one block of its own instead (draft-ramadan-moq-fec-multicast-00,
3913
+ * Receiver Repair Policy): on datagram delivery the frame then waits for
3914
+ * that block's FEC deadline, whatever the flags: ts plus the whole
3915
+ * multicast wait (fec-core-00 9.3), ts the arrival of the lowest held
3916
+ * multicast source above the run, up to the newest (fec-multicast-00,
3917
+ * Deadline Anchoring; under reordering at or after the draft's first
3918
+ * signal, never before). The wait returned counts from `arrivedAtMs`, the
3919
+ * frame's arrival; without it, it is the whole wait. With no such source
3920
+ * held (never arrived, retired, or evicted) the frame keeps the hold
3921
+ * ceiling: a MoQ arrival is no clock for a multicast deadline.
3922
+ */
3923
+ isoRepairlessWaitMs(packetId, catalogTrackName, fromSsId, toSsId, rapMayHide = false, rapNext = false, arrivedAtMs) {
3924
+ const config = this.#trackConfigs.get(packetId);
3925
+ if (config === void 0 || !_FECManager.#hasCatalogTiming(config)) return void 0;
3926
+ const streamKey = this.#streamKey(packetId, catalogTrackName);
3927
+ const locator = this.#isoLocators.get(streamKey);
3928
+ if (locator === void 0) return void 0;
3929
+ const run = locator.undelimitedBetween(fromSsId, toSsId);
3930
+ const readyAtMs = run === null || run.missing !== void 0 ? void 0 : this.#lossFreeRunReadyAtMs(config, locator, run.start, toSsId);
3931
+ if (readyAtMs !== void 0 && this.#receiptNow() < readyAtMs) {
3932
+ return Math.max(0, readyAtMs - (arrivedAtMs ?? this.#receiptNow()));
3933
+ }
3934
+ const newestSource = locator.newestSource;
3935
+ if (newestSource === void 0 || serial32Precedes(newestSource, toSsId)) return void 0;
3936
+ if (run?.missing !== void 0) {
3937
+ if (this.#multicastSymbolDelivery === "ordered-relay" || this.isoSpanResolution(packetId, catalogTrackName, fromSsId, run.start) === null) {
3938
+ return void 0;
3939
+ }
3940
+ const kept = this.#runAnchors.get(streamKey);
3941
+ const anchor = kept?.fromSsId === fromSsId && this.#receiptNow() - kept.tsMs <= 2 * (config.interleaveDepthMs + 2 * this.#blockLifetimeMs(config)) ? kept : void 0;
3942
+ let tsMs = anchor?.tsMs;
3943
+ for (let ssId = run.missing + 1 >>> 0; !serial32Precedes(newestSource, ssId); ssId = ssId + 1 >>> 0) {
3944
+ const atMs = this.#lossyReceiptMs(locator.heldSource(ssId));
3945
+ if (atMs === void 0) continue;
3946
+ if (tsMs === void 0 || atMs < tsMs) tsMs = atMs;
3947
+ break;
3948
+ }
3949
+ if (tsMs === void 0) return void 0;
3950
+ const waitMs2 = this.#multicastWaitMs(config);
3951
+ let counted = anchor?.counted === true;
3952
+ if (!counted && tsMs + waitMs2 <= this.#receiptNow()) {
3953
+ counted = true;
3954
+ this.onUndelimitedRunGap?.(packetId, catalogTrackName);
3955
+ }
3956
+ this.#runAnchors.set(streamKey, { fromSsId, tsMs, counted });
3957
+ return arrivedAtMs === void 0 ? waitMs2 : Math.max(0, tsMs + waitMs2 - arrivedAtMs);
3958
+ }
3959
+ if (!locator.coversBetween(fromSsId, run?.start ?? toSsId)) return void 0;
3960
+ let repairless = false;
3961
+ let stated = false;
3962
+ for (const block of locator.blocksBetween(fromSsId, toSsId)) {
3963
+ const key = this.#blockKey(block.start, packetId, catalogTrackName);
3964
+ if (this.#completedBlocks.has(key)) continue;
3965
+ if (block.span && block.length > locator.kMax) {
3966
+ repairless = true;
3967
+ continue;
3968
+ }
3969
+ const received = this.#blockSourceEsis.get(key);
3970
+ const rebuilt = this.#blockRebuiltEsis.get(key);
3971
+ let missesInGap = false;
3972
+ let missesOutside = false;
3973
+ for (let esi = 0; esi < block.length; esi++) {
3974
+ const ssId = serial32Add(block.start, esi);
3975
+ if (received?.has(esi) || locator.isHeld(ssId)) continue;
3976
+ if (serial32Precedes(fromSsId, ssId) && serial32Precedes(ssId, toSsId)) missesInGap = true;
3977
+ else if (!rebuilt?.has(esi)) missesOutside = true;
3978
+ }
3979
+ if (!missesInGap) continue;
3980
+ if (missesOutside || block.span || !this.#groupCannotComplete(key)) return void 0;
3981
+ repairless = stated = true;
3982
+ }
3983
+ if (!repairless) return void 0;
3984
+ const waitMs = rapMayHide || stated && rapNext ? this.#overlayLateWaitMs(packetId, catalogTrackName, config) : this.#multicastWaitMs(config) - config.interleaveDepthMs;
3985
+ return waitMs === void 0 ? void 0 : Math.max(0, waitMs);
3986
+ }
3502
3987
  /**
3503
3988
  * iso-ssbg1-v1 block location: repair symbols it refused, oversize sources,
3504
3989
  * the sources held now for a block no repair has stated yet, and the held
@@ -3549,11 +4034,34 @@ var FECManager = class _FECManager {
3549
4034
  /**
3550
4035
  * Whether an MPU boundary still bounds this stream's RAPs (see
3551
4036
  * `BlockClassifier.mpuBoundsRaps`). Gap decisions drop MPU evidence once
3552
- * it does not. A stream with no classifier yet has shown nothing against it.
4037
+ * it does not. A stream with no classifier yet has shown nothing against it,
4038
+ * and neither has a stream without catalog timing until it releases two
4039
+ * RAPs inside one MPU.
4040
+ *
4041
+ * With catalog timing and a `gap` inside one MPU, between the authored
4042
+ * timing of the frames on either side, it also takes the catalog keyframe
4043
+ * interval to rule a keyframe out of the gap
4044
+ * (`BlockClassifier.keyframeFreeBetween`, fec-multicast-00 Block
4045
+ * Classification item 3), and no source held between the frames' SS_IDs
4046
+ * may carry the RAP flag or another MPU's number (item 1 read on the held
4047
+ * packets: a RAP the loss cut in two is never released, so the frames
4048
+ * alone cannot show it); anything unknown counts as a keyframe.
3553
4049
  */
3554
- mpuBoundsRapsFor(packetId, catalogTrackName) {
3555
- if (!this.#hasCatalogTimingForPacket(packetId)) return false;
3556
- return this.#classifiers.get(this.#streamKey(packetId, catalogTrackName))?.mpuBoundsRaps ?? true;
4050
+ mpuBoundsRapsFor(packetId, catalogTrackName, gap) {
4051
+ const streamKey = this.#streamKey(packetId, catalogTrackName);
4052
+ if (!this.#hasCatalogTimingForPacket(packetId)) return this.#noSpanRaps.get(streamKey)?.inside !== true;
4053
+ const classifier = this.#classifiers.get(streamKey);
4054
+ if (gap === void 0) return classifier?.mpuBoundsRaps ?? true;
4055
+ const { mpu, from, to, fromSsId, toSsId } = gap;
4056
+ if (fromSsId !== void 0 && toSsId !== void 0) {
4057
+ const run = this.#isoLocators.get(streamKey)?.undelimitedBetween(
4058
+ fromSsId,
4059
+ toSsId,
4060
+ ({ evidence }) => evidence.rap || evidence.mpu !== void 0 && evidence.mpu !== mpu
4061
+ );
4062
+ if (run?.flagged) return false;
4063
+ }
4064
+ return classifier !== void 0 && from !== void 0 && to !== void 0 && from.mediaTimescale === to.mediaTimescale && classifier.keyframeFreeBetween(mpu, from.decodeTime, to.decodeTime, from.mediaTimescale);
3557
4065
  }
3558
4066
  /** A block's class now, or frozen at its resolution. */
3559
4067
  blockClassFor(sbn, packetId, catalogTrackName) {
@@ -3576,17 +4084,27 @@ var FECManager = class _FECManager {
3576
4084
  }
3577
4085
  /**
3578
4086
  * Record a RAP frame released in order, for the classifier's RAP-spacing
3579
- * invariant (consecutive MPUs exactly one keyframe interval apart).
4087
+ * invariant (consecutive MPUs exactly one keyframe interval apart) and,
4088
+ * with its decode time, as the MPU start gap decisions measure the catalog
4089
+ * keyframe interval from (mpuBoundsRapsFor).
3580
4090
  */
3581
- noteReleasedRap(packetId, catalogTrackName, mpu, presentationTime, mediaTimescale) {
3582
- if (!this.#trackConfigs.has(packetId) || !this.#hasCatalogTimingForPacket(packetId)) return;
4091
+ noteReleasedRap(packetId, catalogTrackName, mpu, presentationTime, mediaTimescale, decodeTime) {
4092
+ if (!this.#trackConfigs.has(packetId)) return;
4093
+ if (!this.#hasCatalogTimingForPacket(packetId)) {
4094
+ const streamKey = this.#streamKey(packetId, catalogTrackName);
4095
+ const last = this.#noSpanRaps.get(streamKey);
4096
+ const inside = last?.inside === true || last?.mpu === mpu && last.presentationTime !== presentationTime;
4097
+ this.#noSpanRaps.set(streamKey, { mpu, presentationTime, inside });
4098
+ return;
4099
+ }
3583
4100
  this.#applyClassifierChanges(
3584
4101
  packetId,
3585
4102
  catalogTrackName,
3586
4103
  this.#classifierFor(packetId, catalogTrackName).noteReleasedRap(
3587
4104
  mpu,
3588
4105
  presentationTime,
3589
- mediaTimescale
4106
+ mediaTimescale,
4107
+ decodeTime
3590
4108
  )
3591
4109
  );
3592
4110
  }
@@ -3792,11 +4310,14 @@ var FECManager = class _FECManager {
3792
4310
  });
3793
4311
  client.onCleanupComplete((sbn) => {
3794
4312
  if (this.#releasedWorkerClients.has(client)) return;
3795
- const cleanupKeys = [...this.#pendingWorkerCleanups].filter((key) => {
4313
+ const owed = [...this.#pendingWorkerCleanups].filter((key) => this.#pendingWorkerCleanupClients.get(key) === client);
4314
+ const cleanupKeys = owed.filter((key) => {
3796
4315
  const block = this.#parseBlockKey(key);
3797
- return block.packetId === packetId && block.sbn === sbn && this.#pendingWorkerCleanupClients.get(key) === client;
4316
+ return block.packetId === packetId && block.sbn === sbn;
3798
4317
  });
3799
- for (const key of cleanupKeys) this.#settleOwedCleanup(key, "cleanup-ack");
4318
+ const askedAtMs = Math.max(...cleanupKeys.map((key) => this.#pendingWorkerCleanupBounds.get(key)?.askedAtMs ?? -Infinity));
4319
+ const due = owed.filter((key) => !cleanupKeys.includes(key) && (this.#pendingWorkerCleanupBounds.get(key)?.dueAtMs ?? Infinity) <= askedAtMs);
4320
+ for (const key of [...cleanupKeys, ...due]) this.#settleOwedCleanup(key, "cleanup-ack");
3800
4321
  });
3801
4322
  client.onError((msg, code) => {
3802
4323
  const requestedKind = code ?? msg;
@@ -3866,9 +4387,11 @@ var FECManager = class _FECManager {
3866
4387
  * fail deadline already passed, so failing it is the decision that deadline
3867
4388
  * made. A late ack then finds nothing owed and is a no-op.
3868
4389
  */
3869
- #boundOwedCleanup(key, packetId, client) {
4390
+ #boundOwedCleanup(key, packetId, client, dueAtMs) {
3870
4391
  this.#clearOwedCleanupBound(key);
3871
4392
  const bound = {
4393
+ dueAtMs,
4394
+ askedAtMs: performance.now(),
3872
4395
  handle: setTimeout(() => {
3873
4396
  if (this.#pendingWorkerCleanupBounds.get(key) !== bound) return;
3874
4397
  this.#pendingWorkerCleanupBounds.delete(key);
@@ -3966,7 +4489,15 @@ var FECManager = class _FECManager {
3966
4489
  this.#isoSourceOversizeCount++;
3967
4490
  return null;
3968
4491
  }
3969
- const sbn = located.block.start;
4492
+ return this.#feedDecoderSource(packetId, ssId, symbolData, catalogTrackName, located.block.start);
4493
+ }
4494
+ /**
4495
+ * Hand one placed source symbol to the stream's decoder: its Worker, which
4496
+ * answers through onFrame (null here), or its synchronous decoder, whose
4497
+ * recovered block is returned. `sbn` is the SS_Start of the block a repair
4498
+ * stated for `ssId`.
4499
+ */
4500
+ #feedDecoderSource(packetId, ssId, symbolData, catalogTrackName, sbn) {
3970
4501
  const blockKey = this.#blockKey(sbn, packetId, catalogTrackName);
3971
4502
  const syncBlockOwner = this.#syncDecoderBlockOwners.get(blockKey);
3972
4503
  const workerClient = this.#routableWorkerForPacket(packetId, catalogTrackName);
@@ -3995,6 +4526,74 @@ var FECManager = class _FECManager {
3995
4526
  }
3996
4527
  return recovered;
3997
4528
  }
4529
+ /**
4530
+ * Feed a source symbol of this stream that another FEC instance over the
4531
+ * same packets rebuilt: a keyframe overlay recovery whose base SS_ID the
4532
+ * caller determined (draft-ramadan-moq-mmt-02 7.1, 7.4.4; joint decoding,
4533
+ * BLO-52085). It joins its block's decoder as a received source would, so
4534
+ * it reduces the erasures the block still has to solve, but it is not a
4535
+ * network arrival: no transport accounting, no classification evidence,
4536
+ * no deadline refresh, and it never makes a block clean.
4537
+ *
4538
+ * Only a block a repair stated takes one (a repair-less span has no
4539
+ * decoder block). A block that is terminal, or already holds that source,
4540
+ * does not. The symbol is the exact protected packet, unpadded, as
4541
+ * feedSourceSymbol takes it.
4542
+ *
4543
+ * `recovered` is the block the synchronous decoder completed with it; the
4544
+ * caller routes it and completes the block, as for any recovery. When
4545
+ * every source of the block is then present, received or rebuilt, and no
4546
+ * decoder returned it (a Worker answers asynchronously, and reports such a
4547
+ * block as clean on its own side), the block is completed here as
4548
+ * recovered, with nothing left to route.
4549
+ */
4550
+ feedRebuiltSourceSymbol(packetId, ssId, symbolData, catalogTrackName) {
4551
+ if (!this.#hasBlockProfile(packetId)) return NOT_TAKEN;
4552
+ const located = this.#isoLocatorFor(packetId, catalogTrackName).locate(ssId);
4553
+ if (located === null || located.block.span) return NOT_TAKEN;
4554
+ if (!ssbg1FitsSymbol(symbolData.byteLength, this.#trackConfig(packetId).symbolSize)) return NOT_TAKEN;
4555
+ const sbn = located.block.start;
4556
+ const key = this.#blockKey(sbn, packetId, catalogTrackName);
4557
+ if (this.#completedBlocks.has(key) || this.#blockSourceEsis.get(key)?.has(located.esi)) return NOT_TAKEN;
4558
+ let rebuilt = this.#blockRebuiltEsis.get(key);
4559
+ if (rebuilt?.has(located.esi)) return NOT_TAKEN;
4560
+ if (rebuilt === void 0) {
4561
+ rebuilt = /* @__PURE__ */ new Set();
4562
+ this.#blockRebuiltEsis.set(key, rebuilt);
4563
+ }
4564
+ const config = this.#trackConfigs.get(packetId);
4565
+ const wasShort = config !== void 0 && this.#lateSourceWaitMs(key, sbn, packetId, catalogTrackName, config) !== void 0;
4566
+ rebuilt.add(located.esi);
4567
+ const recovered = this.#feedDecoderSource(packetId, ssId, symbolData, catalogTrackName, sbn);
4568
+ if (recovered === null && !this.#completeWhenSourcesPresent(key, sbn, located.block.length, packetId, catalogTrackName)) {
4569
+ if (wasShort && this.#activeBlocks.has(key) && this.#lateSourceWaitMs(key, sbn, packetId, catalogTrackName, config) === void 0) {
4570
+ this.#armBlockTimeout(sbn, packetId, catalogTrackName, this.#multicastWaitMs(config));
4571
+ }
4572
+ }
4573
+ return { taken: true, recovered };
4574
+ }
4575
+ /**
4576
+ * A block that holds every one of its `length` sources, received or
4577
+ * rebuilt by another instance, with at least one rebuilt, is recovered:
4578
+ * every packet of it was delivered, by its own arrival or by the instance
4579
+ * that rebuilt it, so nothing is routed. Returns whether it completed it.
4580
+ */
4581
+ #completeWhenSourcesPresent(key, sbn, length, packetId, catalogTrackName) {
4582
+ const rebuilt = this.#blockRebuiltEsis.get(key);
4583
+ if (rebuilt === void 0 || this.#completedBlocks.has(key)) return false;
4584
+ let present = rebuilt.size;
4585
+ for (const esi of this.#blockSourceEsis.get(key) ?? []) if (!rebuilt.has(esi)) present++;
4586
+ if (present < length) return false;
4587
+ this.#traceLifecycle("recovered", sbn, packetId, catalogTrackName);
4588
+ this.countRecovery(packetId, sbn, catalogTrackName);
4589
+ this.cancelBlockTimeout(sbn, packetId, catalogTrackName);
4590
+ if (catalogTrackName === void 0) {
4591
+ this.onBlockComplete?.(sbn, packetId);
4592
+ } else {
4593
+ this.onBlockComplete?.(sbn, packetId, catalogTrackName);
4594
+ }
4595
+ return true;
4596
+ }
3998
4597
  /**
3999
4598
  * fec-01 4.5: a reed-solomon repair packet whose SSB_length + RSB_length
4000
4599
  * exceeds 255, or whose RS_ID is not below RSB_length, is discarded with the
@@ -4056,7 +4655,10 @@ var FECManager = class _FECManager {
4056
4655
  this.#layerAnchorHorizon(packetId),
4057
4656
  this.#sourceFrontiers.get(streamKey)?.sbn
4058
4657
  );
4059
- if (verdict === "discard") return false;
4658
+ if (verdict === "discard") {
4659
+ this.#unlistedRepairTrackRefusals++;
4660
+ return false;
4661
+ }
4060
4662
  if (verdict !== void 0) {
4061
4663
  this.#failClosed(streamKey, verdict, packetId, catalogTrackName);
4062
4664
  return false;
@@ -4197,8 +4799,115 @@ var FECManager = class _FECManager {
4197
4799
  this.#completeCleanBlock(blockKey, sbn, packetId, catalogTrackName);
4198
4800
  return null;
4199
4801
  }
4802
+ if (recovered === null) this.#shortenUnrecoverableWait(blockKey, sbn, packetId, catalogTrackName);
4200
4803
  return recovered;
4201
4804
  }
4805
+ /**
4806
+ * A first-arrival multicast source, held or placed. A datagram flow keeps
4807
+ * its sources in send order, so one that moves the stream's newest source
4808
+ * position past a block leaves the sources that block still misses
4809
+ * reordered behind it at best: it shortens the wait of every block it
4810
+ * passed that only a late source can complete. MoQ is left out: its streams
4811
+ * can overtake one another, so a MoQ source shows nothing about the sources
4812
+ * sent before it.
4813
+ */
4814
+ #noteMulticastSource(locator, ssId, packetId, catalogTrackName) {
4815
+ const previous = locator.newestSource;
4816
+ if (!locator.noteSource(ssId) || previous === void 0) return;
4817
+ for (const block of locator.blocksBetween(previous - 1 >>> 0, ssId)) {
4818
+ if (!serial32Precedes(serial32Add(block.start, block.length - 1), ssId)) continue;
4819
+ this.#shortenUnrecoverableWait(
4820
+ this.#blockKey(block.start, packetId, catalogTrackName),
4821
+ block.start,
4822
+ packetId,
4823
+ catalogTrackName
4824
+ );
4825
+ }
4826
+ }
4827
+ /**
4828
+ * The wait of a block that only a late source can complete (BLO-52085): a
4829
+ * repair stated it, its received sources plus every symbol its repair group
4830
+ * carries (RSB_length) are short of K' (the repair scheduler's
4831
+ * group-cannot-complete), and a later source of the stream has arrived, so
4832
+ * a source it misses is lost or reordered behind that one. The span, which
4833
+ * covers repair symbols paced after its sources, buys it nothing: it waits
4834
+ * the late-symbol allowance alone, the multicast wait less the span (the
4835
+ * catalog tolerance or the measured delivery gap). Undefined for any other
4836
+ * block. While a keyframe overlay is installed, the block waits for its
4837
+ * repair too (#overlayLateWaitMs). A repair-less span keeps its whole
4838
+ * wait, even one longer than K whose group is empty (#openIsoSpan): its
4839
+ * resolution is what releases the frames behind it, and failing it early
4840
+ * released them past a keyframe the overlay was still rebuilding, which
4841
+ * then arrived stale. The release gate lets them go instead
4842
+ * (isoRepairlessWaitMs).
4843
+ */
4844
+ #lateSourceWaitMs(key, sbn, packetId, catalogTrackName, config) {
4845
+ if (this.#isoSpanKeys.has(key) || !this.#groupCannotComplete(key)) return void 0;
4846
+ const authoredLength = this.#blockAuthoredLengths.get(key);
4847
+ const newestSource = this.#isoLocators.get(this.#streamKey(packetId, catalogTrackName))?.newestSource;
4848
+ if (newestSource === void 0 || !serial32Precedes(serial32Add(sbn, authoredLength - 1), newestSource)) {
4849
+ return void 0;
4850
+ }
4851
+ return this.#overlayLateWaitMs(packetId, catalogTrackName, config);
4852
+ }
4853
+ /**
4854
+ * A repair stated the block, and its sources plus every symbol its repair
4855
+ * group carries (RSB_length) are short of its SSB_length.
4856
+ */
4857
+ #groupCannotComplete(key) {
4858
+ const authoredLength = this.#blockAuthoredLengths.get(key);
4859
+ const rsbLength = this.#blockRsbLengths.get(key);
4860
+ if (authoredLength === void 0 || rsbLength === void 0) return false;
4861
+ let present = this.#blockSourceCounts.get(key) ?? 0;
4862
+ const received = this.#blockSourceEsis.get(key);
4863
+ for (const esi of this.#blockRebuiltEsis.get(key) ?? []) if (!received?.has(esi)) present++;
4864
+ return present + rsbLength < authoredLength;
4865
+ }
4866
+ /**
4867
+ * How long a loss only a late source or an installed keyframe overlay can
4868
+ * still fill waits once a later source passed it: the late-symbol
4869
+ * allowance alone (the multicast wait less the span) while no overlay is
4870
+ * installed. An installed one (keyframeOverlayTiming) may still rebuild a
4871
+ * RAP packet the loss took: a second FEC instance over the same packets
4872
+ * (draft-ramadan-moq-mmt-02 7.4), whose repair is paced across its own
4873
+ * span, unrelated to the base's. That packet can complete a stated block
4874
+ * (feedRebuiltSourceSymbol), or play behind a span; giving the loss up
4875
+ * first lets the release gate pass the keyframe, whose rebuild then lands
4876
+ * stale. The overlay repair of a packet the loss took goes out within the
4877
+ * overlay's span of that packet (fec-core-00 9.3), and the later source was
4878
+ * sent after it, so the loss waits that span plus the larger of the
4879
+ * allowance and the overlay's own tolerance. An overlay that states no
4880
+ * span (its block may then span keyframes), or one whose wait is not
4881
+ * shorter than the whole multicast wait, leaves the whole wait: undefined.
4882
+ */
4883
+ #overlayLateWaitMs(packetId, catalogTrackName, config) {
4884
+ const waitMs = this.#multicastWaitMs(config);
4885
+ const allowanceMs = waitMs - config.interleaveDepthMs;
4886
+ const overlay = this.keyframeOverlayTiming?.(packetId, catalogTrackName);
4887
+ if (overlay === void 0) return allowanceMs;
4888
+ const overlaySpanMs = overlay.interleaveDepthMs ?? 0;
4889
+ const overlayWaitMs = overlaySpanMs + Math.max(allowanceMs, overlay.reorderToleranceMs ?? 0);
4890
+ return overlaySpanMs > 0 && overlayWaitMs < waitMs ? overlayWaitMs : void 0;
4891
+ }
4892
+ /**
4893
+ * Bring an open block's wait in to its late-source wait, when it has one.
4894
+ * Never later than it is armed: only a source can complete such a block,
4895
+ * so a repair of its group (paced across the span) must not keep it open.
4896
+ */
4897
+ #shortenUnrecoverableWait(key, sbn, packetId, catalogTrackName) {
4898
+ if (!this.#activeBlocks.has(key)) return;
4899
+ const config = this.#trackConfigs.get(packetId);
4900
+ if (config === void 0) return;
4901
+ const waitMs = this.#lateSourceWaitMs(key, sbn, packetId, catalogTrackName, config);
4902
+ if (waitMs === void 0) return;
4903
+ const armedAtMs = this.#blockWaitDeadlinesMs.get(key);
4904
+ this.#armBlockTimeout(
4905
+ sbn,
4906
+ packetId,
4907
+ catalogTrackName,
4908
+ armedAtMs === void 0 ? waitMs : Math.min(waitMs, armedAtMs - this.#receiptNow())
4909
+ );
4910
+ }
4202
4911
  /**
4203
4912
  * Record a block's authored SSB_length from a repair symbol. Returns false,
4204
4913
  * leaving the block unchanged, when the symbol contradicts the block: a
@@ -4296,12 +5005,13 @@ var FECManager = class _FECManager {
4296
5005
  * the inactivity deadline on each symbol, records the symbol's in-band
4297
5006
  * classification evidence, and marks the block clean when all of its
4298
5007
  * authored source symbols arrived: the SSB_length a repair symbol declared,
4299
- * otherwise K.
5008
+ * otherwise K. `held` marks a source held unplaced since it arrived and
5009
+ * placed only now (see #noteSbnSeen).
4300
5010
  *
4301
5011
  * Throws for a packetId that was never configured, so a classified block
4302
5012
  * never reads the manager-global K or timeout.
4303
5013
  */
4304
- trackBlock(sbn, packetId, catalogTrackName, esi, evidence) {
5014
+ trackBlock(sbn, packetId, catalogTrackName, esi, evidence, held = false) {
4305
5015
  const trackConfig = this.#trackConfigs.get(packetId);
4306
5016
  if (trackConfig === void 0) {
4307
5017
  throw new Error(
@@ -4317,7 +5027,8 @@ var FECManager = class _FECManager {
4317
5027
  catalogTrackName,
4318
5028
  "source",
4319
5029
  evidence.packetSequenceNumber,
4320
- Number.isInteger(esi) ? sbn + esi >>> 0 : sbn
5030
+ Number.isInteger(esi) ? sbn + esi >>> 0 : sbn,
5031
+ held
4321
5032
  );
4322
5033
  const authoredLength = this.#blockAuthoredLengths.get(key) ?? k;
4323
5034
  if (Number.isInteger(esi) && esi >= authoredLength) {
@@ -4353,6 +5064,15 @@ var FECManager = class _FECManager {
4353
5064
  this.#completeCleanBlock(key, sbn, packetId, catalogTrackName);
4354
5065
  return;
4355
5066
  }
5067
+ if (this.#blockRebuiltEsis.has(key) && this.#completeWhenSourcesPresent(
5068
+ key,
5069
+ sbn,
5070
+ this.#isoLocatorFor(packetId, catalogTrackName).blockAt(sbn)?.length ?? authoredLength,
5071
+ packetId,
5072
+ catalogTrackName
5073
+ )) {
5074
+ return;
5075
+ }
4356
5076
  this.noteBlockActivity(sbn, packetId, catalogTrackName, true);
4357
5077
  }
4358
5078
  #completeCleanBlock(key, sbn, packetId, catalogTrackName) {
@@ -4361,7 +5081,9 @@ var FECManager = class _FECManager {
4361
5081
  this.cancelBlockTimeout(sbn, packetId, catalogTrackName);
4362
5082
  this.#blockSourceCounts.delete(key);
4363
5083
  this.#blockSourceEsis.delete(key);
5084
+ this.#blockRebuiltEsis.delete(key);
4364
5085
  this.#blockAuthoredLengths.delete(key);
5086
+ this.#isoSpanKeys.delete(key);
4365
5087
  this.#blockRepairRsIds.delete(key);
4366
5088
  this.#blockRsbLengths.delete(key);
4367
5089
  this.#markTerminal(key, sbn, packetId, true);
@@ -4392,6 +5114,19 @@ var FECManager = class _FECManager {
4392
5114
  #receiptNow() {
4393
5115
  return this.#receiptAtMs ?? performance.now();
4394
5116
  }
5117
+ /**
5118
+ * The whole-ms delay of a timer for `deadlineMs` armed inside a receipt
5119
+ * scope: counted from the receipt time, as the timer of a symbol fed when
5120
+ * it was received was, less the whole ms since. So it fires on the tick
5121
+ * that timer would have, never later than a delay counted from now, and
5122
+ * its handler still re-arms a timer that fires before its deadline.
5123
+ * Counted from now, a deadline set from the receipt time is rounded up
5124
+ * from another fraction of a ms (BLO-52085).
5125
+ */
5126
+ #receiptTimerDelayMs(deadlineMs, minMs) {
5127
+ const receivedAtMs = this.#receiptAtMs;
5128
+ return Math.max(minMs, Math.ceil(deadlineMs - receivedAtMs) - Math.ceil(performance.now() - receivedAtMs));
5129
+ }
4395
5130
  /**
4396
5131
  * A held packet admitted late arms from its (earlier) receipt time; that
4397
5132
  * never pulls in a deadline later activity already armed. Outside a
@@ -4427,10 +5162,20 @@ var FECManager = class _FECManager {
4427
5162
  this.#repairDeadlineMs(trackConfig)
4428
5163
  );
4429
5164
  }
4430
- this.#armBlockTimeout(sbn, packetId, catalogTrackName, timeoutMs);
5165
+ if (!sourceProgress && this.#lateSourceWaitMs(key, sbn, packetId, catalogTrackName, trackConfig) !== void 0) {
5166
+ this.#shortenUnrecoverableWait(key, sbn, packetId, catalogTrackName);
5167
+ return;
5168
+ }
5169
+ this.#armBlockTimeout(
5170
+ sbn,
5171
+ packetId,
5172
+ catalogTrackName,
5173
+ this.#lateSourceWaitMs(key, sbn, packetId, catalogTrackName, trackConfig) ?? timeoutMs
5174
+ );
4431
5175
  }
4432
5176
  #failBlock(sbn, packetId, catalogTrackName) {
4433
5177
  const key = this.#blockKey(sbn, packetId, catalogTrackName);
5178
+ const span = this.#isoSpanKeys.delete(key) ? { length: this.#blockAuthoredLengths.get(key) ?? 0, sources: this.#blockSourceCounts.get(key) ?? 0 } : void 0;
4434
5179
  this.#traceLifecycle("failed", sbn, packetId, catalogTrackName);
4435
5180
  this.#clearRepairDeadline(key);
4436
5181
  this.#clearBlockTimer(key);
@@ -4440,6 +5185,7 @@ var FECManager = class _FECManager {
4440
5185
  }
4441
5186
  this.#blockSourceCounts.delete(key);
4442
5187
  this.#blockSourceEsis.delete(key);
5188
+ this.#blockRebuiltEsis.delete(key);
4443
5189
  this.#blockAuthoredLengths.delete(key);
4444
5190
  this.#blockRepairRsIds.delete(key);
4445
5191
  this.#blockRsbLengths.delete(key);
@@ -4449,7 +5195,8 @@ var FECManager = class _FECManager {
4449
5195
  this.#markPathBlockComplete(sbn, packetId, catalogTrackName);
4450
5196
  this.#fecStats.blocksFailed++;
4451
5197
  this.#statsForPacket(packetId).blocksFailed++;
4452
- this.onBlockFailed?.(sbn, packetId, catalogTrackName, blockClass);
5198
+ if (span === void 0) this.onBlockFailed?.(sbn, packetId, catalogTrackName, blockClass);
5199
+ else this.onBlockFailed?.(sbn, packetId, catalogTrackName, blockClass, span);
4453
5200
  this.onBlockResolved?.(sbn, packetId, catalogTrackName);
4454
5201
  }
4455
5202
  /**
@@ -4478,7 +5225,9 @@ var FECManager = class _FECManager {
4478
5225
  this.#retireBlockClass(key, block.sbn, block.packetId, block.catalogTrackName);
4479
5226
  this.#blockSourceCounts.delete(key);
4480
5227
  this.#blockSourceEsis.delete(key);
5228
+ this.#blockRebuiltEsis.delete(key);
4481
5229
  this.#blockAuthoredLengths.delete(key);
5230
+ this.#isoSpanKeys.delete(key);
4482
5231
  this.#blockRepairRsIds.delete(key);
4483
5232
  this.#blockRsbLengths.delete(key);
4484
5233
  this.#clearBlockTimer(key);
@@ -4569,7 +5318,9 @@ var FECManager = class _FECManager {
4569
5318
  this.#blockKeysBySbn.clear();
4570
5319
  this.#blockSourceCounts.clear();
4571
5320
  this.#blockSourceEsis.clear();
5321
+ this.#blockRebuiltEsis.clear();
4572
5322
  this.#blockAuthoredLengths.clear();
5323
+ this.#isoSpanKeys.clear();
4573
5324
  this.#blockRepairRsIds.clear();
4574
5325
  this.#blockRsbLengths.clear();
4575
5326
  this.#heldLayerStates.clear();
@@ -4594,6 +5345,7 @@ var FECManager = class _FECManager {
4594
5345
  this.#blockAnchorsMs.clear();
4595
5346
  this.#blockCatalogTrackNames.clear();
4596
5347
  this.#classifiers.clear();
5348
+ this.#noSpanRaps.clear();
4597
5349
  this.#streamGeometries.clear();
4598
5350
  this.#failedBlocks.clear();
4599
5351
  this.#resolvedBlockClasses.clear();
@@ -4602,6 +5354,7 @@ var FECManager = class _FECManager {
4602
5354
  this.#highestSbn.clear();
4603
5355
  this.#sourceFrontiers.clear();
4604
5356
  this.#isoLocators.clear();
5357
+ this.#runAnchors.clear();
4605
5358
  this.#sourceAnchors.clear();
4606
5359
  this.#trackConfigs.clear();
4607
5360
  this.#fecDecoders.clear();