-
Notifications
You must be signed in to change notification settings - Fork 202
Expand file tree
/
Copy pathdecoder.ts
More file actions
438 lines (358 loc) · 13.6 KB
/
Copy pathdecoder.ts
File metadata and controls
438 lines (358 loc) · 13.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
import * as Catalog from "@moq/hang/catalog";
import * as Container from "@moq/hang/container";
import * as Util from "@moq/hang/util";
import type * as Moq from "@moq/net";
import { Time } from "@moq/net";
import { Effect, type Getter, Signal } from "@moq/signals";
import type { BufferedRanges } from "../backend";
import { base64ToBytes } from "../base64";
import { type AudioBuffer, createAudioBuffer } from "./buffer";
// Compiled and inlined as a blob URL via vite-plugin-worklet.
import RenderWorklet from "./render-worklet.ts?worklet";
import type { Source } from "./source";
export type DecoderProps = {
// Enable to download the audio track.
enabled?: boolean | Signal<boolean>;
};
export interface AudioStats {
bytesReceived: number;
}
// Downloads audio from a track and emits it to an AudioContext.
// The user is responsible for hooking up audio to speakers, an analyzer, etc.
export class Decoder {
source: Source;
enabled: Signal<boolean>;
#context = new Signal<AudioContext | undefined>(undefined);
readonly context: Getter<AudioContext | undefined> = this.#context;
// The root of the audio graph, which can be used for custom visualizations.
#worklet = new Signal<AudioWorkletNode | undefined>(undefined);
// Downcast to AudioNode so it matches Publish.Audio
readonly root = this.#worklet as Getter<AudioNode | undefined>;
#sampleRate = new Signal<number | undefined>(undefined);
readonly sampleRate: Getter<number | undefined> = this.#sampleRate;
#stats = new Signal<AudioStats | undefined>(undefined);
readonly stats: Getter<AudioStats | undefined> = this.#stats;
// Current playback timestamp from worklet
#timestamp = new Signal<Time.Milli | undefined>(undefined);
readonly timestamp: Getter<Time.Milli | undefined> = this.#timestamp;
// Whether the audio buffer is stalled (waiting to fill)
#stalled = new Signal<boolean>(true);
readonly stalled: Getter<boolean> = this.#stalled;
// Decode buffer: audio sent to worklet but not yet played
#decodeBuffered = new Signal<BufferedRanges>([]);
// Combined buffered ranges (network jitter + decode buffer)
#buffered = new Signal<BufferedRanges>([]);
readonly buffered: Getter<BufferedRanges> = this.#buffered;
// Audio ring bridging main thread and worklet (shared memory or postMessage transport).
#ring: AudioBuffer | undefined;
#signals = new Effect();
constructor(source: Source, props?: DecoderProps) {
this.source = source;
this.source.supported.set(supported); // super hacky
this.enabled = Signal.from(props?.enabled ?? false);
this.#signals.run(this.#runWorklet.bind(this));
this.#signals.run(this.#runEnabled.bind(this));
this.#signals.run(this.#runLatency.bind(this));
this.#signals.run(this.#runDecoder.bind(this));
}
#runWorklet(effect: Effect): void {
// It takes a second or so to initialize the AudioContext/AudioWorklet, so do it even if disabled.
// This is less efficient for video-only playback but makes muting/unmuting instant.
//const enabled = effect.get(this.enabled);
//if (!enabled) return;
const config = effect.get(this.source.config);
if (!config) return;
const sampleRate = config.sampleRate;
const channelCount = config.numberOfChannels;
// NOTE: We still create an AudioContext even when muted.
// This way we can process the audio for visualizations.
const context = new AudioContext({
latencyHint: "interactive", // We don't use real-time because of the buffer.
sampleRate,
});
effect.set(this.#context, context);
effect.cleanup(() => context.close());
effect.spawn(async () => {
// Register the AudioWorklet processor
await context.audioWorklet.addModule(RenderWorklet);
// Ensure the context is running before creating the worklet
if (context.state === "closed") return;
// Create the worklet node. outputChannelCount must be set explicitly
// so the process() callback receives a matching channel layout —
// Firefox defaults differently than Chrome otherwise.
const worklet = new AudioWorkletNode(context, "render", {
channelCount,
channelCountMode: "explicit",
outputChannelCount: [channelCount],
});
effect.cleanup(() => worklet.disconnect());
// Initial target latency in samples.
const latency = this.source.sync.buffer.peek();
const latencySamples = Math.ceil(sampleRate * Time.Second.fromMilli(latency));
// Let the factory pick the best transport (SharedArrayBuffer or postMessage).
const ring = createAudioBuffer(worklet, channelCount, sampleRate, latencySamples);
this.#ring = ring;
effect.cleanup(() => {
ring.close();
this.#ring = undefined;
});
// Mirror ring state (timestamp/stalled) onto our public signals.
effect.run((inner) => {
const ts = Time.Milli.fromMicro(inner.get(ring.timestamp));
this.#timestamp.set(ts);
this.#trimDecodeBuffered(ts);
});
effect.run((inner) => {
this.#stalled.set(inner.get(ring.stalled));
});
effect.set(this.#worklet, worklet);
});
}
#runEnabled(effect: Effect): void {
const values = effect.getAll([this.enabled, this.#context]);
if (!values) return;
const [_, context] = values;
context.resume();
// NOTE: You should disconnect/reconnect the worklet to save power when disabled.
}
#runLatency(effect: Effect): void {
// Gate on the worklet signal so this effect re-runs once the ring is created.
const worklet = effect.get(this.#worklet);
if (!worklet) return;
const ring = this.#ring;
if (!ring) return;
const latency = effect.get(this.source.sync.buffer);
const latencySamples = Math.ceil(ring.rate * Time.Second.fromMilli(latency));
ring.setLatency(latencySamples);
}
#runDecoder(effect: Effect): void {
const enabled = effect.get(this.enabled);
if (!enabled) return;
const broadcast = effect.get(this.source.broadcast);
if (!broadcast) return;
const track = effect.get(this.source.track);
if (!track) return;
const config = effect.get(this.source.config);
if (!config) return;
// Honor a per-rendition `broadcast` override: resolve to the source broadcast if set,
// otherwise use the catalog's own broadcast.
const active = broadcast.trackBroadcast(effect, config.broadcast);
if (!active) return;
const sub = active.subscribe(track, Catalog.PRIORITY.audio);
effect.cleanup(() => sub.close());
if (config.container.kind === "cmaf") {
this.#runCmafDecoder(effect, sub, config);
} else {
this.#runLegacyDecoder(effect, sub, config);
}
}
#runLegacyDecoder(effect: Effect, sub: Moq.Track, config: Catalog.AudioConfig): void {
const format = config.container.kind === "loc" ? new Container.Loc.Format() : new Container.Legacy.Format();
// Create consumer with slightly less latency than the render worklet to avoid underflowing.
// TODO include JITTER_UNDERHEAD
const consumer = new Container.Consumer(sub, {
format,
latency: this.source.sync.buffer,
});
effect.cleanup(() => consumer.close());
// Combine network jitter buffer with decode buffer
effect.run((inner) => {
const network = inner.get(consumer.buffered);
const decode = inner.get(this.#decodeBuffered);
this.#buffered.update(() => Container.mergeBufferedRanges(network, decode));
});
effect.spawn(async () => {
const loaded = await Util.Libav.polyfill();
if (!loaded) return; // cancelled
let warmed = 0;
const decoder = new AudioDecoder({
output: (data) => {
warmed++;
if (warmed <= 3) {
// Drop the first 3 frames to prime the decoder.
data.close();
return;
}
this.#emit(data);
},
error: (error) => console.error(error),
});
effect.cleanup(() => {
if (decoder.state !== "closed") decoder.close();
});
// Opus in CMAF uses raw packets; dOps is not a valid OGG Identification Header.
const description =
config.codec === "opus"
? undefined
: config.description
? Util.Hex.toBytes(config.description)
: undefined;
decoder.configure({
...config,
description,
});
for (;;) {
const next = await consumer.next();
if (!next) break;
const { frame } = next;
if (!frame) continue;
// Mark that we received this frame right now.
const timestamp = Time.Milli.fromMicro(frame.timestamp as Time.Micro);
this.source.sync.received(timestamp, "audio");
this.#stats.update((stats) => ({
bytesReceived: (stats?.bytesReceived ?? 0) + frame.data.byteLength,
}));
const chunk = new EncodedAudioChunk({
type: frame.keyframe ? "key" : "delta",
data: frame.data,
timestamp: frame.timestamp,
});
decoder.decode(chunk);
}
});
}
#runCmafDecoder(effect: Effect, sub: Moq.Track, config: Catalog.AudioConfig): void {
if (config.container.kind !== "cmaf") return; // just to help typescript
const initSegment = base64ToBytes(config.container.init);
const init = Container.Cmaf.decodeInitSegment(initSegment);
// Opus in CMAF uses raw packets (not OGG-wrapped), so description must be omitted.
// The dOps box from the init segment is not a valid OGG Identification Header.
const description =
config.codec === "opus"
? undefined
: config.description
? Util.Hex.toBytes(config.description)
: init.description;
const consumer = new Container.Consumer(sub, {
format: new Container.Cmaf.Format(init),
latency: this.source.sync.buffer,
});
effect.cleanup(() => consumer.close());
// Combine network jitter buffer with decode buffer
effect.run((inner) => {
const network = inner.get(consumer.buffered);
const decode = inner.get(this.#decodeBuffered);
this.#buffered.update(() => Container.mergeBufferedRanges(network, decode));
});
effect.spawn(async () => {
const loaded = await Util.Libav.polyfill();
if (!loaded) return; // cancelled
const decoder = new AudioDecoder({
output: (data) => this.#emit(data),
error: (error) => console.error(error),
});
effect.cleanup(() => {
if (decoder.state !== "closed") decoder.close();
});
// Configure decoder with description from catalog
decoder.configure({
codec: config.codec,
sampleRate: config.sampleRate,
numberOfChannels: config.numberOfChannels,
description,
});
for (;;) {
const next = await consumer.next();
if (!next) break;
const { frame } = next;
if (!frame) continue;
const timestamp = Time.Milli.fromMicro(frame.timestamp);
this.source.sync.received(timestamp, "audio");
this.#stats.update((stats) => ({
bytesReceived: (stats?.bytesReceived ?? 0) + frame.data.byteLength,
}));
if (decoder.state === "closed") break;
decoder.decode(
new EncodedAudioChunk({
type: frame.keyframe ? "key" : "delta",
data: frame.data,
timestamp: frame.timestamp,
}),
);
}
});
}
#emit(sample: AudioData) {
const timestamp = sample.timestamp as Time.Micro;
const timestampMilli = Time.Milli.fromMicro(timestamp);
const ring = this.#ring;
if (!ring) {
// We're probably in the process of closing.
sample.close();
return;
}
// Calculate end time from sample duration
const durationMicro = ((sample.numberOfFrames / sample.sampleRate) * 1_000_000) as Time.Micro;
const durationMilli = Time.Milli.fromMicro(durationMicro);
const end = Time.Milli.add(timestampMilli, durationMilli);
// Add to decode buffer
this.#addDecodeBuffered(timestampMilli, end);
// Firefox's Opus decoder sometimes outputs more channels than requested
// (e.g. 6 for stereo). Clamp to the ring's channel count.
const channels = Math.min(sample.numberOfChannels, ring.channels);
const channelData: Float32Array[] = [];
for (let channel = 0; channel < channels; channel++) {
const data = new Float32Array(sample.numberOfFrames);
sample.copyTo(data, { format: "f32-planar", planeIndex: channel });
channelData.push(data);
}
// Hand off to the ring. Shared transport writes directly; post transport
// transfers the ArrayBuffers.
ring.insert(timestamp, channelData);
sample.close();
}
#addDecodeBuffered(start: Time.Milli, end: Time.Milli): void {
if (start > end) return;
this.#decodeBuffered.mutate((current) => {
for (const range of current) {
// Extend range if new sample overlaps or is adjacent (1ms tolerance for float precision)
if (start <= range.end + 1 && end >= range.start) {
range.start = Time.Milli.min(range.start, start);
range.end = Time.Milli.max(range.end, end);
return;
}
}
current.push({ start, end });
current.sort((a, b) => a.start - b.start);
});
}
#trimDecodeBuffered(timestamp: Time.Milli): void {
this.#decodeBuffered.mutate((current) => {
while (current.length > 0) {
if (current[0].end >= timestamp) {
current[0].start = Time.Milli.max(current[0].start, timestamp);
break;
}
current.shift();
}
});
}
close() {
this.#signals.close();
}
}
async function supported(config: Catalog.AudioConfig): Promise<boolean> {
// Opus in CMAF uses raw packets; dOps is not a valid OGG Identification Header.
let description: Uint8Array | undefined;
if (config.codec !== "opus") {
if (config.description) {
description = Util.Hex.toBytes(config.description);
} else if (config.container.kind === "cmaf") {
try {
description = Container.Cmaf.decodeInitSegment(base64ToBytes(config.container.init)).description;
} catch (err) {
// A malformed init segment means we can't extract the codec
// description, so we can't probe support reliably. Reject the
// track rather than letting isConfigSupported pass on a
// description-less config and then having decode() fail later.
console.warn(`audio: malformed CMAF init segment for codec ${config.codec}`, err);
return false;
}
}
}
const res = await AudioDecoder.isConfigSupported({
...config,
description,
});
return res.supported ?? false;
}