@camstack/addon-decoder-ffmpeg 1.1.4 → 1.1.5

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.
Files changed (3) hide show
  1. package/dist/index.js +1254 -41
  2. package/dist/index.mjs +1254 -41
  3. package/package.json +6 -2
package/dist/index.js CHANGED
@@ -12534,42 +12534,79 @@ var SessionInventoryEntrySchema = object({
12534
12534
  framesIn: number(),
12535
12535
  framesOut: number()
12536
12536
  });
12537
- method(_void(), array(AudioCodecInfoSchema).readonly()), method(object({
12538
- codec: string(),
12539
- kind: _enum(["decode", "encode"])
12540
- }), boolean()), method(AudioDecodeSessionConfigSchema, object({
12541
- sessionId: string(),
12542
- nodeId: string()
12543
- }), { kind: "mutation" }), method(AudioEncodeSessionConfigSchema, object({
12544
- sessionId: string(),
12545
- nodeId: string()
12546
- }), { kind: "mutation" }), method(object({
12547
- sessionId: string(),
12548
- nodeId: string().optional()
12549
- }), _void(), { kind: "mutation" }), method(object({
12550
- sessionId: string(),
12551
- nodeId: string().optional(),
12552
- data: _instanceof(Uint8Array),
12553
- /** Source PTS in milliseconds. Synthesised when omitted. */
12554
- pts: number().optional()
12555
- }), _void(), { kind: "mutation" }), method(object({
12556
- sessionId: string(),
12557
- nodeId: string().optional(),
12558
- maxCount: number().int().positive().default(8)
12559
- }), array(AudioPcmChunkSchema)), method(object({
12560
- sessionId: string(),
12561
- nodeId: string().optional(),
12562
- data: _instanceof(Uint8Array),
12563
- /** Source PTS in milliseconds. */
12564
- pts: number().optional()
12565
- }), _void(), { kind: "mutation" }), method(object({
12566
- sessionId: string(),
12567
- nodeId: string().optional(),
12568
- maxCount: number().int().positive().default(8)
12569
- }), array(AudioEncodedChunkSchema)), method(object({
12570
- sessionId: string(),
12571
- nodeId: string().optional()
12572
- }), array(AudioEncodedChunkSchema), { kind: "mutation" }), method(_void(), array(SessionInventoryEntrySchema).readonly());
12537
+ /**
12538
+ * audio-codec — bidirectional PCM ↔ encoded audio I/O box.
12539
+ *
12540
+ * Independent per-consumer sessions. The provider runs decode + resample
12541
+ * (or resample + encode) inside the session so a 16kHz mono ASA
12542
+ * subscriber and a 48kHz stereo WebRTC subscriber on the same source
12543
+ * stream don't share resamplers.
12544
+ *
12545
+ * Singleton on each node. Decoder and encoder live in the same provider
12546
+ * because they share the underlying libav contexts (node-av today,
12547
+ * pluggable later) — operators always install one or the other together.
12548
+ */
12549
+ var audioCodecCapability = {
12550
+ name: "audio-codec",
12551
+ scope: "system",
12552
+ mode: "singleton",
12553
+ preferredProvider: "decoder-nodeav",
12554
+ methods: {
12555
+ /** Probe the local runtime and return the supported codec matrix. */
12556
+ listSupportedCodecs: method(_void(), array(AudioCodecInfoSchema).readonly()),
12557
+ /** Cheap predicate — does the runtime support `(codec, kind)`? */
12558
+ canHandle: method(object({
12559
+ codec: string(),
12560
+ kind: _enum(["decode", "encode"])
12561
+ }), boolean()),
12562
+ createDecodeSession: method(AudioDecodeSessionConfigSchema, object({
12563
+ sessionId: string(),
12564
+ nodeId: string()
12565
+ }), { kind: "mutation" }),
12566
+ createEncodeSession: method(AudioEncodeSessionConfigSchema, object({
12567
+ sessionId: string(),
12568
+ nodeId: string()
12569
+ }), { kind: "mutation" }),
12570
+ closeSession: method(object({
12571
+ sessionId: string(),
12572
+ nodeId: string().optional()
12573
+ }), _void(), { kind: "mutation" }),
12574
+ /** Push one encoded audio frame into a decode session. */
12575
+ pushEncodedFrame: method(object({
12576
+ sessionId: string(),
12577
+ nodeId: string().optional(),
12578
+ data: _instanceof(Uint8Array),
12579
+ /** Source PTS in milliseconds. Synthesised when omitted. */
12580
+ pts: number().optional()
12581
+ }), _void(), { kind: "mutation" }),
12582
+ /** Pull up to `maxCount` PCM chunks from a decode session. */
12583
+ pullPcm: method(object({
12584
+ sessionId: string(),
12585
+ nodeId: string().optional(),
12586
+ maxCount: number().int().positive().default(8)
12587
+ }), array(AudioPcmChunkSchema)),
12588
+ /** Push one PCM chunk into an encode session. */
12589
+ pushPcm: method(object({
12590
+ sessionId: string(),
12591
+ nodeId: string().optional(),
12592
+ data: _instanceof(Uint8Array),
12593
+ /** Source PTS in milliseconds. */
12594
+ pts: number().optional()
12595
+ }), _void(), { kind: "mutation" }),
12596
+ /** Pull up to `maxCount` encoded chunks from an encode session. */
12597
+ pullEncoded: method(object({
12598
+ sessionId: string(),
12599
+ nodeId: string().optional(),
12600
+ maxCount: number().int().positive().default(8)
12601
+ }), array(AudioEncodedChunkSchema)),
12602
+ /** Flush any pending encoded output (call before close on graceful tear). */
12603
+ flushEncode: method(object({
12604
+ sessionId: string(),
12605
+ nodeId: string().optional()
12606
+ }), array(AudioEncodedChunkSchema), { kind: "mutation" }),
12607
+ listActiveSessions: method(_void(), array(SessionInventoryEntrySchema).readonly())
12608
+ }
12609
+ };
12573
12610
  var AuthResultSchema = object({
12574
12611
  userId: string(),
12575
12612
  username: string(),
@@ -24138,6 +24175,1160 @@ function probeGpuScaleFilters(ffmpegPath, logger) {
24138
24175
  });
24139
24176
  }
24140
24177
  //#endregion
24178
+ //#region src/audio-codec/ffmpeg-audio-process.ts
24179
+ /**
24180
+ * Minimal ffmpeg subprocess wrapper shared by the audio decode + encode
24181
+ * sessions of `audio-codec-ffmpeg`.
24182
+ *
24183
+ * Owns exactly the process concerns both directions need: spawn, stdin write
24184
+ * with tolerated backpressure, stdout chunk delivery, and the kill sequence.
24185
+ *
24186
+ * The kill logic copies the CORRECTED pattern from `decoder-ffmpeg`'s
24187
+ * `killFfmpeg`: SIGTERM, then SIGKILL after a grace — gated on
24188
+ * `exitCode === null && signalCode === null`, NOT on `child.killed` (which Node
24189
+ * sets true after ANY `kill()`, so `!child.killed` never fires and the SIGKILL
24190
+ * would leak the process).
24191
+ */
24192
+ /** Grace period between SIGTERM and the escalated SIGKILL. */
24193
+ var KILL_GRACE_MS = 500;
24194
+ var FfmpegAudioProcess = class {
24195
+ child = null;
24196
+ logger;
24197
+ opts;
24198
+ killed = false;
24199
+ constructor(opts) {
24200
+ this.opts = opts;
24201
+ this.logger = opts.logger;
24202
+ this.spawn();
24203
+ }
24204
+ spawn() {
24205
+ let child;
24206
+ try {
24207
+ child = (0, node_child_process.spawn)(this.opts.ffmpegPath, [...this.opts.args]);
24208
+ } catch (err) {
24209
+ this.logger.error("audio-codec-ffmpeg: spawn threw", { meta: { error: errMsg(err) } });
24210
+ return;
24211
+ }
24212
+ this.child = child;
24213
+ child.stdin?.on("error", () => {});
24214
+ child.stdout?.on("data", (chunk) => this.opts.onStdout(chunk));
24215
+ child.stderr?.on("data", (data) => {
24216
+ const line = data.toString().trim();
24217
+ if (line) this.logger.debug("audio-codec-ffmpeg stderr", { meta: { line } });
24218
+ });
24219
+ child.on("error", (err) => {
24220
+ this.logger.error("audio-codec-ffmpeg: process error", { meta: { error: err.message } });
24221
+ });
24222
+ child.on("exit", (code, signal) => {
24223
+ if (this.killed) return;
24224
+ this.opts.onExit(code, signal);
24225
+ });
24226
+ }
24227
+ /** Write encoded frames / PCM into ffmpeg stdin. Backpressure is tolerated. */
24228
+ write(data) {
24229
+ const stdin = this.child?.stdin;
24230
+ if (!stdin) return;
24231
+ try {
24232
+ stdin.write(Buffer.from(data.buffer, data.byteOffset, data.byteLength));
24233
+ } catch {}
24234
+ }
24235
+ /** Close stdin so ffmpeg flushes and drains its remaining output. */
24236
+ endStdin() {
24237
+ try {
24238
+ this.child?.stdin?.end();
24239
+ } catch {}
24240
+ }
24241
+ /** SIGTERM then escalated SIGKILL — see class doc for the gating rationale. */
24242
+ kill() {
24243
+ const child = this.child;
24244
+ if (!child) return;
24245
+ this.killed = true;
24246
+ this.child = null;
24247
+ try {
24248
+ child.stdin?.end();
24249
+ } catch {}
24250
+ try {
24251
+ child.kill("SIGTERM");
24252
+ } catch {}
24253
+ const { pid } = child;
24254
+ setTimeout(() => {
24255
+ if (pid !== void 0 && child.exitCode === null && child.signalCode === null) try {
24256
+ child.kill("SIGKILL");
24257
+ } catch {}
24258
+ }, KILL_GRACE_MS).unref?.();
24259
+ }
24260
+ };
24261
+ //#endregion
24262
+ //#region src/audio-codec/audio-codec-args.ts
24263
+ /**
24264
+ * Low-latency flag set shared by both the decode and encode invocations.
24265
+ *
24266
+ * - `-hide_banner -nostats -loglevel error`: quiet stderr (no periodic counter).
24267
+ * - `-fflags +nobuffer+flush_packets`: don't buffer input; flush output packets
24268
+ * the moment they are ready (critical for the intercom encode path).
24269
+ * - `-flags low_delay`: request the lowest-latency codec behaviour.
24270
+ * - `-probesize 32 -analyzeduration 0`: skip stream probing — the input format
24271
+ * is fully specified by `-f`/`-ar`/`-ac`, so probing only adds startup delay.
24272
+ * - `-threads 1`: single-threaded — audio frames are tiny, and extra worker
24273
+ * threads only add scheduling jitter to a real-time path.
24274
+ */
24275
+ var AUDIO_LOW_LATENCY_ARGS = [
24276
+ "-hide_banner",
24277
+ "-nostats",
24278
+ "-loglevel",
24279
+ "error",
24280
+ "-fflags",
24281
+ "+nobuffer+flush_packets",
24282
+ "-flags",
24283
+ "low_delay",
24284
+ "-probesize",
24285
+ "32",
24286
+ "-analyzeduration",
24287
+ "0",
24288
+ "-threads",
24289
+ "1"
24290
+ ];
24291
+ /**
24292
+ * Normalise an SDP-reported codec name to the libav/ffmpeg name. Mirrors the
24293
+ * `resolveCodecAlias` in `audio-codec-nodeav` so callers can pass the
24294
+ * SDP-reported value verbatim.
24295
+ */
24296
+ function resolveAudioCodecAlias(codec) {
24297
+ const c = codec.toLowerCase();
24298
+ if (c === "mpeg4-generic") return "aac";
24299
+ if (c === "l16") return "pcm_s16be";
24300
+ return c;
24301
+ }
24302
+ /**
24303
+ * codec → ffmpeg DECODE input format.
24304
+ *
24305
+ * | codec | -f | raw? | note |
24306
+ * | ----------------------------- | ------ | ---- | ----------------------------- |
24307
+ * | pcm_mulaw | mulaw | yes | G.711 µ-law |
24308
+ * | pcm_alaw | alaw | yes | G.711 A-law |
24309
+ * | pcm_s16be (l16) | s16be | yes | RTP L16 |
24310
+ * | pcm_s16le | s16le | yes | |
24311
+ * | g722 | g722 | yes | 16 kHz wideband |
24312
+ * | aac / aac_latm / mpeg4-generic| aac | no | ADTS demuxer |
24313
+ * | opus | ogg | no | ⚠ needs Ogg framing (see NOTE)|
24314
+ *
24315
+ * NOTE (opus decode): ffmpeg has no raw-packet Opus demuxer. `audio-codec-nodeav`
24316
+ * feeds raw RTP Opus packets straight to the `AV_CODEC_ID_OPUS` decoder with no
24317
+ * demuxer; a subprocess cannot do that from a pipe. We map to `ogg`, which is
24318
+ * correct only if the pushed bytes are Ogg-Opus. Raw RTP Opus decode via the
24319
+ * subprocess is a KNOWN limitation — see the addon report.
24320
+ */
24321
+ var DECODE_FORMAT_BY_CODEC = {
24322
+ pcm_mulaw: {
24323
+ inputFormat: "mulaw",
24324
+ rawInput: true
24325
+ },
24326
+ pcm_alaw: {
24327
+ inputFormat: "alaw",
24328
+ rawInput: true
24329
+ },
24330
+ pcm_s16be: {
24331
+ inputFormat: "s16be",
24332
+ rawInput: true
24333
+ },
24334
+ pcm_s16le: {
24335
+ inputFormat: "s16le",
24336
+ rawInput: true
24337
+ },
24338
+ g722: {
24339
+ inputFormat: "g722",
24340
+ rawInput: true
24341
+ },
24342
+ aac: {
24343
+ inputFormat: "aac",
24344
+ rawInput: false
24345
+ },
24346
+ aac_latm: {
24347
+ inputFormat: "aac",
24348
+ rawInput: false
24349
+ },
24350
+ opus: {
24351
+ inputFormat: "ogg",
24352
+ rawInput: false
24353
+ }
24354
+ };
24355
+ /**
24356
+ * codec → ffmpeg ENCODE codec + container.
24357
+ *
24358
+ * | codec | -c:a | -f | bitrate? |
24359
+ * | --------- | ---------- | ----- | -------- |
24360
+ * | pcm_mulaw | pcm_mulaw | mulaw | no |
24361
+ * | pcm_alaw | pcm_alaw | alaw | no |
24362
+ * | g722 | g722 | g722 | no |
24363
+ * | aac | aac | adts | yes |
24364
+ * | opus | libopus | ogg | yes |
24365
+ */
24366
+ var ENCODE_FORMAT_BY_CODEC = {
24367
+ pcm_mulaw: {
24368
+ encoder: "pcm_mulaw",
24369
+ outputFormat: "mulaw",
24370
+ acceptsBitrate: false
24371
+ },
24372
+ pcm_alaw: {
24373
+ encoder: "pcm_alaw",
24374
+ outputFormat: "alaw",
24375
+ acceptsBitrate: false
24376
+ },
24377
+ g722: {
24378
+ encoder: "g722",
24379
+ outputFormat: "g722",
24380
+ acceptsBitrate: false
24381
+ },
24382
+ aac: {
24383
+ encoder: "aac",
24384
+ outputFormat: "adts",
24385
+ acceptsBitrate: true
24386
+ },
24387
+ opus: {
24388
+ encoder: "libopus",
24389
+ outputFormat: "ogg",
24390
+ acceptsBitrate: true
24391
+ }
24392
+ };
24393
+ /** Resolve the DECODE input-format descriptor for a codec, or `null` if unknown. */
24394
+ function decodeFormatForCodec(codec) {
24395
+ return DECODE_FORMAT_BY_CODEC[resolveAudioCodecAlias(codec)] ?? null;
24396
+ }
24397
+ /** Resolve the ENCODE codec+container descriptor for a codec, or `null` if unknown. */
24398
+ function encodeFormatForCodec(codec) {
24399
+ return ENCODE_FORMAT_BY_CODEC[resolveAudioCodecAlias(codec)] ?? null;
24400
+ }
24401
+ /** Packed bytes-per-sample for a PCM format (s16le = 2, f32le = 4). */
24402
+ function audioBytesPerSample(format) {
24403
+ return format === "f32le" ? 4 : 2;
24404
+ }
24405
+ /**
24406
+ * Build the ffmpeg argv for a DECODE session — encoded frames on `pipe:0`, raw
24407
+ * PCM on `pipe:1`. PURE: no spawn, no env access.
24408
+ *
24409
+ * ffmpeg <low-latency> -f <inputFmt> [-ar <src> -ac <srcCh>] -i pipe:0 \
24410
+ * -ar <target> -ac <targetCh> -f <s16le|f32le> pipe:1
24411
+ *
24412
+ * @throws if the codec has no known decode mapping.
24413
+ */
24414
+ function buildAudioDecodeArgs(config) {
24415
+ const entry = decodeFormatForCodec(config.codec);
24416
+ if (!entry) throw new Error(`audio-codec-ffmpeg: no decode format for codec '${config.codec}'`);
24417
+ const outFormat = config.targetFormat ?? "s16le";
24418
+ const inputRateChannel = entry.rawInput ? [
24419
+ "-ar",
24420
+ String(config.sourceSampleRate),
24421
+ "-ac",
24422
+ String(config.sourceChannels)
24423
+ ] : [];
24424
+ return [
24425
+ ...AUDIO_LOW_LATENCY_ARGS,
24426
+ "-f",
24427
+ entry.inputFormat,
24428
+ ...inputRateChannel,
24429
+ "-i",
24430
+ "pipe:0",
24431
+ "-ar",
24432
+ String(config.targetSampleRate),
24433
+ "-ac",
24434
+ String(config.targetChannels),
24435
+ "-f",
24436
+ outFormat,
24437
+ "pipe:1"
24438
+ ];
24439
+ }
24440
+ /**
24441
+ * Build the ffmpeg argv for an ENCODE session — raw PCM on `pipe:0`, encoded
24442
+ * bytes on `pipe:1`. PURE: no spawn, no env access.
24443
+ *
24444
+ * ffmpeg <low-latency> -f <s16le|f32le> -ar <src> -ac <srcCh> -i pipe:0 \
24445
+ * -c:a <encoder> -ar <target> -ac <targetCh> [-b:a <k>k] \
24446
+ * [-application lowdelay] -f <containerFmt> pipe:1
24447
+ *
24448
+ * For `libopus` the intercom-oriented `-application lowdelay` mode is added,
24449
+ * trading a little quality for the lowest algorithmic latency.
24450
+ *
24451
+ * @throws if the codec has no known encode mapping.
24452
+ */
24453
+ function buildAudioEncodeArgs(config) {
24454
+ const entry = encodeFormatForCodec(config.codec);
24455
+ if (!entry) throw new Error(`audio-codec-ffmpeg: no encode format for codec '${config.codec}'`);
24456
+ const inFormat = config.sourceFormat ?? "s16le";
24457
+ const bitrateArgs = entry.acceptsBitrate && config.bitrateKbps !== void 0 ? ["-b:a", `${config.bitrateKbps}k`] : [];
24458
+ const opusLowDelay = entry.encoder === "libopus" ? ["-application", "lowdelay"] : [];
24459
+ return [
24460
+ ...AUDIO_LOW_LATENCY_ARGS,
24461
+ "-f",
24462
+ inFormat,
24463
+ "-ar",
24464
+ String(config.sourceSampleRate),
24465
+ "-ac",
24466
+ String(config.sourceChannels),
24467
+ "-i",
24468
+ "pipe:0",
24469
+ "-c:a",
24470
+ entry.encoder,
24471
+ "-ar",
24472
+ String(config.targetSampleRate),
24473
+ "-ac",
24474
+ String(config.targetChannels),
24475
+ ...bitrateArgs,
24476
+ ...opusLowDelay,
24477
+ "-f",
24478
+ entry.outputFormat,
24479
+ "pipe:1"
24480
+ ];
24481
+ }
24482
+ //#endregion
24483
+ //#region src/audio-codec/ogg-opus-framer.ts
24484
+ /**
24485
+ * Ogg-Opus encapsulator for the `audio-codec-ffmpeg` decode path.
24486
+ *
24487
+ * Cameras / RTP deliver RAW Opus packets — one self-delimited Opus packet per
24488
+ * `pushEncodedFrame`. ffmpeg has NO raw-Opus demuxer, so the subprocess cannot
24489
+ * decode a bare packet stream from a pipe (unlike `audio-codec-nodeav`, which
24490
+ * feeds raw packets straight to `AV_CODEC_ID_OPUS` in-process). To decode Opus
24491
+ * out-of-process we must wrap the raw packets in a valid **Ogg-Opus** container
24492
+ * so `ffmpeg -f ogg -i pipe:0` can demux + decode them.
24493
+ *
24494
+ * This framer turns a stream of raw Opus packets into a byte stream shaped per
24495
+ * RFC 7845 (Ogg Encapsulation for Opus) + RFC 3533 (the Ogg container):
24496
+ *
24497
+ * Page 0 (BOS) : one "OpusHead" identification packet
24498
+ * Page 1 : one "OpusTags" comment packet
24499
+ * Page 2..n : one Opus audio packet each (one-packet-per-page = lowest
24500
+ * latency, simplest lacing), granule = running 48 kHz sample
24501
+ * total.
24502
+ * flush() : optional final EOS page.
24503
+ *
24504
+ * The Ogg page CRC is NOT the common zlib CRC-32: it uses polynomial
24505
+ * 0x04C11DB7 with init 0, NO input/output bit reflection, and NO final XOR,
24506
+ * computed over the whole page with the CRC field zeroed (see `oggCrc32`).
24507
+ */
24508
+ /** Ogg capture pattern, "OggS". */
24509
+ var OGG_CAPTURE_PATTERN = Buffer.from("OggS", "ascii");
24510
+ /** Opus identification header magic, "OpusHead". */
24511
+ var OPUS_HEAD_MAGIC = Buffer.from("OpusHead", "ascii");
24512
+ /** Opus comment header magic, "OpusTags". */
24513
+ var OPUS_TAGS_MAGIC = Buffer.from("OpusTags", "ascii");
24514
+ /** Ogg page header byte offsets (before the segment table). */
24515
+ var OGG_HEADER_FIXED_BYTES = 27;
24516
+ /** Byte offset of the 4-byte CRC field within the fixed header. */
24517
+ var OGG_CRC_OFFSET = 22;
24518
+ var HEADER_TYPE_BOS = 2;
24519
+ var HEADER_TYPE_EOS = 4;
24520
+ /** OpusHead `input sample rate` — informational per RFC 7845 (§5.1). */
24521
+ var OPUS_INPUT_SAMPLE_RATE = 48e3;
24522
+ /** Opus granule positions are ALWAYS counted at 48 kHz (RFC 7845 §4). */
24523
+ var OPUS_GRANULE_SAMPLE_RATE = 48e3;
24524
+ /**
24525
+ * Opus frame size, in 48 kHz samples, indexed by the 5-bit TOC `config`
24526
+ * (0..31). Derived from the frame-duration table in RFC 6716 §3.1:
24527
+ *
24528
+ * config 0-11 SILK NB/MB/WB → 10 / 20 / 40 / 60 ms
24529
+ * config 12-15 Hybrid SWB/FB → 10 / 20 ms
24530
+ * config 16-31 CELT NB/…/FB → 2.5 / 5 / 10 / 20 ms
24531
+ *
24532
+ * samples@48k = ms * 48 (2.5→120, 5→240, 10→480, 20→960, 40→1920, 60→2880).
24533
+ */
24534
+ var OPUS_FRAME_SAMPLES_48K = [
24535
+ 480,
24536
+ 960,
24537
+ 1920,
24538
+ 2880,
24539
+ 480,
24540
+ 960,
24541
+ 1920,
24542
+ 2880,
24543
+ 480,
24544
+ 960,
24545
+ 1920,
24546
+ 2880,
24547
+ 480,
24548
+ 960,
24549
+ 480,
24550
+ 960,
24551
+ 120,
24552
+ 240,
24553
+ 480,
24554
+ 960,
24555
+ 120,
24556
+ 240,
24557
+ 480,
24558
+ 960,
24559
+ 120,
24560
+ 240,
24561
+ 480,
24562
+ 960,
24563
+ 120,
24564
+ 240,
24565
+ 480,
24566
+ 960
24567
+ ];
24568
+ /**
24569
+ * Precomputed Ogg CRC-32 lookup table (MSB-first, polynomial 0x04C11DB7).
24570
+ *
24571
+ * Each entry is the CRC of the single byte `i` placed in the top position of a
24572
+ * 32-bit register, shifted out MSB-first with no reflection. This is the table
24573
+ * form of the bit-serial algorithm used by `oggCrc32`.
24574
+ */
24575
+ var OGG_CRC_TABLE = (() => {
24576
+ const table = new Uint32Array(256);
24577
+ for (let i = 0; i < 256; i++) {
24578
+ let r = i << 24 >>> 0;
24579
+ for (let bit = 0; bit < 8; bit++) r = (r & 2147483648) !== 0 ? (r << 1 ^ 79764919) >>> 0 : r << 1 >>> 0;
24580
+ table[i] = r >>> 0;
24581
+ }
24582
+ return table;
24583
+ })();
24584
+ /**
24585
+ * Ogg page CRC-32 (RFC 3533 §4). Polynomial 0x04C11DB7, init 0, NO input or
24586
+ * output reflection, NO final XOR — deliberately different from the reflected
24587
+ * zlib/IEEE CRC-32. Computed over the ENTIRE page bytes with the 4-byte CRC
24588
+ * field already zeroed.
24589
+ */
24590
+ function oggCrc32(data) {
24591
+ let crc = 0;
24592
+ for (let i = 0; i < data.length; i++) {
24593
+ const idx = (crc >>> 24 ^ (data[i] ?? 0)) & 255;
24594
+ crc = (crc << 8 >>> 0 ^ (OGG_CRC_TABLE[idx] ?? 0)) >>> 0;
24595
+ }
24596
+ return crc >>> 0;
24597
+ }
24598
+ /**
24599
+ * Decode an Opus packet's total duration in samples from its TOC byte
24600
+ * (RFC 6716 §3). Returns samples at `sampleRate` (default 48 kHz — the rate Ogg
24601
+ * granule positions are counted in).
24602
+ *
24603
+ * TOC byte layout: `config` (bits 3-7), `s` stereo (bit 2), `c` frame-count
24604
+ * code (bits 0-1):
24605
+ * - code 0 → 1 frame
24606
+ * - code 1 / 2 → 2 frames
24607
+ * - code 3 → arbitrary; frame count = low 6 bits of the byte after the TOC.
24608
+ *
24609
+ * Returns 0 for an empty or malformed (code-3 with no count byte) packet.
24610
+ */
24611
+ function opusPacketSampleCount(packet, sampleRate = 48e3) {
24612
+ if (packet.length < 1) return 0;
24613
+ const toc = packet[0] ?? 0;
24614
+ const config = toc >> 3;
24615
+ const code = toc & 3;
24616
+ const samplesPerFrame48k = OPUS_FRAME_SAMPLES_48K[config] ?? 0;
24617
+ if (samplesPerFrame48k === 0) return 0;
24618
+ let frameCount;
24619
+ if (code === 0) frameCount = 1;
24620
+ else if (code === 1 || code === 2) frameCount = 2;
24621
+ else {
24622
+ if (packet.length < 2) return 0;
24623
+ frameCount = (packet[1] ?? 0) & 63;
24624
+ }
24625
+ const samples48k = samplesPerFrame48k * frameCount;
24626
+ if (sampleRate === OPUS_GRANULE_SAMPLE_RATE) return samples48k;
24627
+ return Math.round(samples48k * sampleRate / OPUS_GRANULE_SAMPLE_RATE);
24628
+ }
24629
+ /**
24630
+ * Encode an Ogg lacing segment table for a single packet of length `len`
24631
+ * (RFC 3533 §6): `floor(len/255)` bytes of 0xFF then one final byte `len%255`.
24632
+ * A length that is an exact multiple of 255 therefore ends in a `0x00` lacing
24633
+ * value — required so the demuxer knows the packet terminates on this page.
24634
+ */
24635
+ function buildLacing(len) {
24636
+ const full = Math.floor(len / 255);
24637
+ const table = Buffer.alloc(full + 1);
24638
+ table.fill(255, 0, full);
24639
+ table[full] = len % 255;
24640
+ return table;
24641
+ }
24642
+ /**
24643
+ * Turn a hash-derivable seed into a stable 32-bit Ogg bitstream serial. FNV-1a
24644
+ * over the seed string keeps distinct sessions on distinct serials without
24645
+ * requiring the caller to allocate one.
24646
+ */
24647
+ function serialFromSeed(seed) {
24648
+ let hash = 2166136261;
24649
+ for (let i = 0; i < seed.length; i++) {
24650
+ hash ^= seed.charCodeAt(i) & 255;
24651
+ hash = Math.imul(hash, 16777619);
24652
+ }
24653
+ return hash >>> 0;
24654
+ }
24655
+ /**
24656
+ * Streaming Ogg-Opus encapsulator. Stateful: tracks page sequence numbers and
24657
+ * the running 48 kHz granule position across calls. One instance per decode
24658
+ * session; call {@link reset} to reuse it for a fresh stream.
24659
+ */
24660
+ var OggOpusFramer = class {
24661
+ channelCount;
24662
+ serial;
24663
+ preSkip;
24664
+ vendor;
24665
+ pageSeq = 0;
24666
+ granule = 0;
24667
+ headersEmitted = false;
24668
+ audioEmitted = false;
24669
+ constructor(config) {
24670
+ this.channelCount = config.channelCount;
24671
+ this.serial = config.serial >>> 0;
24672
+ this.preSkip = config.preSkip ?? 3840;
24673
+ this.vendor = config.vendor ?? "camstack";
24674
+ }
24675
+ /**
24676
+ * Build the two header pages (OpusHead BOS + OpusTags). Advances the page
24677
+ * sequence to 2 so audio pages follow. Returns an empty buffer if the headers
24678
+ * were already emitted (idempotent within a stream).
24679
+ */
24680
+ headerPages() {
24681
+ if (this.headersEmitted) return Buffer.alloc(0);
24682
+ this.headersEmitted = true;
24683
+ const head = this.buildOpusHead();
24684
+ const tags = this.buildOpusTags();
24685
+ const headPage = this.buildPage(head, HEADER_TYPE_BOS, 0);
24686
+ const tagsPage = this.buildPage(tags, 0, 0);
24687
+ return Buffer.concat([headPage, tagsPage]);
24688
+ }
24689
+ /**
24690
+ * Encapsulate one raw Opus packet as a single audio page, advancing the
24691
+ * granule by the packet's decoded sample count. Header pages are emitted
24692
+ * (and prepended) automatically on the first call, so the caller can simply
24693
+ * write the returned bytes to ffmpeg stdin.
24694
+ */
24695
+ framePacket(packet) {
24696
+ const prefix = this.headersEmitted ? Buffer.alloc(0) : this.headerPages();
24697
+ this.granule += opusPacketSampleCount(packet, OPUS_GRANULE_SAMPLE_RATE);
24698
+ this.audioEmitted = true;
24699
+ const page = this.buildPage(packet, 0, this.granule);
24700
+ return prefix.length > 0 ? Buffer.concat([prefix, page]) : page;
24701
+ }
24702
+ /**
24703
+ * Emit a final EOS page terminating the logical bitstream. Returns `null`
24704
+ * when no audio was written (nothing to terminate). The EOS page carries no
24705
+ * packet data (0 segments) and the final granule position.
24706
+ */
24707
+ flush() {
24708
+ if (!this.audioEmitted) return null;
24709
+ return this.buildPage(null, HEADER_TYPE_EOS, this.granule);
24710
+ }
24711
+ /** Reset all page/granule state so the instance can frame a fresh stream. */
24712
+ reset() {
24713
+ this.pageSeq = 0;
24714
+ this.granule = 0;
24715
+ this.headersEmitted = false;
24716
+ this.audioEmitted = false;
24717
+ }
24718
+ /** OpusHead identification packet (RFC 7845 §5.1) — 19 bytes for family 0. */
24719
+ buildOpusHead() {
24720
+ const buf = Buffer.alloc(19);
24721
+ OPUS_HEAD_MAGIC.copy(buf, 0);
24722
+ buf.writeUInt8(1, 8);
24723
+ buf.writeUInt8(this.channelCount, 9);
24724
+ buf.writeUInt16LE(this.preSkip & 65535, 10);
24725
+ buf.writeUInt32LE(OPUS_INPUT_SAMPLE_RATE, 12);
24726
+ buf.writeInt16LE(0, 16);
24727
+ buf.writeUInt8(0, 18);
24728
+ return buf;
24729
+ }
24730
+ /** OpusTags comment packet (RFC 7845 §5.2) — magic, vendor, 0 comments. */
24731
+ buildOpusTags() {
24732
+ const vendor = Buffer.from(this.vendor, "utf8");
24733
+ const buf = Buffer.alloc(12 + vendor.length + 4);
24734
+ OPUS_TAGS_MAGIC.copy(buf, 0);
24735
+ buf.writeUInt32LE(vendor.length, 8);
24736
+ vendor.copy(buf, 12);
24737
+ buf.writeUInt32LE(0, 12 + vendor.length);
24738
+ return buf;
24739
+ }
24740
+ /**
24741
+ * Assemble one Ogg page around a single packet (or none, for an EOS
24742
+ * terminator), compute its CRC, and return the framed bytes. Keeps to a
24743
+ * single packet per page so the segment count never approaches the 255 limit
24744
+ * (an Opus packet is ≤ ~1275 bytes → ≤ 6 lacing segments).
24745
+ */
24746
+ buildPage(packet, headerType, granule) {
24747
+ const lacing = packet !== null ? buildLacing(packet.length) : Buffer.alloc(0);
24748
+ const segmentCount = lacing.length;
24749
+ const bodyLen = packet !== null ? packet.length : 0;
24750
+ const page = Buffer.alloc(OGG_HEADER_FIXED_BYTES + segmentCount + bodyLen);
24751
+ OGG_CAPTURE_PATTERN.copy(page, 0);
24752
+ page.writeUInt8(0, 4);
24753
+ page.writeUInt8(headerType & 7, 5);
24754
+ this.writeGranule(page, granule, 6);
24755
+ page.writeUInt32LE(this.serial, 14);
24756
+ page.writeUInt32LE(this.pageSeq >>> 0, 18);
24757
+ page.writeUInt32LE(0, OGG_CRC_OFFSET);
24758
+ page.writeUInt8(segmentCount, 26);
24759
+ lacing.copy(page, OGG_HEADER_FIXED_BYTES);
24760
+ if (packet !== null) packet.copy(page, OGG_HEADER_FIXED_BYTES + segmentCount);
24761
+ const crc = oggCrc32(page);
24762
+ page.writeUInt32LE(crc, OGG_CRC_OFFSET);
24763
+ this.pageSeq = this.pageSeq + 1 >>> 0;
24764
+ return page;
24765
+ }
24766
+ /**
24767
+ * Write a 64-bit little-endian granule position. Values fit comfortably in a
24768
+ * JS safe integer for any realistic stream duration (2^53 samples @48k ≈ 5940
24769
+ * years), so a split 32-bit lo/hi write is exact and avoids BigInt on the
24770
+ * hot path.
24771
+ */
24772
+ writeGranule(page, granule, offset) {
24773
+ const lo = granule >>> 0;
24774
+ const hi = Math.floor(granule / 4294967296) >>> 0;
24775
+ page.writeUInt32LE(lo, offset);
24776
+ page.writeUInt32LE(hi, offset + 4);
24777
+ }
24778
+ };
24779
+ //#endregion
24780
+ //#region src/audio-codec/adts.ts
24781
+ /**
24782
+ * ADTS framing for raw AAC access units.
24783
+ *
24784
+ * The audio decode path runs `ffmpeg -f aac -i pipe:0` — the **ADTS** demuxer,
24785
+ * which requires each frame to begin with a 7-byte ADTS header (0xFFF sync).
24786
+ * But every AAC source that reaches the decoder delivers **raw** AAC access
24787
+ * units with no ADTS header: push sources (Reolink Baichuan) emit one bare
24788
+ * codec frame per packet, and rfc3640 (RTP AAC) depacketization yields bare
24789
+ * AUs too. Piping those straight into `-f aac` fails with
24790
+ * `Invalid data found when processing input` → no PCM → a silent WebRTC audio
24791
+ * track. Wrapping each raw AU in an ADTS header (built from the source
24792
+ * sample-rate / channel-count / AAC object type) makes the demuxer accept it.
24793
+ *
24794
+ * Opus is handled separately (Ogg encapsulation); this module is AAC-only.
24795
+ */
24796
+ /** MPEG-4 AAC sampling-frequency index table (ISO/IEC 14496-3). */
24797
+ var AAC_SAMPLE_RATES = [
24798
+ 96e3,
24799
+ 88200,
24800
+ 64e3,
24801
+ 48e3,
24802
+ 44100,
24803
+ 32e3,
24804
+ 24e3,
24805
+ 22050,
24806
+ 16e3,
24807
+ 12e3,
24808
+ 11025,
24809
+ 8e3,
24810
+ 7350
24811
+ ];
24812
+ /** AAC-LC is object type 2; the ADTS `profile` field is objectType - 1. */
24813
+ var DEFAULT_AAC_OBJECT_TYPE = 2;
24814
+ /** Resolve the ADTS sampling-frequency index for a sample rate (default 16 kHz → 8). */
24815
+ function aacSampleRateIndex(sampleRate) {
24816
+ const idx = AAC_SAMPLE_RATES.indexOf(sampleRate);
24817
+ return idx >= 0 ? idx : AAC_SAMPLE_RATES.indexOf(16e3);
24818
+ }
24819
+ /**
24820
+ * True when `buf` already begins with an ADTS syncword (0xFFF) + MPEG layer 0.
24821
+ * Raw AAC AUs never do; ADTS-framed frames always do. Lets the wrapper pass
24822
+ * already-framed input through untouched.
24823
+ */
24824
+ function hasAdtsSync(buf) {
24825
+ return buf.length >= 2 && buf[0] === 255 && (buf[1] & 246) === 240;
24826
+ }
24827
+ /**
24828
+ * Build the 7-byte ADTS header (no CRC) for a payload of `payloadLength` bytes.
24829
+ */
24830
+ function buildAdtsHeader(config, payloadLength) {
24831
+ const objectType = config.aacObjectType ?? DEFAULT_AAC_OBJECT_TYPE;
24832
+ const profile = Math.max(0, objectType - 1) & 3;
24833
+ const freqIdx = aacSampleRateIndex(config.sampleRate) & 15;
24834
+ const chanCfg = Math.max(1, config.channels) & 7;
24835
+ const frameLength = payloadLength + 7;
24836
+ const h = Buffer.alloc(7);
24837
+ h[0] = 255;
24838
+ h[1] = 241;
24839
+ h[2] = profile << 6 | freqIdx << 2 | chanCfg >> 2 & 1;
24840
+ h[3] = (chanCfg & 3) << 6 | frameLength >> 11 & 3;
24841
+ h[4] = frameLength >> 3 & 255;
24842
+ h[5] = (frameLength & 7) << 5 | 31;
24843
+ h[6] = 252;
24844
+ return h;
24845
+ }
24846
+ /**
24847
+ * Return `frame` framed as ADTS: unchanged when it already carries an ADTS
24848
+ * sync, otherwise the 7-byte header prepended. Pure; the input is never mutated.
24849
+ */
24850
+ function wrapAacAsAdts(frame, config) {
24851
+ if (hasAdtsSync(frame)) return Buffer.from(frame.buffer, frame.byteOffset, frame.byteLength);
24852
+ const payload = Buffer.from(frame.buffer, frame.byteOffset, frame.byteLength);
24853
+ return Buffer.concat([buildAdtsHeader(config, payload.length), payload]);
24854
+ }
24855
+ //#endregion
24856
+ //#region src/audio-codec/ffmpeg-audio-decode-session.ts
24857
+ var FfmpegAudioDecodeSession = class {
24858
+ logger;
24859
+ process;
24860
+ outFormat;
24861
+ bytesPerFrame;
24862
+ targetSampleRate;
24863
+ targetChannels;
24864
+ onChunk;
24865
+ /**
24866
+ * Ogg-Opus encapsulator, present ONLY when the codec is Opus. Raw Opus
24867
+ * packets have no ffmpeg demuxer, so each pushed packet is wrapped in an
24868
+ * Ogg page (with OpusHead/OpusTags emitted before the first) so
24869
+ * `ffmpeg -f ogg -i pipe:0` can demux + decode. `null` for all other codecs,
24870
+ * which pipe their encoded frames straight through.
24871
+ */
24872
+ oggFramer;
24873
+ /**
24874
+ * ADTS framing config, present ONLY when the codec is AAC. `ffmpeg -f aac`
24875
+ * is the ADTS demuxer, but every AAC source delivers raw AAC access units
24876
+ * (push sources emit bare frames; rfc3640 depacketization yields bare AUs) —
24877
+ * piping those in raw fails `Invalid data found when processing input` → no
24878
+ * PCM → silent WebRTC audio. Each frame is ADTS-wrapped before ffmpeg.
24879
+ */
24880
+ adtsConfig;
24881
+ residual = Buffer.alloc(0);
24882
+ nextPts = 0;
24883
+ destroyed = false;
24884
+ constructor(params, logger, onChunk) {
24885
+ this.logger = logger;
24886
+ this.onChunk = onChunk;
24887
+ this.outFormat = params.targetFormat ?? "s16le";
24888
+ this.targetSampleRate = params.targetSampleRate;
24889
+ this.targetChannels = params.targetChannels;
24890
+ this.bytesPerFrame = Math.max(1, params.targetChannels * audioBytesPerSample(this.outFormat));
24891
+ this.oggFramer = resolveAudioCodecAlias(params.codec) === "opus" ? new OggOpusFramer({
24892
+ channelCount: Math.max(1, params.sourceChannels),
24893
+ serial: serialFromSeed(params.oggSerialSeed ?? "audio-codec-ffmpeg-opus")
24894
+ }) : null;
24895
+ this.adtsConfig = resolveAudioCodecAlias(params.codec) === "aac" ? {
24896
+ sampleRate: params.sourceSampleRate,
24897
+ channels: Math.max(1, params.sourceChannels)
24898
+ } : null;
24899
+ const args = buildAudioDecodeArgs(params);
24900
+ this.process = new FfmpegAudioProcess({
24901
+ ffmpegPath: params.ffmpegPath,
24902
+ args,
24903
+ logger,
24904
+ onStdout: (chunk) => this.handleStdout(chunk),
24905
+ onExit: (code, signal) => {
24906
+ if (this.destroyed) return;
24907
+ this.logger.warn("audio-codec-ffmpeg: decode child exited", { meta: {
24908
+ code,
24909
+ signal
24910
+ } });
24911
+ }
24912
+ });
24913
+ }
24914
+ /** Push one encoded audio frame into ffmpeg stdin. */
24915
+ pushEncoded(data) {
24916
+ if (this.destroyed) return;
24917
+ if (this.oggFramer) {
24918
+ const packet = Buffer.from(data.buffer, data.byteOffset, data.byteLength);
24919
+ this.process.write(this.oggFramer.framePacket(packet));
24920
+ return;
24921
+ }
24922
+ if (this.adtsConfig) {
24923
+ this.process.write(wrapAacAsAdts(data, this.adtsConfig));
24924
+ return;
24925
+ }
24926
+ this.process.write(data);
24927
+ }
24928
+ handleStdout(chunk) {
24929
+ if (this.destroyed) return;
24930
+ const buf = this.residual.length === 0 ? chunk : Buffer.concat([this.residual, chunk]);
24931
+ const frameBytes = this.bytesPerFrame;
24932
+ const usable = buf.length - buf.length % frameBytes;
24933
+ if (usable <= 0) {
24934
+ this.residual = buf;
24935
+ return;
24936
+ }
24937
+ const emit = buf.subarray(0, usable);
24938
+ this.residual = usable < buf.length ? Buffer.from(buf.subarray(usable)) : Buffer.alloc(0);
24939
+ const out = new Uint8Array(new ArrayBuffer(usable));
24940
+ out.set(emit);
24941
+ const samples = usable / frameBytes;
24942
+ const pts = this.nextPts;
24943
+ this.nextPts = pts + Math.round(samples * 1e3 / this.targetSampleRate);
24944
+ this.onChunk({
24945
+ data: out,
24946
+ sampleRate: this.targetSampleRate,
24947
+ channels: this.targetChannels,
24948
+ format: this.outFormat,
24949
+ pts
24950
+ });
24951
+ }
24952
+ destroy() {
24953
+ if (this.destroyed) return;
24954
+ this.destroyed = true;
24955
+ if (this.oggFramer) {
24956
+ const eos = this.oggFramer.flush();
24957
+ if (eos) this.process.write(eos);
24958
+ }
24959
+ this.process.kill();
24960
+ this.residual = Buffer.alloc(0);
24961
+ }
24962
+ };
24963
+ //#endregion
24964
+ //#region src/audio-codec/ffmpeg-audio-encode-session.ts
24965
+ var FfmpegAudioEncodeSession = class {
24966
+ logger;
24967
+ process;
24968
+ codec;
24969
+ onChunk;
24970
+ nextPts = 0;
24971
+ destroyed = false;
24972
+ constructor(params, logger, onChunk) {
24973
+ this.logger = logger;
24974
+ this.onChunk = onChunk;
24975
+ this.codec = resolveAudioCodecAlias(params.codec);
24976
+ const args = buildAudioEncodeArgs(params);
24977
+ this.process = new FfmpegAudioProcess({
24978
+ ffmpegPath: params.ffmpegPath,
24979
+ args,
24980
+ logger,
24981
+ onStdout: (chunk) => this.handleStdout(chunk),
24982
+ onExit: (code, signal) => {
24983
+ if (this.destroyed) return;
24984
+ this.logger.warn("audio-codec-ffmpeg: encode child exited", { meta: {
24985
+ code,
24986
+ signal
24987
+ } });
24988
+ }
24989
+ });
24990
+ }
24991
+ /** Push one PCM chunk into ffmpeg stdin. */
24992
+ pushPcm(data) {
24993
+ if (this.destroyed) return;
24994
+ this.process.write(data);
24995
+ }
24996
+ /** Close stdin so ffmpeg flushes and drains the remaining encoded output. */
24997
+ flush() {
24998
+ if (this.destroyed) return;
24999
+ this.process.endStdin();
25000
+ }
25001
+ handleStdout(chunk) {
25002
+ if (this.destroyed || chunk.length === 0) return;
25003
+ const out = new Uint8Array(new ArrayBuffer(chunk.length));
25004
+ out.set(chunk);
25005
+ const pts = this.nextPts;
25006
+ this.nextPts = pts + 1;
25007
+ this.onChunk({
25008
+ data: out,
25009
+ codec: this.codec,
25010
+ pts,
25011
+ frameComplete: true
25012
+ });
25013
+ }
25014
+ destroy() {
25015
+ if (this.destroyed) return;
25016
+ this.destroyed = true;
25017
+ this.process.kill();
25018
+ }
25019
+ };
25020
+ //#endregion
25021
+ //#region src/audio-codec/provider.ts
25022
+ var CODEC_CATALOG = [
25023
+ {
25024
+ codec: "pcm_mulaw",
25025
+ canDecode: true,
25026
+ canEncode: true,
25027
+ label: "PCM µ-law (G.711)"
25028
+ },
25029
+ {
25030
+ codec: "pcm_alaw",
25031
+ canDecode: true,
25032
+ canEncode: true,
25033
+ label: "PCM A-law (G.711)"
25034
+ },
25035
+ {
25036
+ codec: "g722",
25037
+ canDecode: true,
25038
+ canEncode: true,
25039
+ label: "G.722"
25040
+ },
25041
+ {
25042
+ codec: "aac",
25043
+ canDecode: true,
25044
+ canEncode: true,
25045
+ label: "AAC"
25046
+ },
25047
+ {
25048
+ codec: "aac_latm",
25049
+ canDecode: true,
25050
+ canEncode: false,
25051
+ label: "AAC LATM"
25052
+ },
25053
+ {
25054
+ codec: "mpeg4-generic",
25055
+ canDecode: true,
25056
+ canEncode: false,
25057
+ label: "AAC (MPEG4-GENERIC)"
25058
+ },
25059
+ {
25060
+ codec: "opus",
25061
+ canDecode: true,
25062
+ canEncode: true,
25063
+ label: "Opus"
25064
+ }
25065
+ ];
25066
+ var DEFAULT_IDLE_MS = 3e4;
25067
+ var MAX_PCM_QUEUE_CHUNKS = 500;
25068
+ var REAPER_INTERVAL_MS = 5e3;
25069
+ /**
25070
+ * Grace wait in `flushEncode` for ffmpeg to drain its encoder after stdin is
25071
+ * closed. This is a graceful-teardown path (not the real-time push path), so a
25072
+ * short wait is acceptable to capture the encoder's tail output.
25073
+ */
25074
+ var FLUSH_DRAIN_MS = 60;
25075
+ /**
25076
+ * Audio codec I/O box backed by an **ffmpeg subprocess** (Phase B replacement
25077
+ * for `audio-codec-nodeav`, which runs libavcodec + libswresample in-process).
25078
+ *
25079
+ * Each `createDecodeSession` / `createEncodeSession` spawns its own ffmpeg
25080
+ * child, so consumers never share a resampler and a codec crash is isolated to
25081
+ * the child — the addon runner survives. Cap surface + session bookkeeping
25082
+ * mirror the node-av addon exactly; only the codec backend changed.
25083
+ *
25084
+ * This is a PLAIN provider class merged into the `decoder-ffmpeg` addon (which
25085
+ * provides both the `decoder` video cap and this `audio-codec` cap). The parent
25086
+ * addon owns lifecycle: it resolves the ffmpeg path once, calls {@link start} in
25087
+ * `onInitialize`, and {@link stop} in `onShutdown`.
25088
+ */
25089
+ var FfmpegAudioCodecProvider = class {
25090
+ deps;
25091
+ sessions = /* @__PURE__ */ new Map();
25092
+ reaperTimer = null;
25093
+ constructor(deps) {
25094
+ this.deps = deps;
25095
+ }
25096
+ /** Start the idle-session reaper. Called by the parent addon at init. */
25097
+ start() {
25098
+ this.reaperTimer = setInterval(() => this.reapIdleSessions(), REAPER_INTERVAL_MS);
25099
+ if (typeof this.reaperTimer.unref === "function") this.reaperTimer.unref();
25100
+ }
25101
+ /** Clear the reaper and dispose every session. Called by the parent at shutdown. */
25102
+ stop() {
25103
+ if (this.reaperTimer) {
25104
+ clearInterval(this.reaperTimer);
25105
+ this.reaperTimer = null;
25106
+ }
25107
+ for (const s of this.sessions.values()) this.disposeSession(s);
25108
+ this.sessions.clear();
25109
+ }
25110
+ async listSupportedCodecs() {
25111
+ return CODEC_CATALOG.map((e) => ({
25112
+ codec: e.codec,
25113
+ canDecode: e.canDecode,
25114
+ canEncode: e.canEncode,
25115
+ ...e.label ? { label: e.label } : {}
25116
+ }));
25117
+ }
25118
+ async canHandle(input) {
25119
+ const resolved = resolveAudioCodecAlias(input.codec);
25120
+ const entry = CODEC_CATALOG.find((e) => resolveAudioCodecAlias(e.codec) === resolved);
25121
+ if (!entry) return false;
25122
+ return input.kind === "decode" ? entry.canDecode : entry.canEncode;
25123
+ }
25124
+ async createDecodeSession(input) {
25125
+ const codec = resolveAudioCodecAlias(input.codec);
25126
+ const entry = CODEC_CATALOG.find((e) => resolveAudioCodecAlias(e.codec) === codec);
25127
+ if (!entry || !entry.canDecode) throw new Error(`audio-codec-ffmpeg: decode unsupported for codec '${input.codec}'`);
25128
+ const sessionId = `dec-${(0, node_crypto.randomUUID)()}`;
25129
+ const state = {
25130
+ sessionId,
25131
+ kind: "decode",
25132
+ config: {
25133
+ ...input,
25134
+ codec
25135
+ },
25136
+ ...input.tag ? { tag: input.tag } : {},
25137
+ createdAtMs: Date.now(),
25138
+ lastActivityMs: Date.now(),
25139
+ framesIn: 0,
25140
+ framesOut: 0,
25141
+ pcmQueue: [],
25142
+ session: null
25143
+ };
25144
+ state.session = this.spawnDecodeSession(state);
25145
+ this.sessions.set(sessionId, state);
25146
+ this.deps.logger.info("audio-codec-ffmpeg: decode session created", {
25147
+ tags: { sessionId },
25148
+ meta: {
25149
+ codec,
25150
+ target: `${input.targetSampleRate}Hz×${input.targetChannels}`
25151
+ }
25152
+ });
25153
+ return {
25154
+ sessionId,
25155
+ nodeId: this.deps.resolveLocalNodeId()
25156
+ };
25157
+ }
25158
+ async createEncodeSession(input) {
25159
+ const codec = resolveAudioCodecAlias(input.codec);
25160
+ const entry = CODEC_CATALOG.find((e) => resolveAudioCodecAlias(e.codec) === codec);
25161
+ if (!entry || !entry.canEncode) throw new Error(`audio-codec-ffmpeg: encode unsupported for codec '${input.codec}'`);
25162
+ const sessionId = `enc-${(0, node_crypto.randomUUID)()}`;
25163
+ const state = {
25164
+ sessionId,
25165
+ kind: "encode",
25166
+ config: {
25167
+ ...input,
25168
+ codec
25169
+ },
25170
+ ...input.tag ? { tag: input.tag } : {},
25171
+ createdAtMs: Date.now(),
25172
+ lastActivityMs: Date.now(),
25173
+ framesIn: 0,
25174
+ framesOut: 0,
25175
+ encodedQueue: [],
25176
+ session: null
25177
+ };
25178
+ state.session = this.spawnEncodeSession(state);
25179
+ this.sessions.set(sessionId, state);
25180
+ this.deps.logger.info("audio-codec-ffmpeg: encode session created", {
25181
+ tags: { sessionId },
25182
+ meta: {
25183
+ codec,
25184
+ target: `${input.targetSampleRate}Hz×${input.targetChannels}`
25185
+ }
25186
+ });
25187
+ return {
25188
+ sessionId,
25189
+ nodeId: this.deps.resolveLocalNodeId()
25190
+ };
25191
+ }
25192
+ async closeSession(input) {
25193
+ const s = this.sessions.get(input.sessionId);
25194
+ if (!s) return;
25195
+ this.disposeSession(s);
25196
+ this.sessions.delete(input.sessionId);
25197
+ }
25198
+ async pushEncodedFrame(input) {
25199
+ const s = this.sessions.get(input.sessionId);
25200
+ if (!s || s.kind !== "decode") throw new Error(`audio-codec-ffmpeg: decode session '${input.sessionId}' not found`);
25201
+ s.lastActivityMs = Date.now();
25202
+ s.framesIn++;
25203
+ if (!s.session) s.session = this.spawnDecodeSession(s);
25204
+ s.session.pushEncoded(input.data);
25205
+ }
25206
+ async pullPcm(input) {
25207
+ const s = this.sessions.get(input.sessionId);
25208
+ if (!s || s.kind !== "decode") throw new Error(`audio-codec-ffmpeg: decode session '${input.sessionId}' not found`);
25209
+ s.lastActivityMs = Date.now();
25210
+ const out = s.pcmQueue.splice(0, input.maxCount);
25211
+ s.framesOut += out.length;
25212
+ return out;
25213
+ }
25214
+ async pushPcm(input) {
25215
+ const s = this.sessions.get(input.sessionId);
25216
+ if (!s || s.kind !== "encode") throw new Error(`audio-codec-ffmpeg: encode session '${input.sessionId}' not found`);
25217
+ s.lastActivityMs = Date.now();
25218
+ s.framesIn++;
25219
+ if (!s.session) s.session = this.spawnEncodeSession(s);
25220
+ s.session.pushPcm(input.data);
25221
+ }
25222
+ async pullEncoded(input) {
25223
+ const s = this.sessions.get(input.sessionId);
25224
+ if (!s || s.kind !== "encode") throw new Error(`audio-codec-ffmpeg: encode session '${input.sessionId}' not found`);
25225
+ s.lastActivityMs = Date.now();
25226
+ const out = s.encodedQueue.splice(0, input.maxCount);
25227
+ s.framesOut += out.length;
25228
+ return out;
25229
+ }
25230
+ async flushEncode(input) {
25231
+ const s = this.sessions.get(input.sessionId);
25232
+ if (!s || s.kind !== "encode") throw new Error(`audio-codec-ffmpeg: encode session '${input.sessionId}' not found`);
25233
+ s.lastActivityMs = Date.now();
25234
+ s.session?.flush();
25235
+ await new Promise((resolve) => setTimeout(resolve, FLUSH_DRAIN_MS));
25236
+ const out = s.encodedQueue.splice(0);
25237
+ s.framesOut += out.length;
25238
+ return out;
25239
+ }
25240
+ async listActiveSessions() {
25241
+ return [...this.sessions.values()].map((s) => ({
25242
+ sessionId: s.sessionId,
25243
+ kind: s.kind,
25244
+ codec: s.config.codec,
25245
+ sourceSampleRate: s.config.sourceSampleRate,
25246
+ sourceChannels: s.config.sourceChannels,
25247
+ targetSampleRate: s.config.targetSampleRate,
25248
+ targetChannels: s.config.targetChannels,
25249
+ format: this.resolveFormat(s),
25250
+ ...s.tag ? { tag: s.tag } : {},
25251
+ createdAtMs: s.createdAtMs,
25252
+ lastActivityMs: s.lastActivityMs,
25253
+ framesIn: s.framesIn,
25254
+ framesOut: s.framesOut
25255
+ }));
25256
+ }
25257
+ spawnDecodeSession(s) {
25258
+ return new FfmpegAudioDecodeSession({
25259
+ codec: s.config.codec,
25260
+ sourceSampleRate: s.config.sourceSampleRate,
25261
+ sourceChannels: s.config.sourceChannels,
25262
+ targetSampleRate: s.config.targetSampleRate,
25263
+ targetChannels: s.config.targetChannels,
25264
+ targetFormat: this.pcmFormat(s.config.targetFormat),
25265
+ ffmpegPath: this.deps.ffmpegPath,
25266
+ oggSerialSeed: s.sessionId
25267
+ }, this.deps.logger, (chunk) => {
25268
+ s.pcmQueue.push(chunk);
25269
+ if (s.pcmQueue.length > MAX_PCM_QUEUE_CHUNKS) s.pcmQueue.splice(0, s.pcmQueue.length - MAX_PCM_QUEUE_CHUNKS);
25270
+ });
25271
+ }
25272
+ spawnEncodeSession(s) {
25273
+ return new FfmpegAudioEncodeSession({
25274
+ codec: s.config.codec,
25275
+ sourceSampleRate: s.config.sourceSampleRate,
25276
+ sourceChannels: s.config.sourceChannels,
25277
+ sourceFormat: this.pcmFormat(s.config.sourceFormat),
25278
+ targetSampleRate: s.config.targetSampleRate,
25279
+ targetChannels: s.config.targetChannels,
25280
+ ...s.config.bitrateKbps !== void 0 ? { bitrateKbps: s.config.bitrateKbps } : {},
25281
+ ffmpegPath: this.deps.ffmpegPath
25282
+ }, this.deps.logger, (chunk) => {
25283
+ s.encodedQueue.push(chunk);
25284
+ });
25285
+ }
25286
+ /** Narrow the cap's PCM format enum to the two ffmpeg sessions produce/read. */
25287
+ pcmFormat(format) {
25288
+ return format === "f32le" ? "f32le" : "s16le";
25289
+ }
25290
+ resolveFormat(s) {
25291
+ if (s.kind === "decode") return this.pcmFormat(s.config.targetFormat);
25292
+ return this.pcmFormat(s.config.sourceFormat);
25293
+ }
25294
+ reapIdleSessions() {
25295
+ const now = Date.now();
25296
+ for (const [id, s] of this.sessions) {
25297
+ const limit = s.config.idleMs ?? this.deps.defaultIdleMs ?? DEFAULT_IDLE_MS;
25298
+ if (now - s.lastActivityMs > limit) {
25299
+ this.deps.logger.info("audio-codec-ffmpeg: reaping idle session", {
25300
+ tags: { sessionId: id },
25301
+ meta: {
25302
+ kind: s.kind,
25303
+ idleMs: now - s.lastActivityMs,
25304
+ limit
25305
+ }
25306
+ });
25307
+ try {
25308
+ this.disposeSession(s);
25309
+ } catch (err) {
25310
+ this.deps.logger.warn("audio-codec-ffmpeg: dispose failed during reap", {
25311
+ tags: { sessionId: id },
25312
+ meta: { error: errMsg(err) }
25313
+ });
25314
+ }
25315
+ this.sessions.delete(id);
25316
+ }
25317
+ }
25318
+ }
25319
+ disposeSession(s) {
25320
+ try {
25321
+ s.session?.destroy();
25322
+ } catch (err) {
25323
+ this.deps.logger.warn("audio-codec-ffmpeg: session destroy failed", {
25324
+ tags: { sessionId: s.sessionId },
25325
+ meta: { error: errMsg(err) }
25326
+ });
25327
+ }
25328
+ s.session = null;
25329
+ }
25330
+ };
25331
+ //#endregion
24141
25332
  //#region src/addon/index.ts
24142
25333
  var FRAME_BUFFER_CAPACITY = 32;
24143
25334
  /**
@@ -24177,6 +25368,14 @@ var DecoderFfmpegAddon = class extends BaseAddon {
24177
25368
  * process — e.g. a videotoolbox build whose decode returns software frames.
24178
25369
  */
24179
25370
  unsupportedGpuScaleFilters = /* @__PURE__ */ new Set();
25371
+ /**
25372
+ * The merged-in ffmpeg AUDIO codec provider — registers the `audio-codec` cap
25373
+ * ALWAYS (independent of the per-node video decoder backend), so this addon
25374
+ * serves both video decode and bidirectional audio transcode. Owns its own
25375
+ * ffmpeg subprocess sessions + idle reaper; started at init, stopped at
25376
+ * shutdown. Shares the single node-level ffmpeg binary this addon resolves.
25377
+ */
25378
+ audioProvider = null;
24180
25379
  constructor() {
24181
25380
  super(DEFAULT_DECODER_FFMPEG_ADDON_CONFIG);
24182
25381
  }
@@ -24214,23 +25413,35 @@ var DecoderFfmpegAddon = class extends BaseAddon {
24214
25413
  if (!this.config.probedBestHwaccel) this.reprobeHwaccel().catch((err) => {
24215
25414
  this.ctx.logger.warn("ffmpeg: auto-reprobe hwaccel failed", { meta: { error: err instanceof Error ? err.message : String(err) } });
24216
25415
  });
25416
+ const registrations = [];
25417
+ this.ffmpegPath = await this.resolveFfmpegBinaryPath();
25418
+ this.audioProvider = new FfmpegAudioCodecProvider({
25419
+ logger: this.ctx.logger,
25420
+ ffmpegPath: this.ffmpegPath,
25421
+ resolveLocalNodeId: () => this.resolveLocalNodeId()
25422
+ });
25423
+ this.audioProvider.start();
25424
+ registrations.push({
25425
+ capability: audioCodecCapability,
25426
+ provider: this.audioProvider
25427
+ });
24217
25428
  const backend = await resolveDecoderBackend(this.ctx.api, this.resolveLocalNodeId(), this.ctx.logger);
24218
25429
  if (backend !== "ffmpeg") {
24219
- this.ctx.logger.info("ffmpeg decoder: this node selects a different decoder backend — standing down (no decoder provider registered)", { meta: { selectedBackend: backend } });
24220
- return [];
25430
+ this.ctx.logger.info("ffmpeg decoder: this node selects a different decoder backend — standing down (no decoder provider registered; audio-codec stays)", { meta: { selectedBackend: backend } });
25431
+ return registrations;
24221
25432
  }
24222
25433
  this.ctx.logger.info("ffmpeg decoder addon initialized", { meta: { selectedBackend: backend } });
24223
25434
  this.frameReaders = new _camstack_shm_ring.FrameRingReaderCache(this.ctx.logger);
24224
- this.ffmpegPath = await this.resolveFfmpegBinaryPath();
24225
25435
  this.probedGpuScaleFilters = await probeGpuScaleFilters(this.ffmpegPath, this.ctx.logger);
24226
25436
  this.ctx.logger.info("decoder-ffmpeg: probed GPU scale filters", { meta: {
24227
25437
  filters: [...this.probedGpuScaleFilters],
24228
25438
  ffmpeg: this.ffmpegPath
24229
25439
  } });
24230
- return [{
25440
+ registrations.push({
24231
25441
  capability: decoderCapability,
24232
25442
  provider: this
24233
- }];
25443
+ });
25444
+ return registrations;
24234
25445
  }
24235
25446
  /**
24236
25447
  * Resolve the ffmpeg binary the sessions spawn — node-level only.
@@ -24518,6 +25729,8 @@ var DecoderFfmpegAddon = class extends BaseAddon {
24518
25729
  }
24519
25730
  async onShutdown() {
24520
25731
  this.ctx.logger.info("ffmpeg decoder addon shutdown — destroying all sessions");
25732
+ this.audioProvider?.stop();
25733
+ this.audioProvider = null;
24521
25734
  const destroyPromises = [];
24522
25735
  for (const [sessionId, session] of this.sessions) {
24523
25736
  const unsub = this.unsubscribers.get(sessionId);