@blockcast/mmt-container 0.4.0 → 0.4.1-main.22841c1f0a2d

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 (220) hide show
  1. package/README.md +37 -21
  2. package/dist/align.d.ts +24 -18
  3. package/dist/align.d.ts.map +1 -1
  4. package/dist/align.js +23 -4
  5. package/dist/align.js.map +2 -2
  6. package/dist/browser-runtime.js +2037 -881
  7. package/dist/browser-runtime.js.map +4 -4
  8. package/dist/carp-catalog.d.ts +76 -39
  9. package/dist/carp-catalog.d.ts.map +1 -1
  10. package/dist/carp-catalog.js +321 -205
  11. package/dist/carp-catalog.js.map +3 -3
  12. package/dist/catalog-fec-params.d.ts +12 -4
  13. package/dist/catalog-fec-params.d.ts.map +1 -1
  14. package/dist/catalog-fec-params.js +40 -17
  15. package/dist/catalog-fec-params.js.map +3 -3
  16. package/dist/catalog-network-source.d.ts +28 -7
  17. package/dist/catalog-network-source.d.ts.map +1 -1
  18. package/dist/catalog-network-source.js +21 -1
  19. package/dist/catalog-network-source.js.map +2 -2
  20. package/dist/catalog-validator.d.ts +4 -3
  21. package/dist/catalog-validator.d.ts.map +1 -1
  22. package/dist/catalog-validator.js +0 -29
  23. package/dist/catalog-validator.js.map +3 -3
  24. package/dist/cmcd-producer.d.ts +13 -1
  25. package/dist/cmcd-producer.d.ts.map +1 -1
  26. package/dist/cmcd-producer.js +14 -0
  27. package/dist/cmcd-producer.js.map +2 -2
  28. package/dist/delivery-catalog.js +98 -234
  29. package/dist/delivery-catalog.js.map +4 -4
  30. package/dist/fec-manager.d.ts +108 -1
  31. package/dist/fec-manager.d.ts.map +1 -1
  32. package/dist/fec-manager.js +452 -66
  33. package/dist/fec-manager.js.map +4 -4
  34. package/dist/fec-state.d.ts +2 -1
  35. package/dist/fec-state.d.ts.map +1 -1
  36. package/dist/fec-state.js +18 -3
  37. package/dist/fec-state.js.map +3 -3
  38. package/dist/fec-worker-setup.d.ts +10 -0
  39. package/dist/fec-worker-setup.d.ts.map +1 -1
  40. package/dist/fec-worker-setup.js +15 -4
  41. package/dist/fec-worker-setup.js.map +2 -2
  42. package/dist/held-layer-admission.d.ts +58 -0
  43. package/dist/held-layer-admission.d.ts.map +1 -0
  44. package/dist/held-layer-admission.js +105 -0
  45. package/dist/held-layer-admission.js.map +7 -0
  46. package/dist/index.d.ts +5 -5
  47. package/dist/index.d.ts.map +1 -1
  48. package/dist/index.js +8 -8
  49. package/dist/index.js.map +2 -2
  50. package/dist/iso-block-locator.d.ts +39 -1
  51. package/dist/iso-block-locator.d.ts.map +1 -1
  52. package/dist/iso-block-locator.js +109 -17
  53. package/dist/iso-block-locator.js.map +3 -3
  54. package/dist/mfu-reassembler.js +21 -13
  55. package/dist/mfu-reassembler.js.map +2 -2
  56. package/dist/mmtp-container-parser.d.ts.map +1 -1
  57. package/dist/mmtp-container-parser.js +93 -22
  58. package/dist/mmtp-container-parser.js.map +2 -2
  59. package/dist/mmtp-container-parser.no-modules.js +3677 -1213
  60. package/dist/mmtp-container-parser.no-modules.js.map +4 -4
  61. package/dist/mmtp-decode-pipeline.js +93 -22
  62. package/dist/mmtp-decode-pipeline.js.map +2 -2
  63. package/dist/mmtp-fec-session.d.ts +192 -0
  64. package/dist/mmtp-fec-session.d.ts.map +1 -0
  65. package/dist/mmtp-fec-session.js +640 -0
  66. package/dist/mmtp-fec-session.js.map +7 -0
  67. package/dist/mmtp-router.d.ts +69 -32
  68. package/dist/mmtp-router.d.ts.map +1 -1
  69. package/dist/mmtp-router.js +296 -192
  70. package/dist/mmtp-router.js.map +2 -2
  71. package/dist/multicast-auth-catalog.d.ts +86 -92
  72. package/dist/multicast-auth-catalog.d.ts.map +1 -1
  73. package/dist/multicast-auth-catalog.js +313 -124
  74. package/dist/multicast-auth-catalog.js.map +3 -3
  75. package/dist/multicast-input.d.ts +31 -8
  76. package/dist/multicast-input.d.ts.map +1 -1
  77. package/dist/multicast-input.js +299 -35
  78. package/dist/multicast-input.js.map +3 -3
  79. package/dist/multicast-manager.d.ts +97 -11
  80. package/dist/multicast-manager.d.ts.map +1 -1
  81. package/dist/multicast-manager.js +1022 -527
  82. package/dist/multicast-manager.js.map +4 -4
  83. package/dist/multicast-transport.d.ts +12 -2
  84. package/dist/multicast-transport.d.ts.map +1 -1
  85. package/dist/multicast-transport.js +108 -238
  86. package/dist/multicast-transport.js.map +4 -4
  87. package/dist/multicast-unwrap.js +21 -13
  88. package/dist/multicast-unwrap.js.map +2 -2
  89. package/dist/no-modules.d.ts +5 -3
  90. package/dist/no-modules.d.ts.map +1 -1
  91. package/dist/no-modules.js +3625 -1194
  92. package/dist/no-modules.js.map +4 -4
  93. package/dist/phase14-catalog-override.d.ts +1 -1
  94. package/dist/phase14-catalog-override.d.ts.map +1 -1
  95. package/dist/phase14-catalog-override.js +2 -2
  96. package/dist/phase14-catalog-override.js.map +2 -2
  97. package/dist/repair-fetch.d.ts +81 -6
  98. package/dist/repair-fetch.d.ts.map +1 -1
  99. package/dist/repair-fetch.js +156 -31
  100. package/dist/repair-fetch.js.map +3 -3
  101. package/dist/repair-layers.d.ts +2 -0
  102. package/dist/repair-layers.d.ts.map +1 -1
  103. package/dist/repair-layers.js +19 -2
  104. package/dist/repair-layers.js.map +2 -2
  105. package/dist/sample-combiner.d.ts +7 -1
  106. package/dist/sample-combiner.d.ts.map +1 -1
  107. package/dist/sample-combiner.js +14 -2
  108. package/dist/sample-combiner.js.map +2 -2
  109. package/dist/signaling.d.ts +2 -2
  110. package/dist/signaling.d.ts.map +1 -1
  111. package/dist/signaling.js +3 -3
  112. package/dist/signaling.js.map +2 -2
  113. package/dist/switching-set-alignment.d.ts +1 -1
  114. package/dist/switching-set-alignment.js.map +1 -1
  115. package/dist/timestamp-codec.js +21 -13
  116. package/dist/timestamp-codec.js.map +2 -2
  117. package/dist/wasm-mfu-reassembler.js.map +1 -1
  118. package/dist/wasm-mmtp-container-parser.js +93 -22
  119. package/dist/wasm-mmtp-container-parser.js.map +2 -2
  120. package/package.json +6 -18
  121. package/schemas/catalog.schema.json +133 -38
  122. package/src/align.ts +52 -21
  123. package/src/carp-catalog.ts +188 -68
  124. package/src/catalog-fec-params.ts +28 -3
  125. package/src/catalog-network-source.ts +49 -7
  126. package/src/catalog-validator.ts +4 -11
  127. package/src/cmcd-producer.ts +23 -1
  128. package/src/fec-manager.ts +366 -63
  129. package/src/fec-state.ts +11 -4
  130. package/src/fec-worker-setup.ts +30 -2
  131. package/src/held-layer-admission.ts +161 -0
  132. package/src/index.ts +32 -19
  133. package/src/iso-block-locator.ts +110 -7
  134. package/src/mmtp-container-parser.ts +75 -7
  135. package/src/mmtp-fec-session.ts +606 -0
  136. package/src/mmtp-router.ts +301 -240
  137. package/src/multicast-auth-catalog.ts +134 -238
  138. package/src/multicast-input.ts +148 -124
  139. package/src/multicast-manager.ts +271 -40
  140. package/src/multicast-transport.ts +17 -3
  141. package/src/no-modules.ts +8 -7
  142. package/src/phase14-catalog-override.ts +3 -3
  143. package/src/repair-fetch.ts +283 -39
  144. package/src/repair-layers.ts +22 -2
  145. package/src/sample-combiner.ts +15 -2
  146. package/src/signaling.ts +2 -2
  147. package/src/switching-set-alignment.ts +1 -1
  148. package/test-vectors/align.json +108 -32
  149. package/test-vectors/bc-provenance-v2.json +1526 -0
  150. package/test-vectors/catalog/README.md +71 -4
  151. package/test-vectors/catalog/negative/fec-block-profile-missing.json +1 -1
  152. package/test-vectors/catalog/negative/fec-block-profile-retired.json +1 -1
  153. package/test-vectors/catalog/negative/fec-block-profile-unknown.json +1 -1
  154. package/test-vectors/catalog/negative/fec-iso-symbol-size-below-floor.json +28 -0
  155. package/test-vectors/catalog/negative/fec-reed-solomon-iso-symbol-size-13.json +28 -0
  156. package/test-vectors/catalog/negative/fec-reed-solomon-k-plus-p-over-255.json +39 -0
  157. package/test-vectors/catalog/negative/fec-reed-solomon-overlay-over-255.json +40 -0
  158. package/test-vectors/catalog/negative/fec-repair-overlay-source-symbols-zero.json +43 -0
  159. package/test-vectors/catalog/negative/fec-repair-symbols-zero.json +28 -0
  160. package/test-vectors/catalog/negative/fec-source-symbols-zero.json +28 -0
  161. package/test-vectors/catalog/negative/fec-symbol-size-zero.json +28 -0
  162. package/test-vectors/catalog/negative/init-data-inline-without-data.json +22 -0
  163. package/test-vectors/catalog/negative/mmtp-mode-mpu.json +15 -0
  164. package/test-vectors/catalog/negative/multicast-amt-dns-without-relay.json +37 -0
  165. package/test-vectors/catalog/negative/multicast-amt-driad-with-relay.json +38 -0
  166. package/test-vectors/catalog/negative/multicast-auth-asm-missing-sender-address.json +77 -0
  167. package/test-vectors/catalog/negative/multicast-auth-duplicate-first-epoch.json +56 -0
  168. package/test-vectors/catalog/negative/multicast-auth-duplicate-kid.json +56 -0
  169. package/test-vectors/catalog/negative/multicast-auth-endpoint-auth-without-object-auth.json +38 -0
  170. package/test-vectors/catalog/negative/multicast-auth-endpoint-missing-auth.json +49 -0
  171. package/test-vectors/catalog/negative/multicast-auth-endpoint-missing-bandwidth.json +55 -0
  172. package/test-vectors/catalog/negative/multicast-auth-manifest-port-equals-port.json +56 -0
  173. package/test-vectors/catalog/negative/multicast-auth-manifest-timer-out-of-range.json +56 -0
  174. package/test-vectors/catalog/negative/multicast-auth-max-auth-delay-above-max.json +56 -0
  175. package/test-vectors/catalog/negative/multicast-auth-max-auth-delay-below-min.json +56 -0
  176. package/test-vectors/catalog/negative/multicast-auth-root-dotted-key.json +55 -0
  177. package/test-vectors/catalog/negative/multicast-auth-sender-address-on-ssm.json +57 -0
  178. package/test-vectors/catalog/negative/multicast-auth-version-1.json +56 -0
  179. package/test-vectors/catalog/negative/multicast-auth-version-3.json +56 -0
  180. package/test-vectors/catalog/negative/multicast-auth-version-missing.json +55 -0
  181. package/test-vectors/catalog/negative/multicast-endpoint-foreign-namespace-track.json +44 -0
  182. package/test-vectors/catalog/negative/multicast-endpoint-protocol-amt.json +38 -0
  183. package/test-vectors/catalog/negative/multicast-endpoint-protocol-atsc3.json +38 -0
  184. package/test-vectors/catalog/negative/presentation-anchor-unsafe.json +16 -0
  185. package/test-vectors/catalog/positive/fec-algorithm-unimplemented.json +29 -0
  186. package/test-vectors/catalog/positive/fec-iso-ssbg1.json +1 -1
  187. package/test-vectors/catalog/positive/fec-multicast.json +1 -1
  188. package/test-vectors/catalog/positive/fec-reed-solomon-layered.json +50 -0
  189. package/test-vectors/catalog/positive/mmt01-12-1-init-ref-anchor.json +29 -0
  190. package/test-vectors/catalog/positive/multicast-amt-discovery-omitted.json +37 -0
  191. package/test-vectors/catalog/positive/multicast-amt-dns-relay.json +38 -0
  192. package/test-vectors/catalog/positive/multicast-amt-manual-relay.json +3 -3
  193. package/test-vectors/catalog/{negative/multicast-endpoint-missing-protocol-and-source.json → positive/multicast-asm-unprotected.json} +10 -5
  194. package/test-vectors/catalog/positive/multicast-auth-asm-sender.json +78 -0
  195. package/test-vectors/catalog/positive/multicast-auth-rotation.json +39 -38
  196. package/test-vectors/catalog/positive/multicast-draft01-4-1-example.json +81 -0
  197. package/test-vectors/catalog/positive/multicast-draft01-7-2-6-example.json +70 -0
  198. package/test-vectors/catalog/positive/presentation-anchor-negative.json +27 -0
  199. package/test-vectors/catalog/section-codes.json +31 -1
  200. package/test-vectors/catalog-multicast-auth-v1.json +38 -20
  201. package/test-vectors/catalog-validation.json +101 -5
  202. package/test-vectors/catalog-validator-public-api.mjs +14 -9
  203. package/test-vectors/generate-bc-provenance-v2.mjs +252 -0
  204. package/dist/fec-codec.d.ts +0 -32
  205. package/dist/fec-codec.d.ts.map +0 -1
  206. package/dist/fec-codec.js +0 -47
  207. package/dist/fec-codec.js.map +0 -7
  208. package/dist/manifest-auth-plan.d.ts +0 -30
  209. package/dist/manifest-auth-plan.d.ts.map +0 -1
  210. package/dist/manifest-auth-plan.js +0 -1266
  211. package/dist/manifest-auth-plan.js.map +0 -7
  212. package/dist/source-auth.d.ts +0 -2
  213. package/dist/source-auth.d.ts.map +0 -1
  214. package/dist/source-auth.js +0 -1209
  215. package/dist/source-auth.js.map +0 -7
  216. package/src/fec-codec.ts +0 -61
  217. package/src/manifest-auth-plan.ts +0 -107
  218. package/src/source-auth.ts +0 -1
  219. package/test-vectors/generate-source-auth-fixture.mjs +0 -114
  220. package/test-vectors/source-auth-v1.json +0 -61
@@ -186,16 +186,6 @@ var ALL_ROLES = Object.freeze(["video", "audio"]);
186
186
 
187
187
  // src/iso-block-locator.ts
188
188
  var IsoBlockLocator = class _IsoBlockLocator {
189
- /** Known blocks and spans, in serial order of their start. */
190
- #blocks = [];
191
- /** Held first-arrival sources whose block no repair has stated yet. */
192
- #held = /* @__PURE__ */ new Map();
193
- /** End of contiguous coverage: the SS_ID the next unknown block starts at. */
194
- #coverageEnd;
195
- #kMax;
196
- #retentionBlocks;
197
- #heldEvicted = 0;
198
- #newest;
199
189
  /**
200
190
  * @param kMax the catalog maximum K (`sourceSymbols`), the bound on SSB_length.
201
191
  * @param retentionBlocks how many blocks behind the newest position the
@@ -203,12 +193,35 @@ var IsoBlockLocator = class _IsoBlockLocator {
203
193
  * no locator exists without retention.
204
194
  */
205
195
  constructor(kMax, retentionBlocks) {
196
+ /** Known blocks and spans, in serial order of their start. */
197
+ this.#blocks = [];
198
+ /** Held first-arrival sources whose block no repair has stated yet. */
199
+ this.#held = /* @__PURE__ */ new Map();
200
+ this.#heldEvicted = 0;
201
+ /** Pending holes (see `deferIncompleteGaps`): unstated, sources still held. */
202
+ this.#holes = [];
203
+ /**
204
+ * Keep a gap that is missing a source as a pending hole rather than a span,
205
+ * so a repair FETCHed for it (draft-ramadan-moq-fec §7.5) can still state
206
+ * its blocks. Set only while a range repair scheduler is wired; its owner
207
+ * must close every hole it is told about (`closeHolesWithin`).
208
+ */
209
+ this.deferIncompleteGaps = false;
206
210
  if (!Number.isSafeInteger(kMax) || kMax <= 0) {
207
211
  throw new Error(`[IsoBlockLocator] maximum K must be a positive integer (got ${kMax})`);
208
212
  }
209
213
  this.#kMax = kMax;
210
214
  this.#retentionBlocks = _IsoBlockLocator.#checkedRetention(retentionBlocks);
211
215
  }
216
+ #blocks;
217
+ #held;
218
+ /** End of contiguous coverage: the SS_ID the next unknown block starts at. */
219
+ #coverageEnd;
220
+ #kMax;
221
+ #retentionBlocks;
222
+ #heldEvicted;
223
+ #newest;
224
+ #holes;
212
225
  static #checkedRetention(retentionBlocks) {
213
226
  if (!Number.isSafeInteger(retentionBlocks) || retentionBlocks <= 0) {
214
227
  throw new Error(
@@ -271,6 +284,14 @@ var IsoBlockLocator = class _IsoBlockLocator {
271
284
  }
272
285
  return null;
273
286
  }
287
+ /** Number of pending holes. */
288
+ get holeCount() {
289
+ return this.#holes.length;
290
+ }
291
+ /** The stated block (never a span) ending exactly at `end`. */
292
+ blockEndingAt(end) {
293
+ return this.#blocks.find((block) => !block.span && isoBlockEnd(block.start, block.length) === end);
294
+ }
274
295
  /** The known block or span starting exactly at `start`. */
275
296
  blockAt(start) {
276
297
  return this.#blocks.find((block) => block.start === start);
@@ -293,6 +314,9 @@ var IsoBlockLocator = class _IsoBlockLocator {
293
314
  coversBetween(a, b) {
294
315
  if (this.#coverageEnd === void 0) return false;
295
316
  if (!serial32Precedes(a, b) || serial32Distance(a, b) <= 1) return true;
317
+ const first = a + 1 >>> 0;
318
+ const count = serial32Distance(a, b) - 1;
319
+ if (this.#holes.some((hole) => isoRangesOverlap(first, count, hole.start, hole.length))) return false;
296
320
  return serial32Precedes(b - 1 >>> 0, this.#coverageEnd);
297
321
  }
298
322
  /**
@@ -322,6 +346,15 @@ var IsoBlockLocator = class _IsoBlockLocator {
322
346
  * known block (a different length, or an overlap with another block or a
323
347
  * span) fails closed: the repair symbol is dropped and no block changes.
324
348
  * A newly stated block retires behind its SS_Start (see the class note).
349
+ * A block so far past `newest` that stating it would retire the position
350
+ * `newest` is refused as `ahead`. When `newest` is a placed block's SS_Start
351
+ * this is slightly conservative (retirement measures that block from its
352
+ * last SS_ID), dropping up to one block-length of repairs past the horizon
353
+ * until a source moves `newest`. Placing it would turn everything
354
+ * between into a repair-less span and leave every real repair behind it
355
+ * `stale`. Sources move `newest` (`hold`, `observe`), so after a real outage
356
+ * the first source back brings the next repair inside the horizon; until one
357
+ * arrives, a stream's repairs alone cannot pass it.
325
358
  */
326
359
  placeRepair(start, length) {
327
360
  if (!Number.isSafeInteger(length) || length <= 0 || length > this.#kMax) {
@@ -340,6 +373,22 @@ var IsoBlockLocator = class _IsoBlockLocator {
340
373
  }
341
374
  return { status: "placed", block: known, placed: [] };
342
375
  }
376
+ const pending = this.#holes.find((h) => isoBlockContains(h.start, h.length, start));
377
+ if (pending !== void 0) {
378
+ if (!isoBlockContains(pending.start, pending.length, serial32Add(start, length - 1))) {
379
+ return { status: "overlap", reason: `repair block ${start}+${length} runs past pending hole ${pending.start}+${pending.length}` };
380
+ }
381
+ this.#holes.splice(this.#holes.indexOf(pending), 1);
382
+ const before = serial32Distance(pending.start, start);
383
+ const afterStart = isoBlockEnd(start, length);
384
+ const after = pending.length - before - length;
385
+ if (before > 0) this.#holes.push({ start: pending.start, length: before, span: false });
386
+ if (after > 0) this.#holes.push({ start: afterStart, length: after, span: false });
387
+ const block2 = { start, length, span: false };
388
+ const placed2 = this.#claim(block2);
389
+ this.#blocks.push(block2);
390
+ return { status: "placed", block: block2, placed: placed2 };
391
+ }
343
392
  for (const block2 of this.#blocks) {
344
393
  if (isoBlockContains(block2.start, block2.length, start) || isoBlockContains(start, length, block2.start)) {
345
394
  return {
@@ -348,7 +397,11 @@ var IsoBlockLocator = class _IsoBlockLocator {
348
397
  };
349
398
  }
350
399
  }
400
+ if (this.#newest !== void 0 && isoBlocksBehindLowerBound(this.#newest, start, this.#kMax) > this.#retentionBlocks) {
401
+ return { status: "ahead", reason: `repair block ${start}+${length} is past the retention horizon ahead of ${this.#newest}` };
402
+ }
351
403
  let span;
404
+ let hole;
352
405
  if (this.#coverageEnd === void 0) {
353
406
  for (const ssId of this.#held.keys()) {
354
407
  if (serial32Precedes(ssId, start)) this.#held.delete(ssId);
@@ -357,21 +410,51 @@ var IsoBlockLocator = class _IsoBlockLocator {
357
410
  } else if (serial32Precedes(start, this.#coverageEnd)) {
358
411
  return { status: "stale", reason: `repair block ${start}+${length} precedes known coverage` };
359
412
  } else if (start !== this.#coverageEnd) {
360
- const spanBlock = {
361
- start: this.#coverageEnd,
362
- length: serial32Distance(this.#coverageEnd, start),
363
- span: true
364
- };
365
- span = { block: spanBlock, placed: this.#claim(spanBlock) };
366
- this.#blocks.push(spanBlock);
413
+ const gap = { start: this.#coverageEnd, length: serial32Distance(this.#coverageEnd, start) };
414
+ const lostSsId = this.deferIncompleteGaps ? this.#highestMissing(gap.start, gap.length) : void 0;
415
+ if (lostSsId !== void 0) {
416
+ const holeBlock = { ...gap, span: false };
417
+ this.#holes.push(holeBlock);
418
+ hole = { block: holeBlock, lostSsId };
419
+ } else {
420
+ const spanBlock = { ...gap, span: true };
421
+ span = { block: spanBlock, placed: this.#claim(spanBlock) };
422
+ this.#blocks.push(spanBlock);
423
+ }
367
424
  }
368
425
  const block = { start, length, span: false };
369
426
  const placed = this.#claim(block);
370
427
  this.#blocks.push(block);
371
428
  this.#coverageEnd = isoBlockEnd(start, length);
372
429
  this.retireBehind(start);
430
+ if (hole !== void 0) return { status: "placed", block, placed, hole };
373
431
  return span === void 0 ? { status: "placed", block, placed } : { status: "placed", block, placed, span };
374
432
  }
433
+ /**
434
+ * Turn every pending hole inside `[start, start + length)` into a
435
+ * repair-less span, claiming its held sources: its range FETCH ended, so
436
+ * nothing more will state its blocks. Returns the spans, in SS_ID order.
437
+ */
438
+ closeHolesWithin(start, length) {
439
+ const closed = [];
440
+ for (const hole of [...this.#holes]) {
441
+ if (!isoBlockContains(start, length, hole.start)) continue;
442
+ this.#holes.splice(this.#holes.indexOf(hole), 1);
443
+ const spanBlock = { start: hole.start, length: hole.length, span: true };
444
+ closed.push({ block: spanBlock, placed: this.#claim(spanBlock) });
445
+ this.#blocks.push(spanBlock);
446
+ }
447
+ return closed.sort((x, y) => serial32Distance(start, x.block.start) - serial32Distance(start, y.block.start));
448
+ }
449
+ /** The highest SS_ID in the range no source is held for, or undefined when all are. */
450
+ #highestMissing(start, length) {
451
+ const steps = Math.min(length, this.#held.size + 1);
452
+ for (let i = 1; i <= steps; i++) {
453
+ const ssId = serial32Add(start, length - i);
454
+ if (!this.#held.has(ssId)) return ssId;
455
+ }
456
+ return void 0;
457
+ }
375
458
  /**
376
459
  * Retire every block and span whose last SS_ID, and every held source,
377
460
  * lies more than `retentionBlocks` blocks behind `position`, with the shared
@@ -390,6 +473,11 @@ var IsoBlockLocator = class _IsoBlockLocator {
390
473
  this.#blocks.splice(i, 1);
391
474
  }
392
475
  }
476
+ for (let i = this.#holes.length - 1; i >= 0; i--) {
477
+ const hole = this.#holes[i];
478
+ const last = serial32Add(hole.start, hole.length - 1);
479
+ if (isoBlocksBehindLowerBound(last, position, this.#kMax) > horizon) this.#holes.splice(i, 1);
480
+ }
393
481
  for (const ssId of [...this.#held.keys()]) {
394
482
  if (isoBlocksBehindLowerBound(ssId, position, this.#kMax) > horizon) this.#held.delete(ssId);
395
483
  }
@@ -398,6 +486,7 @@ var IsoBlockLocator = class _IsoBlockLocator {
398
486
  /** Forget everything (publisher restart, track reset). */
399
487
  reset() {
400
488
  this.#blocks.length = 0;
489
+ this.#holes.length = 0;
401
490
  this.#held.clear();
402
491
  this.#coverageEnd = void 0;
403
492
  this.#newest = void 0;
@@ -413,6 +502,9 @@ var IsoBlockLocator = class _IsoBlockLocator {
413
502
  return placed;
414
503
  }
415
504
  };
505
+ function isoRangesOverlap(aStart, aLength, bStart, bLength) {
506
+ return serial32Distance(aStart, bStart) < aLength || serial32Distance(bStart, aStart) < bLength;
507
+ }
416
508
 
417
509
  // src/fec-state.ts
418
510
  var MIN_INTERLEAVE_DEPTH_MS = 1;
@@ -762,7 +854,81 @@ var BlockClassifier = class {
762
854
  }
763
855
  };
764
856
 
857
+ // src/held-layer-admission.ts
858
+ var HORIZON_CAP = 1073741824;
859
+ function heldLayerAnchorHorizon(retentionBlocks, k) {
860
+ return retentionBlocks === void 0 || k <= 0 ? HORIZON_CAP : Math.min(HORIZON_CAP, retentionBlocks * k);
861
+ }
862
+ var HeldLayerAdmission = class {
863
+ #layerNames;
864
+ #states;
865
+ constructor(layers, heldLayers = [0]) {
866
+ const held = new Set(heldLayers);
867
+ this.#layerNames = new Set(layers.map((l) => l.repairTrackName));
868
+ this.#states = layers.filter((l) => held.has(l.layer)).map((l) => ({ layer: l.layer, repairTrackName: l.repairTrackName, total: l.rsIdOffset + l.repairSymbols }));
869
+ }
870
+ /**
871
+ * Judges one repair packet, after the 4.5 discards and the size check and
872
+ * before it places a block. `repairTrackName` is the subscribed repair
873
+ * track, never inferred from RS_ID; without it only (b) runs. A packet on a
874
+ * repair track the catalog does not list is `'discard'` and anchors nothing.
875
+ * `sourceFrontier` is the newest source SS_ID, if any: a repair more than
876
+ * `horizon` past it is judged without moving the anchored state.
877
+ *
878
+ * @returns undefined to admit, `'discard'`, or the fail-closed reason.
879
+ */
880
+ admit(repairTrackName, ssStart, rsbLength, horizon, sourceFrontier) {
881
+ if (repairTrackName !== void 0 && !this.#layerNames.has(repairTrackName)) return "discard";
882
+ const farAhead = sourceFrontier !== void 0 && serial32Precedes(sourceFrontier, ssStart) && serial32Distance(sourceFrontier, ssStart) > horizon;
883
+ for (const state of this.#states) {
884
+ let code;
885
+ if (farAhead) {
886
+ if (rsbLength < state.total) {
887
+ if (state.layer > 0 && repairTrackName === state.repairTrackName) code = "below_own_track_total";
888
+ else if (state.anchor !== void 0 || state.settled) code = "below_anchored_total";
889
+ }
890
+ } else {
891
+ if (state.newest === void 0 || serial32Precedes(state.newest, ssStart)) state.newest = ssStart;
892
+ const stale = (s) => serial32Distance(s, state.newest) > horizon;
893
+ if (state.latestBelow !== void 0 && stale(state.latestBelow)) {
894
+ delete state.latestBelow;
895
+ delete state.latestBelowRsb;
896
+ }
897
+ if (state.anchor !== void 0 && stale(state.anchor)) state.settled = true;
898
+ if (rsbLength < state.total) {
899
+ if (state.layer > 0 && repairTrackName === state.repairTrackName) code = "below_own_track_total";
900
+ else if (stale(ssStart)) continue;
901
+ else if (state.settled || state.anchor !== void 0 && !serial32Precedes(ssStart, state.anchor)) code = "below_anchored_total";
902
+ else if (state.latestBelow === void 0 || serial32Precedes(state.latestBelow, ssStart)) {
903
+ state.latestBelow = ssStart;
904
+ state.latestBelowRsb = rsbLength;
905
+ }
906
+ } else if (!state.settled && !stale(ssStart)) {
907
+ if (state.anchor === void 0 || serial32Precedes(ssStart, state.anchor)) state.anchor = ssStart;
908
+ if (state.latestBelow !== void 0 && !serial32Precedes(state.latestBelow, state.anchor)) {
909
+ code = "below_anchored_total";
910
+ ssStart = state.latestBelow;
911
+ rsbLength = state.latestBelowRsb;
912
+ }
913
+ }
914
+ }
915
+ if (code !== void 0) {
916
+ return {
917
+ code,
918
+ layer: state.layer,
919
+ total: state.total,
920
+ rsbLength,
921
+ ssStart,
922
+ ...state.anchor === void 0 ? {} : { anchor: state.anchor }
923
+ };
924
+ }
925
+ }
926
+ return void 0;
927
+ }
928
+ };
929
+
765
930
  // src/fec-manager.ts
931
+ var RS_MAX_BLOCK_SYMBOLS = 255;
766
932
  var WORKER_ERROR_LOG_CAP = 5;
767
933
  var WORKER_ERROR_KIND_CAP = 32;
768
934
  var WORKER_ERROR_OVERFLOW_KIND = "other";
@@ -877,6 +1043,11 @@ var FECManager = class _FECManager {
877
1043
  // RSB_length per block (C.5.3 Repair FEC Payload ID): how many repair
878
1044
  // symbols its repair group carries. Same lifecycle as #blockRepairRsIds.
879
1045
  this.#blockRsbLengths = /* @__PURE__ */ new Map();
1046
+ /** fec-01 6.3.9 anchor state per stream key, one entry per held layer. */
1047
+ this.#heldLayerStates = /* @__PURE__ */ new Map();
1048
+ /** Streams whose FEC failed closed at runtime (fec-01 6.3.9). */
1049
+ this.#fecFailClosed = /* @__PURE__ */ new Map();
1050
+ this.#onFecFailClosed = null;
880
1051
  // Blocks already recovered — prevents late source symbols from restarting timeouts.
881
1052
  // WASM removes blocks after recovery; late symbols create empty blocks returning null,
882
1053
  // which would falsely start new timeouts without this guard.
@@ -997,12 +1168,17 @@ var FECManager = class _FECManager {
997
1168
  // per stream. Not only throws: the ordinary out-of-range case is detected by
998
1169
  // arithmetic and never throws at all.
999
1170
  this.#orderedHorizonWarnedFor = /* @__PURE__ */ new Set();
1000
- // Newest block start (SS_Start) seen per stream and when: the frontier for
1001
- // source-discontinuity detection and isoSpanResolution.
1171
+ // Newest block start (SS_Start) any symbol carried per stream and when: the
1172
+ // frontier isoSpanResolution reads.
1002
1173
  this.#highestSbn = /* @__PURE__ */ new Map();
1174
+ // The same from source symbols alone: the frontier a source is judged
1175
+ // against for a discontinuity. Repairs ride their own group, which can run
1176
+ // blocks ahead of the media they protect.
1177
+ this.#sourceFrontiers = /* @__PURE__ */ new Map();
1003
1178
  // iso-ssbg1-v1 block location per stream (streamKey). A source belongs to a
1004
1179
  // block only once a repair symbol stated the block's (SS_Start, SSB_length).
1005
1180
  this.#isoLocators = /* @__PURE__ */ new Map();
1181
+ this.#deferIsoGaps = false;
1006
1182
  this.#isoRepairRefusedCount = 0;
1007
1183
  this.#isoSourceOversizeCount = 0;
1008
1184
  // Held sources a locator's capacity cap evicted (IsoBlockLocator.heldCapacity):
@@ -1017,8 +1193,8 @@ var FECManager = class _FECManager {
1017
1193
  // rate-limited like the other per-symbol warns here. Cumulative.
1018
1194
  this.#isoDecoderUnplacedRetireFailed = 0;
1019
1195
  // The latest source symbol per stream that carried its MMTP
1020
- // packet_sequence_number: the anchor that bounds how many blocks the
1021
- // producer can have started before a later source symbol.
1196
+ // packet_sequence_number, by its own SS_ID: the anchor that bounds how far
1197
+ // the producer's SS_IDs can have moved before or after another source symbol.
1022
1198
  this.#sourceAnchors = /* @__PURE__ */ new Map();
1023
1199
  // Catalog/reset epoch per packetId. Gap decisions carry it for fencing.
1024
1200
  this.#packetEpochs = /* @__PURE__ */ new Map();
@@ -1077,6 +1253,14 @@ var FECManager = class _FECManager {
1077
1253
  this.onBlockReclassified = null;
1078
1254
  /** A video stream's classifier mode was established or changed. */
1079
1255
  this.onClassifierMode = null;
1256
+ /**
1257
+ * A repair stated a block past a gap that is missing a source and whose
1258
+ * every repair was lost: a lost SS_ID in an undelimited span
1259
+ * (draft-ramadan-moq-fec §7.5). Set with `setUndelimitedLossHandler`; the
1260
+ * receiver keeps the gap as a pending hole until `closeIsoHoles` is called
1261
+ * for it, which it must be.
1262
+ */
1263
+ this.#onUndelimitedLoss = null;
1080
1264
  /** A block every symbol of which vanished was created by SBN evidence. */
1081
1265
  this.onBlockVanished = null;
1082
1266
  /** A block reached a terminal state (completed, recovered, or failed). */
@@ -1118,6 +1302,9 @@ var FECManager = class _FECManager {
1118
1302
  #ssbLengthConflictCount;
1119
1303
  #blockRepairRsIds;
1120
1304
  #blockRsbLengths;
1305
+ #heldLayerStates;
1306
+ #fecFailClosed;
1307
+ #onFecFailClosed;
1121
1308
  #completedBlocks;
1122
1309
  #terminalExpiryMs;
1123
1310
  #terminalQueues;
@@ -1156,7 +1343,9 @@ var FECManager = class _FECManager {
1156
1343
  #materializedBlocks;
1157
1344
  #orderedHorizonWarnedFor;
1158
1345
  #highestSbn;
1346
+ #sourceFrontiers;
1159
1347
  #isoLocators;
1348
+ #deferIsoGaps;
1160
1349
  #isoRepairRefusedCount;
1161
1350
  #isoSourceOversizeCount;
1162
1351
  #isoHeldEvictedCount;
@@ -1170,6 +1359,7 @@ var FECManager = class _FECManager {
1170
1359
  #workerErrorCounts;
1171
1360
  #decoderUnavailableEvents;
1172
1361
  #repairSymbolSizeMismatchCount;
1362
+ #onUndelimitedLoss;
1173
1363
  #encodeTrackName(catalogTrackName) {
1174
1364
  let encoded = this.#encodedTrackNames.get(catalogTrackName);
1175
1365
  if (encoded === void 0) {
@@ -1746,8 +1936,8 @@ var FECManager = class _FECManager {
1746
1936
  // Deliberately one block tighter than deriveFecRetentionBlocks over the same
1747
1937
  // inputs (that function's `+ 1`). Different question: the retention horizon
1748
1938
  // asks how long the decoder still ACCEPTS symbols for a block, this asks how
1749
- // many blocks a stream can live behind (or ahead of) its frontier before a
1750
- // source symbol there is a discontinuity rather than loss (see #noteSbnSeen).
1939
+ // many blocks a stream can jump ahead of its frontier before a source symbol
1940
+ // there is a discontinuity rather than loss (see #noteSbnSeen).
1751
1941
  // The extra block of retention is slack for a block still being fed; a block
1752
1942
  // nothing has been heard from does not get it. Not the units defect — but it
1753
1943
  // is the next place the two can drift, so change both or neither.
@@ -1757,55 +1947,72 @@ var FECManager = class _FECManager {
1757
1947
  }
1758
1948
  /**
1759
1949
  * Record that a symbol for block `sbn` (the block's SS_Start) arrived and
1760
- * advance the stream's frontier, the newest block start seen.
1950
+ * advance the stream's frontiers, the newest block start seen.
1761
1951
  *
1762
- * A SOURCE symbol whose block goes back further than one live window, or
1763
- * jumps further than the stream could have produced since its last symbol,
1764
- * is a source discontinuity, not loss. A repair symbol can only advance the
1952
+ * A SOURCE symbol is judged against the newest source block alone: repairs
1953
+ * ride their own group, which can run blocks ahead of the media. One whose
1954
+ * block goes back further than the blocks this manager retains, or that
1955
+ * cannot have been sent before the source it lies behind, or that jumps
1956
+ * further than the stream could have produced since its last symbol, is a
1957
+ * source discontinuity, not loss. A repair symbol can only advance the
1765
1958
  * frontier: a D-28 fetch legitimately answers for an old block, so it never
1766
1959
  * signals a discontinuity.
1767
1960
  *
1768
1961
  * The producer closes a block at its Dth media-unit boundary OR when K
1769
1962
  * source symbols filled it, so one interleave span can start several blocks
1770
- * (every large keyframe does). What bounds a forward jump is therefore
1771
- * packet evidence, not a block rate: SS_ID advances by one per FEC source
1772
- * packet (ISO/IEC 23008-1 C.5.2) and the MMTP packet_sequence_number by one
1773
- * per packet of the packet_id, so with both source symbols'
1774
- * packet_sequence_number a block start can move at most the packets sent
1775
- * between them plus the anchor's own offset into its block (< K). Without
1776
- * it, only the D-close rate is known.
1963
+ * (every large keyframe does). What bounds a jump is therefore packet
1964
+ * evidence, not a block rate: SS_ID advances by one per FEC source packet
1965
+ * (ISO/IEC 23008-1 C.5.2) and the MMTP packet_sequence_number by one per
1966
+ * packet of the packet_id, so with both source symbols'
1967
+ * packet_sequence_number their SS_IDs (`ssId`, not their block starts,
1968
+ * which a repair-less span can put far behind) can move at most the packets
1969
+ * sent between them, in either direction, plus K of slack for sources lost
1970
+ * before a hop that renumbers packets. Without it, only the D-close rate is
1971
+ * known.
1777
1972
  *
1778
1973
  * Vanished blocks are not enumerated here: their starts are unknown until a
1779
1974
  * repair states them. The SS_IDs between two stated blocks become one
1780
1975
  * repair-less span when the later block's repair arrives (placeIsoRepair).
1781
1976
  */
1782
- #noteSbnSeen(sbn, packetId, catalogTrackName, symbol, packetSequenceNumber) {
1977
+ #noteSbnSeen(sbn, packetId, catalogTrackName, symbol, packetSequenceNumber, ssId = sbn) {
1783
1978
  const streamKey = this.#streamKey(packetId, catalogTrackName);
1784
1979
  const now = performance.now();
1785
- const previous = this.#highestSbn.get(streamKey);
1980
+ const previous = (symbol === "source" ? this.#sourceFrontiers : this.#highestSbn).get(streamKey);
1786
1981
  const anchor = this.#sourceAnchors.get(streamKey);
1787
1982
  const psn = symbol === "source" && packetSequenceNumber !== void 0 && Number.isSafeInteger(packetSequenceNumber) && packetSequenceNumber >= 0 && packetSequenceNumber <= 4294967295 ? packetSequenceNumber : void 0;
1788
1983
  const anchorHere = () => {
1789
- if (psn !== void 0 && (anchor === void 0 || !this.#blockPrecedes(sbn, anchor.sbn))) {
1790
- this.#sourceAnchors.set(streamKey, { sbn, psn, atMs: now });
1984
+ if (psn !== void 0 && (anchor === void 0 || !serial32Precedes(ssId, anchor.ssId))) {
1985
+ this.#sourceAnchors.set(streamKey, { ssId, psn, atMs: now });
1986
+ }
1987
+ };
1988
+ const advance = () => {
1989
+ const highest = this.#highestSbn.get(streamKey);
1990
+ if (highest === void 0 || this.#blockPrecedes(highest.sbn, sbn)) {
1991
+ this.#highestSbn.set(streamKey, { sbn, atMs: now });
1791
1992
  }
1993
+ if (symbol === "source") this.#sourceFrontiers.set(streamKey, { sbn, atMs: now });
1994
+ anchorHere();
1792
1995
  };
1793
1996
  const discontinuity = () => {
1794
1997
  this.#classifiers.get(streamKey)?.noteSourceDiscontinuity();
1795
1998
  this.#highestSbn.set(streamKey, { sbn, atMs: now });
1999
+ this.#sourceFrontiers.set(streamKey, { sbn, atMs: now });
1796
2000
  if (psn === void 0) this.#sourceAnchors.delete(streamKey);
1797
- else this.#sourceAnchors.set(streamKey, { sbn, psn, atMs: now });
2001
+ else this.#sourceAnchors.set(streamKey, { ssId, psn, atMs: now });
1798
2002
  this.onSourceDiscontinuity?.(packetId, catalogTrackName);
1799
2003
  };
1800
2004
  if (previous === void 0) {
1801
- this.#highestSbn.set(streamKey, { sbn, atMs: now });
1802
- anchorHere();
2005
+ advance();
1803
2006
  return;
1804
2007
  }
1805
2008
  const config = this.#trackConfig(packetId);
1806
2009
  const window = this.#liveWindowBlocks(config);
2010
+ const sentInOrder = (from, to) => {
2011
+ const sent = to.psn - from.psn >>> 0;
2012
+ return sent <= 2147483647 && serial32Distance(from.ssId, to.ssId) <= sent + config.k;
2013
+ };
1807
2014
  if (!this.#blockPrecedes(previous.sbn, sbn)) {
1808
- if (symbol === "source" && window !== null && this.#blocksBehind(sbn, previous.sbn, packetId) > window) {
2015
+ 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))) {
1809
2016
  discontinuity();
1810
2017
  return;
1811
2018
  }
@@ -1815,9 +2022,8 @@ var FECManager = class _FECManager {
1815
2022
  const missing = this.#blocksBehind(previous.sbn, sbn, packetId) - 1;
1816
2023
  let plausible = true;
1817
2024
  if (window !== null) {
1818
- if (psn !== void 0 && anchor !== void 0 && this.#blockPrecedes(anchor.sbn, sbn) && psn !== anchor.psn) {
1819
- const sent = psn - anchor.psn >>> 0;
1820
- plausible = sent <= 2147483647 && serial32Distance(anchor.sbn, sbn) <= sent + config.k;
2025
+ if (psn !== void 0 && anchor !== void 0 && serial32Precedes(anchor.ssId, ssId) && psn !== anchor.psn) {
2026
+ plausible = sentInOrder(anchor, { ssId, psn });
1821
2027
  } else {
1822
2028
  plausible = missing <= window + Math.ceil((now - previous.atMs) / config.interleaveDepthMs);
1823
2029
  }
@@ -1827,8 +2033,7 @@ var FECManager = class _FECManager {
1827
2033
  discontinuity();
1828
2034
  return;
1829
2035
  }
1830
- this.#highestSbn.set(streamKey, { sbn, atMs: now });
1831
- anchorHere();
2036
+ advance();
1832
2037
  }
1833
2038
  /*
1834
2039
  * Block order for one stream (BLO-35597 iso:5). A block is keyed by its
@@ -2145,8 +2350,10 @@ var FECManager = class _FECManager {
2145
2350
  );
2146
2351
  }
2147
2352
  const hasCatalogTiming = _FECManager.#hasCatalogTiming({ interleaveDepthMs: ownInterleaveDepthMs });
2353
+ const algorithm = config.algorithm ?? previous.algorithm;
2148
2354
  const next = {
2149
2355
  blockProfile,
2356
+ ...algorithm === void 0 ? {} : { algorithm },
2150
2357
  k,
2151
2358
  symbolSize: config.symbolSize != null && config.symbolSize > 0 ? config.symbolSize : previousTrack?.symbolSize ?? 0,
2152
2359
  interleaveDepthMs: ownInterleaveDepthMs,
@@ -2154,8 +2361,19 @@ var FECManager = class _FECManager {
2154
2361
  timeoutMs: config.timeoutMs ?? (timingChanged ? 0 : previous.timeoutMs ?? 0),
2155
2362
  ...(config.trackKind ?? previous.trackKind) === void 0 ? {} : { trackKind: config.trackKind ?? previous.trackKind },
2156
2363
  ...(config.codec ?? previous.codec) === void 0 ? {} : { codec: config.codec ?? previous.codec },
2157
- ...(config.geometry ?? previous.geometry) === void 0 ? {} : { geometry: config.geometry ?? previous.geometry }
2364
+ ...(config.geometry ?? previous.geometry) === void 0 ? {} : { geometry: config.geometry ?? previous.geometry },
2365
+ ...(config.repairLayers ?? previous.repairLayers) === void 0 ? {} : { repairLayers: config.repairLayers ?? previous.repairLayers },
2366
+ ...(config.heldLayers ?? previous.heldLayers) === void 0 ? {} : { heldLayers: config.heldLayers ?? previous.heldLayers }
2158
2367
  };
2368
+ if (
2369
+ // By content: a catalog refresh rebuilds equal arrays, and forgetting
2370
+ // the anchors on one would fail open.
2371
+ JSON.stringify(next.repairLayers) !== JSON.stringify(previous.repairLayers) || JSON.stringify(next.heldLayers) !== JSON.stringify(previous.heldLayers)
2372
+ ) {
2373
+ for (const key of [...this.#heldLayerStates.keys()]) {
2374
+ if (this.#belongsToPacket(key, packetId)) this.#heldLayerStates.delete(key);
2375
+ }
2376
+ }
2159
2377
  if (!hasCatalogTiming) {
2160
2378
  next.timeoutMs = config.timeoutMs !== void 0 && config.timeoutMs > 0 ? config.timeoutMs : previousTrack !== void 0 && previousTrack.interleaveDepthMs === 0 ? previousTrack.timeoutMs : LOCAL_FEC_TIMEOUT_MS;
2161
2379
  } else {
@@ -2390,6 +2608,8 @@ var FECManager = class _FECManager {
2390
2608
  resetMap(this.#blockAuthoredLengths);
2391
2609
  resetMap(this.#blockRepairRsIds);
2392
2610
  resetMap(this.#blockRsbLengths);
2611
+ resetMap(this.#heldLayerStates);
2612
+ resetMap(this.#fecFailClosed);
2393
2613
  resetMap(this.#blockPathCounts);
2394
2614
  resetMap(this.#blockAnchorsMs);
2395
2615
  resetMap(this.#blockCatalogTrackNames);
@@ -2412,6 +2632,7 @@ var FECManager = class _FECManager {
2412
2632
  resetSet(this.#materializedBlocks);
2413
2633
  resetMap(this.#resolvedBlockClasses);
2414
2634
  resetMap(this.#highestSbn);
2635
+ resetMap(this.#sourceFrontiers);
2415
2636
  resetMap(this.#isoLocators);
2416
2637
  resetMap(this.#sourceAnchors);
2417
2638
  this.#classifiers.delete(this.#streamKey(packetId, catalogTrackName));
@@ -2517,6 +2738,7 @@ var FECManager = class _FECManager {
2517
2738
  dropMap(this.#orderedPendingBlocks);
2518
2739
  dropMap(this.#resolvedBlockClasses);
2519
2740
  dropMap(this.#highestSbn);
2741
+ dropMap(this.#sourceFrontiers);
2520
2742
  dropMap(this.#isoLocators);
2521
2743
  dropMap(this.#sourceAnchors);
2522
2744
  dropMap(this.#classifiers);
@@ -2609,7 +2831,13 @@ var FECManager = class _FECManager {
2609
2831
  if (isSourceSymbol && esi === 0) {
2610
2832
  this.#startupPartialBlocks.delete(blockKey);
2611
2833
  this.#startupAlignedPackets.add(streamKey);
2612
- } else if (isStartupPartial && !isReadyMulticastRepair || !this.#startupAlignedPackets.has(streamKey) && !isReadyMulticastRepair) {
2834
+ } else if (
2835
+ // A ready multicast repair opens a block the join has not seen yet, never
2836
+ // a join partial: its sources were refused, so its repairs alone open a
2837
+ // block that cannot decode and fails at its deadline, once per packet ID
2838
+ // per join (BLO-48308).
2839
+ isStartupPartial || !this.#startupAlignedPackets.has(streamKey) && !isReadyMulticastRepair
2840
+ ) {
2613
2841
  this.#markStartupPartial(blockKey);
2614
2842
  return false;
2615
2843
  }
@@ -2891,6 +3119,7 @@ var FECManager = class _FECManager {
2891
3119
  let locator = this.#isoLocators.get(streamKey);
2892
3120
  if (!locator) {
2893
3121
  locator = new IsoBlockLocator(this.#trackConfig(packetId).k, this.#isoRetentionBlocks(packetId));
3122
+ locator.deferIncompleteGaps = this.#deferIsoGaps;
2894
3123
  this.#isoLocators.set(streamKey, locator);
2895
3124
  }
2896
3125
  return locator;
@@ -2934,6 +3163,10 @@ var FECManager = class _FECManager {
2934
3163
  }
2935
3164
  return blocks;
2936
3165
  }
3166
+ /** The iso-ssbg1-v1 locator horizon of a configured packetId, in blocks (#isoRetentionBlocks). */
3167
+ isoRetentionBlocksForPacket(packetId) {
3168
+ return this.#hasBlockProfile(packetId) ? this.#isoRetentionBlocks(packetId) : void 0;
3169
+ }
2937
3170
  /** Whether an iso-ssbg1-v1 protected packet fits one ssbg_mode1 symbol (len + 2 <= T). */
2938
3171
  isoSourceFits(packetId, packetLength) {
2939
3172
  const symbolSize = this.#trackConfigs.get(packetId)?.symbolSize;
@@ -2983,15 +3216,61 @@ var FECManager = class _FECManager {
2983
3216
  }
2984
3217
  return placement;
2985
3218
  }
2986
- if (placement.span !== void 0) {
2987
- const key = this.#blockKey(placement.span.block.start, packetId, catalogTrackName);
2988
- if (!this.#blockAuthoredLengths.has(key)) this.#blockAuthoredLengths.set(key, placement.span.block.length);
2989
- }
2990
- if (placement.span !== void 0 && placement.span.placed.length === 0) {
2991
- this.#materializeBlock(placement.span.block.start, packetId, catalogTrackName, true);
3219
+ if (placement.span !== void 0) this.#openIsoSpan(placement.span, packetId, catalogTrackName);
3220
+ if (placement.hole !== void 0) {
3221
+ const locator = this.#isoLocatorFor(packetId, catalogTrackName);
3222
+ const hole = placement.hole.block;
3223
+ const previous = locator.blockEndingAt(hole.start);
3224
+ const loss = {
3225
+ packetId,
3226
+ catalogTrackName,
3227
+ holeStart: hole.start,
3228
+ holeLength: hole.length,
3229
+ lostSsId: placement.hole.lostSsId,
3230
+ kMax: this.#trackConfig(packetId).k,
3231
+ ...previous === void 0 ? {} : { previousBlockStart: previous.start }
3232
+ };
3233
+ this.#onUndelimitedLoss(loss);
2992
3234
  }
2993
3235
  return placement;
2994
3236
  }
3237
+ /**
3238
+ * Keep an iso-ssbg1-v1 gap missing a source as a pending hole, reported to
3239
+ * `handler`, instead of a repair-less span; null restores spans. Set by the
3240
+ * range repair scheduler; applies to every stream from its next gap. The
3241
+ * handler and the deferral are one setting, so no hole is ever created with
3242
+ * nobody to close it.
3243
+ */
3244
+ setUndelimitedLossHandler(handler) {
3245
+ this.#onUndelimitedLoss = handler;
3246
+ this.#deferIsoGaps = handler !== null;
3247
+ for (const locator of this.#isoLocators.values()) locator.deferIncompleteGaps = this.#deferIsoGaps;
3248
+ }
3249
+ /**
3250
+ * The range repair for a hole ended: every hole still pending inside
3251
+ * `[start, start + length)` becomes a repair-less span. Returns those spans
3252
+ * with the held sources they placed, for the caller to feed (MmtpRouter).
3253
+ */
3254
+ closeIsoHoles(packetId, catalogTrackName, start, length) {
3255
+ const locator = this.#isoLocators.get(this.#streamKey(packetId, catalogTrackName));
3256
+ if (locator === void 0) return [];
3257
+ const spans = locator.closeHolesWithin(start, length);
3258
+ for (const span of spans) this.#openIsoSpan(span, packetId, catalogTrackName);
3259
+ return spans;
3260
+ }
3261
+ /**
3262
+ * A repair-less span has no repair to state its length, so it is known
3263
+ * here: its sources make it clean exactly when all of them arrived. A
3264
+ * stated block learns its SSB_length from its own repair symbol, fed after
3265
+ * its placed sources exactly as on the wire (feedRepairSymbol). A span every
3266
+ * SS_ID of which vanished resolves through its deadline like any vanished
3267
+ * block.
3268
+ */
3269
+ #openIsoSpan(span, packetId, catalogTrackName) {
3270
+ const key = this.#blockKey(span.block.start, packetId, catalogTrackName);
3271
+ if (!this.#blockAuthoredLengths.has(key)) this.#blockAuthoredLengths.set(key, span.block.length);
3272
+ if (span.placed.length === 0) this.#materializeBlock(span.block.start, packetId, catalogTrackName, true);
3273
+ }
2995
3274
  /**
2996
3275
  * Resolution of the SS_IDs strictly between two released frames' SS_IDs on
2997
3276
  * an iso-ssbg1-v1 stream, for in-order frame release: null while a block
@@ -3508,6 +3787,112 @@ var FECManager = class _FECManager {
3508
3787
  }
3509
3788
  return recovered;
3510
3789
  }
3790
+ /**
3791
+ * fec-01 4.5: a reed-solomon repair packet whose SSB_length + RSB_length
3792
+ * exceeds 255, or whose RS_ID is not below RSB_length, is discarded with the
3793
+ * block state unchanged. The router calls this before the repair places a
3794
+ * block or takes any other part in block bookkeeping. A RaptorQ track admits
3795
+ * every header that parsed (its RS_ID < RSB_length check is in the parser).
3796
+ */
3797
+ admitRepairBlockLength(packetId, ssbLength, rsbLength, rsId) {
3798
+ if (rsId >= rsbLength) return false;
3799
+ if (this.#trackConfigs.get(packetId)?.algorithm !== "reed-solomon") return true;
3800
+ return ssbLength + rsbLength <= RS_MAX_BLOCK_SYMBOLS;
3801
+ }
3802
+ /**
3803
+ * Whether a repair symbol of `byteLength` bytes is a valid member of a
3804
+ * `packetId` block: every repair symbol is exactly the track's T. A symbol of
3805
+ * any other size is counted on `repairSymbolSizeMismatches`, warned (rate
3806
+ * limited) and refused. The router calls this before the symbol takes any
3807
+ * other part in block state (placement, first-arrival dedup, completed-block
3808
+ * accounting), so a malformed repair is counted whatever its arrival order:
3809
+ * judged after dedup, one landing on an already-complete block, or behind a
3810
+ * valid copy from the other transport, was swallowed as a duplicate and never
3811
+ * counted (BLO-48306). A packetId with no configured block profile admits
3812
+ * nothing here; its repair is dropped upstream as unreadable.
3813
+ */
3814
+ admitRepairSymbolSize(packetId, ssStart, ssbLength, rsId, byteLength) {
3815
+ if (!this.#hasBlockProfile(packetId)) return false;
3816
+ const symbolSize = this.#trackConfig(packetId).symbolSize;
3817
+ if (symbolSize > 0 && byteLength !== symbolSize) {
3818
+ this.#repairSymbolSizeMismatchCount++;
3819
+ if (this.#repairSymbolSizeMismatchCount <= 5 || this.#repairSymbolSizeMismatchCount % 100 === 0) {
3820
+ console.warn(
3821
+ `[FECManager] Dropping repair symbol with invalid size: got ${byteLength}B, expected ${symbolSize}B (packetId=${packetId}, ssStart=${ssStart}, ssbLength=${ssbLength}, rsId=${rsId})`
3822
+ );
3823
+ }
3824
+ return false;
3825
+ }
3826
+ return true;
3827
+ }
3828
+ /**
3829
+ * fec-01 6.3.9 receiver check, run on a repair packet after the 4.5 discard
3830
+ * and the size check, before it places a block. For each held layer i with
3831
+ * total B_i = O_i + P_i: the anchor is the earliest block, in SS_Start
3832
+ * serial order, whose repair carried RSB_length >= B_i. The source track
3833
+ * fails closed on (a) a packet on layer i's own repair track (i > 0) below
3834
+ * B_i, or (b) any packet of the instance at or after the anchor below B_i.
3835
+ * Blocks before the anchor (pre-append) stay valid at the previous total. A
3836
+ * packet on a repair track the catalog does not list is discarded and
3837
+ * anchors nothing. `repairTrackName` is the subscribed repair track, never
3838
+ * inferred from RS_ID; without it only (b) runs. Returns false when the
3839
+ * repair must be dropped, including every repair once the track failed.
3840
+ */
3841
+ admitRepairLayerTotal(packetId, catalogTrackName, repairTrackName, ssStart, rsbLength) {
3842
+ const streamKey = this.#streamKey(packetId, catalogTrackName);
3843
+ if (this.#fecFailClosed.has(streamKey)) return false;
3844
+ const config = this.#trackConfigs.get(packetId);
3845
+ const layers = config?.repairLayers;
3846
+ if (layers === void 0 || layers.length === 0) return true;
3847
+ let admission = this.#heldLayerStates.get(streamKey);
3848
+ if (admission === void 0) {
3849
+ admission = new HeldLayerAdmission(layers, config.heldLayers);
3850
+ this.#heldLayerStates.set(streamKey, admission);
3851
+ }
3852
+ const verdict = admission.admit(
3853
+ repairTrackName,
3854
+ ssStart,
3855
+ rsbLength,
3856
+ this.#layerAnchorHorizon(packetId),
3857
+ this.#sourceFrontiers.get(streamKey)?.sbn
3858
+ );
3859
+ if (verdict === "discard") return false;
3860
+ if (verdict !== void 0) {
3861
+ this.#fecFailClosed.set(streamKey, verdict);
3862
+ this.#onFecFailClosed?.({ ...verdict, packetId, catalogTrackName });
3863
+ console.warn(
3864
+ `[FECManager] FEC failed closed (fec-01 6.3.9) packetId=${packetId} track=${catalogTrackName ?? "-"}: ${JSON.stringify(verdict)}`
3865
+ );
3866
+ return false;
3867
+ }
3868
+ return true;
3869
+ }
3870
+ /**
3871
+ * How far (in SS_Start symbols) behind the newest block a 6.3.9 anchor or
3872
+ * below-total block can still matter: the iso locator's retention in blocks
3873
+ * times K, capped at 2^30 so compared serials stay inside one half-window.
3874
+ */
3875
+ #layerAnchorHorizon(packetId) {
3876
+ let blocks;
3877
+ try {
3878
+ blocks = this.isoRetentionBlocksForPacket(packetId);
3879
+ } catch {
3880
+ blocks = void 0;
3881
+ }
3882
+ return heldLayerAnchorHorizon(blocks, this.#trackConfigs.get(packetId)?.k ?? 0);
3883
+ }
3884
+ /**
3885
+ * Report each stream whose FEC fails closed at runtime (fec-01 6.3.9), once,
3886
+ * when it does; null stops reporting. The player wiring forwards it to
3887
+ * CmcdProducer.updateFecFailClosed.
3888
+ */
3889
+ setFecFailClosedHandler(handler) {
3890
+ this.#onFecFailClosed = handler;
3891
+ }
3892
+ /** The runtime fail-closed reason of a stream's FEC (fec-01 6.3.9), if any. */
3893
+ fecFailClosedReason(packetId, catalogTrackName) {
3894
+ return this.#fecFailClosed.get(this.#streamKey(packetId, catalogTrackName));
3895
+ }
3511
3896
  /**
3512
3897
  * Feed a repair symbol to the WASM FEC decoder.
3513
3898
  *
@@ -3519,16 +3904,7 @@ var FECManager = class _FECManager {
3519
3904
  this.#fecStats.repairPackets++;
3520
3905
  this.#statsForPacket(packetId).repairPackets++;
3521
3906
  if (!this.#hasBlockProfile(packetId)) return null;
3522
- const symbolSize = this.#trackConfig(packetId).symbolSize;
3523
- if (symbolSize > 0 && repairData.byteLength !== symbolSize) {
3524
- this.#repairSymbolSizeMismatchCount++;
3525
- if (this.#repairSymbolSizeMismatchCount <= 5 || this.#repairSymbolSizeMismatchCount % 100 === 0) {
3526
- console.warn(
3527
- `[FECManager] Dropping repair symbol with invalid size: got ${repairData.byteLength}B, expected ${symbolSize}B (packetId=${packetId}, ssStart=${ssStart}, ssbLength=${ssbLength}, rsId=${rsId})`
3528
- );
3529
- }
3530
- return null;
3531
- }
3907
+ if (!this.admitRepairSymbolSize(packetId, ssStart, ssbLength, rsId, repairData.byteLength)) return null;
3532
3908
  const fecData = repairData;
3533
3909
  const k = this.#trackConfig(packetId).k;
3534
3910
  const block = this.#isoLocatorFor(packetId, catalogTrackName).blockAt(ssStart);
@@ -3681,7 +4057,14 @@ var FECManager = class _FECManager {
3681
4057
  const key = this.#blockKey(sbn, packetId, catalogTrackName);
3682
4058
  const k = trackConfig.k;
3683
4059
  if (this.#completedBlocks.has(key)) return;
3684
- this.#noteSbnSeen(sbn, packetId, catalogTrackName, "source", evidence.packetSequenceNumber);
4060
+ this.#noteSbnSeen(
4061
+ sbn,
4062
+ packetId,
4063
+ catalogTrackName,
4064
+ "source",
4065
+ evidence.packetSequenceNumber,
4066
+ Number.isInteger(esi) ? sbn + esi >>> 0 : sbn
4067
+ );
3685
4068
  const authoredLength = this.#blockAuthoredLengths.get(key) ?? k;
3686
4069
  if (Number.isInteger(esi) && esi >= authoredLength) {
3687
4070
  this.#failBlock(sbn, packetId, catalogTrackName);
@@ -3908,6 +4291,8 @@ var FECManager = class _FECManager {
3908
4291
  this.#blockAuthoredLengths.clear();
3909
4292
  this.#blockRepairRsIds.clear();
3910
4293
  this.#blockRsbLengths.clear();
4294
+ this.#heldLayerStates.clear();
4295
+ this.#fecFailClosed.clear();
3911
4296
  this.#completedBlocks.clear();
3912
4297
  this.#cleanlyCompletedBlocks.clear();
3913
4298
  this.#repairRequestedBlocks.clear();
@@ -3932,6 +4317,7 @@ var FECManager = class _FECManager {
3932
4317
  this.#repairedBlocks.clear();
3933
4318
  this.#materializedBlocks.clear();
3934
4319
  this.#highestSbn.clear();
4320
+ this.#sourceFrontiers.clear();
3935
4321
  this.#isoLocators.clear();
3936
4322
  this.#sourceAnchors.clear();
3937
4323
  this.#trackConfigs.clear();