feat #14740: two-track capture and recorder
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,327 @@
|
||||
//! WASAPI capture (shared mode, event driven) via the `wasapi` crate.
|
||||
//!
|
||||
//! - Microphone: the default *communications* capture endpoint, or the
|
||||
//! endpoint with the given device id.
|
||||
//! - Output, `Process`: application loopback of the call app's process tree
|
||||
//! (Windows 10 2004+), so only the call's audio is recorded.
|
||||
//! - Output, `DefaultRender` / `Device`: loopback of the default
|
||||
//! communications render endpoint / of the given render endpoint.
|
||||
//!
|
||||
//! All streams are delivered as interleaved `f32`. When the endpoint goes
|
||||
//! away mid-recording (`AUDCLNT_E_DEVICE_INVALIDATED`, e.g. a headset was
|
||||
//! unplugged), the source reopens the default endpoint once, in the original
|
||||
//! format so the recorder's conditioner stays valid. If that fails, it stops
|
||||
//! delivering data and the recorder pads the track with silence.
|
||||
|
||||
use std::sync::atomic::{AtomicBool, Ordering};
|
||||
use std::thread;
|
||||
|
||||
use recorder_core::capture::{
|
||||
CaptureError, CaptureSource, DataCallback, DeviceInfo, Devices, OutputTarget, StreamFormat,
|
||||
};
|
||||
use wasapi::{
|
||||
deinitialize, initialize_mta, AudioCaptureClient, AudioClient, Device, DeviceEnumerator,
|
||||
Direction, Handle, Role, SampleType, StreamMode, WasapiError, WaveFormat,
|
||||
};
|
||||
|
||||
use crate::capture_thread::{CaptureThread, Ready};
|
||||
|
||||
/// `AUDCLNT_E_DEVICE_INVALIDATED`: the endpoint was removed or disabled.
|
||||
const AUDCLNT_E_DEVICE_INVALIDATED: i32 = 0x8889_0004_u32 as i32;
|
||||
/// Format requested for application loopback, which has no mix format.
|
||||
const PROCESS_LOOPBACK_FORMAT: StreamFormat = StreamFormat {
|
||||
rate: 48_000,
|
||||
channels: 2,
|
||||
};
|
||||
/// Shared-mode buffer size (100 ns units): 200 ms of headroom so a slow
|
||||
/// disk write in the recorder does not overflow the capture buffer.
|
||||
const BUFFER_DURATION_HNS: i64 = 2_000_000;
|
||||
/// Event wait timeout (ms). Loopback endpoints signal no events while
|
||||
/// nothing plays, so the loop wakes up regularly to check the stop flag and
|
||||
/// to notice an invalidated device.
|
||||
const WAIT_MS: u32 = 100;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
enum Endpoint {
|
||||
/// Capture endpoint id; `None` for the default communications endpoint.
|
||||
Mic(Option<String>),
|
||||
Output(OutputTarget),
|
||||
}
|
||||
|
||||
impl Endpoint {
|
||||
/// What to reopen after the endpoint was invalidated.
|
||||
fn fallback(&self) -> Endpoint {
|
||||
match self {
|
||||
Endpoint::Mic(_) => Endpoint::Mic(None),
|
||||
Endpoint::Output(OutputTarget::Process { .. }) => self.clone(),
|
||||
Endpoint::Output(_) => Endpoint::Output(OutputTarget::DefaultRender),
|
||||
}
|
||||
}
|
||||
|
||||
fn label(&self) -> &'static str {
|
||||
match self {
|
||||
Endpoint::Mic(_) => "mic",
|
||||
Endpoint::Output(_) => "out",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct WasapiSource {
|
||||
endpoint: Endpoint,
|
||||
thread: Option<CaptureThread>,
|
||||
}
|
||||
|
||||
pub fn mic_source(device: Option<&str>) -> Box<dyn CaptureSource> {
|
||||
Box::new(WasapiSource {
|
||||
endpoint: Endpoint::Mic(device.map(str::to_owned)),
|
||||
thread: None,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn output_source(target: OutputTarget) -> Box<dyn CaptureSource> {
|
||||
Box::new(WasapiSource {
|
||||
endpoint: Endpoint::Output(target),
|
||||
thread: None,
|
||||
})
|
||||
}
|
||||
|
||||
/// Active capture (inputs) and render (outputs) endpoints. Enumerated on a
|
||||
/// fresh thread so COM is initialised the way this code expects,
|
||||
/// independent of the caller's apartment. Errors yield empty lists.
|
||||
pub fn list_devices() -> Devices {
|
||||
thread::spawn(|| {
|
||||
if initialize_mta().is_err() {
|
||||
return Devices::default();
|
||||
}
|
||||
let devices = Devices {
|
||||
inputs: endpoints(&Direction::Capture),
|
||||
outputs: endpoints(&Direction::Render),
|
||||
};
|
||||
deinitialize();
|
||||
devices
|
||||
})
|
||||
.join()
|
||||
.unwrap_or_default()
|
||||
}
|
||||
|
||||
fn endpoints(direction: &Direction) -> Vec<DeviceInfo> {
|
||||
let collection = match DeviceEnumerator::new()
|
||||
.and_then(|enumerator| enumerator.get_device_collection(direction))
|
||||
{
|
||||
Ok(collection) => collection,
|
||||
Err(err) => {
|
||||
log::warn!("cannot enumerate {direction} devices: {err}");
|
||||
return Vec::new();
|
||||
}
|
||||
};
|
||||
(&collection)
|
||||
.into_iter()
|
||||
.filter_map(|device| {
|
||||
let device = device.ok()?;
|
||||
Some(DeviceInfo {
|
||||
id: device.get_id().ok()?,
|
||||
name: device.get_friendlyname().ok()?,
|
||||
})
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
impl CaptureSource for WasapiSource {
|
||||
fn start(&mut self, on_data: DataCallback) -> Result<StreamFormat, CaptureError> {
|
||||
self.stop();
|
||||
let endpoint = self.endpoint.clone();
|
||||
let (thread, format) = CaptureThread::spawn(
|
||||
&format!("wasapi {}", endpoint.label()),
|
||||
move |stop, ready| run(endpoint, stop, ready, on_data),
|
||||
)?;
|
||||
self.thread = Some(thread);
|
||||
Ok(format)
|
||||
}
|
||||
|
||||
fn stop(&mut self) {
|
||||
if let Some(thread) = self.thread.take() {
|
||||
thread.stop();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for WasapiSource {
|
||||
fn drop(&mut self) {
|
||||
self.stop();
|
||||
}
|
||||
}
|
||||
|
||||
/// An opened, started WASAPI capture stream. Lives on the capture thread.
|
||||
struct Stream {
|
||||
client: AudioClient,
|
||||
capture: AudioCaptureClient,
|
||||
event: Handle,
|
||||
format: StreamFormat,
|
||||
}
|
||||
|
||||
impl Drop for Stream {
|
||||
fn drop(&mut self) {
|
||||
let _ = self.client.stop_stream();
|
||||
}
|
||||
}
|
||||
|
||||
fn backend(err: WasapiError) -> CaptureError {
|
||||
CaptureError::Backend(format!("WASAPI: {err}"))
|
||||
}
|
||||
|
||||
fn is_device_invalidated(err: &WasapiError) -> bool {
|
||||
matches!(err, WasapiError::Windows(e) if e.code().0 == AUDCLNT_E_DEVICE_INVALIDATED)
|
||||
}
|
||||
|
||||
fn device_by_id(enumerator: &DeviceEnumerator, id: &str) -> Result<Device, CaptureError> {
|
||||
enumerator
|
||||
.get_device(id)
|
||||
.map_err(|_| CaptureError::DeviceNotFound(id.to_owned()))
|
||||
}
|
||||
|
||||
/// Opens `endpoint` and starts streaming. `format` forces a stream format
|
||||
/// (used when reopening); otherwise the endpoint's mix format is used, with
|
||||
/// the samples converted to `f32` by the audio engine.
|
||||
fn open(endpoint: &Endpoint, format: Option<StreamFormat>) -> Result<Stream, CaptureError> {
|
||||
let mut client = match endpoint {
|
||||
Endpoint::Output(OutputTarget::Process { root_pid }) => {
|
||||
AudioClient::new_application_loopback_client(*root_pid, true).map_err(backend)?
|
||||
}
|
||||
_ => {
|
||||
let enumerator = DeviceEnumerator::new().map_err(backend)?;
|
||||
let device = match endpoint {
|
||||
Endpoint::Mic(Some(id)) | Endpoint::Output(OutputTarget::Device(id)) => {
|
||||
device_by_id(&enumerator, id)?
|
||||
}
|
||||
Endpoint::Mic(None) => enumerator
|
||||
.get_default_device_for_role(&Direction::Capture, &Role::Communications)
|
||||
.map_err(backend)?,
|
||||
Endpoint::Output(_) => enumerator
|
||||
.get_default_device_for_role(&Direction::Render, &Role::Communications)
|
||||
.map_err(backend)?,
|
||||
};
|
||||
device.get_iaudioclient().map_err(backend)?
|
||||
}
|
||||
};
|
||||
|
||||
let format = match (format, endpoint) {
|
||||
(Some(format), _) => format,
|
||||
(None, Endpoint::Output(OutputTarget::Process { .. })) => PROCESS_LOOPBACK_FORMAT,
|
||||
(None, _) => {
|
||||
let mix = client.get_mixformat().map_err(backend)?;
|
||||
StreamFormat {
|
||||
rate: mix.get_samplespersec(),
|
||||
channels: mix.get_nchannels(),
|
||||
}
|
||||
}
|
||||
};
|
||||
let wave_format = WaveFormat::new(
|
||||
32,
|
||||
32,
|
||||
&SampleType::Float,
|
||||
format.rate as usize,
|
||||
format.channels as usize,
|
||||
None,
|
||||
);
|
||||
let mode = StreamMode::EventsShared {
|
||||
autoconvert: true,
|
||||
buffer_duration_hns: BUFFER_DURATION_HNS,
|
||||
};
|
||||
// Capture on a capture endpoint records it; on a render endpoint (and on
|
||||
// the application loopback client) it sets up loopback capture.
|
||||
client
|
||||
.initialize_client(&wave_format, &Direction::Capture, &mode)
|
||||
.map_err(backend)?;
|
||||
let event = client.set_get_eventhandle().map_err(backend)?;
|
||||
let capture = client.get_audiocaptureclient().map_err(backend)?;
|
||||
client.start_stream().map_err(backend)?;
|
||||
Ok(Stream {
|
||||
client,
|
||||
capture,
|
||||
event,
|
||||
format,
|
||||
})
|
||||
}
|
||||
|
||||
fn run(endpoint: Endpoint, stop: &AtomicBool, ready: Ready, on_data: DataCallback) {
|
||||
if let Err(err) = initialize_mta().ok() {
|
||||
let _ = ready.send(Err(CaptureError::Backend(format!(
|
||||
"COM initialisation failed: {err}"
|
||||
))));
|
||||
return;
|
||||
}
|
||||
capture(&endpoint, stop, ready, on_data);
|
||||
// Every COM object is dropped by now.
|
||||
deinitialize();
|
||||
}
|
||||
|
||||
fn capture(endpoint: &Endpoint, stop: &AtomicBool, ready: Ready, mut on_data: DataCallback) {
|
||||
let mut stream = match open(endpoint, None) {
|
||||
Ok(stream) => stream,
|
||||
Err(err) => {
|
||||
let _ = ready.send(Err(err));
|
||||
return;
|
||||
}
|
||||
};
|
||||
let format = stream.format;
|
||||
let _ = ready.send(Ok(format));
|
||||
|
||||
let label = endpoint.label();
|
||||
let mut reopened = false;
|
||||
let mut bytes = Vec::new();
|
||||
let mut samples = Vec::new();
|
||||
while !stop.load(Ordering::SeqCst) {
|
||||
// A timeout is normal (silent loopback); drain regardless, which
|
||||
// also surfaces an invalidated device.
|
||||
let _ = stream.event.wait_for_event(WAIT_MS);
|
||||
let Err(err) = drain(&stream, &mut bytes, &mut samples, &mut on_data) else {
|
||||
continue;
|
||||
};
|
||||
if !is_device_invalidated(&err) || reopened {
|
||||
log::warn!("WASAPI capture '{label}' ended: {err}");
|
||||
return;
|
||||
}
|
||||
reopened = true;
|
||||
log::warn!("WASAPI capture '{label}': device invalidated, reopening the default device");
|
||||
drop(stream);
|
||||
stream = match open(&endpoint.fallback(), Some(format)) {
|
||||
Ok(stream) => stream,
|
||||
Err(err) => {
|
||||
log::warn!("WASAPI capture '{label}': reopening failed: {err}");
|
||||
return;
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/// Delivers every packet currently queued in the capture buffer.
|
||||
fn drain(
|
||||
stream: &Stream,
|
||||
bytes: &mut Vec<u8>,
|
||||
samples: &mut Vec<f32>,
|
||||
on_data: &mut DataCallback,
|
||||
) -> Result<(), WasapiError> {
|
||||
let block_align = stream.format.channels as usize * std::mem::size_of::<f32>();
|
||||
loop {
|
||||
let frames = stream.capture.get_next_packet_size()?.unwrap_or(0) as usize;
|
||||
if frames == 0 {
|
||||
return Ok(());
|
||||
}
|
||||
bytes.resize(frames * block_align, 0);
|
||||
let (read, info) = stream.capture.read_from_device(bytes)?;
|
||||
let len = read as usize * stream.format.channels as usize;
|
||||
samples.clear();
|
||||
if info.flags.silent {
|
||||
samples.resize(len, 0.0);
|
||||
} else {
|
||||
samples.extend(
|
||||
bytes.as_chunks::<4>().0[..len]
|
||||
.iter()
|
||||
.map(|raw| f32::from_le_bytes(*raw)),
|
||||
);
|
||||
}
|
||||
if !samples.is_empty() {
|
||||
on_data(samples);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user