werift 0.25.1 → 0.25.2
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/lib/index.mjs +4101 -1781
- package/lib/nonstandard/index.mjs +3582 -1221
- package/lib/polyfill/dom.mjs +16711 -17852
- package/lib/polyfill/index.mjs +16711 -17852
- package/lib/rtp/src/codec/av1.d.ts +61 -1
- package/lib/rtp/src/codec/av1.js +332 -33
- package/lib/rtp/src/codec/av1.js.map +1 -1
- package/lib/rtp/src/codec/base.d.ts +49 -1
- package/lib/rtp/src/codec/base.js +81 -1
- package/lib/rtp/src/codec/base.js.map +1 -1
- package/lib/rtp/src/codec/g711.d.ts +37 -0
- package/lib/rtp/src/codec/g711.js +109 -0
- package/lib/rtp/src/codec/g711.js.map +1 -0
- package/lib/rtp/src/codec/g722.d.ts +31 -0
- package/lib/rtp/src/codec/g722.js +82 -0
- package/lib/rtp/src/codec/g722.js.map +1 -0
- package/lib/rtp/src/codec/h264.d.ts +81 -7
- package/lib/rtp/src/codec/h264.js +382 -21
- package/lib/rtp/src/codec/h264.js.map +1 -1
- package/lib/rtp/src/codec/h265.d.ts +101 -0
- package/lib/rtp/src/codec/h265.js +615 -0
- package/lib/rtp/src/codec/h265.js.map +1 -0
- package/lib/rtp/src/codec/index.d.ts +6 -1
- package/lib/rtp/src/codec/index.js +25 -0
- package/lib/rtp/src/codec/index.js.map +1 -1
- package/lib/rtp/src/codec/mp4a.d.ts +88 -0
- package/lib/rtp/src/codec/mp4a.js +386 -0
- package/lib/rtp/src/codec/mp4a.js.map +1 -0
- package/lib/rtp/src/codec/opus.d.ts +14 -2
- package/lib/rtp/src/codec/opus.js +32 -2
- package/lib/rtp/src/codec/opus.js.map +1 -1
- package/lib/rtp/src/codec/telephoneEvent.d.ts +102 -0
- package/lib/rtp/src/codec/telephoneEvent.js +176 -0
- package/lib/rtp/src/codec/telephoneEvent.js.map +1 -0
- package/lib/rtp/src/codec/vp8.d.ts +26 -1
- package/lib/rtp/src/codec/vp8.js +76 -2
- package/lib/rtp/src/codec/vp8.js.map +1 -1
- package/lib/rtp/src/codec/vp9.d.ts +25 -1
- package/lib/rtp/src/codec/vp9.js +64 -2
- package/lib/rtp/src/codec/vp9.js.map +1 -1
- package/lib/rtp/src/const.d.ts +1 -0
- package/lib/rtp/src/const.js +5 -0
- package/lib/rtp/src/const.js.map +1 -0
- package/lib/rtp/src/extra/container/mp4/container.js +4 -1
- package/lib/rtp/src/extra/container/mp4/container.js.map +1 -1
- package/lib/rtp/src/extra/container/mp4/h264.d.ts +26 -0
- package/lib/rtp/src/extra/container/mp4/h264.js +54 -0
- package/lib/rtp/src/extra/container/mp4/h264.js.map +1 -1
- package/lib/rtp/src/extra/processor/mp4.d.ts +16 -0
- package/lib/rtp/src/extra/processor/mp4.js +54 -20
- package/lib/rtp/src/extra/processor/mp4.js.map +1 -1
- package/lib/rtp/src/index.d.ts +1 -0
- package/lib/rtp/src/index.js +1 -0
- package/lib/rtp/src/index.js.map +1 -1
- package/lib/rtp/src/rtp/mediaClock.d.ts +283 -0
- package/lib/rtp/src/rtp/mediaClock.js +655 -0
- package/lib/rtp/src/rtp/mediaClock.js.map +1 -0
- package/lib/rtp/src/util.d.ts +59 -6
- package/lib/rtp/src/util.js +88 -10
- package/lib/rtp/src/util.js.map +1 -1
- package/lib/webrtc/src/media/codec.d.ts +18 -0
- package/lib/webrtc/src/media/codec.js +43 -2
- package/lib/webrtc/src/media/codec.js.map +1 -1
- package/lib/webrtc/src/media/codecCompatibility.d.ts +5 -0
- package/lib/webrtc/src/media/codecCompatibility.js +15 -4
- package/lib/webrtc/src/media/codecCompatibility.js.map +1 -1
- package/lib/webrtc/src/nonstandard/dummyMedia.js +20 -6
- package/lib/webrtc/src/nonstandard/dummyMedia.js.map +1 -1
- package/lib/webrtc/src/nonstandard/userMedia/packetizer.d.ts +2 -2
- package/lib/webrtc/src/nonstandard/userMedia/packetizer.js +61 -306
- package/lib/webrtc/src/nonstandard/userMedia/packetizer.js.map +1 -1
- package/lib/webrtc/src/nonstandard/userMedia.d.ts +2 -0
- package/lib/webrtc/src/nonstandard/userMedia.js +41 -7
- package/lib/webrtc/src/nonstandard/userMedia.js.map +1 -1
- package/lib/webrtc/src/peerConnection.js +3 -1
- package/lib/webrtc/src/peerConnection.js.map +1 -1
- package/lib/webrtc/src/sdpManager.js +3 -2
- package/lib/webrtc/src/sdpManager.js.map +1 -1
- package/lib/webrtc/src/transceiverManager.js +2 -1
- package/lib/webrtc/src/transceiverManager.js.map +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,655 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.RtpMediaPacer = exports.RtpMediaClock = exports.RtpMediaTimeline = void 0;
|
|
4
|
+
const src_1 = require("../../../common/src");
|
|
5
|
+
const UINT32_MODULO = 4294967296;
|
|
6
|
+
/** Absorbs floating point noise when `now` lands exactly on a deadline. */
|
|
7
|
+
const SLOT_EPSILON = 1e-9;
|
|
8
|
+
/**
|
|
9
|
+
* Pure, deterministic RTP media timeline.
|
|
10
|
+
*
|
|
11
|
+
* All values are computed absolutely from the frame index, so no error
|
|
12
|
+
* accumulates no matter how long the timeline runs.
|
|
13
|
+
*/
|
|
14
|
+
class RtpMediaTimeline {
|
|
15
|
+
constructor(options) {
|
|
16
|
+
Object.defineProperty(this, "clockRate", {
|
|
17
|
+
enumerable: true,
|
|
18
|
+
configurable: true,
|
|
19
|
+
writable: true,
|
|
20
|
+
value: void 0
|
|
21
|
+
});
|
|
22
|
+
Object.defineProperty(this, "frameSamples", {
|
|
23
|
+
enumerable: true,
|
|
24
|
+
configurable: true,
|
|
25
|
+
writable: true,
|
|
26
|
+
value: void 0
|
|
27
|
+
});
|
|
28
|
+
Object.defineProperty(this, "frameDurationMs", {
|
|
29
|
+
enumerable: true,
|
|
30
|
+
configurable: true,
|
|
31
|
+
writable: true,
|
|
32
|
+
value: void 0
|
|
33
|
+
});
|
|
34
|
+
Object.defineProperty(this, "initialTimestamp", {
|
|
35
|
+
enumerable: true,
|
|
36
|
+
configurable: true,
|
|
37
|
+
writable: true,
|
|
38
|
+
value: void 0
|
|
39
|
+
});
|
|
40
|
+
const { clockRate, frameSamples, frameDurationMs } = options;
|
|
41
|
+
assertPositive("clockRate", clockRate);
|
|
42
|
+
if ((frameSamples === undefined) === (frameDurationMs === undefined)) {
|
|
43
|
+
throw new TypeError("exactly one of frameSamples or frameDurationMs must be specified");
|
|
44
|
+
}
|
|
45
|
+
this.clockRate = clockRate;
|
|
46
|
+
if (frameSamples !== undefined) {
|
|
47
|
+
assertPositive("frameSamples", frameSamples);
|
|
48
|
+
this.frameSamples = frameSamples;
|
|
49
|
+
this.frameDurationMs = (frameSamples * 1000) / clockRate;
|
|
50
|
+
}
|
|
51
|
+
else {
|
|
52
|
+
assertPositive("frameDurationMs", frameDurationMs);
|
|
53
|
+
this.frameDurationMs = frameDurationMs;
|
|
54
|
+
this.frameSamples = (frameDurationMs * clockRate) / 1000;
|
|
55
|
+
}
|
|
56
|
+
this.initialTimestamp = toUint32(Math.round(options.initialTimestamp ?? (0, src_1.random32)()));
|
|
57
|
+
}
|
|
58
|
+
/** `round(N * frameSamples)`: samples elapsed from frame 0 to frame N. */
|
|
59
|
+
elapsedSamples(frameIndex) {
|
|
60
|
+
return Math.round(frameIndex * this.frameSamples);
|
|
61
|
+
}
|
|
62
|
+
/** `(initialTimestamp + elapsedSamples(N)) mod 2^32` */
|
|
63
|
+
timestamp(frameIndex) {
|
|
64
|
+
return toUint32(this.initialTimestamp + this.elapsedSamples(frameIndex));
|
|
65
|
+
}
|
|
66
|
+
/** `anchor.time + (N - anchor.frameIndex) * frameDurationMs` */
|
|
67
|
+
deadline(anchor, frameIndex) {
|
|
68
|
+
return (anchor.time + (frameIndex - anchor.frameIndex) * this.frameDurationMs);
|
|
69
|
+
}
|
|
70
|
+
/** Latest slot whose deadline has been reached at `now`. */
|
|
71
|
+
latestFrameIndex(anchor, now) {
|
|
72
|
+
return (anchor.frameIndex +
|
|
73
|
+
Math.floor((now - anchor.time) / this.frameDurationMs + SLOT_EPSILON));
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Resolves the tick to emit at `now`.
|
|
77
|
+
*
|
|
78
|
+
* Returns `undefined` when the deadline of `nextFrameIndex` has not been
|
|
79
|
+
* reached yet. When several slots have elapsed, `stallPolicy: "skip"`
|
|
80
|
+
* (default) returns only the latest reached slot and reports the slots in
|
|
81
|
+
* between as `skippedFrames`; `"delay"` returns `nextFrameIndex` with its
|
|
82
|
+
* original deadline, so `lateness` shows how far behind the clock is.
|
|
83
|
+
* `skippedFrames` and `elapsedSamples` are counted from `lastFrameIndex`
|
|
84
|
+
* (or from `baseFrameIndex` before the first tick), so a `nextFrameIndex`
|
|
85
|
+
* moved forward (e.g. by `resume()`) is reported as skipped too.
|
|
86
|
+
*/
|
|
87
|
+
resolveTick({ anchor, nextFrameIndex, lastFrameIndex, baseFrameIndex = nextFrameIndex, now, stallPolicy = "skip", }) {
|
|
88
|
+
const latest = this.latestFrameIndex(anchor, now);
|
|
89
|
+
if (latest < nextFrameIndex) {
|
|
90
|
+
return undefined;
|
|
91
|
+
}
|
|
92
|
+
const frameIndex = stallPolicy === "delay" ? nextFrameIndex : latest;
|
|
93
|
+
const deadline = this.deadline(anchor, frameIndex);
|
|
94
|
+
return {
|
|
95
|
+
frameIndex,
|
|
96
|
+
timestamp: this.timestamp(frameIndex),
|
|
97
|
+
elapsedSamples: this.elapsedSamples(frameIndex) -
|
|
98
|
+
this.elapsedSamples(lastFrameIndex ?? baseFrameIndex),
|
|
99
|
+
skippedFrames: frameIndex -
|
|
100
|
+
(lastFrameIndex !== undefined ? lastFrameIndex + 1 : baseFrameIndex),
|
|
101
|
+
deadline,
|
|
102
|
+
lateness: now - deadline,
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
exports.RtpMediaTimeline = RtpMediaTimeline;
|
|
107
|
+
const defaultScheduler = {
|
|
108
|
+
setTimeout: (cb, ms) => setTimeout(cb, ms),
|
|
109
|
+
clearTimeout: (handle) => clearTimeout(handle),
|
|
110
|
+
};
|
|
111
|
+
/**
|
|
112
|
+
* Monotonic real-time RTP media clock / pacer.
|
|
113
|
+
*
|
|
114
|
+
* - Each slot N is scheduled at the absolute deadline
|
|
115
|
+
* `origin + N * frameDurationMs`, so timer lateness never accumulates.
|
|
116
|
+
* - When the event loop stalls past several deadlines, `onTick` fires once.
|
|
117
|
+
* With `stallPolicy: "skip"` (default) it fires for the latest reached slot;
|
|
118
|
+
* the slots in between are reported via `skippedFrames` and appear as a
|
|
119
|
+
* timestamp gap. With `"delay"` it fires for the next slot and re-anchors
|
|
120
|
+
* the timeline, so no slot is skipped. Late ticks are never burst.
|
|
121
|
+
* - `onTick` subscribers are invoked synchronously after the internal state
|
|
122
|
+
* (including the next timer) has been updated. Returned promises are not
|
|
123
|
+
* awaited; backpressure is out of scope.
|
|
124
|
+
* - `resume()` re-anchors the origin to the resume time and emits a tick
|
|
125
|
+
* immediately. With `"skip"` the paused time is reflected as a timestamp gap
|
|
126
|
+
* by default; pass `{ continuous: true }` to collapse it (default for
|
|
127
|
+
* `"delay"`).
|
|
128
|
+
*
|
|
129
|
+
* This is an opt-in utility for media sources. `RTCRtpSender.sendRtp()` never
|
|
130
|
+
* rewrites RTP timestamps based on send time.
|
|
131
|
+
*/
|
|
132
|
+
class RtpMediaClock {
|
|
133
|
+
constructor(options) {
|
|
134
|
+
Object.defineProperty(this, "timeline", {
|
|
135
|
+
enumerable: true,
|
|
136
|
+
configurable: true,
|
|
137
|
+
writable: true,
|
|
138
|
+
value: void 0
|
|
139
|
+
});
|
|
140
|
+
Object.defineProperty(this, "onTick", {
|
|
141
|
+
enumerable: true,
|
|
142
|
+
configurable: true,
|
|
143
|
+
writable: true,
|
|
144
|
+
value: new src_1.Event()
|
|
145
|
+
});
|
|
146
|
+
Object.defineProperty(this, "stallPolicy", {
|
|
147
|
+
enumerable: true,
|
|
148
|
+
configurable: true,
|
|
149
|
+
writable: true,
|
|
150
|
+
value: void 0
|
|
151
|
+
});
|
|
152
|
+
Object.defineProperty(this, "now", {
|
|
153
|
+
enumerable: true,
|
|
154
|
+
configurable: true,
|
|
155
|
+
writable: true,
|
|
156
|
+
value: void 0
|
|
157
|
+
});
|
|
158
|
+
Object.defineProperty(this, "scheduler", {
|
|
159
|
+
enumerable: true,
|
|
160
|
+
configurable: true,
|
|
161
|
+
writable: true,
|
|
162
|
+
value: void 0
|
|
163
|
+
});
|
|
164
|
+
Object.defineProperty(this, "unref", {
|
|
165
|
+
enumerable: true,
|
|
166
|
+
configurable: true,
|
|
167
|
+
writable: true,
|
|
168
|
+
value: void 0
|
|
169
|
+
});
|
|
170
|
+
Object.defineProperty(this, "signal", {
|
|
171
|
+
enumerable: true,
|
|
172
|
+
configurable: true,
|
|
173
|
+
writable: true,
|
|
174
|
+
value: void 0
|
|
175
|
+
});
|
|
176
|
+
Object.defineProperty(this, "_state", {
|
|
177
|
+
enumerable: true,
|
|
178
|
+
configurable: true,
|
|
179
|
+
writable: true,
|
|
180
|
+
value: "idle"
|
|
181
|
+
});
|
|
182
|
+
Object.defineProperty(this, "anchor", {
|
|
183
|
+
enumerable: true,
|
|
184
|
+
configurable: true,
|
|
185
|
+
writable: true,
|
|
186
|
+
value: { time: 0, frameIndex: 0 }
|
|
187
|
+
});
|
|
188
|
+
Object.defineProperty(this, "nextFrameIndex", {
|
|
189
|
+
enumerable: true,
|
|
190
|
+
configurable: true,
|
|
191
|
+
writable: true,
|
|
192
|
+
value: 0
|
|
193
|
+
});
|
|
194
|
+
Object.defineProperty(this, "_lastTick", {
|
|
195
|
+
enumerable: true,
|
|
196
|
+
configurable: true,
|
|
197
|
+
writable: true,
|
|
198
|
+
value: void 0
|
|
199
|
+
});
|
|
200
|
+
Object.defineProperty(this, "timerHandle", {
|
|
201
|
+
enumerable: true,
|
|
202
|
+
configurable: true,
|
|
203
|
+
writable: true,
|
|
204
|
+
value: void 0
|
|
205
|
+
});
|
|
206
|
+
Object.defineProperty(this, "handleAbort", {
|
|
207
|
+
enumerable: true,
|
|
208
|
+
configurable: true,
|
|
209
|
+
writable: true,
|
|
210
|
+
value: () => {
|
|
211
|
+
this.stop();
|
|
212
|
+
}
|
|
213
|
+
});
|
|
214
|
+
Object.defineProperty(this, "handleTimer", {
|
|
215
|
+
enumerable: true,
|
|
216
|
+
configurable: true,
|
|
217
|
+
writable: true,
|
|
218
|
+
value: () => {
|
|
219
|
+
this.timerHandle = undefined;
|
|
220
|
+
if (this._state !== "running") {
|
|
221
|
+
return;
|
|
222
|
+
}
|
|
223
|
+
const now = this.now();
|
|
224
|
+
const tick = this.timeline.resolveTick({
|
|
225
|
+
anchor: this.anchor,
|
|
226
|
+
nextFrameIndex: this.nextFrameIndex,
|
|
227
|
+
lastFrameIndex: this._lastTick?.frameIndex,
|
|
228
|
+
// the timeline starts at slot 0; keep counting from there even if
|
|
229
|
+
// resume() moved nextFrameIndex before the first tick
|
|
230
|
+
baseFrameIndex: 0,
|
|
231
|
+
now,
|
|
232
|
+
stallPolicy: this.stallPolicy,
|
|
233
|
+
});
|
|
234
|
+
if (!tick) {
|
|
235
|
+
// timer fired early
|
|
236
|
+
this.schedule(Math.max(0, this.timeline.deadline(this.anchor, this.nextFrameIndex) - now));
|
|
237
|
+
return;
|
|
238
|
+
}
|
|
239
|
+
// commit state and the next timer first so that a throwing subscriber
|
|
240
|
+
// cannot corrupt the clock
|
|
241
|
+
if (this.stallPolicy === "delay" &&
|
|
242
|
+
tick.lateness >= this.timeline.frameDurationMs) {
|
|
243
|
+
// "delay" policy: absorb the stall as latency instead of bursting
|
|
244
|
+
this.anchor = { time: now, frameIndex: tick.frameIndex };
|
|
245
|
+
}
|
|
246
|
+
this._lastTick = tick;
|
|
247
|
+
this.nextFrameIndex = tick.frameIndex + 1;
|
|
248
|
+
this.schedule(Math.max(0, this.timeline.deadline(this.anchor, this.nextFrameIndex) - now));
|
|
249
|
+
this.onTick.execute(tick);
|
|
250
|
+
}
|
|
251
|
+
});
|
|
252
|
+
this.timeline = new RtpMediaTimeline(options);
|
|
253
|
+
this.stallPolicy = options.stallPolicy ?? "skip";
|
|
254
|
+
this.now = options.now ?? (() => performance.now());
|
|
255
|
+
this.scheduler = options.scheduler ?? defaultScheduler;
|
|
256
|
+
this.unref = options.unref ?? false;
|
|
257
|
+
this.signal = options.signal;
|
|
258
|
+
}
|
|
259
|
+
get state() {
|
|
260
|
+
return this._state;
|
|
261
|
+
}
|
|
262
|
+
get clockRate() {
|
|
263
|
+
return this.timeline.clockRate;
|
|
264
|
+
}
|
|
265
|
+
/** The most recently emitted tick. */
|
|
266
|
+
get lastTick() {
|
|
267
|
+
return this._lastTick;
|
|
268
|
+
}
|
|
269
|
+
/** RTP timestamp of the latest tick, or of frame 0 before the first tick. */
|
|
270
|
+
get timestamp() {
|
|
271
|
+
return this._lastTick?.timestamp ?? this.timeline.timestamp(0);
|
|
272
|
+
}
|
|
273
|
+
start(onTick) {
|
|
274
|
+
if (this._state !== "idle") {
|
|
275
|
+
throw new Error(`RtpMediaClock cannot start from state ${this._state}`);
|
|
276
|
+
}
|
|
277
|
+
if (onTick) {
|
|
278
|
+
this.onTick.subscribe(onTick);
|
|
279
|
+
}
|
|
280
|
+
if (this.signal?.aborted) {
|
|
281
|
+
this.stop();
|
|
282
|
+
return;
|
|
283
|
+
}
|
|
284
|
+
this.signal?.addEventListener("abort", this.handleAbort);
|
|
285
|
+
this._state = "running";
|
|
286
|
+
this.anchor = { time: this.now(), frameIndex: 0 };
|
|
287
|
+
this.nextFrameIndex = 0;
|
|
288
|
+
this.schedule(0);
|
|
289
|
+
}
|
|
290
|
+
pause() {
|
|
291
|
+
if (this._state !== "running") {
|
|
292
|
+
return;
|
|
293
|
+
}
|
|
294
|
+
this._state = "paused";
|
|
295
|
+
this.clearTimer();
|
|
296
|
+
}
|
|
297
|
+
resume(options = {}) {
|
|
298
|
+
if (this._state !== "paused") {
|
|
299
|
+
return;
|
|
300
|
+
}
|
|
301
|
+
const now = this.now();
|
|
302
|
+
const continuous = options.continuous ?? this.stallPolicy === "delay";
|
|
303
|
+
const frameIndex = continuous
|
|
304
|
+
? this.nextFrameIndex
|
|
305
|
+
: Math.max(this.nextFrameIndex, this.timeline.latestFrameIndex(this.anchor, now));
|
|
306
|
+
// re-anchor the origin to the resume time so that resume never bursts, and
|
|
307
|
+
// resolve the resume tick from that slot so that "delay" honors the gap too
|
|
308
|
+
this.anchor = { time: now, frameIndex };
|
|
309
|
+
this.nextFrameIndex = frameIndex;
|
|
310
|
+
this._state = "running";
|
|
311
|
+
this.schedule(0);
|
|
312
|
+
}
|
|
313
|
+
stop() {
|
|
314
|
+
if (this._state === "stopped") {
|
|
315
|
+
return;
|
|
316
|
+
}
|
|
317
|
+
this._state = "stopped";
|
|
318
|
+
this.clearTimer();
|
|
319
|
+
this.signal?.removeEventListener("abort", this.handleAbort);
|
|
320
|
+
this.onTick.complete();
|
|
321
|
+
}
|
|
322
|
+
schedule(delay) {
|
|
323
|
+
const handle = this.scheduler.setTimeout(this.handleTimer, delay);
|
|
324
|
+
if (this.unref) {
|
|
325
|
+
handle?.unref?.();
|
|
326
|
+
}
|
|
327
|
+
this.timerHandle = handle;
|
|
328
|
+
}
|
|
329
|
+
clearTimer() {
|
|
330
|
+
if (this.timerHandle !== undefined) {
|
|
331
|
+
this.scheduler.clearTimeout(this.timerHandle);
|
|
332
|
+
this.timerHandle = undefined;
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
exports.RtpMediaClock = RtpMediaClock;
|
|
337
|
+
/**
|
|
338
|
+
* Monotonic real-time pacer for frames with their own timestamps, such as
|
|
339
|
+
* variable-frame-rate encoded video. Works both for sources that are read
|
|
340
|
+
* ahead (demuxed files) and for streaming sources that deliver frames in real
|
|
341
|
+
* time with jitter (live encoders, pipes, network ingest).
|
|
342
|
+
*
|
|
343
|
+
* - Frame k is emitted at the absolute deadline
|
|
344
|
+
* `anchor.time + (dts(k) - anchor.dts) * 1000 / clockRate`, so timer
|
|
345
|
+
* lateness never accumulates. The RTP timestamp is
|
|
346
|
+
* `initialTimestamp + (pts(k) - pts(first frame))`, which keeps the source's
|
|
347
|
+
* frame spacing (including B-frame reordering).
|
|
348
|
+
* - Frames are never skipped. When the emitted frame and the next queued frame
|
|
349
|
+
* are both overdue (a stall, or a producer that pushes a late batch), only
|
|
350
|
+
* the emitted frame is sent and the timeline is re-anchored to `now`, so the
|
|
351
|
+
* stall becomes added latency (`delay`) instead of a burst. A frame pushed
|
|
352
|
+
* after its deadline while the queue is empty is emitted immediately.
|
|
353
|
+
* - For streaming sources, frames arriving early (bursts) wait for their
|
|
354
|
+
* deadline and frames arriving late are sent at once, so `delay` grows only
|
|
355
|
+
* up to the worst arrival lateness seen, never cumulatively. `latencyMs`
|
|
356
|
+
* reserves headroom for that jitter up front.
|
|
357
|
+
* - `resume()` emits the next frame immediately if it became due while paused
|
|
358
|
+
* and re-anchors to it; the paused time is added to `delay`.
|
|
359
|
+
* - `onTick` subscribers are invoked synchronously after the internal state has
|
|
360
|
+
* been updated. Returned promises are not awaited; use `queueLength` for
|
|
361
|
+
* backpressure on the producer side.
|
|
362
|
+
*/
|
|
363
|
+
class RtpMediaPacer {
|
|
364
|
+
constructor(options) {
|
|
365
|
+
Object.defineProperty(this, "clockRate", {
|
|
366
|
+
enumerable: true,
|
|
367
|
+
configurable: true,
|
|
368
|
+
writable: true,
|
|
369
|
+
value: void 0
|
|
370
|
+
});
|
|
371
|
+
Object.defineProperty(this, "initialTimestamp", {
|
|
372
|
+
enumerable: true,
|
|
373
|
+
configurable: true,
|
|
374
|
+
writable: true,
|
|
375
|
+
value: void 0
|
|
376
|
+
});
|
|
377
|
+
Object.defineProperty(this, "onTick", {
|
|
378
|
+
enumerable: true,
|
|
379
|
+
configurable: true,
|
|
380
|
+
writable: true,
|
|
381
|
+
value: new src_1.Event()
|
|
382
|
+
});
|
|
383
|
+
Object.defineProperty(this, "now", {
|
|
384
|
+
enumerable: true,
|
|
385
|
+
configurable: true,
|
|
386
|
+
writable: true,
|
|
387
|
+
value: void 0
|
|
388
|
+
});
|
|
389
|
+
Object.defineProperty(this, "scheduler", {
|
|
390
|
+
enumerable: true,
|
|
391
|
+
configurable: true,
|
|
392
|
+
writable: true,
|
|
393
|
+
value: void 0
|
|
394
|
+
});
|
|
395
|
+
Object.defineProperty(this, "unref", {
|
|
396
|
+
enumerable: true,
|
|
397
|
+
configurable: true,
|
|
398
|
+
writable: true,
|
|
399
|
+
value: void 0
|
|
400
|
+
});
|
|
401
|
+
Object.defineProperty(this, "signal", {
|
|
402
|
+
enumerable: true,
|
|
403
|
+
configurable: true,
|
|
404
|
+
writable: true,
|
|
405
|
+
value: void 0
|
|
406
|
+
});
|
|
407
|
+
Object.defineProperty(this, "latencyMs", {
|
|
408
|
+
enumerable: true,
|
|
409
|
+
configurable: true,
|
|
410
|
+
writable: true,
|
|
411
|
+
value: void 0
|
|
412
|
+
});
|
|
413
|
+
Object.defineProperty(this, "_state", {
|
|
414
|
+
enumerable: true,
|
|
415
|
+
configurable: true,
|
|
416
|
+
writable: true,
|
|
417
|
+
value: "idle"
|
|
418
|
+
});
|
|
419
|
+
Object.defineProperty(this, "queue", {
|
|
420
|
+
enumerable: true,
|
|
421
|
+
configurable: true,
|
|
422
|
+
writable: true,
|
|
423
|
+
value: []
|
|
424
|
+
});
|
|
425
|
+
Object.defineProperty(this, "firstPts", {
|
|
426
|
+
enumerable: true,
|
|
427
|
+
configurable: true,
|
|
428
|
+
writable: true,
|
|
429
|
+
value: void 0
|
|
430
|
+
});
|
|
431
|
+
Object.defineProperty(this, "lastPushedDts", {
|
|
432
|
+
enumerable: true,
|
|
433
|
+
configurable: true,
|
|
434
|
+
writable: true,
|
|
435
|
+
value: void 0
|
|
436
|
+
});
|
|
437
|
+
/** maps a dts to a monotonic time; undefined until the first emission */
|
|
438
|
+
Object.defineProperty(this, "anchor", {
|
|
439
|
+
enumerable: true,
|
|
440
|
+
configurable: true,
|
|
441
|
+
writable: true,
|
|
442
|
+
value: void 0
|
|
443
|
+
});
|
|
444
|
+
Object.defineProperty(this, "frameIndex", {
|
|
445
|
+
enumerable: true,
|
|
446
|
+
configurable: true,
|
|
447
|
+
writable: true,
|
|
448
|
+
value: 0
|
|
449
|
+
});
|
|
450
|
+
Object.defineProperty(this, "_delay", {
|
|
451
|
+
enumerable: true,
|
|
452
|
+
configurable: true,
|
|
453
|
+
writable: true,
|
|
454
|
+
value: 0
|
|
455
|
+
});
|
|
456
|
+
Object.defineProperty(this, "_lastTick", {
|
|
457
|
+
enumerable: true,
|
|
458
|
+
configurable: true,
|
|
459
|
+
writable: true,
|
|
460
|
+
value: void 0
|
|
461
|
+
});
|
|
462
|
+
Object.defineProperty(this, "timerHandle", {
|
|
463
|
+
enumerable: true,
|
|
464
|
+
configurable: true,
|
|
465
|
+
writable: true,
|
|
466
|
+
value: void 0
|
|
467
|
+
});
|
|
468
|
+
Object.defineProperty(this, "handleAbort", {
|
|
469
|
+
enumerable: true,
|
|
470
|
+
configurable: true,
|
|
471
|
+
writable: true,
|
|
472
|
+
value: () => {
|
|
473
|
+
this.stop();
|
|
474
|
+
}
|
|
475
|
+
});
|
|
476
|
+
Object.defineProperty(this, "handleTimer", {
|
|
477
|
+
enumerable: true,
|
|
478
|
+
configurable: true,
|
|
479
|
+
writable: true,
|
|
480
|
+
value: () => {
|
|
481
|
+
this.timerHandle = undefined;
|
|
482
|
+
const entry = this.queue[0];
|
|
483
|
+
if (this._state !== "running" || !entry) {
|
|
484
|
+
return;
|
|
485
|
+
}
|
|
486
|
+
const now = this.now();
|
|
487
|
+
const deadline = this.deadline(entry.dts);
|
|
488
|
+
if (now < deadline - SLOT_EPSILON) {
|
|
489
|
+
// timer fired early
|
|
490
|
+
this.scheduleNext(now);
|
|
491
|
+
return;
|
|
492
|
+
}
|
|
493
|
+
this.queue.shift();
|
|
494
|
+
const next = this.queue[0];
|
|
495
|
+
// stall: the following frame is overdue too -> absorb as latency
|
|
496
|
+
const stalled = next !== undefined && this.deadline(next.dts) <= now;
|
|
497
|
+
const tick = {
|
|
498
|
+
frame: entry.frame,
|
|
499
|
+
frameIndex: this.frameIndex,
|
|
500
|
+
timestamp: toUint32(this.initialTimestamp + Math.round(entry.pts - this.firstPts)),
|
|
501
|
+
pts: entry.pts,
|
|
502
|
+
dts: entry.dts,
|
|
503
|
+
deadline,
|
|
504
|
+
lateness: now - deadline,
|
|
505
|
+
delay: 0,
|
|
506
|
+
};
|
|
507
|
+
// commit state and the next timer first so that a throwing subscriber
|
|
508
|
+
// cannot corrupt the pacer
|
|
509
|
+
if (stalled) {
|
|
510
|
+
// plain timer lateness keeps the anchor and therefore never drifts
|
|
511
|
+
this.reanchor(now, entry.dts);
|
|
512
|
+
}
|
|
513
|
+
tick.delay = this._delay;
|
|
514
|
+
this.frameIndex++;
|
|
515
|
+
this._lastTick = tick;
|
|
516
|
+
this.scheduleNext(now);
|
|
517
|
+
this.onTick.execute(tick);
|
|
518
|
+
}
|
|
519
|
+
});
|
|
520
|
+
assertPositive("clockRate", options.clockRate);
|
|
521
|
+
this.clockRate = options.clockRate;
|
|
522
|
+
this.initialTimestamp = toUint32(Math.round(options.initialTimestamp ?? (0, src_1.random32)()));
|
|
523
|
+
this.latencyMs = options.latencyMs ?? 0;
|
|
524
|
+
if (!Number.isFinite(this.latencyMs) || this.latencyMs < 0) {
|
|
525
|
+
throw new RangeError("latencyMs must be a non-negative finite number");
|
|
526
|
+
}
|
|
527
|
+
this.now = options.now ?? (() => performance.now());
|
|
528
|
+
this.scheduler = options.scheduler ?? defaultScheduler;
|
|
529
|
+
this.unref = options.unref ?? false;
|
|
530
|
+
this.signal = options.signal;
|
|
531
|
+
}
|
|
532
|
+
get state() {
|
|
533
|
+
return this._state;
|
|
534
|
+
}
|
|
535
|
+
/** Frames pushed but not emitted yet. */
|
|
536
|
+
get queueLength() {
|
|
537
|
+
return this.queue.length;
|
|
538
|
+
}
|
|
539
|
+
/** The most recently emitted tick. */
|
|
540
|
+
get lastTick() {
|
|
541
|
+
return this._lastTick;
|
|
542
|
+
}
|
|
543
|
+
/** Total time (ms) the timeline has been shifted so far. */
|
|
544
|
+
get delay() {
|
|
545
|
+
return this._delay;
|
|
546
|
+
}
|
|
547
|
+
/** Queues a frame. Frames may be pushed before `start()`. */
|
|
548
|
+
push(frame, timing) {
|
|
549
|
+
if (this._state === "stopped") {
|
|
550
|
+
throw new Error("RtpMediaPacer is stopped");
|
|
551
|
+
}
|
|
552
|
+
const pts = timing.pts;
|
|
553
|
+
const dts = timing.dts ?? pts;
|
|
554
|
+
if (!Number.isFinite(pts) || !Number.isFinite(dts)) {
|
|
555
|
+
throw new RangeError("pts and dts must be finite numbers");
|
|
556
|
+
}
|
|
557
|
+
if (this.lastPushedDts !== undefined && dts < this.lastPushedDts) {
|
|
558
|
+
throw new RangeError("frames must be pushed in non-decreasing dts order");
|
|
559
|
+
}
|
|
560
|
+
this.lastPushedDts = dts;
|
|
561
|
+
this.firstPts ?? (this.firstPts = pts);
|
|
562
|
+
this.queue.push({ frame, pts, dts });
|
|
563
|
+
if (this._state === "running" && this.timerHandle === undefined) {
|
|
564
|
+
this.scheduleNext(this.now());
|
|
565
|
+
}
|
|
566
|
+
}
|
|
567
|
+
start(onTick) {
|
|
568
|
+
if (this._state !== "idle") {
|
|
569
|
+
throw new Error(`RtpMediaPacer cannot start from state ${this._state}`);
|
|
570
|
+
}
|
|
571
|
+
if (onTick) {
|
|
572
|
+
this.onTick.subscribe(onTick);
|
|
573
|
+
}
|
|
574
|
+
if (this.signal?.aborted) {
|
|
575
|
+
this.stop();
|
|
576
|
+
return;
|
|
577
|
+
}
|
|
578
|
+
this.signal?.addEventListener("abort", this.handleAbort);
|
|
579
|
+
this._state = "running";
|
|
580
|
+
if (this.queue.length > 0) {
|
|
581
|
+
this.scheduleNext(this.now());
|
|
582
|
+
}
|
|
583
|
+
}
|
|
584
|
+
pause() {
|
|
585
|
+
if (this._state !== "running") {
|
|
586
|
+
return;
|
|
587
|
+
}
|
|
588
|
+
this._state = "paused";
|
|
589
|
+
this.clearTimer();
|
|
590
|
+
}
|
|
591
|
+
resume() {
|
|
592
|
+
if (this._state !== "paused") {
|
|
593
|
+
return;
|
|
594
|
+
}
|
|
595
|
+
this._state = "running";
|
|
596
|
+
const now = this.now();
|
|
597
|
+
const next = this.queue[0];
|
|
598
|
+
if (next && this.anchor && this.deadline(next.dts) < now) {
|
|
599
|
+
// the paused time becomes latency: the next frame is due now, no burst
|
|
600
|
+
this.reanchor(now, next.dts);
|
|
601
|
+
}
|
|
602
|
+
this.scheduleNext(now);
|
|
603
|
+
}
|
|
604
|
+
stop() {
|
|
605
|
+
if (this._state === "stopped") {
|
|
606
|
+
return;
|
|
607
|
+
}
|
|
608
|
+
this._state = "stopped";
|
|
609
|
+
this.clearTimer();
|
|
610
|
+
this.queue = [];
|
|
611
|
+
this.signal?.removeEventListener("abort", this.handleAbort);
|
|
612
|
+
this.onTick.complete();
|
|
613
|
+
}
|
|
614
|
+
deadline(dts) {
|
|
615
|
+
const anchor = this.anchor;
|
|
616
|
+
return anchor.time + ((dts - anchor.dts) * 1000) / this.clockRate;
|
|
617
|
+
}
|
|
618
|
+
reanchor(time, dts) {
|
|
619
|
+
if (this.anchor) {
|
|
620
|
+
this._delay += time - this.deadline(dts);
|
|
621
|
+
}
|
|
622
|
+
this.anchor = { time, dts };
|
|
623
|
+
}
|
|
624
|
+
scheduleNext(now) {
|
|
625
|
+
const next = this.queue[0];
|
|
626
|
+
if (!next) {
|
|
627
|
+
return;
|
|
628
|
+
}
|
|
629
|
+
// the first frame anchors the timeline, `latencyMs` after it is scheduled
|
|
630
|
+
this.anchor ?? (this.anchor = { time: now + this.latencyMs, dts: next.dts });
|
|
631
|
+
const delay = Math.max(0, this.deadline(next.dts) - now);
|
|
632
|
+
const handle = this.scheduler.setTimeout(this.handleTimer, delay);
|
|
633
|
+
if (this.unref) {
|
|
634
|
+
handle?.unref?.();
|
|
635
|
+
}
|
|
636
|
+
this.timerHandle = handle;
|
|
637
|
+
}
|
|
638
|
+
clearTimer() {
|
|
639
|
+
if (this.timerHandle !== undefined) {
|
|
640
|
+
this.scheduler.clearTimeout(this.timerHandle);
|
|
641
|
+
this.timerHandle = undefined;
|
|
642
|
+
}
|
|
643
|
+
}
|
|
644
|
+
}
|
|
645
|
+
exports.RtpMediaPacer = RtpMediaPacer;
|
|
646
|
+
function toUint32(value) {
|
|
647
|
+
const mod = value % UINT32_MODULO;
|
|
648
|
+
return mod < 0 ? mod + UINT32_MODULO : mod;
|
|
649
|
+
}
|
|
650
|
+
function assertPositive(name, value) {
|
|
651
|
+
if (!Number.isFinite(value) || value <= 0) {
|
|
652
|
+
throw new RangeError(`${name} must be a positive finite number`);
|
|
653
|
+
}
|
|
654
|
+
}
|
|
655
|
+
//# sourceMappingURL=mediaClock.js.map
|