@camstack/addon-pipeline 1.1.37 → 1.1.39
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/dist/audio-analyzer/index.js +1 -1
- package/dist/audio-analyzer/index.mjs +1 -1
- package/dist/detection-pipeline/index.js +1 -1
- package/dist/detection-pipeline/index.mjs +1 -1
- package/dist/{dist-CdltGhTt.js → dist-DbGdZ8Nr.js} +36 -79
- package/dist/{dist-DdZEBePv.mjs → dist-DvvYzO58.mjs} +37 -74
- package/dist/{frame-handle-plane-zfv8z5a5.js → frame-handle-plane-B7jMIZc2.js} +1 -1
- package/dist/{frame-handle-plane-C0Rzay-b.mjs → frame-handle-plane-BFzoIfkd.mjs} +1 -1
- package/dist/motion-wasm/index.js +1 -1
- package/dist/motion-wasm/index.mjs +1 -1
- package/dist/pipeline-runner/index.js +2 -2
- package/dist/pipeline-runner/index.mjs +2 -2
- package/dist/recorder/index.js +1 -1
- package/dist/recorder/index.mjs +1 -1
- package/dist/stream-broker/{_virtual_mf-localSharedImportMap___mfe_internal__addon_stream_broker_widgets-CfUpZdH5.mjs → _virtual_mf-localSharedImportMap___mfe_internal__addon_stream_broker_widgets-DezFy4Fu.mjs} +1 -1
- package/dist/stream-broker/{hostInit-YSFfELtG.mjs → hostInit-BOf0hg9q.mjs} +1 -1
- package/dist/stream-broker/index.js +2 -2
- package/dist/stream-broker/index.mjs +2 -2
- package/dist/stream-broker/remoteEntry.js +1 -1
- package/embed-dist/assets/{MaskShapeCanvas-DI4BY7W2-D3FtMsgt.js → MaskShapeCanvas-DI4BY7W2-Br2fqwe7.js} +1 -1
- package/embed-dist/assets/{MotionZonesSettings-NcxxQN8r-DidcQOd2.js → MotionZonesSettings-NcxxQN8r-DYXRf30g.js} +1 -1
- package/embed-dist/assets/{PrivacyMaskSettings-APgPLF7p-DyzMlv2v.js → PrivacyMaskSettings-APgPLF7p-DLpqLgPo.js} +1 -1
- package/embed-dist/assets/{index-Cr7799kl.js → index-CR367rYR.js} +4 -4
- package/embed-dist/index.html +1 -1
- package/package.json +1 -26
- package/dist/audio-codec-ffmpeg/index.js +0 -1238
- package/dist/audio-codec-ffmpeg/index.mjs +0 -1220
|
@@ -1,1220 +0,0 @@
|
|
|
1
|
-
import { L as errMsg, R as BaseAddon, g as audioCodecCapability } from "../dist-DdZEBePv.mjs";
|
|
2
|
-
import { randomUUID } from "node:crypto";
|
|
3
|
-
import { spawn } from "node:child_process";
|
|
4
|
-
//#region src/audio-codec-ffmpeg/ffmpeg-audio-process.ts
|
|
5
|
-
/**
|
|
6
|
-
* Minimal ffmpeg subprocess wrapper shared by the audio decode + encode
|
|
7
|
-
* sessions of `audio-codec-ffmpeg`.
|
|
8
|
-
*
|
|
9
|
-
* Owns exactly the process concerns both directions need: spawn, stdin write
|
|
10
|
-
* with tolerated backpressure, stdout chunk delivery, and the kill sequence.
|
|
11
|
-
*
|
|
12
|
-
* The kill logic copies the CORRECTED pattern from `decoder-ffmpeg`'s
|
|
13
|
-
* `killFfmpeg`: SIGTERM, then SIGKILL after a grace — gated on
|
|
14
|
-
* `exitCode === null && signalCode === null`, NOT on `child.killed` (which Node
|
|
15
|
-
* sets true after ANY `kill()`, so `!child.killed` never fires and the SIGKILL
|
|
16
|
-
* would leak the process).
|
|
17
|
-
*/
|
|
18
|
-
/** Grace period between SIGTERM and the escalated SIGKILL. */
|
|
19
|
-
var KILL_GRACE_MS = 500;
|
|
20
|
-
var FfmpegAudioProcess = class {
|
|
21
|
-
child = null;
|
|
22
|
-
logger;
|
|
23
|
-
opts;
|
|
24
|
-
killed = false;
|
|
25
|
-
constructor(opts) {
|
|
26
|
-
this.opts = opts;
|
|
27
|
-
this.logger = opts.logger;
|
|
28
|
-
this.spawn();
|
|
29
|
-
}
|
|
30
|
-
spawn() {
|
|
31
|
-
let child;
|
|
32
|
-
try {
|
|
33
|
-
child = spawn(this.opts.ffmpegPath, [...this.opts.args]);
|
|
34
|
-
} catch (err) {
|
|
35
|
-
this.logger.error("audio-codec-ffmpeg: spawn threw", { meta: { error: errMsg(err) } });
|
|
36
|
-
return;
|
|
37
|
-
}
|
|
38
|
-
this.child = child;
|
|
39
|
-
child.stdin?.on("error", () => {});
|
|
40
|
-
child.stdout?.on("data", (chunk) => this.opts.onStdout(chunk));
|
|
41
|
-
child.stderr?.on("data", (data) => {
|
|
42
|
-
const line = data.toString().trim();
|
|
43
|
-
if (line) this.logger.debug("audio-codec-ffmpeg stderr", { meta: { line } });
|
|
44
|
-
});
|
|
45
|
-
child.on("error", (err) => {
|
|
46
|
-
this.logger.error("audio-codec-ffmpeg: process error", { meta: { error: err.message } });
|
|
47
|
-
});
|
|
48
|
-
child.on("exit", (code, signal) => {
|
|
49
|
-
if (this.killed) return;
|
|
50
|
-
this.opts.onExit(code, signal);
|
|
51
|
-
});
|
|
52
|
-
}
|
|
53
|
-
/** Write encoded frames / PCM into ffmpeg stdin. Backpressure is tolerated. */
|
|
54
|
-
write(data) {
|
|
55
|
-
const stdin = this.child?.stdin;
|
|
56
|
-
if (!stdin) return;
|
|
57
|
-
try {
|
|
58
|
-
stdin.write(Buffer.from(data.buffer, data.byteOffset, data.byteLength));
|
|
59
|
-
} catch {}
|
|
60
|
-
}
|
|
61
|
-
/** Close stdin so ffmpeg flushes and drains its remaining output. */
|
|
62
|
-
endStdin() {
|
|
63
|
-
try {
|
|
64
|
-
this.child?.stdin?.end();
|
|
65
|
-
} catch {}
|
|
66
|
-
}
|
|
67
|
-
/** SIGTERM then escalated SIGKILL — see class doc for the gating rationale. */
|
|
68
|
-
kill() {
|
|
69
|
-
const child = this.child;
|
|
70
|
-
if (!child) return;
|
|
71
|
-
this.killed = true;
|
|
72
|
-
this.child = null;
|
|
73
|
-
try {
|
|
74
|
-
child.stdin?.end();
|
|
75
|
-
} catch {}
|
|
76
|
-
try {
|
|
77
|
-
child.kill("SIGTERM");
|
|
78
|
-
} catch {}
|
|
79
|
-
const { pid } = child;
|
|
80
|
-
setTimeout(() => {
|
|
81
|
-
if (pid !== void 0 && child.exitCode === null && child.signalCode === null) try {
|
|
82
|
-
child.kill("SIGKILL");
|
|
83
|
-
} catch {}
|
|
84
|
-
}, KILL_GRACE_MS).unref?.();
|
|
85
|
-
}
|
|
86
|
-
};
|
|
87
|
-
//#endregion
|
|
88
|
-
//#region src/audio-codec-ffmpeg/audio-codec-args.ts
|
|
89
|
-
/**
|
|
90
|
-
* Low-latency flag set shared by both the decode and encode invocations.
|
|
91
|
-
*
|
|
92
|
-
* - `-hide_banner -nostats -loglevel error`: quiet stderr (no periodic counter).
|
|
93
|
-
* - `-fflags +nobuffer+flush_packets`: don't buffer input; flush output packets
|
|
94
|
-
* the moment they are ready (critical for the intercom encode path).
|
|
95
|
-
* - `-flags low_delay`: request the lowest-latency codec behaviour.
|
|
96
|
-
* - `-probesize 32 -analyzeduration 0`: skip stream probing — the input format
|
|
97
|
-
* is fully specified by `-f`/`-ar`/`-ac`, so probing only adds startup delay.
|
|
98
|
-
* - `-threads 1`: single-threaded — audio frames are tiny, and extra worker
|
|
99
|
-
* threads only add scheduling jitter to a real-time path.
|
|
100
|
-
*/
|
|
101
|
-
var AUDIO_LOW_LATENCY_ARGS = [
|
|
102
|
-
"-hide_banner",
|
|
103
|
-
"-nostats",
|
|
104
|
-
"-loglevel",
|
|
105
|
-
"error",
|
|
106
|
-
"-fflags",
|
|
107
|
-
"+nobuffer+flush_packets",
|
|
108
|
-
"-flags",
|
|
109
|
-
"low_delay",
|
|
110
|
-
"-probesize",
|
|
111
|
-
"32",
|
|
112
|
-
"-analyzeduration",
|
|
113
|
-
"0",
|
|
114
|
-
"-threads",
|
|
115
|
-
"1"
|
|
116
|
-
];
|
|
117
|
-
/**
|
|
118
|
-
* Normalise an SDP-reported codec name to the libav/ffmpeg name. Mirrors the
|
|
119
|
-
* `resolveCodecAlias` in `audio-codec-nodeav` so callers can pass the
|
|
120
|
-
* SDP-reported value verbatim.
|
|
121
|
-
*/
|
|
122
|
-
function resolveAudioCodecAlias(codec) {
|
|
123
|
-
const c = codec.toLowerCase();
|
|
124
|
-
if (c === "mpeg4-generic") return "aac";
|
|
125
|
-
if (c === "l16") return "pcm_s16be";
|
|
126
|
-
return c;
|
|
127
|
-
}
|
|
128
|
-
/**
|
|
129
|
-
* codec → ffmpeg DECODE input format.
|
|
130
|
-
*
|
|
131
|
-
* | codec | -f | raw? | note |
|
|
132
|
-
* | ----------------------------- | ------ | ---- | ----------------------------- |
|
|
133
|
-
* | pcm_mulaw | mulaw | yes | G.711 µ-law |
|
|
134
|
-
* | pcm_alaw | alaw | yes | G.711 A-law |
|
|
135
|
-
* | pcm_s16be (l16) | s16be | yes | RTP L16 |
|
|
136
|
-
* | pcm_s16le | s16le | yes | |
|
|
137
|
-
* | g722 | g722 | yes | 16 kHz wideband |
|
|
138
|
-
* | aac / aac_latm / mpeg4-generic| aac | no | ADTS demuxer |
|
|
139
|
-
* | opus | ogg | no | ⚠ needs Ogg framing (see NOTE)|
|
|
140
|
-
*
|
|
141
|
-
* NOTE (opus decode): ffmpeg has no raw-packet Opus demuxer. `audio-codec-nodeav`
|
|
142
|
-
* feeds raw RTP Opus packets straight to the `AV_CODEC_ID_OPUS` decoder with no
|
|
143
|
-
* demuxer; a subprocess cannot do that from a pipe. We map to `ogg`, which is
|
|
144
|
-
* correct only if the pushed bytes are Ogg-Opus. Raw RTP Opus decode via the
|
|
145
|
-
* subprocess is a KNOWN limitation — see the addon report.
|
|
146
|
-
*/
|
|
147
|
-
var DECODE_FORMAT_BY_CODEC = {
|
|
148
|
-
pcm_mulaw: {
|
|
149
|
-
inputFormat: "mulaw",
|
|
150
|
-
rawInput: true
|
|
151
|
-
},
|
|
152
|
-
pcm_alaw: {
|
|
153
|
-
inputFormat: "alaw",
|
|
154
|
-
rawInput: true
|
|
155
|
-
},
|
|
156
|
-
pcm_s16be: {
|
|
157
|
-
inputFormat: "s16be",
|
|
158
|
-
rawInput: true
|
|
159
|
-
},
|
|
160
|
-
pcm_s16le: {
|
|
161
|
-
inputFormat: "s16le",
|
|
162
|
-
rawInput: true
|
|
163
|
-
},
|
|
164
|
-
g722: {
|
|
165
|
-
inputFormat: "g722",
|
|
166
|
-
rawInput: true
|
|
167
|
-
},
|
|
168
|
-
aac: {
|
|
169
|
-
inputFormat: "aac",
|
|
170
|
-
rawInput: false
|
|
171
|
-
},
|
|
172
|
-
aac_latm: {
|
|
173
|
-
inputFormat: "aac",
|
|
174
|
-
rawInput: false
|
|
175
|
-
},
|
|
176
|
-
opus: {
|
|
177
|
-
inputFormat: "ogg",
|
|
178
|
-
rawInput: false
|
|
179
|
-
}
|
|
180
|
-
};
|
|
181
|
-
/**
|
|
182
|
-
* codec → ffmpeg ENCODE codec + container.
|
|
183
|
-
*
|
|
184
|
-
* | codec | -c:a | -f | bitrate? |
|
|
185
|
-
* | --------- | ---------- | ----- | -------- |
|
|
186
|
-
* | pcm_mulaw | pcm_mulaw | mulaw | no |
|
|
187
|
-
* | pcm_alaw | pcm_alaw | alaw | no |
|
|
188
|
-
* | g722 | g722 | g722 | no |
|
|
189
|
-
* | aac | aac | adts | yes |
|
|
190
|
-
* | opus | libopus | ogg | yes |
|
|
191
|
-
*/
|
|
192
|
-
var ENCODE_FORMAT_BY_CODEC = {
|
|
193
|
-
pcm_mulaw: {
|
|
194
|
-
encoder: "pcm_mulaw",
|
|
195
|
-
outputFormat: "mulaw",
|
|
196
|
-
acceptsBitrate: false
|
|
197
|
-
},
|
|
198
|
-
pcm_alaw: {
|
|
199
|
-
encoder: "pcm_alaw",
|
|
200
|
-
outputFormat: "alaw",
|
|
201
|
-
acceptsBitrate: false
|
|
202
|
-
},
|
|
203
|
-
g722: {
|
|
204
|
-
encoder: "g722",
|
|
205
|
-
outputFormat: "g722",
|
|
206
|
-
acceptsBitrate: false
|
|
207
|
-
},
|
|
208
|
-
aac: {
|
|
209
|
-
encoder: "aac",
|
|
210
|
-
outputFormat: "adts",
|
|
211
|
-
acceptsBitrate: true
|
|
212
|
-
},
|
|
213
|
-
opus: {
|
|
214
|
-
encoder: "libopus",
|
|
215
|
-
outputFormat: "ogg",
|
|
216
|
-
acceptsBitrate: true
|
|
217
|
-
}
|
|
218
|
-
};
|
|
219
|
-
/** Resolve the DECODE input-format descriptor for a codec, or `null` if unknown. */
|
|
220
|
-
function decodeFormatForCodec(codec) {
|
|
221
|
-
return DECODE_FORMAT_BY_CODEC[resolveAudioCodecAlias(codec)] ?? null;
|
|
222
|
-
}
|
|
223
|
-
/** Resolve the ENCODE codec+container descriptor for a codec, or `null` if unknown. */
|
|
224
|
-
function encodeFormatForCodec(codec) {
|
|
225
|
-
return ENCODE_FORMAT_BY_CODEC[resolveAudioCodecAlias(codec)] ?? null;
|
|
226
|
-
}
|
|
227
|
-
/** Packed bytes-per-sample for a PCM format (s16le = 2, f32le = 4). */
|
|
228
|
-
function audioBytesPerSample(format) {
|
|
229
|
-
return format === "f32le" ? 4 : 2;
|
|
230
|
-
}
|
|
231
|
-
/**
|
|
232
|
-
* Build the ffmpeg argv for a DECODE session — encoded frames on `pipe:0`, raw
|
|
233
|
-
* PCM on `pipe:1`. PURE: no spawn, no env access.
|
|
234
|
-
*
|
|
235
|
-
* ffmpeg <low-latency> -f <inputFmt> [-ar <src> -ac <srcCh>] -i pipe:0 \
|
|
236
|
-
* -ar <target> -ac <targetCh> -f <s16le|f32le> pipe:1
|
|
237
|
-
*
|
|
238
|
-
* @throws if the codec has no known decode mapping.
|
|
239
|
-
*/
|
|
240
|
-
function buildAudioDecodeArgs(config) {
|
|
241
|
-
const entry = decodeFormatForCodec(config.codec);
|
|
242
|
-
if (!entry) throw new Error(`audio-codec-ffmpeg: no decode format for codec '${config.codec}'`);
|
|
243
|
-
const outFormat = config.targetFormat ?? "s16le";
|
|
244
|
-
const inputRateChannel = entry.rawInput ? [
|
|
245
|
-
"-ar",
|
|
246
|
-
String(config.sourceSampleRate),
|
|
247
|
-
"-ac",
|
|
248
|
-
String(config.sourceChannels)
|
|
249
|
-
] : [];
|
|
250
|
-
return [
|
|
251
|
-
...AUDIO_LOW_LATENCY_ARGS,
|
|
252
|
-
"-f",
|
|
253
|
-
entry.inputFormat,
|
|
254
|
-
...inputRateChannel,
|
|
255
|
-
"-i",
|
|
256
|
-
"pipe:0",
|
|
257
|
-
"-ar",
|
|
258
|
-
String(config.targetSampleRate),
|
|
259
|
-
"-ac",
|
|
260
|
-
String(config.targetChannels),
|
|
261
|
-
"-f",
|
|
262
|
-
outFormat,
|
|
263
|
-
"pipe:1"
|
|
264
|
-
];
|
|
265
|
-
}
|
|
266
|
-
/**
|
|
267
|
-
* Build the ffmpeg argv for an ENCODE session — raw PCM on `pipe:0`, encoded
|
|
268
|
-
* bytes on `pipe:1`. PURE: no spawn, no env access.
|
|
269
|
-
*
|
|
270
|
-
* ffmpeg <low-latency> -f <s16le|f32le> -ar <src> -ac <srcCh> -i pipe:0 \
|
|
271
|
-
* -c:a <encoder> -ar <target> -ac <targetCh> [-b:a <k>k] \
|
|
272
|
-
* [-application lowdelay] -f <containerFmt> pipe:1
|
|
273
|
-
*
|
|
274
|
-
* For `libopus` the intercom-oriented `-application lowdelay` mode is added,
|
|
275
|
-
* trading a little quality for the lowest algorithmic latency.
|
|
276
|
-
*
|
|
277
|
-
* @throws if the codec has no known encode mapping.
|
|
278
|
-
*/
|
|
279
|
-
function buildAudioEncodeArgs(config) {
|
|
280
|
-
const entry = encodeFormatForCodec(config.codec);
|
|
281
|
-
if (!entry) throw new Error(`audio-codec-ffmpeg: no encode format for codec '${config.codec}'`);
|
|
282
|
-
const inFormat = config.sourceFormat ?? "s16le";
|
|
283
|
-
const bitrateArgs = entry.acceptsBitrate && config.bitrateKbps !== void 0 ? ["-b:a", `${config.bitrateKbps}k`] : [];
|
|
284
|
-
const opusLowDelay = entry.encoder === "libopus" ? ["-application", "lowdelay"] : [];
|
|
285
|
-
return [
|
|
286
|
-
...AUDIO_LOW_LATENCY_ARGS,
|
|
287
|
-
"-f",
|
|
288
|
-
inFormat,
|
|
289
|
-
"-ar",
|
|
290
|
-
String(config.sourceSampleRate),
|
|
291
|
-
"-ac",
|
|
292
|
-
String(config.sourceChannels),
|
|
293
|
-
"-i",
|
|
294
|
-
"pipe:0",
|
|
295
|
-
"-c:a",
|
|
296
|
-
entry.encoder,
|
|
297
|
-
"-ar",
|
|
298
|
-
String(config.targetSampleRate),
|
|
299
|
-
"-ac",
|
|
300
|
-
String(config.targetChannels),
|
|
301
|
-
...bitrateArgs,
|
|
302
|
-
...opusLowDelay,
|
|
303
|
-
"-f",
|
|
304
|
-
entry.outputFormat,
|
|
305
|
-
"pipe:1"
|
|
306
|
-
];
|
|
307
|
-
}
|
|
308
|
-
//#endregion
|
|
309
|
-
//#region src/audio-codec-ffmpeg/ogg-opus-framer.ts
|
|
310
|
-
/**
|
|
311
|
-
* Ogg-Opus encapsulator for the `audio-codec-ffmpeg` decode path.
|
|
312
|
-
*
|
|
313
|
-
* Cameras / RTP deliver RAW Opus packets — one self-delimited Opus packet per
|
|
314
|
-
* `pushEncodedFrame`. ffmpeg has NO raw-Opus demuxer, so the subprocess cannot
|
|
315
|
-
* decode a bare packet stream from a pipe (unlike `audio-codec-nodeav`, which
|
|
316
|
-
* feeds raw packets straight to `AV_CODEC_ID_OPUS` in-process). To decode Opus
|
|
317
|
-
* out-of-process we must wrap the raw packets in a valid **Ogg-Opus** container
|
|
318
|
-
* so `ffmpeg -f ogg -i pipe:0` can demux + decode them.
|
|
319
|
-
*
|
|
320
|
-
* This framer turns a stream of raw Opus packets into a byte stream shaped per
|
|
321
|
-
* RFC 7845 (Ogg Encapsulation for Opus) + RFC 3533 (the Ogg container):
|
|
322
|
-
*
|
|
323
|
-
* Page 0 (BOS) : one "OpusHead" identification packet
|
|
324
|
-
* Page 1 : one "OpusTags" comment packet
|
|
325
|
-
* Page 2..n : one Opus audio packet each (one-packet-per-page = lowest
|
|
326
|
-
* latency, simplest lacing), granule = running 48 kHz sample
|
|
327
|
-
* total.
|
|
328
|
-
* flush() : optional final EOS page.
|
|
329
|
-
*
|
|
330
|
-
* The Ogg page CRC is NOT the common zlib CRC-32: it uses polynomial
|
|
331
|
-
* 0x04C11DB7 with init 0, NO input/output bit reflection, and NO final XOR,
|
|
332
|
-
* computed over the whole page with the CRC field zeroed (see `oggCrc32`).
|
|
333
|
-
*/
|
|
334
|
-
/** Ogg capture pattern, "OggS". */
|
|
335
|
-
var OGG_CAPTURE_PATTERN = Buffer.from("OggS", "ascii");
|
|
336
|
-
/** Opus identification header magic, "OpusHead". */
|
|
337
|
-
var OPUS_HEAD_MAGIC = Buffer.from("OpusHead", "ascii");
|
|
338
|
-
/** Opus comment header magic, "OpusTags". */
|
|
339
|
-
var OPUS_TAGS_MAGIC = Buffer.from("OpusTags", "ascii");
|
|
340
|
-
/** Ogg page header byte offsets (before the segment table). */
|
|
341
|
-
var OGG_HEADER_FIXED_BYTES = 27;
|
|
342
|
-
/** Byte offset of the 4-byte CRC field within the fixed header. */
|
|
343
|
-
var OGG_CRC_OFFSET = 22;
|
|
344
|
-
var HEADER_TYPE_BOS = 2;
|
|
345
|
-
var HEADER_TYPE_EOS = 4;
|
|
346
|
-
/**
|
|
347
|
-
* OpusHead `pre-skip`: samples the decoder discards at stream start. 3840
|
|
348
|
-
* samples (80 ms at 48 kHz) is a common, safe over-estimate for libopus — it
|
|
349
|
-
* only trims a one-time startup transient and never drops steady-state audio.
|
|
350
|
-
* (The precise libopus encoder lookahead is 312 samples; for a live stream we
|
|
351
|
-
* do not need sample-accurate trimming, so the larger value is fine.)
|
|
352
|
-
*/
|
|
353
|
-
var DEFAULT_OPUS_PRE_SKIP = 3840;
|
|
354
|
-
/** OpusHead `input sample rate` — informational per RFC 7845 (§5.1). */
|
|
355
|
-
var OPUS_INPUT_SAMPLE_RATE = 48e3;
|
|
356
|
-
/** Opus granule positions are ALWAYS counted at 48 kHz (RFC 7845 §4). */
|
|
357
|
-
var OPUS_GRANULE_SAMPLE_RATE = 48e3;
|
|
358
|
-
/**
|
|
359
|
-
* Opus frame size, in 48 kHz samples, indexed by the 5-bit TOC `config`
|
|
360
|
-
* (0..31). Derived from the frame-duration table in RFC 6716 §3.1:
|
|
361
|
-
*
|
|
362
|
-
* config 0-11 SILK NB/MB/WB → 10 / 20 / 40 / 60 ms
|
|
363
|
-
* config 12-15 Hybrid SWB/FB → 10 / 20 ms
|
|
364
|
-
* config 16-31 CELT NB/…/FB → 2.5 / 5 / 10 / 20 ms
|
|
365
|
-
*
|
|
366
|
-
* samples@48k = ms * 48 (2.5→120, 5→240, 10→480, 20→960, 40→1920, 60→2880).
|
|
367
|
-
*/
|
|
368
|
-
var OPUS_FRAME_SAMPLES_48K = [
|
|
369
|
-
480,
|
|
370
|
-
960,
|
|
371
|
-
1920,
|
|
372
|
-
2880,
|
|
373
|
-
480,
|
|
374
|
-
960,
|
|
375
|
-
1920,
|
|
376
|
-
2880,
|
|
377
|
-
480,
|
|
378
|
-
960,
|
|
379
|
-
1920,
|
|
380
|
-
2880,
|
|
381
|
-
480,
|
|
382
|
-
960,
|
|
383
|
-
480,
|
|
384
|
-
960,
|
|
385
|
-
120,
|
|
386
|
-
240,
|
|
387
|
-
480,
|
|
388
|
-
960,
|
|
389
|
-
120,
|
|
390
|
-
240,
|
|
391
|
-
480,
|
|
392
|
-
960,
|
|
393
|
-
120,
|
|
394
|
-
240,
|
|
395
|
-
480,
|
|
396
|
-
960,
|
|
397
|
-
120,
|
|
398
|
-
240,
|
|
399
|
-
480,
|
|
400
|
-
960
|
|
401
|
-
];
|
|
402
|
-
/**
|
|
403
|
-
* Precomputed Ogg CRC-32 lookup table (MSB-first, polynomial 0x04C11DB7).
|
|
404
|
-
*
|
|
405
|
-
* Each entry is the CRC of the single byte `i` placed in the top position of a
|
|
406
|
-
* 32-bit register, shifted out MSB-first with no reflection. This is the table
|
|
407
|
-
* form of the bit-serial algorithm used by `oggCrc32`.
|
|
408
|
-
*/
|
|
409
|
-
var OGG_CRC_TABLE = (() => {
|
|
410
|
-
const table = new Uint32Array(256);
|
|
411
|
-
for (let i = 0; i < 256; i++) {
|
|
412
|
-
let r = i << 24 >>> 0;
|
|
413
|
-
for (let bit = 0; bit < 8; bit++) r = (r & 2147483648) !== 0 ? (r << 1 ^ 79764919) >>> 0 : r << 1 >>> 0;
|
|
414
|
-
table[i] = r >>> 0;
|
|
415
|
-
}
|
|
416
|
-
return table;
|
|
417
|
-
})();
|
|
418
|
-
/**
|
|
419
|
-
* Ogg page CRC-32 (RFC 3533 §4). Polynomial 0x04C11DB7, init 0, NO input or
|
|
420
|
-
* output reflection, NO final XOR — deliberately different from the reflected
|
|
421
|
-
* zlib/IEEE CRC-32. Computed over the ENTIRE page bytes with the 4-byte CRC
|
|
422
|
-
* field already zeroed.
|
|
423
|
-
*/
|
|
424
|
-
function oggCrc32(data) {
|
|
425
|
-
let crc = 0;
|
|
426
|
-
for (let i = 0; i < data.length; i++) {
|
|
427
|
-
const idx = (crc >>> 24 ^ (data[i] ?? 0)) & 255;
|
|
428
|
-
crc = (crc << 8 >>> 0 ^ (OGG_CRC_TABLE[idx] ?? 0)) >>> 0;
|
|
429
|
-
}
|
|
430
|
-
return crc >>> 0;
|
|
431
|
-
}
|
|
432
|
-
/**
|
|
433
|
-
* Decode an Opus packet's total duration in samples from its TOC byte
|
|
434
|
-
* (RFC 6716 §3). Returns samples at `sampleRate` (default 48 kHz — the rate Ogg
|
|
435
|
-
* granule positions are counted in).
|
|
436
|
-
*
|
|
437
|
-
* TOC byte layout: `config` (bits 3-7), `s` stereo (bit 2), `c` frame-count
|
|
438
|
-
* code (bits 0-1):
|
|
439
|
-
* - code 0 → 1 frame
|
|
440
|
-
* - code 1 / 2 → 2 frames
|
|
441
|
-
* - code 3 → arbitrary; frame count = low 6 bits of the byte after the TOC.
|
|
442
|
-
*
|
|
443
|
-
* Returns 0 for an empty or malformed (code-3 with no count byte) packet.
|
|
444
|
-
*/
|
|
445
|
-
function opusPacketSampleCount(packet, sampleRate = 48e3) {
|
|
446
|
-
if (packet.length < 1) return 0;
|
|
447
|
-
const toc = packet[0] ?? 0;
|
|
448
|
-
const config = toc >> 3;
|
|
449
|
-
const code = toc & 3;
|
|
450
|
-
const samplesPerFrame48k = OPUS_FRAME_SAMPLES_48K[config] ?? 0;
|
|
451
|
-
if (samplesPerFrame48k === 0) return 0;
|
|
452
|
-
let frameCount;
|
|
453
|
-
if (code === 0) frameCount = 1;
|
|
454
|
-
else if (code === 1 || code === 2) frameCount = 2;
|
|
455
|
-
else {
|
|
456
|
-
if (packet.length < 2) return 0;
|
|
457
|
-
frameCount = (packet[1] ?? 0) & 63;
|
|
458
|
-
}
|
|
459
|
-
const samples48k = samplesPerFrame48k * frameCount;
|
|
460
|
-
if (sampleRate === OPUS_GRANULE_SAMPLE_RATE) return samples48k;
|
|
461
|
-
return Math.round(samples48k * sampleRate / OPUS_GRANULE_SAMPLE_RATE);
|
|
462
|
-
}
|
|
463
|
-
/**
|
|
464
|
-
* Encode an Ogg lacing segment table for a single packet of length `len`
|
|
465
|
-
* (RFC 3533 §6): `floor(len/255)` bytes of 0xFF then one final byte `len%255`.
|
|
466
|
-
* A length that is an exact multiple of 255 therefore ends in a `0x00` lacing
|
|
467
|
-
* value — required so the demuxer knows the packet terminates on this page.
|
|
468
|
-
*/
|
|
469
|
-
function buildLacing(len) {
|
|
470
|
-
const full = Math.floor(len / 255);
|
|
471
|
-
const table = Buffer.alloc(full + 1);
|
|
472
|
-
table.fill(255, 0, full);
|
|
473
|
-
table[full] = len % 255;
|
|
474
|
-
return table;
|
|
475
|
-
}
|
|
476
|
-
/**
|
|
477
|
-
* Turn a hash-derivable seed into a stable 32-bit Ogg bitstream serial. FNV-1a
|
|
478
|
-
* over the seed string keeps distinct sessions on distinct serials without
|
|
479
|
-
* requiring the caller to allocate one.
|
|
480
|
-
*/
|
|
481
|
-
function serialFromSeed(seed) {
|
|
482
|
-
let hash = 2166136261;
|
|
483
|
-
for (let i = 0; i < seed.length; i++) {
|
|
484
|
-
hash ^= seed.charCodeAt(i) & 255;
|
|
485
|
-
hash = Math.imul(hash, 16777619);
|
|
486
|
-
}
|
|
487
|
-
return hash >>> 0;
|
|
488
|
-
}
|
|
489
|
-
/**
|
|
490
|
-
* Streaming Ogg-Opus encapsulator. Stateful: tracks page sequence numbers and
|
|
491
|
-
* the running 48 kHz granule position across calls. One instance per decode
|
|
492
|
-
* session; call {@link reset} to reuse it for a fresh stream.
|
|
493
|
-
*/
|
|
494
|
-
var OggOpusFramer = class {
|
|
495
|
-
channelCount;
|
|
496
|
-
serial;
|
|
497
|
-
preSkip;
|
|
498
|
-
vendor;
|
|
499
|
-
pageSeq = 0;
|
|
500
|
-
granule = 0;
|
|
501
|
-
headersEmitted = false;
|
|
502
|
-
audioEmitted = false;
|
|
503
|
-
constructor(config) {
|
|
504
|
-
this.channelCount = config.channelCount;
|
|
505
|
-
this.serial = config.serial >>> 0;
|
|
506
|
-
this.preSkip = config.preSkip ?? 3840;
|
|
507
|
-
this.vendor = config.vendor ?? "camstack";
|
|
508
|
-
}
|
|
509
|
-
/**
|
|
510
|
-
* Build the two header pages (OpusHead BOS + OpusTags). Advances the page
|
|
511
|
-
* sequence to 2 so audio pages follow. Returns an empty buffer if the headers
|
|
512
|
-
* were already emitted (idempotent within a stream).
|
|
513
|
-
*/
|
|
514
|
-
headerPages() {
|
|
515
|
-
if (this.headersEmitted) return Buffer.alloc(0);
|
|
516
|
-
this.headersEmitted = true;
|
|
517
|
-
const head = this.buildOpusHead();
|
|
518
|
-
const tags = this.buildOpusTags();
|
|
519
|
-
const headPage = this.buildPage(head, HEADER_TYPE_BOS, 0);
|
|
520
|
-
const tagsPage = this.buildPage(tags, 0, 0);
|
|
521
|
-
return Buffer.concat([headPage, tagsPage]);
|
|
522
|
-
}
|
|
523
|
-
/**
|
|
524
|
-
* Encapsulate one raw Opus packet as a single audio page, advancing the
|
|
525
|
-
* granule by the packet's decoded sample count. Header pages are emitted
|
|
526
|
-
* (and prepended) automatically on the first call, so the caller can simply
|
|
527
|
-
* write the returned bytes to ffmpeg stdin.
|
|
528
|
-
*/
|
|
529
|
-
framePacket(packet) {
|
|
530
|
-
const prefix = this.headersEmitted ? Buffer.alloc(0) : this.headerPages();
|
|
531
|
-
this.granule += opusPacketSampleCount(packet, OPUS_GRANULE_SAMPLE_RATE);
|
|
532
|
-
this.audioEmitted = true;
|
|
533
|
-
const page = this.buildPage(packet, 0, this.granule);
|
|
534
|
-
return prefix.length > 0 ? Buffer.concat([prefix, page]) : page;
|
|
535
|
-
}
|
|
536
|
-
/**
|
|
537
|
-
* Emit a final EOS page terminating the logical bitstream. Returns `null`
|
|
538
|
-
* when no audio was written (nothing to terminate). The EOS page carries no
|
|
539
|
-
* packet data (0 segments) and the final granule position.
|
|
540
|
-
*/
|
|
541
|
-
flush() {
|
|
542
|
-
if (!this.audioEmitted) return null;
|
|
543
|
-
return this.buildPage(null, HEADER_TYPE_EOS, this.granule);
|
|
544
|
-
}
|
|
545
|
-
/** Reset all page/granule state so the instance can frame a fresh stream. */
|
|
546
|
-
reset() {
|
|
547
|
-
this.pageSeq = 0;
|
|
548
|
-
this.granule = 0;
|
|
549
|
-
this.headersEmitted = false;
|
|
550
|
-
this.audioEmitted = false;
|
|
551
|
-
}
|
|
552
|
-
/** OpusHead identification packet (RFC 7845 §5.1) — 19 bytes for family 0. */
|
|
553
|
-
buildOpusHead() {
|
|
554
|
-
const buf = Buffer.alloc(19);
|
|
555
|
-
OPUS_HEAD_MAGIC.copy(buf, 0);
|
|
556
|
-
buf.writeUInt8(1, 8);
|
|
557
|
-
buf.writeUInt8(this.channelCount, 9);
|
|
558
|
-
buf.writeUInt16LE(this.preSkip & 65535, 10);
|
|
559
|
-
buf.writeUInt32LE(OPUS_INPUT_SAMPLE_RATE, 12);
|
|
560
|
-
buf.writeInt16LE(0, 16);
|
|
561
|
-
buf.writeUInt8(0, 18);
|
|
562
|
-
return buf;
|
|
563
|
-
}
|
|
564
|
-
/** OpusTags comment packet (RFC 7845 §5.2) — magic, vendor, 0 comments. */
|
|
565
|
-
buildOpusTags() {
|
|
566
|
-
const vendor = Buffer.from(this.vendor, "utf8");
|
|
567
|
-
const buf = Buffer.alloc(12 + vendor.length + 4);
|
|
568
|
-
OPUS_TAGS_MAGIC.copy(buf, 0);
|
|
569
|
-
buf.writeUInt32LE(vendor.length, 8);
|
|
570
|
-
vendor.copy(buf, 12);
|
|
571
|
-
buf.writeUInt32LE(0, 12 + vendor.length);
|
|
572
|
-
return buf;
|
|
573
|
-
}
|
|
574
|
-
/**
|
|
575
|
-
* Assemble one Ogg page around a single packet (or none, for an EOS
|
|
576
|
-
* terminator), compute its CRC, and return the framed bytes. Keeps to a
|
|
577
|
-
* single packet per page so the segment count never approaches the 255 limit
|
|
578
|
-
* (an Opus packet is ≤ ~1275 bytes → ≤ 6 lacing segments).
|
|
579
|
-
*/
|
|
580
|
-
buildPage(packet, headerType, granule) {
|
|
581
|
-
const lacing = packet !== null ? buildLacing(packet.length) : Buffer.alloc(0);
|
|
582
|
-
const segmentCount = lacing.length;
|
|
583
|
-
const bodyLen = packet !== null ? packet.length : 0;
|
|
584
|
-
const page = Buffer.alloc(OGG_HEADER_FIXED_BYTES + segmentCount + bodyLen);
|
|
585
|
-
OGG_CAPTURE_PATTERN.copy(page, 0);
|
|
586
|
-
page.writeUInt8(0, 4);
|
|
587
|
-
page.writeUInt8(headerType & 7, 5);
|
|
588
|
-
this.writeGranule(page, granule, 6);
|
|
589
|
-
page.writeUInt32LE(this.serial, 14);
|
|
590
|
-
page.writeUInt32LE(this.pageSeq >>> 0, 18);
|
|
591
|
-
page.writeUInt32LE(0, OGG_CRC_OFFSET);
|
|
592
|
-
page.writeUInt8(segmentCount, 26);
|
|
593
|
-
lacing.copy(page, OGG_HEADER_FIXED_BYTES);
|
|
594
|
-
if (packet !== null) packet.copy(page, OGG_HEADER_FIXED_BYTES + segmentCount);
|
|
595
|
-
const crc = oggCrc32(page);
|
|
596
|
-
page.writeUInt32LE(crc, OGG_CRC_OFFSET);
|
|
597
|
-
this.pageSeq = this.pageSeq + 1 >>> 0;
|
|
598
|
-
return page;
|
|
599
|
-
}
|
|
600
|
-
/**
|
|
601
|
-
* Write a 64-bit little-endian granule position. Values fit comfortably in a
|
|
602
|
-
* JS safe integer for any realistic stream duration (2^53 samples @48k ≈ 5940
|
|
603
|
-
* years), so a split 32-bit lo/hi write is exact and avoids BigInt on the
|
|
604
|
-
* hot path.
|
|
605
|
-
*/
|
|
606
|
-
writeGranule(page, granule, offset) {
|
|
607
|
-
const lo = granule >>> 0;
|
|
608
|
-
const hi = Math.floor(granule / 4294967296) >>> 0;
|
|
609
|
-
page.writeUInt32LE(lo, offset);
|
|
610
|
-
page.writeUInt32LE(hi, offset + 4);
|
|
611
|
-
}
|
|
612
|
-
};
|
|
613
|
-
//#endregion
|
|
614
|
-
//#region src/audio-codec-ffmpeg/adts.ts
|
|
615
|
-
/**
|
|
616
|
-
* ADTS framing for raw AAC access units.
|
|
617
|
-
*
|
|
618
|
-
* The audio decode path runs `ffmpeg -f aac -i pipe:0` — the **ADTS** demuxer,
|
|
619
|
-
* which requires each frame to begin with a 7-byte ADTS header (0xFFF sync).
|
|
620
|
-
* But every AAC source that reaches the decoder delivers **raw** AAC access
|
|
621
|
-
* units with no ADTS header: push sources (Reolink Baichuan) emit one bare
|
|
622
|
-
* codec frame per packet, and rfc3640 (RTP AAC) depacketization yields bare
|
|
623
|
-
* AUs too. Piping those straight into `-f aac` fails with
|
|
624
|
-
* `Invalid data found when processing input` → no PCM → a silent WebRTC audio
|
|
625
|
-
* track. Wrapping each raw AU in an ADTS header (built from the source
|
|
626
|
-
* sample-rate / channel-count / AAC object type) makes the demuxer accept it.
|
|
627
|
-
*
|
|
628
|
-
* Opus is handled separately (Ogg encapsulation); this module is AAC-only.
|
|
629
|
-
*/
|
|
630
|
-
/** MPEG-4 AAC sampling-frequency index table (ISO/IEC 14496-3). */
|
|
631
|
-
var AAC_SAMPLE_RATES = [
|
|
632
|
-
96e3,
|
|
633
|
-
88200,
|
|
634
|
-
64e3,
|
|
635
|
-
48e3,
|
|
636
|
-
44100,
|
|
637
|
-
32e3,
|
|
638
|
-
24e3,
|
|
639
|
-
22050,
|
|
640
|
-
16e3,
|
|
641
|
-
12e3,
|
|
642
|
-
11025,
|
|
643
|
-
8e3,
|
|
644
|
-
7350
|
|
645
|
-
];
|
|
646
|
-
/** AAC-LC is object type 2; the ADTS `profile` field is objectType - 1. */
|
|
647
|
-
var DEFAULT_AAC_OBJECT_TYPE = 2;
|
|
648
|
-
/** Resolve the ADTS sampling-frequency index for a sample rate (default 16 kHz → 8). */
|
|
649
|
-
function aacSampleRateIndex(sampleRate) {
|
|
650
|
-
const idx = AAC_SAMPLE_RATES.indexOf(sampleRate);
|
|
651
|
-
return idx >= 0 ? idx : AAC_SAMPLE_RATES.indexOf(16e3);
|
|
652
|
-
}
|
|
653
|
-
/**
|
|
654
|
-
* True when `buf` already begins with an ADTS syncword (0xFFF) + MPEG layer 0.
|
|
655
|
-
* Raw AAC AUs never do; ADTS-framed frames always do. Lets the wrapper pass
|
|
656
|
-
* already-framed input through untouched.
|
|
657
|
-
*/
|
|
658
|
-
function hasAdtsSync(buf) {
|
|
659
|
-
return buf.length >= 2 && buf[0] === 255 && (buf[1] & 246) === 240;
|
|
660
|
-
}
|
|
661
|
-
/**
|
|
662
|
-
* Build the 7-byte ADTS header (no CRC) for a payload of `payloadLength` bytes.
|
|
663
|
-
*/
|
|
664
|
-
function buildAdtsHeader(config, payloadLength) {
|
|
665
|
-
const objectType = config.aacObjectType ?? DEFAULT_AAC_OBJECT_TYPE;
|
|
666
|
-
const profile = Math.max(0, objectType - 1) & 3;
|
|
667
|
-
const freqIdx = aacSampleRateIndex(config.sampleRate) & 15;
|
|
668
|
-
const chanCfg = Math.max(1, config.channels) & 7;
|
|
669
|
-
const frameLength = payloadLength + 7;
|
|
670
|
-
const h = Buffer.alloc(7);
|
|
671
|
-
h[0] = 255;
|
|
672
|
-
h[1] = 241;
|
|
673
|
-
h[2] = profile << 6 | freqIdx << 2 | chanCfg >> 2 & 1;
|
|
674
|
-
h[3] = (chanCfg & 3) << 6 | frameLength >> 11 & 3;
|
|
675
|
-
h[4] = frameLength >> 3 & 255;
|
|
676
|
-
h[5] = (frameLength & 7) << 5 | 31;
|
|
677
|
-
h[6] = 252;
|
|
678
|
-
return h;
|
|
679
|
-
}
|
|
680
|
-
/**
|
|
681
|
-
* Return `frame` framed as ADTS: unchanged when it already carries an ADTS
|
|
682
|
-
* sync, otherwise the 7-byte header prepended. Pure; the input is never mutated.
|
|
683
|
-
*/
|
|
684
|
-
function wrapAacAsAdts(frame, config) {
|
|
685
|
-
if (hasAdtsSync(frame)) return Buffer.from(frame.buffer, frame.byteOffset, frame.byteLength);
|
|
686
|
-
const payload = Buffer.from(frame.buffer, frame.byteOffset, frame.byteLength);
|
|
687
|
-
return Buffer.concat([buildAdtsHeader(config, payload.length), payload]);
|
|
688
|
-
}
|
|
689
|
-
//#endregion
|
|
690
|
-
//#region src/audio-codec-ffmpeg/ffmpeg-audio-decode-session.ts
|
|
691
|
-
var FfmpegAudioDecodeSession = class {
|
|
692
|
-
logger;
|
|
693
|
-
process;
|
|
694
|
-
outFormat;
|
|
695
|
-
bytesPerFrame;
|
|
696
|
-
targetSampleRate;
|
|
697
|
-
targetChannels;
|
|
698
|
-
onChunk;
|
|
699
|
-
/**
|
|
700
|
-
* Ogg-Opus encapsulator, present ONLY when the codec is Opus. Raw Opus
|
|
701
|
-
* packets have no ffmpeg demuxer, so each pushed packet is wrapped in an
|
|
702
|
-
* Ogg page (with OpusHead/OpusTags emitted before the first) so
|
|
703
|
-
* `ffmpeg -f ogg -i pipe:0` can demux + decode. `null` for all other codecs,
|
|
704
|
-
* which pipe their encoded frames straight through.
|
|
705
|
-
*/
|
|
706
|
-
oggFramer;
|
|
707
|
-
/**
|
|
708
|
-
* ADTS framing config, present ONLY when the codec is AAC. `ffmpeg -f aac`
|
|
709
|
-
* is the ADTS demuxer, but every AAC source delivers raw AAC access units
|
|
710
|
-
* (push sources emit bare frames; rfc3640 depacketization yields bare AUs) —
|
|
711
|
-
* piping those in raw fails `Invalid data found when processing input` → no
|
|
712
|
-
* PCM → silent WebRTC audio. Each frame is ADTS-wrapped before ffmpeg.
|
|
713
|
-
*/
|
|
714
|
-
adtsConfig;
|
|
715
|
-
residual = Buffer.alloc(0);
|
|
716
|
-
nextPts = 0;
|
|
717
|
-
destroyed = false;
|
|
718
|
-
constructor(params, logger, onChunk) {
|
|
719
|
-
this.logger = logger;
|
|
720
|
-
this.onChunk = onChunk;
|
|
721
|
-
this.outFormat = params.targetFormat ?? "s16le";
|
|
722
|
-
this.targetSampleRate = params.targetSampleRate;
|
|
723
|
-
this.targetChannels = params.targetChannels;
|
|
724
|
-
this.bytesPerFrame = Math.max(1, params.targetChannels * audioBytesPerSample(this.outFormat));
|
|
725
|
-
this.oggFramer = resolveAudioCodecAlias(params.codec) === "opus" ? new OggOpusFramer({
|
|
726
|
-
channelCount: Math.max(1, params.sourceChannels),
|
|
727
|
-
serial: serialFromSeed(params.oggSerialSeed ?? "audio-codec-ffmpeg-opus")
|
|
728
|
-
}) : null;
|
|
729
|
-
this.adtsConfig = resolveAudioCodecAlias(params.codec) === "aac" ? {
|
|
730
|
-
sampleRate: params.sourceSampleRate,
|
|
731
|
-
channels: Math.max(1, params.sourceChannels)
|
|
732
|
-
} : null;
|
|
733
|
-
const args = buildAudioDecodeArgs(params);
|
|
734
|
-
this.process = new FfmpegAudioProcess({
|
|
735
|
-
ffmpegPath: params.ffmpegPath,
|
|
736
|
-
args,
|
|
737
|
-
logger,
|
|
738
|
-
onStdout: (chunk) => this.handleStdout(chunk),
|
|
739
|
-
onExit: (code, signal) => {
|
|
740
|
-
if (this.destroyed) return;
|
|
741
|
-
this.logger.warn("audio-codec-ffmpeg: decode child exited", { meta: {
|
|
742
|
-
code,
|
|
743
|
-
signal
|
|
744
|
-
} });
|
|
745
|
-
}
|
|
746
|
-
});
|
|
747
|
-
}
|
|
748
|
-
/** Push one encoded audio frame into ffmpeg stdin. */
|
|
749
|
-
pushEncoded(data) {
|
|
750
|
-
if (this.destroyed) return;
|
|
751
|
-
if (this.oggFramer) {
|
|
752
|
-
const packet = Buffer.from(data.buffer, data.byteOffset, data.byteLength);
|
|
753
|
-
this.process.write(this.oggFramer.framePacket(packet));
|
|
754
|
-
return;
|
|
755
|
-
}
|
|
756
|
-
if (this.adtsConfig) {
|
|
757
|
-
this.process.write(wrapAacAsAdts(data, this.adtsConfig));
|
|
758
|
-
return;
|
|
759
|
-
}
|
|
760
|
-
this.process.write(data);
|
|
761
|
-
}
|
|
762
|
-
handleStdout(chunk) {
|
|
763
|
-
if (this.destroyed) return;
|
|
764
|
-
const buf = this.residual.length === 0 ? chunk : Buffer.concat([this.residual, chunk]);
|
|
765
|
-
const frameBytes = this.bytesPerFrame;
|
|
766
|
-
const usable = buf.length - buf.length % frameBytes;
|
|
767
|
-
if (usable <= 0) {
|
|
768
|
-
this.residual = buf;
|
|
769
|
-
return;
|
|
770
|
-
}
|
|
771
|
-
const emit = buf.subarray(0, usable);
|
|
772
|
-
this.residual = usable < buf.length ? Buffer.from(buf.subarray(usable)) : Buffer.alloc(0);
|
|
773
|
-
const out = new Uint8Array(new ArrayBuffer(usable));
|
|
774
|
-
out.set(emit);
|
|
775
|
-
const samples = usable / frameBytes;
|
|
776
|
-
const pts = this.nextPts;
|
|
777
|
-
this.nextPts = pts + Math.round(samples * 1e3 / this.targetSampleRate);
|
|
778
|
-
this.onChunk({
|
|
779
|
-
data: out,
|
|
780
|
-
sampleRate: this.targetSampleRate,
|
|
781
|
-
channels: this.targetChannels,
|
|
782
|
-
format: this.outFormat,
|
|
783
|
-
pts
|
|
784
|
-
});
|
|
785
|
-
}
|
|
786
|
-
destroy() {
|
|
787
|
-
if (this.destroyed) return;
|
|
788
|
-
this.destroyed = true;
|
|
789
|
-
if (this.oggFramer) {
|
|
790
|
-
const eos = this.oggFramer.flush();
|
|
791
|
-
if (eos) this.process.write(eos);
|
|
792
|
-
}
|
|
793
|
-
this.process.kill();
|
|
794
|
-
this.residual = Buffer.alloc(0);
|
|
795
|
-
}
|
|
796
|
-
};
|
|
797
|
-
//#endregion
|
|
798
|
-
//#region src/audio-codec-ffmpeg/ffmpeg-audio-encode-session.ts
|
|
799
|
-
var FfmpegAudioEncodeSession = class {
|
|
800
|
-
logger;
|
|
801
|
-
process;
|
|
802
|
-
codec;
|
|
803
|
-
onChunk;
|
|
804
|
-
nextPts = 0;
|
|
805
|
-
destroyed = false;
|
|
806
|
-
constructor(params, logger, onChunk) {
|
|
807
|
-
this.logger = logger;
|
|
808
|
-
this.onChunk = onChunk;
|
|
809
|
-
this.codec = resolveAudioCodecAlias(params.codec);
|
|
810
|
-
const args = buildAudioEncodeArgs(params);
|
|
811
|
-
this.process = new FfmpegAudioProcess({
|
|
812
|
-
ffmpegPath: params.ffmpegPath,
|
|
813
|
-
args,
|
|
814
|
-
logger,
|
|
815
|
-
onStdout: (chunk) => this.handleStdout(chunk),
|
|
816
|
-
onExit: (code, signal) => {
|
|
817
|
-
if (this.destroyed) return;
|
|
818
|
-
this.logger.warn("audio-codec-ffmpeg: encode child exited", { meta: {
|
|
819
|
-
code,
|
|
820
|
-
signal
|
|
821
|
-
} });
|
|
822
|
-
}
|
|
823
|
-
});
|
|
824
|
-
}
|
|
825
|
-
/** Push one PCM chunk into ffmpeg stdin. */
|
|
826
|
-
pushPcm(data) {
|
|
827
|
-
if (this.destroyed) return;
|
|
828
|
-
this.process.write(data);
|
|
829
|
-
}
|
|
830
|
-
/** Close stdin so ffmpeg flushes and drains the remaining encoded output. */
|
|
831
|
-
flush() {
|
|
832
|
-
if (this.destroyed) return;
|
|
833
|
-
this.process.endStdin();
|
|
834
|
-
}
|
|
835
|
-
handleStdout(chunk) {
|
|
836
|
-
if (this.destroyed || chunk.length === 0) return;
|
|
837
|
-
const out = new Uint8Array(new ArrayBuffer(chunk.length));
|
|
838
|
-
out.set(chunk);
|
|
839
|
-
const pts = this.nextPts;
|
|
840
|
-
this.nextPts = pts + 1;
|
|
841
|
-
this.onChunk({
|
|
842
|
-
data: out,
|
|
843
|
-
codec: this.codec,
|
|
844
|
-
pts,
|
|
845
|
-
frameComplete: true
|
|
846
|
-
});
|
|
847
|
-
}
|
|
848
|
-
destroy() {
|
|
849
|
-
if (this.destroyed) return;
|
|
850
|
-
this.destroyed = true;
|
|
851
|
-
this.process.kill();
|
|
852
|
-
}
|
|
853
|
-
};
|
|
854
|
-
//#endregion
|
|
855
|
-
//#region src/audio-codec-ffmpeg/addon/index.ts
|
|
856
|
-
var CODEC_CATALOG = [
|
|
857
|
-
{
|
|
858
|
-
codec: "pcm_mulaw",
|
|
859
|
-
canDecode: true,
|
|
860
|
-
canEncode: true,
|
|
861
|
-
label: "PCM µ-law (G.711)"
|
|
862
|
-
},
|
|
863
|
-
{
|
|
864
|
-
codec: "pcm_alaw",
|
|
865
|
-
canDecode: true,
|
|
866
|
-
canEncode: true,
|
|
867
|
-
label: "PCM A-law (G.711)"
|
|
868
|
-
},
|
|
869
|
-
{
|
|
870
|
-
codec: "g722",
|
|
871
|
-
canDecode: true,
|
|
872
|
-
canEncode: true,
|
|
873
|
-
label: "G.722"
|
|
874
|
-
},
|
|
875
|
-
{
|
|
876
|
-
codec: "aac",
|
|
877
|
-
canDecode: true,
|
|
878
|
-
canEncode: true,
|
|
879
|
-
label: "AAC"
|
|
880
|
-
},
|
|
881
|
-
{
|
|
882
|
-
codec: "aac_latm",
|
|
883
|
-
canDecode: true,
|
|
884
|
-
canEncode: false,
|
|
885
|
-
label: "AAC LATM"
|
|
886
|
-
},
|
|
887
|
-
{
|
|
888
|
-
codec: "mpeg4-generic",
|
|
889
|
-
canDecode: true,
|
|
890
|
-
canEncode: false,
|
|
891
|
-
label: "AAC (MPEG4-GENERIC)"
|
|
892
|
-
},
|
|
893
|
-
{
|
|
894
|
-
codec: "opus",
|
|
895
|
-
canDecode: true,
|
|
896
|
-
canEncode: true,
|
|
897
|
-
label: "Opus"
|
|
898
|
-
}
|
|
899
|
-
];
|
|
900
|
-
var DEFAULT_IDLE_MS = 3e4;
|
|
901
|
-
var MAX_PCM_QUEUE_CHUNKS = 500;
|
|
902
|
-
var REAPER_INTERVAL_MS = 5e3;
|
|
903
|
-
/**
|
|
904
|
-
* Grace wait in `flushEncode` for ffmpeg to drain its encoder after stdin is
|
|
905
|
-
* closed. This is a graceful-teardown path (not the real-time push path), so a
|
|
906
|
-
* short wait is acceptable to capture the encoder's tail output.
|
|
907
|
-
*/
|
|
908
|
-
var FLUSH_DRAIN_MS = 60;
|
|
909
|
-
var DEFAULT_GLOBAL_CONFIG = { defaultIdleMs: DEFAULT_IDLE_MS };
|
|
910
|
-
/**
|
|
911
|
-
* Audio codec I/O box backed by an **ffmpeg subprocess** (Phase B replacement
|
|
912
|
-
* for `audio-codec-nodeav`, which runs libavcodec + libswresample in-process).
|
|
913
|
-
*
|
|
914
|
-
* Each `createDecodeSession` / `createEncodeSession` spawns its own ffmpeg
|
|
915
|
-
* child, so consumers never share a resampler and a codec crash is isolated to
|
|
916
|
-
* the child — the addon runner survives. Cap surface + session bookkeeping
|
|
917
|
-
* mirror the node-av addon exactly; only the codec backend changed.
|
|
918
|
-
*
|
|
919
|
-
* This addon is the cap's `preferredProvider` (declared on
|
|
920
|
-
* `audio-codec.cap.ts`), so with no operator override it wins the singleton
|
|
921
|
-
* active slot.
|
|
922
|
-
*/
|
|
923
|
-
var AudioCodecFfmpegAddon = class extends BaseAddon {
|
|
924
|
-
sessions = /* @__PURE__ */ new Map();
|
|
925
|
-
reaperTimer = null;
|
|
926
|
-
logger = null;
|
|
927
|
-
/**
|
|
928
|
-
* The ffmpeg binary every session spawns — resolved once at init from the
|
|
929
|
-
* operator `ffmpeg.binaryPath` override, else provisioned via
|
|
930
|
-
* `ctx.deps.ensureFfmpeg()`. Same binary the broker / recorder / decoder use.
|
|
931
|
-
*/
|
|
932
|
-
ffmpegPath = "ffmpeg";
|
|
933
|
-
constructor() {
|
|
934
|
-
super(DEFAULT_GLOBAL_CONFIG);
|
|
935
|
-
}
|
|
936
|
-
async onInitialize() {
|
|
937
|
-
this.logger = this.ctx.logger;
|
|
938
|
-
this.ffmpegPath = await this.resolveFfmpegBinaryPath();
|
|
939
|
-
this.reaperTimer = setInterval(() => this.reapIdleSessions(), REAPER_INTERVAL_MS);
|
|
940
|
-
if (typeof this.reaperTimer.unref === "function") this.reaperTimer.unref();
|
|
941
|
-
return [{
|
|
942
|
-
capability: audioCodecCapability,
|
|
943
|
-
provider: this
|
|
944
|
-
}];
|
|
945
|
-
}
|
|
946
|
-
async onShutdown() {
|
|
947
|
-
if (this.reaperTimer) {
|
|
948
|
-
clearInterval(this.reaperTimer);
|
|
949
|
-
this.reaperTimer = null;
|
|
950
|
-
}
|
|
951
|
-
for (const s of this.sessions.values()) this.disposeSession(s);
|
|
952
|
-
this.sessions.clear();
|
|
953
|
-
}
|
|
954
|
-
/**
|
|
955
|
-
* Resolve the ffmpeg binary the sessions spawn.
|
|
956
|
-
*
|
|
957
|
-
* Precedence (mirrors `decoder-ffmpeg`):
|
|
958
|
-
* 1. Operator override in the cluster `ffmpeg` config section (`binaryPath`).
|
|
959
|
-
* 2. Otherwise `ctx.deps.ensureFfmpeg()` — provisions a portable static
|
|
960
|
-
* ffmpeg into the node's deps dir when the system has none.
|
|
961
|
-
*
|
|
962
|
-
* On failure, logs a loud ERROR and falls back to PATH `ffmpeg`; the addon
|
|
963
|
-
* still registers the cap and a later spawn re-runs against the same path.
|
|
964
|
-
*/
|
|
965
|
-
async resolveFfmpegBinaryPath() {
|
|
966
|
-
try {
|
|
967
|
-
return await this.ctx.deps.ensureFfmpeg();
|
|
968
|
-
} catch (err) {
|
|
969
|
-
this.ctx.logger.error("audio-codec-ffmpeg: ensureFfmpeg() failed to provision ffmpeg — falling back to PATH \"ffmpeg\"", { meta: { error: errMsg(err) } });
|
|
970
|
-
return "ffmpeg";
|
|
971
|
-
}
|
|
972
|
-
}
|
|
973
|
-
async listSupportedCodecs() {
|
|
974
|
-
return CODEC_CATALOG.map((e) => ({
|
|
975
|
-
codec: e.codec,
|
|
976
|
-
canDecode: e.canDecode,
|
|
977
|
-
canEncode: e.canEncode,
|
|
978
|
-
...e.label ? { label: e.label } : {}
|
|
979
|
-
}));
|
|
980
|
-
}
|
|
981
|
-
async canHandle(input) {
|
|
982
|
-
const resolved = resolveAudioCodecAlias(input.codec);
|
|
983
|
-
const entry = CODEC_CATALOG.find((e) => resolveAudioCodecAlias(e.codec) === resolved);
|
|
984
|
-
if (!entry) return false;
|
|
985
|
-
return input.kind === "decode" ? entry.canDecode : entry.canEncode;
|
|
986
|
-
}
|
|
987
|
-
/**
|
|
988
|
-
* The BARE cluster node id this addon runs on (e.g. `hub`) — stamped into a
|
|
989
|
-
* session's return so the caller pins its follow-up `pushEncodedFrame` /
|
|
990
|
-
* `pullPcm` / `closeSession` to the same node. `ctx.kernel.localNodeId` is
|
|
991
|
-
* the COMPOUND `<node>/<addon>` form (e.g. `hub/audio-codec-ffmpeg`), but the
|
|
992
|
-
* cap route resolver matches an explicit `nodeId` against the bare node id
|
|
993
|
-
* (`classifyExplicitNode`: `nodeId === hubNodeId`). Returning the compound
|
|
994
|
-
* form makes every pinned call fall through to a remote-Moleculer dispatch
|
|
995
|
-
* for a non-existent service → `Services waiting is timed out` → no WebRTC
|
|
996
|
-
* audio, cluster-wide. Strip the addon suffix. Mirrors `decoder-ffmpeg`'s
|
|
997
|
-
* `resolveLocalNodeId`.
|
|
998
|
-
*
|
|
999
|
-
* TODO(arch): this bare-node normalization is duplicated across
|
|
1000
|
-
* `decoder-ffmpeg`, `decoder-nodeav`, `audio-codec-ffmpeg`, and
|
|
1001
|
-
* `decoder-backend-keys.normalizeDecoderNodeId`. It should live ONCE on the
|
|
1002
|
-
* shared surface — either a bare `ctx.kernel.nodeId` accessor alongside the
|
|
1003
|
-
* compound `localNodeId`, or a base-addon helper — so no addon can leak the
|
|
1004
|
-
* compound `<node>/<addon>` id into a routing `nodeId` again (this exact bug).
|
|
1005
|
-
*/
|
|
1006
|
-
resolveLocalNodeId() {
|
|
1007
|
-
const raw = this.ctx.kernel.localNodeId ?? "local";
|
|
1008
|
-
return raw.includes("/") ? raw.split("/")[0] : raw;
|
|
1009
|
-
}
|
|
1010
|
-
async createDecodeSession(input) {
|
|
1011
|
-
const codec = resolveAudioCodecAlias(input.codec);
|
|
1012
|
-
const entry = CODEC_CATALOG.find((e) => resolveAudioCodecAlias(e.codec) === codec);
|
|
1013
|
-
if (!entry || !entry.canDecode) throw new Error(`audio-codec-ffmpeg: decode unsupported for codec '${input.codec}'`);
|
|
1014
|
-
const sessionId = `dec-${randomUUID()}`;
|
|
1015
|
-
const state = {
|
|
1016
|
-
sessionId,
|
|
1017
|
-
kind: "decode",
|
|
1018
|
-
config: {
|
|
1019
|
-
...input,
|
|
1020
|
-
codec
|
|
1021
|
-
},
|
|
1022
|
-
...input.tag ? { tag: input.tag } : {},
|
|
1023
|
-
createdAtMs: Date.now(),
|
|
1024
|
-
lastActivityMs: Date.now(),
|
|
1025
|
-
framesIn: 0,
|
|
1026
|
-
framesOut: 0,
|
|
1027
|
-
pcmQueue: [],
|
|
1028
|
-
session: null
|
|
1029
|
-
};
|
|
1030
|
-
state.session = this.spawnDecodeSession(state);
|
|
1031
|
-
this.sessions.set(sessionId, state);
|
|
1032
|
-
this.logger?.info("audio-codec-ffmpeg: decode session created", {
|
|
1033
|
-
tags: { sessionId },
|
|
1034
|
-
meta: {
|
|
1035
|
-
codec,
|
|
1036
|
-
target: `${input.targetSampleRate}Hz×${input.targetChannels}`
|
|
1037
|
-
}
|
|
1038
|
-
});
|
|
1039
|
-
return {
|
|
1040
|
-
sessionId,
|
|
1041
|
-
nodeId: this.resolveLocalNodeId()
|
|
1042
|
-
};
|
|
1043
|
-
}
|
|
1044
|
-
async createEncodeSession(input) {
|
|
1045
|
-
const codec = resolveAudioCodecAlias(input.codec);
|
|
1046
|
-
const entry = CODEC_CATALOG.find((e) => resolveAudioCodecAlias(e.codec) === codec);
|
|
1047
|
-
if (!entry || !entry.canEncode) throw new Error(`audio-codec-ffmpeg: encode unsupported for codec '${input.codec}'`);
|
|
1048
|
-
const sessionId = `enc-${randomUUID()}`;
|
|
1049
|
-
const state = {
|
|
1050
|
-
sessionId,
|
|
1051
|
-
kind: "encode",
|
|
1052
|
-
config: {
|
|
1053
|
-
...input,
|
|
1054
|
-
codec
|
|
1055
|
-
},
|
|
1056
|
-
...input.tag ? { tag: input.tag } : {},
|
|
1057
|
-
createdAtMs: Date.now(),
|
|
1058
|
-
lastActivityMs: Date.now(),
|
|
1059
|
-
framesIn: 0,
|
|
1060
|
-
framesOut: 0,
|
|
1061
|
-
encodedQueue: [],
|
|
1062
|
-
session: null
|
|
1063
|
-
};
|
|
1064
|
-
state.session = this.spawnEncodeSession(state);
|
|
1065
|
-
this.sessions.set(sessionId, state);
|
|
1066
|
-
this.logger?.info("audio-codec-ffmpeg: encode session created", {
|
|
1067
|
-
tags: { sessionId },
|
|
1068
|
-
meta: {
|
|
1069
|
-
codec,
|
|
1070
|
-
target: `${input.targetSampleRate}Hz×${input.targetChannels}`
|
|
1071
|
-
}
|
|
1072
|
-
});
|
|
1073
|
-
return {
|
|
1074
|
-
sessionId,
|
|
1075
|
-
nodeId: this.resolveLocalNodeId()
|
|
1076
|
-
};
|
|
1077
|
-
}
|
|
1078
|
-
async closeSession(input) {
|
|
1079
|
-
const s = this.sessions.get(input.sessionId);
|
|
1080
|
-
if (!s) return;
|
|
1081
|
-
this.disposeSession(s);
|
|
1082
|
-
this.sessions.delete(input.sessionId);
|
|
1083
|
-
}
|
|
1084
|
-
async pushEncodedFrame(input) {
|
|
1085
|
-
const s = this.sessions.get(input.sessionId);
|
|
1086
|
-
if (!s || s.kind !== "decode") throw new Error(`audio-codec-ffmpeg: decode session '${input.sessionId}' not found`);
|
|
1087
|
-
s.lastActivityMs = Date.now();
|
|
1088
|
-
s.framesIn++;
|
|
1089
|
-
if (!s.session) s.session = this.spawnDecodeSession(s);
|
|
1090
|
-
s.session.pushEncoded(input.data);
|
|
1091
|
-
}
|
|
1092
|
-
async pullPcm(input) {
|
|
1093
|
-
const s = this.sessions.get(input.sessionId);
|
|
1094
|
-
if (!s || s.kind !== "decode") throw new Error(`audio-codec-ffmpeg: decode session '${input.sessionId}' not found`);
|
|
1095
|
-
s.lastActivityMs = Date.now();
|
|
1096
|
-
const out = s.pcmQueue.splice(0, input.maxCount);
|
|
1097
|
-
s.framesOut += out.length;
|
|
1098
|
-
return out;
|
|
1099
|
-
}
|
|
1100
|
-
async pushPcm(input) {
|
|
1101
|
-
const s = this.sessions.get(input.sessionId);
|
|
1102
|
-
if (!s || s.kind !== "encode") throw new Error(`audio-codec-ffmpeg: encode session '${input.sessionId}' not found`);
|
|
1103
|
-
s.lastActivityMs = Date.now();
|
|
1104
|
-
s.framesIn++;
|
|
1105
|
-
if (!s.session) s.session = this.spawnEncodeSession(s);
|
|
1106
|
-
s.session.pushPcm(input.data);
|
|
1107
|
-
}
|
|
1108
|
-
async pullEncoded(input) {
|
|
1109
|
-
const s = this.sessions.get(input.sessionId);
|
|
1110
|
-
if (!s || s.kind !== "encode") throw new Error(`audio-codec-ffmpeg: encode session '${input.sessionId}' not found`);
|
|
1111
|
-
s.lastActivityMs = Date.now();
|
|
1112
|
-
const out = s.encodedQueue.splice(0, input.maxCount);
|
|
1113
|
-
s.framesOut += out.length;
|
|
1114
|
-
return out;
|
|
1115
|
-
}
|
|
1116
|
-
async flushEncode(input) {
|
|
1117
|
-
const s = this.sessions.get(input.sessionId);
|
|
1118
|
-
if (!s || s.kind !== "encode") throw new Error(`audio-codec-ffmpeg: encode session '${input.sessionId}' not found`);
|
|
1119
|
-
s.lastActivityMs = Date.now();
|
|
1120
|
-
s.session?.flush();
|
|
1121
|
-
await new Promise((resolve) => setTimeout(resolve, FLUSH_DRAIN_MS));
|
|
1122
|
-
const out = s.encodedQueue.splice(0);
|
|
1123
|
-
s.framesOut += out.length;
|
|
1124
|
-
return out;
|
|
1125
|
-
}
|
|
1126
|
-
async listActiveSessions() {
|
|
1127
|
-
return [...this.sessions.values()].map((s) => ({
|
|
1128
|
-
sessionId: s.sessionId,
|
|
1129
|
-
kind: s.kind,
|
|
1130
|
-
codec: s.config.codec,
|
|
1131
|
-
sourceSampleRate: s.config.sourceSampleRate,
|
|
1132
|
-
sourceChannels: s.config.sourceChannels,
|
|
1133
|
-
targetSampleRate: s.config.targetSampleRate,
|
|
1134
|
-
targetChannels: s.config.targetChannels,
|
|
1135
|
-
format: this.resolveFormat(s),
|
|
1136
|
-
...s.tag ? { tag: s.tag } : {},
|
|
1137
|
-
createdAtMs: s.createdAtMs,
|
|
1138
|
-
lastActivityMs: s.lastActivityMs,
|
|
1139
|
-
framesIn: s.framesIn,
|
|
1140
|
-
framesOut: s.framesOut
|
|
1141
|
-
}));
|
|
1142
|
-
}
|
|
1143
|
-
spawnDecodeSession(s) {
|
|
1144
|
-
const logger = this.logger ?? this.ctx.logger;
|
|
1145
|
-
return new FfmpegAudioDecodeSession({
|
|
1146
|
-
codec: s.config.codec,
|
|
1147
|
-
sourceSampleRate: s.config.sourceSampleRate,
|
|
1148
|
-
sourceChannels: s.config.sourceChannels,
|
|
1149
|
-
targetSampleRate: s.config.targetSampleRate,
|
|
1150
|
-
targetChannels: s.config.targetChannels,
|
|
1151
|
-
targetFormat: this.pcmFormat(s.config.targetFormat),
|
|
1152
|
-
ffmpegPath: this.ffmpegPath,
|
|
1153
|
-
oggSerialSeed: s.sessionId
|
|
1154
|
-
}, logger, (chunk) => {
|
|
1155
|
-
s.pcmQueue.push(chunk);
|
|
1156
|
-
if (s.pcmQueue.length > MAX_PCM_QUEUE_CHUNKS) s.pcmQueue.splice(0, s.pcmQueue.length - MAX_PCM_QUEUE_CHUNKS);
|
|
1157
|
-
});
|
|
1158
|
-
}
|
|
1159
|
-
spawnEncodeSession(s) {
|
|
1160
|
-
const logger = this.logger ?? this.ctx.logger;
|
|
1161
|
-
return new FfmpegAudioEncodeSession({
|
|
1162
|
-
codec: s.config.codec,
|
|
1163
|
-
sourceSampleRate: s.config.sourceSampleRate,
|
|
1164
|
-
sourceChannels: s.config.sourceChannels,
|
|
1165
|
-
sourceFormat: this.pcmFormat(s.config.sourceFormat),
|
|
1166
|
-
targetSampleRate: s.config.targetSampleRate,
|
|
1167
|
-
targetChannels: s.config.targetChannels,
|
|
1168
|
-
...s.config.bitrateKbps !== void 0 ? { bitrateKbps: s.config.bitrateKbps } : {},
|
|
1169
|
-
ffmpegPath: this.ffmpegPath
|
|
1170
|
-
}, logger, (chunk) => {
|
|
1171
|
-
s.encodedQueue.push(chunk);
|
|
1172
|
-
});
|
|
1173
|
-
}
|
|
1174
|
-
/** Narrow the cap's PCM format enum to the two ffmpeg sessions produce/read. */
|
|
1175
|
-
pcmFormat(format) {
|
|
1176
|
-
return format === "f32le" ? "f32le" : "s16le";
|
|
1177
|
-
}
|
|
1178
|
-
resolveFormat(s) {
|
|
1179
|
-
if (s.kind === "decode") return this.pcmFormat(s.config.targetFormat);
|
|
1180
|
-
return this.pcmFormat(s.config.sourceFormat);
|
|
1181
|
-
}
|
|
1182
|
-
reapIdleSessions() {
|
|
1183
|
-
const now = Date.now();
|
|
1184
|
-
for (const [id, s] of this.sessions) {
|
|
1185
|
-
const limit = s.config.idleMs ?? this.config.defaultIdleMs ?? DEFAULT_IDLE_MS;
|
|
1186
|
-
if (now - s.lastActivityMs > limit) {
|
|
1187
|
-
this.logger?.info("audio-codec-ffmpeg: reaping idle session", {
|
|
1188
|
-
tags: { sessionId: id },
|
|
1189
|
-
meta: {
|
|
1190
|
-
kind: s.kind,
|
|
1191
|
-
idleMs: now - s.lastActivityMs,
|
|
1192
|
-
limit
|
|
1193
|
-
}
|
|
1194
|
-
});
|
|
1195
|
-
try {
|
|
1196
|
-
this.disposeSession(s);
|
|
1197
|
-
} catch (err) {
|
|
1198
|
-
this.logger?.warn("audio-codec-ffmpeg: dispose failed during reap", {
|
|
1199
|
-
tags: { sessionId: id },
|
|
1200
|
-
meta: { error: errMsg(err) }
|
|
1201
|
-
});
|
|
1202
|
-
}
|
|
1203
|
-
this.sessions.delete(id);
|
|
1204
|
-
}
|
|
1205
|
-
}
|
|
1206
|
-
}
|
|
1207
|
-
disposeSession(s) {
|
|
1208
|
-
try {
|
|
1209
|
-
s.session?.destroy();
|
|
1210
|
-
} catch (err) {
|
|
1211
|
-
this.logger?.warn("audio-codec-ffmpeg: session destroy failed", {
|
|
1212
|
-
tags: { sessionId: s.sessionId },
|
|
1213
|
-
meta: { error: errMsg(err) }
|
|
1214
|
-
});
|
|
1215
|
-
}
|
|
1216
|
-
s.session = null;
|
|
1217
|
-
}
|
|
1218
|
-
};
|
|
1219
|
-
//#endregion
|
|
1220
|
-
export { AUDIO_LOW_LATENCY_ARGS, AudioCodecFfmpegAddon, AudioCodecFfmpegAddon as default, DEFAULT_OPUS_PRE_SKIP, OggOpusFramer, audioBytesPerSample, buildAudioDecodeArgs, buildAudioEncodeArgs, buildLacing, decodeFormatForCodec, encodeFormatForCodec, oggCrc32, opusPacketSampleCount, resolveAudioCodecAlias, serialFromSeed };
|