@blockcast/mmt-render 0.1.0 → 0.3.0-main.2515e5ca4858
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 +8 -19
- package/dist/.build-stamp +0 -0
- package/dist/audio/audio-render-pipeline.js +217 -248
- package/dist/audio/audio-render-pipeline.js.map +2 -2
- package/dist/audio/audio-ring-buffer.js +93 -127
- package/dist/audio/audio-ring-buffer.js.map +1 -1
- package/dist/audio/index.js +313 -361
- package/dist/audio/index.js.map +3 -3
- package/dist/index.d.ts +1 -1
- package/dist/index.js.map +1 -1
- package/dist/pacing/frame-pacer.d.ts +22 -1
- package/dist/pacing/frame-pacer.d.ts.map +1 -1
- package/dist/pacing/frame-pacer.js +43 -39
- package/dist/pacing/frame-pacer.js.map +2 -2
- package/dist/pacing/index.d.ts +0 -2
- package/dist/pacing/index.d.ts.map +1 -1
- package/dist/pacing/index.js +43 -95
- package/dist/pacing/index.js.map +3 -3
- package/dist/video/index.js +129 -139
- package/dist/video/index.js.map +2 -2
- package/dist/video/keyframe-gate.js +5 -17
- package/dist/video/keyframe-gate.js.map +1 -1
- package/dist/video/video-render-pipeline.js +129 -139
- package/dist/video/video-render-pipeline.js.map +2 -2
- package/dist/worklet/render-worklet.js +171 -219
- package/dist/worklet/render-worklet.js.map +2 -2
- package/package.json +4 -8
- package/src/index.ts +1 -1
- package/src/pacing/frame-pacer.ts +28 -1
- package/src/pacing/index.ts +0 -2
- package/dist/pacing/interleave-timing.d.ts +0 -64
- package/dist/pacing/interleave-timing.d.ts.map +0 -1
- package/dist/pacing/interleave-timing.js +0 -58
- package/dist/pacing/interleave-timing.js.map +0 -7
- package/src/pacing/interleave-timing.ts +0 -95
package/dist/pacing/index.js
CHANGED
|
@@ -1,26 +1,9 @@
|
|
|
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 __privateWrapper = (obj, member, setter, getter) => ({
|
|
9
|
-
set _(value) {
|
|
10
|
-
__privateSet(obj, member, value, setter);
|
|
11
|
-
},
|
|
12
|
-
get _() {
|
|
13
|
-
return __privateGet(obj, member, getter);
|
|
14
|
-
}
|
|
15
|
-
});
|
|
16
|
-
|
|
17
1
|
// src/pacing/frame-pacer.ts
|
|
18
|
-
var _frameCount, _anchorWallMs, _anchored;
|
|
19
2
|
var FrameCountPacer = class {
|
|
3
|
+
#frameCount = 0;
|
|
4
|
+
#anchorWallMs = 0;
|
|
5
|
+
#anchored = false;
|
|
20
6
|
constructor(opts) {
|
|
21
|
-
__privateAdd(this, _frameCount, 0);
|
|
22
|
-
__privateAdd(this, _anchorWallMs, 0);
|
|
23
|
-
__privateAdd(this, _anchored, false);
|
|
24
7
|
if (!(opts.frameDurationMs > 0)) {
|
|
25
8
|
throw new Error("frameDurationMs must be > 0");
|
|
26
9
|
}
|
|
@@ -30,11 +13,11 @@ var FrameCountPacer = class {
|
|
|
30
13
|
}
|
|
31
14
|
/** Call on every emitted decoded frame (AU or picture). Anchors on first call. */
|
|
32
15
|
notifyFrame() {
|
|
33
|
-
if (!
|
|
34
|
-
|
|
35
|
-
|
|
16
|
+
if (!this.#anchored) {
|
|
17
|
+
this.#anchorWallMs = now();
|
|
18
|
+
this.#anchored = true;
|
|
36
19
|
}
|
|
37
|
-
|
|
20
|
+
this.#frameCount++;
|
|
38
21
|
}
|
|
39
22
|
/**
|
|
40
23
|
* Compute sleep ms before reading the next MoQ object.
|
|
@@ -42,41 +25,62 @@ var FrameCountPacer = class {
|
|
|
42
25
|
* - pacing is disabled (NullBlockMapper → threshold=0)
|
|
43
26
|
* - the anchor is not yet set
|
|
44
27
|
* - still in warmup
|
|
28
|
+
* - the caller reports a standing backlog (see `observedDelayMs`)
|
|
45
29
|
* - decoder is not ahead of wall clock by more than threshold
|
|
46
30
|
* Otherwise returns aheadMs - thresholdMs/2 (leaves half the threshold as headroom).
|
|
31
|
+
*
|
|
32
|
+
* `observedDelayMs` is how far this frame is behind the producer's own clock,
|
|
33
|
+
* measured by the caller against a producer-supplied sending instant (for
|
|
34
|
+
* MMTP, the header send-wall timestamp). It is optional because the anchored
|
|
35
|
+
* rate below is blind to it: `#anchorWallMs` is the wall time of the FIRST
|
|
36
|
+
* frame read, so `aheadMs` measures consumption against the join instant and
|
|
37
|
+
* not against the producer. Once `aheadMs` reaches the threshold the loop
|
|
38
|
+
* consumes at exactly the source rate, and recovering latency present at join
|
|
39
|
+
* requires consuming FASTER than the source rate. So without this argument a
|
|
40
|
+
* consumer that joined a live stream mid-block keeps that join latency for the
|
|
41
|
+
* life of the subscription.
|
|
42
|
+
*
|
|
43
|
+
* A caller that can measure the real distance from the producer passes it and
|
|
44
|
+
* gets a drain: while the backlog exceeds the threshold there is nothing to
|
|
45
|
+
* pace against, so return 0 and let the transport set the rate. At the live
|
|
46
|
+
* edge the anchored-rate behavior below applies unchanged.
|
|
47
|
+
*
|
|
48
|
+
* Callers with no producer clock omit it and keep the pure ATSC3-style
|
|
49
|
+
* anchored pacing (see the class doc), where a first-frame anchor is correct
|
|
50
|
+
* because a broadcast push has no live edge to catch up to.
|
|
47
51
|
*/
|
|
48
|
-
computeSleepMs() {
|
|
52
|
+
computeSleepMs(observedDelayMs) {
|
|
49
53
|
if (this.thresholdMs <= 0) return 0;
|
|
50
|
-
if (!
|
|
51
|
-
if (
|
|
52
|
-
|
|
53
|
-
|
|
54
|
+
if (!this.#anchored) return 0;
|
|
55
|
+
if (this.#frameCount <= this.warmupFrames) return 0;
|
|
56
|
+
if (observedDelayMs !== void 0 && Number.isFinite(observedDelayMs) && observedDelayMs > this.thresholdMs) {
|
|
57
|
+
return 0;
|
|
58
|
+
}
|
|
59
|
+
const mediaMs = this.#frameCount * this.frameDurationMs;
|
|
60
|
+
const wallMs = now() - this.#anchorWallMs;
|
|
54
61
|
const aheadMs = mediaMs - wallMs;
|
|
55
62
|
if (aheadMs <= this.thresholdMs) return 0;
|
|
56
63
|
return aheadMs - this.thresholdMs / 2;
|
|
57
64
|
}
|
|
58
65
|
/** Reset anchor + counter. Call on source switch (MoQ ↔ multicast). */
|
|
59
66
|
reset() {
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
67
|
+
this.#frameCount = 0;
|
|
68
|
+
this.#anchorWallMs = 0;
|
|
69
|
+
this.#anchored = false;
|
|
63
70
|
}
|
|
64
71
|
get snapshot() {
|
|
65
|
-
const mediaMs =
|
|
66
|
-
const wallMs =
|
|
72
|
+
const mediaMs = this.#frameCount * this.frameDurationMs;
|
|
73
|
+
const wallMs = this.#anchored ? now() - this.#anchorWallMs : 0;
|
|
67
74
|
return {
|
|
68
|
-
frameCount:
|
|
75
|
+
frameCount: this.#frameCount,
|
|
69
76
|
mediaMs,
|
|
70
77
|
wallMs,
|
|
71
78
|
aheadMs: mediaMs - wallMs,
|
|
72
79
|
thresholdMs: this.thresholdMs,
|
|
73
|
-
anchored:
|
|
80
|
+
anchored: this.#anchored
|
|
74
81
|
};
|
|
75
82
|
}
|
|
76
83
|
};
|
|
77
|
-
_frameCount = new WeakMap();
|
|
78
|
-
_anchorWallMs = new WeakMap();
|
|
79
|
-
_anchored = new WeakMap();
|
|
80
84
|
function now() {
|
|
81
85
|
if (typeof performance !== "undefined" && typeof performance.now === "function") {
|
|
82
86
|
return performance.now();
|
|
@@ -84,61 +88,6 @@ function now() {
|
|
|
84
88
|
return Date.now();
|
|
85
89
|
}
|
|
86
90
|
|
|
87
|
-
// src/pacing/interleave-timing.ts
|
|
88
|
-
var InterleaveTiming = class {
|
|
89
|
-
constructor(opts) {
|
|
90
|
-
if (!(opts.frameDurationUs > 0)) {
|
|
91
|
-
throw new Error("frameDurationUs must be > 0");
|
|
92
|
-
}
|
|
93
|
-
if (!Number.isFinite(opts.anchorMpuUs)) {
|
|
94
|
-
throw new Error("anchorMpuUs must be finite");
|
|
95
|
-
}
|
|
96
|
-
this.mapper = opts.mapper;
|
|
97
|
-
this.frameDurationUs = opts.frameDurationUs;
|
|
98
|
-
this.anchorMpuUs = opts.anchorMpuUs;
|
|
99
|
-
}
|
|
100
|
-
/**
|
|
101
|
-
* Presentation time (µs) for an AU at MoQ coords (group_id, object_id).
|
|
102
|
-
* Per draft §8.3 + Sony anchor.
|
|
103
|
-
*/
|
|
104
|
-
presentationUs(groupId, objectId) {
|
|
105
|
-
const coords = this.mapper.derive(groupId, objectId);
|
|
106
|
-
return this.anchorMpuUs + coords.ssId * this.frameDurationUs;
|
|
107
|
-
}
|
|
108
|
-
/**
|
|
109
|
-
* Presentation time (µs) for an AU identified by its flat SS_ID
|
|
110
|
-
* (multicast FEC Payload ID path).
|
|
111
|
-
*/
|
|
112
|
-
presentationUsFromSsId(ssId) {
|
|
113
|
-
return this.anchorMpuUs + ssId * this.frameDurationUs;
|
|
114
|
-
}
|
|
115
|
-
/**
|
|
116
|
-
* Inverse: which (group_id, object_id) AU is scheduled at the given
|
|
117
|
-
* wall offset (µs) from anchor? Clamps to ssId≥0.
|
|
118
|
-
*/
|
|
119
|
-
frameAtOffset(offsetUs) {
|
|
120
|
-
const rawSsId = Math.max(0, Math.floor(offsetUs / this.frameDurationUs));
|
|
121
|
-
const { sbn, esi } = this.mapper.fromSsId(rawSsId);
|
|
122
|
-
const spg = this.mapper.symbolsPerGroup;
|
|
123
|
-
const groupInBlock = Math.floor(esi / spg);
|
|
124
|
-
const objectId = esi - groupInBlock * spg;
|
|
125
|
-
const groupId = sbn * this.mapper.d + groupInBlock;
|
|
126
|
-
return { groupId, objectId, ssId: rawSsId };
|
|
127
|
-
}
|
|
128
|
-
/**
|
|
129
|
-
* Block start/end in microseconds. Source blocks span `D × GOP_duration`
|
|
130
|
-
* of wall time; this returns the anchored wall range [startUs, endUs).
|
|
131
|
-
*/
|
|
132
|
-
blockTimeRangeUs(sbn) {
|
|
133
|
-
const firstSsId = sbn * this.mapper.k;
|
|
134
|
-
const lastSsId = (sbn + 1) * this.mapper.k;
|
|
135
|
-
return {
|
|
136
|
-
startUs: this.anchorMpuUs + firstSsId * this.frameDurationUs,
|
|
137
|
-
endUs: this.anchorMpuUs + lastSsId * this.frameDurationUs
|
|
138
|
-
};
|
|
139
|
-
}
|
|
140
|
-
};
|
|
141
|
-
|
|
142
91
|
// src/pacing/au-duration.ts
|
|
143
92
|
function auDurationMsFromCodec(codec, sampleRate) {
|
|
144
93
|
if (!(sampleRate > 0)) throw new Error("sampleRate must be > 0");
|
|
@@ -159,7 +108,6 @@ function pictureDurationMs(framerate) {
|
|
|
159
108
|
}
|
|
160
109
|
export {
|
|
161
110
|
FrameCountPacer,
|
|
162
|
-
InterleaveTiming,
|
|
163
111
|
auDurationMsFromCodec,
|
|
164
112
|
pictureDurationMs
|
|
165
113
|
};
|
package/dist/pacing/index.js.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
|
-
"sources": ["../../src/pacing/frame-pacer.ts", "../../src/pacing/
|
|
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
|
|
5
|
-
"mappings": "
|
|
3
|
+
"sources": ["../../src/pacing/frame-pacer.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 * 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;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAgCA,eAAe,iBAAkC;AAC/C,QAAI,KAAK,eAAe,EAAG,QAAO;AAClC,QAAI,CAAC,KAAK,UAAW,QAAO;AAC5B,QAAI,KAAK,eAAe,KAAK,aAAc,QAAO;AAIlD,QAAI,oBAAoB,UAAa,OAAO,SAAS,eAAe,KAAK,kBAAkB,KAAK,aAAa;AAC3G,aAAO;AAAA,IACT;AAEA,UAAM,UAAU,KAAK,cAAc,KAAK;AACxC,UAAM,SAAS,IAAI,IAAI,KAAK;AAC5B,UAAM,UAAU,UAAU;AAC1B,QAAI,WAAW,KAAK,YAAa,QAAO;AACxC,WAAO,UAAU,KAAK,cAAc;AAAA,EACtC;AAAA;AAAA,EAGA,QAAc;AACZ,SAAK,cAAc;AACnB,SAAK,gBAAgB;AACrB,SAAK,YAAY;AAAA,EACnB;AAAA,EAEA,IAAI,WAA0B;AAC5B,UAAM,UAAU,KAAK,cAAc,KAAK;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;;;AC9IO,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
|
}
|
package/dist/video/index.js
CHANGED
|
@@ -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
|
-
|
|
22
|
-
__privateAdd(this, _open, false);
|
|
23
|
-
}
|
|
3
|
+
#open = false;
|
|
24
4
|
get isOpen() {
|
|
25
|
-
return
|
|
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 (
|
|
12
|
+
if (this.#open) return true;
|
|
33
13
|
if (keyframe) {
|
|
34
|
-
|
|
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
|
-
|
|
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 (!
|
|
66
|
-
|
|
67
|
-
|
|
43
|
+
if (!this.#anchored) {
|
|
44
|
+
this.#anchorWallMs = now();
|
|
45
|
+
this.#anchored = true;
|
|
68
46
|
}
|
|
69
|
-
|
|
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 (!
|
|
83
|
-
if (
|
|
84
|
-
|
|
85
|
-
|
|
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
|
-
|
|
93
|
-
|
|
94
|
-
|
|
94
|
+
this.#frameCount = 0;
|
|
95
|
+
this.#anchorWallMs = 0;
|
|
96
|
+
this.#anchored = false;
|
|
95
97
|
}
|
|
96
98
|
get snapshot() {
|
|
97
|
-
const mediaMs =
|
|
98
|
-
const wallMs =
|
|
99
|
+
const mediaMs = this.#frameCount * this.frameDurationMs;
|
|
100
|
+
const wallMs = this.#anchored ? now() - this.#anchorWallMs : 0;
|
|
99
101
|
return {
|
|
100
|
-
frameCount:
|
|
102
|
+
frameCount: this.#frameCount,
|
|
101
103
|
mediaMs,
|
|
102
104
|
wallMs,
|
|
103
105
|
aheadMs: mediaMs - wallMs,
|
|
104
106
|
thresholdMs: this.thresholdMs,
|
|
105
|
-
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
|
-
|
|
136
|
-
|
|
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
|
-
|
|
137
|
+
});
|
|
138
|
+
this.#init();
|
|
142
139
|
}
|
|
143
140
|
feedMoqObject(packet) {
|
|
144
|
-
|
|
141
|
+
this.#pipeline?.feedMoqObject(packet);
|
|
145
142
|
}
|
|
146
143
|
paceSleepMs() {
|
|
147
|
-
return
|
|
144
|
+
return this.#pacer.computeSleepMs();
|
|
148
145
|
}
|
|
149
146
|
get pacerSnapshot() {
|
|
150
|
-
return
|
|
147
|
+
return this.#pacer.snapshot;
|
|
151
148
|
}
|
|
152
149
|
/** Request a fresh keyframe after a decoder error (or source switch). */
|
|
153
150
|
reset() {
|
|
154
|
-
|
|
155
|
-
|
|
151
|
+
this.#gate.reset();
|
|
152
|
+
this.#pacer.reset();
|
|
156
153
|
try {
|
|
157
|
-
|
|
154
|
+
this.#decoder?.reset();
|
|
158
155
|
} catch {
|
|
159
156
|
}
|
|
160
|
-
|
|
157
|
+
this.#configureDecoder();
|
|
161
158
|
}
|
|
162
159
|
close() {
|
|
163
|
-
if (
|
|
164
|
-
|
|
160
|
+
if (this.#closed) return;
|
|
161
|
+
this.#closed = true;
|
|
165
162
|
try {
|
|
166
|
-
|
|
163
|
+
this.#decoder?.close();
|
|
167
164
|
} catch {
|
|
168
165
|
}
|
|
169
166
|
try {
|
|
170
|
-
|
|
167
|
+
this.#pipeline?.dispose();
|
|
171
168
|
} catch {
|
|
172
169
|
}
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
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
|
-
|
|
206
|
-
|
|
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
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
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,
|