@blockcast/mmt-container 0.4.0-main.bb90ce9247fa → 0.4.0-main.bbdfdbf39ded

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 (205) 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 +1721 -705
  7. package/dist/browser-runtime.js.map +4 -4
  8. package/dist/carp-catalog.d.ts +72 -34
  9. package/dist/carp-catalog.d.ts.map +1 -1
  10. package/dist/carp-catalog.js +309 -204
  11. package/dist/carp-catalog.js.map +3 -3
  12. package/dist/catalog-fec-params.d.ts +15 -4
  13. package/dist/catalog-fec-params.d.ts.map +1 -1
  14. package/dist/catalog-fec-params.js +137 -50
  15. package/dist/catalog-fec-params.js.map +4 -4
  16. package/dist/catalog-network-source.d.ts +24 -2
  17. package/dist/catalog-network-source.d.ts.map +1 -1
  18. package/dist/catalog-network-source.js +24 -1
  19. package/dist/catalog-network-source.js.map +3 -3
  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/delivery-catalog.js +100 -230
  25. package/dist/delivery-catalog.js.map +4 -4
  26. package/dist/fec-manager.d.ts +64 -1
  27. package/dist/fec-manager.d.ts.map +1 -1
  28. package/dist/fec-manager.js +275 -62
  29. package/dist/fec-manager.js.map +4 -4
  30. package/dist/fec-state.d.ts +5 -3
  31. package/dist/fec-state.d.ts.map +1 -1
  32. package/dist/fec-state.js +34 -12
  33. package/dist/fec-state.js.map +4 -4
  34. package/dist/fec-worker-setup.d.ts +2 -0
  35. package/dist/fec-worker-setup.d.ts.map +1 -1
  36. package/dist/fec-worker-setup.js +15 -3
  37. package/dist/fec-worker-setup.js.map +3 -3
  38. package/dist/index.d.ts +5 -5
  39. package/dist/index.d.ts.map +1 -1
  40. package/dist/index.js +14 -8
  41. package/dist/index.js.map +2 -2
  42. package/dist/iso-block-locator.d.ts +29 -0
  43. package/dist/iso-block-locator.d.ts.map +1 -1
  44. package/dist/iso-block-locator.js +100 -17
  45. package/dist/iso-block-locator.js.map +4 -4
  46. package/dist/manifest-auth-plan.d.ts +26 -25
  47. package/dist/manifest-auth-plan.d.ts.map +1 -1
  48. package/dist/manifest-auth-plan.js +331 -1171
  49. package/dist/manifest-auth-plan.js.map +4 -4
  50. package/dist/mfu-reassembler.js +24 -13
  51. package/dist/mfu-reassembler.js.map +3 -3
  52. package/dist/mmtp-container-parser.d.ts.map +1 -1
  53. package/dist/mmtp-container-parser.js +96 -22
  54. package/dist/mmtp-container-parser.js.map +3 -3
  55. package/dist/mmtp-container-parser.no-modules.js +1138 -325
  56. package/dist/mmtp-container-parser.no-modules.js.map +4 -4
  57. package/dist/mmtp-decode-pipeline.js +96 -22
  58. package/dist/mmtp-decode-pipeline.js.map +3 -3
  59. package/dist/mmtp-fec-session.d.ts +159 -0
  60. package/dist/mmtp-fec-session.d.ts.map +1 -0
  61. package/dist/mmtp-fec-session.js +531 -0
  62. package/dist/mmtp-fec-session.js.map +7 -0
  63. package/dist/mmtp-router.d.ts +31 -5
  64. package/dist/mmtp-router.d.ts.map +1 -1
  65. package/dist/mmtp-router.js +168 -22
  66. package/dist/mmtp-router.js.map +3 -3
  67. package/dist/multicast-auth-catalog.d.ts +86 -92
  68. package/dist/multicast-auth-catalog.d.ts.map +1 -1
  69. package/dist/multicast-auth-catalog.js +347 -132
  70. package/dist/multicast-auth-catalog.js.map +4 -4
  71. package/dist/multicast-input.d.ts +28 -6
  72. package/dist/multicast-input.d.ts.map +1 -1
  73. package/dist/multicast-input.js +293 -28
  74. package/dist/multicast-input.js.map +3 -3
  75. package/dist/multicast-manager.d.ts +89 -5
  76. package/dist/multicast-manager.d.ts.map +1 -1
  77. package/dist/multicast-manager.js +730 -356
  78. package/dist/multicast-manager.js.map +4 -4
  79. package/dist/multicast-transport.d.ts +12 -2
  80. package/dist/multicast-transport.d.ts.map +1 -1
  81. package/dist/multicast-transport.js +111 -235
  82. package/dist/multicast-transport.js.map +4 -4
  83. package/dist/multicast-unwrap.js +24 -13
  84. package/dist/multicast-unwrap.js.map +3 -3
  85. package/dist/no-modules.d.ts +4 -1
  86. package/dist/no-modules.d.ts.map +1 -1
  87. package/dist/no-modules.js +1087 -309
  88. package/dist/no-modules.js.map +4 -4
  89. package/dist/phase14-catalog-override.d.ts +1 -1
  90. package/dist/phase14-catalog-override.d.ts.map +1 -1
  91. package/dist/phase14-catalog-override.js +2 -2
  92. package/dist/phase14-catalog-override.js.map +2 -2
  93. package/dist/repair-fetch.d.ts +81 -6
  94. package/dist/repair-fetch.d.ts.map +1 -1
  95. package/dist/repair-fetch.js +159 -31
  96. package/dist/repair-fetch.js.map +3 -3
  97. package/dist/repair-layers.d.ts +2 -0
  98. package/dist/repair-layers.d.ts.map +1 -1
  99. package/dist/repair-layers.js +22 -2
  100. package/dist/repair-layers.js.map +3 -3
  101. package/dist/sample-combiner.d.ts +7 -1
  102. package/dist/sample-combiner.d.ts.map +1 -1
  103. package/dist/sample-combiner.js +14 -2
  104. package/dist/sample-combiner.js.map +2 -2
  105. package/dist/signaling.d.ts +2 -2
  106. package/dist/signaling.d.ts.map +1 -1
  107. package/dist/signaling.js +6 -3
  108. package/dist/signaling.js.map +3 -3
  109. package/dist/source-auth.js +2710 -7
  110. package/dist/source-auth.js.map +4 -4
  111. package/dist/switching-set-alignment.d.ts +1 -1
  112. package/dist/switching-set-alignment.js.map +1 -1
  113. package/dist/timestamp-codec.js +24 -13
  114. package/dist/timestamp-codec.js.map +3 -3
  115. package/dist/wasm-mfu-reassembler.js +3 -0
  116. package/dist/wasm-mfu-reassembler.js.map +3 -3
  117. package/dist/wasm-mmtp-container-parser.js +96 -22
  118. package/dist/wasm-mmtp-container-parser.js.map +3 -3
  119. package/package.json +6 -9
  120. package/schemas/catalog.schema.json +124 -37
  121. package/src/align.ts +52 -21
  122. package/src/carp-catalog.ts +163 -61
  123. package/src/catalog-fec-params.ts +66 -20
  124. package/src/catalog-network-source.ts +45 -2
  125. package/src/catalog-validator.ts +4 -11
  126. package/src/fec-manager.ts +239 -63
  127. package/src/fec-state.ts +14 -12
  128. package/src/fec-worker-setup.ts +20 -2
  129. package/src/index.ts +29 -10
  130. package/src/iso-block-locator.ts +97 -6
  131. package/src/manifest-auth-plan.ts +39 -91
  132. package/src/mmtp-container-parser.ts +75 -7
  133. package/src/mmtp-fec-session.ts +533 -0
  134. package/src/mmtp-router.ts +133 -5
  135. package/src/multicast-auth-catalog.ts +131 -238
  136. package/src/multicast-input.ts +119 -61
  137. package/src/multicast-manager.ts +258 -21
  138. package/src/multicast-transport.ts +17 -3
  139. package/src/no-modules.ts +7 -1
  140. package/src/phase14-catalog-override.ts +3 -3
  141. package/src/repair-fetch.ts +283 -39
  142. package/src/repair-layers.ts +22 -2
  143. package/src/sample-combiner.ts +15 -2
  144. package/src/signaling.ts +2 -2
  145. package/src/switching-set-alignment.ts +1 -1
  146. package/test-vectors/align.json +108 -32
  147. package/test-vectors/bc-provenance-v2.json +1526 -0
  148. package/test-vectors/catalog/README.md +65 -4
  149. package/test-vectors/catalog/negative/fec-block-profile-missing.json +1 -1
  150. package/test-vectors/catalog/negative/fec-block-profile-retired.json +1 -1
  151. package/test-vectors/catalog/negative/fec-block-profile-unknown.json +1 -1
  152. package/test-vectors/catalog/negative/fec-iso-symbol-size-below-floor.json +28 -0
  153. package/test-vectors/catalog/negative/fec-reed-solomon-iso-symbol-size-13.json +28 -0
  154. package/test-vectors/catalog/negative/fec-reed-solomon-k-plus-p-over-255.json +39 -0
  155. package/test-vectors/catalog/negative/fec-reed-solomon-overlay-over-255.json +40 -0
  156. package/test-vectors/catalog/negative/fec-repair-overlay-source-symbols-zero.json +43 -0
  157. package/test-vectors/catalog/negative/fec-repair-symbols-zero.json +28 -0
  158. package/test-vectors/catalog/negative/fec-source-symbols-zero.json +28 -0
  159. package/test-vectors/catalog/negative/fec-symbol-size-zero.json +28 -0
  160. package/test-vectors/catalog/negative/init-data-inline-without-data.json +22 -0
  161. package/test-vectors/catalog/negative/mmtp-mode-mpu.json +15 -0
  162. package/test-vectors/catalog/negative/multicast-amt-dns-without-relay.json +37 -0
  163. package/test-vectors/catalog/negative/multicast-auth-asm-missing-sender-address.json +77 -0
  164. package/test-vectors/catalog/negative/multicast-auth-duplicate-first-epoch.json +56 -0
  165. package/test-vectors/catalog/negative/multicast-auth-duplicate-kid.json +56 -0
  166. package/test-vectors/catalog/negative/multicast-auth-endpoint-auth-without-object-auth.json +38 -0
  167. package/test-vectors/catalog/negative/multicast-auth-endpoint-missing-auth.json +49 -0
  168. package/test-vectors/catalog/negative/multicast-auth-endpoint-missing-bandwidth.json +55 -0
  169. package/test-vectors/catalog/negative/multicast-auth-manifest-port-equals-port.json +56 -0
  170. package/test-vectors/catalog/negative/multicast-auth-manifest-timer-out-of-range.json +56 -0
  171. package/test-vectors/catalog/negative/multicast-auth-max-auth-delay-above-max.json +56 -0
  172. package/test-vectors/catalog/negative/multicast-auth-max-auth-delay-below-min.json +56 -0
  173. package/test-vectors/catalog/negative/multicast-auth-root-dotted-key.json +55 -0
  174. package/test-vectors/catalog/negative/multicast-auth-sender-address-on-ssm.json +57 -0
  175. package/test-vectors/catalog/negative/multicast-auth-version-1.json +56 -0
  176. package/test-vectors/catalog/negative/multicast-auth-version-3.json +56 -0
  177. package/test-vectors/catalog/negative/multicast-auth-version-missing.json +55 -0
  178. package/test-vectors/catalog/negative/multicast-endpoint-foreign-namespace-track.json +44 -0
  179. package/test-vectors/catalog/negative/multicast-endpoint-protocol-amt.json +38 -0
  180. package/test-vectors/catalog/negative/multicast-endpoint-protocol-atsc3.json +38 -0
  181. package/test-vectors/catalog/negative/presentation-anchor-unsafe.json +16 -0
  182. package/test-vectors/catalog/positive/fec-algorithm-unimplemented.json +29 -0
  183. package/test-vectors/catalog/positive/fec-iso-ssbg1.json +1 -1
  184. package/test-vectors/catalog/positive/fec-multicast.json +1 -1
  185. package/test-vectors/catalog/positive/fec-reed-solomon-layered.json +50 -0
  186. package/test-vectors/catalog/positive/mmt01-12-1-init-ref-anchor.json +29 -0
  187. package/test-vectors/catalog/positive/multicast-amt-discovery-omitted.json +37 -0
  188. package/test-vectors/catalog/positive/multicast-amt-dns-relay.json +38 -0
  189. package/test-vectors/catalog/positive/multicast-amt-manual-relay.json +2 -2
  190. package/test-vectors/catalog/{negative/multicast-endpoint-missing-protocol-and-source.json → positive/multicast-asm-unprotected.json} +10 -5
  191. package/test-vectors/catalog/positive/multicast-auth-asm-sender.json +78 -0
  192. package/test-vectors/catalog/positive/multicast-auth-rotation.json +39 -38
  193. package/test-vectors/catalog/positive/multicast-draft01-4-1-example.json +81 -0
  194. package/test-vectors/catalog/positive/multicast-draft01-7-2-6-example.json +70 -0
  195. package/test-vectors/catalog/positive/presentation-anchor-negative.json +27 -0
  196. package/test-vectors/catalog/section-codes.json +30 -1
  197. package/test-vectors/catalog-multicast-auth-v1.json +38 -20
  198. package/test-vectors/catalog-validation.json +101 -5
  199. package/test-vectors/catalog-validator-public-api.mjs +14 -9
  200. package/test-vectors/generate-bc-provenance-v2.mjs +252 -0
  201. package/dist/fec-codec.d.ts +0 -32
  202. package/dist/fec-codec.d.ts.map +0 -1
  203. package/dist/fec-codec.js +0 -47
  204. package/dist/fec-codec.js.map +0 -7
  205. package/src/fec-codec.ts +0 -61
@@ -20,6 +20,7 @@ import {
20
20
  deriveFecRetentionBlocks,
21
21
  LOCAL_FEC_RETENTION_BLOCKS,
22
22
  LOCAL_FEC_TIMEOUT_MS,
23
+ MAX_FEC_RETENTION_BLOCKS,
23
24
  } from '@blockcast/mmt-fec'
24
25
  import { computeFecDeliveryWindowMs } from './track-session.js'
25
26
 
@@ -47,6 +48,8 @@ export interface FecWorkerSetupOptions {
47
48
  * Worker is created. Never defaulted, never inferred.
48
49
  */
49
50
  fecBlockProfile: FecBlockProfile
51
+ /** The track's FEC scheme (`TrackFecParams.fecAlgorithm`). Defaults to RaptorQ. */
52
+ fecAlgorithm?: 'raptorq' | 'reed-solomon'
50
53
  /**
51
54
  * FEC K from catalog: the maximum SSB_length (K_max) of any block. Blocks
52
55
  * are variable-length ssbg_mode1 blocks keyed by SS_Start, so K bounds
@@ -237,7 +240,7 @@ export async function setupFecWorkerForTrack(
237
240
  trackType: opts.trackType,
238
241
  framerate: opts.framerate,
239
242
  sampleRate: opts.sampleRate,
240
- algorithm: 'raptor',
243
+ algorithm: opts.fecAlgorithm ?? 'raptorq',
241
244
  blockProfile,
242
245
  k: opts.fecK,
243
246
  // P as this receiver joined it, not the catalog total: the worker snapshot
@@ -252,10 +255,10 @@ export async function setupFecWorkerForTrack(
252
255
  relayBlockSigEnabled: false,
253
256
  } as unknown as FecTrackConfig
254
257
 
255
- client.configure(fecTrackConfig, rqWasm)
256
258
  try {
257
259
  opts.multicastManager.setFecWorker(opts.packetId, client, opts.trackType, {
258
260
  blockProfile,
261
+ algorithm: opts.fecAlgorithm ?? 'raptorq',
259
262
  k: opts.fecK,
260
263
  symbolSize: opts.fecSymbolSize,
261
264
  interleaveDepthMs: opts.fecInterleaveMs,
@@ -276,6 +279,21 @@ export async function setupFecWorkerForTrack(
276
279
  }
277
280
  throw e
278
281
  }
282
+ // The Worker retires a block `interleaveDepth` blocks behind the newest
283
+ // SS_ID it has seen, repair SS_Starts included, by the metric the manager's
284
+ // locator uses for its own horizon. Repairs ride their own group and can run
285
+ // blocks ahead of their media, so a shorter Worker horizon fails a block whose
286
+ // late sources the manager still waits for.
287
+ // ponytail: read once here; a keyframe cadence configured later widens only
288
+ // the manager's horizon (reconfigure the Worker if a player ever does that).
289
+ fecTrackConfig.interleaveDepth = Math.min(
290
+ MAX_FEC_RETENTION_BLOCKS,
291
+ Math.max(
292
+ interleaveDepthBlocks,
293
+ opts.multicastManager.fecManager.isoRetentionBlocksForPacket(opts.packetId) ?? 0,
294
+ ),
295
+ )
296
+ client.configure(fecTrackConfig, rqWasm)
279
297
 
280
298
  let disposed = false
281
299
  return {
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,6 +265,7 @@ export {
265
265
  type WasmFecDecoder,
266
266
  type FecStats,
267
267
  type MulticastSymbolDelivery,
268
+ type UndelimitedIsoLoss,
268
269
  type FecBlockResolution,
269
270
  type RepairVia,
270
271
  } from './fec-manager.js'
@@ -325,14 +326,6 @@ export {
325
326
  type WasmMmtpParserConfig,
326
327
  } from './wasm-mmtp-container-parser.js'
327
328
 
328
- // FEC Codec (generic, algorithm-pluggable)
329
- export {
330
- type FecCodec,
331
- NoneCodec,
332
- ReedSolomonCodec,
333
- createFecCodec,
334
- } from './fec-codec.js'
335
-
336
329
  // Multicast Transport (SSM join/socket/watchdog)
337
330
  export {
338
331
  MulticastTransport,
@@ -552,6 +545,12 @@ export {
552
545
  type TrackFecParams,
553
546
  type TrackFecUnavailable,
554
547
  catalogFecUnavailable,
548
+ catalogFecAvailable,
549
+ catalogFecDeclaresUnconfigurableInstance,
550
+ fecAlgorithmUnimplemented,
551
+ locateCatalogFecSource,
552
+ type CatalogFecRole,
553
+ type CatalogFecSource,
555
554
  catalogGroupDurationMs,
556
555
  catalogKeyframeCadenceMs,
557
556
  catalogFecMediaUnitsPerSecond,
@@ -611,10 +610,26 @@ export {
611
610
  type NoFecCounters,
612
611
  } from "./fec-state.js";
613
612
 
613
+ // In-band AL-FEC for one MMTP asset over a MoQT subscription, for a player
614
+ // that owns its own parser (the Shaka MSF MMTP packaging).
615
+ export {
616
+ MmtpFecSession,
617
+ type MmtpFecSessionCatalogFec,
618
+ type MmtpFecSessionConfig,
619
+ type MmtpFecSessionDecoder,
620
+ type MmtpFecSessionDecoderConstructor,
621
+ type MmtpFecSessionMixedPathStats,
622
+ type MmtpFecSessionPath,
623
+ type MmtpFecSessionPathCounts,
624
+ type MmtpFecSessionStats,
625
+ } from "./mmtp-fec-session.js";
626
+
614
627
  export {
615
628
  catalogAmtNetworkSource,
629
+ catalogAtsc3NetworkSource,
616
630
  DEFAULT_AMT_RELAY_PORT,
617
631
  type AmtNetworkSource,
632
+ type Atsc3NetworkSource,
618
633
  type CatalogNetworkSource,
619
634
  } from "./catalog-network-source.js";
620
635
 
@@ -629,7 +644,11 @@ export {
629
644
  type RepairFetchPriority,
630
645
  type RepairFetchRequest,
631
646
  type RepairFetchTransport,
647
+ type RepairRangeFetchRequest,
648
+ type RepairRangeFetchTransport,
649
+ type RepairDeadlineBound,
632
650
  type RepairPriorityReason,
651
+ type RepairSendMeasurement,
633
652
  type RepairSkipReason,
634
653
  type RepairSubscribeCallback,
635
654
  type WireRepairFetchOptions,
@@ -51,6 +51,12 @@ 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
62
  | { readonly status: 'conflict' | 'overlap' | 'stale'; readonly reason: string }
@@ -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
  }
@@ -243,6 +272,23 @@ export class IsoBlockLocator<T> {
243
272
  }
244
273
  return { status: 'placed', block: known, placed: [] }
245
274
  }
275
+ const pending = this.#holes.find((h) => isoBlockContains(h.start, h.length, start))
276
+ if (pending !== undefined) {
277
+ if (!isoBlockContains(pending.start, pending.length, serial32Add(start, length - 1))) {
278
+ return { status: 'overlap', reason: `repair block ${start}+${length} runs past pending hole ${pending.start}+${pending.length}` }
279
+ }
280
+ // A FETCHed repair stated a block inside a pending hole: split the hole round it.
281
+ this.#holes.splice(this.#holes.indexOf(pending), 1)
282
+ const before = serial32Distance(pending.start, start)
283
+ const afterStart = isoBlockEnd(start, length)
284
+ const after = pending.length - before - length
285
+ if (before > 0) this.#holes.push({ start: pending.start, length: before, span: false })
286
+ if (after > 0) this.#holes.push({ start: afterStart, length: after, span: false })
287
+ const block: IsoBlockRange = { start, length, span: false }
288
+ const placed = this.#claim(block)
289
+ this.#blocks.push(block)
290
+ return { status: 'placed', block, placed }
291
+ }
246
292
  for (const block of this.#blocks) {
247
293
  if (
248
294
  isoBlockContains(block.start, block.length, start) ||
@@ -255,6 +301,7 @@ export class IsoBlockLocator<T> {
255
301
  }
256
302
  }
257
303
  let span: { block: IsoBlockRange; placed: IsoPlacedSource<T>[] } | undefined
304
+ let hole: { block: IsoBlockRange; lostSsId: number } | undefined
258
305
  if (this.#coverageEnd === undefined) {
259
306
  // Reception starts at the first stated block. Held sources before it
260
307
  // are the tail of blocks that began before the join: their range is
@@ -266,24 +313,57 @@ export class IsoBlockLocator<T> {
266
313
  } else if (serial32Precedes(start, this.#coverageEnd)) {
267
314
  return { status: 'stale', reason: `repair block ${start}+${length} precedes known coverage` }
268
315
  } else if (start !== this.#coverageEnd) {
269
- const spanBlock: IsoBlockRange = {
270
- start: this.#coverageEnd,
271
- length: serial32Distance(this.#coverageEnd, start),
272
- span: true,
316
+ const gap = { start: this.#coverageEnd, length: serial32Distance(this.#coverageEnd, start) }
317
+ const lostSsId = this.deferIncompleteGaps ? this.#highestMissing(gap.start, gap.length) : undefined
318
+ if (lostSsId !== undefined) {
319
+ const holeBlock: IsoBlockRange = { ...gap, span: false }
320
+ this.#holes.push(holeBlock)
321
+ hole = { block: holeBlock, lostSsId }
322
+ } else {
323
+ const spanBlock: IsoBlockRange = { ...gap, span: true }
324
+ span = { block: spanBlock, placed: this.#claim(spanBlock) }
325
+ this.#blocks.push(spanBlock)
273
326
  }
274
- span = { block: spanBlock, placed: this.#claim(spanBlock) }
275
- this.#blocks.push(spanBlock)
276
327
  }
277
328
  const block: IsoBlockRange = { start, length, span: false }
278
329
  const placed = this.#claim(block)
279
330
  this.#blocks.push(block)
280
331
  this.#coverageEnd = isoBlockEnd(start, length)
281
332
  this.retireBehind(start)
333
+ if (hole !== undefined) return { status: 'placed', block, placed, hole }
282
334
  return span === undefined
283
335
  ? { status: 'placed', block, placed }
284
336
  : { status: 'placed', block, placed, span }
285
337
  }
286
338
 
339
+ /**
340
+ * Turn every pending hole inside `[start, start + length)` into a
341
+ * repair-less span, claiming its held sources: its range FETCH ended, so
342
+ * nothing more will state its blocks. Returns the spans, in SS_ID order.
343
+ */
344
+ closeHolesWithin(start: number, length: number): { block: IsoBlockRange; placed: IsoPlacedSource<T>[] }[] {
345
+ const closed: { block: IsoBlockRange; placed: IsoPlacedSource<T>[] }[] = []
346
+ for (const hole of [...this.#holes]) {
347
+ if (!isoBlockContains(start, length, hole.start)) continue
348
+ this.#holes.splice(this.#holes.indexOf(hole), 1)
349
+ const spanBlock: IsoBlockRange = { start: hole.start, length: hole.length, span: true }
350
+ closed.push({ block: spanBlock, placed: this.#claim(spanBlock) })
351
+ this.#blocks.push(spanBlock)
352
+ }
353
+ return closed.sort((x, y) => serial32Distance(start, x.block.start) - serial32Distance(start, y.block.start))
354
+ }
355
+
356
+ /** The highest SS_ID in the range no source is held for, or undefined when all are. */
357
+ #highestMissing(start: number, length: number): number | undefined {
358
+ // Every position past the held count cannot all be held: bounded scan.
359
+ const steps = Math.min(length, this.#held.size + 1)
360
+ for (let i = 1; i <= steps; i++) {
361
+ const ssId = serial32Add(start, length - i)
362
+ if (!this.#held.has(ssId)) return ssId
363
+ }
364
+ return undefined
365
+ }
366
+
287
367
  /**
288
368
  * Retire every block and span whose last SS_ID, and every held source,
289
369
  * lies more than `retentionBlocks` blocks behind `position`, with the shared
@@ -302,6 +382,11 @@ export class IsoBlockLocator<T> {
302
382
  this.#blocks.splice(i, 1)
303
383
  }
304
384
  }
385
+ for (let i = this.#holes.length - 1; i >= 0; i--) {
386
+ const hole = this.#holes[i]!
387
+ const last = serial32Add(hole.start, hole.length - 1)
388
+ if (isoBlocksBehindLowerBound(last, position, this.#kMax) > horizon) this.#holes.splice(i, 1)
389
+ }
305
390
  for (const ssId of [...this.#held.keys()]) {
306
391
  if (isoBlocksBehindLowerBound(ssId, position, this.#kMax) > horizon) this.#held.delete(ssId)
307
392
  }
@@ -311,6 +396,7 @@ export class IsoBlockLocator<T> {
311
396
  /** Forget everything (publisher restart, track reset). */
312
397
  reset(): void {
313
398
  this.#blocks.length = 0
399
+ this.#holes.length = 0
314
400
  this.#held.clear()
315
401
  this.#coverageEnd = undefined
316
402
  this.#newest = undefined
@@ -327,3 +413,8 @@ export class IsoBlockLocator<T> {
327
413
  return placed
328
414
  }
329
415
  }
416
+
417
+ /** Whether `[aStart, aStart + aLength)` and `[bStart, bStart + bLength)` share an SS_ID. */
418
+ function isoRangesOverlap(aStart: number, aLength: number, bStart: number, bLength: number): boolean {
419
+ return serial32Distance(aStart, bStart) < aLength || serial32Distance(bStart, aStart) < bLength
420
+ }
@@ -1,107 +1,55 @@
1
+ import type { ManifestKey } from '@blockcast/mmt-manifest-verify'
1
2
  import {
2
- ManifestVerifier,
3
- manifestBindingId,
4
- type ManifestAuthConfig,
5
- type ManifestTrackContext,
6
- type SignatureVerifier,
7
- } from '@blockcast/mmt-manifest-verify'
8
- import {
3
+ multicastAuthKeyFor,
9
4
  resolveMulticastAuthCatalog,
10
- type MulticastAuthMode,
11
5
  type ResolvedMulticastAuthConfig,
6
+ type ResolvedMulticastAuthFlow,
12
7
  } from './multicast-auth-catalog.js'
13
8
 
14
- export interface ManifestAuthBinding {
15
- bindingId: string
16
- mediaTrack: string
17
- manifestTrack: string
18
- sourceSymbolFormat: string
19
- }
20
-
9
+ /**
10
+ * The bc-provenance version 2 plan a receiver binds from one catalog
11
+ * (draft-ramadan-moq-multicast-01 7.2.6): the key set, `minEpoch`, and one
12
+ * flow per protected endpoint. The record verifier and the 7.2.7 state machine
13
+ * consume it; this plan holds no verification state.
14
+ */
21
15
  export interface ManifestAuthPlan {
22
- verifier: ManifestVerifier
23
- mode: MulticastAuthMode
24
- bindings: readonly ManifestAuthBinding[]
16
+ keys: readonly ManifestKey[]
17
+ minEpoch: number
18
+ flows: readonly ResolvedMulticastAuthFlow[]
19
+ /** Every `manifestTrack` a flow names, for a unicast receiver to subscribe. */
25
20
  manifestTracks: readonly string[]
26
- bindingIdFor(mediaTrack: string, sourceSymbolFormat: string): string | undefined
27
- ingestManifestPayload(manifestTrack: string, payload: Uint8Array): void
28
- dispose(): void
21
+ /** The key that signs a record of `kid` at `epoch`; `undefined` drops it.
22
+ * Key selection only: it does not apply `minEpoch`, which the record
23
+ * verifier checks (BLO-49771). */
24
+ keyFor(kid: number, epoch: number): Uint8Array | undefined
25
+ /** The flow whose records arrive on `(S, G, manifestPort)`. */
26
+ flowForManifestPort(source: string, groupAddress: string, manifestPort: number): ResolvedMulticastAuthFlow | undefined
27
+ /** The flow a manifest track mirrors. */
28
+ flowForManifestTrack(manifestTrack: string): ResolvedMulticastAuthFlow | undefined
29
29
  }
30
30
 
31
- type ContextFields = Omit<
32
- ManifestTrackContext,
33
- 'bindingId' | 'trackName' | 'sourceSymbolFormat' | 'authScope' | 'publicKey' | 'acceptedPublicKeys'
34
- >
35
-
36
- /** Build the one canonical verifier plan shared by browser players. Transport
37
- * implementations subscribe the exact returned manifestTracks and feed each
38
- * bare source-auth frame to ingestManifestPayload without name inference. */
31
+ /** Build the plan, or `undefined` for an unprotected catalog. Throws where
32
+ * `resolveMulticastAuthCatalog` does: no key set, or authentication pinned
33
+ * by `requireAuth` on a catalog without `multicast.auth`. */
39
34
  export function createManifestAuthPlan(
40
35
  catalog: unknown,
41
- options: {
42
- playerMode?: MulticastAuthMode
43
- onMissingManifest?: ManifestAuthConfig['onMissingManifest']
44
- verifySignature: SignatureVerifier
45
- contextFor(
46
- binding: ResolvedMulticastAuthConfig['tracks'][number],
47
- resolved: ResolvedMulticastAuthConfig,
48
- ): ContextFields
49
- onFailure?: ConstructorParameters<typeof ManifestVerifier>[0]['onFailure']
50
- },
36
+ options: { requireAuth?: boolean; catalogNamespace?: string } = {},
51
37
  ): ManifestAuthPlan | undefined {
52
- const resolved = resolveMulticastAuthCatalog(catalog, options.playerMode)
53
- if (!resolved || resolved.mode === 'off') return undefined
54
-
55
- const verifier = new ManifestVerifier({
56
- manifestAuth: {
57
- mode: resolved.mode,
58
- onMissingManifest: options.onMissingManifest ??
59
- (resolved.mode === 'enforce' ? 'stall' : 'fallback-shadow'),
60
- },
61
- verifySignature: options.verifySignature,
62
- ...(options.onFailure === undefined ? {} : { onFailure: options.onFailure }),
63
- })
64
- const byManifestTrack = new Map<string, ManifestAuthBinding>()
65
- const bindings = resolved.tracks.map((binding): ManifestAuthBinding => {
66
- const bindingId = manifestBindingId(binding.mediaTrack, binding.sourceSymbolFormat)
67
- const planned = {
68
- bindingId,
69
- mediaTrack: binding.mediaTrack,
70
- manifestTrack: binding.manifestTrack,
71
- sourceSymbolFormat: binding.sourceSymbolFormat,
72
- }
73
- verifier.registerTrack({
74
- ...options.contextFor(binding, resolved),
75
- bindingId,
76
- trackName: binding.mediaTrack,
77
- sourceSymbolFormat: binding.sourceSymbolFormat,
78
- authScope: binding.authScope,
79
- publicKey: resolved.acceptedPublicKeys[0]!,
80
- acceptedPublicKeys: resolved.acceptedPublicKeys,
81
- })
82
- byManifestTrack.set(binding.manifestTrack, planned)
83
- return planned
84
- })
85
- let disposed = false
86
-
38
+ const resolved: ResolvedMulticastAuthConfig | undefined = resolveMulticastAuthCatalog(catalog, options)
39
+ if (!resolved) return undefined
40
+ const byManifestTrack = new Map<string, ResolvedMulticastAuthFlow>()
41
+ for (const flow of resolved.flows) {
42
+ if (flow.manifestTrack !== undefined) byManifestTrack.set(flow.manifestTrack, flow)
43
+ }
87
44
  return {
88
- verifier,
89
- mode: resolved.mode,
90
- bindings,
45
+ keys: resolved.keys,
46
+ minEpoch: resolved.minEpoch,
47
+ flows: resolved.flows,
91
48
  manifestTracks: [...byManifestTrack.keys()],
92
- bindingIdFor(mediaTrack, sourceSymbolFormat) {
93
- const bindingId = manifestBindingId(mediaTrack, sourceSymbolFormat)
94
- return bindings.some((binding) => binding.bindingId === bindingId) ? bindingId : undefined
95
- },
96
- ingestManifestPayload(manifestTrack, payload) {
97
- const binding = byManifestTrack.get(manifestTrack)
98
- if (!binding) throw new Error(`unknown manifest subscription track ${manifestTrack}`)
99
- verifier.ingestManifestFrame(binding.bindingId, payload)
100
- },
101
- dispose() {
102
- if (disposed) return
103
- disposed = true
104
- verifier.dispose()
105
- },
49
+ keyFor: (kid, epoch) => multicastAuthKeyFor(resolved, kid, epoch),
50
+ flowForManifestPort: (source, groupAddress, manifestPort) =>
51
+ resolved.flows.find(flow =>
52
+ flow.source === source && flow.groupAddress === groupAddress && flow.manifestPort === manifestPort),
53
+ flowForManifestTrack: manifestTrack => byManifestTrack.get(manifestTrack),
106
54
  }
107
55
  }
@@ -1034,8 +1034,23 @@ function isFmp4Box(data: Uint8Array): boolean {
1034
1034
  interface TimingStreamState {
1035
1035
  packetId: number
1036
1036
  resolver: IsoBmffTimingResolver
1037
+ /** FI=0/FI=1 heads that arrived before their sample's FT=1, oldest first. */
1038
+ awaitingTiming: AwaitingTimingHead[]
1037
1039
  }
1038
1040
 
1041
+ /** A head refused for missing timing, kept to be fed again when its FT=1 lands. */
1042
+ interface AwaitingTimingHead {
1043
+ packet: Uint8Array
1044
+ options: ParseHeaderOptions | undefined
1045
+ parsed: ParsedMmtpPacket
1046
+ mpuSequenceNumber: number
1047
+ movieFragmentSequenceNumber: number
1048
+ sampleNumber: number
1049
+ arrivedAt: number
1050
+ }
1051
+
1052
+ const MAX_AWAITING_TIMING_HEADS = 8
1053
+
1039
1054
  // ── MmtpContainerParser ─────────────────────────────────────────────
1040
1055
 
1041
1056
  export class MmtpContainerParser {
@@ -1098,6 +1113,7 @@ export class MmtpContainerParser {
1098
1113
  * reordering into the parser. */
1099
1114
  #reliableMoqStartupGate = new Map<string, number>()
1100
1115
  #disposed = false
1116
+ #replayingAwaitingTiming = false
1101
1117
  #fedObjects = 0
1102
1118
  #reassemblerFragmentsFed = 0
1103
1119
  #reassemblerDataUnitsReturned = 0
@@ -1446,7 +1462,10 @@ export class MmtpContainerParser {
1446
1462
  * a single failure bucket. Returns null so call sites stay `return this.#drop(...)`.
1447
1463
  */
1448
1464
  #drop(reason: MmtpFeedDropReason): null {
1449
- this.#countOutcome(`drop:${reason}`)
1465
+ // A replayed head was counted when it was fed; what befalls it now is a
1466
+ // side effect of the FT=1 that released it.
1467
+ if (this.#replayingAwaitingTiming) this.#countDeferredOutcome(reason)
1468
+ else this.#countOutcome(`drop:${reason}`)
1450
1469
  return null
1451
1470
  }
1452
1471
 
@@ -1471,6 +1490,12 @@ export class MmtpContainerParser {
1471
1490
  for (const combiner of this.#sampleCombiners.values()) {
1472
1491
  combiner.cleanupTimeouts(timeoutMs)
1473
1492
  }
1493
+ const now = Date.now()
1494
+ for (const { awaitingTiming } of this.#timingStreams.values()) {
1495
+ while (awaitingTiming.length > 0 && now - awaitingTiming[0]!.arrivedAt > timeoutMs) {
1496
+ awaitingTiming.shift()
1497
+ }
1498
+ }
1474
1499
  }
1475
1500
 
1476
1501
  /**
@@ -1587,7 +1612,7 @@ export class MmtpContainerParser {
1587
1612
  // Fire onSourceSymbol BEFORE MFU extraction so the consumer-side FEC
1588
1613
  // pipeline (e.g. FecWorkerClient.feedSource) starts as early as possible.
1589
1614
  // Source-only: parseHeader already gates fecPayloadId on !isRepair.
1590
- if (info.fecPayloadId !== null && this.#sourceSymbolCb) {
1615
+ if (info.fecPayloadId !== null && this.#sourceSymbolCb && !this.#replayingAwaitingTiming) {
1591
1616
  // Strip the 4-byte fec_payload_id trailer per ISO 23008-1 §C.5.2.
1592
1617
  // Symbol-size zero-padding is the consumer's responsibility — FEC
1593
1618
  // symbol size is a FEC-config concern, not a parser concern.
@@ -1654,6 +1679,7 @@ export class MmtpContainerParser {
1654
1679
  }
1655
1680
  return this.#drop('fragment-timing-refused')
1656
1681
  }
1682
+ this.#replayAwaitingTiming(streamKey, resolver)
1657
1683
  return {
1658
1684
  kind: 'fragment',
1659
1685
  packet: info,
@@ -1679,7 +1705,18 @@ export class MmtpContainerParser {
1679
1705
  mfuDuHeader.movieFragmentSequenceNumber,
1680
1706
  mfuDuHeader.sampleNumber,
1681
1707
  )
1682
- if (!headTiming) return this.#drop('mfu-sample-timing-missing')
1708
+ if (!headTiming) {
1709
+ this.#awaitTiming(streamKey, {
1710
+ packet,
1711
+ options,
1712
+ parsed,
1713
+ mpuSequenceNumber: mpu.mpuSequenceNumber,
1714
+ movieFragmentSequenceNumber: mfuDuHeader.movieFragmentSequenceNumber,
1715
+ sampleNumber: mfuDuHeader.sampleNumber,
1716
+ arrivedAt: Date.now(),
1717
+ })
1718
+ return this.#drop('mfu-sample-timing-missing')
1719
+ }
1683
1720
  // A data unit's authored base offset is its position within the sample
1684
1721
  // (moqcast #37): 0 for a whole-sample DU, nonzero for one subsample DU
1685
1722
  // among siblings. Sizes below are therefore relative to that base, not
@@ -1781,11 +1818,12 @@ export class MmtpContainerParser {
1781
1818
  // a sample this stream declared, so it must be dropped outright -- letting
1782
1819
  // it buffer would occupy a combiner slot and could evict a legitimate
1783
1820
  // partitioned sample waiting on a sibling.
1784
- if (!resolver.sampleTiming(
1821
+ const assembledTiming = resolver.sampleTiming(
1785
1822
  assembled.mpuSequenceNumber,
1786
1823
  movieFragmentSequenceNumber,
1787
1824
  sampleNumber,
1788
- )) return this.#drop('assembled-timing-missing')
1825
+ )
1826
+ if (!assembledTiming) return this.#drop('assembled-timing-missing')
1789
1827
  if (assembled.mpuSequenceNumber !== mpu.mpuSequenceNumber)
1790
1828
  return this.#drop('assembled-mpu-mismatch')
1791
1829
 
@@ -1795,7 +1833,7 @@ export class MmtpContainerParser {
1795
1833
  // unpartitioned sample passes straight through with no buffering.
1796
1834
  let combined: CombinedSample | null
1797
1835
  try {
1798
- combined = this.#sampleCombinerFor(streamKey).addMfu(assembled)
1836
+ combined = this.#sampleCombinerFor(streamKey).addMfu(assembled, assembledTiming.size)
1799
1837
  } catch (error) {
1800
1838
  if (!(error instanceof SampleCombineError)) throw error
1801
1839
  console.warn(
@@ -2150,10 +2188,40 @@ export class MmtpContainerParser {
2150
2188
  if (existing) return existing.resolver
2151
2189
  if (!create) return null
2152
2190
  const resolver = new IsoBmffTimingResolver(this.#maxPendingTimingSamples)
2153
- this.#timingStreams.set(streamKey, { packetId, resolver })
2191
+ this.#timingStreams.set(streamKey, { packetId, resolver, awaitingTiming: [] })
2154
2192
  return resolver
2155
2193
  }
2156
2194
 
2195
+ #awaitTiming(streamKey: string, head: AwaitingTimingHead): void {
2196
+ const awaitingTiming = this.#timingStreams.get(streamKey)?.awaitingTiming
2197
+ if (!awaitingTiming) return
2198
+ awaitingTiming.push(head)
2199
+ if (awaitingTiming.length > MAX_AWAITING_TIMING_HEADS) awaitingTiming.shift()
2200
+ }
2201
+
2202
+ /**
2203
+ * Feed again the heads whose timing the FT=1 just admitted declares. That
2204
+ * FT=1 can be an FEC recovery landing after its sample's FI=1, and neither
2205
+ * FEC path routes a source symbol that already arrived a second time.
2206
+ */
2207
+ #replayAwaitingTiming(streamKey: string, resolver: IsoBmffTimingResolver): void {
2208
+ const state = this.#timingStreams.get(streamKey)
2209
+ if (!state || state.awaitingTiming.length === 0) return
2210
+ const ready = state.awaitingTiming.filter(head => resolver.sampleTiming(
2211
+ head.mpuSequenceNumber,
2212
+ head.movieFragmentSequenceNumber,
2213
+ head.sampleNumber,
2214
+ ))
2215
+ if (ready.length === 0) return
2216
+ state.awaitingTiming = state.awaitingTiming.filter(head => !ready.includes(head))
2217
+ this.#replayingAwaitingTiming = true
2218
+ try {
2219
+ for (const head of ready) this.#feed(head.packet, head.options, head.parsed)
2220
+ } finally {
2221
+ this.#replayingAwaitingTiming = false
2222
+ }
2223
+ }
2224
+
2157
2225
  #dropTimingStream(streamKey: string): void {
2158
2226
  const state = this.#timingStreams.get(streamKey)
2159
2227
  if (!state) return