@blockcast/mmt-container 0.4.0 → 0.4.1-main.30f0611c24e8

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 (215) hide show
  1. package/README.md +36 -20
  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 +1904 -692
  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 +8 -0
  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/manifest-auth-plan.d.ts +26 -25
  55. package/dist/manifest-auth-plan.d.ts.map +1 -1
  56. package/dist/manifest-auth-plan.js +331 -1171
  57. package/dist/manifest-auth-plan.js.map +4 -4
  58. package/dist/mfu-reassembler.js +21 -13
  59. package/dist/mfu-reassembler.js.map +2 -2
  60. package/dist/mmtp-container-parser.d.ts.map +1 -1
  61. package/dist/mmtp-container-parser.js +93 -22
  62. package/dist/mmtp-container-parser.js.map +2 -2
  63. package/dist/mmtp-container-parser.no-modules.js +1250 -317
  64. package/dist/mmtp-container-parser.no-modules.js.map +4 -4
  65. package/dist/mmtp-decode-pipeline.js +93 -22
  66. package/dist/mmtp-decode-pipeline.js.map +2 -2
  67. package/dist/mmtp-fec-session.d.ts +192 -0
  68. package/dist/mmtp-fec-session.d.ts.map +1 -0
  69. package/dist/mmtp-fec-session.js +640 -0
  70. package/dist/mmtp-fec-session.js.map +7 -0
  71. package/dist/mmtp-router.d.ts +39 -6
  72. package/dist/mmtp-router.d.ts.map +1 -1
  73. package/dist/mmtp-router.js +184 -27
  74. package/dist/mmtp-router.js.map +2 -2
  75. package/dist/multicast-auth-catalog.d.ts +86 -92
  76. package/dist/multicast-auth-catalog.d.ts.map +1 -1
  77. package/dist/multicast-auth-catalog.js +347 -132
  78. package/dist/multicast-auth-catalog.js.map +4 -4
  79. package/dist/multicast-input.d.ts +31 -8
  80. package/dist/multicast-input.d.ts.map +1 -1
  81. package/dist/multicast-input.js +299 -35
  82. package/dist/multicast-input.js.map +3 -3
  83. package/dist/multicast-manager.d.ts +94 -5
  84. package/dist/multicast-manager.d.ts.map +1 -1
  85. package/dist/multicast-manager.js +908 -357
  86. package/dist/multicast-manager.js.map +4 -4
  87. package/dist/multicast-transport.d.ts +12 -2
  88. package/dist/multicast-transport.d.ts.map +1 -1
  89. package/dist/multicast-transport.js +108 -238
  90. package/dist/multicast-transport.js.map +4 -4
  91. package/dist/multicast-unwrap.js +21 -13
  92. package/dist/multicast-unwrap.js.map +2 -2
  93. package/dist/no-modules.d.ts +4 -1
  94. package/dist/no-modules.d.ts.map +1 -1
  95. package/dist/no-modules.js +1195 -301
  96. package/dist/no-modules.js.map +4 -4
  97. package/dist/phase14-catalog-override.d.ts +1 -1
  98. package/dist/phase14-catalog-override.d.ts.map +1 -1
  99. package/dist/phase14-catalog-override.js +2 -2
  100. package/dist/phase14-catalog-override.js.map +2 -2
  101. package/dist/repair-fetch.d.ts +81 -6
  102. package/dist/repair-fetch.d.ts.map +1 -1
  103. package/dist/repair-fetch.js +156 -31
  104. package/dist/repair-fetch.js.map +3 -3
  105. package/dist/repair-layers.d.ts +2 -0
  106. package/dist/repair-layers.d.ts.map +1 -1
  107. package/dist/repair-layers.js +19 -2
  108. package/dist/repair-layers.js.map +2 -2
  109. package/dist/sample-combiner.d.ts +7 -1
  110. package/dist/sample-combiner.d.ts.map +1 -1
  111. package/dist/sample-combiner.js +14 -2
  112. package/dist/sample-combiner.js.map +2 -2
  113. package/dist/signaling.d.ts +2 -2
  114. package/dist/signaling.d.ts.map +1 -1
  115. package/dist/signaling.js +3 -3
  116. package/dist/signaling.js.map +2 -2
  117. package/dist/source-auth.js +2710 -7
  118. package/dist/source-auth.js.map +4 -4
  119. package/dist/switching-set-alignment.d.ts +1 -1
  120. package/dist/switching-set-alignment.js.map +1 -1
  121. package/dist/timestamp-codec.js +21 -13
  122. package/dist/timestamp-codec.js.map +2 -2
  123. package/dist/wasm-mfu-reassembler.js.map +1 -1
  124. package/dist/wasm-mmtp-container-parser.js +93 -22
  125. package/dist/wasm-mmtp-container-parser.js.map +2 -2
  126. package/package.json +6 -9
  127. package/schemas/catalog.schema.json +133 -38
  128. package/src/align.ts +52 -21
  129. package/src/carp-catalog.ts +188 -68
  130. package/src/catalog-fec-params.ts +28 -3
  131. package/src/catalog-network-source.ts +49 -7
  132. package/src/catalog-validator.ts +4 -11
  133. package/src/cmcd-producer.ts +17 -0
  134. package/src/fec-manager.ts +366 -63
  135. package/src/fec-state.ts +11 -4
  136. package/src/fec-worker-setup.ts +30 -2
  137. package/src/held-layer-admission.ts +161 -0
  138. package/src/index.ts +27 -10
  139. package/src/iso-block-locator.ts +110 -7
  140. package/src/manifest-auth-plan.ts +39 -91
  141. package/src/mmtp-container-parser.ts +75 -7
  142. package/src/mmtp-fec-session.ts +606 -0
  143. package/src/mmtp-router.ts +166 -7
  144. package/src/multicast-auth-catalog.ts +131 -238
  145. package/src/multicast-input.ts +148 -124
  146. package/src/multicast-manager.ts +267 -21
  147. package/src/multicast-transport.ts +17 -3
  148. package/src/no-modules.ts +7 -1
  149. package/src/phase14-catalog-override.ts +3 -3
  150. package/src/repair-fetch.ts +283 -39
  151. package/src/repair-layers.ts +22 -2
  152. package/src/sample-combiner.ts +15 -2
  153. package/src/signaling.ts +2 -2
  154. package/src/switching-set-alignment.ts +1 -1
  155. package/test-vectors/align.json +108 -32
  156. package/test-vectors/bc-provenance-v2.json +1526 -0
  157. package/test-vectors/catalog/README.md +71 -4
  158. package/test-vectors/catalog/negative/fec-block-profile-missing.json +1 -1
  159. package/test-vectors/catalog/negative/fec-block-profile-retired.json +1 -1
  160. package/test-vectors/catalog/negative/fec-block-profile-unknown.json +1 -1
  161. package/test-vectors/catalog/negative/fec-iso-symbol-size-below-floor.json +28 -0
  162. package/test-vectors/catalog/negative/fec-reed-solomon-iso-symbol-size-13.json +28 -0
  163. package/test-vectors/catalog/negative/fec-reed-solomon-k-plus-p-over-255.json +39 -0
  164. package/test-vectors/catalog/negative/fec-reed-solomon-overlay-over-255.json +40 -0
  165. package/test-vectors/catalog/negative/fec-repair-overlay-source-symbols-zero.json +43 -0
  166. package/test-vectors/catalog/negative/fec-repair-symbols-zero.json +28 -0
  167. package/test-vectors/catalog/negative/fec-source-symbols-zero.json +28 -0
  168. package/test-vectors/catalog/negative/fec-symbol-size-zero.json +28 -0
  169. package/test-vectors/catalog/negative/init-data-inline-without-data.json +22 -0
  170. package/test-vectors/catalog/negative/mmtp-mode-mpu.json +15 -0
  171. package/test-vectors/catalog/negative/multicast-amt-dns-without-relay.json +37 -0
  172. package/test-vectors/catalog/negative/multicast-amt-driad-with-relay.json +38 -0
  173. package/test-vectors/catalog/negative/multicast-auth-asm-missing-sender-address.json +77 -0
  174. package/test-vectors/catalog/negative/multicast-auth-duplicate-first-epoch.json +56 -0
  175. package/test-vectors/catalog/negative/multicast-auth-duplicate-kid.json +56 -0
  176. package/test-vectors/catalog/negative/multicast-auth-endpoint-auth-without-object-auth.json +38 -0
  177. package/test-vectors/catalog/negative/multicast-auth-endpoint-missing-auth.json +49 -0
  178. package/test-vectors/catalog/negative/multicast-auth-endpoint-missing-bandwidth.json +55 -0
  179. package/test-vectors/catalog/negative/multicast-auth-manifest-port-equals-port.json +56 -0
  180. package/test-vectors/catalog/negative/multicast-auth-manifest-timer-out-of-range.json +56 -0
  181. package/test-vectors/catalog/negative/multicast-auth-max-auth-delay-above-max.json +56 -0
  182. package/test-vectors/catalog/negative/multicast-auth-max-auth-delay-below-min.json +56 -0
  183. package/test-vectors/catalog/negative/multicast-auth-root-dotted-key.json +55 -0
  184. package/test-vectors/catalog/negative/multicast-auth-sender-address-on-ssm.json +57 -0
  185. package/test-vectors/catalog/negative/multicast-auth-version-1.json +56 -0
  186. package/test-vectors/catalog/negative/multicast-auth-version-3.json +56 -0
  187. package/test-vectors/catalog/negative/multicast-auth-version-missing.json +55 -0
  188. package/test-vectors/catalog/negative/multicast-endpoint-foreign-namespace-track.json +44 -0
  189. package/test-vectors/catalog/negative/multicast-endpoint-protocol-amt.json +38 -0
  190. package/test-vectors/catalog/negative/multicast-endpoint-protocol-atsc3.json +38 -0
  191. package/test-vectors/catalog/negative/presentation-anchor-unsafe.json +16 -0
  192. package/test-vectors/catalog/positive/fec-algorithm-unimplemented.json +29 -0
  193. package/test-vectors/catalog/positive/fec-iso-ssbg1.json +1 -1
  194. package/test-vectors/catalog/positive/fec-multicast.json +1 -1
  195. package/test-vectors/catalog/positive/fec-reed-solomon-layered.json +50 -0
  196. package/test-vectors/catalog/positive/mmt01-12-1-init-ref-anchor.json +29 -0
  197. package/test-vectors/catalog/positive/multicast-amt-discovery-omitted.json +37 -0
  198. package/test-vectors/catalog/positive/multicast-amt-dns-relay.json +38 -0
  199. package/test-vectors/catalog/positive/multicast-amt-manual-relay.json +3 -3
  200. package/test-vectors/catalog/{negative/multicast-endpoint-missing-protocol-and-source.json → positive/multicast-asm-unprotected.json} +10 -5
  201. package/test-vectors/catalog/positive/multicast-auth-asm-sender.json +78 -0
  202. package/test-vectors/catalog/positive/multicast-auth-rotation.json +39 -38
  203. package/test-vectors/catalog/positive/multicast-draft01-4-1-example.json +81 -0
  204. package/test-vectors/catalog/positive/multicast-draft01-7-2-6-example.json +70 -0
  205. package/test-vectors/catalog/positive/presentation-anchor-negative.json +27 -0
  206. package/test-vectors/catalog/section-codes.json +31 -1
  207. package/test-vectors/catalog-multicast-auth-v1.json +38 -20
  208. package/test-vectors/catalog-validation.json +101 -5
  209. package/test-vectors/catalog-validator-public-api.mjs +14 -9
  210. package/test-vectors/generate-bc-provenance-v2.mjs +252 -0
  211. package/dist/fec-codec.d.ts +0 -32
  212. package/dist/fec-codec.d.ts.map +0 -1
  213. package/dist/fec-codec.js +0 -47
  214. package/dist/fec-codec.js.map +0 -7
  215. package/src/fec-codec.ts +0 -61
package/src/fec-state.ts CHANGED
@@ -13,7 +13,12 @@
13
13
  // timers or elapsed time. The rules live here so the three players cannot
14
14
  // disagree about precedence.
15
15
 
16
- import { isFecBlockProfile, isoBlockSpanUsable } from '@blockcast/mmt-base'
16
+ import {
17
+ catalogFecDeclaresUnconfigurableInstance,
18
+ fecAlgorithmUnimplemented,
19
+ isFecBlockProfile,
20
+ isoBlockSpanUsable,
21
+ } from '@blockcast/mmt-base'
17
22
 
18
23
  /** The optional elapsed-span rule lives in mmt-base beside the other catalog FEC rules. */
19
24
  export { isoBlockSpanUsable }
@@ -103,7 +108,8 @@ function isPositiveSafeInteger(value: unknown): boolean {
103
108
  * Whether one catalog `fec` block (a hang rendition's `fec` or an MSF/CMSF
104
109
  * track's `fec`) declares an FEC instance a receiver can configure: source
105
110
  * symbols per block (K) and a symbol size (T) are both positive integers, the
106
- * algorithm is not `none`, `blockProfile` names the implemented profile
111
+ * algorithm is neither `none` nor a token this receiver does not implement
112
+ * (`fecAlgorithmUnimplemented`, draft-ramadan-moq-fec-01 4), `blockProfile` names the implemented profile
107
113
  * `iso-ssbg1-v1` (BLO-35597), and the block carries that profile's usable
108
114
  * span field (`isoBlockSpanUsable`); zero/absence still permits in-band FEC. Total: it never throws, and answers false for
109
115
  * any other value.
@@ -141,12 +147,13 @@ function isPositiveSafeInteger(value: unknown): boolean {
141
147
  */
142
148
  export function catalogDeclaresFec(fec: unknown): boolean {
143
149
  const block = recordOf(fec)
144
- if (block === undefined || block['algorithm'] === 'none') return false
150
+ if (block === undefined || block['algorithm'] === 'none' || fecAlgorithmUnimplemented(block['algorithm'])) return false
145
151
  return (
146
152
  isPositiveSafeInteger(block['sourceSymbols']) &&
147
153
  isPositiveSafeInteger(block['symbolSize']) &&
148
154
  isFecBlockProfile(block['blockProfile']) &&
149
- isoBlockSpanUsable(block)
155
+ isoBlockSpanUsable(block) &&
156
+ !catalogFecDeclaresUnconfigurableInstance(block)
150
157
  )
151
158
  }
152
159
 
@@ -16,10 +16,12 @@
16
16
  import { parseFecBlockProfile, type FecBlockProfile } from '@blockcast/mmt-base'
17
17
  import type { FecWorkerClient, FecTrackConfig } from '@blockcast/fec-worker'
18
18
  import type { MulticastManager } from './multicast-manager.js'
19
+ import type { FecRepairLayer } from './fec-manager.js'
19
20
  import {
20
21
  deriveFecRetentionBlocks,
21
22
  LOCAL_FEC_RETENTION_BLOCKS,
22
23
  LOCAL_FEC_TIMEOUT_MS,
24
+ MAX_FEC_RETENTION_BLOCKS,
23
25
  } from '@blockcast/mmt-fec'
24
26
  import { computeFecDeliveryWindowMs } from './track-session.js'
25
27
 
@@ -47,6 +49,8 @@ export interface FecWorkerSetupOptions {
47
49
  * Worker is created. Never defaulted, never inferred.
48
50
  */
49
51
  fecBlockProfile: FecBlockProfile
52
+ /** The track's FEC scheme (`TrackFecParams.fecAlgorithm`). Defaults to RaptorQ. */
53
+ fecAlgorithm?: 'raptorq' | 'reed-solomon'
50
54
  /**
51
55
  * FEC K from catalog: the maximum SSB_length (K_max) of any block. Blocks
52
56
  * are variable-length ssbg_mode1 blocks keyed by SS_Start, so K bounds
@@ -77,6 +81,13 @@ export interface FecWorkerSetupOptions {
77
81
  mediaUnitDurationMs?: number
78
82
  /** Optional repair track name (MoQ). Forwarded to FecTrackConfig. */
79
83
  repairTrackName?: string
84
+ /**
85
+ * The catalog's base repair layers (`TrackFecParams.repairLayers`) and the
86
+ * layers this receiver holds (default `[0]`), for the fec-01 6.3.9 anchored
87
+ * RSB_length check. Absent layers: no check.
88
+ */
89
+ fecRepairLayers?: readonly FecRepairLayer[]
90
+ heldRepairLayers?: readonly number[]
80
91
  /** Audio sample rate (optional — audio tracks only). */
81
92
  sampleRate?: number
82
93
  /** Codec string (optional; "unknown" is acceptable for first setup). */
@@ -237,7 +248,7 @@ export async function setupFecWorkerForTrack(
237
248
  trackType: opts.trackType,
238
249
  framerate: opts.framerate,
239
250
  sampleRate: opts.sampleRate,
240
- algorithm: 'raptor',
251
+ algorithm: opts.fecAlgorithm ?? 'raptorq',
241
252
  blockProfile,
242
253
  k: opts.fecK,
243
254
  // P as this receiver joined it, not the catalog total: the worker snapshot
@@ -252,10 +263,10 @@ export async function setupFecWorkerForTrack(
252
263
  relayBlockSigEnabled: false,
253
264
  } as unknown as FecTrackConfig
254
265
 
255
- client.configure(fecTrackConfig, rqWasm)
256
266
  try {
257
267
  opts.multicastManager.setFecWorker(opts.packetId, client, opts.trackType, {
258
268
  blockProfile,
269
+ algorithm: opts.fecAlgorithm ?? 'raptorq',
259
270
  k: opts.fecK,
260
271
  symbolSize: opts.fecSymbolSize,
261
272
  interleaveDepthMs: opts.fecInterleaveMs,
@@ -264,6 +275,8 @@ export async function setupFecWorkerForTrack(
264
275
  // or classification.
265
276
  ...(hasCatalogTiming ? { fps: 1000 / opts.mediaUnitDurationMs! } : {}),
266
277
  timeoutMs: deliveryWindowMs,
278
+ ...(opts.fecRepairLayers ? { repairLayers: opts.fecRepairLayers } : {}),
279
+ ...(opts.heldRepairLayers ? { heldLayers: opts.heldRepairLayers } : {}),
267
280
  }, opts.trackName)
268
281
  } catch (e) {
269
282
  // The manager refused the registration (for example the stream still
@@ -276,6 +289,21 @@ export async function setupFecWorkerForTrack(
276
289
  }
277
290
  throw e
278
291
  }
292
+ // The Worker retires a block `interleaveDepth` blocks behind the newest
293
+ // SS_ID it has seen, repair SS_Starts included, by the metric the manager's
294
+ // locator uses for its own horizon. Repairs ride their own group and can run
295
+ // blocks ahead of their media, so a shorter Worker horizon fails a block whose
296
+ // late sources the manager still waits for.
297
+ // ponytail: read once here; a keyframe cadence configured later widens only
298
+ // the manager's horizon (reconfigure the Worker if a player ever does that).
299
+ fecTrackConfig.interleaveDepth = Math.min(
300
+ MAX_FEC_RETENTION_BLOCKS,
301
+ Math.max(
302
+ interleaveDepthBlocks,
303
+ opts.multicastManager.fecManager.isoRetentionBlocksForPacket(opts.packetId) ?? 0,
304
+ ),
305
+ )
306
+ client.configure(fecTrackConfig, rqWasm)
279
307
 
280
308
  let disposed = false
281
309
  return {
@@ -0,0 +1,161 @@
1
+ /**
2
+ * The fec-01 6.3.9 receiver check (held-layer anchored RSB_length), in one
3
+ * place. FECManager and MmtpFecSession both run their repair packets through
4
+ * HeldLayerAdmission; neither restates the rule.
5
+ */
6
+
7
+ import { serial32Distance, serial32Precedes } from '@blockcast/mmt-base'
8
+
9
+ /**
10
+ * Why a source track's FEC failed closed at runtime (fec-01 6.3.9): a repair
11
+ * packet stated an RSB_length below a held layer's total B_i on that layer's
12
+ * own repair track (`below_own_track_total`), or at or after the anchor, the
13
+ * earliest block in SS_Start serial order whose repair carried >= B_i
14
+ * (`below_anchored_total`). Repair for the track is refused from then on.
15
+ */
16
+ export interface FecFailClosedReason {
17
+ code: 'below_own_track_total' | 'below_anchored_total'
18
+ layer: number
19
+ total: number
20
+ rsbLength: number
21
+ ssStart: number
22
+ anchor?: number
23
+ }
24
+
25
+ /** One repair layer as the catalog states it: B_i = rsIdOffset + repairSymbols. */
26
+ export interface HeldLayerInput {
27
+ layer: number
28
+ repairTrackName: string
29
+ rsIdOffset: number
30
+ repairSymbols: number
31
+ }
32
+
33
+ interface HeldLayerState {
34
+ layer: number
35
+ repairTrackName: string
36
+ total: number
37
+ /** Earliest (serial order) block start whose repair carried >= total. */
38
+ anchor?: number
39
+ /** Latest (serial order) block start whose repair carried < total. */
40
+ latestBelow?: number
41
+ /** RSB_length that latestBelow's repair carried, for the fail-closed reason. */
42
+ latestBelowRsb?: number
43
+ /** Newest block start seen; anchor and latestBelow are retired behind it. */
44
+ newest?: number
45
+ /**
46
+ * The anchor fell behind the horizon, so no block that can still arrive
47
+ * precedes it: every later packet must carry >= total. Its serial is no
48
+ * longer compared, so SS_Start wrapping past it cannot misorder it.
49
+ */
50
+ settled?: boolean
51
+ }
52
+
53
+ /** The largest horizon compared, so every compared serial stays in one half-window. */
54
+ const HORIZON_CAP = 0x40000000
55
+
56
+ /**
57
+ * How far (in SS_Start symbols) behind the newest block a 6.3.9 anchor or
58
+ * below-total block can still matter: the iso retention in blocks times K,
59
+ * capped at 2^30. Unknown inputs give the cap.
60
+ */
61
+ export function heldLayerAnchorHorizon(retentionBlocks: number | undefined, k: number): number {
62
+ return retentionBlocks === undefined || k <= 0 ? HORIZON_CAP : Math.min(HORIZON_CAP, retentionBlocks * k)
63
+ }
64
+
65
+ /**
66
+ * fec-01 6.3.9 anchored state for one FEC instance's held layers. For each
67
+ * held layer i with total B_i = O_i + P_i: the anchor is the earliest block,
68
+ * in SS_Start serial order, whose repair carried RSB_length >= B_i. The
69
+ * instance fails closed on (a) a packet on layer i's own repair track (i > 0)
70
+ * below B_i, or (b) any packet of the instance at or after the anchor below
71
+ * B_i. Blocks before the anchor (pre-append) stay valid at the previous total.
72
+ * The result is not latched here: the caller owns what failing closed means.
73
+ */
74
+ export class HeldLayerAdmission {
75
+ readonly #layerNames: ReadonlySet<string>
76
+ readonly #states: HeldLayerState[]
77
+
78
+ constructor(layers: readonly HeldLayerInput[], heldLayers: readonly number[] = [0]) {
79
+ const held = new Set(heldLayers)
80
+ this.#layerNames = new Set(layers.map((l) => l.repairTrackName))
81
+ this.#states = layers
82
+ .filter((l) => held.has(l.layer))
83
+ .map((l) => ({ layer: l.layer, repairTrackName: l.repairTrackName, total: l.rsIdOffset + l.repairSymbols }))
84
+ }
85
+
86
+ /**
87
+ * Judges one repair packet, after the 4.5 discards and the size check and
88
+ * before it places a block. `repairTrackName` is the subscribed repair
89
+ * track, never inferred from RS_ID; without it only (b) runs. A packet on a
90
+ * repair track the catalog does not list is `'discard'` and anchors nothing.
91
+ * `sourceFrontier` is the newest source SS_ID, if any: a repair more than
92
+ * `horizon` past it is judged without moving the anchored state.
93
+ *
94
+ * @returns undefined to admit, `'discard'`, or the fail-closed reason.
95
+ */
96
+ admit(
97
+ repairTrackName: string | undefined,
98
+ ssStart: number,
99
+ rsbLength: number,
100
+ horizon: number,
101
+ sourceFrontier?: number,
102
+ ): FecFailClosedReason | 'discard' | undefined {
103
+ if (repairTrackName !== undefined && !this.#layerNames.has(repairTrackName)) return 'discard'
104
+ // A repair may run ahead of the media, but one more than `horizon` past the
105
+ // source frontier must not move the held-layer state: advancing `newest` on
106
+ // it would retire every real anchor and skip this check until traffic caught
107
+ // up. Such a repair is still judged below the total: it is on its own track,
108
+ // or after any anchor by definition. With no anchor yet it says nothing.
109
+ // It is still admitted: in iso the frontier only moves when a repair places
110
+ // held sources, so dropping it would stall recovery after a long outage.
111
+ // IsoBlockLocator.placeRepair may still refuse the same repair as `ahead`:
112
+ // it measures from `newest`, which held sources move, not from this frontier.
113
+ const farAhead =
114
+ sourceFrontier !== undefined &&
115
+ serial32Precedes(sourceFrontier, ssStart) &&
116
+ serial32Distance(sourceFrontier, ssStart) > horizon
117
+ for (const state of this.#states) {
118
+ let code: FecFailClosedReason['code'] | undefined
119
+ if (farAhead) {
120
+ if (rsbLength < state.total) {
121
+ if (state.layer > 0 && repairTrackName === state.repairTrackName) code = 'below_own_track_total'
122
+ else if (state.anchor !== undefined || state.settled) code = 'below_anchored_total'
123
+ }
124
+ } else {
125
+ // Serials more than `horizon` behind the newest block are retired, so
126
+ // every serial still compared sits inside one half-window and SS_Start
127
+ // wrapping on a long-running stream cannot reorder them.
128
+ if (state.newest === undefined || serial32Precedes(state.newest, ssStart)) state.newest = ssStart
129
+ const stale = (s: number) => serial32Distance(s, state.newest!) > horizon
130
+ if (state.latestBelow !== undefined && stale(state.latestBelow)) {
131
+ delete state.latestBelow
132
+ delete state.latestBelowRsb
133
+ }
134
+ if (state.anchor !== undefined && stale(state.anchor)) state.settled = true
135
+ if (rsbLength < state.total) {
136
+ if (state.layer > 0 && repairTrackName === state.repairTrackName) code = 'below_own_track_total'
137
+ else if (stale(ssStart)) continue // past retention: undecodable, anchors nothing
138
+ else if (state.settled || (state.anchor !== undefined && !serial32Precedes(ssStart, state.anchor))) code = 'below_anchored_total'
139
+ else if (state.latestBelow === undefined || serial32Precedes(state.latestBelow, ssStart)) {
140
+ state.latestBelow = ssStart
141
+ state.latestBelowRsb = rsbLength
142
+ }
143
+ } else if (!state.settled && !stale(ssStart)) {
144
+ if (state.anchor === undefined || serial32Precedes(ssStart, state.anchor)) state.anchor = ssStart
145
+ if (state.latestBelow !== undefined && !serial32Precedes(state.latestBelow, state.anchor)) {
146
+ code = 'below_anchored_total'
147
+ ssStart = state.latestBelow
148
+ rsbLength = state.latestBelowRsb!
149
+ }
150
+ }
151
+ }
152
+ if (code !== undefined) {
153
+ return {
154
+ code, layer: state.layer, total: state.total, rsbLength, ssStart,
155
+ ...(state.anchor === undefined ? {} : { anchor: state.anchor }),
156
+ }
157
+ }
158
+ }
159
+ return undefined
160
+ }
161
+ }
package/src/index.ts CHANGED
@@ -121,7 +121,7 @@ export {
121
121
  } from './loc.js'
122
122
 
123
123
  // CARP Catalog Extensions (draft-law-ramadan-moq-carp-reliability §3-4)
124
- // + Track Timing (draft-ramadan-moq-mmt-00 §4.4.4)
124
+ // + Track Timing (draft-ramadan-moq-mmt-01 §4.4.2)
125
125
  export {
126
126
  type FecAlgorithm,
127
127
  type FecCatalogConfig,
@@ -173,7 +173,7 @@ export {
173
173
  } from './delivery-catalog.js'
174
174
 
175
175
  // Multicast input contract — validator + builder for publisher-emitted
176
- // catalogs (draft-ramadan-moq-multicast-00 §4.1, BLO-3323 A.1).
176
+ // catalogs (draft-ramadan-moq-multicast-01 §4.1, BLO-3323 A.1).
177
177
  // `multicastFromEnv` is the env-var helper used by FFmpeg and the
178
178
  // cast bridge (BLO-3337).
179
179
  export {
@@ -265,8 +265,12 @@ export {
265
265
  type WasmFecDecoder,
266
266
  type FecStats,
267
267
  type MulticastSymbolDelivery,
268
+ type UndelimitedIsoLoss,
268
269
  type FecBlockResolution,
269
270
  type RepairVia,
271
+ type FecRepairLayer,
272
+ type FecFailClosedReason,
273
+ type FecFailClosedEvent,
270
274
  } from './fec-manager.js'
271
275
 
272
276
  // MMTP Router (packet processing + frame routing)
@@ -325,14 +329,6 @@ export {
325
329
  type WasmMmtpParserConfig,
326
330
  } from './wasm-mmtp-container-parser.js'
327
331
 
328
- // FEC Codec (generic, algorithm-pluggable)
329
- export {
330
- type FecCodec,
331
- NoneCodec,
332
- ReedSolomonCodec,
333
- createFecCodec,
334
- } from './fec-codec.js'
335
-
336
332
  // Multicast Transport (SSM join/socket/watchdog)
337
333
  export {
338
334
  MulticastTransport,
@@ -554,6 +550,7 @@ export {
554
550
  catalogFecUnavailable,
555
551
  catalogFecAvailable,
556
552
  catalogFecDeclaresUnconfigurableInstance,
553
+ fecAlgorithmUnimplemented,
557
554
  locateCatalogFecSource,
558
555
  type CatalogFecRole,
559
556
  type CatalogFecSource,
@@ -616,10 +613,26 @@ export {
616
613
  type NoFecCounters,
617
614
  } from "./fec-state.js";
618
615
 
616
+ // In-band AL-FEC for one MMTP asset over a MoQT subscription, for a player
617
+ // that owns its own parser (the Shaka MSF MMTP packaging).
618
+ export {
619
+ MmtpFecSession,
620
+ type MmtpFecSessionCatalogFec,
621
+ type MmtpFecSessionConfig,
622
+ type MmtpFecSessionDecoder,
623
+ type MmtpFecSessionDecoderConstructor,
624
+ type MmtpFecSessionMixedPathStats,
625
+ type MmtpFecSessionPath,
626
+ type MmtpFecSessionPathCounts,
627
+ type MmtpFecSessionStats,
628
+ } from "./mmtp-fec-session.js";
629
+
619
630
  export {
620
631
  catalogAmtNetworkSource,
632
+ catalogAtsc3NetworkSource,
621
633
  DEFAULT_AMT_RELAY_PORT,
622
634
  type AmtNetworkSource,
635
+ type Atsc3NetworkSource,
623
636
  type CatalogNetworkSource,
624
637
  } from "./catalog-network-source.js";
625
638
 
@@ -634,7 +647,11 @@ export {
634
647
  type RepairFetchPriority,
635
648
  type RepairFetchRequest,
636
649
  type RepairFetchTransport,
650
+ type RepairRangeFetchRequest,
651
+ type RepairRangeFetchTransport,
652
+ type RepairDeadlineBound,
637
653
  type RepairPriorityReason,
654
+ type RepairSendMeasurement,
638
655
  type RepairSkipReason,
639
656
  type RepairSubscribeCallback,
640
657
  type WireRepairFetchOptions,
@@ -51,9 +51,15 @@ export type IsoRepairPlacement<T> =
51
51
  readonly placed: readonly IsoPlacedSource<T>[]
52
52
  /** The repair-less span this repair closed in front of `block`, if any. */
53
53
  readonly span?: { readonly block: IsoBlockRange; readonly placed: readonly IsoPlacedSource<T>[] }
54
+ /**
55
+ * With `deferIncompleteGaps`: the SS_IDs in front of `block` whose every
56
+ * repair was lost and some source too, kept as a pending hole instead of
57
+ * a span (see `closeHolesWithin`). `lostSsId` is the highest missing one.
58
+ */
59
+ readonly hole?: { readonly block: IsoBlockRange; readonly lostSsId: number }
54
60
  }
55
61
  /** Not a block this stream can use; the repair symbol is dropped. */
56
- | { readonly status: 'conflict' | 'overlap' | 'stale'; readonly reason: string }
62
+ | { readonly status: 'conflict' | 'overlap' | 'stale' | 'ahead'; readonly reason: string }
57
63
 
58
64
  /**
59
65
  * Retention: the locator is never without one. Every call that can grow it
@@ -79,6 +85,15 @@ export class IsoBlockLocator<T> {
79
85
  #retentionBlocks: number
80
86
  #heldEvicted = 0
81
87
  #newest: number | undefined
88
+ /** Pending holes (see `deferIncompleteGaps`): unstated, sources still held. */
89
+ readonly #holes: IsoBlockRange[] = []
90
+ /**
91
+ * Keep a gap that is missing a source as a pending hole rather than a span,
92
+ * so a repair FETCHed for it (draft-ramadan-moq-fec §7.5) can still state
93
+ * its blocks. Set only while a range repair scheduler is wired; its owner
94
+ * must close every hole it is told about (`closeHolesWithin`).
95
+ */
96
+ deferIncompleteGaps = false
82
97
 
83
98
  /**
84
99
  * @param kMax the catalog maximum K (`sourceSymbols`), the bound on SSB_length.
@@ -168,6 +183,16 @@ export class IsoBlockLocator<T> {
168
183
  return null
169
184
  }
170
185
 
186
+ /** Number of pending holes. */
187
+ get holeCount(): number {
188
+ return this.#holes.length
189
+ }
190
+
191
+ /** The stated block (never a span) ending exactly at `end`. */
192
+ blockEndingAt(end: number): IsoBlockRange | undefined {
193
+ return this.#blocks.find((block) => !block.span && isoBlockEnd(block.start, block.length) === end)
194
+ }
195
+
171
196
  /** The known block or span starting exactly at `start`. */
172
197
  blockAt(start: number): IsoBlockRange | undefined {
173
198
  return this.#blocks.find((block) => block.start === start)
@@ -192,6 +217,10 @@ export class IsoBlockLocator<T> {
192
217
  coversBetween(a: number, b: number): boolean {
193
218
  if (this.#coverageEnd === undefined) return false
194
219
  if (!serial32Precedes(a, b) || serial32Distance(a, b) <= 1) return true
220
+ // A pending hole may still be stated by a FETCHed repair.
221
+ const first = (a + 1) >>> 0
222
+ const count = serial32Distance(a, b) - 1
223
+ if (this.#holes.some((hole) => isoRangesOverlap(first, count, hole.start, hole.length))) return false
195
224
  // The last SS_ID strictly before b must precede the coverage end.
196
225
  return serial32Precedes((b - 1) >>> 0, this.#coverageEnd)
197
226
  }
@@ -225,6 +254,15 @@ export class IsoBlockLocator<T> {
225
254
  * known block (a different length, or an overlap with another block or a
226
255
  * span) fails closed: the repair symbol is dropped and no block changes.
227
256
  * A newly stated block retires behind its SS_Start (see the class note).
257
+ * A block so far past `newest` that stating it would retire the position
258
+ * `newest` is refused as `ahead`. When `newest` is a placed block's SS_Start
259
+ * this is slightly conservative (retirement measures that block from its
260
+ * last SS_ID), dropping up to one block-length of repairs past the horizon
261
+ * until a source moves `newest`. Placing it would turn everything
262
+ * between into a repair-less span and leave every real repair behind it
263
+ * `stale`. Sources move `newest` (`hold`, `observe`), so after a real outage
264
+ * the first source back brings the next repair inside the horizon; until one
265
+ * arrives, a stream's repairs alone cannot pass it.
228
266
  */
229
267
  placeRepair(start: number, length: number): IsoRepairPlacement<T> {
230
268
  if (!Number.isSafeInteger(length) || length <= 0 || length > this.#kMax) {
@@ -243,6 +281,23 @@ export class IsoBlockLocator<T> {
243
281
  }
244
282
  return { status: 'placed', block: known, placed: [] }
245
283
  }
284
+ const pending = this.#holes.find((h) => isoBlockContains(h.start, h.length, start))
285
+ if (pending !== undefined) {
286
+ if (!isoBlockContains(pending.start, pending.length, serial32Add(start, length - 1))) {
287
+ return { status: 'overlap', reason: `repair block ${start}+${length} runs past pending hole ${pending.start}+${pending.length}` }
288
+ }
289
+ // A FETCHed repair stated a block inside a pending hole: split the hole round it.
290
+ this.#holes.splice(this.#holes.indexOf(pending), 1)
291
+ const before = serial32Distance(pending.start, start)
292
+ const afterStart = isoBlockEnd(start, length)
293
+ const after = pending.length - before - length
294
+ if (before > 0) this.#holes.push({ start: pending.start, length: before, span: false })
295
+ if (after > 0) this.#holes.push({ start: afterStart, length: after, span: false })
296
+ const block: IsoBlockRange = { start, length, span: false }
297
+ const placed = this.#claim(block)
298
+ this.#blocks.push(block)
299
+ return { status: 'placed', block, placed }
300
+ }
246
301
  for (const block of this.#blocks) {
247
302
  if (
248
303
  isoBlockContains(block.start, block.length, start) ||
@@ -254,7 +309,11 @@ export class IsoBlockLocator<T> {
254
309
  }
255
310
  }
256
311
  }
312
+ if (this.#newest !== undefined && isoBlocksBehindLowerBound(this.#newest, start, this.#kMax) > this.#retentionBlocks) {
313
+ return { status: 'ahead', reason: `repair block ${start}+${length} is past the retention horizon ahead of ${this.#newest}` }
314
+ }
257
315
  let span: { block: IsoBlockRange; placed: IsoPlacedSource<T>[] } | undefined
316
+ let hole: { block: IsoBlockRange; lostSsId: number } | undefined
258
317
  if (this.#coverageEnd === undefined) {
259
318
  // Reception starts at the first stated block. Held sources before it
260
319
  // are the tail of blocks that began before the join: their range is
@@ -266,24 +325,57 @@ export class IsoBlockLocator<T> {
266
325
  } else if (serial32Precedes(start, this.#coverageEnd)) {
267
326
  return { status: 'stale', reason: `repair block ${start}+${length} precedes known coverage` }
268
327
  } else if (start !== this.#coverageEnd) {
269
- const spanBlock: IsoBlockRange = {
270
- start: this.#coverageEnd,
271
- length: serial32Distance(this.#coverageEnd, start),
272
- span: true,
328
+ const gap = { start: this.#coverageEnd, length: serial32Distance(this.#coverageEnd, start) }
329
+ const lostSsId = this.deferIncompleteGaps ? this.#highestMissing(gap.start, gap.length) : undefined
330
+ if (lostSsId !== undefined) {
331
+ const holeBlock: IsoBlockRange = { ...gap, span: false }
332
+ this.#holes.push(holeBlock)
333
+ hole = { block: holeBlock, lostSsId }
334
+ } else {
335
+ const spanBlock: IsoBlockRange = { ...gap, span: true }
336
+ span = { block: spanBlock, placed: this.#claim(spanBlock) }
337
+ this.#blocks.push(spanBlock)
273
338
  }
274
- span = { block: spanBlock, placed: this.#claim(spanBlock) }
275
- this.#blocks.push(spanBlock)
276
339
  }
277
340
  const block: IsoBlockRange = { start, length, span: false }
278
341
  const placed = this.#claim(block)
279
342
  this.#blocks.push(block)
280
343
  this.#coverageEnd = isoBlockEnd(start, length)
281
344
  this.retireBehind(start)
345
+ if (hole !== undefined) return { status: 'placed', block, placed, hole }
282
346
  return span === undefined
283
347
  ? { status: 'placed', block, placed }
284
348
  : { status: 'placed', block, placed, span }
285
349
  }
286
350
 
351
+ /**
352
+ * Turn every pending hole inside `[start, start + length)` into a
353
+ * repair-less span, claiming its held sources: its range FETCH ended, so
354
+ * nothing more will state its blocks. Returns the spans, in SS_ID order.
355
+ */
356
+ closeHolesWithin(start: number, length: number): { block: IsoBlockRange; placed: IsoPlacedSource<T>[] }[] {
357
+ const closed: { block: IsoBlockRange; placed: IsoPlacedSource<T>[] }[] = []
358
+ for (const hole of [...this.#holes]) {
359
+ if (!isoBlockContains(start, length, hole.start)) continue
360
+ this.#holes.splice(this.#holes.indexOf(hole), 1)
361
+ const spanBlock: IsoBlockRange = { start: hole.start, length: hole.length, span: true }
362
+ closed.push({ block: spanBlock, placed: this.#claim(spanBlock) })
363
+ this.#blocks.push(spanBlock)
364
+ }
365
+ return closed.sort((x, y) => serial32Distance(start, x.block.start) - serial32Distance(start, y.block.start))
366
+ }
367
+
368
+ /** The highest SS_ID in the range no source is held for, or undefined when all are. */
369
+ #highestMissing(start: number, length: number): number | undefined {
370
+ // Every position past the held count cannot all be held: bounded scan.
371
+ const steps = Math.min(length, this.#held.size + 1)
372
+ for (let i = 1; i <= steps; i++) {
373
+ const ssId = serial32Add(start, length - i)
374
+ if (!this.#held.has(ssId)) return ssId
375
+ }
376
+ return undefined
377
+ }
378
+
287
379
  /**
288
380
  * Retire every block and span whose last SS_ID, and every held source,
289
381
  * lies more than `retentionBlocks` blocks behind `position`, with the shared
@@ -302,6 +394,11 @@ export class IsoBlockLocator<T> {
302
394
  this.#blocks.splice(i, 1)
303
395
  }
304
396
  }
397
+ for (let i = this.#holes.length - 1; i >= 0; i--) {
398
+ const hole = this.#holes[i]!
399
+ const last = serial32Add(hole.start, hole.length - 1)
400
+ if (isoBlocksBehindLowerBound(last, position, this.#kMax) > horizon) this.#holes.splice(i, 1)
401
+ }
305
402
  for (const ssId of [...this.#held.keys()]) {
306
403
  if (isoBlocksBehindLowerBound(ssId, position, this.#kMax) > horizon) this.#held.delete(ssId)
307
404
  }
@@ -311,6 +408,7 @@ export class IsoBlockLocator<T> {
311
408
  /** Forget everything (publisher restart, track reset). */
312
409
  reset(): void {
313
410
  this.#blocks.length = 0
411
+ this.#holes.length = 0
314
412
  this.#held.clear()
315
413
  this.#coverageEnd = undefined
316
414
  this.#newest = undefined
@@ -327,3 +425,8 @@ export class IsoBlockLocator<T> {
327
425
  return placed
328
426
  }
329
427
  }
428
+
429
+ /** Whether `[aStart, aStart + aLength)` and `[bStart, bStart + bLength)` share an SS_ID. */
430
+ function isoRangesOverlap(aStart: number, aLength: number, bStart: number, bLength: number): boolean {
431
+ return serial32Distance(aStart, bStart) < aLength || serial32Distance(bStart, aStart) < bLength
432
+ }