@blockcast/fec-worker 0.1.0 → 0.3.0-main.1cd3cdea89da

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 (41) hide show
  1. package/README.md +28 -23
  2. package/dist/block-order.d.ts +22 -0
  3. package/dist/block-order.d.ts.map +1 -0
  4. package/dist/block-order.js +48 -0
  5. package/dist/block-order.js.map +1 -0
  6. package/dist/fec-worker-client.d.ts +23 -20
  7. package/dist/fec-worker-client.d.ts.map +1 -1
  8. package/dist/fec-worker-client.js +102 -54
  9. package/dist/fec-worker-client.js.map +1 -1
  10. package/dist/fec-worker-types.d.ts +162 -88
  11. package/dist/fec-worker-types.d.ts.map +1 -1
  12. package/dist/fec-worker-types.js +2 -2
  13. package/dist/fec-worker.js +328 -195
  14. package/dist/fec-worker.js.map +4 -4
  15. package/dist/index.d.ts +1 -1
  16. package/dist/index.d.ts.map +1 -1
  17. package/dist/index.js.map +1 -1
  18. package/dist/shred-fec-worker.js +23 -2
  19. package/dist/shred-fec-worker.js.map +2 -2
  20. package/dist/shred-worker-types.d.ts +16 -1
  21. package/dist/shred-worker-types.d.ts.map +1 -1
  22. package/dist/shred-worker-types.js +16 -1
  23. package/dist/shred-worker-types.js.map +1 -1
  24. package/package.json +10 -8
  25. package/src/block-order.ts +59 -0
  26. package/src/fec-worker-client.ts +107 -56
  27. package/src/fec-worker-types.ts +161 -84
  28. package/src/fec-worker.ts +517 -468
  29. package/src/index.ts +3 -0
  30. package/src/shred-fec-worker.ts +34 -3
  31. package/src/shred-worker-types.ts +16 -1
  32. package/src/__tests__/fec-worker-alta.test.ts +0 -231
  33. package/src/__tests__/fec-worker-cleanup-integration.test.ts +0 -250
  34. package/src/__tests__/fec-worker-pending-queue.test.ts +0 -315
  35. package/src/__tests__/fec-worker-repair-size.test.ts +0 -1029
  36. package/src/__tests__/fec-worker-wasm-contract.test.ts +0 -84
  37. package/src/__tests__/shred-fec-worker.test.ts +0 -448
  38. package/src/fec-worker-client.test.ts +0 -530
  39. package/src/fec-worker-types.test.ts +0 -400
  40. package/src/shred-worker-client.test.ts +0 -182
  41. package/src/transfer.test.ts +0 -24
@@ -2,14 +2,27 @@
2
2
  * @blockcast/fec-worker — Shared Type Definitions
3
3
  *
4
4
  * Defines the Worker message protocol (commands/events), FecTrackStats shape
5
- * consumed by fec-panel (Phase 5) and CmcdProducer (Phase 6), and auth
6
- * interface stubs (ALTA + relay block sig).
5
+ * consumed by fec-panel (Phase 5) and CmcdProducer (Phase 6), and the relay
6
+ * block signature stub.
7
7
  *
8
8
  * Types here are importable from both main thread and Worker contexts.
9
9
  */
10
10
 
11
11
  // --- Config ---
12
12
 
13
+ /**
14
+ * FEC block profile (BLO-35597, ISO/IEC 23008-1 Annex C): the catalog
15
+ * `fec.blockProfile` token. Required and never inferred; the only profile is
16
+ * `iso-ssbg1-v1` (C.3.3 ssbg_mode1): SS_ID increments by one per source
17
+ * packet mod 2^32; a block is `[SS_Start, SS_Start + SSB_length)` keyed by
18
+ * SS_Start; `k` is only the maximum SSB_length; repair ESI =
19
+ * SSB_length + RS_ID. The retired `k-padded-v1` token is refused like any
20
+ * unknown one.
21
+ *
22
+ * Mirrors @blockcast/mmt-base `FecBlockProfile`.
23
+ */
24
+ export type FecBlockProfile = "iso-ssbg1-v1";
25
+
13
26
  /** Per-track FEC configuration. Matches design spec section 3.2 FecTrackConfig. */
14
27
  export interface FecTrackConfig {
15
28
  /** Codec string, e.g. 'avc1.64001f', 'mp4a.40.2' */
@@ -27,25 +40,39 @@ export interface FecTrackConfig {
27
40
 
28
41
  /** FEC algorithm identifier: 'raptor', 'reed-solomon', 'none' */
29
42
  algorithm: string;
30
- /** Source symbols per block (K) */
43
+ /** Block profile; the Worker refuses to configure without a known token. */
44
+ blockProfile: FecBlockProfile;
45
+ /** Source symbols per block (K). Under `iso-ssbg1-v1` the maximum SSB_length (kMax). */
31
46
  k: number;
32
47
  /** Repair symbols per block (P). NOT symbol size. */
33
48
  repairCount: number;
34
49
  /** Bytes per symbol */
35
50
  symbolSize: number;
36
- /** Interleave depth in frames (D) */
51
+ /** Retained decoder horizon as a BLOCK COUNT, not D. `isRetiredSbn`
52
+ * retires blocks more than this many blocks behind the live SS_ID, by
53
+ * `isoBlocksBehind(SS_ID, live SS_ID, k)` (block-order.ts), the lower
54
+ * bound every ISO receiver layer shares, and `MmtFecDecoder::new`
55
+ * documents it as "the number of interleaved blocks".
56
+ * Never assign `deriveFecInterleaveDepth()` here — that returns media units
57
+ * per block, which is a different quantity (BLO-35597). */
37
58
  interleaveDepth: number;
38
- /** Interleave depth in milliseconds (D * frameDurationMs) */
59
+ /**
60
+ * Catalog interleave span in integer milliseconds. A positive value is the complete
61
+ * source-plus-repair span; zero represents an absent/zero catalog span and
62
+ * carries no elapsed-time deadline.
63
+ */
39
64
  interleaveMs: number;
40
- /** Catalog-derived source+repair delivery window used for decoder cleanup. */
65
+ /**
66
+ * Positive cleanup budget. For a positive `interleaveMs` this is the
67
+ * catalog span plus its explicit reorder allowance. For zero it is a
68
+ * receiver-local bound only, never an inferred catalog deadline.
69
+ */
41
70
  deliveryWindowMs: number;
42
71
  /** FEC encoding mode */
43
72
  fecMode: "subframe" | "object";
44
73
  /** MoQ repair track name (optional) */
45
74
  repairTrack?: string;
46
75
 
47
- /** Whether ALTA publisher auth is enabled (AUTH-01) */
48
- altaEnabled: boolean;
49
76
  /** Whether relay block signature verification is enabled (AUTH-01) */
50
77
  relayBlockSigEnabled: boolean;
51
78
  }
@@ -54,16 +81,21 @@ export interface FecTrackConfig {
54
81
 
55
82
  /** Per-block snapshot for live block visualization and stats. */
56
83
  export interface FecBlockSnapshot {
57
- /** Source block number */
84
+ /** Block key: the block's SS_Start. */
58
85
  sbn: number;
59
86
  /** Current block state */
60
- state: "collecting" | "complete" | "recovered" | "failed" | "green-fill";
87
+ state: "collecting" | "complete" | "recovered" | "failed";
61
88
  /** Number of source symbols received */
62
89
  sourceReceived: number;
63
90
  /** Number of repair symbols received */
64
91
  repairReceived: number;
65
- /** Expected source symbols (K) */
92
+ /** Configured source-block geometry (K); under `iso-ssbg1-v1` the block's SSB_length. */
66
93
  k: number;
94
+ /** Authored source symbols (SSB_length) once a repair symbol declared it.
95
+ * Under `iso-ssbg1-v1` a block is only known from a repair symbol, so it is
96
+ * always set — except on a cleanup tombstone for a block the Worker never
97
+ * saw, whose length nothing declared. */
98
+ ssbLength?: number;
67
99
  /** Expected repair symbols (P) */
68
100
  repairCount: number;
69
101
  /** Timestamp of first symbol arrival (ms) */
@@ -72,10 +104,11 @@ export interface FecBlockSnapshot {
72
104
  lastSymbolTs: number;
73
105
  /** Timestamp when block completed (ms), undefined if not yet complete */
74
106
  completedTs?: number;
75
- /** Deadline timestamp for block completion (ms) */
107
+ /**
108
+ * Cleanup timestamp (ms). It is the catalog completion deadline when
109
+ * `config.interleaveMs > 0`, and a local retention timestamp otherwise.
110
+ */
76
111
  deadlineTs: number;
77
- /** Count of source symbols with valid ALTA signature (AUTH-02) */
78
- altaVerified: number;
79
112
  /** Relay block signature validity: true/false after verification, null if not yet verified (AUTH-02) */
80
113
  relayBlockSigValid: boolean | null;
81
114
  }
@@ -97,53 +130,19 @@ export interface FecTrackStats {
97
130
  blocksRecovered: number;
98
131
  /** Blocks that failed (unrecoverable) */
99
132
  blocksFailed: number;
100
- /** Frames replaced with green-fill */
101
- greenFillFrames: number;
102
133
  /** Recovery success rate 0-100 */
103
134
  recoveryRate: number;
104
135
 
105
- /** Packets with valid publisher ALTA signature */
106
- altaVerified: number;
107
- /** Packets with MAC / signature / hash-chain mismatch (tamper-confirmed) */
108
- altaFailed: number;
109
- /** Packets that couldn't be verified due to missing trailer / not-ready verifier /
110
- * late-joiner bootstrap gap. Neither tamper nor error — just unverifiable. */
111
- altaPending: number;
112
- /** Deferred-queue entries evicted by cap (queue full). Separate counter so
113
- * cap misconfiguration is distinguishable from natural bootstrap-gap noise. */
114
- altaPendingEvictedCap: number;
115
- /** Deferred-queue entries evicted by TTL (retry window expired). */
116
- altaPendingEvictedTtl: number;
117
- /** verifyDetached / parse threw an exception (WASM trap, OOM, unmarshal panic).
118
- * Not counted as altaFailed because the cause isn't crypto — helps ops
119
- * distinguish infrastructure flake from tamper. */
120
- altaError: number;
121
- /** True if ALTA was enabled at configure time but the verifier WASM init
122
- * failed. Ops alerting should treat this as "ALTA disabled due to error",
123
- * distinct from "ALTA disabled by config" (altaEnabled flag not present). */
124
- altaInitFailed: boolean;
125
- /** Diagnostic: last ≤16 altaFailed events with full context (seq, error
126
- * string from the verifier, auth+payload lengths, and which code path
127
- * raised the failure). Bounded so it can't blow memory. Intended for
128
- * investigation — not a stable API; consumers should treat it as
129
- * opt-in and defensive (may be undefined). */
130
- altaFailedDetails?: Array<{
131
- seq: number;
132
- error: string;
133
- authLen: number;
134
- payloadLen: number;
135
- path: string;
136
- }>;
137
136
  /** Blocks with valid relay signature */
138
137
  relayBlocksVerified: number;
139
138
  /** Blocks with invalid relay signature */
140
139
  relayBlocksFailed: number;
141
140
 
142
- /** Current live source block number */
141
+ /** Live media position: the newest accepted SS_ID / SS_Start. */
143
142
  liveSbn: number;
144
143
  /** Active block snapshots */
145
144
  blocks: FecBlockSnapshot[];
146
- /** Number of blocks in the interleave window */
145
+ /** Number of blocks in the FEC block span */
147
146
  interleaveWindowBlocks: number;
148
147
 
149
148
  /** Average recovery latency (ms) */
@@ -152,6 +151,52 @@ export interface FecTrackStats {
152
151
  maxRecoveryMs: number;
153
152
  /** Repair symbols rejected before WASM because their byte length did not match T. */
154
153
  repairSymbolSizeMismatch?: number;
154
+ /**
155
+ * Source symbols whose SBN had already fallen below the retained window.
156
+ *
157
+ * This and its two peers below are dropped before WASM and narrow the
158
+ * explanations for a block that settles at K-1 source symbols (BLO-35597
159
+ * defect 2): a late symbol past retention, a symbol for a block that had
160
+ * already settled (complete, recovered *or failed*), or a repeated
161
+ * (SBN, ESI).
162
+ *
163
+ * They narrow; they do not uniquely attribute. A symbol lost in transit
164
+ * never reaches the worker and increments nothing, so all three reading
165
+ * zero on a short block means only that no *receiver-side* drop was
166
+ * recorded — wire loss and a short authored block both remain open. Use
167
+ * `FecBlockSnapshot.ssbLength` to separate them: below `k` is a genuinely
168
+ * short authored block, equal to `k` with fewer accepted is loss-or-late.
169
+ * `ssbLength` is undefined until a repair symbol declares it, in which case
170
+ * both stay open.
171
+ *
172
+ * All three are cumulative across decoder epochs: `configure` clears block
173
+ * tracking but none of them, so they stay comparable against the
174
+ * `sourceSymbols` denominator. Only a reset zeroes them.
175
+ */
176
+ sourceDroppedRetired?: number;
177
+ /** Source symbols for a block that had already completed, recovered or failed. */
178
+ sourceDroppedTerminal?: number;
179
+ /**
180
+ * Source symbols repeating an (SBN, ESI) already accepted in the current
181
+ * epoch; `configure` clears the accepted-ESI sets.
182
+ *
183
+ * Unlike the other two, this one cannot fire once a block settles — the
184
+ * terminal check returns first, and marking a block terminal retires its
185
+ * accepted-ESI set anyway — so a non-zero value means the repeat reached
186
+ * the worker while the block was **still collecting**. It attributes the
187
+ * timing, not the origin: usually a duplicate packet on the wire,
188
+ * sometimes an SBN/ESI derivation collision.
189
+ */
190
+ sourceDroppedDuplicate?: number;
191
+ /**
192
+ * Accepted source symbols whose block never became known (no repair
193
+ * symbol named a range containing them) before their SS_ID fell behind
194
+ * the retained horizon. They were already fed to the decoder and routed
195
+ * directly; this counts the bookkeeping retired with them, i.e. evidence
196
+ * that a block's repair symbols never arrived. The decoder retires its
197
+ * own copies by the same horizon inside `cleanup_by_ss_start`.
198
+ */
199
+ sourceUnplacedRetired: number;
155
200
  /** Native decoder counters, converted to numbers so browser diagnostics remain JSON-safe. */
156
201
  decoder?: {
157
202
  sourcePackets: number;
@@ -168,7 +213,15 @@ export interface FecTrackStats {
168
213
  export type FecWorkerErrorCode =
169
214
  | "recovered-symbol-size-unconfigured"
170
215
  | "recovered-block-too-short"
216
+ | "recovered-symbol-ssb-length-invalid"
171
217
  | "repair-symbol-size-mismatch"
218
+ | "repair-ssb-length-conflict"
219
+ /** `iso-ssbg1-v1`: a repair's `[SS_Start, SS_Start + SSB_length)` overlaps a different known block. */
220
+ | "repair-block-overlap"
221
+ /** `iso-ssbg1-v1`: a recovered symbol's ssbg_mode1 length field exceeds T-2 or its padding is non-zero. */
222
+ | "recovered-ssbg1-symbol-invalid"
223
+ /** `iso-ssbg1-v1`: the decoder returned a recovered block for a source the Worker could not place in a known block. */
224
+ | "recovered-block-unplaced"
172
225
  | "reset-failed"
173
226
  | "unknown-command"
174
227
  | "command-failed"
@@ -179,14 +232,21 @@ export type FecWorkerErrorCode =
179
232
 
180
233
  /** Per-frame metadata emitted with frame events from Worker. */
181
234
  export interface FrameMeta {
182
- /** ALTA verification result: true/false after check, null when ALTA not configured (AUTH-04) */
183
- altaVerified: boolean | null;
184
235
  /** Whether this frame was recovered via FEC (not received directly) */
185
236
  recovered: boolean;
186
- /** Source block number this frame belongs to */
237
+ /** Block key this frame belongs to: the block's SS_Start */
187
238
  sbn: number;
188
- /** Encoding Symbol ID within the recovered source block (0..K-1). */
239
+ /** Encoding Symbol ID within the recovered source block (0..ssbLength-1).
240
+ * Under `iso-ssbg1-v1` a recovered frame is the full T-byte ssbg_mode1
241
+ * symbol (length field, packet, zero padding); the Worker validates but
242
+ * never trims it. */
189
243
  esi?: number;
244
+ /**
245
+ * Authored source-symbol count of the recovered block (the repair
246
+ * symbols' SSB_length, 1..K). Recovered frames are ESI 0..ssbLength-1;
247
+ * ESIs ssbLength..K-1 are encoder zero padding and are never emitted.
248
+ */
249
+ ssbLength?: number;
190
250
  }
191
251
 
192
252
  // --- Worker commands (main -> worker) ---
@@ -197,50 +257,44 @@ export type FecWorkerCommand =
197
257
  type: "configure";
198
258
  config: FecTrackConfig;
199
259
  wasmModule: WebAssembly.Module;
200
- /**
201
- * ALTA public key as PKCS#8 SubjectPublicKeyInfo DER bytes (self-describing).
202
- * Algorithm (Ed25519 or ECDSA P-256) is auto-detected from the SPKI OID.
203
- * Ed25519 SPKI is 44 bytes; P-256 uncompressed is 91 bytes.
204
- */
205
- altaPublicKey?: ArrayBuffer;
206
- /** Pre-compiled ALTA WASM module. Structured-cloneable (NOT transferable) — do not add to transfer list. */
207
- altaWasmModule?: WebAssembly.Module;
208
260
  }
209
261
  | {
210
262
  type: "feedSource";
211
263
  /** Flat 32-bit Source Symbol ID from the MMTP Source FEC Payload ID
212
- * (ISO 23008-1 §C.5.2). The WASM decoder derives SBN = floor(ssId/K)
213
- * and ESI = ssId % K internally — do NOT pre-compute. */
264
+ * (ISO 23008-1 §C.5.2). SS_IDs are consecutive per source packet mod
265
+ * 2^32 and the block comes from a repair's range — do NOT
266
+ * pre-compute a block. */
214
267
  ssId: number;
268
+ /** The protected packet exactly, unpadded and without its SS_ID (at
269
+ * most T-2 bytes). */
215
270
  data: ArrayBuffer;
216
271
  ts: number;
217
- /** Optional ALTA authenticator bytes (detached mode — 08-07 MMTP
218
- * trailer). Present when the source packet carried a
219
- * `[0xF0A1][len][auth]` trailer and ALTA is enabled at the worker.
220
- * When omitted, the worker skips verification and the packet
221
- * counts as `altaPending`. */
222
- altaAuth?: ArrayBuffer;
223
- /** Byte offset within `data` where the signed MMTP payload ends
224
- * (== where the trailer began on the wire). Required when
225
- * `altaAuth` is present. Worker constructs
226
- * `payload = new Uint8Array(data, 0, altaPayloadEnd)` for
227
- * `verifyDetached(altaAuth, payload)`. */
228
- altaPayloadEnd?: number;
229
272
  }
230
273
  | {
231
274
  type: "feedRepair";
232
- /** SS_Start: SS_ID of the first source symbol in the block
233
- * (= SBN * SSB_length), from the MMTP Repair FEC Payload ID
234
- * (ISO 23008-1 §C.5.3). */
275
+ /** SS_Start: SS_ID of the first source symbol in the block, from the
276
+ * MMTP Repair FEC Payload ID (ISO 23008-1 §C.5.3): any uint32; the
277
+ * block key. */
235
278
  ssStart: number;
236
- /** SSB_length: K for this block (variable-K allowed per spec). */
279
+ /** Authored source-symbol count for this block; 1..configured K. */
237
280
  ssbLength: number;
238
- /** RS_ID: repair symbol index within the block; WASM maps to
239
- * repair ESI = SSB_length + RS_ID. */
281
+ /** RS_ID: repair symbol index within the block; WASM maps to repair
282
+ * ESI = SSB_length + RS_ID. */
240
283
  rsId: number;
241
284
  data: ArrayBuffer;
242
285
  ts: number;
243
286
  }
287
+ | {
288
+ /**
289
+ * Every source and repair symbol the page fed in one task, in feed
290
+ * order, sharing one transfer list. The client sends symbols this
291
+ * way instead of one message per symbol (BLO-36599); the Worker
292
+ * handles each record exactly like the single-symbol command of the
293
+ * same `type`, and a record that fails does not stop the others.
294
+ */
295
+ type: "feedSymbols";
296
+ symbols: FecSymbolCommand[];
297
+ }
244
298
  | {
245
299
  type: "relayBlockSig";
246
300
  sbn: number;
@@ -249,12 +303,26 @@ export type FecWorkerCommand =
249
303
  }
250
304
  | {
251
305
  type: "cleanup";
252
- /** Every timed-out block covered by this coalesced queue-drain barrier. */
306
+ /** Every timed-out block covered by this coalesced queue-drain barrier,
307
+ * by block key (SS_Start). */
253
308
  sbns: number[];
254
309
  }
255
310
  | { type: "reset" }
311
+ /**
312
+ * Ask the Worker to post a blockUpdate on every block transition. Without
313
+ * it the Worker posts none: each one is a structured clone and a task on
314
+ * the page thread, and the player does not consume them (BLO-36599).
315
+ * Sent only to a Worker that announced it in `capabilities`.
316
+ */
317
+ | { type: "subscribeBlockUpdates" }
256
318
  | { type: "dispose" };
257
319
 
320
+ /** One symbol record of a `feedSymbols` batch. */
321
+ export type FecSymbolCommand = Extract<
322
+ FecWorkerCommand,
323
+ { type: "feedSource" } | { type: "feedRepair" }
324
+ >;
325
+
258
326
  // --- Worker events (worker -> main) ---
259
327
 
260
328
  /** Discriminated union of all events emitted from Worker to main thread. */
@@ -262,7 +330,16 @@ export type FecWorkerEvent =
262
330
  | { type: "frame"; data: ArrayBuffer; meta: FrameMeta }
263
331
  | { type: "snapshot"; stats: FecTrackStats }
264
332
  | { type: "blockUpdate"; block: FecBlockSnapshot }
265
- | { type: "greenFill"; sbn: number; data: ArrayBuffer }
266
333
  | { type: "cleanupComplete"; sbns: number[] }
267
334
  | { type: "resetComplete" }
335
+ /**
336
+ * Optional commands this Worker accepts, posted once it is configured.
337
+ * The client uses `feedSymbols` only after the Worker names it, so a page
338
+ * that meets an older cached Worker bundle keeps sending one command per
339
+ * symbol instead of a batch that Worker would reject (BLO-36599).
340
+ */
341
+ | { type: "capabilities"; commands: FecWorkerOptionalCommand[] }
268
342
  | { type: "error"; message: string; code?: FecWorkerErrorCode };
343
+
344
+ /** Commands a client may send only after the Worker announced them. */
345
+ export type FecWorkerOptionalCommand = "feedSymbols" | "subscribeBlockUpdates";