@blockcast/mmt-render 0.1.0 → 0.3.0-main.36c5f6abe867

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.
@@ -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 * - 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 computeSleepMs(): number {\n if (this.thresholdMs <= 0) return 0\n if (!this.#anchored) return 0\n if (this.#frameCount <= this.warmupFrames) return 0\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}\n"],
5
- "mappings": ";;;;;;;;;;;;;;;;;AAAA;AAoDO,IAAM,kBAAN,MAAsB;AAAA,EAS3B,YAAY,MAA2B;AAJvC,oCAAc;AACd,sCAAgB;AAChB,kCAAY;AAGV,QAAI,EAAE,KAAK,kBAAkB,IAAI;AAC/B,YAAM,IAAI,MAAM,6BAA6B;AAAA,IAC/C;AACA,SAAK,kBAAkB,KAAK;AAC5B,SAAK,cAAc,KAAK,OAAO;AAC/B,SAAK,eAAe,KAAK,IAAI,GAAG,KAAK,gBAAgB,CAAC;AAAA,EACxD;AAAA;AAAA,EAGA,cAAoB;AAClB,QAAI,CAAC,mBAAK,YAAW;AACnB,yBAAK,eAAgB,IAAI;AACzB,yBAAK,WAAY;AAAA,IACnB;AACA,2BAAK,aAAL;AAAA,EACF;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAWA,iBAAyB;AACvB,QAAI,KAAK,eAAe,EAAG,QAAO;AAClC,QAAI,CAAC,mBAAK,WAAW,QAAO;AAC5B,QAAI,mBAAK,gBAAe,KAA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AE,YAAY,GAAI,OAAM,IAAI,MAAM,uBAAuB;AAC7D,SAAO,MAAO;AAChB;",
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,6BAA6B;AAAA,IAC/C;AACA,SAAK,kBAAkB,KAAK;AAC5B,SAAK,cAAc,KAAK,OAAO;AAC/B,SAAK,eAAe,KAAK,IAAI,GAAG,KAAK,gBAAgB,CAAC;AAAA,EACxD;AAAA;AAAA,EAGA,cAAoB;AAClB,QAAI,CAAC,KAAK,WAAW;AACnB,WAAK,gBAAgB,IAAI;AACzB,WAAK,YAAY;AAAA,IACnB;AACA,SAAK;AAAA,EACP;AAAA;AAAA;AAAA;AAAA;AAAA;AA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ChE,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;;;AC5GO,SAAS,sBAAsB,OAAe,YAA4B;AAC/E,MAAI,EAAE,aAAa,GAAI,OAAM,IAAI,MAAM,wBAAwB;AAC/D,MAAI,MAAM,WAAW,MAAM,KAAK,MAAM,WAAW,MAAM,GAAG;AACxD,WAAQ,MAAM,aAAc;AAAA,EAC9B;AACA,MAAI,MAAM,WAAW,MAAM,KAAK,MAAM,WAAW,KAAK,KAAK,MAAM,WAAW,KAAK,GAAG;AAClF,WAAQ,OAAO,aAAc;AAAA,EAC/B;AACA,MAAI,MAAM,WAAW,MAAM,KAAK,MAAM,WAAW,MAAM,GAAG;AACxD,WAAQ,OAAO,aAAc;AAAA,EAC/B;AACA,QAAM,IAAI,MAAM,mDAAmD,KAAK,EAAE;AAC5E;AAKO,SAAS,kBAAkB,WAA2B;AAC3D,MAAI,EAAE,YAAY,GAAI,OAAM,IAAI,MAAM,uBAAuB;AAC7D,SAAO,MAAO;AAChB;",
6
6
  "names": []
7
7
  }
@@ -18,10 +18,17 @@
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
  import type { FecBlockMapper } from '@blockcast/mmt-fec';
23
30
  export interface InterleaveTimingOpts {
24
- /** Track's FEC mapper (provides K, D, symbolsPerGroup). */
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;;;;;;;;;;;;;;;;;;;;GAoBG;AAEH,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAA;AAExD,MAAM,WAAW,oBAAoB;IACnC,2DAA2D;IAC3D,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;gBAEhB,IAAI,EAAE,oBAAoB;IAYtC;;;OAGG;IACH,cAAc,CAAC,OAAO,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAG,MAAM;IAKzD;;;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,C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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 coords = this.mapper.derive(groupId, objectId);
20
- return this.anchorMpuUs + coords.ssId * this.frameDurationUs;
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 { sbn, esi } = this.mapper.fromSsId(rawSsId);
36
- const spg = this.mapper.symbolsPerGroup;
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, 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"],
5
- "mappings": ";AAiCO,IAAM,mBAAN,MAAuB;AAAA,EAK5B,YAAY,MAA4B;AACtC,QAAI,EAAE,KAAK,kBAAkB,IAAI;AAC/B,YAAM,IAAI,MAAM,6BAA6B;AAAA,IAC/C;AACA,QAAI,CAAC,OAAO,SAAS,KAAK,WAAW,GAAG;AACtC,YAAM,IAAI,MAAM,4BAA4B;AAAA,IAC9C;AACA,SAAK,SAAS,KAAK;AACnB,SAAK,kBAAkB,KAAK;AAC5B,SAAK,cAAc,KAAK;AAAA,EAC1B;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,eAAe,SAAiB,UAA0B;AACxD,UAAM,SAAS,KAAK,OAAO,OAAO,SAAS,QAAQ;AACnD,WAAO,KAAK,cAAc,OAAO,OAAO,KAAK;AAAA,EAC/C;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,uBAAuB,MAAsB;AAC3C,WAAO,KAAK,cAAc,OAAO,KAAK;AAAA,EACxC;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,cAAc,UAAuE;AACnF,UAAM,UAAU,KAAK,IAAI,GAAG,KAAK,MAAM,WAAW,KAAK,eAAe,CAAC;AACvE,UAAM,EAAE,KAAK,IAAI,IAAI,KAAK,OAAO,SAAS,OAAO;AAEjD,UAAM,MAAM,KAAK,OAAO;AACxB,UAAM,eAAe,KAAK,MAAM,MAAM,GAAG;AACzC,UAAM,WAAW,MAAM,eAAe;AACtC,UAAM,UAAU,MAAM,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;",
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;AAWZ,IAAM,mBAAN,MAAuB;AAAA,EAO5B,YAAY,MAA4B;AACtC,QAAI,EAAE,KAAK,kBAAkB,IAAI;AAC/B,YAAM,IAAI,MAAM,6BAA6B;AAAA,IAC/C;AACA,QAAI,CAAC,OAAO,SAAS,KAAK,WAAW,GAAG;AACtC,YAAM,IAAI,MAAM,4BAA4B;AAAA,IAC9C;AACA,QAAI,KAAK,OAAO,iBAAiB,eAAe;AAC9C,YAAM,IAAI;AAAA,QACR,gEAAgE,KAAK,OAAO,gBAAgB,SAAS;AAAA,MAEvG;AAAA,IACF;AACA,SAAK,SAAS,KAAK;AACnB,SAAK,kBAAkB,KAAK,KAAK,KAAK,OAAO,IAAI,KAAK,OAAO,CAAC;AAC9D,SAAK,kBAAkB,KAAK;AAC5B,SAAK,cAAc,KAAK;AAAA,EAC1B;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,eAAe,SAAiB,UAA0B;AAIxD,UAAM,EAAE,GAAG,EAAE,IAAI,KAAK;AACtB,UAAM,MAAM,KAAK,MAAM,UAAU,CAAC;AAClC,UAAM,OAAO,UAAU,MAAM,KAAK,KAAK,kBAAkB;AACzD,UAAM,QAAS,MAAM,aAAc,IAAI,OAAO;AAC9C,WAAO,KAAK,cAAc,OAAO,KAAK;AAAA,EACxC;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,uBAAuB,MAAsB;AAC3C,WAAO,KAAK,cAAc,OAAO,KAAK;AAAA,EACxC;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,cAAc,UAAuE;AACnF,UAAM,UAAU,KAAK,IAAI,GAAG,KAAK,MAAM,WAAW,KAAK,eAAe,CAAC;AAEvE,UAAM,MAAM,KAAK,MAAM,UAAU,KAAK,OAAO,CAAC;AAC9C,UAAM,MAAM,UAAU,KAAK,OAAO;AAElC,UAAM,MAAM,KAAK;AACjB,UAAM,eAAe,KAAK,MAAM,MAAM,GAAG;AACzC,UAAM,WAAW,MAAM,eAAe;AACtC,UAAM,UAAU,MAAM,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
  }
@@ -1,58 +1,36 @@
1
- var __typeError = (msg) => {
2
- throw TypeError(msg);
3
- };
4
- var __accessCheck = (obj, member, msg) => member.has(obj) || __typeError("Cannot " + msg);
5
- var __privateGet = (obj, member, getter) => (__accessCheck(obj, member, "read from private field"), getter ? getter.call(obj) : member.get(obj));
6
- var __privateAdd = (obj, member, value) => member.has(obj) ? __typeError("Cannot add the same private member more than once") : member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
7
- var __privateSet = (obj, member, value, setter) => (__accessCheck(obj, member, "write to private field"), setter ? setter.call(obj, value) : member.set(obj, value), value);
8
- var __privateMethod = (obj, member, method) => (__accessCheck(obj, member, "access private method"), method);
9
- var __privateWrapper = (obj, member, setter, getter) => ({
10
- set _(value) {
11
- __privateSet(obj, member, value, setter);
12
- },
13
- get _() {
14
- return __privateGet(obj, member, getter);
15
- }
16
- });
17
-
18
1
  // src/video/keyframe-gate.ts
19
- var _open;
20
2
  var KeyframeGate = class {
21
- constructor() {
22
- __privateAdd(this, _open, false);
23
- }
3
+ #open = false;
24
4
  get isOpen() {
25
- return __privateGet(this, _open);
5
+ return this.#open;
26
6
  }
27
7
  /**
28
8
  * Decide whether to pass a frame through.
29
9
  * Opens permanently once the first keyframe is seen.
30
10
  */
31
11
  allow(keyframe) {
32
- if (__privateGet(this, _open)) return true;
12
+ if (this.#open) return true;
33
13
  if (keyframe) {
34
- __privateSet(this, _open, true);
14
+ this.#open = true;
35
15
  return true;
36
16
  }
37
17
  return false;
38
18
  }
39
19
  /** Re-close the gate. Next keyframe reopens it. */
40
20
  reset() {
41
- __privateSet(this, _open, false);
21
+ this.#open = false;
42
22
  }
43
23
  };
44
- _open = new WeakMap();
45
24
 
46
25
  // src/video/video-render-pipeline.ts
47
26
  import { MmtpDecodePipeline } from "@blockcast/mmt-container/mmtp-decode-pipeline";
48
27
 
49
28
  // src/pacing/frame-pacer.ts
50
- var _frameCount, _anchorWallMs, _anchored;
51
29
  var FrameCountPacer = class {
30
+ #frameCount = 0;
31
+ #anchorWallMs = 0;
32
+ #anchored = false;
52
33
  constructor(opts) {
53
- __privateAdd(this, _frameCount, 0);
54
- __privateAdd(this, _anchorWallMs, 0);
55
- __privateAdd(this, _anchored, false);
56
34
  if (!(opts.frameDurationMs > 0)) {
57
35
  throw new Error("frameDurationMs must be > 0");
58
36
  }
@@ -62,11 +40,11 @@ var FrameCountPacer = class {
62
40
  }
63
41
  /** Call on every emitted decoded frame (AU or picture). Anchors on first call. */
64
42
  notifyFrame() {
65
- if (!__privateGet(this, _anchored)) {
66
- __privateSet(this, _anchorWallMs, now());
67
- __privateSet(this, _anchored, true);
43
+ if (!this.#anchored) {
44
+ this.#anchorWallMs = now();
45
+ this.#anchored = true;
68
46
  }
69
- __privateWrapper(this, _frameCount)._++;
47
+ this.#frameCount++;
70
48
  }
71
49
  /**
72
50
  * Compute sleep ms before reading the next MoQ object.
@@ -74,41 +52,62 @@ var FrameCountPacer = class {
74
52
  * - pacing is disabled (NullBlockMapper → threshold=0)
75
53
  * - the anchor is not yet set
76
54
  * - still in warmup
55
+ * - the caller reports a standing backlog (see `observedDelayMs`)
77
56
  * - decoder is not ahead of wall clock by more than threshold
78
57
  * Otherwise returns aheadMs - thresholdMs/2 (leaves half the threshold as headroom).
58
+ *
59
+ * `observedDelayMs` is how far this frame is behind the producer's own clock,
60
+ * measured by the caller against a producer-supplied sending instant (for
61
+ * MMTP, the header send-wall timestamp). It is optional because the anchored
62
+ * rate below is blind to it: `#anchorWallMs` is the wall time of the FIRST
63
+ * frame read, so `aheadMs` measures consumption against the join instant and
64
+ * not against the producer. Once `aheadMs` reaches the threshold the loop
65
+ * consumes at exactly the source rate, and recovering latency present at join
66
+ * requires consuming FASTER than the source rate. So without this argument a
67
+ * consumer that joined a live stream mid-block keeps that join latency for the
68
+ * life of the subscription.
69
+ *
70
+ * A caller that can measure the real distance from the producer passes it and
71
+ * gets a drain: while the backlog exceeds the threshold there is nothing to
72
+ * pace against, so return 0 and let the transport set the rate. At the live
73
+ * edge the anchored-rate behavior below applies unchanged.
74
+ *
75
+ * Callers with no producer clock omit it and keep the pure ATSC3-style
76
+ * anchored pacing (see the class doc), where a first-frame anchor is correct
77
+ * because a broadcast push has no live edge to catch up to.
79
78
  */
80
- computeSleepMs() {
79
+ computeSleepMs(observedDelayMs) {
81
80
  if (this.thresholdMs <= 0) return 0;
82
- if (!__privateGet(this, _anchored)) return 0;
83
- if (__privateGet(this, _frameCount) <= this.warmupFrames) return 0;
84
- const mediaMs = __privateGet(this, _frameCount) * this.frameDurationMs;
85
- const wallMs = now() - __privateGet(this, _anchorWallMs);
81
+ if (!this.#anchored) return 0;
82
+ if (this.#frameCount <= this.warmupFrames) return 0;
83
+ if (observedDelayMs !== void 0 && Number.isFinite(observedDelayMs) && observedDelayMs > this.thresholdMs) {
84
+ return 0;
85
+ }
86
+ const mediaMs = this.#frameCount * this.frameDurationMs;
87
+ const wallMs = now() - this.#anchorWallMs;
86
88
  const aheadMs = mediaMs - wallMs;
87
89
  if (aheadMs <= this.thresholdMs) return 0;
88
90
  return aheadMs - this.thresholdMs / 2;
89
91
  }
90
92
  /** Reset anchor + counter. Call on source switch (MoQ ↔ multicast). */
91
93
  reset() {
92
- __privateSet(this, _frameCount, 0);
93
- __privateSet(this, _anchorWallMs, 0);
94
- __privateSet(this, _anchored, false);
94
+ this.#frameCount = 0;
95
+ this.#anchorWallMs = 0;
96
+ this.#anchored = false;
95
97
  }
96
98
  get snapshot() {
97
- const mediaMs = __privateGet(this, _frameCount) * this.frameDurationMs;
98
- const wallMs = __privateGet(this, _anchored) ? now() - __privateGet(this, _anchorWallMs) : 0;
99
+ const mediaMs = this.#frameCount * this.frameDurationMs;
100
+ const wallMs = this.#anchored ? now() - this.#anchorWallMs : 0;
99
101
  return {
100
- frameCount: __privateGet(this, _frameCount),
102
+ frameCount: this.#frameCount,
101
103
  mediaMs,
102
104
  wallMs,
103
105
  aheadMs: mediaMs - wallMs,
104
106
  thresholdMs: this.thresholdMs,
105
- anchored: __privateGet(this, _anchored)
107
+ anchored: this.#anchored
106
108
  };
107
109
  }
108
110
  };
109
- _frameCount = new WeakMap();
110
- _anchorWallMs = new WeakMap();
111
- _anchored = new WeakMap();
112
111
  function now() {
113
112
  if (typeof performance !== "undefined" && typeof performance.now === "function") {
114
113
  return performance.now();
@@ -121,124 +120,115 @@ function deriveVideoReorderWindow(framerate, mapper) {
121
120
  const framesPerGroup = Math.ceil(framerate * mapper.gopDurationMs / 1e3);
122
121
  return mapper.d * framesPerGroup;
123
122
  }
124
- var _pacer, _gate, _opts, _decoder, _pipeline, _closed, _VideoRenderPipeline_instances, init_fn, configureDecoder_fn;
125
123
  var VideoRenderPipeline = class {
124
+ #pacer;
125
+ #gate = new KeyframeGate();
126
+ #opts;
127
+ #decoder = null;
128
+ #pipeline = null;
129
+ #closed = false;
126
130
  constructor(opts) {
127
- __privateAdd(this, _VideoRenderPipeline_instances);
128
- __privateAdd(this, _pacer);
129
- __privateAdd(this, _gate, new KeyframeGate());
130
- __privateAdd(this, _opts);
131
- __privateAdd(this, _decoder, null);
132
- __privateAdd(this, _pipeline, null);
133
- __privateAdd(this, _closed, false);
134
131
  if (!(opts.framerate > 0)) throw new Error("framerate must be > 0");
135
- __privateSet(this, _opts, opts);
136
- __privateSet(this, _pacer, new FrameCountPacer({
132
+ this.#opts = opts;
133
+ this.#pacer = new FrameCountPacer({
137
134
  frameDurationMs: 1e3 / opts.framerate,
138
135
  mapper: opts.mapper,
139
136
  warmupFrames: opts.warmupFrames ?? 3
140
- }));
141
- __privateMethod(this, _VideoRenderPipeline_instances, init_fn).call(this);
137
+ });
138
+ this.#init();
142
139
  }
143
140
  feedMoqObject(packet) {
144
- __privateGet(this, _pipeline)?.feedMoqObject(packet);
141
+ this.#pipeline?.feedMoqObject(packet);
145
142
  }
146
143
  paceSleepMs() {
147
- return __privateGet(this, _pacer).computeSleepMs();
144
+ return this.#pacer.computeSleepMs();
148
145
  }
149
146
  get pacerSnapshot() {
150
- return __privateGet(this, _pacer).snapshot;
147
+ return this.#pacer.snapshot;
151
148
  }
152
149
  /** Request a fresh keyframe after a decoder error (or source switch). */
153
150
  reset() {
154
- __privateGet(this, _gate).reset();
155
- __privateGet(this, _pacer).reset();
151
+ this.#gate.reset();
152
+ this.#pacer.reset();
156
153
  try {
157
- __privateGet(this, _decoder)?.reset();
154
+ this.#decoder?.reset();
158
155
  } catch {
159
156
  }
160
- __privateMethod(this, _VideoRenderPipeline_instances, configureDecoder_fn).call(this);
157
+ this.#configureDecoder();
161
158
  }
162
159
  close() {
163
- if (__privateGet(this, _closed)) return;
164
- __privateSet(this, _closed, true);
160
+ if (this.#closed) return;
161
+ this.#closed = true;
165
162
  try {
166
- __privateGet(this, _decoder)?.close();
163
+ this.#decoder?.close();
167
164
  } catch {
168
165
  }
169
166
  try {
170
- __privateGet(this, _pipeline)?.dispose();
167
+ this.#pipeline?.dispose();
171
168
  } catch {
172
169
  }
173
- __privateSet(this, _decoder, null);
174
- __privateSet(this, _pipeline, null);
175
- }
176
- };
177
- _pacer = new WeakMap();
178
- _gate = new WeakMap();
179
- _opts = new WeakMap();
180
- _decoder = new WeakMap();
181
- _pipeline = new WeakMap();
182
- _closed = new WeakMap();
183
- _VideoRenderPipeline_instances = new WeakSet();
184
- // ─── Private ─────────────────────────────────────────────────────
185
- init_fn = function() {
186
- __privateMethod(this, _VideoRenderPipeline_instances, configureDecoder_fn).call(this);
187
- const pipeline = new MmtpDecodePipeline();
188
- const reorderWindow = __privateGet(this, _opts).reorderWindow ?? deriveVideoReorderWindow(__privateGet(this, _opts).framerate, __privateGet(this, _opts).mapper);
189
- pipeline.configure({
190
- codec: __privateGet(this, _opts).codec,
191
- packetId: __privateGet(this, _opts).packetId,
192
- description: __privateGet(this, _opts).description,
193
- reorderWindow,
194
- framerate: __privateGet(this, _opts).framerate,
195
- groupDurationMs: __privateGet(this, _opts).mapper.gopDurationMs,
196
- reassemblyPolicy: __privateGet(this, _opts).reassemblyPolicy
197
- });
198
- if (__privateGet(this, _opts).fecClient) pipeline.setFecClient(__privateGet(this, _opts).fecClient);
199
- pipeline.onFrame((codecData, meta) => {
200
- if (__privateGet(this, _closed) || !__privateGet(this, _decoder)) return;
201
- if (codecData.byteLength === 0) return;
202
- if (!__privateGet(this, _gate).allow(meta.keyframe)) return;
203
- __privateGet(this, _pacer).notifyFrame();
170
+ this.#decoder = null;
171
+ this.#pipeline = null;
172
+ }
173
+ // ─── Private ─────────────────────────────────────────────────────
174
+ #init() {
175
+ this.#configureDecoder();
176
+ const pipeline = new MmtpDecodePipeline();
177
+ const reorderWindow = this.#opts.reorderWindow ?? deriveVideoReorderWindow(this.#opts.framerate, this.#opts.mapper);
178
+ pipeline.configure({
179
+ codec: this.#opts.codec,
180
+ packetId: this.#opts.packetId,
181
+ description: this.#opts.description,
182
+ reorderWindow,
183
+ framerate: this.#opts.framerate,
184
+ groupDurationMs: this.#opts.mapper.gopDurationMs,
185
+ reassemblyPolicy: this.#opts.reassemblyPolicy
186
+ });
187
+ if (this.#opts.fecClient) pipeline.setFecClient(this.#opts.fecClient);
188
+ pipeline.onFrame((codecData, meta) => {
189
+ if (this.#closed || !this.#decoder) return;
190
+ if (codecData.byteLength === 0) return;
191
+ if (!this.#gate.allow(meta.keyframe)) return;
192
+ this.#pacer.notifyFrame();
193
+ try {
194
+ this.#decoder.decode(new EncodedVideoChunk({
195
+ type: meta.keyframe ? "key" : "delta",
196
+ data: codecData,
197
+ timestamp: meta.timestamp
198
+ }));
199
+ } catch (err) {
200
+ this.#opts.onError?.(err);
201
+ this.reset();
202
+ }
203
+ });
204
+ this.#pipeline = pipeline;
205
+ }
206
+ #configureDecoder() {
204
207
  try {
205
- __privateGet(this, _decoder).decode(new EncodedVideoChunk({
206
- type: meta.keyframe ? "key" : "delta",
207
- data: codecData,
208
- timestamp: meta.timestamp
209
- }));
210
- } catch (err) {
211
- __privateGet(this, _opts).onError?.(err);
212
- this.reset();
213
- }
214
- });
215
- __privateSet(this, _pipeline, pipeline);
216
- };
217
- configureDecoder_fn = function() {
218
- try {
219
- __privateGet(this, _decoder)?.close();
220
- } catch {
221
- }
222
- const userMeta = { timestamp: 0, keyframe: false, packetId: 0 };
223
- const decoder = new VideoDecoder({
224
- output: (frame) => {
225
- userMeta.timestamp = frame.timestamp;
226
- __privateGet(this, _opts).onFrame(frame, userMeta);
227
- },
228
- error: (err) => {
229
- __privateGet(this, _opts).onError?.(err);
230
- __privateGet(this, _gate).reset();
208
+ this.#decoder?.close();
209
+ } catch {
231
210
  }
232
- });
233
- const config = {
234
- codec: __privateGet(this, _opts).codec,
235
- codedWidth: __privateGet(this, _opts).width,
236
- codedHeight: __privateGet(this, _opts).height,
237
- ...__privateGet(this, _opts).description ? { description: __privateGet(this, _opts).description } : {},
238
- ...__privateGet(this, _opts).hardwareAcceleration ? { hardwareAcceleration: __privateGet(this, _opts).hardwareAcceleration } : {}
239
- };
240
- decoder.configure(config);
241
- __privateSet(this, _decoder, decoder);
211
+ const userMeta = { timestamp: 0, keyframe: false, packetId: 0 };
212
+ const decoder = new VideoDecoder({
213
+ output: (frame) => {
214
+ userMeta.timestamp = frame.timestamp;
215
+ this.#opts.onFrame(frame, userMeta);
216
+ },
217
+ error: (err) => {
218
+ this.#opts.onError?.(err);
219
+ this.#gate.reset();
220
+ }
221
+ });
222
+ const config = {
223
+ codec: this.#opts.codec,
224
+ codedWidth: this.#opts.width,
225
+ codedHeight: this.#opts.height,
226
+ ...this.#opts.description ? { description: this.#opts.description } : {},
227
+ ...this.#opts.hardwareAcceleration ? { hardwareAcceleration: this.#opts.hardwareAcceleration } : {}
228
+ };
229
+ decoder.configure(config);
230
+ this.#decoder = decoder;
231
+ }
242
232
  };
243
233
  export {
244
234
  KeyframeGate,