@blockcast/mmt-container 2.2.0-main.a88c3a0aa4f3 → 2.2.0-main.aee8d33d2cc8

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 (118) hide show
  1. package/README.md +34 -19
  2. package/dist/block-policy.d.ts +55 -13
  3. package/dist/block-policy.d.ts.map +1 -1
  4. package/dist/block-policy.js +104 -32
  5. package/dist/block-policy.js.map +2 -2
  6. package/dist/browser-runtime.js +2094 -385
  7. package/dist/browser-runtime.js.map +4 -4
  8. package/dist/carp-catalog.js.map +2 -2
  9. package/dist/catalog-fec-params.d.ts +18 -2
  10. package/dist/catalog-fec-params.d.ts.map +1 -1
  11. package/dist/catalog-fec-params.js +29 -1
  12. package/dist/catalog-fec-params.js.map +2 -2
  13. package/dist/catalog-network-source.js.map +2 -2
  14. package/dist/cmaf-block-assembler.js +92 -1
  15. package/dist/cmaf-block-assembler.js.map +2 -2
  16. package/dist/cmcd-producer.d.ts +60 -47
  17. package/dist/cmcd-producer.d.ts.map +1 -1
  18. package/dist/cmcd-producer.js +138 -109
  19. package/dist/cmcd-producer.js.map +2 -2
  20. package/dist/codec-utils.d.ts.map +1 -1
  21. package/dist/codec-utils.js +92 -1
  22. package/dist/codec-utils.js.map +2 -2
  23. package/dist/delivery-catalog.js +26 -16
  24. package/dist/delivery-catalog.js.map +2 -2
  25. package/dist/delta-fallback-config.js +5 -1
  26. package/dist/delta-fallback-config.js.map +3 -3
  27. package/dist/fec-manager.d.ts +114 -15
  28. package/dist/fec-manager.d.ts.map +1 -1
  29. package/dist/fec-manager.js +639 -95
  30. package/dist/fec-manager.js.map +2 -2
  31. package/dist/fec-state.js +5 -1
  32. package/dist/fec-state.js.map +2 -2
  33. package/dist/fec-worker-setup.d.ts +1 -1
  34. package/dist/fec-worker-setup.js +1 -1
  35. package/dist/fec-worker-setup.js.map +2 -2
  36. package/dist/held-layer-admission.js.map +2 -2
  37. package/dist/index.d.ts +6 -5
  38. package/dist/index.d.ts.map +1 -1
  39. package/dist/index.js +12 -2
  40. package/dist/index.js.map +2 -2
  41. package/dist/iso-block-locator.d.ts +48 -13
  42. package/dist/iso-block-locator.d.ts.map +1 -1
  43. package/dist/iso-block-locator.js +97 -25
  44. package/dist/iso-block-locator.js.map +2 -2
  45. package/dist/iso-bmff-timing.d.ts +10 -1
  46. package/dist/iso-bmff-timing.d.ts.map +1 -1
  47. package/dist/iso-bmff-timing.js +36 -2
  48. package/dist/iso-bmff-timing.js.map +2 -2
  49. package/dist/media-router.js.map +1 -1
  50. package/dist/mfu-reassembler.js +12 -19
  51. package/dist/mfu-reassembler.js.map +2 -2
  52. package/dist/mmtp-container-parser.d.ts +106 -12
  53. package/dist/mmtp-container-parser.d.ts.map +1 -1
  54. package/dist/mmtp-container-parser.js +324 -46
  55. package/dist/mmtp-container-parser.js.map +2 -2
  56. package/dist/mmtp-container-parser.no-modules.js +466 -66
  57. package/dist/mmtp-container-parser.no-modules.js.map +4 -4
  58. package/dist/mmtp-decode-pipeline.js +323 -46
  59. package/dist/mmtp-decode-pipeline.js.map +2 -2
  60. package/dist/mmtp-fec-session.d.ts +41 -3
  61. package/dist/mmtp-fec-session.d.ts.map +1 -1
  62. package/dist/mmtp-fec-session.js +118 -13
  63. package/dist/mmtp-fec-session.js.map +3 -3
  64. package/dist/mmtp-router.d.ts +36 -8
  65. package/dist/mmtp-router.d.ts.map +1 -1
  66. package/dist/mmtp-router.js +716 -100
  67. package/dist/mmtp-router.js.map +3 -3
  68. package/dist/multicast-auth-catalog.js.map +2 -2
  69. package/dist/multicast-input.js.map +2 -2
  70. package/dist/multicast-manager.d.ts +28 -3
  71. package/dist/multicast-manager.d.ts.map +1 -1
  72. package/dist/multicast-manager.js +1451 -215
  73. package/dist/multicast-manager.js.map +3 -3
  74. package/dist/multicast-transport.js +28 -18
  75. package/dist/multicast-transport.js.map +2 -2
  76. package/dist/multicast-unwrap.js +12 -19
  77. package/dist/multicast-unwrap.js.map +2 -2
  78. package/dist/no-modules.d.ts +1 -0
  79. package/dist/no-modules.d.ts.map +1 -1
  80. package/dist/no-modules.js +463 -64
  81. package/dist/no-modules.js.map +4 -4
  82. package/dist/repair-fetch.d.ts +38 -8
  83. package/dist/repair-fetch.d.ts.map +1 -1
  84. package/dist/repair-fetch.js +286 -27
  85. package/dist/repair-fetch.js.map +3 -3
  86. package/dist/repair-layer-subscriptions.d.ts +82 -0
  87. package/dist/repair-layer-subscriptions.d.ts.map +1 -0
  88. package/dist/repair-layer-subscriptions.js +192 -0
  89. package/dist/repair-layer-subscriptions.js.map +7 -0
  90. package/dist/signaling.js.map +2 -2
  91. package/dist/timestamp-codec.js +12 -19
  92. package/dist/timestamp-codec.js.map +2 -2
  93. package/dist/track-session.d.ts +1 -1
  94. package/dist/track-session.d.ts.map +1 -1
  95. package/dist/track-session.js +1 -0
  96. package/dist/track-session.js.map +2 -2
  97. package/dist/wasm-mfu-reassembler.js.map +2 -2
  98. package/dist/wasm-mmtp-container-parser.js +323 -46
  99. package/dist/wasm-mmtp-container-parser.js.map +2 -2
  100. package/package.json +8 -5
  101. package/schemas/catalog.schema.json +4 -0
  102. package/src/block-policy.ts +216 -52
  103. package/src/catalog-fec-params.ts +37 -1
  104. package/src/cmcd-producer.ts +189 -179
  105. package/src/codec-utils.ts +121 -1
  106. package/src/fec-manager.ts +619 -37
  107. package/src/fec-worker-setup.ts +2 -2
  108. package/src/index.ts +22 -2
  109. package/src/iso-block-locator.ts +133 -31
  110. package/src/iso-bmff-timing.ts +40 -1
  111. package/src/mmtp-container-parser.ts +353 -32
  112. package/src/mmtp-fec-session.ts +115 -15
  113. package/src/mmtp-router.ts +618 -44
  114. package/src/multicast-manager.ts +139 -16
  115. package/src/no-modules.ts +3 -0
  116. package/src/repair-fetch.ts +366 -50
  117. package/src/repair-layer-subscriptions.ts +261 -0
  118. package/src/track-session.ts +4 -1
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@blockcast/mmt-container",
3
- "version": "2.2.0-main.a88c3a0aa4f3",
3
+ "version": "2.2.0-main.aee8d33d2cc8",
4
4
  "description": "MMTP/LOC container parsing for MMT over MoQ and SSM",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -169,10 +169,10 @@
169
169
  "test:watch": "vitest"
170
170
  },
171
171
  "dependencies": {
172
- "@blockcast/fec-worker": "2.2.0-main.a88c3a0aa4f3",
173
- "@blockcast/mmt-base": "2.2.0-main.a88c3a0aa4f3",
174
- "@blockcast/mmt-fec": "2.2.0-main.a88c3a0aa4f3",
175
- "@blockcast/mmt-manifest-verify": "2.2.0-main.a88c3a0aa4f3",
172
+ "@blockcast/fec-worker": "2.2.0-main.aee8d33d2cc8",
173
+ "@blockcast/mmt-base": "2.2.0-main.aee8d33d2cc8",
174
+ "@blockcast/mmt-fec": "2.2.0-main.aee8d33d2cc8",
175
+ "@blockcast/mmt-manifest-verify": "2.2.0-main.aee8d33d2cc8",
176
176
  "@noble/curves": "^1.9.7",
177
177
  "@noble/hashes": "^1.8.0",
178
178
  "ajv": "^8.17.1",
@@ -180,6 +180,9 @@
180
180
  },
181
181
  "devDependencies": {
182
182
  "@blockcast/mmt-transport": "file:../transport",
183
+ "@svta/cml-cmcd": "^2.8.4",
184
+ "@svta/cml-structured-field-values": "^1.1.7",
185
+ "@svta/cml-utils": "^1.7.0",
183
186
  "esbuild": "^0.28.0",
184
187
  "hash-wasm": "^4.12.0",
185
188
  "typescript": "^5.3.0",
@@ -138,6 +138,10 @@
138
138
  "$ref": "#/$defs/positiveInteger",
139
139
  "description": "Keyframe interval in the track's timescale ticks (moqcast-draft §4.4.3). Exact where keyframeIntervalMs rounds (e.g. 30000/1001 video); when both are present, keyframeIntervalTicks wins."
140
140
  },
141
+ "deliveryMode": {
142
+ "type": "string",
143
+ "description": "How the publisher sends this track's objects (draft-ramadan-moq-fec-catalog-00 §3.3, proposed in moqcast-draft#26): \"datagram\" or \"stream\". A receiver treats a missing or unrecognized value as \"datagram\", so any string is admitted here rather than rejected. Allowed on source, repair and symbol tracks."
144
+ },
141
145
  "fec": { "$ref": "#/$defs/fec" }
142
146
  },
143
147
  "allOf": [
@@ -81,7 +81,17 @@ export type ClassifierReason =
81
81
  | 'rap-not-mpu-start'
82
82
  | 'mpu-longer-than-gop'
83
83
  | 'block-not-contiguous'
84
+ /**
85
+ * MPU-start RAPs are not exactly one keyframe interval apart per MPU:
86
+ * released RAPs in the media timescale, or, once each frame is its own
87
+ * movie fragment, RAP fragments G fragments apart per MPU (P3).
88
+ */
84
89
  | 'rap-spacing-mismatch'
90
+ /**
91
+ * An MPU of several movie fragments whose fragments hold several samples:
92
+ * neither sample_number nor the fragment number states a frame's MPU
93
+ * position (P3).
94
+ */
85
95
  | 'mpu-multiple-fragments'
86
96
  | 'block-wider-than-interleave'
87
97
 
@@ -237,13 +247,18 @@ export interface SourceSymbolEvidence {
237
247
  /** MPU_sequence_number, when parsed from this packet. */
238
248
  readonly mpu?: number
239
249
  /**
240
- * 0-based sample position within the MPU: the MFU DU-header
250
+ * 0-based sample position within its movie fragment: the MFU DU-header
241
251
  * `sample_number` minus one (ISO/IEC 23008-1 numbers samples from 1, and
242
252
  * the parser rejects `sample_number` 0). Present only when this packet
243
- * carried a DU header.
253
+ * carried a DU header. It is the MPU position only while the MPU is one
254
+ * movie fragment; BlockClassifier derives MPU positions (P3).
244
255
  */
245
256
  readonly idx?: number
246
- /** movie_fragment_sequence_number from the DU header, when present. */
257
+ /**
258
+ * movie_fragment_sequence_number from the DU header, when present. When
259
+ * each frame is its own fragment, it positions the frame from its MPU's
260
+ * RAP fragment (P3).
261
+ */
247
262
  readonly movieFragment?: number
248
263
  /**
249
264
  * DU-header `offset`: the data unit's authored byte position within its
@@ -279,8 +294,9 @@ export type ClassifierChange =
279
294
  interface BlockEvidence {
280
295
  rapSeen: boolean
281
296
  mpus: Set<number>
282
- idxMin?: number
283
- idxMax?: number
297
+ /** The lowest and highest MPU positions its DU headers proved (P3). */
298
+ positionMin?: number
299
+ positionMax?: number
284
300
  cls: BlockClass
285
301
  }
286
302
 
@@ -292,14 +308,40 @@ interface DataUnitStart {
292
308
  readonly sbn: number
293
309
  }
294
310
 
295
- /** Authored order of two data-unit starts of one MPU. */
311
+ /**
312
+ * Authored order of two data-unit starts of one MPU. Fragment numbers wrap
313
+ * at 2^32, so they compare in RFC 1982 serial order.
314
+ */
296
315
  function compareDataUnitStarts(
297
316
  a: Omit<DataUnitStart, 'sbn'>,
298
317
  b: Omit<DataUnitStart, 'sbn'>,
299
318
  ): number {
300
- return a.movieFragment - b.movieFragment || a.idx - b.idx || a.offset - b.offset
319
+ return ((a.movieFragment - b.movieFragment) | 0) || a.idx - b.idx || a.offset - b.offset
320
+ }
321
+
322
+ /** One MPU's movie fragments (P3). */
323
+ interface MpuLayout {
324
+ /** Serial-earliest movie_fragment_sequence_number of its DU headers. */
325
+ low: number
326
+ /** The fragment of its first RAP-flagged DU header: position 0. */
327
+ anchor?: number
301
328
  }
302
329
 
330
+ /** What one DU header proves about its MPU (BlockClassifier `#place`). */
331
+ interface Placement {
332
+ /** Its 0-based MPU position, when the wire proves it. */
333
+ readonly position?: number
334
+ /**
335
+ * It shows a RAP inside an MPU, or an MPU longer than G: MPU boundaries
336
+ * no longer bound the stream's RAPs (`rap-not-mpu-start`).
337
+ */
338
+ readonly rapNotMpuStart: boolean
339
+ /** A refuted layout premise. */
340
+ readonly violation?: ClassifierReason
341
+ }
342
+
343
+ const UNPLACED: Placement = { rapNotMpuStart: false }
344
+
303
345
  /**
304
346
  * Per-stream block classifier.
305
347
  *
@@ -322,14 +364,33 @@ function compareDataUnitStarts(
322
364
  * from the previous block (`block-wider-than-interleave`). The producer
323
365
  * counts a media-unit boundary only once the open block holds a symbol.
324
366
  * - P3: an MPU starts with its only RAP and holds at most G frames
325
- * (`rap-not-mpu-start`, `mpu-longer-than-gop`, `rap-spacing-mismatch`), in
326
- * one movie fragment (`mpu-multiple-fragments`).
367
+ * (`rap-not-mpu-start`, `mpu-longer-than-gop`, `rap-spacing-mismatch`),
368
+ * and its DU headers state each frame's MPU position in one of two layouts
369
+ * (`mpu-multiple-fragments` when an MPU shows two fragments and a DU
370
+ * header states sample_number above 1, which is neither):
371
+ * - one movie fragment per MPU (ATSC 3.0): the position is
372
+ * sample_number − 1. This is the rule until some MPU of the sequence
373
+ * shows two fragments.
374
+ * - one fragment per frame, numbered one per frame (FFmpeg's MoQ MMT
375
+ * muxer, every DU header stating sample_number 1): the position is the
376
+ * fragment's distance from the fragment of the MPU's first RAP-flagged
377
+ * DU header (its anchor). The RAP's own fragment is position 0. Each
378
+ * anchor must lie exactly G fragments per MPU from the anchor received
379
+ * before it, which is kept past the horizon, and from the anchor
380
+ * received last (`rap-spacing-mismatch`, as after a dropped frame).
381
+ * Once two anchors kept that spacing, fragment numbers count frames:
382
+ * later fragments get positions, and an anchor arriving past the
383
+ * spacing is a RAP inside its MPU or follows an MPU longer than G
384
+ * (`rap-not-mpu-start`). An MPU whose anchor was not received has no
385
+ * positions; its blocks stay unknown. A second RAP-flagged fragment, or
386
+ * a DU header in a fragment before the anchor, refutes the MPU start
387
+ * (`rap-not-mpu-start`).
327
388
  *
328
389
  * P1 and P2 are what make a block's frames contiguous and lie within
329
- * [idxMax − D + 1, idxMin + D − 1] of its MPU. What refutes P1 is a pair of
330
- * source packets whose wire-stated authored order and SBN order disagree:
331
- * a later MPU in an earlier block, a later data unit of one MPU (by
332
- * movie_fragment_sequence_number, sample_number, then DU offset) in an
390
+ * [positionMax − D + 1, positionMin + D − 1] of its MPU. What refutes P1 is
391
+ * a pair of source packets whose wire-stated authored order and SBN order
392
+ * disagree: a later MPU in an earlier block, a later data unit of one MPU
393
+ * (by movie_fragment_sequence_number, sample_number, then DU offset) in an
333
394
  * earlier block, or one data unit in two blocks. That is the signature of a
334
395
  * round-robin interleave, which scatters one run over several concurrent
335
396
  * blocks. Non-adjacency alone is not: a sample larger than K packets
@@ -357,8 +418,23 @@ export class BlockClassifier {
357
418
  #mpuSbnSpans = new Map<number, { min: number; max: number }>()
358
419
  #dataUnitStarts = new Map<number, DataUnitStart[]>()
359
420
  #highestMpu: number | null = null
360
- /** MPU → movie_fragment_sequence_number observed in it. */
361
- #mpuFragments = new Map<number, number>()
421
+ /**
422
+ * P3 layout, kept for the MPUs within the classification horizon and valid
423
+ * within one continuous SBN sequence (`noteSourceDiscontinuity`).
424
+ */
425
+ #mpus = new Map<number, MpuLayout>()
426
+ /** An MPU of this sequence showed two movie fragments: each frame is one. */
427
+ #fragmentPerFrame = false
428
+ /** A DU header of this sequence stated sample_number above 1. */
429
+ #multiSampleFragment = false
430
+ /**
431
+ * The anchor received last, whether or not its MPU is still in the
432
+ * horizon. Each new anchor, and the anchor of every DU header's MPU, is
433
+ * checked against it.
434
+ */
435
+ #lastAnchor: { mpu: number; fragment: number } | null = null
436
+ /** Two anchors were exactly G fragments per MPU apart. */
437
+ #fragmentsProven = false
362
438
  #lastRap: { mpu: number; presentationTime: bigint; mediaTimescale: number } | null = null
363
439
 
364
440
  constructor(geometry: BlockClassifierGeometry) {
@@ -394,7 +470,7 @@ export class BlockClassifier {
394
470
  this.#geometry = geometry
395
471
  this.#blocks.clear()
396
472
  this.#clearContiguityLedger()
397
- this.#mpuFragments.clear()
473
+ this.#clearMpuLayout()
398
474
  this.#lastRap = null
399
475
  this.#mpuBoundsRaps = true
400
476
  if (geometry.trackKind === 'video' && geometry.mode === 'all-protected') {
@@ -430,11 +506,13 @@ export class BlockClassifier {
430
506
  /**
431
507
  * The stream's SBN sequence restarted (FECManager source discontinuity:
432
508
  * a publisher restart, or the SS_ID space wrapped). Authored order and SBN
433
- * order are comparable only within one continuous sequence, so the P1
434
- * ledger starts again.
509
+ * order are comparable only within one continuous sequence, and a
510
+ * restarted publisher numbers MPUs and movie fragments again, so the P1
511
+ * ledger and the P3 layout start again.
435
512
  */
436
513
  noteSourceDiscontinuity(): void {
437
514
  this.#clearContiguityLedger()
515
+ this.#clearMpuLayout()
438
516
  }
439
517
 
440
518
  /** Register a block that exists without evidence (a vanished block). */
@@ -457,21 +535,23 @@ export class BlockClassifier {
457
535
  if (this.#geometry.trackKind === 'audio') return []
458
536
  const changes: ClassifierChange[] = []
459
537
  const block = this.#blockFor(sbn)
538
+ const placement = this.#place(evidence)
460
539
 
461
540
  if (evidence.rap) block.rapSeen = true
462
541
  if (evidence.mpu !== undefined) block.mpus.add(evidence.mpu)
463
- if (evidence.idx !== undefined) {
464
- block.idxMin = block.idxMin === undefined ? evidence.idx : Math.min(block.idxMin, evidence.idx)
465
- block.idxMax = block.idxMax === undefined ? evidence.idx : Math.max(block.idxMax, evidence.idx)
542
+ const { position } = placement
543
+ if (position !== undefined) {
544
+ block.positionMin =
545
+ block.positionMin === undefined ? position : Math.min(block.positionMin, position)
546
+ block.positionMax =
547
+ block.positionMax === undefined ? position : Math.max(block.positionMax, position)
466
548
  }
467
549
 
468
550
  // A RAP inside an MPU refutes "an MPU boundary bounds RAPs" whatever the
469
551
  // classifier mode, because gap decisions rely on it in every mode.
470
- if (evidence.rap && evidence.idx !== undefined && evidence.idx !== 0) {
471
- this.#mpuBoundsRaps = false
472
- }
552
+ if (placement.rapNotMpuStart) this.#mpuBoundsRaps = false
473
553
 
474
- const violation = this.#checkInvariants(sbn, block, evidence)
554
+ const violation = this.#checkInvariants(sbn, block, evidence, placement)
475
555
  if (violation !== null) changes.push(...this.#disable(violation))
476
556
 
477
557
  const next = this.#classify(block)
@@ -549,14 +629,13 @@ export class BlockClassifier {
549
629
  sbn: number,
550
630
  block: BlockEvidence,
551
631
  evidence: SourceSymbolEvidence,
632
+ placement: Placement,
552
633
  ): ClassifierReason | null {
553
634
  if (this.#mode === 'all-protected' || this.#geometry.trackKind !== 'video') return null
554
635
  const geometry = this.#geometry
555
636
  if (geometry.mode !== 'classifying') return null
556
- if (evidence.rap && evidence.idx !== undefined && evidence.idx !== 0) {
557
- return 'rap-not-mpu-start'
558
- }
559
- if (evidence.idx !== undefined && evidence.idx >= geometry.gopFrames) {
637
+ if (placement.rapNotMpuStart) return 'rap-not-mpu-start'
638
+ if (placement.position !== undefined && placement.position >= geometry.gopFrames) {
560
639
  return 'mpu-longer-than-gop'
561
640
  }
562
641
  // A block closes at the latest after D media units, so the DU headers it
@@ -565,32 +644,109 @@ export class BlockClassifier {
565
644
  // round-robin across concurrent blocks) and the window rule is void.
566
645
  if (
567
646
  block.mpus.size === 1 &&
568
- block.idxMin !== undefined &&
569
- block.idxMax !== undefined &&
570
- block.idxMax - block.idxMin >= geometry.interleaveUnits
647
+ block.positionMin !== undefined &&
648
+ block.positionMax !== undefined &&
649
+ block.positionMax - block.positionMin >= geometry.interleaveUnits
571
650
  ) {
572
651
  return 'block-wider-than-interleave'
573
652
  }
574
- if (evidence.mpu !== undefined && evidence.movieFragment !== undefined) {
575
- const fragment = this.#mpuFragments.get(evidence.mpu)
576
- if (fragment === undefined) {
577
- this.#mpuFragments.set(evidence.mpu, evidence.movieFragment)
578
- // One entry per MPU still inside the classification horizon.
579
- for (const mpu of this.#mpuFragments.keys()) {
580
- if (mpu + 1 < evidence.mpu) this.#mpuFragments.delete(mpu)
581
- }
582
- } else if (fragment !== evidence.movieFragment) {
583
- // sample_number is then a position within a movie fragment, not
584
- // within the MPU, and the window rule's geometry does not hold.
585
- return 'mpu-multiple-fragments'
586
- }
587
- }
653
+ if (placement.violation !== undefined) return placement.violation
588
654
  if (evidence.mpu !== undefined && this.#contradictsAuthoredOrder(sbn, evidence.mpu, evidence)) {
589
655
  return 'block-not-contiguous'
590
656
  }
591
657
  return null
592
658
  }
593
659
 
660
+ /**
661
+ * The MPU position a source symbol's DU header proves (P3), and what it
662
+ * refutes. Runs in every mode: mpuBoundsRaps needs its RAP verdict.
663
+ */
664
+ #place(evidence: SourceSymbolEvidence): Placement {
665
+ const { mpu, idx, movieFragment: fragment } = evidence
666
+ if (idx === undefined) return UNPLACED
667
+ const bySample: Placement = { position: idx, rapNotMpuStart: evidence.rap && idx !== 0 }
668
+ // Every wire DU header states both; evidence without them keeps the
669
+ // one-fragment rule unless the sequence is fragment-per-frame.
670
+ if (mpu === undefined || fragment === undefined) {
671
+ return this.#fragmentPerFrame && !this.#multiSampleFragment
672
+ ? { rapNotMpuStart: bySample.rapNotMpuStart }
673
+ : bySample
674
+ }
675
+ let layout = this.#mpus.get(mpu)
676
+ if (layout === undefined) {
677
+ layout = { low: fragment }
678
+ this.#mpus.set(mpu, layout)
679
+ // One entry per MPU still inside the classification horizon.
680
+ for (const other of this.#mpus.keys()) {
681
+ if (other + 1 < mpu) this.#mpus.delete(other)
682
+ }
683
+ } else if (fragment !== layout.low) {
684
+ this.#fragmentPerFrame = true
685
+ if (((fragment - layout.low) | 0) < 0) layout.low = fragment
686
+ }
687
+ if (idx !== 0) this.#multiSampleFragment = true
688
+ let arrival: number | undefined
689
+ if (evidence.rap && layout.anchor === undefined) {
690
+ layout.anchor = fragment
691
+ arrival = this.#anchorDrift(mpu, fragment)
692
+ // Proof takes two MPUs: an MPU's RAP header seen again proves nothing.
693
+ if (arrival === 0 && this.#lastAnchor?.mpu !== mpu) this.#fragmentsProven = true
694
+ this.#lastAnchor = { mpu, fragment }
695
+ }
696
+ // Until an MPU shows two fragments, sample_number − 1 is the position.
697
+ // Once one has, a sample_number above 1 leaves neither layout.
698
+ if (!this.#fragmentPerFrame) return bySample
699
+ if (this.#multiSampleFragment) return { ...bySample, violation: 'mpu-multiple-fragments' }
700
+ const { anchor } = layout
701
+ if (anchor === undefined) return UNPLACED
702
+ // A new anchor against the anchor received before it; any other DU
703
+ // header's anchor against the anchor received last, so an anchor that
704
+ // arrived before the layout was known is held to the spacing too.
705
+ const drift = arrival ?? this.#anchorDrift(mpu, anchor)
706
+ // RFC 1982 distance from the anchor: positive for a later fragment.
707
+ const offset = (fragment - anchor) | 0
708
+ return {
709
+ ...(offset === 0 || (offset > 0 && this.#fragmentsProven) ? { position: offset } : {}),
710
+ rapNotMpuStart:
711
+ (evidence.rap && offset !== 0) ||
712
+ ((layout.low - anchor) | 0) < 0 ||
713
+ // Fragments count frames: an anchor arriving past the spacing is a
714
+ // RAP inside its MPU, or an earlier MPU outran G.
715
+ (arrival !== undefined && arrival > 0 && this.#fragmentsProven),
716
+ ...(drift === undefined || drift === 0 ? {} : { violation: 'rap-spacing-mismatch' }),
717
+ }
718
+ }
719
+
720
+ /**
721
+ * How many fragments MPU m's anchor `fragment` lies past the start that
722
+ * the anchor received last predicts for MPU m: one frame per fragment and
723
+ * G frames per MPU put the anchors of MPUs m and m' exactly G * (m - m')
724
+ * fragments apart, modulo 2^32. Undefined without a last anchor, a
725
+ * cadence, or this layout.
726
+ */
727
+ #anchorDrift(mpu: number, fragment: number): number | undefined {
728
+ const geometry = this.#geometry
729
+ const last = this.#lastAnchor
730
+ if (
731
+ last === null ||
732
+ !this.#fragmentPerFrame ||
733
+ this.#multiSampleFragment ||
734
+ geometry.trackKind !== 'video' ||
735
+ geometry.mode !== 'classifying'
736
+ ) {
737
+ return undefined
738
+ }
739
+ return (fragment - last.fragment - geometry.gopFrames * (mpu - last.mpu)) | 0
740
+ }
741
+
742
+ #clearMpuLayout(): void {
743
+ this.#mpus.clear()
744
+ this.#fragmentPerFrame = false
745
+ this.#multiSampleFragment = false
746
+ this.#lastAnchor = null
747
+ this.#fragmentsProven = false
748
+ }
749
+
594
750
  #clearContiguityLedger(): void {
595
751
  this.#mpuSbnSpans.clear()
596
752
  this.#dataUnitStarts.clear()
@@ -698,14 +854,17 @@ export class BlockClassifier {
698
854
  if (geometry.trackKind !== 'video' || geometry.mode !== 'classifying') return 'unknown'
699
855
  if (
700
856
  block.mpus.size === 1 &&
701
- block.idxMin !== undefined &&
702
- block.idxMax !== undefined
857
+ block.positionMin !== undefined &&
858
+ block.positionMax !== undefined
703
859
  ) {
704
860
  // A block closes every D media units, so its frames are contiguous and
705
- // lie in [idxMax − D + 1, idxMin + D − 1] of MPU m. Index 0 is the only
706
- // RAP of an MPU and G bounds its length.
861
+ // lie in [positionMax − D + 1, positionMin + D − 1] of MPU m. Position
862
+ // 0 is the only RAP of an MPU and G bounds its length.
707
863
  const span = geometry.interleaveUnits - 1
708
- if (block.idxMax - span >= 1 && block.idxMin + span <= geometry.gopFrames - 1) {
864
+ if (
865
+ block.positionMax - span >= 1 &&
866
+ block.positionMin + span <= geometry.gopFrames - 1
867
+ ) {
709
868
  return 'delta'
710
869
  }
711
870
  }
@@ -746,7 +905,12 @@ export interface GapFrameEvidence {
746
905
  * same-MPU then no longer proves a gap lost only deltas.
747
906
  */
748
907
  readonly mpu?: number
749
- /** 0-based sample position within the MPU, when the geometry verifies it. */
908
+ /**
909
+ * 0-based sample position within its movie fragment (sample_number − 1).
910
+ * State it on both sides only when both frames are in one movie fragment:
911
+ * decideGap reads `after.idx − before.idx − 1` as the samples missed
912
+ * between them.
913
+ */
750
914
  readonly idx?: number
751
915
  /** Authored presentation time in `timescale` ticks. */
752
916
  readonly pts?: bigint
@@ -30,12 +30,14 @@ import type {
30
30
  RepairOverlayParams,
31
31
  } from "./repair-layers.js";
32
32
  import type { RepairTarget } from "./repair-fetch.js";
33
+ import type { MoqRepairDelivery } from "./repair-layer-subscriptions.js";
33
34
 
34
35
  export { catalogFecInterleaveMs, type FecInterleaveCatalog };
35
36
  export { FEC_BLOCK_PROFILES, isFecBlockProfile, parseFecBlockProfile, type FecBlockProfile };
36
37
  // Re-exported here, not from index.ts: index's bundle keeps `./*.js` external,
37
38
  // so a direct mmt-base re-export there leaves a dangling `./mmtp.js` import.
38
39
  export {
40
+ runtimeFecCatalogReport,
39
41
  validateFecCatalog,
40
42
  type FecCatalogCode,
41
43
  type FecCatalogInstance,
@@ -273,6 +275,9 @@ export type TrackFecParams = {
273
275
  /** Keyframe overlay instances declared for this track. Present (possibly
274
276
  * empty) whenever repairLayers is. */
275
277
  repairOverlays?: readonly RepairOverlayParams[];
278
+ /** The publisher's MoQ delivery mode (`catalogTrackDeliveryMode`).
279
+ * Present whenever repairLayers is; `RepairLayerSubscriptions` reads it. */
280
+ deliveryMode?: MoqRepairDelivery;
276
281
  fps?: number;
277
282
  sampleRate?: number;
278
283
  };
@@ -293,7 +298,7 @@ const NO_FEC: TrackFecParams = {
293
298
  * A keyframe overlay is a separate FEC instance with its own K, its own
294
299
  * RSB_length and its own RS_ID space, so it is never added here however it was
295
300
  * joined. Counting it would overstate the base instance's P and misreport
296
- * CMCD `x-bc-fec-p`.
301
+ * CMCD `com.blockcast-fp`.
297
302
  *
298
303
  * Falls back to P_0 when the track declares no layers, which is the
299
304
  * single-layer identity rather than a default: with one layer, P_0 is the
@@ -368,6 +373,36 @@ export function resolveRepairLayers(
368
373
  };
369
374
  }
370
375
 
376
+ /** The catalog value a JSON Pointer (RFC 6901) names, or undefined. */
377
+ function valueAt(catalog: unknown, pointer: string): unknown {
378
+ let node = catalog;
379
+ for (const segment of pointer.split("/").slice(1)) {
380
+ if (typeof node !== "object" || node === null) return undefined;
381
+ node = (node as Record<string, unknown>)[segment.replace(/~1/g, "/").replace(/~0/g, "~")];
382
+ }
383
+ return node;
384
+ }
385
+
386
+ /**
387
+ * The publisher's MoQ delivery mode for one source track: `deliveryMode` on
388
+ * its base track entry (the hang rendition or MSF `tracks[]` entry its FEC
389
+ * binds to). `stream` means QUIC retransmits, so the receiver subscribes no
390
+ * repair track. Absent or any other value is `datagram`, FFmpeg's
391
+ * `moq_delivery_mode` default: a receiver never silently drops repair on a
392
+ * lossy path. When a track is declared both as a hang rendition and as an
393
+ * MSF `tracks[]` entry, the rendition is authoritative: a `deliveryMode` on
394
+ * the shadowed `tracks[]` entry is not read (fails safe to `datagram`).
395
+ * Never throws.
396
+ */
397
+ export function catalogTrackDeliveryMode(
398
+ catalog: RepairLayerCatalog | undefined,
399
+ source: string | CatalogTrackRef,
400
+ ): MoqRepairDelivery {
401
+ const binding = bindingOf(catalog, source);
402
+ const entry = binding ? valueAt(catalog, binding.pointer) : undefined;
403
+ return (entry as { deliveryMode?: unknown } | undefined)?.deliveryMode === "stream" ? "stream" : "datagram";
404
+ }
405
+
371
406
  /** A MoQ Transport priority (0..255, lower first), or null. */
372
407
  function moqtPriority(value: unknown): number | null {
373
408
  return Number.isSafeInteger(value) && (value as number) >= 0 && (value as number) <= 255 ? (value as number) : null;
@@ -444,6 +479,7 @@ function fecParams(
444
479
  repairTrackName: base.repairTrack,
445
480
  repairLayers: repair.baseLayers,
446
481
  repairOverlays: repair.overlays,
482
+ deliveryMode: catalogTrackDeliveryMode(catalog, track!),
447
483
  ...mediaRate,
448
484
  };
449
485
  }