@blockcast/mmt-container 0.4.0-main.d6f2737828fc → 0.4.0

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 (102) hide show
  1. package/README.md +12 -9
  2. package/dist/browser-runtime.js +14219 -13716
  3. package/dist/browser-runtime.js.map +4 -4
  4. package/dist/carp-catalog.d.ts +1 -12
  5. package/dist/carp-catalog.d.ts.map +1 -1
  6. package/dist/carp-catalog.js +14 -6
  7. package/dist/carp-catalog.js.map +4 -4
  8. package/dist/catalog-fec-params.d.ts +5 -2
  9. package/dist/catalog-fec-params.d.ts.map +1 -1
  10. package/dist/catalog-fec-params.js +101 -42
  11. package/dist/catalog-fec-params.js.map +4 -4
  12. package/dist/catalog-network-source.js +4 -6
  13. package/dist/catalog-network-source.js.map +4 -4
  14. package/dist/cmcd-producer.d.ts.map +1 -1
  15. package/dist/cmcd-producer.js +0 -4
  16. package/dist/cmcd-producer.js.map +2 -2
  17. package/dist/delivery-catalog.d.ts +124 -0
  18. package/dist/delivery-catalog.d.ts.map +1 -0
  19. package/dist/delivery-catalog.js +1867 -0
  20. package/dist/delivery-catalog.js.map +7 -0
  21. package/dist/fec-manager.d.ts +17 -4
  22. package/dist/fec-manager.d.ts.map +1 -1
  23. package/dist/fec-manager.js +41 -20
  24. package/dist/fec-manager.js.map +4 -4
  25. package/dist/fec-state.d.ts +3 -2
  26. package/dist/fec-state.d.ts.map +1 -1
  27. package/dist/fec-state.js +17 -15
  28. package/dist/fec-state.js.map +4 -4
  29. package/dist/fec-worker-setup.d.ts +2 -8
  30. package/dist/fec-worker-setup.d.ts.map +1 -1
  31. package/dist/fec-worker-setup.js +12 -62
  32. package/dist/fec-worker-setup.js.map +4 -4
  33. package/dist/index.d.ts +2 -1
  34. package/dist/index.d.ts.map +1 -1
  35. package/dist/index.js +10 -0
  36. package/dist/index.js.map +2 -2
  37. package/dist/iso-block-locator.js +4 -6
  38. package/dist/iso-block-locator.js.map +4 -4
  39. package/dist/mfu-reassembler.js +27 -29
  40. package/dist/mfu-reassembler.js.map +4 -4
  41. package/dist/mmtp-container-parser.d.ts +9 -11
  42. package/dist/mmtp-container-parser.d.ts.map +1 -1
  43. package/dist/mmtp-container-parser.js +107 -98
  44. package/dist/mmtp-container-parser.js.map +4 -4
  45. package/dist/mmtp-container-parser.no-modules.js +133 -108
  46. package/dist/mmtp-container-parser.no-modules.js.map +4 -4
  47. package/dist/mmtp-decode-pipeline.js +107 -98
  48. package/dist/mmtp-decode-pipeline.js.map +4 -4
  49. package/dist/mmtp-router.d.ts +34 -14
  50. package/dist/mmtp-router.d.ts.map +1 -1
  51. package/dist/mmtp-router.js +143 -113
  52. package/dist/mmtp-router.js.map +4 -4
  53. package/dist/multicast-input.js +4 -6
  54. package/dist/multicast-input.js.map +4 -4
  55. package/dist/multicast-manager.d.ts +39 -3
  56. package/dist/multicast-manager.d.ts.map +1 -1
  57. package/dist/multicast-manager.js +264 -156
  58. package/dist/multicast-manager.js.map +4 -4
  59. package/dist/multicast-transport.js +14 -14
  60. package/dist/multicast-transport.js.map +4 -4
  61. package/dist/multicast-unwrap.js +32 -34
  62. package/dist/multicast-unwrap.js.map +4 -4
  63. package/dist/no-modules.js +130 -107
  64. package/dist/no-modules.js.map +4 -4
  65. package/dist/repair-fetch.d.ts +86 -2
  66. package/dist/repair-fetch.d.ts.map +1 -1
  67. package/dist/repair-fetch.js +86 -17
  68. package/dist/repair-fetch.js.map +4 -4
  69. package/dist/repair-layers.js +4 -6
  70. package/dist/repair-layers.js.map +4 -4
  71. package/dist/signaling.js +5 -7
  72. package/dist/signaling.js.map +4 -4
  73. package/dist/timestamp-codec.js +27 -29
  74. package/dist/timestamp-codec.js.map +4 -4
  75. package/dist/wasm-mfu-reassembler.js +4 -6
  76. package/dist/wasm-mfu-reassembler.js.map +4 -4
  77. package/dist/wasm-mmtp-container-parser.js +107 -98
  78. package/dist/wasm-mmtp-container-parser.js.map +4 -4
  79. package/package.json +5 -6
  80. package/src/carp-catalog.ts +16 -11
  81. package/src/catalog-fec-params.ts +38 -17
  82. package/src/cmcd-producer.ts +0 -6
  83. package/src/delivery-catalog.ts +498 -0
  84. package/src/fec-manager.ts +44 -17
  85. package/src/fec-state.ts +4 -9
  86. package/src/fec-worker-setup.ts +4 -35
  87. package/src/index.ts +27 -0
  88. package/src/mmtp-container-parser.ts +80 -20
  89. package/src/mmtp-router.ts +71 -29
  90. package/src/multicast-manager.ts +93 -26
  91. package/src/repair-fetch.ts +228 -17
  92. package/test-vectors/catalog/README.md +10 -1
  93. package/test-vectors/catalog/negative/multicast-endpoint-relay-address.json +39 -0
  94. package/test-vectors/catalog/negative/raptorq-unaligned-symbol.json +1 -0
  95. package/test-vectors/catalog/positive/multicast-amt-manual-relay.json +38 -0
  96. package/test-vectors/catalog/section-codes.json +33 -0
  97. package/test-vectors/delivery-catalog/README.md +11 -0
  98. package/test-vectors/delivery-catalog/negative/missing-block-profile.json +58 -0
  99. package/test-vectors/delivery-catalog/negative/packet-id-signaling.json +59 -0
  100. package/test-vectors/delivery-catalog/negative/retired-k-padded-block-profile.json +59 -0
  101. package/test-vectors/delivery-catalog/positive/dash.json +56 -0
  102. package/test-vectors/delivery-catalog/positive/mmtp-source-only.json +43 -0
@@ -89,11 +89,9 @@ export interface MulticastEndpointConfig {
89
89
  port: number;
90
90
  /** Explicit gateway transport policy. Omitted means gateway-selected auto mode. */
91
91
  mode?: 'auto' | 'native' | 'tunnel';
92
- /** AMT relay hostname/IP from catalog endpoint. */
93
- relayAddress?: string;
94
92
  /** AMT relay port from catalog endpoint. */
95
93
  relayPort?: number;
96
- /** AMT relay hostname/IP from player/UI overrides. Wins over `relayAddress`. */
94
+ /** AMT relay hostname/IP: the player/UI override, or the catalog's `multicast.networkSource` AMT relay. */
97
95
  amtRelay?: string;
98
96
  /** AMT relay port from player/UI overrides. Wins over `relayPort`. */
99
97
  amtRelayPort?: number;
@@ -420,6 +418,33 @@ export interface BlockPolicyStats {
420
418
  deltaRepairsDeferred: number;
421
419
  /** Repair requests sent no FETCH (see the `repair-skipped` event). */
422
420
  repairsSkipped: number;
421
+ /** `repairsSkipped` by reason: the reliable path offered no RTT. */
422
+ repairsSkippedRttUnmeasured: number;
423
+ /** `repairsSkipped` by reason: the RTT did not fit the time left to the deadline. */
424
+ repairsSkippedRttExceedsBudget: number;
425
+ /** `repairsSkipped` by reason: the player gave no presentation delay. */
426
+ repairsSkippedPresentationDelayUnavailable: number;
427
+ /**
428
+ * The most recent skip: its reason, the measured RTT (null when
429
+ * unmeasured), the time that was left to the deadline, and the request's
430
+ * repair priority (0 keyframe/unknown, 1 audio, 2 delta; null when the
431
+ * scheduler did not say). Null until a request is skipped.
432
+ */
433
+ lastRepairSkip: {
434
+ readonly reason: RepairSkipReason;
435
+ readonly rttMs: number | null;
436
+ readonly budgetMs: number;
437
+ readonly priority: RepairPriority | null;
438
+ } | null;
439
+ /** Repair FETCHes sent for keyframe/unknown (P0) blocks, every attempt counted. */
440
+ keyframeRepairFetchesSent: number;
441
+ /**
442
+ * Requests the keyframe late-repair budget extended past the presentation
443
+ * deadline (each one can hold the stream behind its block that long).
444
+ */
445
+ keyframeBudgetHolds: number;
446
+ /** Sum of those extensions in ms: the most video the budget could hold. */
447
+ keyframeBudgetHoldMs: number;
423
448
  deltaBlocksFailed: number;
424
449
  audioBlocksRecoveredInband: number;
425
450
  audioBlocksRecoveredQuic: number;
@@ -454,6 +479,13 @@ function newBlockPolicyStats(
454
479
  deltaRepairsPreempted: 0,
455
480
  deltaRepairsDeferred: 0,
456
481
  repairsSkipped: 0,
482
+ repairsSkippedRttUnmeasured: 0,
483
+ repairsSkippedRttExceedsBudget: 0,
484
+ repairsSkippedPresentationDelayUnavailable: 0,
485
+ lastRepairSkip: null,
486
+ keyframeRepairFetchesSent: 0,
487
+ keyframeBudgetHolds: 0,
488
+ keyframeBudgetHoldMs: 0,
457
489
  deltaBlocksFailed: 0,
458
490
  audioBlocksRecoveredInband: 0,
459
491
  audioBlocksRecoveredQuic: 0,
@@ -935,7 +967,10 @@ function projectFecDiagnosticBlock(block: FecBlockSnapshot) {
935
967
  lastSymbolTs: block.lastSymbolTs,
936
968
  ...(block.completedTs === undefined ? {} : { completedTs: block.completedTs }),
937
969
  deadlineTs: block.deadlineTs,
938
- altaVerified: block.altaVerified,
970
+ // ponytail: the per-packet authenticator is gone, so this is always 0. It
971
+ // stays only because pim-multicast-gateway's normalizeSubscriptionDiagnostics
972
+ // still requires a number here; drop it once that validator stops asking.
973
+ altaVerified: 0,
939
974
  relayBlockSigValid: block.relayBlockSigValid,
940
975
  };
941
976
  }
@@ -966,7 +1001,6 @@ function projectFecDiagnosticConfig(config: FecTrackConfig) {
966
1001
  interleaveMs: config.interleaveMs,
967
1002
  fecMode: config.fecMode,
968
1003
  ...(config.repairTrack === undefined ? {} : { repairTrack: config.repairTrack }),
969
- altaEnabled: config.altaEnabled,
970
1004
  relayBlockSigEnabled: config.relayBlockSigEnabled,
971
1005
  };
972
1006
  }
@@ -1912,21 +1946,11 @@ export class MulticastManager {
1912
1946
  ),
1913
1947
  ),
1914
1948
  };
1915
- // Precedence between two sources, not a tolerant alias. BOTH operands can
1916
- // carry a catalog-declared relay, so neither one is "the" inlet:
1917
- // `amtRelay` — the player/UI override, AND the post-migration catalog
1918
- // path: `endpointToMulticastConfig` (packages/transport
1919
- // multicast-catalog-policy.ts) resolves
1920
- // `multicast.networkSource[{type:"amt"}]` into it
1921
- // through `findAmtRelay`.
1922
- // `relayAddress` — the pre-migration per-endpoint relay, retained so a
1923
- // catalog published before BLO-37834 still joins.
1924
- // The override/networkSource value wins, else the legacy endpoint value.
1925
- // `??`, not `||`, so precedence is deliberately NOT trim-aware: a player
1926
- // clearing its override field means "no AMT", not "fall back to the legacy
1927
- // relay". Blank is absent for the port decision below but present here.
1928
- // Retiring `relayAddress` is the legacy cut (BLO-38068), not this branch.
1929
- const amtRelay = config.amtRelay ?? config.relayAddress;
1949
+ // The single relay inlet: the player/UI override, or the catalog's
1950
+ // `multicast.networkSource[{type:"amt"}]` relay, which
1951
+ // `endpointToMulticastConfig` (packages/transport
1952
+ // multicast-catalog-policy.ts) resolves into `amtRelay` via `findAmtRelay`.
1953
+ const amtRelay = config.amtRelay;
1930
1954
  const transportConfig = {
1931
1955
  sourceAddress: config.sourceAddress,
1932
1956
  groupAddress: config.groupAddress,
@@ -1939,9 +1963,8 @@ export class MulticastManager {
1939
1963
  // endpoint-config.ts) deliberately has no implicit relay port — it throws
1940
1964
  // and `MulticastTransport.join` resolves false — so the default is
1941
1965
  // resolved HERE and arrives there explicit, exactly as the two catalog
1942
- // readers do it. This is the last of the three readers: the `relayAddress`
1943
- // half cannot supply a port at all (the pre-migration wire shape has no
1944
- // port field), and `resolveNetworkSource` emits the same port-less shape.
1966
+ // readers do it. This is the last of the three readers: `resolveNetworkSource`
1967
+ // emits a port-less relay shape.
1945
1968
  // Keyed on the resolved relay, so a DRIAD entry — which pins no relay —
1946
1969
  // never has a port manufactured for it. Blank counts as no relay, the
1947
1970
  // same test `catalogAmtNetworkSource` applies (it trims, then skips) and
@@ -2682,12 +2705,22 @@ export class MulticastManager {
2682
2705
  /** Record a repair request the scheduler sent no FETCH for. */
2683
2706
  noteRepairSkipped(
2684
2707
  reason: RepairSkipReason,
2685
- measurement: { readonly rttMs: number | null; readonly budgetMs: number },
2708
+ measurement: { readonly rttMs: number | null; readonly budgetMs: number; readonly priority?: RepairPriority },
2686
2709
  sbn: number,
2687
2710
  packetId: number,
2688
2711
  catalogTrackName?: string,
2689
2712
  ): void {
2690
- this.#statsFor(packetId, catalogTrackName).repairsSkipped++;
2713
+ const stats = this.#statsFor(packetId, catalogTrackName);
2714
+ stats.repairsSkipped++;
2715
+ if (reason === "rtt-unmeasured") stats.repairsSkippedRttUnmeasured++;
2716
+ else if (reason === "rtt-exceeds-budget") stats.repairsSkippedRttExceedsBudget++;
2717
+ else stats.repairsSkippedPresentationDelayUnavailable++;
2718
+ stats.lastRepairSkip = {
2719
+ reason,
2720
+ rttMs: measurement.rttMs,
2721
+ budgetMs: measurement.budgetMs,
2722
+ priority: measurement.priority ?? null,
2723
+ };
2691
2724
  this.#emitRepairLifecycle({
2692
2725
  version: 2,
2693
2726
  type: "repair-skipped",
@@ -2698,6 +2731,37 @@ export class MulticastManager {
2698
2731
  });
2699
2732
  }
2700
2733
 
2734
+ /**
2735
+ * Arrival time of the earliest frame the in-order release gate holds for
2736
+ * the stream: the gate releases it as a discontinuity once
2737
+ * `terminalRetentionMsForPacket` has passed since then.
2738
+ */
2739
+ earliestGatedFrameAtMs(packetId: number, catalogTrackName?: string): number | undefined {
2740
+ return this.#router.fecReleaseEarliestHeldAtMs(packetId, catalogTrackName);
2741
+ }
2742
+
2743
+ /** Record a keyframe late-repair budget the scheduler granted a block. */
2744
+ noteKeyframeBudgetGranted(
2745
+ extensionMs: number,
2746
+ _sbn: number,
2747
+ packetId: number,
2748
+ catalogTrackName?: string,
2749
+ ): void {
2750
+ const stats = this.#statsFor(packetId, catalogTrackName);
2751
+ stats.keyframeBudgetHolds++;
2752
+ stats.keyframeBudgetHoldMs += extensionMs;
2753
+ }
2754
+
2755
+ /** Record a repair FETCH the scheduler admitted and sent. */
2756
+ noteRepairFetchSent(
2757
+ priority: RepairPriority,
2758
+ _sbn: number,
2759
+ packetId: number,
2760
+ catalogTrackName?: string,
2761
+ ): void {
2762
+ if (priority === 0) this.#statsFor(packetId, catalogTrackName).keyframeRepairFetchesSent++;
2763
+ }
2764
+
2701
2765
  /**
2702
2766
  * Record a repair-scheduler decision about a delta block: preempted for
2703
2767
  * keyframe/audio work, or never dispatched before its block resolved.
@@ -2732,7 +2796,10 @@ export class MulticastManager {
2732
2796
  const out: Record<string, BlockPolicyStats> = {};
2733
2797
  for (const [key, stats] of this.#blockPolicyStats) {
2734
2798
  if (stats.epoch !== this.#fec.epochFor(stats.packetId)) continue;
2735
- out[key] = { ...stats };
2799
+ out[key] = {
2800
+ ...stats,
2801
+ lastRepairSkip: stats.lastRepairSkip === null ? null : { ...stats.lastRepairSkip },
2802
+ };
2736
2803
  }
2737
2804
  return out;
2738
2805
  }
@@ -57,6 +57,29 @@
57
57
  * than sent and cancelled -- and an admitted FETCH holds the block open to
58
58
  * that deadline, so the multicast span elapsing never aborts it.
59
59
  *
60
+ * Keyframe late-repair budget: a block that may hold a RAP may be given a
61
+ * longer deadline than the playout budget (`keyframeRepairBudgetMs`). Losing
62
+ * such a block costs every frame up to the next keyframe, so holding it open
63
+ * for a FETCH that lands within the budget trades a short hold for a GOP-long
64
+ * freeze. At a small playout budget (a low-latency target) the presentation
65
+ * deadline alone leaves less than one round trip by the time the multicast
66
+ * wait gives up on the block, so every keyframe request would be refused
67
+ * (`rtt-exceeds-budget`). Eligible (`keyframeBudgetEligible`): a `keyframe`
68
+ * block (in-band RAP evidence), and an `unknown` block only while its
69
+ * stream's classifier is `classifying`, where `unknown` means the window rule
70
+ * could not exclude a RAP. Under `all-protected` every block reads `unknown`
71
+ * whatever it holds, so there only RAP evidence earns the budget. Never
72
+ * audio (P1) or delta (P2), and never a block its repair group cannot
73
+ * complete (received sources + RSB_length < K'). The hold ends when its
74
+ * FETCH ends without making the block decodable, not at the budget.
75
+ *
76
+ * Every deadline is capped by the release gate's hold ceiling measured the
77
+ * way the gate measures it: from the earliest frame it holds for the stream
78
+ * (a surviving frame of the block itself is routed before a repair symbol
79
+ * states the block, so before its anchor), never later than from the anchor.
80
+ * A FETCH landing after the gate forced that frame out would recover media
81
+ * the gate then drops as stale.
82
+ *
60
83
  * The scheduler arms no timers. A fetch ends when its transport completes or
61
84
  * throws, when it is preempted, when any later scheduling event finds it past
62
85
  * its presentation deadline, when its block resolves (recovered, or failed at
@@ -64,7 +87,12 @@
64
87
  */
65
88
 
66
89
  import { parseFecBlockProfile, type FecBlockProfile } from '@blockcast/mmt-base'
67
- import { repairPriorityForClass, type BlockClass, type RepairPriority } from './block-policy.js'
90
+ import {
91
+ repairPriorityForClass,
92
+ type BlockClass,
93
+ type ClassifierMode,
94
+ type RepairPriority,
95
+ } from './block-policy.js'
68
96
  import type { RepairLifecycleObserver } from './multicast-manager.js'
69
97
  import { REPAIR_PRIORITY_MIN } from './repair-layers.js'
70
98
 
@@ -235,6 +263,30 @@ export function repairFetchSubscriberPriority(
235
263
  return { kind: 'carry', subscriberPriority: Math.max(repair, REPAIR_PRIORITY_MIN) }
236
264
  }
237
265
 
266
+ /** What `RepairFetchHost.fecManager.blockSymbolCounts` reports for one open block. */
267
+ export interface BlockSymbolCounts {
268
+ readonly sources: number
269
+ readonly repairs: number
270
+ readonly authoredLength: number | undefined
271
+ readonly repairGroupLength: number | undefined
272
+ }
273
+
274
+ /**
275
+ * Whether a block of `blockClass` may get the keyframe late-repair budget:
276
+ * a `keyframe` block always (in-band RAP evidence), an `unknown` block only
277
+ * while its stream's classifier is `classifying` (the window rule could not
278
+ * exclude a RAP). An `all-protected` stream reads `unknown` for every block,
279
+ * delta-only ones included, so there `unknown` is no evidence of a RAP and
280
+ * earns no hold. Audio and delta never do.
281
+ */
282
+ export function keyframeBudgetEligible(
283
+ blockClass: BlockClass,
284
+ classifierMode: ClassifierMode | undefined,
285
+ ): boolean {
286
+ if (blockClass === 'keyframe') return true
287
+ return blockClass === 'unknown' && classifierMode === 'classifying'
288
+ }
289
+
238
290
  export interface WireRepairFetchOptions {
239
291
  resolveTarget: ResolveRepairTarget
240
292
  transport: RepairFetchTransport
@@ -261,6 +313,20 @@ export interface WireRepairFetchOptions {
261
313
  * (`presentation-delay-unavailable`).
262
314
  */
263
315
  presentationDelayMs: (packetId: number, catalogTrackName?: string) => number
316
+ /**
317
+ * How long after the first symbol of a block that may hold a RAP a repair
318
+ * FETCH of it may still land, in ms (optional). Such a block
319
+ * (`keyframeBudgetEligible`) gets the larger of this and
320
+ * `presentationDelayMs`, still never later than the release gate's hold
321
+ * ceiling and never earlier than the multicast wait; RTT admission is
322
+ * unchanged. Losing a keyframe block costs the rest of its GOP, so a player
323
+ * may hold the block (and the frames released after it) while its FETCH is
324
+ * in flight rather than freeze to the next keyframe. Absent, throwing, or
325
+ * not finite and positive: every block gets the presentation deadline
326
+ * alone. Never applied to audio, delta, or an `unknown` block of an
327
+ * `all-protected` stream.
328
+ */
329
+ keyframeRepairBudgetMs?: (packetId: number, catalogTrackName?: string) => number
264
330
  }
265
331
 
266
332
  /** Why the D-28 layer asked for a repair: the block's class at its deadline. */
@@ -287,6 +353,20 @@ export interface RepairFetchHost {
287
353
  repairFetchTimeoutMsForBlock(sbn: number, packetId: number, catalogTrackName?: string): number | undefined
288
354
  /** Receiver-local time of the block's first accepted symbol, if any. */
289
355
  blockAnchorAtMs(sbn: number, packetId: number, catalogTrackName?: string): number | undefined
356
+ /** The stream's classifier mode (optional; absent reads as unknown). */
357
+ classifierModeFor?(packetId: number, catalogTrackName?: string): { readonly mode: ClassifierMode }
358
+ /**
359
+ * An open block's received sources and distinct repair symbols, its K'
360
+ * (SSB_length) and RSB_length (optional). The repair group carries
361
+ * RSB_length symbols, so it can complete the block only when
362
+ * sources + RSB_length >= K'; a FETCH that ended leaving fewer than K'
363
+ * distinct symbols cannot be decoding it.
364
+ */
365
+ blockSymbolCounts?(
366
+ sbn: number,
367
+ packetId: number,
368
+ catalogTrackName?: string,
369
+ ): BlockSymbolCounts | undefined
290
370
  /** How long a terminal block stays authoritative: the release-gate hold ceiling. */
291
371
  terminalRetentionMsForPacket(packetId: number): number | undefined
292
372
  /** Keep the block open to `untilAtMs` for an admitted FETCH. */
@@ -300,6 +380,20 @@ export interface RepairFetchHost {
300
380
  releaseRepairHold(sbn: number, packetId: number, catalogTrackName?: string): void
301
381
  }
302
382
  onRepairSubscribe(cb: RepairSubscribeCallback): void
383
+ /**
384
+ * Arrival time of the earliest frame the in-order release gate holds for
385
+ * the stream, or undefined (optional; absent, the block's anchor bounds the
386
+ * ceiling alone). The gate releases that frame `terminalRetentionMsForPacket`
387
+ * after it, whatever its blocks are doing.
388
+ */
389
+ earliestGatedFrameAtMs?(packetId: number, catalogTrackName?: string): number | undefined
390
+ /** The keyframe budget extended a request's deadline by `extensionMs` (optional). */
391
+ noteKeyframeBudgetGranted?(
392
+ extensionMs: number,
393
+ sbn: number,
394
+ packetId: number,
395
+ catalogTrackName?: string,
396
+ ): void
303
397
  addRepairLifecycleObserver(observer: RepairLifecycleObserver): () => void
304
398
  addTrackResetObserver(observer: (packetId: number, catalogTrackName: string) => void): () => void
305
399
  clearRepairForBlock(sbn: number, packetId: number, catalogTrackName?: string): void
@@ -319,10 +413,24 @@ export interface RepairFetchHost {
319
413
  packetId: number,
320
414
  catalogTrackName?: string,
321
415
  ): void
322
- /** A request the scheduler did not send (see RepairSkipReason). */
416
+ /**
417
+ * A request the scheduler did not send (see RepairSkipReason).
418
+ * `measurement.priority` is the request's repair priority (0 keyframe or
419
+ * unknown, 1 audio, 2 delta).
420
+ */
323
421
  noteRepairSkipped(
324
422
  reason: RepairSkipReason,
325
- measurement: { readonly rttMs: number | null; readonly budgetMs: number },
423
+ measurement: { readonly rttMs: number | null; readonly budgetMs: number; readonly priority?: RepairPriority },
424
+ sbn: number,
425
+ packetId: number,
426
+ catalogTrackName?: string,
427
+ ): void
428
+ /**
429
+ * A FETCH the scheduler admitted and handed to the transport (optional):
430
+ * every attempt, a retry or a reprioritised restart included.
431
+ */
432
+ noteRepairFetchSent?(
433
+ priority: RepairPriority,
326
434
  sbn: number,
327
435
  packetId: number,
328
436
  catalogTrackName?: string,
@@ -350,9 +458,10 @@ interface RepairEntry {
350
458
  subscriberPriority: number | undefined
351
459
  /**
352
460
  * Absolute `performance.now()` millisecond by which a FETCH must land: the
353
- * block's presentation deadline (see the module note).
461
+ * block's presentation deadline (see the module note). Only ever moves
462
+ * later: a block reclassified from delta can gain the keyframe budget.
354
463
  */
355
- readonly deadlineAt: number
464
+ deadlineAt: number
356
465
  /** Monotonic enqueue order, the EDF tie-break. */
357
466
  readonly order: number
358
467
  attempts: number
@@ -415,6 +524,18 @@ export function wireRepairFetch(mc: RepairFetchHost, opts: WireRepairFetchOption
415
524
  if (reason === 'preempted') releaseHold(entry)
416
525
  }
417
526
 
527
+ /** The keyframe budget for a P0 block, or null: absent, throwing or not positive. */
528
+ const readKeyframeBudget = (packetId: number, catalogTrackName: string | undefined): number | null => {
529
+ const reader = opts.keyframeRepairBudgetMs
530
+ if (reader === undefined) return null
531
+ try {
532
+ const budgetMs = reader(packetId, catalogTrackName)
533
+ return Number.isFinite(budgetMs) && budgetMs > 0 ? budgetMs : null
534
+ } catch {
535
+ return null
536
+ }
537
+ }
538
+
418
539
  const readRtt = (): number | null => {
419
540
  try {
420
541
  const rtt = opts.reliableRttMs()
@@ -438,7 +559,7 @@ export function wireRepairFetch(mc: RepairFetchHost, opts: WireRepairFetchOption
438
559
  clearRepair(entry.sbn, entry.packetId, entry.catalogTrackName)
439
560
  mc.noteRepairSkipped(
440
561
  rttMs === null ? 'rtt-unmeasured' : 'rtt-exceeds-budget',
441
- { rttMs, budgetMs },
562
+ { rttMs, budgetMs, priority: entry.priority },
442
563
  entry.sbn,
443
564
  entry.packetId,
444
565
  entry.catalogTrackName,
@@ -490,6 +611,7 @@ export function wireRepairFetch(mc: RepairFetchHost, opts: WireRepairFetchOption
490
611
  const start = (entry: RepairEntry, counted = true): void => {
491
612
  if (!admit(entry, performance.now())) return
492
613
  if (counted) entry.attempts++
614
+ mc.noteRepairFetchSent?.(entry.priority, entry.sbn, entry.packetId, entry.catalogTrackName)
493
615
  const run: FetchRun = { controller: new AbortController(), stopped: null }
494
616
  entry.running = run
495
617
  // Hold the block open to the presentation deadline while this FETCH can
@@ -540,6 +662,45 @@ export function wireRepairFetch(mc: RepairFetchHost, opts: WireRepairFetchOption
540
662
  .forEach((entry) => start(entry))
541
663
  }
542
664
 
665
+ /**
666
+ * Whether the block may still be decoding from what it holds: false only
667
+ * when the host shows fewer distinct symbols than its K'. A FETCH that
668
+ * ended leaving it short delivered all it could (its group carries no more
669
+ * symbols), so holding the block, and every frame behind it, longer buys
670
+ * nothing: it goes back to its multicast wait. Unknown counts read as
671
+ * decodable (the FEC layer resolves the block).
672
+ */
673
+ const symbolCounts = (
674
+ sbn: number,
675
+ packetId: number,
676
+ catalogTrackName: string | undefined,
677
+ ): BlockSymbolCounts | undefined => {
678
+ try {
679
+ return mc.fecManager.blockSymbolCounts?.(sbn, packetId, catalogTrackName)
680
+ } catch {
681
+ return undefined
682
+ }
683
+ }
684
+ const decodable = (entry: RepairEntry): boolean => {
685
+ const counts = symbolCounts(entry.sbn, entry.packetId, entry.catalogTrackName)
686
+ if (counts === undefined || counts.authoredLength === undefined) return true
687
+ return counts.sources + counts.repairs >= counts.authoredLength
688
+ }
689
+ /**
690
+ * Whether the block's repair group can complete it: its received sources
691
+ * plus every symbol the group carries reach K'. A loss of more sources than
692
+ * the group has repair symbols (every source-only loss among them: the
693
+ * block then already holds the whole group) gets no FETCH-length hold. True
694
+ * while the counts are not known (no repair stated the block yet).
695
+ */
696
+ const groupCanComplete = (sbn: number, packetId: number, catalogTrackName: string | undefined): boolean => {
697
+ const counts = symbolCounts(sbn, packetId, catalogTrackName)
698
+ if (counts === undefined || counts.authoredLength === undefined || counts.repairGroupLength === undefined) {
699
+ return true
700
+ }
701
+ return counts.sources + counts.repairGroupLength >= counts.authoredLength
702
+ }
703
+
543
704
  const finish = (entry: RepairEntry, run: FetchRun, fed: number, error: unknown): void => {
544
705
  if (run.stopped !== null || entry.running !== run || entries.get(entry.key) !== entry) {
545
706
  return
@@ -550,7 +711,7 @@ export function wireRepairFetch(mc: RepairFetchHost, opts: WireRepairFetchOption
550
711
  // A fetch that ended without feeding anything cannot resolve the block;
551
712
  // release the slot and the hold so failure accounting is not wedged. A
552
713
  // fetch that fed symbols leaves resolution to the FEC layer.
553
- if (fed === 0) {
714
+ if (fed === 0 || !decodable(entry)) {
554
715
  releaseHold(entry)
555
716
  clearRepair(entry.sbn, entry.packetId, entry.catalogTrackName)
556
717
  }
@@ -644,6 +805,19 @@ export function wireRepairFetch(mc: RepairFetchHost, opts: WireRepairFetchOption
644
805
  if (wire === null) return
645
806
  entry.priority = promoted
646
807
  entry.subscriberPriority = wire.subscriberPriority
808
+ // A block that turned out to carry a keyframe gets the keyframe budget
809
+ // its request would have had (never a shorter deadline than it had).
810
+ const promotedDeadline = presentationDeadlineAt(
811
+ entry.sbn,
812
+ entry.packetId,
813
+ entry.catalogTrackName,
814
+ performance.now(),
815
+ event.to,
816
+ )
817
+ if (promotedDeadline !== null && promotedDeadline.deadlineAt > entry.deadlineAt) {
818
+ entry.deadlineAt = promotedDeadline.deadlineAt
819
+ noteBudget(entry, promotedDeadline.extensionMs)
820
+ }
647
821
  if (entry.running) {
648
822
  // The running FETCH carries the delta priority. Cancel it and send
649
823
  // the block's repair again under its new class (§11.4.5): a keyframe
@@ -673,21 +847,26 @@ export function wireRepairFetch(mc: RepairFetchHost, opts: WireRepairFetchOption
673
847
  * is open that long whatever happens, so a FETCH landing inside it still
674
848
  * counts) and above by anchor + the release gate's hold ceiling (a successor
675
849
  * frame may not wait behind a block longer than that). A block no symbol
676
- * reached has no anchor; its deadline is the multicast wait.
850
+ * reached has no anchor; its deadline is the multicast wait. The ceiling is
851
+ * measured from the earlier of the anchor and the earliest frame the gate
852
+ * holds for the stream (see the module note). A block that may hold a RAP
853
+ * (`keyframeBudgetEligible`) uses the larger of the presentation delay and
854
+ * the keyframe budget; `extensionMs` is how much later that made it.
677
855
  */
678
856
  const presentationDeadlineAt = (
679
857
  sbn: number,
680
858
  packetId: number,
681
859
  catalogTrackName: string | undefined,
682
860
  now: number,
683
- ): number | null => {
861
+ blockClass: BlockClass,
862
+ ): { readonly deadlineAt: number; readonly extensionMs: number } | null => {
684
863
  // Undefined: the packetId is no longer configured (released), so it has
685
864
  // no repair budget.
686
865
  const waitMs = mc.fecManager.repairFetchTimeoutMsForBlock(sbn, packetId, catalogTrackName)
687
866
  if (waitMs === undefined) return null
688
867
  const waitDeadlineAt = now + waitMs
689
868
  const anchor = mc.fecManager.blockAnchorAtMs(sbn, packetId, catalogTrackName)
690
- if (anchor === undefined) return waitDeadlineAt
869
+ if (anchor === undefined) return { deadlineAt: waitDeadlineAt, extensionMs: 0 }
691
870
  let delayMs: number
692
871
  try {
693
872
  delayMs = opts.presentationDelayMs(packetId, catalogTrackName)
@@ -697,8 +876,38 @@ export function wireRepairFetch(mc: RepairFetchHost, opts: WireRepairFetchOption
697
876
  if (!(Number.isFinite(delayMs) && delayMs > 0)) return null
698
877
  const retentionMs = mc.fecManager.terminalRetentionMsForPacket(packetId)
699
878
  if (retentionMs === undefined) return null
700
- const ceilingAt = anchor + retentionMs
701
- return Math.max(waitDeadlineAt, Math.min(anchor + delayMs, ceilingAt))
879
+ const ceilingAt = Math.min(anchor, earliestGatedFrameAt(packetId, catalogTrackName) ?? anchor) + retentionMs
880
+ const deadlineAt = (delay: number): number => Math.max(waitDeadlineAt, Math.min(anchor + delay, ceilingAt))
881
+ const base = deadlineAt(delayMs)
882
+ const budgetMs =
883
+ keyframeBudgetEligible(blockClass, classifierMode(packetId, catalogTrackName)) &&
884
+ groupCanComplete(sbn, packetId, catalogTrackName)
885
+ ? readKeyframeBudget(packetId, catalogTrackName)
886
+ : null
887
+ if (budgetMs === null || budgetMs <= delayMs) return { deadlineAt: base, extensionMs: 0 }
888
+ const extended = deadlineAt(budgetMs)
889
+ return { deadlineAt: extended, extensionMs: extended - base }
890
+ }
891
+
892
+ const earliestGatedFrameAt = (packetId: number, catalogTrackName: string | undefined): number | undefined => {
893
+ try {
894
+ const at = mc.earliestGatedFrameAtMs?.(packetId, catalogTrackName)
895
+ return at !== undefined && Number.isFinite(at) ? at : undefined
896
+ } catch {
897
+ return undefined
898
+ }
899
+ }
900
+
901
+ const classifierMode = (packetId: number, catalogTrackName: string | undefined): ClassifierMode | undefined => {
902
+ try {
903
+ return mc.fecManager.classifierModeFor?.(packetId, catalogTrackName).mode
904
+ } catch {
905
+ return undefined
906
+ }
907
+ }
908
+
909
+ const noteBudget = (entry: Pick<RepairEntry, 'sbn' | 'packetId' | 'catalogTrackName'>, extensionMs: number): void => {
910
+ if (extensionMs > 0) mc.noteKeyframeBudgetGranted?.(extensionMs, entry.sbn, entry.packetId, entry.catalogTrackName)
702
911
  }
703
912
 
704
913
  mc.onRepairSubscribe((sbn, packetId, catalogTrackName, blockClass) => {
@@ -748,12 +957,13 @@ export function wireRepairFetch(mc: RepairFetchHost, opts: WireRepairFetchOption
748
957
  return
749
958
  }
750
959
  const now = performance.now()
751
- const deadlineAt = presentationDeadlineAt(sbn, packetId, catalogTrackName, now)
752
- if (deadlineAt === null) {
960
+ const priority = repairPriorityForClass(blockClass)
961
+ const deadline = presentationDeadlineAt(sbn, packetId, catalogTrackName, now, blockClass)
962
+ if (deadline === null) {
753
963
  clearRepair(sbn, packetId, catalogTrackName)
754
964
  mc.noteRepairSkipped(
755
965
  'presentation-delay-unavailable',
756
- { rttMs: readRtt(), budgetMs: 0 },
966
+ { rttMs: readRtt(), budgetMs: 0, priority },
757
967
  sbn,
758
968
  packetId,
759
969
  catalogTrackName,
@@ -765,14 +975,15 @@ export function wireRepairFetch(mc: RepairFetchHost, opts: WireRepairFetchOption
765
975
  ...identity,
766
976
  repairTrack: target.repairTrack,
767
977
  blockProfile,
768
- priority: repairPriorityForClass(blockClass),
978
+ priority,
769
979
  subscriberPriority: wire.subscriberPriority,
770
- deadlineAt,
980
+ deadlineAt: deadline.deadlineAt,
771
981
  order: enqueueOrder++,
772
982
  attempts: 0,
773
983
  running: null,
774
984
  }
775
985
  entries.set(key, entry)
986
+ noteBudget(entry, deadline.extensionMs)
776
987
  if (entry.priority < 2) preemptDeltas()
777
988
  dispatch()
778
989
  })
@@ -8,13 +8,22 @@ Positive fixtures MUST validate against `schemas/catalog.schema.json`.
8
8
  `releases/` is a third category — a frozen record of what a shipped release
9
9
  actually emitted, which validates or fails depending on what that release
10
10
  predates; it is never edited to make it pass.
11
- Negative fixtures are the migration worklist and MUST fail:
11
+ Negative fixtures are the migration worklist and MUST fail.
12
+ `section-codes.json` lists every negative once: the code and path the
13
+ TypeScript runtime validator and the Rust catalog section (`mmt_fec::catalog`)
14
+ both reject it with, or `outOfSection` when the section does not own the field.
15
+ The Rust section owns only the track MMTP/FEC/repair/timing fields.
16
+
17
+ The negatives:
12
18
 
13
19
  - `legacy-selection-params.json`: rendition fields must be flat.
14
20
  - `missing-mmtp-fields.json`: MMTP tracks require mode, timescale, and duration.
15
21
  - `multicast-packet-id-signaling.json`: an endpoint track's `packetId` is
16
22
  1..65535; `packetId` 0 is the MMTP signaling flow and is never a track's.
17
23
  - `network-source-object.json`: `networkSource` is always an array.
24
+ - `multicast-endpoint-relay-address.json`: the AMT relay is catalog-level
25
+ `networkSource[{type:"amt",relay}]`; an endpoint `relayAddress` is retired
26
+ and rejected (`multicast_relay_address`), not ignored.
18
27
  - `multicast-network-source-empty.json`: `networkSource` is optional, but an
19
28
  empty array is not a way to say "absent" — omit the key instead.
20
29
  - `repair-track-legacy-shape.json`: repair tracks use `packaging: "fec-repair"`.
@@ -0,0 +1,39 @@
1
+ {
2
+ "version": 1,
3
+ "streamingFormat": "mmtp",
4
+ "streamingFormatVersion": "draft-ramadan-moq-mmt-00",
5
+ "tracks": [
6
+ {
7
+ "name": "video",
8
+ "packaging": "mmtp",
9
+ "codec": "avc1.640028",
10
+ "mmtpMode": "mfu",
11
+ "timescale": 90000,
12
+ "groupDurationMs": 1000
13
+ }
14
+ ],
15
+ "multicast": {
16
+ "endpoints": [
17
+ {
18
+ "protocol": "amt",
19
+ "sourceAddress": "192.0.2.10",
20
+ "groupAddress": "232.1.1.1",
21
+ "port": 5000,
22
+ "relayAddress": "amt-relay.example.test",
23
+ "tracks": [
24
+ {
25
+ "name": "video",
26
+ "packetId": 1
27
+ }
28
+ ]
29
+ }
30
+ ],
31
+ "networkSource": [
32
+ {
33
+ "type": "amt",
34
+ "discovery": "manual",
35
+ "relay": "amt-relay.example.test"
36
+ }
37
+ ]
38
+ }
39
+ }
@@ -6,6 +6,7 @@
6
6
  {
7
7
  "name": "video",
8
8
  "packaging": "mmtp",
9
+ "codec": "avc1.640028",
9
10
  "mmtpMode": "mfu",
10
11
  "timescale": 90000,
11
12
  "groupDurationMs": 1000,