Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
73 lines
2.6 KiB
Rust
73 lines
2.6 KiB
Rust
//! Platform-neutral capture interface.
|
|
//!
|
|
//! The platform crate (`recorder-audio`) implements [`CaptureSource`] for the
|
|
//! microphone and for the call app's audio output (WASAPI on Windows,
|
|
//! PulseAudio on Linux). The [`Recorder`](crate::recorder::Recorder) only sees
|
|
//! this trait, which keeps it testable with fake sources.
|
|
|
|
use thiserror::Error;
|
|
|
|
/// Format of the interleaved `f32` samples a [`CaptureSource`] delivers.
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
pub struct StreamFormat {
|
|
pub rate: u32,
|
|
pub channels: u16,
|
|
}
|
|
|
|
/// What the output ("out") track records.
|
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
pub enum OutputTarget {
|
|
/// Only the audio rendered by this process and its children (Windows
|
|
/// application loopback). Falls back to [`OutputTarget::DefaultRender`]
|
|
/// where per-process capture is not available.
|
|
Process { root_pid: u32 },
|
|
/// Everything played on the default (communications) output device.
|
|
DefaultRender,
|
|
/// Everything played on the output device with this id.
|
|
Device(String),
|
|
}
|
|
|
|
/// One audio endpoint as shown in the device picker.
|
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
pub struct DeviceInfo {
|
|
pub id: String,
|
|
pub name: String,
|
|
}
|
|
|
|
/// Available capture inputs (microphones) and outputs (render endpoints).
|
|
#[derive(Debug, Clone, Default, PartialEq, Eq)]
|
|
pub struct Devices {
|
|
pub inputs: Vec<DeviceInfo>,
|
|
pub outputs: Vec<DeviceInfo>,
|
|
}
|
|
|
|
#[derive(Debug, Error)]
|
|
pub enum CaptureError {
|
|
#[error("audio device not found: {0}")]
|
|
DeviceNotFound(String),
|
|
#[error("unsupported: {0}")]
|
|
Unsupported(String),
|
|
#[error("audio backend error: {0}")]
|
|
Backend(String),
|
|
}
|
|
|
|
/// Receives interleaved `f32` samples from a running [`CaptureSource`].
|
|
pub type DataCallback = Box<dyn FnMut(&[f32]) + Send>;
|
|
|
|
/// A live audio stream (microphone or output loopback).
|
|
///
|
|
/// Implementations run the platform stream on their own thread, so the
|
|
/// non-`Send` platform handles never leave it. [`CaptureSource::start`]
|
|
/// returns only once the stream is actually open (or failed to open);
|
|
/// afterwards `on_data` is called from the capture thread with interleaved
|
|
/// `f32` samples in the returned [`StreamFormat`]. [`CaptureSource::stop`]
|
|
/// blocks until the capture thread has ended and `on_data` will not be called
|
|
/// again (a thread stuck in a platform call may be detached after a timeout
|
|
/// instead, so callers must tolerate a stray late call); calling it more than
|
|
/// once is harmless.
|
|
pub trait CaptureSource: Send {
|
|
fn start(&mut self, on_data: DataCallback) -> Result<StreamFormat, CaptureError>;
|
|
|
|
fn stop(&mut self);
|
|
}
|