@blockcast/fec-worker 0.1.0 → 0.3.0-main.2515e5ca4858

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 (40) hide show
  1. package/README.md +19 -9
  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 +19 -7
  7. package/dist/fec-worker-client.d.ts.map +1 -1
  8. package/dist/fec-worker-client.js +105 -30
  9. package/dist/fec-worker-client.js.map +1 -1
  10. package/dist/fec-worker-types.d.ts +146 -25
  11. package/dist/fec-worker-types.d.ts.map +1 -1
  12. package/dist/fec-worker.js +326 -127
  13. package/dist/fec-worker.js.map +4 -4
  14. package/dist/index.d.ts +1 -1
  15. package/dist/index.d.ts.map +1 -1
  16. package/dist/index.js.map +1 -1
  17. package/dist/shred-fec-worker.js +23 -2
  18. package/dist/shred-fec-worker.js.map +2 -2
  19. package/dist/shred-worker-types.d.ts +16 -1
  20. package/dist/shred-worker-types.d.ts.map +1 -1
  21. package/dist/shred-worker-types.js +16 -1
  22. package/dist/shred-worker-types.js.map +1 -1
  23. package/package.json +8 -3
  24. package/src/block-order.ts +59 -0
  25. package/src/fec-worker-client.ts +114 -32
  26. package/src/fec-worker-types.ts +145 -21
  27. package/src/fec-worker.ts +518 -180
  28. package/src/index.ts +3 -0
  29. package/src/shred-fec-worker.ts +34 -3
  30. package/src/shred-worker-types.ts +16 -1
  31. package/src/__tests__/fec-worker-alta.test.ts +0 -231
  32. package/src/__tests__/fec-worker-cleanup-integration.test.ts +0 -250
  33. package/src/__tests__/fec-worker-pending-queue.test.ts +0 -315
  34. package/src/__tests__/fec-worker-repair-size.test.ts +0 -1029
  35. package/src/__tests__/fec-worker-wasm-contract.test.ts +0 -84
  36. package/src/__tests__/shred-fec-worker.test.ts +0 -448
  37. package/src/fec-worker-client.test.ts +0 -530
  38. package/src/fec-worker-types.test.ts +0 -400
  39. package/src/shred-worker-client.test.ts +0 -182
  40. package/src/transfer.test.ts +0 -24
@@ -10,6 +10,19 @@
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,13 +40,21 @@ 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
59
  /** Interleave depth in milliseconds (D * frameDurationMs) */
39
60
  interleaveMs: number;
@@ -54,16 +75,21 @@ export interface FecTrackConfig {
54
75
 
55
76
  /** Per-block snapshot for live block visualization and stats. */
56
77
  export interface FecBlockSnapshot {
57
- /** Source block number */
78
+ /** Block key: the block's SS_Start. */
58
79
  sbn: number;
59
80
  /** Current block state */
60
- state: "collecting" | "complete" | "recovered" | "failed" | "green-fill";
81
+ state: "collecting" | "complete" | "recovered" | "failed";
61
82
  /** Number of source symbols received */
62
83
  sourceReceived: number;
63
84
  /** Number of repair symbols received */
64
85
  repairReceived: number;
65
- /** Expected source symbols (K) */
86
+ /** Configured source-block geometry (K); under `iso-ssbg1-v1` the block's SSB_length. */
66
87
  k: number;
88
+ /** Authored source symbols (SSB_length) once a repair symbol declared it.
89
+ * Under `iso-ssbg1-v1` a block is only known from a repair symbol, so it is
90
+ * always set — except on a cleanup tombstone for a block the Worker never
91
+ * saw, whose length nothing declared. */
92
+ ssbLength?: number;
67
93
  /** Expected repair symbols (P) */
68
94
  repairCount: number;
69
95
  /** Timestamp of first symbol arrival (ms) */
@@ -97,8 +123,6 @@ export interface FecTrackStats {
97
123
  blocksRecovered: number;
98
124
  /** Blocks that failed (unrecoverable) */
99
125
  blocksFailed: number;
100
- /** Frames replaced with green-fill */
101
- greenFillFrames: number;
102
126
  /** Recovery success rate 0-100 */
103
127
  recoveryRate: number;
104
128
 
@@ -139,11 +163,11 @@ export interface FecTrackStats {
139
163
  /** Blocks with invalid relay signature */
140
164
  relayBlocksFailed: number;
141
165
 
142
- /** Current live source block number */
166
+ /** Live media position: the newest accepted SS_ID / SS_Start. */
143
167
  liveSbn: number;
144
168
  /** Active block snapshots */
145
169
  blocks: FecBlockSnapshot[];
146
- /** Number of blocks in the interleave window */
170
+ /** Number of blocks in the FEC block span */
147
171
  interleaveWindowBlocks: number;
148
172
 
149
173
  /** Average recovery latency (ms) */
@@ -152,6 +176,52 @@ export interface FecTrackStats {
152
176
  maxRecoveryMs: number;
153
177
  /** Repair symbols rejected before WASM because their byte length did not match T. */
154
178
  repairSymbolSizeMismatch?: number;
179
+ /**
180
+ * Source symbols whose SBN had already fallen below the retained window.
181
+ *
182
+ * This and its two peers below are dropped before WASM and narrow the
183
+ * explanations for a block that settles at K-1 source symbols (BLO-35597
184
+ * defect 2): a late symbol past retention, a symbol for a block that had
185
+ * already settled (complete, recovered *or failed*), or a repeated
186
+ * (SBN, ESI).
187
+ *
188
+ * They narrow; they do not uniquely attribute. A symbol lost in transit
189
+ * never reaches the worker and increments nothing, so all three reading
190
+ * zero on a short block means only that no *receiver-side* drop was
191
+ * recorded — wire loss and a short authored block both remain open. Use
192
+ * `FecBlockSnapshot.ssbLength` to separate them: below `k` is a genuinely
193
+ * short authored block, equal to `k` with fewer accepted is loss-or-late.
194
+ * `ssbLength` is undefined until a repair symbol declares it, in which case
195
+ * both stay open.
196
+ *
197
+ * All three are cumulative across decoder epochs: `configure` clears block
198
+ * tracking but none of them, so they stay comparable against the
199
+ * `sourceSymbols` denominator. Only a reset zeroes them.
200
+ */
201
+ sourceDroppedRetired?: number;
202
+ /** Source symbols for a block that had already completed, recovered or failed. */
203
+ sourceDroppedTerminal?: number;
204
+ /**
205
+ * Source symbols repeating an (SBN, ESI) already accepted in the current
206
+ * epoch; `configure` clears the accepted-ESI sets.
207
+ *
208
+ * Unlike the other two, this one cannot fire once a block settles — the
209
+ * terminal check returns first, and marking a block terminal retires its
210
+ * accepted-ESI set anyway — so a non-zero value means the repeat reached
211
+ * the worker while the block was **still collecting**. It attributes the
212
+ * timing, not the origin: usually a duplicate packet on the wire,
213
+ * sometimes an SBN/ESI derivation collision.
214
+ */
215
+ sourceDroppedDuplicate?: number;
216
+ /**
217
+ * Accepted source symbols whose block never became known (no repair
218
+ * symbol named a range containing them) before their SS_ID fell behind
219
+ * the retained horizon. They were already fed to the decoder and routed
220
+ * directly; this counts the bookkeeping retired with them, i.e. evidence
221
+ * that a block's repair symbols never arrived. The decoder retires its
222
+ * own copies by the same horizon inside `cleanup_by_ss_start`.
223
+ */
224
+ sourceUnplacedRetired: number;
155
225
  /** Native decoder counters, converted to numbers so browser diagnostics remain JSON-safe. */
156
226
  decoder?: {
157
227
  sourcePackets: number;
@@ -168,7 +238,15 @@ export interface FecTrackStats {
168
238
  export type FecWorkerErrorCode =
169
239
  | "recovered-symbol-size-unconfigured"
170
240
  | "recovered-block-too-short"
241
+ | "recovered-symbol-ssb-length-invalid"
171
242
  | "repair-symbol-size-mismatch"
243
+ | "repair-ssb-length-conflict"
244
+ /** `iso-ssbg1-v1`: a repair's `[SS_Start, SS_Start + SSB_length)` overlaps a different known block. */
245
+ | "repair-block-overlap"
246
+ /** `iso-ssbg1-v1`: a recovered symbol's ssbg_mode1 length field exceeds T-2 or its padding is non-zero. */
247
+ | "recovered-ssbg1-symbol-invalid"
248
+ /** `iso-ssbg1-v1`: the decoder returned a recovered block for a source the Worker could not place in a known block. */
249
+ | "recovered-block-unplaced"
172
250
  | "reset-failed"
173
251
  | "unknown-command"
174
252
  | "command-failed"
@@ -183,10 +261,19 @@ export interface FrameMeta {
183
261
  altaVerified: boolean | null;
184
262
  /** Whether this frame was recovered via FEC (not received directly) */
185
263
  recovered: boolean;
186
- /** Source block number this frame belongs to */
264
+ /** Block key this frame belongs to: the block's SS_Start */
187
265
  sbn: number;
188
- /** Encoding Symbol ID within the recovered source block (0..K-1). */
266
+ /** Encoding Symbol ID within the recovered source block (0..ssbLength-1).
267
+ * Under `iso-ssbg1-v1` a recovered frame is the full T-byte ssbg_mode1
268
+ * symbol (length field, packet, zero padding); the Worker validates but
269
+ * never trims it. */
189
270
  esi?: number;
271
+ /**
272
+ * Authored source-symbol count of the recovered block (the repair
273
+ * symbols' SSB_length, 1..K). Recovered frames are ESI 0..ssbLength-1;
274
+ * ESIs ssbLength..K-1 are encoder zero padding and are never emitted.
275
+ */
276
+ ssbLength?: number;
190
277
  }
191
278
 
192
279
  // --- Worker commands (main -> worker) ---
@@ -209,9 +296,12 @@ export type FecWorkerCommand =
209
296
  | {
210
297
  type: "feedSource";
211
298
  /** 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. */
299
+ * (ISO 23008-1 §C.5.2). SS_IDs are consecutive per source packet mod
300
+ * 2^32 and the block comes from a repair's range — do NOT
301
+ * pre-compute a block. */
214
302
  ssId: number;
303
+ /** The protected packet exactly, unpadded and without its SS_ID (at
304
+ * most T-2 bytes). */
215
305
  data: ArrayBuffer;
216
306
  ts: number;
217
307
  /** Optional ALTA authenticator bytes (detached mode — 08-07 MMTP
@@ -229,18 +319,29 @@ export type FecWorkerCommand =
229
319
  }
230
320
  | {
231
321
  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). */
322
+ /** SS_Start: SS_ID of the first source symbol in the block, from the
323
+ * MMTP Repair FEC Payload ID (ISO 23008-1 §C.5.3): any uint32; the
324
+ * block key. */
235
325
  ssStart: number;
236
- /** SSB_length: K for this block (variable-K allowed per spec). */
326
+ /** Authored source-symbol count for this block; 1..configured K. */
237
327
  ssbLength: number;
238
- /** RS_ID: repair symbol index within the block; WASM maps to
239
- * repair ESI = SSB_length + RS_ID. */
328
+ /** RS_ID: repair symbol index within the block; WASM maps to repair
329
+ * ESI = SSB_length + RS_ID. */
240
330
  rsId: number;
241
331
  data: ArrayBuffer;
242
332
  ts: number;
243
333
  }
334
+ | {
335
+ /**
336
+ * Every source and repair symbol the page fed in one task, in feed
337
+ * order, sharing one transfer list. The client sends symbols this
338
+ * way instead of one message per symbol (BLO-36599); the Worker
339
+ * handles each record exactly like the single-symbol command of the
340
+ * same `type`, and a record that fails does not stop the others.
341
+ */
342
+ type: "feedSymbols";
343
+ symbols: FecSymbolCommand[];
344
+ }
244
345
  | {
245
346
  type: "relayBlockSig";
246
347
  sbn: number;
@@ -249,12 +350,26 @@ export type FecWorkerCommand =
249
350
  }
250
351
  | {
251
352
  type: "cleanup";
252
- /** Every timed-out block covered by this coalesced queue-drain barrier. */
353
+ /** Every timed-out block covered by this coalesced queue-drain barrier,
354
+ * by block key (SS_Start). */
253
355
  sbns: number[];
254
356
  }
255
357
  | { type: "reset" }
358
+ /**
359
+ * Ask the Worker to post a blockUpdate on every block transition. Without
360
+ * it the Worker posts none: each one is a structured clone and a task on
361
+ * the page thread, and the player does not consume them (BLO-36599).
362
+ * Sent only to a Worker that announced it in `capabilities`.
363
+ */
364
+ | { type: "subscribeBlockUpdates" }
256
365
  | { type: "dispose" };
257
366
 
367
+ /** One symbol record of a `feedSymbols` batch. */
368
+ export type FecSymbolCommand = Extract<
369
+ FecWorkerCommand,
370
+ { type: "feedSource" } | { type: "feedRepair" }
371
+ >;
372
+
258
373
  // --- Worker events (worker -> main) ---
259
374
 
260
375
  /** Discriminated union of all events emitted from Worker to main thread. */
@@ -262,7 +377,16 @@ export type FecWorkerEvent =
262
377
  | { type: "frame"; data: ArrayBuffer; meta: FrameMeta }
263
378
  | { type: "snapshot"; stats: FecTrackStats }
264
379
  | { type: "blockUpdate"; block: FecBlockSnapshot }
265
- | { type: "greenFill"; sbn: number; data: ArrayBuffer }
266
380
  | { type: "cleanupComplete"; sbns: number[] }
267
381
  | { type: "resetComplete" }
382
+ /**
383
+ * Optional commands this Worker accepts, posted once it is configured.
384
+ * The client uses `feedSymbols` only after the Worker names it, so a page
385
+ * that meets an older cached Worker bundle keeps sending one command per
386
+ * symbol instead of a batch that Worker would reject (BLO-36599).
387
+ */
388
+ | { type: "capabilities"; commands: FecWorkerOptionalCommand[] }
268
389
  | { type: "error"; message: string; code?: FecWorkerErrorCode };
390
+
391
+ /** Commands a client may send only after the Worker announced them. */
392
+ export type FecWorkerOptionalCommand = "feedSymbols" | "subscribeBlockUpdates";