@blockcast/mmt-render 0.1.0-main.ffdace838275 → 0.3.0-main.0bde85f7082f
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.
- package/README.md +5 -4
- package/dist/pacing/index.js +15 -4
- package/dist/pacing/index.js.map +2 -2
- package/dist/pacing/interleave-timing.d.ts +10 -1
- package/dist/pacing/interleave-timing.d.ts.map +1 -1
- package/dist/pacing/interleave-timing.js +15 -4
- package/dist/pacing/interleave-timing.js.map +2 -2
- package/package.json +3 -3
- package/src/pacing/interleave-timing.ts +32 -5
package/README.md
CHANGED
|
@@ -8,7 +8,7 @@ Shared render pipeline for MMT players: FEC-aware wall-clock pacing, per-AU pres
|
|
|
8
8
|
pnpm add @blockcast/mmt-render
|
|
9
9
|
```
|
|
10
10
|
|
|
11
|
-
Peer-installs through normal deps: `@blockcast/mmt-container` (provides `MmtpDecodePipeline` and the FEC-aware MFU reassembler) and `@blockcast/mmt-fec` (provides `FecBlockMapper`, which carries `pacingThresholdMs`, `K`, `D
|
|
11
|
+
Peer-installs through normal deps: `@blockcast/mmt-container` (provides `MmtpDecodePipeline` and the FEC-aware MFU reassembler) and `@blockcast/mmt-fec` (provides `FecBlockMapper`, which carries `pacingThresholdMs`, `K`, `D` and the catalog's required `fec.blockProfile`).
|
|
12
12
|
|
|
13
13
|
## Quick start (audio)
|
|
14
14
|
|
|
@@ -17,7 +17,8 @@ import { AudioRenderPipeline } from "@blockcast/mmt-render/audio"
|
|
|
17
17
|
import { FecBlockMapper } from "@blockcast/mmt-fec"
|
|
18
18
|
|
|
19
19
|
const ctx = new AudioContext({ sampleRate: 48000 })
|
|
20
|
-
|
|
20
|
+
// blockProfile is the catalog `fec.blockProfile`: required, never defaulted.
|
|
21
|
+
const mapper = new FecBlockMapper({ blockProfile: "k-padded-v1", interleaveDepthMs: 66, gopDurationMs: 21.33, k: 32 })
|
|
21
22
|
|
|
22
23
|
const audio = new AudioRenderPipeline({
|
|
23
24
|
context: ctx,
|
|
@@ -103,7 +104,7 @@ Media-agnostic wall-clock pacer. Anchors on the first `notifyFrame()` call; comp
|
|
|
103
104
|
|
|
104
105
|
### `InterleaveTiming` (`src/pacing/interleave-timing.ts`)
|
|
105
106
|
|
|
106
|
-
Per-AU presentation-time reconstruction from FEC coordinates
|
|
107
|
+
Per-AU presentation-time reconstruction from FEC coordinates: the `k-padded-v1` K-stride pacing model of `draft-ramadan-moq-fec-00 §8.3`, with `symbolsPerGroup = ceil(K / D)` computed here. It is pacing arithmetic, not an FEC block locator, and refuses a mapper under any other `fec.blockProfile`:
|
|
107
108
|
|
|
108
109
|
```
|
|
109
110
|
au_index_in_block = groupInBlock × symbolsPerGroup + objectId
|
|
@@ -189,7 +190,7 @@ There is **no CMCD `x-bc-*` emission, no catalog (CMSF) inspector, and no MPU/MF
|
|
|
189
190
|
## Related packages
|
|
190
191
|
|
|
191
192
|
- `@blockcast/mmt-container` — `MmtpDecodePipeline` (consumed for AU framing), MFU reassembly, catalog parser
|
|
192
|
-
- `@blockcast/mmt-fec` — `FecBlockMapper` (provides `pacingThresholdMs`, `K`, `D`, `
|
|
193
|
+
- `@blockcast/mmt-fec` — `FecBlockMapper` (provides `pacingThresholdMs`, `K`, `D`, `blockProfile`); `NullBlockMapper` disables pacing
|
|
193
194
|
- `@blockcast/mmt-transport` — produces the MoQ objects that feed `feedMoqObject()`
|
|
194
195
|
|
|
195
196
|
## Testing
|
package/dist/pacing/index.js
CHANGED
|
@@ -89,6 +89,7 @@ function now() {
|
|
|
89
89
|
}
|
|
90
90
|
|
|
91
91
|
// src/pacing/interleave-timing.ts
|
|
92
|
+
var SS_ID_SPAN = 4294967296;
|
|
92
93
|
var InterleaveTiming = class {
|
|
93
94
|
constructor(opts) {
|
|
94
95
|
if (!(opts.frameDurationUs > 0)) {
|
|
@@ -97,7 +98,13 @@ var InterleaveTiming = class {
|
|
|
97
98
|
if (!Number.isFinite(opts.anchorMpuUs)) {
|
|
98
99
|
throw new Error("anchorMpuUs must be finite");
|
|
99
100
|
}
|
|
101
|
+
if (opts.mapper.blockProfile !== "k-padded-v1") {
|
|
102
|
+
throw new Error(
|
|
103
|
+
`InterleaveTiming is the k-padded-v1 K-stride pacing model; a ${opts.mapper.blockProfile ?? "non-FEC"} stream has no K-stride block geometry`
|
|
104
|
+
);
|
|
105
|
+
}
|
|
100
106
|
this.mapper = opts.mapper;
|
|
107
|
+
this.symbolsPerGroup = Math.ceil(opts.mapper.k / opts.mapper.d);
|
|
101
108
|
this.frameDurationUs = opts.frameDurationUs;
|
|
102
109
|
this.anchorMpuUs = opts.anchorMpuUs;
|
|
103
110
|
}
|
|
@@ -106,8 +113,11 @@ var InterleaveTiming = class {
|
|
|
106
113
|
* Per draft §8.3 + Sony anchor.
|
|
107
114
|
*/
|
|
108
115
|
presentationUs(groupId, objectId) {
|
|
109
|
-
const
|
|
110
|
-
|
|
116
|
+
const { k, d } = this.mapper;
|
|
117
|
+
const sbn = Math.floor(groupId / d);
|
|
118
|
+
const esi = (groupId - sbn * d) * this.symbolsPerGroup + objectId;
|
|
119
|
+
const ssId = (sbn % SS_ID_SPAN * k + esi) % SS_ID_SPAN;
|
|
120
|
+
return this.anchorMpuUs + ssId * this.frameDurationUs;
|
|
111
121
|
}
|
|
112
122
|
/**
|
|
113
123
|
* Presentation time (µs) for an AU identified by its flat SS_ID
|
|
@@ -122,8 +132,9 @@ var InterleaveTiming = class {
|
|
|
122
132
|
*/
|
|
123
133
|
frameAtOffset(offsetUs) {
|
|
124
134
|
const rawSsId = Math.max(0, Math.floor(offsetUs / this.frameDurationUs));
|
|
125
|
-
const
|
|
126
|
-
const
|
|
135
|
+
const sbn = Math.floor(rawSsId / this.mapper.k);
|
|
136
|
+
const esi = rawSsId % this.mapper.k;
|
|
137
|
+
const spg = this.symbolsPerGroup;
|
|
127
138
|
const groupInBlock = Math.floor(esi / spg);
|
|
128
139
|
const objectId = esi - groupInBlock * spg;
|
|
129
140
|
const groupId = sbn * this.mapper.d + groupInBlock;
|
package/dist/pacing/index.js.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/pacing/frame-pacer.ts", "../../src/pacing/interleave-timing.ts", "../../src/pacing/au-duration.ts"],
|
|
4
|
-
"sourcesContent": ["/**\n * FrameCountPacer \u2014 media-agnostic wall-clock pacer for MMT A/V decode loops.\n *\n * Why not use MPU timestamps? Under FEC interleaving, encoders pack multiple\n * Access Units (AUs) per MMTP MPU with a single mpu_timestamp. The observed\n * timestamp delta between MPUs is a fraction of the real per-AU duration\n * (e.g. 10.22ms between MPUs for 21.33ms AAC frames at D=2 interleave).\n * Pacing on that delta sees \"media \u2248 wall\" and never sleeps, letting burst\n * relay delivery saturate the decoder / ring buffer.\n *\n * The Sony libatsc3 pattern (atsc3_mmt_context_mfu_depacketizer) solves this\n * by anchoring wall-clock time to the first received AU and counting AUs:\n * media_elapsed_ms = au_count \u00D7 nominal_au_duration_ms\n * The MPU timestamp is used elsewhere as an absolute-time anchor, never as\n * an incremental clock.\n *\n * The sleep threshold comes from the catalog's FEC interleaveDepthMs (via\n * FecBlockMapper.pacingThresholdMs). When no FEC is configured, a\n * NullBlockMapper yields threshold=0 and this pacer never sleeps.\n *\n * One pacer instance = one track (e.g. audio, video) on one player.\n */\n\nimport type { FecBlockMapper } from '@blockcast/mmt-fec'\n\nexport interface FrameCountPacerOpts {\n /** Required: AU duration (audio) or picture interval (video), in ms. */\n frameDurationMs: number\n /** FEC mapper from @blockcast/mmt-fec. NullBlockMapper disables pacing. */\n mapper: FecBlockMapper\n /**\n * Number of frames to let through before pacing starts. Lets startup\n * burst drain populate the ring buffer / decoder queue. Default 0.\n */\n warmupFrames?: number\n}\n\nexport interface PacerSnapshot {\n /** Frames observed (from notifyFrame). */\n frameCount: number\n /** Computed media time: frameCount \u00D7 frameDurationMs. */\n mediaMs: number\n /** Wall-clock time since anchor. */\n wallMs: number\n /** mediaMs - wallMs. Positive \u2192 decoder ahead of realtime. */\n aheadMs: number\n /** Pacing threshold (ms) from the FEC mapper. */\n thresholdMs: number\n /** Has the first frame been observed (wall anchor set)? */\n anchored: boolean\n}\n\nexport class FrameCountPacer {\n readonly frameDurationMs: number\n readonly thresholdMs: number\n readonly warmupFrames: number\n\n #frameCount = 0\n #anchorWallMs = 0\n #anchored = false\n\n constructor(opts: FrameCountPacerOpts) {\n if (!(opts.frameDurationMs > 0)) {\n throw new Error('frameDurationMs must be > 0')\n }\n this.frameDurationMs = opts.frameDurationMs\n this.thresholdMs = opts.mapper.pacingThresholdMs\n this.warmupFrames = Math.max(0, opts.warmupFrames ?? 0)\n }\n\n /** Call on every emitted decoded frame (AU or picture). Anchors on first call. */\n notifyFrame(): void {\n if (!this.#anchored) {\n this.#anchorWallMs = now()\n this.#anchored = true\n }\n this.#frameCount++\n }\n\n /**\n * Compute sleep ms before reading the next MoQ object.\n * Returns 0 when:\n * - pacing is disabled (NullBlockMapper \u2192 threshold=0)\n * - the anchor is not yet set\n * - still in warmup\n * - the caller reports a standing backlog (see `observedDelayMs`)\n * - decoder is not ahead of wall clock by more than threshold\n * Otherwise returns aheadMs - thresholdMs/2 (leaves half the threshold as headroom).\n *\n * `observedDelayMs` is how far this frame is behind the producer's own clock,\n * measured by the caller against a producer-supplied sending instant (for\n * MMTP, the header send-wall timestamp). It is optional because the anchored\n * rate below is blind to it: `#anchorWallMs` is the wall time of the FIRST\n * frame read, so `aheadMs` measures consumption against the join instant and\n * not against the producer. Once `aheadMs` reaches the threshold the loop\n * consumes at exactly the source rate, and recovering latency present at join\n * requires consuming FASTER than the source rate. So without this argument a\n * consumer that joined a live stream mid-block keeps that join latency for the\n * life of the subscription.\n *\n * A caller that can measure the real distance from the producer passes it and\n * gets a drain: while the backlog exceeds the threshold there is nothing to\n * pace against, so return 0 and let the transport set the rate. At the live\n * edge the anchored-rate behavior below applies unchanged.\n *\n * Callers with no producer clock omit it and keep the pure ATSC3-style\n * anchored pacing (see the class doc), where a first-frame anchor is correct\n * because a broadcast push has no live edge to catch up to.\n */\n computeSleepMs(observedDelayMs?: number): number {\n if (this.thresholdMs <= 0) return 0\n if (!this.#anchored) return 0\n if (this.#frameCount <= this.warmupFrames) return 0\n // Only a finite measurement can license the drain. NaN/Infinity mean the\n // caller could not measure the distance, which is not the same claim as\n // \"the consumer is at the edge\", so fall through to anchored pacing.\n if (observedDelayMs !== undefined && Number.isFinite(observedDelayMs) && observedDelayMs > this.thresholdMs) {\n return 0\n }\n\n const mediaMs = this.#frameCount * this.frameDurationMs\n const wallMs = now() - this.#anchorWallMs\n const aheadMs = mediaMs - wallMs\n if (aheadMs <= this.thresholdMs) return 0\n return aheadMs - this.thresholdMs / 2\n }\n\n /** Reset anchor + counter. Call on source switch (MoQ \u2194 multicast). */\n reset(): void {\n this.#frameCount = 0\n this.#anchorWallMs = 0\n this.#anchored = false\n }\n\n get snapshot(): PacerSnapshot {\n const mediaMs = this.#frameCount * this.frameDurationMs\n const wallMs = this.#anchored ? now() - this.#anchorWallMs : 0\n return {\n frameCount: this.#frameCount,\n mediaMs,\n wallMs,\n aheadMs: mediaMs - wallMs,\n thresholdMs: this.thresholdMs,\n anchored: this.#anchored,\n }\n }\n}\n\nfunction now(): number {\n // performance.now() is available in main thread, workers, and AudioWorklet.\n // Fall back to Date.now() for exotic runtimes (shouldn't happen in browsers).\n if (typeof performance !== 'undefined' && typeof performance.now === 'function') {\n return performance.now()\n }\n return Date.now()\n}\n", "/**\n * InterleaveTiming \u2014 per-AU presentation-time reconstruction.\n *\n * When encoders pack multiple AUs per MMTP MPU with a single mpu_timestamp\n * (normal for FEC-interleaved audio, and for video with multi-picture MPUs),\n * the raw mpu_timestamp is ambiguous for per-AU scheduling. The FEC block\n * math (draft-ramadan-moq-fec-00 \u00A78.3) combined with the Sony libatsc3\n * pattern recovers correct per-AU time:\n *\n * au_index_in_block = groupInBlock \u00D7 symbolsPerGroup + objectId\n * ssId = sbn \u00D7 K + au_index_in_block\n * presentationUs = anchorMpuUs + ssId \u00D7 frameDurationUs\n *\n * The anchor (anchorMpuUs) is the mpu_timestamp of the first observed AU.\n * Every downstream per-AU time is then deterministic from FEC coordinates\n * + codec frame duration \u2014 identical whether the AU arrives via MoQ unicast\n * (group_id/object_id) or multicast UDP (SS_ID from FEC Payload ID).\n *\n * Consumers: CMAF segment assembler (block boundary \u2192 segment cut points),\n * debug overlays, cross-path FEC combining, sync diagnostics.\n */\n\nimport type { FecBlockMapper } from '@blockcast/mmt-fec'\n\nexport interface InterleaveTimingOpts {\n /** Track's FEC mapper (provides K, D, symbolsPerGroup). */\n mapper: FecBlockMapper\n /** Nominal AU duration in microseconds. */\n frameDurationUs: number\n /** Wall-time anchor: the mpu_timestamp of the first observed AU (\u00B5s). */\n anchorMpuUs: number\n}\n\nexport class InterleaveTiming {\n readonly mapper: FecBlockMapper\n readonly frameDurationUs: number\n readonly anchorMpuUs: number\n\n constructor(opts: InterleaveTimingOpts) {\n if (!(opts.frameDurationUs > 0)) {\n throw new Error('frameDurationUs must be > 0')\n }\n if (!Number.isFinite(opts.anchorMpuUs)) {\n throw new Error('anchorMpuUs must be finite')\n }\n this.mapper = opts.mapper\n this.frameDurationUs = opts.frameDurationUs\n this.anchorMpuUs = opts.anchorMpuUs\n }\n\n /**\n * Presentation time (\u00B5s) for an AU at MoQ coords (group_id, object_id).\n * Per draft \u00A78.3 + Sony anchor.\n */\n presentationUs(groupId: number, objectId: number): number {\n const coords = this.mapper.derive(groupId, objectId)\n return this.anchorMpuUs + coords.ssId * this.frameDurationUs\n }\n\n /**\n * Presentation time (\u00B5s) for an AU identified by its flat SS_ID\n * (multicast FEC Payload ID path).\n */\n presentationUsFromSsId(ssId: number): number {\n return this.anchorMpuUs + ssId * this.frameDurationUs\n }\n\n /**\n * Inverse: which (group_id, object_id) AU is scheduled at the given\n * wall offset (\u00B5s) from anchor? Clamps to ssId\u22650.\n */\n frameAtOffset(offsetUs: number): { groupId: number; objectId: number; ssId: number } {\n const rawSsId = Math.max(0, Math.floor(offsetUs / this.frameDurationUs))\n const { sbn, esi } = this.mapper.fromSsId(rawSsId)\n // Invert \u00A78.3: esi = groupInBlock * symbolsPerGroup + objectId\n const spg = this.mapper.symbolsPerGroup\n const groupInBlock = Math.floor(esi / spg)\n const objectId = esi - groupInBlock * spg\n const groupId = sbn * this.mapper.d + groupInBlock\n return { groupId, objectId, ssId: rawSsId }\n }\n\n /**\n * Block start/end in microseconds. Source blocks span `D \u00D7 GOP_duration`\n * of wall time; this returns the anchored wall range [startUs, endUs).\n */\n blockTimeRangeUs(sbn: number): { startUs: number; endUs: number } {\n const firstSsId = sbn * this.mapper.k\n const lastSsId = (sbn + 1) * this.mapper.k\n return {\n startUs: this.anchorMpuUs + firstSsId * this.frameDurationUs,\n endUs: this.anchorMpuUs + lastSsId * this.frameDurationUs,\n }\n }\n}\n", "/**\n * Nominal Access Unit duration helpers for common audio + video codecs.\n *\n * Kept in the pacing subpath (no mmt-container dep) so lean consumers like\n * moqtail's player.ts can import the pacer + AU duration without pulling\n * in the full audio-render-pipeline (which depends on @blockcast/mmt-container).\n */\n\n/**\n * Nominal AU duration in ms for common audio codecs at the given sample rate.\n * AAC-LC: 1024 samples. Opus (default 20ms): 960 samples @ 48kHz.\n * Throws for unknown codecs so callers supply correct timing.\n */\nexport function auDurationMsFromCodec(codec: string, sampleRate: number): number {\n if (!(sampleRate > 0)) throw new Error('sampleRate must be > 0')\n if (codec.startsWith('opus') || codec.startsWith('Opus')) {\n return (960 / sampleRate) * 1000\n }\n if (codec.startsWith('mp4a') || codec.startsWith('AAC') || codec.startsWith('aac')) {\n return (1024 / sampleRate) * 1000\n }\n if (codec.startsWith('flac') || codec.startsWith('fLaC')) {\n return (4096 / sampleRate) * 1000\n }\n throw new Error(`unknown audio codec for AU duration derivation: ${codec}`)\n}\n\n/**\n * Nominal picture duration in ms from framerate.\n */\nexport function pictureDurationMs(framerate: number): number {\n if (!(framerate > 0)) throw new Error('framerate must be > 0')\n return 1000 / framerate\n}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AK;AACxC,UAAM,SAAS,KAAK,YAAY,IAAI,IAAI,KAAK,gBAAgB;AAC7D,WAAO;AAAA,MACL,YAAY,KAAK;AAAA,MACjB;AAAA,MACA;AAAA,MACA,SAAS,UAAU;AAAA,MACnB,aAAa,KAAK;AAAA,MAClB,UAAU,KAAK;AAAA,IACjB;AAAA,EACF;AACF;AAEA,SAAS,MAAc;AAGrB,MAAI,OAAO,gBAAgB,eAAe,OAAO,YAAY,QAAQ,YAAY;AAC/E,WAAO,YAAY,IAAI;AAAA,EACzB;AACA,SAAO,KAAK,IAAI;AAClB;;;
|
|
4
|
+
"sourcesContent": ["/**\n * FrameCountPacer \u2014 media-agnostic wall-clock pacer for MMT A/V decode loops.\n *\n * Why not use MPU timestamps? Under FEC interleaving, encoders pack multiple\n * Access Units (AUs) per MMTP MPU with a single mpu_timestamp. The observed\n * timestamp delta between MPUs is a fraction of the real per-AU duration\n * (e.g. 10.22ms between MPUs for 21.33ms AAC frames at D=2 interleave).\n * Pacing on that delta sees \"media \u2248 wall\" and never sleeps, letting burst\n * relay delivery saturate the decoder / ring buffer.\n *\n * The Sony libatsc3 pattern (atsc3_mmt_context_mfu_depacketizer) solves this\n * by anchoring wall-clock time to the first received AU and counting AUs:\n * media_elapsed_ms = au_count \u00D7 nominal_au_duration_ms\n * The MPU timestamp is used elsewhere as an absolute-time anchor, never as\n * an incremental clock.\n *\n * The sleep threshold comes from the catalog's FEC interleaveDepthMs (via\n * FecBlockMapper.pacingThresholdMs). When no FEC is configured, a\n * NullBlockMapper yields threshold=0 and this pacer never sleeps.\n *\n * One pacer instance = one track (e.g. audio, video) on one player.\n */\n\nimport type { FecBlockMapper } from '@blockcast/mmt-fec'\n\nexport interface FrameCountPacerOpts {\n /** Required: AU duration (audio) or picture interval (video), in ms. */\n frameDurationMs: number\n /** FEC mapper from @blockcast/mmt-fec. NullBlockMapper disables pacing. */\n mapper: FecBlockMapper\n /**\n * Number of frames to let through before pacing starts. Lets startup\n * burst drain populate the ring buffer / decoder queue. Default 0.\n */\n warmupFrames?: number\n}\n\nexport interface PacerSnapshot {\n /** Frames observed (from notifyFrame). */\n frameCount: number\n /** Computed media time: frameCount \u00D7 frameDurationMs. */\n mediaMs: number\n /** Wall-clock time since anchor. */\n wallMs: number\n /** mediaMs - wallMs. Positive \u2192 decoder ahead of realtime. */\n aheadMs: number\n /** Pacing threshold (ms) from the FEC mapper. */\n thresholdMs: number\n /** Has the first frame been observed (wall anchor set)? */\n anchored: boolean\n}\n\nexport class FrameCountPacer {\n readonly frameDurationMs: number\n readonly thresholdMs: number\n readonly warmupFrames: number\n\n #frameCount = 0\n #anchorWallMs = 0\n #anchored = false\n\n constructor(opts: FrameCountPacerOpts) {\n if (!(opts.frameDurationMs > 0)) {\n throw new Error('frameDurationMs must be > 0')\n }\n this.frameDurationMs = opts.frameDurationMs\n this.thresholdMs = opts.mapper.pacingThresholdMs\n this.warmupFrames = Math.max(0, opts.warmupFrames ?? 0)\n }\n\n /** Call on every emitted decoded frame (AU or picture). Anchors on first call. */\n notifyFrame(): void {\n if (!this.#anchored) {\n this.#anchorWallMs = now()\n this.#anchored = true\n }\n this.#frameCount++\n }\n\n /**\n * Compute sleep ms before reading the next MoQ object.\n * Returns 0 when:\n * - pacing is disabled (NullBlockMapper \u2192 threshold=0)\n * - the anchor is not yet set\n * - still in warmup\n * - the caller reports a standing backlog (see `observedDelayMs`)\n * - decoder is not ahead of wall clock by more than threshold\n * Otherwise returns aheadMs - thresholdMs/2 (leaves half the threshold as headroom).\n *\n * `observedDelayMs` is how far this frame is behind the producer's own clock,\n * measured by the caller against a producer-supplied sending instant (for\n * MMTP, the header send-wall timestamp). It is optional because the anchored\n * rate below is blind to it: `#anchorWallMs` is the wall time of the FIRST\n * frame read, so `aheadMs` measures consumption against the join instant and\n * not against the producer. Once `aheadMs` reaches the threshold the loop\n * consumes at exactly the source rate, and recovering latency present at join\n * requires consuming FASTER than the source rate. So without this argument a\n * consumer that joined a live stream mid-block keeps that join latency for the\n * life of the subscription.\n *\n * A caller that can measure the real distance from the producer passes it and\n * gets a drain: while the backlog exceeds the threshold there is nothing to\n * pace against, so return 0 and let the transport set the rate. At the live\n * edge the anchored-rate behavior below applies unchanged.\n *\n * Callers with no producer clock omit it and keep the pure ATSC3-style\n * anchored pacing (see the class doc), where a first-frame anchor is correct\n * because a broadcast push has no live edge to catch up to.\n */\n computeSleepMs(observedDelayMs?: number): number {\n if (this.thresholdMs <= 0) return 0\n if (!this.#anchored) return 0\n if (this.#frameCount <= this.warmupFrames) return 0\n // Only a finite measurement can license the drain. NaN/Infinity mean the\n // caller could not measure the distance, which is not the same claim as\n // \"the consumer is at the edge\", so fall through to anchored pacing.\n if (observedDelayMs !== undefined && Number.isFinite(observedDelayMs) && observedDelayMs > this.thresholdMs) {\n return 0\n }\n\n const mediaMs = this.#frameCount * this.frameDurationMs\n const wallMs = now() - this.#anchorWallMs\n const aheadMs = mediaMs - wallMs\n if (aheadMs <= this.thresholdMs) return 0\n return aheadMs - this.thresholdMs / 2\n }\n\n /** Reset anchor + counter. Call on source switch (MoQ \u2194 multicast). */\n reset(): void {\n this.#frameCount = 0\n this.#anchorWallMs = 0\n this.#anchored = false\n }\n\n get snapshot(): PacerSnapshot {\n const mediaMs = this.#frameCount * this.frameDurationMs\n const wallMs = this.#anchored ? now() - this.#anchorWallMs : 0\n return {\n frameCount: this.#frameCount,\n mediaMs,\n wallMs,\n aheadMs: mediaMs - wallMs,\n thresholdMs: this.thresholdMs,\n anchored: this.#anchored,\n }\n }\n}\n\nfunction now(): number {\n // performance.now() is available in main thread, workers, and AudioWorklet.\n // Fall back to Date.now() for exotic runtimes (shouldn't happen in browsers).\n if (typeof performance !== 'undefined' && typeof performance.now === 'function') {\n return performance.now()\n }\n return Date.now()\n}\n", "/**\n * InterleaveTiming \u2014 per-AU presentation-time reconstruction.\n *\n * When encoders pack multiple AUs per MMTP MPU with a single mpu_timestamp\n * (normal for FEC-interleaved audio, and for video with multi-picture MPUs),\n * the raw mpu_timestamp is ambiguous for per-AU scheduling. The FEC block\n * math (draft-ramadan-moq-fec-00 \u00A78.3) combined with the Sony libatsc3\n * pattern recovers correct per-AU time:\n *\n * au_index_in_block = groupInBlock \u00D7 symbolsPerGroup + objectId\n * ssId = sbn \u00D7 K + au_index_in_block\n * presentationUs = anchorMpuUs + ssId \u00D7 frameDurationUs\n *\n * The anchor (anchorMpuUs) is the mpu_timestamp of the first observed AU.\n * Every downstream per-AU time is then deterministic from FEC coordinates\n * + codec frame duration \u2014 identical whether the AU arrives via MoQ unicast\n * (group_id/object_id) or multicast UDP (SS_ID from FEC Payload ID).\n *\n * Consumers: CMAF segment assembler (block boundary \u2192 segment cut points),\n * debug overlays, cross-path FEC combining, sync diagnostics.\n *\n * This is the `k-padded-v1` K-stride pacing model and nothing more: it is\n * presentation-time arithmetic for a stream whose blocks are K symbols and D\n * media groups long, NOT an FEC block locator. FecBlockMapper no longer maps\n * media groups to blocks (BLO-35597), so the stride math lives here, and a\n * stream under any other `fec.blockProfile` (an `iso-ssbg1-v1` block's length\n * is stated per block on the wire, so it has no stride) is refused.\n */\n\nimport type { FecBlockMapper } from '@blockcast/mmt-fec'\n\n/** 2^32: SS_ID is a 32-bit serial number (ISO/IEC 23008-1 C.5.2). */\nconst SS_ID_SPAN = 0x1_0000_0000\n\nexport interface InterleaveTimingOpts {\n /** Track's FEC mapper (provides K and D); must be `k-padded-v1`. */\n mapper: FecBlockMapper\n /** Nominal AU duration in microseconds. */\n frameDurationUs: number\n /** Wall-time anchor: the mpu_timestamp of the first observed AU (\u00B5s). */\n anchorMpuUs: number\n}\n\nexport class InterleaveTiming {\n readonly mapper: FecBlockMapper\n readonly frameDurationUs: number\n readonly anchorMpuUs: number\n /** Source symbols contributed by each media group: ceil(K / D). */\n readonly symbolsPerGroup: number\n\n constructor(opts: InterleaveTimingOpts) {\n if (!(opts.frameDurationUs > 0)) {\n throw new Error('frameDurationUs must be > 0')\n }\n if (!Number.isFinite(opts.anchorMpuUs)) {\n throw new Error('anchorMpuUs must be finite')\n }\n if (opts.mapper.blockProfile !== 'k-padded-v1') {\n throw new Error(\n `InterleaveTiming is the k-padded-v1 K-stride pacing model; a ${opts.mapper.blockProfile ?? 'non-FEC'} ` +\n 'stream has no K-stride block geometry',\n )\n }\n this.mapper = opts.mapper\n this.symbolsPerGroup = Math.ceil(opts.mapper.k / opts.mapper.d)\n this.frameDurationUs = opts.frameDurationUs\n this.anchorMpuUs = opts.anchorMpuUs\n }\n\n /**\n * Presentation time (\u00B5s) for an AU at MoQ coords (group_id, object_id).\n * Per draft \u00A78.3 + Sony anchor.\n */\n presentationUs(groupId: number, objectId: number): number {\n // \u00A78.3 under k-padded-v1: SBN = floor(G / D), ESI = groupInBlock \u00D7\n // symbolsPerGroup + O, SS_ID = SBN \u00D7 K + ESI (a 32-bit serial number;\n // reduce SBN first so the product stays exact).\n const { k, d } = this.mapper\n const sbn = Math.floor(groupId / d)\n const esi = (groupId - sbn * d) * this.symbolsPerGroup + objectId\n const ssId = ((sbn % SS_ID_SPAN) * k + esi) % SS_ID_SPAN\n return this.anchorMpuUs + ssId * this.frameDurationUs\n }\n\n /**\n * Presentation time (\u00B5s) for an AU identified by its flat SS_ID\n * (multicast FEC Payload ID path).\n */\n presentationUsFromSsId(ssId: number): number {\n return this.anchorMpuUs + ssId * this.frameDurationUs\n }\n\n /**\n * Inverse: which (group_id, object_id) AU is scheduled at the given\n * wall offset (\u00B5s) from anchor? Clamps to ssId\u22650.\n */\n frameAtOffset(offsetUs: number): { groupId: number; objectId: number; ssId: number } {\n const rawSsId = Math.max(0, Math.floor(offsetUs / this.frameDurationUs))\n // k-padded-v1: SBN = floor(SS_ID / K), ESI = SS_ID mod K.\n const sbn = Math.floor(rawSsId / this.mapper.k)\n const esi = rawSsId % this.mapper.k\n // Invert \u00A78.3: esi = groupInBlock * symbolsPerGroup + objectId\n const spg = this.symbolsPerGroup\n const groupInBlock = Math.floor(esi / spg)\n const objectId = esi - groupInBlock * spg\n const groupId = sbn * this.mapper.d + groupInBlock\n return { groupId, objectId, ssId: rawSsId }\n }\n\n /**\n * Block start/end in microseconds. Source blocks span `D \u00D7 GOP_duration`\n * of wall time; this returns the anchored wall range [startUs, endUs).\n */\n blockTimeRangeUs(sbn: number): { startUs: number; endUs: number } {\n const firstSsId = sbn * this.mapper.k\n const lastSsId = (sbn + 1) * this.mapper.k\n return {\n startUs: this.anchorMpuUs + firstSsId * this.frameDurationUs,\n endUs: this.anchorMpuUs + lastSsId * this.frameDurationUs,\n }\n }\n}\n", "/**\n * Nominal Access Unit duration helpers for common audio + video codecs.\n *\n * Kept in the pacing subpath (no mmt-container dep) so lean consumers like\n * moqtail's player.ts can import the pacer + AU duration without pulling\n * in the full audio-render-pipeline (which depends on @blockcast/mmt-container).\n */\n\n/**\n * Nominal AU duration in ms for common audio codecs at the given sample rate.\n * AAC-LC: 1024 samples. Opus (default 20ms): 960 samples @ 48kHz.\n * Throws for unknown codecs so callers supply correct timing.\n */\nexport function auDurationMsFromCodec(codec: string, sampleRate: number): number {\n if (!(sampleRate > 0)) throw new Error('sampleRate must be > 0')\n if (codec.startsWith('opus') || codec.startsWith('Opus')) {\n return (960 / sampleRate) * 1000\n }\n if (codec.startsWith('mp4a') || codec.startsWith('AAC') || codec.startsWith('aac')) {\n return (1024 / sampleRate) * 1000\n }\n if (codec.startsWith('flac') || codec.startsWith('fLaC')) {\n return (4096 / sampleRate) * 1000\n }\n throw new Error(`unknown audio codec for AU duration derivation: ${codec}`)\n}\n\n/**\n * Nominal picture duration in ms from framerate.\n */\nexport function pictureDurationMs(framerate: number): number {\n if (!(framerate > 0)) throw new Error('framerate must be > 0')\n return 1000 / framerate\n}\n"],
|
|
5
|
+
"mappings": ";AAoDO,IAAM,kBAAN,MAAsB;AAAA,EAK3B,cAAc;AAAA,EACd,gBAAgB;AAAA,EAChB,YAAY;AAAA,EAEZ,YAAY,MAA2B;AACrC,QAAI,EAAE,KAAK,kBAAkB,IAAI;AAC/B,YAAM,IAAI,MAAM,6BAA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FEC combining, sync diagnostics.
|
|
21
|
+
*
|
|
22
|
+
* This is the `k-padded-v1` K-stride pacing model and nothing more: it is
|
|
23
|
+
* presentation-time arithmetic for a stream whose blocks are K symbols and D
|
|
24
|
+
* media groups long, NOT an FEC block locator. FecBlockMapper no longer maps
|
|
25
|
+
* media groups to blocks (BLO-35597), so the stride math lives here, and a
|
|
26
|
+
* stream under any other `fec.blockProfile` (an `iso-ssbg1-v1` block's length
|
|
27
|
+
* is stated per block on the wire, so it has no stride) is refused.
|
|
21
28
|
*/
|
|
22
29
|
import type { FecBlockMapper } from '@blockcast/mmt-fec';
|
|
23
30
|
export interface InterleaveTimingOpts {
|
|
24
|
-
/** Track's FEC mapper (provides K
|
|
31
|
+
/** Track's FEC mapper (provides K and D); must be `k-padded-v1`. */
|
|
25
32
|
mapper: FecBlockMapper;
|
|
26
33
|
/** Nominal AU duration in microseconds. */
|
|
27
34
|
frameDurationUs: number;
|
|
@@ -32,6 +39,8 @@ export declare class InterleaveTiming {
|
|
|
32
39
|
readonly mapper: FecBlockMapper;
|
|
33
40
|
readonly frameDurationUs: number;
|
|
34
41
|
readonly anchorMpuUs: number;
|
|
42
|
+
/** Source symbols contributed by each media group: ceil(K / D). */
|
|
43
|
+
readonly symbolsPerGroup: number;
|
|
35
44
|
constructor(opts: InterleaveTimingOpts);
|
|
36
45
|
/**
|
|
37
46
|
* Presentation time (µs) for an AU at MoQ coords (group_id, object_id).
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"interleave-timing.d.ts","sourceRoot":"","sources":["../../src/pacing/interleave-timing.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"interleave-timing.d.ts","sourceRoot":"","sources":["../../src/pacing/interleave-timing.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AAEH,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAA;AAKxD,MAAM,WAAW,oBAAoB;IACnC,oEAAoE;IACpE,MAAM,EAAE,cAAc,CAAA;IACtB,2CAA2C;IAC3C,eAAe,EAAE,MAAM,CAAA;IACvB,yEAAyE;IACzE,WAAW,EAAE,MAAM,CAAA;CACpB;AAED,qBAAa,gBAAgB;IAC3B,QAAQ,CAAC,MAAM,EAAE,cAAc,CAAA;IAC/B,QAAQ,CAAC,eAAe,EAAE,MAAM,CAAA;IAChC,QAAQ,CAAC,WAAW,EAAE,MAAM,CAAA;IAC5B,mEAAmE;IACnE,QAAQ,CAAC,eAAe,EAAE,MAAM,CAAA;gBAEpB,IAAI,EAAE,oBAAoB;IAmBtC;;;OAGG;IACH,cAAc,CAAC,OAAO,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAG,MAAM;IAWzD;;;OAGG;IACH,sBAAsB,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM;IAI5C;;;OAGG;IACH,aAAa,CAAC,QAAQ,EAAE,MAAM,GAAG;QAAE,OAAO,EAAE,MAAM,CAAC;QAAC,QAAQ,EAAE,MAAM,CAAC;QAAC,IAAI,EAAE,MAAM,CAAA;KAAE;IAapF;;;OAGG;IACH,gBAAgB,CAAC,GAAG,EAAE,MAAM,GAAG;QAAE,OAAO,EAAE,MAAM,CAAC;QAAC,KAAK,EAAE,MAAM,CAAA;KAAE;CAQlE"}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
// src/pacing/interleave-timing.ts
|
|
2
|
+
var SS_ID_SPAN = 4294967296;
|
|
2
3
|
var InterleaveTiming = class {
|
|
3
4
|
constructor(opts) {
|
|
4
5
|
if (!(opts.frameDurationUs > 0)) {
|
|
@@ -7,7 +8,13 @@ var InterleaveTiming = class {
|
|
|
7
8
|
if (!Number.isFinite(opts.anchorMpuUs)) {
|
|
8
9
|
throw new Error("anchorMpuUs must be finite");
|
|
9
10
|
}
|
|
11
|
+
if (opts.mapper.blockProfile !== "k-padded-v1") {
|
|
12
|
+
throw new Error(
|
|
13
|
+
`InterleaveTiming is the k-padded-v1 K-stride pacing model; a ${opts.mapper.blockProfile ?? "non-FEC"} stream has no K-stride block geometry`
|
|
14
|
+
);
|
|
15
|
+
}
|
|
10
16
|
this.mapper = opts.mapper;
|
|
17
|
+
this.symbolsPerGroup = Math.ceil(opts.mapper.k / opts.mapper.d);
|
|
11
18
|
this.frameDurationUs = opts.frameDurationUs;
|
|
12
19
|
this.anchorMpuUs = opts.anchorMpuUs;
|
|
13
20
|
}
|
|
@@ -16,8 +23,11 @@ var InterleaveTiming = class {
|
|
|
16
23
|
* Per draft §8.3 + Sony anchor.
|
|
17
24
|
*/
|
|
18
25
|
presentationUs(groupId, objectId) {
|
|
19
|
-
const
|
|
20
|
-
|
|
26
|
+
const { k, d } = this.mapper;
|
|
27
|
+
const sbn = Math.floor(groupId / d);
|
|
28
|
+
const esi = (groupId - sbn * d) * this.symbolsPerGroup + objectId;
|
|
29
|
+
const ssId = (sbn % SS_ID_SPAN * k + esi) % SS_ID_SPAN;
|
|
30
|
+
return this.anchorMpuUs + ssId * this.frameDurationUs;
|
|
21
31
|
}
|
|
22
32
|
/**
|
|
23
33
|
* Presentation time (µs) for an AU identified by its flat SS_ID
|
|
@@ -32,8 +42,9 @@ var InterleaveTiming = class {
|
|
|
32
42
|
*/
|
|
33
43
|
frameAtOffset(offsetUs) {
|
|
34
44
|
const rawSsId = Math.max(0, Math.floor(offsetUs / this.frameDurationUs));
|
|
35
|
-
const
|
|
36
|
-
const
|
|
45
|
+
const sbn = Math.floor(rawSsId / this.mapper.k);
|
|
46
|
+
const esi = rawSsId % this.mapper.k;
|
|
47
|
+
const spg = this.symbolsPerGroup;
|
|
37
48
|
const groupInBlock = Math.floor(esi / spg);
|
|
38
49
|
const objectId = esi - groupInBlock * spg;
|
|
39
50
|
const groupId = sbn * this.mapper.d + groupInBlock;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/pacing/interleave-timing.ts"],
|
|
4
|
-
"sourcesContent": ["/**\n * InterleaveTiming \u2014 per-AU presentation-time reconstruction.\n *\n * When encoders pack multiple AUs per MMTP MPU with a single mpu_timestamp\n * (normal for FEC-interleaved audio, and for video with multi-picture MPUs),\n * the raw mpu_timestamp is ambiguous for per-AU scheduling. The FEC block\n * math (draft-ramadan-moq-fec-00 \u00A78.3) combined with the Sony libatsc3\n * pattern recovers correct per-AU time:\n *\n * au_index_in_block = groupInBlock \u00D7 symbolsPerGroup + objectId\n * ssId = sbn \u00D7 K + au_index_in_block\n * presentationUs = anchorMpuUs + ssId \u00D7 frameDurationUs\n *\n * The anchor (anchorMpuUs) is the mpu_timestamp of the first observed AU.\n * Every downstream per-AU time is then deterministic from FEC coordinates\n * + codec frame duration \u2014 identical whether the AU arrives via MoQ unicast\n * (group_id/object_id) or multicast UDP (SS_ID from FEC Payload ID).\n *\n * Consumers: CMAF segment assembler (block boundary \u2192 segment cut points),\n * debug overlays, cross-path FEC combining, sync diagnostics.\n */\n\nimport type { FecBlockMapper } from '@blockcast/mmt-fec'\n\nexport interface InterleaveTimingOpts {\n /** Track's FEC mapper (provides K
|
|
5
|
-
"mappings": ";
|
|
4
|
+
"sourcesContent": ["/**\n * InterleaveTiming \u2014 per-AU presentation-time reconstruction.\n *\n * When encoders pack multiple AUs per MMTP MPU with a single mpu_timestamp\n * (normal for FEC-interleaved audio, and for video with multi-picture MPUs),\n * the raw mpu_timestamp is ambiguous for per-AU scheduling. The FEC block\n * math (draft-ramadan-moq-fec-00 \u00A78.3) combined with the Sony libatsc3\n * pattern recovers correct per-AU time:\n *\n * au_index_in_block = groupInBlock \u00D7 symbolsPerGroup + objectId\n * ssId = sbn \u00D7 K + au_index_in_block\n * presentationUs = anchorMpuUs + ssId \u00D7 frameDurationUs\n *\n * The anchor (anchorMpuUs) is the mpu_timestamp of the first observed AU.\n * Every downstream per-AU time is then deterministic from FEC coordinates\n * + codec frame duration \u2014 identical whether the AU arrives via MoQ unicast\n * (group_id/object_id) or multicast UDP (SS_ID from FEC Payload ID).\n *\n * Consumers: CMAF segment assembler (block boundary \u2192 segment cut points),\n * debug overlays, cross-path FEC combining, sync diagnostics.\n *\n * This is the `k-padded-v1` K-stride pacing model and nothing more: it is\n * presentation-time arithmetic for a stream whose blocks are K symbols and D\n * media groups long, NOT an FEC block locator. FecBlockMapper no longer maps\n * media groups to blocks (BLO-35597), so the stride math lives here, and a\n * stream under any other `fec.blockProfile` (an `iso-ssbg1-v1` block's length\n * is stated per block on the wire, so it has no stride) is refused.\n */\n\nimport type { FecBlockMapper } from '@blockcast/mmt-fec'\n\n/** 2^32: SS_ID is a 32-bit serial number (ISO/IEC 23008-1 C.5.2). */\nconst SS_ID_SPAN = 0x1_0000_0000\n\nexport interface InterleaveTimingOpts {\n /** Track's FEC mapper (provides K and D); must be `k-padded-v1`. */\n mapper: FecBlockMapper\n /** Nominal AU duration in microseconds. */\n frameDurationUs: number\n /** Wall-time anchor: the mpu_timestamp of the first observed AU (\u00B5s). */\n anchorMpuUs: number\n}\n\nexport class InterleaveTiming {\n readonly mapper: FecBlockMapper\n readonly frameDurationUs: number\n readonly anchorMpuUs: number\n /** Source symbols contributed by each media group: ceil(K / D). */\n readonly symbolsPerGroup: number\n\n constructor(opts: InterleaveTimingOpts) {\n if (!(opts.frameDurationUs > 0)) {\n throw new Error('frameDurationUs must be > 0')\n }\n if (!Number.isFinite(opts.anchorMpuUs)) {\n throw new Error('anchorMpuUs must be finite')\n }\n if (opts.mapper.blockProfile !== 'k-padded-v1') {\n throw new Error(\n `InterleaveTiming is the k-padded-v1 K-stride pacing model; a ${opts.mapper.blockProfile ?? 'non-FEC'} ` +\n 'stream has no K-stride block geometry',\n )\n }\n this.mapper = opts.mapper\n this.symbolsPerGroup = Math.ceil(opts.mapper.k / opts.mapper.d)\n this.frameDurationUs = opts.frameDurationUs\n this.anchorMpuUs = opts.anchorMpuUs\n }\n\n /**\n * Presentation time (\u00B5s) for an AU at MoQ coords (group_id, object_id).\n * Per draft \u00A78.3 + Sony anchor.\n */\n presentationUs(groupId: number, objectId: number): number {\n // \u00A78.3 under k-padded-v1: SBN = floor(G / D), ESI = groupInBlock \u00D7\n // symbolsPerGroup + O, SS_ID = SBN \u00D7 K + ESI (a 32-bit serial number;\n // reduce SBN first so the product stays exact).\n const { k, d } = this.mapper\n const sbn = Math.floor(groupId / d)\n const esi = (groupId - sbn * d) * this.symbolsPerGroup + objectId\n const ssId = ((sbn % SS_ID_SPAN) * k + esi) % SS_ID_SPAN\n return this.anchorMpuUs + ssId * this.frameDurationUs\n }\n\n /**\n * Presentation time (\u00B5s) for an AU identified by its flat SS_ID\n * (multicast FEC Payload ID path).\n */\n presentationUsFromSsId(ssId: number): number {\n return this.anchorMpuUs + ssId * this.frameDurationUs\n }\n\n /**\n * Inverse: which (group_id, object_id) AU is scheduled at the given\n * wall offset (\u00B5s) from anchor? Clamps to ssId\u22650.\n */\n frameAtOffset(offsetUs: number): { groupId: number; objectId: number; ssId: number } {\n const rawSsId = Math.max(0, Math.floor(offsetUs / this.frameDurationUs))\n // k-padded-v1: SBN = floor(SS_ID / K), ESI = SS_ID mod K.\n const sbn = Math.floor(rawSsId / this.mapper.k)\n const esi = rawSsId % this.mapper.k\n // Invert \u00A78.3: esi = groupInBlock * symbolsPerGroup + objectId\n const spg = this.symbolsPerGroup\n const groupInBlock = Math.floor(esi / spg)\n const objectId = esi - groupInBlock * spg\n const groupId = sbn * this.mapper.d + groupInBlock\n return { groupId, objectId, ssId: rawSsId }\n }\n\n /**\n * Block start/end in microseconds. Source blocks span `D \u00D7 GOP_duration`\n * of wall time; this returns the anchored wall range [startUs, endUs).\n */\n blockTimeRangeUs(sbn: number): { startUs: number; endUs: number } {\n const firstSsId = sbn * this.mapper.k\n const lastSsId = (sbn + 1) * this.mapper.k\n return {\n startUs: this.anchorMpuUs + firstSsId * this.frameDurationUs,\n endUs: this.anchorMpuUs + lastSsId * this.frameDurationUs,\n }\n }\n}\n"],
|
|
5
|
+
"mappings": ";AAgCA,IAAM,aAAa;AA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,KAAK,OAAO,IAAI;AACtC,WAAO,EAAE,SAAS,UAAU,MAAM,QAAQ;AAAA,EAC5C;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,iBAAiB,KAAiD;AAChE,UAAM,YAAY,MAAM,KAAK,OAAO;AACpC,UAAM,YAAY,MAAM,KAAK,KAAK,OAAO;AACzC,WAAO;AAAA,MACL,SAAS,KAAK,cAAc,YAAY,KAAK;AAAA,MAC7C,OAAO,KAAK,cAAc,WAAW,KAAK;AAAA,IAC5C;AAAA,EACF;AACF;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@blockcast/mmt-render",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0-main.0bde85f7082f",
|
|
4
4
|
"description": "Shared MMT render pipeline (FEC-aware pacing, interleave timing, audio worklet) for MoQ players",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -73,8 +73,8 @@
|
|
|
73
73
|
"test:watch": "vitest"
|
|
74
74
|
},
|
|
75
75
|
"dependencies": {
|
|
76
|
-
"@blockcast/mmt-container": "0.
|
|
77
|
-
"@blockcast/mmt-fec": "0.
|
|
76
|
+
"@blockcast/mmt-container": "0.3.0-main.0bde85f7082f",
|
|
77
|
+
"@blockcast/mmt-fec": "0.3.0-main.0bde85f7082f"
|
|
78
78
|
},
|
|
79
79
|
"devDependencies": {
|
|
80
80
|
"@types/audioworklet": "^0.0.77",
|
|
@@ -18,12 +18,22 @@
|
|
|
18
18
|
*
|
|
19
19
|
* Consumers: CMAF segment assembler (block boundary → segment cut points),
|
|
20
20
|
* debug overlays, cross-path FEC combining, sync diagnostics.
|
|
21
|
+
*
|
|
22
|
+
* This is the `k-padded-v1` K-stride pacing model and nothing more: it is
|
|
23
|
+
* presentation-time arithmetic for a stream whose blocks are K symbols and D
|
|
24
|
+
* media groups long, NOT an FEC block locator. FecBlockMapper no longer maps
|
|
25
|
+
* media groups to blocks (BLO-35597), so the stride math lives here, and a
|
|
26
|
+
* stream under any other `fec.blockProfile` (an `iso-ssbg1-v1` block's length
|
|
27
|
+
* is stated per block on the wire, so it has no stride) is refused.
|
|
21
28
|
*/
|
|
22
29
|
|
|
23
30
|
import type { FecBlockMapper } from '@blockcast/mmt-fec'
|
|
24
31
|
|
|
32
|
+
/** 2^32: SS_ID is a 32-bit serial number (ISO/IEC 23008-1 C.5.2). */
|
|
33
|
+
const SS_ID_SPAN = 0x1_0000_0000
|
|
34
|
+
|
|
25
35
|
export interface InterleaveTimingOpts {
|
|
26
|
-
/** Track's FEC mapper (provides K
|
|
36
|
+
/** Track's FEC mapper (provides K and D); must be `k-padded-v1`. */
|
|
27
37
|
mapper: FecBlockMapper
|
|
28
38
|
/** Nominal AU duration in microseconds. */
|
|
29
39
|
frameDurationUs: number
|
|
@@ -35,6 +45,8 @@ export class InterleaveTiming {
|
|
|
35
45
|
readonly mapper: FecBlockMapper
|
|
36
46
|
readonly frameDurationUs: number
|
|
37
47
|
readonly anchorMpuUs: number
|
|
48
|
+
/** Source symbols contributed by each media group: ceil(K / D). */
|
|
49
|
+
readonly symbolsPerGroup: number
|
|
38
50
|
|
|
39
51
|
constructor(opts: InterleaveTimingOpts) {
|
|
40
52
|
if (!(opts.frameDurationUs > 0)) {
|
|
@@ -43,7 +55,14 @@ export class InterleaveTiming {
|
|
|
43
55
|
if (!Number.isFinite(opts.anchorMpuUs)) {
|
|
44
56
|
throw new Error('anchorMpuUs must be finite')
|
|
45
57
|
}
|
|
58
|
+
if (opts.mapper.blockProfile !== 'k-padded-v1') {
|
|
59
|
+
throw new Error(
|
|
60
|
+
`InterleaveTiming is the k-padded-v1 K-stride pacing model; a ${opts.mapper.blockProfile ?? 'non-FEC'} ` +
|
|
61
|
+
'stream has no K-stride block geometry',
|
|
62
|
+
)
|
|
63
|
+
}
|
|
46
64
|
this.mapper = opts.mapper
|
|
65
|
+
this.symbolsPerGroup = Math.ceil(opts.mapper.k / opts.mapper.d)
|
|
47
66
|
this.frameDurationUs = opts.frameDurationUs
|
|
48
67
|
this.anchorMpuUs = opts.anchorMpuUs
|
|
49
68
|
}
|
|
@@ -53,8 +72,14 @@ export class InterleaveTiming {
|
|
|
53
72
|
* Per draft §8.3 + Sony anchor.
|
|
54
73
|
*/
|
|
55
74
|
presentationUs(groupId: number, objectId: number): number {
|
|
56
|
-
|
|
57
|
-
|
|
75
|
+
// §8.3 under k-padded-v1: SBN = floor(G / D), ESI = groupInBlock ×
|
|
76
|
+
// symbolsPerGroup + O, SS_ID = SBN × K + ESI (a 32-bit serial number;
|
|
77
|
+
// reduce SBN first so the product stays exact).
|
|
78
|
+
const { k, d } = this.mapper
|
|
79
|
+
const sbn = Math.floor(groupId / d)
|
|
80
|
+
const esi = (groupId - sbn * d) * this.symbolsPerGroup + objectId
|
|
81
|
+
const ssId = ((sbn % SS_ID_SPAN) * k + esi) % SS_ID_SPAN
|
|
82
|
+
return this.anchorMpuUs + ssId * this.frameDurationUs
|
|
58
83
|
}
|
|
59
84
|
|
|
60
85
|
/**
|
|
@@ -71,9 +96,11 @@ export class InterleaveTiming {
|
|
|
71
96
|
*/
|
|
72
97
|
frameAtOffset(offsetUs: number): { groupId: number; objectId: number; ssId: number } {
|
|
73
98
|
const rawSsId = Math.max(0, Math.floor(offsetUs / this.frameDurationUs))
|
|
74
|
-
|
|
99
|
+
// k-padded-v1: SBN = floor(SS_ID / K), ESI = SS_ID mod K.
|
|
100
|
+
const sbn = Math.floor(rawSsId / this.mapper.k)
|
|
101
|
+
const esi = rawSsId % this.mapper.k
|
|
75
102
|
// Invert §8.3: esi = groupInBlock * symbolsPerGroup + objectId
|
|
76
|
-
const spg = this.
|
|
103
|
+
const spg = this.symbolsPerGroup
|
|
77
104
|
const groupInBlock = Math.floor(esi / spg)
|
|
78
105
|
const objectId = esi - groupInBlock * spg
|
|
79
106
|
const groupId = sbn * this.mapper.d + groupInBlock
|