Bundle Linux voice runtimes and improve audio reliability (#44714)

## Why

Linux voice needs system ALSA plugins and enough buffering to accommodate PipeWire graph cycles without losing capture samples. Voice startup failures also need actionable diagnostics without exposing native error details.

## What changed

- Build and bundle GNU voice helpers and runtimes with primary Linux musl release archives, and sign the archives. Keep Python wheels free of these libraries to preserve `manylinux_2_17` compatibility.
- Discover ALSA plugins in fixed system directories and increase Linux capture and playback buffering to support larger PipeWire graph cycles.
- Report voice failures by stage, preserve negotiation timeout classification, and discard native error sources. Suppress the misleading `requested` closure message after failure cleanup.
- Add explicit Windows MSVC, pkgconf, and CMake toolchain configuration and preserve host architecture in native build environments.

## Testing

Add coverage for Linux release assembly, ALSA plugin discovery, PipeWire capture and playback, classified startup failures, failure cleanup rendering, and Windows build environment handling.

GitOrigin-RevId: d805eace96a669ce3a4489f12e2db6f68f9f7f53
This commit is contained in:
Benjamin Carlsson authored and copyberry committed 2026-09-11 03:16:01 +00:00
1 parent 08e49689b8
commit da20788df9
27 files changed
+578 -70

No files matched your search

+6 -3
View File
@@ -243,9 +243,12 @@ fn bounded_stream_config(
if min > max {
return Err(io::Error::other("unsupported audio callback size range"));
}
// Aim for 10 ms without consuming the queue's service headroom.
// Do not fall back to the backend's potentially much larger default buffer.
let frames = (config.sample_rate / 100).clamp(min, max);
// ALSA allocates two periods. A 20 ms ring can be smaller than one PipeWire
// graph cycle (e.g. 2048 frames at 48 kHz), silently losing capture samples
// every cycle. Give Linux a bounded 100 ms ring instead, while retaining
// 10 ms callbacks elsewhere and rejecting incompatible device ranges below.
let periods_per_second = if cfg!(target_os = "linux") { 20 } else { 100 };
let frames = (config.sample_rate / periods_per_second).clamp(min, max);
let callback_duration =
Duration::from_secs_f64(f64::from(frames) / f64::from(config.sample_rate));
// Backends may deliver smaller callbacks than requested. Packing makes queue
+86 -10
View File
@@ -72,17 +72,22 @@ fn startup_output_waits_for_service_and_later_overflow_discards_reference() {
#[test]
fn callback_configuration_fits_supported_range_and_actual_queue() {
for (rate, min, max, frames) in [
(96_000, 0, u32::MAX, 960),
(96_000, 1, 16, 16),
(384_000, 1, 64, 64),
(8_000, 7_680, 8_192, 7_680),
(384_000, 1, 16, 16),
(96_000, 1, 15, 15),
(48_000, 1, 128, 128),
(48_000, 2_048, 16_384, 2_048),
(384_000, 6_016, 16_384, 6_016),
for (rate, min, max, other_frames, linux_frames) in [
(96_000, 0, u32::MAX, 960, 4_800),
(96_000, 1, 16, 16, 16),
(384_000, 1, 64, 64, 64),
(8_000, 7_680, 8_192, 7_680, 7_680),
(384_000, 1, 16, 16, 16),
(96_000, 1, 15, 15, 15),
(48_000, 1, 128, 128, 128),
(48_000, 2_048, 16_384, 2_048, 2_400),
(384_000, 6_016, 16_384, 6_016, 8_192),
] {
let frames = if cfg!(target_os = "linux") {
linux_frames
} else {
other_frames
};
let supported = cpal::SupportedStreamConfig::new(
/*channels*/ 2,
rate,
@@ -116,6 +121,77 @@ fn callback_configuration_fits_supported_range_and_actual_queue() {
}
}
#[test]
#[cfg(target_os = "linux")]
fn pipewire_graph_cycles_support_capture_and_playback() {
let supported = cpal::SupportedStreamConfig::new(
/*channels*/ 2,
/*sample_rate*/ 48_000,
cpal::SupportedBufferSize::Range {
min: 1,
max: 16_384,
},
cpal::SampleFormat::F32,
);
let cpal::BufferSize::Fixed(period) = bounded_stream_config(&supported).unwrap().buffer_size
else {
panic!("callback size must be bounded");
};
let mut processor =
processing::Processor::new(/*input_rate*/ 48_000, /*output_rate*/ 48_000).unwrap();
let mut packer = FramePacker::default();
let buffers = Arc::new(Buffers::new(
/*input_rate*/ 48_000, /*output_rate*/ 48_000,
));
let start = Instant::now();
let mut available = 0;
let mut packets = Vec::new();
// Model measured 2048-frame graph cycles and ALSA's two-period ring.
// Packing and encoding must sustain 20 ms packets despite this cadence.
for cycle in 1..=48 {
available = (available + 2_048).min(2 * period);
let now = start + Duration::from_secs_f64(f64::from(cycle * 2_048) / 48_000.0);
while available >= period {
let captured = now - Duration::from_secs_f64(f64::from(available) / 48_000.0);
packer.discard_capture_gap(captured, /*rate*/ 48_000.0);
for offset in (0..period as usize).step_by(BLOCK) {
let frame = Frame {
samples: [0.25; BLOCK],
len: BLOCK.min(period as usize - offset),
at: captured + Duration::from_secs_f64(offset as f64 / 48_000.0),
generation: 2,
};
assert!(packer.push(frame, /*rate*/ 48_000.0, &buffers.capture));
}
available -= period;
while let Some(frame) = buffers.capture.pop() {
packets.extend(processor.capture(&frame, || now).unwrap());
}
}
}
assert!(packets.len() >= 90, "capture starved Opus");
// Queue a full output callback without racing the producer.
Buffers::set_disabled(&buffers.speaker, /*disabled*/ false).unwrap();
buffers.serviced.store(true, Ordering::Release);
let port = PlaybackPort::new(buffers.clone(), /*rate*/ 48_000);
let writer = port.writer();
let bytes = 0.25_f32.to_le_bytes().repeat(period as usize);
for chunk in bytes.chunks(BLOCK * 4) {
assert_eq!(writer.write(chunk).unwrap(), chunk.len());
}
let mut output = vec![0.0_f32; period as usize];
render_output(
&mut output,
/*channels*/ 1,
/*rate*/ 48_000.0,
Instant::now(),
&buffers,
&mut OutputState::default(),
);
assert_eq!(output, vec![0.25; period as usize]);
}
#[test]
fn callback_configuration_fits_dynamic_queue_with_following_callbacks() {
for min in [6_016, 8_192] {
+5 -1
View File
@@ -84,7 +84,11 @@ impl PlaybackWriter {
.map_err(|_| "speaker writer failed")?;
let deadline = Instant::now() + Duration::from_millis(/*millis*/ 100);
let buffers = &self.state.buffers;
let limit = (self.state.rate / 25).min((BLOCK * buffers.playback.capacity()) as u32);
// Linux requests 50 ms callbacks; retain two callbacks of audio so a
// callback can fill without racing the producer. Keep the queue cap.
let windows_per_second = if cfg!(target_os = "linux") { 10 } else { 25 };
let limit = (self.state.rate / windows_per_second)
.min((BLOCK * buffers.playback.capacity()) as u32);
loop {
if self.epoch % 2 == 1
|| buffers.speaker.load(Ordering::Acquire) != self.epoch
+12 -4
View File
@@ -35,7 +35,9 @@ fn partial_writes_account_for_samples_until_the_device_consumes_them() {
fn suppression_cancels_a_full_writer_and_old_writers_cannot_resume() {
let (buffers, port, writer) = active(/*rate*/ 8000);
let bytes = vec![0; BLOCK * 4];
writer.write(&bytes).unwrap();
for _ in 0..if cfg!(target_os = "linux") { 3 } else { 1 } {
writer.write(&bytes).unwrap();
}
let waiting = std::thread::spawn(move || writer.write(&bytes));
Buffers::set_disabled(&buffers.speaker, /*disabled*/ true).unwrap();
assert_eq!(waiting.join().unwrap(), Err("speaker writer cancelled"));
@@ -129,11 +131,17 @@ fn invalid_samples_fail_but_stalled_consumption_drops_and_resumes() {
writer.write(&f32::NAN.to_le_bytes()),
Err("invalid speaker sample")
);
writer.write(&vec![0; BLOCK * 4]).unwrap();
let blocks = if cfg!(target_os = "linux") { 3 } else { 1 };
for _ in 0..blocks {
writer.write(&vec![0; BLOCK * 4]).unwrap();
}
assert_eq!(writer.write(&vec![0; BLOCK * 4]), Ok(BLOCK * 4));
assert_eq!(buffers.queued.load(Ordering::Acquire), BLOCK as u32);
assert_eq!(
buffers.queued.load(Ordering::Acquire),
(blocks * BLOCK) as u32
);
let mut playback = Playback::default();
for _ in 0..BLOCK {
for _ in 0..blocks * BLOCK {
assert!(playback.next(&buffers).is_some());
}
assert_eq!(writer.write(&vec![0; BLOCK * 4]), Ok(BLOCK * 4));