forked from moq-dev/moq
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
287 lines (231 loc) · 8.33 KB
/
Copy pathindex.ts
File metadata and controls
287 lines (231 loc) · 8.33 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
import * as Moq from "@kixelated/moq";
import { Effect, type Getter, Signal } from "@kixelated/signals";
import type * as Catalog from "../../catalog";
import { u53, u8 } from "../../catalog/integers";
import * as Container from "../../container";
import { Captions, type CaptionsProps } from "./captions";
import type * as Capture from "./capture";
import { Speaking, type SpeakingProps } from "./speaking";
export * from "./captions";
const GAIN_MIN = 0.001;
const FADE_TIME = 0.2;
export type AudioConstraints = Omit<
MediaTrackConstraints,
"aspectRatio" | "backgroundBlur" | "displaySurface" | "facingMode" | "frameRate" | "height" | "width"
>;
// Stronger typing for the MediaStreamTrack interface.
export interface AudioTrack extends MediaStreamTrack {
kind: "audio";
clone(): AudioTrack;
}
// MediaTrackSettings can represent both audio and video, which means a LOT of possibly undefined properties.
// This is a fork of the MediaTrackSettings interface with properties required for audio or vidfeo.
export interface AudioTrackSettings {
deviceId: string;
groupId: string;
autoGainControl: boolean;
channelCount: number;
echoCancellation: boolean;
noiseSuppression: boolean;
sampleRate: number;
sampleSize: number;
}
// The initial values for our signals.
export type AudioProps = {
enabled?: boolean;
media?: AudioTrack;
constraints?: AudioConstraints;
muted?: boolean;
volume?: number;
captions?: CaptionsProps;
speaking?: SpeakingProps;
// The size of each group. Larger groups mean fewer drops but the viewer can fall further behind.
// NOTE: Each frame is always flushed to the network immediately.
maxLatency?: DOMHighResTimeStamp;
};
export class Audio {
broadcast: Moq.BroadcastProducer;
enabled: Signal<boolean>;
muted: Signal<boolean>;
volume: Signal<number>;
captions: Captions;
speaking: Speaking;
maxLatency: DOMHighResTimeStamp;
media: Signal<AudioTrack | undefined>;
constraints: Signal<AudioConstraints | undefined>;
#catalog = new Signal<Catalog.Audio | undefined>(undefined);
readonly catalog: Getter<Catalog.Audio | undefined> = this.#catalog;
#config = new Signal<Catalog.AudioConfig | undefined>(undefined);
readonly config: Getter<Catalog.AudioConfig | undefined> = this.#config;
#worklet = new Signal<AudioWorkletNode | undefined>(undefined);
#gain = new Signal<GainNode | undefined>(undefined);
readonly root: Getter<AudioNode | undefined> = this.#gain;
#track = new Moq.TrackProducer("audio", 1);
#group?: Moq.GroupProducer;
#groupTimestamp = 0;
#signals = new Effect();
constructor(broadcast: Moq.BroadcastProducer, props?: AudioProps) {
this.broadcast = broadcast;
this.media = new Signal(props?.media);
this.enabled = new Signal(props?.enabled ?? false);
this.speaking = new Speaking(this, props?.speaking);
this.captions = new Captions(this, props?.captions);
this.constraints = new Signal(props?.constraints);
this.muted = new Signal(props?.muted ?? false);
this.volume = new Signal(props?.volume ?? 1);
this.maxLatency = props?.maxLatency ?? 100; // Default is a group every 100ms
this.#signals.effect(this.#runSource.bind(this));
this.#signals.effect(this.#runGain.bind(this));
this.#signals.effect(this.#runEncoder.bind(this));
this.#signals.effect(this.#runCatalog.bind(this));
}
#runSource(effect: Effect): void {
const enabled = effect.get(this.enabled);
const media = effect.get(this.media);
if (!enabled || !media) return;
const settings = media.getSettings();
if (!settings) {
throw new Error("track has no settings");
}
const context = new AudioContext({
latencyHint: "interactive",
sampleRate: settings.sampleRate,
});
effect.cleanup(() => context.close());
const root = new MediaStreamAudioSourceNode(context, {
mediaStream: new MediaStream([media]),
});
effect.cleanup(() => root.disconnect());
const gain = new GainNode(context, {
gain: this.volume.peek(),
});
root.connect(gain);
effect.cleanup(() => gain.disconnect());
// Async because we need to wait for the worklet to be registered.
effect.spawn(async () => {
// Hacky workaround to support Webpack and Vite:
// https://github.com/webpack/webpack/issues/11543#issuecomment-2045809214
const { register } = navigator.serviceWorker;
// @ts-ignore hack to make webpack believe that it is registering a worker
navigator.serviceWorker.register = (url: URL) => context.audioWorklet.addModule(url);
await navigator.serviceWorker.register(new URL("./capture-worklet", import.meta.url));
navigator.serviceWorker.register = register;
const worklet = new AudioWorkletNode(context, "capture", {
numberOfInputs: 1,
numberOfOutputs: 0,
channelCount: settings.channelCount,
});
effect.set(this.#worklet, worklet);
gain.connect(worklet);
effect.cleanup(() => worklet.disconnect());
// Only set the gain after the worklet is registered.
effect.set(this.#gain, gain);
});
}
#runGain(effect: Effect): void {
const gain = effect.get(this.#gain);
if (!gain) return;
effect.cleanup(() => gain.gain.cancelScheduledValues(gain.context.currentTime));
const volume = effect.get(this.muted) ? 0 : effect.get(this.volume);
if (volume < GAIN_MIN) {
gain.gain.exponentialRampToValueAtTime(GAIN_MIN, gain.context.currentTime + FADE_TIME);
gain.gain.setValueAtTime(0, gain.context.currentTime + FADE_TIME + 0.01);
} else {
gain.gain.exponentialRampToValueAtTime(volume, gain.context.currentTime + FADE_TIME);
}
}
#runEncoder(effect: Effect): void {
if (!effect.get(this.enabled)) return;
const media = effect.get(this.media);
if (!media) return;
const worklet = effect.get(this.#worklet);
if (!worklet) return;
this.broadcast.insertTrack(this.#track.consume());
effect.cleanup(() => {
this.broadcast.removeTrack(this.#track.name);
});
const settings = media.getSettings() as AudioTrackSettings;
const config = {
// TODO get codec and description from decoderConfig
codec: "opus",
// Firefox doesn't provide the sampleRate in the settings.
sampleRate: u53(settings.sampleRate ?? worklet?.context.sampleRate),
numberOfChannels: u53(settings.channelCount),
// TODO configurable
bitrate: u53(settings.channelCount * 32_000),
};
const encoder = new AudioEncoder({
output: (frame) => {
if (frame.type !== "key") {
throw new Error("only key frames are supported");
}
if (!this.#group || frame.timestamp - this.#groupTimestamp >= 1000 * this.maxLatency) {
this.#group?.close();
this.#group = this.#track.appendGroup();
this.#groupTimestamp = frame.timestamp;
}
const buffer = Container.encodeFrame(frame, frame.timestamp);
this.#group.writeFrame(buffer);
},
error: (err) => {
this.#group?.abort(err);
this.#group = undefined;
this.#track.abort(err);
},
});
effect.cleanup(() => encoder.close());
effect.cleanup(() => {
this.#group?.close();
this.#group = undefined;
this.#groupTimestamp = 0;
});
encoder.configure({
codec: config.codec,
numberOfChannels: config.numberOfChannels,
sampleRate: config.sampleRate,
bitrate: config.bitrate,
});
effect.set(this.#config, config);
worklet.port.onmessage = ({ data }: { data: Capture.AudioFrame }) => {
const channels = data.channels.slice(0, settings.channelCount);
const joinedLength = channels.reduce((a, b) => a + b.length, 0);
const joined = new Float32Array(joinedLength);
channels.reduce((offset: number, channel: Float32Array): number => {
joined.set(channel, offset);
return offset + channel.length;
}, 0);
const frame = new AudioData({
format: "f32-planar",
sampleRate: worklet.context.sampleRate,
numberOfFrames: channels[0].length,
numberOfChannels: channels.length,
timestamp: (1_000_000 * data.timestamp) / worklet.context.sampleRate,
data: joined,
transfer: [joined.buffer],
});
encoder.encode(frame);
frame.close();
};
}
#runCatalog(effect: Effect): void {
const config = effect.get(this.#config);
if (!config) return;
const captions = effect.get(this.captions.catalog);
const speaking = effect.get(this.speaking.catalog);
const catalog: Catalog.Audio = {
track: {
name: this.#track.name,
priority: u8(this.#track.priority),
},
config,
captions,
speaking,
};
effect.set(this.#catalog, catalog);
}
close() {
this.#signals.close();
this.captions.close();
this.#track.close();
}
}