Files
buzz/crates/buzz-audio-client/src/encoder.rs
klopez4212 5dc693aeaf Extract shared Huddle audio client for desktop consumers (#8129)
Huddle audio was embedded in the v0 desktop client, forcing other
clients to duplicate its protocol and codec behavior. This extracts
`buzz-audio-client` so v1 can depend on a pinned library revision
without building the v0 application.

The crate owns the bounded v2 audio handshake, roster parsing, wire
format, Opus sender, and per-peer NetEQ jitter buffer. The v0 human and
agent senders use it, and the desktop retains room admission,
identity/signing, TLS policy, device routing, mute, and teardown.
Receive decoding preserves v1's support for 120 ms Opus packets. No
relay protocol changes, speech models, or model downloads are included.
Docker builder stages include CMake because cargo-chef compiles the
workspace dependency graph.

Dependency security: Opus 0.4 replaces the unmaintained `audiopus_sys`
backend with `opusic-sys`. NetEQ is pinned exactly to 0.8.3 because its
published Rust sources never reference its declared ringbuf dependency.
RUSTSEC-2026-0293 is documented as unreachable for this audited source,
and a wrapper restriction rejects every other consumer of vulnerable
ringbuf versions. Independent review confirmed the scope;
re-audit/remove the exception when changing NetEQ. NetEQ 0.9.1 still
declares ringbuf 0.3, so a version bump alone does not remove it. Local
`cargo deny check` passes with this constrained exception.

Validation:
- All 21 shared-crate tests pass, including real loopback WebSocket
authentication/cancellation, real Opus-to-NetEQ playback, malformed
input, and 120 ms decoding. These tests are included in `just
test-unit`.
- Shared-crate, root workspace, and desktop Tauri Clippy pass; `just
desktop-tauri-check` passes.
- The desktop Tauri test workspace passes; native two-person
listening/device checks remain separate.
- All mandatory push checks pass at `d06bac5dc`, including the complete
Rust unit gate and desktop workspace checks. Existing agent timing and
relay log-capture tests failed under concurrent load, passed separately,
and passed in the full serial test run; no tests or hooks were skipped.
- One test-only fixture change allows cold Perl startup before the
escaped-process test's inner timeout (2 seconds instead of 300 ms). Its
independent 5-second watchdog, 300-second surviving pipe writer, and all
assertions remain intact. All eight process-helper tests pass;
independent review confirmed this still tests bounded drain completion.
- Dependency inspection confirms the library brings in no desktop
application, Tauri, identity store, relay server, or speech-model
crates.
- Independent source review found no correctness blockers.

Draft: a native two-person call, mute/leave/cancellation, and
microphone/speaker switching still need a runtime tryout and human
review. Full `just ci` was stopped during the unrelated mobile toolchain
download because of disk pressure; it is not a passing gate. Consumer
integration is tracked in block/buzz-app#489.

---------

Signed-off-by: kenny lopez <klopez4212@gmail.com>
2026-10-13 16:24:27 -04:00

66 lines
2.4 KiB
Rust

//! Stateful 48 kHz mono Opus sender shared by human and agent publishers.
use crate::wire::{audio_level_dbov, FrameHeader, FLAG_DTX, V2_HEADER_LEN};
/// Samples in one 20 ms, 48 kHz mono frame.
pub const FRAME_SAMPLES: usize = 960;
/// Opus encoder and v2 sequence/media clock for one connection.
pub struct AudioEncoder {
opus: opus::Encoder,
sequence: u16,
timestamp: u32,
output: Vec<u8>,
}
impl AudioEncoder {
/// Create the standard 32 kbit/s VoIP encoder with discontinuous transmission.
pub fn new() -> Result<Self, String> {
let mut opus = opus::Encoder::new(48_000, opus::Channels::Mono, opus::Application::Voip)
.map_err(|e| format!("opus encoder: {e}"))?;
opus.set_bitrate(opus::Bitrate::Bits(32_000))
.map_err(|e| format!("opus bitrate: {e}"))?;
opus.set_dtx(true).map_err(|e| format!("opus dtx: {e}"))?;
Ok(Self {
opus,
sequence: 0,
timestamp: 0,
output: vec![0; 4_000],
})
}
/// Encode up to 20 ms of finite mono PCM; pad the final short frame with silence.
///
/// Returns the eight-byte v2 header followed by Opus. Empty/oversized or
/// non-finite input is rejected without advancing the media clock.
pub fn encode(&mut self, samples: &[f32]) -> Result<Vec<u8>, String> {
if samples.is_empty()
|| samples.len() > FRAME_SAMPLES
|| samples.iter().any(|s| !s.is_finite())
{
return Err("Invalid Huddle audio frame".into());
}
let mut padded = [0.0; FRAME_SAMPLES];
padded[..samples.len()].copy_from_slice(samples);
let n = self
.opus
.encode_float(&padded, &mut self.output)
.map_err(|e| format!("opus encode: {e}"))?;
if n == 0 {
return Err("Opus produced an empty frame".into());
}
let header = FrameHeader {
seq: self.sequence,
ts_48k: self.timestamp,
level_dbov: audio_level_dbov(samples),
flags: if n <= 2 { FLAG_DTX } else { 0 },
};
let mut frame = Vec::with_capacity(V2_HEADER_LEN + n);
frame.extend_from_slice(&header.encode());
frame.extend_from_slice(&self.output[..n]);
self.sequence = self.sequence.wrapping_add(1);
self.timestamp = self.timestamp.wrapping_add(FRAME_SAMPLES as u32);
Ok(frame)
}
}