fix #14740: report capture sources that die mid-recording

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
hauke
2026-11-05 14:38:40 +01:00
co-authored by Claude Opus 5.5
parent 8e8a3c3c58
commit ef07ecab1e
5 changed files with 125 additions and 24 deletions
+30 -5
View File
@@ -3,11 +3,13 @@
//! Platform stream handles (COM interfaces, PulseAudio connections) are not
//! `Send`, so they are created, used and dropped on this one thread. The
//! thread reports whether the stream opened through a channel, which lets
//! `CaptureSource::start` return that result synchronously.
//! `CaptureSource::start` return that result synchronously. If the body
//! ends on its own (not because stop was requested), its error is kept for
//! `CaptureSource::failure`.
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::mpsc::{self, Receiver, RecvTimeoutError, SyncSender};
use std::sync::Arc;
use std::sync::{Arc, Mutex};
use std::thread::{self, JoinHandle};
use std::time::Duration;
@@ -22,6 +24,8 @@ pub(crate) type Ready = SyncSender<Result<StreamFormat, CaptureError>>;
pub(crate) struct CaptureThread {
stop: Arc<AtomicBool>,
/// The error the body ended with, if it returned one.
error: Arc<Mutex<Option<String>>>,
/// Disconnects when the thread ends (its sender is dropped with it).
done: Receiver<()>,
handle: JoinHandle<()>,
@@ -30,12 +34,15 @@ pub(crate) struct CaptureThread {
impl CaptureThread {
/// Spawns `body` on a new thread and waits until it has reported the
/// stream format (or an error) through `ready`. `body` must return soon
/// after the stop flag it is given is set.
/// after the stop flag it is given is set; it returns `Err(reason)` when
/// the stream ended on its own.
pub(crate) fn spawn<F>(name: &str, body: F) -> Result<(Self, StreamFormat), CaptureError>
where
F: FnOnce(&AtomicBool, Ready) + Send + 'static,
F: FnOnce(&AtomicBool, Ready) -> Result<(), String> + Send + 'static,
{
let stop = Arc::new(AtomicBool::new(false));
let error = Arc::new(Mutex::new(None));
let thread_error = error.clone();
let (ready_tx, ready_rx) = mpsc::sync_channel(1);
let (done_tx, done_rx) = mpsc::channel::<()>();
let thread_stop = stop.clone();
@@ -43,7 +50,9 @@ impl CaptureThread {
.name(name.to_string())
.spawn(move || {
let _done = done_tx;
body(&thread_stop, ready_tx);
if let Err(reason) = body(&thread_stop, ready_tx) {
*thread_error.lock().unwrap_or_else(|p| p.into_inner()) = Some(reason);
}
})
.map_err(|err| CaptureError::Backend(format!("cannot spawn capture thread: {err}")))?;
@@ -51,6 +60,7 @@ impl CaptureThread {
Ok(Ok(format)) => Ok((
Self {
stop,
error,
done: done_rx,
handle,
},
@@ -69,6 +79,21 @@ impl CaptureThread {
}
}
/// Why the thread ended without being asked to stop (`None` while it
/// runs or after a requested stop). A body that panicked or returned
/// `Ok` early gets a generic reason.
pub(crate) fn failure(&self) -> Option<String> {
if self.stop.load(Ordering::SeqCst) || !self.handle.is_finished() {
return None;
}
let error = self.error.lock().unwrap_or_else(|p| p.into_inner());
Some(
error
.clone()
.unwrap_or_else(|| "capture stopped unexpectedly".into()),
)
}
/// Signals the thread to stop and waits for it to end.
pub(crate) fn stop(self) {
self.stop.store(true, Ordering::SeqCst);
+11 -5
View File
@@ -80,6 +80,10 @@ impl CaptureSource for PulseSource {
thread.stop();
}
}
fn failure(&self) -> Option<String> {
self.thread.as_ref().and_then(CaptureThread::failure)
}
}
impl Drop for PulseSource {
@@ -127,12 +131,13 @@ fn run(
stop: &AtomicBool,
ready: Ready,
mut on_data: DataCallback,
) {
) -> Result<(), String> {
let simple = match open(device, stream_name) {
Ok(simple) => simple,
Err(err) => {
let reason = err.to_string();
let _ = ready.send(Err(err));
return;
return Err(reason);
}
};
let _ = ready.send(Ok(FORMAT));
@@ -141,14 +146,15 @@ fn run(
let mut samples = vec![0f32; CHUNK_SAMPLES];
while !stop.load(Ordering::SeqCst) {
if let Err(err) = simple.read(&mut bytes) {
// Source gone (unplugged, server restart): stop delivering; the
// recorder keeps the track aligned with silence.
// Source gone (unplugged, server restart): stop delivering and
// report it; the recorder keeps the track aligned with silence.
log::warn!("PulseAudio capture '{stream_name}' ended: {err}");
return;
return Err(format!("PulseAudio: {err}"));
}
for (sample, raw) in samples.iter_mut().zip(bytes.as_chunks::<4>().0) {
*sample = f32::from_ne_bytes(*raw);
}
on_data(&samples);
}
Ok(())
}
+26 -10
View File
@@ -143,6 +143,10 @@ impl CaptureSource for WasapiSource {
thread.stop();
}
}
fn failure(&self) -> Option<String> {
self.thread.as_ref().and_then(CaptureThread::failure)
}
}
impl Drop for WasapiSource {
@@ -243,24 +247,35 @@ fn open(endpoint: &Endpoint, format: Option<StreamFormat>) -> Result<Stream, Cap
})
}
fn run(endpoint: Endpoint, stop: &AtomicBool, ready: Ready, on_data: DataCallback) {
fn run(
endpoint: Endpoint,
stop: &AtomicBool,
ready: Ready,
on_data: DataCallback,
) -> Result<(), String> {
if let Err(err) = initialize_mta().ok() {
let _ = ready.send(Err(CaptureError::Backend(format!(
"COM initialisation failed: {err}"
))));
return;
let reason = format!("COM initialisation failed: {err}");
let _ = ready.send(Err(CaptureError::Backend(reason.clone())));
return Err(reason);
}
capture(&endpoint, stop, ready, on_data);
let result = capture(&endpoint, stop, ready, on_data);
// Every COM object is dropped by now.
deinitialize();
result
}
fn capture(endpoint: &Endpoint, stop: &AtomicBool, ready: Ready, mut on_data: DataCallback) {
fn capture(
endpoint: &Endpoint,
stop: &AtomicBool,
ready: Ready,
mut on_data: DataCallback,
) -> Result<(), String> {
let mut stream = match open(endpoint, None) {
Ok(stream) => stream,
Err(err) => {
let reason = err.to_string();
let _ = ready.send(Err(err));
return;
return Err(reason);
}
};
let format = stream.format;
@@ -279,7 +294,7 @@ fn capture(endpoint: &Endpoint, stop: &AtomicBool, ready: Ready, mut on_data: Da
};
if !is_device_invalidated(&err) || reopened {
log::warn!("WASAPI capture '{label}' ended: {err}");
return;
return Err(format!("WASAPI: {err}"));
}
reopened = true;
log::warn!("WASAPI capture '{label}': device invalidated, reopening the default device");
@@ -288,10 +303,11 @@ fn capture(endpoint: &Endpoint, stop: &AtomicBool, ready: Ready, mut on_data: Da
Ok(stream) => stream,
Err(err) => {
log::warn!("WASAPI capture '{label}': reopening failed: {err}");
return;
return Err(format!("device lost, reopening failed: {err}"));
}
};
}
Ok(())
}
/// Delivers every packet currently queued in the capture buffer.