@blockcast/mmt-container 2.2.0-main.5da4e39e7fb8 → 2.2.0-main.5deae23da0f9

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 (129) hide show
  1. package/README.md +125 -2
  2. package/dist/__tests__/ad-creative-signer.d.ts +11 -0
  3. package/dist/__tests__/ad-creative-signer.d.ts.map +1 -0
  4. package/dist/__tests__/test-utils.d.ts +1 -0
  5. package/dist/__tests__/test-utils.d.ts.map +1 -1
  6. package/dist/ad-beacon.d.ts +39 -0
  7. package/dist/ad-beacon.d.ts.map +1 -0
  8. package/dist/ad-beacon.js +21 -0
  9. package/dist/ad-beacon.js.map +7 -0
  10. package/dist/ad-creative-record.d.ts +65 -0
  11. package/dist/ad-creative-record.d.ts.map +1 -0
  12. package/dist/ad-creative-record.js +2281 -0
  13. package/dist/ad-creative-record.js.map +7 -0
  14. package/dist/ad-creative-timeline.d.ts +103 -0
  15. package/dist/ad-creative-timeline.d.ts.map +1 -0
  16. package/dist/ad-creative-timeline.js +2548 -0
  17. package/dist/ad-creative-timeline.js.map +7 -0
  18. package/dist/block-policy.d.ts +128 -14
  19. package/dist/block-policy.d.ts.map +1 -1
  20. package/dist/block-policy.js +472 -27
  21. package/dist/block-policy.js.map +2 -2
  22. package/dist/browser-runtime.js +1024 -117
  23. package/dist/browser-runtime.js.map +4 -4
  24. package/dist/carp-catalog.d.ts +1 -0
  25. package/dist/carp-catalog.d.ts.map +1 -1
  26. package/dist/carp-catalog.js +34 -0
  27. package/dist/carp-catalog.js.map +4 -4
  28. package/dist/catalog-fec-params.js.map +1 -1
  29. package/dist/catalog-network-source.d.ts +1 -17
  30. package/dist/catalog-network-source.d.ts.map +1 -1
  31. package/dist/catalog-network-source.js +25 -19
  32. package/dist/catalog-network-source.js.map +3 -3
  33. package/dist/cmaf-block-assembler.d.ts +10 -2
  34. package/dist/cmaf-block-assembler.d.ts.map +1 -1
  35. package/dist/cmaf-block-assembler.js +30 -8
  36. package/dist/cmaf-block-assembler.js.map +3 -3
  37. package/dist/codec-utils.d.ts.map +1 -1
  38. package/dist/codec-utils.js +16 -3
  39. package/dist/codec-utils.js.map +2 -2
  40. package/dist/conceal-timeline.d.ts +24 -1
  41. package/dist/conceal-timeline.d.ts.map +1 -1
  42. package/dist/conceal-timeline.js +201 -4
  43. package/dist/conceal-timeline.js.map +3 -3
  44. package/dist/datagram-leg-deadline.d.ts +4 -0
  45. package/dist/datagram-leg-deadline.d.ts.map +1 -1
  46. package/dist/datagram-leg-deadline.js.map +2 -2
  47. package/dist/event-timeline-mmtp.d.ts +17 -0
  48. package/dist/event-timeline-mmtp.d.ts.map +1 -0
  49. package/dist/event-timeline-mmtp.js +3117 -0
  50. package/dist/event-timeline-mmtp.js.map +7 -0
  51. package/dist/fec-manager.d.ts +24 -3
  52. package/dist/fec-manager.d.ts.map +1 -1
  53. package/dist/fec-manager.js +531 -38
  54. package/dist/fec-manager.js.map +2 -2
  55. package/dist/feed-gate.d.ts +114 -0
  56. package/dist/feed-gate.d.ts.map +1 -0
  57. package/dist/feed-gate.js +175 -0
  58. package/dist/feed-gate.js.map +7 -0
  59. package/dist/index.d.ts +3 -2
  60. package/dist/index.d.ts.map +1 -1
  61. package/dist/index.js +13 -1
  62. package/dist/index.js.map +2 -2
  63. package/dist/iso-bmff-timing.d.ts +16 -0
  64. package/dist/iso-bmff-timing.d.ts.map +1 -1
  65. package/dist/iso-bmff-timing.js +28 -0
  66. package/dist/iso-bmff-timing.js.map +2 -2
  67. package/dist/mfu-reassembler.d.ts +2 -0
  68. package/dist/mfu-reassembler.d.ts.map +1 -1
  69. package/dist/mfu-reassembler.js +4 -2
  70. package/dist/mfu-reassembler.js.map +2 -2
  71. package/dist/mmtp-container-parser.d.ts +12 -0
  72. package/dist/mmtp-container-parser.d.ts.map +1 -1
  73. package/dist/mmtp-container-parser.js +73 -3
  74. package/dist/mmtp-container-parser.js.map +2 -2
  75. package/dist/mmtp-container-parser.no-modules.js +103 -25
  76. package/dist/mmtp-container-parser.no-modules.js.map +4 -4
  77. package/dist/mmtp-decode-pipeline.js +73 -3
  78. package/dist/mmtp-decode-pipeline.js.map +2 -2
  79. package/dist/mmtp-router.d.ts +20 -2
  80. package/dist/mmtp-router.d.ts.map +1 -1
  81. package/dist/mmtp-router.js +150 -23
  82. package/dist/mmtp-router.js.map +3 -3
  83. package/dist/multicast-auth-catalog.js.map +1 -1
  84. package/dist/multicast-input.js.map +1 -1
  85. package/dist/multicast-manager.d.ts +23 -2
  86. package/dist/multicast-manager.d.ts.map +1 -1
  87. package/dist/multicast-manager.js +737 -86
  88. package/dist/multicast-manager.js.map +3 -3
  89. package/dist/multicast-transport.js +44 -22
  90. package/dist/multicast-transport.js.map +2 -2
  91. package/dist/no-modules.js +98 -22
  92. package/dist/no-modules.js.map +4 -4
  93. package/dist/repair-fetch.d.ts +10 -3
  94. package/dist/repair-fetch.d.ts.map +1 -1
  95. package/dist/repair-fetch.js +31 -5
  96. package/dist/repair-fetch.js.map +2 -2
  97. package/dist/wasm-mfu-reassembler.js.map +2 -2
  98. package/dist/wasm-mmtp-container-parser.js +73 -3
  99. package/dist/wasm-mmtp-container-parser.js.map +2 -2
  100. package/package.json +17 -5
  101. package/schemas/catalog.schema.json +7 -2
  102. package/src/ad-beacon.ts +51 -0
  103. package/src/ad-creative-record.ts +276 -0
  104. package/src/ad-creative-timeline.ts +410 -0
  105. package/src/block-policy.ts +673 -42
  106. package/src/carp-catalog.ts +9 -0
  107. package/src/catalog-network-source.ts +3 -37
  108. package/src/cmaf-block-assembler.ts +17 -0
  109. package/src/codec-utils.ts +19 -3
  110. package/src/conceal-timeline.ts +60 -5
  111. package/src/datagram-leg-deadline.ts +4 -0
  112. package/src/event-timeline-mmtp.ts +55 -0
  113. package/src/fec-manager.ts +84 -12
  114. package/src/feed-gate.ts +295 -0
  115. package/src/index.ts +21 -0
  116. package/src/iso-bmff-timing.ts +33 -0
  117. package/src/mfu-reassembler.ts +4 -0
  118. package/src/mmtp-container-parser.ts +51 -5
  119. package/src/mmtp-router.ts +116 -4
  120. package/src/multicast-manager.ts +31 -0
  121. package/src/repair-fetch.ts +53 -8
  122. package/test-vectors/catalog/README.md +1 -0
  123. package/test-vectors/catalog/negative/event-timeline-empty-event-type.json +41 -0
  124. package/test-vectors/catalog/positive/event-timeline-ad-creative.json +41 -0
  125. package/test-vectors/catalog/positive/event-timeline-wallclock-no-depends.json +38 -0
  126. package/test-vectors/catalog/positive/multicast-event-timeline.json +81 -0
  127. package/test-vectors/catalog/section-codes.json +1 -0
  128. package/test-vectors/frame-needs-v1.json +295 -0
  129. package/test-vectors/repair-policy-v1.json +36 -3
@@ -782,6 +782,23 @@ var INACTIVE = Object.freeze({
782
782
 
783
783
  // src/block-policy.ts
784
784
  import { deriveFecInterleaveDepth } from "@blockcast/mmt-fec";
785
+ function isPositiveFinite(value) {
786
+ return value !== void 0 && Number.isFinite(value) && value > 0;
787
+ }
788
+ function isPositiveSafeInteger(value) {
789
+ return Number.isSafeInteger(value) && value > 0;
790
+ }
791
+ function mpuRuleCatalogReason(geometry) {
792
+ const interval = geometry.keyframeInterval;
793
+ if (interval === void 0 || !isPositiveSafeInteger(interval.ticks) || !isPositiveSafeInteger(interval.timescale)) {
794
+ return "keyframe-interval-absent";
795
+ }
796
+ const groupMs = geometry.groupDurationMs;
797
+ if (!isPositiveFinite(groupMs)) return "group-duration-absent";
798
+ const { ticks, timescale } = interval;
799
+ const equal = Number.isSafeInteger(groupMs) ? BigInt(ticks) * 1000n === BigInt(groupMs) * BigInt(timescale) : Math.abs(ticks / timescale * 1e3 - groupMs) <= 2 * Number.EPSILON * groupMs;
800
+ return equal ? null : "keyframe-interval-not-group-duration";
801
+ }
785
802
  function keyframeIntervalMediaTicks(geometry, mediaTimescale) {
786
803
  if (geometry.trackKind !== "video" || geometry.keyframeInterval === void 0) return null;
787
804
  if (!Number.isSafeInteger(mediaTimescale) || mediaTimescale <= 0) return null;
@@ -799,6 +816,25 @@ function keyframeInstantBetween(start, interval, from, to) {
799
816
  function compareDataUnitStarts(a, b) {
800
817
  return a.movieFragment - b.movieFragment | 0 || a.idx - b.idx || a.offset - b.offset;
801
818
  }
819
+ function firstStartAtOrAfter(starts, sbn) {
820
+ let lo = 0;
821
+ let hi = starts.length;
822
+ while (lo < hi) {
823
+ const mid = lo + hi >>> 1;
824
+ if (starts[mid].sbn < sbn) lo = mid + 1;
825
+ else hi = mid;
826
+ }
827
+ return lo;
828
+ }
829
+ var ARRIVED_START_MAX = 4096;
830
+ function nextSampleHeader(headers, at) {
831
+ const of = headers[at];
832
+ for (let i = at + 1; i < headers.length; i++) {
833
+ const header = headers[i];
834
+ if (header.movieFragment !== of.movieFragment || header.idx !== of.idx) return header;
835
+ }
836
+ return void 0;
837
+ }
802
838
  var LATE_RAP_MAX_MPUS_BACK = 64;
803
839
  var UNPLACED = { rapNotMpuStart: false };
804
840
  var BlockClassifier = class {
@@ -823,6 +859,50 @@ var BlockClassifier = class {
823
859
  #mpuSbnSpans = /* @__PURE__ */ new Map();
824
860
  #dataUnitStarts = /* @__PURE__ */ new Map();
825
861
  #highestMpu = null;
862
+ /**
863
+ * The DU headers held, per MPU in authored order, for the MPU rule. Kept in
864
+ * every mode (the rule needs no catalog cadence) for the same horizon and
865
+ * the same continuous SBN sequence as the P1 ledger.
866
+ */
867
+ #headers = /* @__PURE__ */ new Map();
868
+ /**
869
+ * The source packets that arrived, per MPU. The MPU rule asks it whether
870
+ * every packet between two held DU headers arrived, and where. Same
871
+ * horizon as the headers.
872
+ */
873
+ #arrived = /* @__PURE__ */ new Map();
874
+ #heldHighestMpu = null;
875
+ /** Two DU headers of this sequence lay in blocks against their authored order: the MPU rule proves nothing. */
876
+ #headerOrderRefuted = false;
877
+ /**
878
+ * A DU header held carried RAP_flag and was not of its MPU's first sample:
879
+ * not the first sample of its movie fragment, or in another movie fragment
880
+ * than the first DU header held of its MPU. This stream puts a RAP inside
881
+ * an MPU, and neither rule proves anything for the rest of the epoch. It is
882
+ * the MPU rule's own reading, in every DU-header layout and of rebuilt
883
+ * headers too. The first case is read on a header of an MPU behind the two
884
+ * kept as well. `mpuBoundsRaps`, which the stall or conceal decision reads,
885
+ * is set as before and not from here.
886
+ */
887
+ #rapInsideMpu = false;
888
+ /** What the catalog lacks for the MPU rule, or null when it states a keyframe interval equal to the group duration. */
889
+ #mpuRuleCatalog = null;
890
+ /**
891
+ * The lowest and highest decode time known in an MPU, for the two MPUs of
892
+ * the highest numbers: those of the samples that held movie fragment
893
+ * metadata times (noteDecodeTimes). Valid within one MPU sequence, as the
894
+ * P1 ledger is; if MPU numbers go back with no source discontinuity, the
895
+ * new ones are not read until then, as their DU headers are not.
896
+ */
897
+ #decodeSpans = /* @__PURE__ */ new Map();
898
+ /**
899
+ * An MPU held a sample one keyframe interval or more after another by
900
+ * decode time, so it lasts longer than the interval. A track of an earlier
901
+ * revision whose interval equals its group duration can hold a further RAP
902
+ * only in such an MPU: neither rule proves anything for the rest of the
903
+ * epoch.
904
+ */
905
+ #mpuOutlastsInterval = false;
826
906
  /**
827
907
  * P3 layout, kept for the MPUs within the classification horizon and valid
828
908
  * within one continuous SBN sequence (`noteSourceDiscontinuity`).
@@ -865,6 +945,20 @@ var BlockClassifier = class {
865
945
  get mpuBoundsRaps() {
866
946
  return this.#mpuBoundsRaps;
867
947
  }
948
+ /**
949
+ * Why the MPU rule classes no block on this track now, or null while it
950
+ * can. A catalog reason is known from the start of the epoch; the others
951
+ * name what the stream showed. A block the rule would have classed reads
952
+ * `unknown` under every one of them, and so does one the window rule would
953
+ * have classed, under every one but `du-headers-out-of-order`.
954
+ */
955
+ get mpuRuleOff() {
956
+ if (this.#mpuRuleCatalog !== null) return this.#mpuRuleCatalog;
957
+ if (this.#rapInsideMpu) return "rap-inside-mpu";
958
+ if (this.#mpuOutlastsInterval) return "mpu-longer-than-keyframe-interval";
959
+ if (!this.#mpuBoundsRaps) return "mpu-bounds-raps-refuted";
960
+ return this.#headerOrderRefuted ? "du-headers-out-of-order" : null;
961
+ }
868
962
  /** Begin a fresh epoch (catalog change or track reset). */
869
963
  reset(geometry = this.#geometry) {
870
964
  this.#geometry = geometry;
@@ -873,6 +967,9 @@ var BlockClassifier = class {
873
967
  this.#clearMpuLayout();
874
968
  this.#lastRap = null;
875
969
  this.#mpuBoundsRaps = true;
970
+ this.#rapInsideMpu = false;
971
+ this.#mpuOutlastsInterval = false;
972
+ this.#mpuRuleCatalog = geometry.trackKind === "video" ? mpuRuleCatalogReason(geometry) : null;
876
973
  if (geometry.trackKind === "video" && geometry.mode === "all-protected") {
877
974
  this.#mode = "all-protected";
878
975
  this.#reason = geometry.reason;
@@ -913,9 +1010,16 @@ var BlockClassifier = class {
913
1010
  this.#clearMpuLayout();
914
1011
  this.#lastRap = null;
915
1012
  }
916
- /** Register a block that exists without evidence (a vanished block). */
1013
+ /**
1014
+ * Register a block that exists without evidence of its own: a vanished
1015
+ * block, or one only repair symbols state. The DU headers already held
1016
+ * around it class it now (the MPU rule); no later header need come between
1017
+ * them to ask again.
1018
+ */
917
1019
  touch(sbn) {
918
- this.#blockFor(sbn);
1020
+ const changes = [];
1021
+ this.#reclassify(sbn, this.#blockFor(sbn), changes);
1022
+ return changes;
919
1023
  }
920
1024
  /** Forget a resolved block. */
921
1025
  forget(sbn) {
@@ -925,8 +1029,15 @@ var BlockClassifier = class {
925
1029
  if (this.#geometry.trackKind === "audio") return "audio";
926
1030
  return this.#blocks.get(sbn)?.cls ?? "unknown";
927
1031
  }
928
- /** Record one first-arrival source symbol's evidence for block `sbn`. */
929
- observe(sbn, evidence) {
1032
+ /**
1033
+ * Record one first-arrival source symbol's evidence for block `sbn`.
1034
+ * `ssId` is its FEC source symbol id (the block's SS_Start plus its ESI,
1035
+ * ISO/IEC 23008-1 C.5.2). The publisher numbers the packets of the FEC
1036
+ * flow with it, one each, so the MPU rule reads from it whether every
1037
+ * packet between two DU headers arrived. Without it the rule's two
1038
+ * exceptions prove nothing.
1039
+ */
1040
+ observe(sbn, evidence, ssId) {
930
1041
  if (this.#geometry.trackKind === "audio") return [];
931
1042
  const changes = [];
932
1043
  const block = this.#blockFor(sbn);
@@ -938,22 +1049,137 @@ var BlockClassifier = class {
938
1049
  block.positionMin = block.positionMin === void 0 ? position : Math.min(block.positionMin, position);
939
1050
  block.positionMax = block.positionMax === void 0 ? position : Math.max(block.positionMax, position);
940
1051
  }
941
- if (placement.rapNotMpuStart) this.#mpuBoundsRaps = false;
1052
+ if (placement.rapNotMpuStart) this.#refuteMpuBounds(changes);
942
1053
  const violation = this.#checkInvariants(sbn, block, evidence, placement);
943
1054
  if (violation !== null) changes.push(...this.#disable(violation));
944
- const next = this.#classify(block);
945
- if (next !== block.cls && this.#allowed(block.cls, next)) {
946
- changes.push({
947
- kind: "reclassified",
948
- sbn,
949
- from: block.cls,
950
- to: next,
951
- evidence: this.#evidenceFor(block, next)
952
- });
953
- block.cls = next;
1055
+ this.#noteArrived(sbn, evidence.mpu, ssId);
1056
+ const bounds = this.#noteHeader(sbn, evidence, position, ssId, changes);
1057
+ this.#reclassify(sbn, block, changes);
1058
+ if (bounds) this.#reclassifyOthers(sbn, changes);
1059
+ return changes;
1060
+ }
1061
+ /**
1062
+ * A first-arrival source symbol placed in a repair-less span (IsoBlockLocator:
1063
+ * the SS_IDs between two stated blocks whose every repair was lost). A span
1064
+ * is no block: it can hold several producer blocks, so nothing block-scoped
1065
+ * is read from it (no record, P1, P2 or MPU-rule bound) and its blocks stay
1066
+ * unknown. What its DU header says of its MPU (P3) still counts (BLO-57323).
1067
+ */
1068
+ observeInSpan(evidence) {
1069
+ if (this.#geometry.trackKind === "audio") return [];
1070
+ const changes = [];
1071
+ const placement = this.#place(evidence);
1072
+ if (placement.rapNotMpuStart) this.#refuteMpuBounds(changes);
1073
+ const geometry = this.#geometry;
1074
+ if (this.#mode === "all-protected" || geometry.mode !== "classifying") return changes;
1075
+ if (placement.rapNotMpuStart) changes.push(...this.#disable("rap-not-mpu-start"));
1076
+ else if (placement.position !== void 0 && placement.position >= geometry.gopFrames) {
1077
+ changes.push(...this.#disable("mpu-longer-than-gop"));
1078
+ } else if (placement.violation !== void 0) changes.push(...this.#disable(placement.violation));
1079
+ return changes;
1080
+ }
1081
+ /**
1082
+ * The DU header of a source packet that a decode rebuilt, in block `sbn`.
1083
+ * It bounds the blocks around that block for the MPU rule as a header that
1084
+ * arrived does: the rule reads the DU headers the receiver holds. The
1085
+ * rebuilt block is whole, so it gets no class and no record, and the
1086
+ * packet is no evidence for P1 or P3, which rest on what arrived: it
1087
+ * supports no class by the window rule, and can only end it. It is not
1088
+ * counted as a packet that arrived either (#arrivedBetween).
1089
+ */
1090
+ observeRecovered(sbn, evidence) {
1091
+ if (this.#geometry.trackKind === "audio") return [];
1092
+ const { mpu, idx, movieFragment } = evidence;
1093
+ if (mpu === void 0 || idx === void 0 || movieFragment === void 0) return [];
1094
+ const changes = [];
1095
+ if (idx !== 0) {
1096
+ this.#multiSampleFragment = true;
1097
+ const geometry = this.#geometry;
1098
+ if (this.#fragmentPerFrame && geometry.trackKind === "video" && geometry.mode === "classifying") {
1099
+ changes.push(...this.#disable("mpu-multiple-fragments"));
1100
+ }
1101
+ }
1102
+ if (this.#noteHeader(sbn, evidence, this.#positionHeld(mpu, idx, movieFragment), void 0, changes)) {
1103
+ this.#reclassifyOthers(sbn, changes);
954
1104
  }
955
1105
  return changes;
956
1106
  }
1107
+ /**
1108
+ * Decode times the receiver can determine: `first` and `last` are those of
1109
+ * the first and the last sample that one held movie fragment's metadata
1110
+ * (FT=1) times, of MPU `mpu`, in ticks of `mediaTimescale`; a `trun` lists
1111
+ * samples in decode order, so `first` is not after `last`. They are read
1112
+ * for one thing, and only on a track the MPU rule applies to: an MPU with
1113
+ * two samples a keyframe interval or more apart lasts longer than the
1114
+ * interval, so its first sample need not be its only RAP. The rule then
1115
+ * stops for the rest of the epoch, the window rule with it, and every delta
1116
+ * class goes (fec-multicast-01 Block Classification). No class comes from
1117
+ * a decode time. The MPU's first sample is never later than the earliest
1118
+ * one known, so the known span is no longer than the real one.
1119
+ */
1120
+ noteDecodeTimes(mpu, first, last, mediaTimescale) {
1121
+ const changes = [];
1122
+ const geometry = this.#geometry;
1123
+ if (geometry.trackKind !== "video" || this.#mpuRuleCatalog !== null || !isPositiveSafeInteger(mediaTimescale)) {
1124
+ return changes;
1125
+ }
1126
+ const held = this.#decodeSpans.get(mpu);
1127
+ const known = held?.mediaTimescale === mediaTimescale ? held : void 0;
1128
+ let late = false;
1129
+ if (held === void 0 && this.#decodeSpans.size >= 2) {
1130
+ const lowest = Math.min(...this.#decodeSpans.keys());
1131
+ late = mpu < lowest;
1132
+ if (!late) this.#decodeSpans.delete(lowest);
1133
+ }
1134
+ const min = known !== void 0 && known.min < first ? known.min : first;
1135
+ const max = known !== void 0 && known.max > last ? known.max : last;
1136
+ if (!late) this.#decodeSpans.set(mpu, { min, max, mediaTimescale });
1137
+ const { ticks, timescale } = geometry.keyframeInterval;
1138
+ if ((max - min) * BigInt(timescale) >= BigInt(ticks) * BigInt(mediaTimescale)) {
1139
+ this.#mpuOutlastsInterval = true;
1140
+ this.#withdraw(changes);
1141
+ }
1142
+ return changes;
1143
+ }
1144
+ /** Ask again for every block held but `own`: a new DU header can bound any of them. */
1145
+ #reclassifyOthers(own, changes) {
1146
+ for (const [sbn, block] of this.#blocks) {
1147
+ if (sbn !== own) this.#reclassify(sbn, block, changes);
1148
+ }
1149
+ }
1150
+ /**
1151
+ * Move delta blocks back to unknown: a premise their class stood on is
1152
+ * gone. After DU headers out of block order, what the window rule still
1153
+ * holds for is classed again as its blocks are seen.
1154
+ */
1155
+ #withdraw(changes) {
1156
+ for (const [sbn, block] of this.#blocks) {
1157
+ if (block.cls !== "delta") continue;
1158
+ block.cls = "unknown";
1159
+ changes.push({ kind: "reclassified", sbn, from: "delta", to: "unknown", evidence: "classifier-disabled" });
1160
+ }
1161
+ }
1162
+ /**
1163
+ * An MPU boundary stopped bounding this stream's RAPs: neither rule proves
1164
+ * anything from now on, and every delta class goes.
1165
+ */
1166
+ #refuteMpuBounds(changes) {
1167
+ if (!this.#mpuBoundsRaps) return;
1168
+ this.#mpuBoundsRaps = false;
1169
+ this.#withdraw(changes);
1170
+ }
1171
+ #reclassify(sbn, block, changes) {
1172
+ const next = this.#classify(block, sbn);
1173
+ if (next === block.cls || !this.#allowed(block.cls, next)) return;
1174
+ changes.push({
1175
+ kind: "reclassified",
1176
+ sbn,
1177
+ from: block.cls,
1178
+ to: next,
1179
+ evidence: this.#evidenceFor(block, next)
1180
+ });
1181
+ block.cls = next;
1182
+ }
957
1183
  /**
958
1184
  * Record a RAP frame released in order. Released MPU-start RAPs of MPUs m
959
1185
  * and m+n must be exactly n keyframe intervals apart in the media
@@ -981,7 +1207,9 @@ var BlockClassifier = class {
981
1207
  if (presentationTime === previous.presentationTime) return [];
982
1208
  const interval2 = keyframeIntervalMediaTicks(this.#geometry, mediaTimescale);
983
1209
  if (interval2 === null || decodeTime === void 0 || previous.decodeTime === void 0 || (decodeTime - previous.decodeTime) % interval2 !== 0n) {
984
- this.#mpuBoundsRaps = false;
1210
+ const changes = [];
1211
+ this.#refuteMpuBounds(changes);
1212
+ return changes;
985
1213
  }
986
1214
  return [];
987
1215
  }
@@ -1086,6 +1314,18 @@ var BlockClassifier = class {
1086
1314
  ...drift === void 0 || drift === 0 ? {} : { violation: "rap-spacing-mismatch" }
1087
1315
  };
1088
1316
  }
1317
+ /**
1318
+ * The MPU position #place gives a DU header, read from the layout the
1319
+ * arrived headers built and recording nothing: where a rebuilt header
1320
+ * stands for the catalog interval's check in the MPU rule.
1321
+ */
1322
+ #positionHeld(mpu, idx, fragment) {
1323
+ if (!this.#fragmentPerFrame || this.#multiSampleFragment) return idx;
1324
+ const anchor = this.#mpus.get(mpu)?.anchor;
1325
+ if (anchor === void 0) return void 0;
1326
+ const offset = fragment - anchor | 0;
1327
+ return offset === 0 || offset > 0 && this.#fragmentsProven ? offset : void 0;
1328
+ }
1089
1329
  /**
1090
1330
  * How many fragments MPU m's anchor `fragment` lies past the start that
1091
1331
  * the anchor received last predicts for MPU m: one frame per fragment and
@@ -1112,6 +1352,11 @@ var BlockClassifier = class {
1112
1352
  this.#mpuSbnSpans.clear();
1113
1353
  this.#dataUnitStarts.clear();
1114
1354
  this.#highestMpu = null;
1355
+ this.#headers.clear();
1356
+ this.#arrived.clear();
1357
+ this.#heldHighestMpu = null;
1358
+ this.#headerOrderRefuted = false;
1359
+ this.#decodeSpans.clear();
1115
1360
  }
1116
1361
  /**
1117
1362
  * P1 check for one source packet of MPU `mpu` in block `sbn`, then record
@@ -1202,18 +1447,218 @@ var BlockClassifier = class {
1202
1447
  }
1203
1448
  return changes;
1204
1449
  }
1205
- #classify(block) {
1450
+ #classify(block, sbn) {
1206
1451
  if (block.rapSeen || block.mpus.size >= 2) return "keyframe";
1207
- if (this.#mode === "all-protected") return "unknown";
1452
+ if (block.cls === "delta") return "delta";
1453
+ if (this.#windowHolds(block) || this.#mpuRuleHolds(block, sbn)) return "delta";
1454
+ return "unknown";
1455
+ }
1456
+ #windowHolds(block) {
1457
+ const off = this.mpuRuleOff;
1458
+ if (this.#mode === "all-protected" || off !== null && off !== "du-headers-out-of-order") return false;
1208
1459
  const geometry = this.#geometry;
1209
- if (geometry.trackKind !== "video" || geometry.mode !== "classifying") return "unknown";
1210
- if (block.mpus.size === 1 && block.positionMin !== void 0 && block.positionMax !== void 0) {
1211
- const span = geometry.interleaveUnits - 1;
1212
- if (block.positionMax - span >= 1 && block.positionMin + span <= geometry.gopFrames - 1) {
1213
- return "delta";
1460
+ if (geometry.trackKind !== "video" || geometry.mode !== "classifying") return false;
1461
+ if (block.mpus.size !== 1 || block.positionMin === void 0 || block.positionMax === void 0) {
1462
+ return false;
1463
+ }
1464
+ const span = geometry.interleaveUnits - 1;
1465
+ return block.positionMax - span >= 1 && block.positionMin + span <= geometry.gopFrames - 1;
1466
+ }
1467
+ /**
1468
+ * Keep the header and arrival ledgers to the two newest MPUs. False for an
1469
+ * MPU behind them, which is not kept: no block still asked lies there, and
1470
+ * the ledgers stay bounded when MPU numbers go back with no source
1471
+ * discontinuity (a restart nothing signalled, or the 2^32 wrap). The rule
1472
+ * is then silent until the next one.
1473
+ */
1474
+ #noteHeldMpu(mpu) {
1475
+ const highest = this.#heldHighestMpu;
1476
+ if (highest !== null && mpu <= highest) return mpu + 1 >= highest;
1477
+ this.#heldHighestMpu = mpu;
1478
+ for (const held of [this.#headers, this.#arrived]) {
1479
+ for (const other of [...held.keys()]) {
1480
+ if (other + 1 < mpu) held.delete(other);
1214
1481
  }
1215
1482
  }
1216
- return "unknown";
1483
+ return true;
1484
+ }
1485
+ /** Record a source packet that arrived as source symbol `ssId`, for #arrivedBetween. */
1486
+ #noteArrived(sbn, mpu, ssId) {
1487
+ if (mpu === void 0 || ssId === void 0 || !this.#noteHeldMpu(mpu)) return;
1488
+ const arrived = this.#arrived.get(mpu);
1489
+ if (arrived === void 0) this.#arrived.set(mpu, { start: /* @__PURE__ */ new Map([[ssId, sbn]]), last: ssId });
1490
+ else {
1491
+ if (arrived.start.size < ARRIVED_START_MAX) arrived.start.set(ssId, sbn);
1492
+ if (ssId > arrived.last) arrived.last = ssId;
1493
+ }
1494
+ }
1495
+ /**
1496
+ * Record a held DU header for the MPU rule; `ssId` is its source symbol id
1497
+ * when it arrived, undefined for one that FEC rebuilt. True when it is new
1498
+ * and in order: the blocks held are then asked again. Blocks follow
1499
+ * authored order (P1), so the first header that lies against it withdraws
1500
+ * every class, and the rule proves nothing for the rest of the sequence.
1501
+ */
1502
+ #noteHeader(sbn, evidence, position, ssId, changes) {
1503
+ const { mpu, idx, movieFragment } = evidence;
1504
+ if (this.#geometry.trackKind !== "video" || mpu === void 0 || idx === void 0 || movieFragment === void 0) {
1505
+ return false;
1506
+ }
1507
+ if (!this.#noteHeldMpu(mpu)) {
1508
+ if (evidence.rap && idx !== 0 && !this.#rapInsideMpu) {
1509
+ this.#rapInsideMpu = true;
1510
+ this.#withdraw(changes);
1511
+ }
1512
+ return false;
1513
+ }
1514
+ const header = {
1515
+ movieFragment,
1516
+ idx,
1517
+ offset: evidence.offset ?? 0,
1518
+ sbn,
1519
+ rap: evidence.rap,
1520
+ ...ssId === void 0 ? {} : { ssId },
1521
+ ...position === void 0 ? {} : { position }
1522
+ };
1523
+ let headers = this.#headers.get(mpu);
1524
+ if (headers === void 0) {
1525
+ headers = [];
1526
+ this.#headers.set(mpu, headers);
1527
+ }
1528
+ let at = 0;
1529
+ let end = headers.length;
1530
+ while (at < end) {
1531
+ const mid = at + end >>> 1;
1532
+ if (compareDataUnitStarts(headers[mid], header) < 0) at = mid + 1;
1533
+ else end = mid;
1534
+ }
1535
+ if (at < headers.length && compareDataUnitStarts(headers[at], header) === 0) return false;
1536
+ headers.splice(at, 0, header);
1537
+ const first = headers[0];
1538
+ if (!this.#rapInsideMpu && (header.rap && header.idx !== 0 || (at === 0 ? headers : [header]).some((held) => held.rap && held.movieFragment !== first.movieFragment))) {
1539
+ this.#rapInsideMpu = true;
1540
+ this.#withdraw(changes);
1541
+ }
1542
+ const before = headers[at - 1]?.sbn;
1543
+ const after = headers[at + 1]?.sbn;
1544
+ if (before !== void 0 && before > sbn || after !== void 0 && after < sbn) {
1545
+ if (!this.#headerOrderRefuted) {
1546
+ this.#headerOrderRefuted = true;
1547
+ this.#withdraw(changes);
1548
+ }
1549
+ return false;
1550
+ }
1551
+ return true;
1552
+ }
1553
+ /**
1554
+ * The MPU rule. L is the DU header held last before the block and U the
1555
+ * one held first after it. Every packet carries its MPU number, and an MPU
1556
+ * holds one RAP: its first sample.
1557
+ *
1558
+ * - R1, L and U in one MPU: the block holds no part of a RAP, provided the
1559
+ * MPU's first sample ended before the block (#firstSampleEndedBefore).
1560
+ * - R2, U in the next MPU: the block may hold the start of that MPU,
1561
+ * unless that start arrived whole (#startArrivedWhole). The block is
1562
+ * then the tail of L's MPU, and R1's proviso decides.
1563
+ * - R3, more than one MPU apart: a whole MPU is gone, its RAP with it.
1564
+ * - R4, no U held yet (or no L): not known. The next DU header asks again.
1565
+ *
1566
+ * It rests on the publisher keeping one RAP to an MPU, which no packet
1567
+ * says. So it classes nothing unless the catalog states a keyframe interval
1568
+ * equal to the group duration, and nothing once the stream showed
1569
+ * otherwise: a RAP inside an MPU (mpuBoundsRaps, or a RAP-flagged DU header
1570
+ * held that is not of its MPU's first sample: #noteHeader), an MPU longer than the
1571
+ * keyframe interval by the decode times known (noteDecodeTimes), or DU
1572
+ * headers against the block order. `mpuRuleOff` names which. `dep_counter`
1573
+ * is not read.
1574
+ */
1575
+ #mpuRuleHolds(block, sbn) {
1576
+ if (this.mpuRuleOff !== null) return false;
1577
+ let lower;
1578
+ let upper;
1579
+ let lowerMpu = 0;
1580
+ let upperMpu = 0;
1581
+ let lowerAt = 0;
1582
+ let upperAt = 0;
1583
+ for (const [mpu, headers] of this.#headers) {
1584
+ const before = firstStartAtOrAfter(headers, sbn) - 1;
1585
+ const after = firstStartAtOrAfter(headers, sbn + 1);
1586
+ const below = headers[before];
1587
+ const above = headers[after];
1588
+ if (below !== void 0 && (lower === void 0 || below.sbn > lower.sbn || below.sbn === lower.sbn && mpu > lowerMpu)) {
1589
+ lower = below;
1590
+ lowerMpu = mpu;
1591
+ lowerAt = before;
1592
+ }
1593
+ if (above !== void 0 && (upper === void 0 || above.sbn < upper.sbn || above.sbn === upper.sbn && mpu < upperMpu)) {
1594
+ upper = above;
1595
+ upperMpu = mpu;
1596
+ upperAt = after;
1597
+ }
1598
+ }
1599
+ if (lower === void 0 || upper === void 0) return false;
1600
+ if (block.mpus.size === 1 && !block.mpus.has(lowerMpu)) return false;
1601
+ if (upperMpu === lowerMpu) {
1602
+ if (upper.rap) return false;
1603
+ const geometry = this.#geometry;
1604
+ if (geometry.trackKind === "video" && geometry.mode === "classifying" && lower.position !== void 0 && upper.position !== void 0 && Math.floor(upper.position / geometry.gopFrames) > Math.floor(lower.position / geometry.gopFrames)) {
1605
+ return false;
1606
+ }
1607
+ } else if (upperMpu !== lowerMpu + 1 || !this.#startArrivedWhole(upperMpu, upperAt)) {
1608
+ return false;
1609
+ }
1610
+ return this.#firstSampleEndedBefore(sbn, lowerMpu, lowerAt);
1611
+ }
1612
+ /**
1613
+ * R1's proviso for block `sbn`, where header `at` of MPU `mpu` is the one
1614
+ * held last before it: no part of the MPU's first sample lies in the
1615
+ * block. A sample start without RAP_flag is not the first sample. Any
1616
+ * other header may be of it (a later data unit of a sample need not repeat
1617
+ * the flag). That sample ended before the block only if every packet from
1618
+ * the header to the next sample's DU header arrived, in earlier blocks.
1619
+ */
1620
+ #firstSampleEndedBefore(sbn, mpu, at) {
1621
+ const headers = this.#headers.get(mpu);
1622
+ const lower = headers[at];
1623
+ if (lower.offset === 0 && !lower.rap) return true;
1624
+ const next = nextSampleHeader(headers, at);
1625
+ return next !== void 0 && this.#arrivedBetween(mpu, lower, next, sbn);
1626
+ }
1627
+ /**
1628
+ * R2's exception: the start of MPU `mpu` arrived whole. Its header `at`,
1629
+ * the DU header held first after the block, is of the MPU's RAP. Every
1630
+ * packet before it arrived, back to the last packet of the MPU before: so
1631
+ * did the MPU and movie fragment metadata sent before the sample. And
1632
+ * every packet after it arrived, up to the next sample's DU header.
1633
+ */
1634
+ #startArrivedWhole(mpu, at) {
1635
+ const headers = this.#headers.get(mpu);
1636
+ const first = headers[at];
1637
+ const arrived = this.#arrived.get(mpu);
1638
+ const previous = this.#arrived.get(mpu - 1);
1639
+ if (!first.rap || first.ssId === void 0 || arrived === void 0 || previous === void 0) return false;
1640
+ let ssId = first.ssId - 1;
1641
+ while (arrived.start.has(ssId)) ssId--;
1642
+ if (ssId !== previous.last) return false;
1643
+ const next = nextSampleHeader(headers, at);
1644
+ return next !== void 0 && this.#arrivedBetween(mpu, first, next);
1645
+ }
1646
+ /**
1647
+ * Whether every packet sent after DU header `from` and before DU header
1648
+ * `to`, both of MPU `mpu`, arrived as a packet of that MPU, each in a block
1649
+ * before `before` when one is given. The source symbol id counts every
1650
+ * packet of the FEC flow, so no id may be missing between the two. Ids are
1651
+ * read as plain numbers, as block starts are: past the 2^32 wrap of the id
1652
+ * nothing is proved.
1653
+ */
1654
+ #arrivedBetween(mpu, from, to, before) {
1655
+ const arrived = this.#arrived.get(mpu);
1656
+ if (arrived === void 0 || from.ssId === void 0 || to.ssId === void 0) return false;
1657
+ for (let ssId = from.ssId + 1; ssId !== to.ssId; ssId++) {
1658
+ const sbn = arrived.start.get(ssId);
1659
+ if (sbn === void 0 || before !== void 0 && sbn >= before) return false;
1660
+ }
1661
+ return true;
1217
1662
  }
1218
1663
  #allowed(from, to) {
1219
1664
  if (from === "keyframe") return false;
@@ -1222,7 +1667,7 @@ var BlockClassifier = class {
1222
1667
  return false;
1223
1668
  }
1224
1669
  #evidenceFor(block, to) {
1225
- if (to === "delta") return "window";
1670
+ if (to === "delta") return this.#windowHolds(block) ? "window" : "mpu-rule";
1226
1671
  return block.rapSeen ? "rap" : "mpu-span";
1227
1672
  }
1228
1673
  };
@@ -2374,6 +2819,33 @@ var FECManager = class _FECManager {
2374
2819
  }
2375
2820
  }
2376
2821
  }
2822
+ /**
2823
+ * The DU header of a source packet that a decode rebuilt in block `sbn`.
2824
+ * It can class the blocks around that block (the MPU rule reads the DU
2825
+ * headers the receiver holds); the rebuilt block itself is resolved.
2826
+ */
2827
+ noteRecoveredSource(sbn, packetId, catalogTrackName, evidence) {
2828
+ const trackConfig = this.#trackConfigs.get(packetId);
2829
+ if (trackConfig === void 0 || !_FECManager.#hasCatalogTiming(trackConfig)) return;
2830
+ this.#applyClassifierChanges(
2831
+ packetId,
2832
+ catalogTrackName,
2833
+ this.#classifierFor(packetId, catalogTrackName).observeRecovered(sbn, evidence)
2834
+ );
2835
+ }
2836
+ /**
2837
+ * Tell the classifier of a block no source symbol of which is held, so the
2838
+ * DU headers around it can class it (the MPU rule reads the headers held
2839
+ * before and after a block's missing symbols; here every one is missing).
2840
+ * A block the classifier has no record of is never classed.
2841
+ */
2842
+ #registerSourcelessBlock(sbn, packetId, catalogTrackName) {
2843
+ this.#applyClassifierChanges(
2844
+ packetId,
2845
+ catalogTrackName,
2846
+ this.#classifierFor(packetId, catalogTrackName).touch(sbn)
2847
+ );
2848
+ }
2377
2849
  /**
2378
2850
  * Freeze a block's class at resolution and drop its classifier state. The
2379
2851
  * frozen class answers every later query (a recovery counted after the
@@ -2541,7 +3013,7 @@ var FECManager = class _FECManager {
2541
3013
  if (this.#blockResolutionFor(key) !== "untracked") return;
2542
3014
  this.#materializedBlocks.add(key);
2543
3015
  if (this.#hasCatalogTimingForPacket(packetId)) {
2544
- this.#classifierFor(packetId, catalogTrackName).touch(sbn);
3016
+ this.#registerSourcelessBlock(sbn, packetId, catalogTrackName);
2545
3017
  }
2546
3018
  this.onBlockVanished?.(sbn, packetId, catalogTrackName);
2547
3019
  if (repairable) {
@@ -4136,6 +4608,16 @@ var FECManager = class _FECManager {
4136
4608
  const classifier = this.#classifierFor(packetId, catalogTrackName);
4137
4609
  return { mode: classifier.mode, reason: classifier.reason };
4138
4610
  }
4611
+ /**
4612
+ * Why the MPU rule classes no block on a video stream now
4613
+ * (`BlockClassifier.mpuRuleOff`): what its catalog does not state, or what
4614
+ * the stream showed. Null while the rule can class a block. Undefined for a
4615
+ * stream with no classifier: one not heard yet, or without catalog timing.
4616
+ */
4617
+ mpuRuleOffFor(packetId, catalogTrackName) {
4618
+ const classifier = this.#classifiers.get(this.#streamKey(packetId, catalogTrackName));
4619
+ return classifier?.geometry.trackKind === "video" ? classifier.mpuRuleOff : void 0;
4620
+ }
4139
4621
  /**
4140
4622
  * Whether an MPU boundary still bounds this stream's RAPs (see
4141
4623
  * `BlockClassifier.mpuBoundsRaps`). Gap decisions drop MPU evidence once
@@ -4213,6 +4695,20 @@ var FECManager = class _FECManager {
4213
4695
  )
4214
4696
  );
4215
4697
  }
4698
+ /**
4699
+ * Decode times of the first and last sample that one held movie fragment's
4700
+ * metadata times, in MPU `mpu`. On a video stream the classifier looks
4701
+ * among them for an MPU longer than the catalog keyframe interval, where
4702
+ * the MPU rule stops (`BlockClassifier.noteDecodeTimes`).
4703
+ */
4704
+ noteSampleDecodeTimes(packetId, catalogTrackName, mpu, first, last, mediaTimescale) {
4705
+ if (!this.#hasCatalogTimingForPacket(packetId)) return;
4706
+ this.#applyClassifierChanges(
4707
+ packetId,
4708
+ catalogTrackName,
4709
+ this.#classifierFor(packetId, catalogTrackName).noteDecodeTimes(mpu, first, last, mediaTimescale)
4710
+ );
4711
+ }
4216
4712
  // -----------------------------------------------------------------------
4217
4713
  // Configuration
4218
4714
  // -----------------------------------------------------------------------
@@ -4884,6 +5380,9 @@ var FECManager = class _FECManager {
4884
5380
  if (!repairIds) {
4885
5381
  repairIds = /* @__PURE__ */ new Set();
4886
5382
  this.#blockRepairRsIds.set(blockKey, repairIds);
5383
+ if (!this.#blockSourceCounts.has(blockKey) && this.#hasCatalogTimingForPacket(packetId)) {
5384
+ this.#registerSourcelessBlock(sbn, packetId, catalogTrackName);
5385
+ }
4887
5386
  }
4888
5387
  repairIds.add(rsId);
4889
5388
  if (this.#repairRequestedBlocks.has(blockKey)) {
@@ -5113,12 +5612,13 @@ var FECManager = class _FECManager {
5113
5612
  * classification evidence, and marks the block clean when all of its
5114
5613
  * authored source symbols arrived: the SSB_length a repair symbol declared,
5115
5614
  * otherwise K. `held` marks a source held unplaced since it arrived and
5116
- * placed only now (see #noteSbnSeen).
5615
+ * placed only now (see #noteSbnSeen); `inSpan` one placed in a repair-less
5616
+ * span, which the classifier reads as no block (BLO-57323).
5117
5617
  *
5118
5618
  * Throws for a packetId that was never configured, so a classified block
5119
5619
  * never reads the manager-global K or timeout.
5120
5620
  */
5121
- trackBlock(sbn, packetId, catalogTrackName, esi, evidence, held = false) {
5621
+ trackBlock(sbn, packetId, catalogTrackName, esi, evidence, held = false, inSpan = false) {
5122
5622
  const trackConfig = this.#trackConfigs.get(packetId);
5123
5623
  if (trackConfig === void 0) {
5124
5624
  throw new Error(
@@ -5128,15 +5628,8 @@ var FECManager = class _FECManager {
5128
5628
  const key = this.#blockKey(sbn, packetId, catalogTrackName);
5129
5629
  const k = trackConfig.k;
5130
5630
  if (this.#completedBlocks.has(key)) return;
5131
- this.#noteSbnSeen(
5132
- sbn,
5133
- packetId,
5134
- catalogTrackName,
5135
- "source",
5136
- evidence.packetSequenceNumber,
5137
- Number.isInteger(esi) ? sbn + esi >>> 0 : sbn,
5138
- held
5139
- );
5631
+ const ssId = Number.isInteger(esi) ? sbn + esi >>> 0 : void 0;
5632
+ this.#noteSbnSeen(sbn, packetId, catalogTrackName, "source", evidence.packetSequenceNumber, ssId, held);
5140
5633
  const authoredLength = this.#blockAuthoredLengths.get(key) ?? k;
5141
5634
  if (Number.isInteger(esi) && esi >= authoredLength) {
5142
5635
  this.#failBlock(sbn, packetId, catalogTrackName);
@@ -5146,7 +5639,7 @@ var FECManager = class _FECManager {
5146
5639
  this.#applyClassifierChanges(
5147
5640
  packetId,
5148
5641
  catalogTrackName,
5149
- this.#classifierFor(packetId, catalogTrackName).observe(sbn, evidence)
5642
+ inSpan ? this.#classifierFor(packetId, catalogTrackName).observeInSpan(evidence) : this.#classifierFor(packetId, catalogTrackName).observe(sbn, evidence, ssId)
5150
5643
  );
5151
5644
  }
5152
5645
  const count = (this.#blockSourceCounts.get(key) ?? 0) + 1;