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