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:
@@ -3,11 +3,13 @@
|
|||||||
//! Platform stream handles (COM interfaces, PulseAudio connections) are not
|
//! Platform stream handles (COM interfaces, PulseAudio connections) are not
|
||||||
//! `Send`, so they are created, used and dropped on this one thread. The
|
//! `Send`, so they are created, used and dropped on this one thread. The
|
||||||
//! thread reports whether the stream opened through a channel, which lets
|
//! 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::atomic::{AtomicBool, Ordering};
|
||||||
use std::sync::mpsc::{self, Receiver, RecvTimeoutError, SyncSender};
|
use std::sync::mpsc::{self, Receiver, RecvTimeoutError, SyncSender};
|
||||||
use std::sync::Arc;
|
use std::sync::{Arc, Mutex};
|
||||||
use std::thread::{self, JoinHandle};
|
use std::thread::{self, JoinHandle};
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
@@ -22,6 +24,8 @@ pub(crate) type Ready = SyncSender<Result<StreamFormat, CaptureError>>;
|
|||||||
|
|
||||||
pub(crate) struct CaptureThread {
|
pub(crate) struct CaptureThread {
|
||||||
stop: Arc<AtomicBool>,
|
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).
|
/// Disconnects when the thread ends (its sender is dropped with it).
|
||||||
done: Receiver<()>,
|
done: Receiver<()>,
|
||||||
handle: JoinHandle<()>,
|
handle: JoinHandle<()>,
|
||||||
@@ -30,12 +34,15 @@ pub(crate) struct CaptureThread {
|
|||||||
impl CaptureThread {
|
impl CaptureThread {
|
||||||
/// Spawns `body` on a new thread and waits until it has reported the
|
/// Spawns `body` on a new thread and waits until it has reported the
|
||||||
/// stream format (or an error) through `ready`. `body` must return soon
|
/// 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>
|
pub(crate) fn spawn<F>(name: &str, body: F) -> Result<(Self, StreamFormat), CaptureError>
|
||||||
where
|
where
|
||||||
F: FnOnce(&AtomicBool, Ready) + Send + 'static,
|
F: FnOnce(&AtomicBool, Ready) -> Result<(), String> + Send + 'static,
|
||||||
{
|
{
|
||||||
let stop = Arc::new(AtomicBool::new(false));
|
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 (ready_tx, ready_rx) = mpsc::sync_channel(1);
|
||||||
let (done_tx, done_rx) = mpsc::channel::<()>();
|
let (done_tx, done_rx) = mpsc::channel::<()>();
|
||||||
let thread_stop = stop.clone();
|
let thread_stop = stop.clone();
|
||||||
@@ -43,7 +50,9 @@ impl CaptureThread {
|
|||||||
.name(name.to_string())
|
.name(name.to_string())
|
||||||
.spawn(move || {
|
.spawn(move || {
|
||||||
let _done = done_tx;
|
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}")))?;
|
.map_err(|err| CaptureError::Backend(format!("cannot spawn capture thread: {err}")))?;
|
||||||
|
|
||||||
@@ -51,6 +60,7 @@ impl CaptureThread {
|
|||||||
Ok(Ok(format)) => Ok((
|
Ok(Ok(format)) => Ok((
|
||||||
Self {
|
Self {
|
||||||
stop,
|
stop,
|
||||||
|
error,
|
||||||
done: done_rx,
|
done: done_rx,
|
||||||
handle,
|
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.
|
/// Signals the thread to stop and waits for it to end.
|
||||||
pub(crate) fn stop(self) {
|
pub(crate) fn stop(self) {
|
||||||
self.stop.store(true, Ordering::SeqCst);
|
self.stop.store(true, Ordering::SeqCst);
|
||||||
|
|||||||
@@ -80,6 +80,10 @@ impl CaptureSource for PulseSource {
|
|||||||
thread.stop();
|
thread.stop();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn failure(&self) -> Option<String> {
|
||||||
|
self.thread.as_ref().and_then(CaptureThread::failure)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Drop for PulseSource {
|
impl Drop for PulseSource {
|
||||||
@@ -127,12 +131,13 @@ fn run(
|
|||||||
stop: &AtomicBool,
|
stop: &AtomicBool,
|
||||||
ready: Ready,
|
ready: Ready,
|
||||||
mut on_data: DataCallback,
|
mut on_data: DataCallback,
|
||||||
) {
|
) -> Result<(), String> {
|
||||||
let simple = match open(device, stream_name) {
|
let simple = match open(device, stream_name) {
|
||||||
Ok(simple) => simple,
|
Ok(simple) => simple,
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
|
let reason = err.to_string();
|
||||||
let _ = ready.send(Err(err));
|
let _ = ready.send(Err(err));
|
||||||
return;
|
return Err(reason);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
let _ = ready.send(Ok(FORMAT));
|
let _ = ready.send(Ok(FORMAT));
|
||||||
@@ -141,14 +146,15 @@ fn run(
|
|||||||
let mut samples = vec![0f32; CHUNK_SAMPLES];
|
let mut samples = vec![0f32; CHUNK_SAMPLES];
|
||||||
while !stop.load(Ordering::SeqCst) {
|
while !stop.load(Ordering::SeqCst) {
|
||||||
if let Err(err) = simple.read(&mut bytes) {
|
if let Err(err) = simple.read(&mut bytes) {
|
||||||
// Source gone (unplugged, server restart): stop delivering; the
|
// Source gone (unplugged, server restart): stop delivering and
|
||||||
// recorder keeps the track aligned with silence.
|
// report it; the recorder keeps the track aligned with silence.
|
||||||
log::warn!("PulseAudio capture '{stream_name}' ended: {err}");
|
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) {
|
for (sample, raw) in samples.iter_mut().zip(bytes.as_chunks::<4>().0) {
|
||||||
*sample = f32::from_ne_bytes(*raw);
|
*sample = f32::from_ne_bytes(*raw);
|
||||||
}
|
}
|
||||||
on_data(&samples);
|
on_data(&samples);
|
||||||
}
|
}
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -143,6 +143,10 @@ impl CaptureSource for WasapiSource {
|
|||||||
thread.stop();
|
thread.stop();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn failure(&self) -> Option<String> {
|
||||||
|
self.thread.as_ref().and_then(CaptureThread::failure)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Drop for WasapiSource {
|
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() {
|
if let Err(err) = initialize_mta().ok() {
|
||||||
let _ = ready.send(Err(CaptureError::Backend(format!(
|
let reason = format!("COM initialisation failed: {err}");
|
||||||
"COM initialisation failed: {err}"
|
let _ = ready.send(Err(CaptureError::Backend(reason.clone())));
|
||||||
))));
|
return Err(reason);
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
capture(&endpoint, stop, ready, on_data);
|
let result = capture(&endpoint, stop, ready, on_data);
|
||||||
// Every COM object is dropped by now.
|
// Every COM object is dropped by now.
|
||||||
deinitialize();
|
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) {
|
let mut stream = match open(endpoint, None) {
|
||||||
Ok(stream) => stream,
|
Ok(stream) => stream,
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
|
let reason = err.to_string();
|
||||||
let _ = ready.send(Err(err));
|
let _ = ready.send(Err(err));
|
||||||
return;
|
return Err(reason);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
let format = stream.format;
|
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 {
|
if !is_device_invalidated(&err) || reopened {
|
||||||
log::warn!("WASAPI capture '{label}' ended: {err}");
|
log::warn!("WASAPI capture '{label}' ended: {err}");
|
||||||
return;
|
return Err(format!("WASAPI: {err}"));
|
||||||
}
|
}
|
||||||
reopened = true;
|
reopened = true;
|
||||||
log::warn!("WASAPI capture '{label}': device invalidated, reopening the default device");
|
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,
|
Ok(stream) => stream,
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
log::warn!("WASAPI capture '{label}': reopening failed: {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.
|
/// Delivers every packet currently queued in the capture buffer.
|
||||||
|
|||||||
@@ -69,4 +69,10 @@ pub trait CaptureSource: Send {
|
|||||||
fn start(&mut self, on_data: DataCallback) -> Result<StreamFormat, CaptureError>;
|
fn start(&mut self, on_data: DataCallback) -> Result<StreamFormat, CaptureError>;
|
||||||
|
|
||||||
fn stop(&mut self);
|
fn stop(&mut self);
|
||||||
|
|
||||||
|
/// Why the stream ended on its own (device lost, read error, reopen
|
||||||
|
/// failed), or `None` while it is healthy, before `start` and after a
|
||||||
|
/// requested `stop`. Switching to another device successfully is not a
|
||||||
|
/// failure.
|
||||||
|
fn failure(&self) -> Option<String>;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -317,12 +317,16 @@ impl Recorder {
|
|||||||
(self.tracks[0].shared.level(), self.tracks[1].shared.level())
|
(self.tracks[0].shared.level(), self.tracks[1].shared.level())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Names ("mic" / "out") of tracks whose source failed to start or whose
|
/// Names ("mic" / "out") of tracks whose source failed to start, died
|
||||||
/// file could not be written.
|
/// mid-recording, or whose file could not be written.
|
||||||
pub fn failed_tracks(&self) -> Vec<&'static str> {
|
pub fn failed_tracks(&self) -> Vec<&'static str> {
|
||||||
self.tracks
|
self.tracks
|
||||||
.iter()
|
.iter()
|
||||||
.filter(|t| t.start_failed || t.shared.write_failed.load(Ordering::Relaxed))
|
.filter(|t| {
|
||||||
|
t.start_failed
|
||||||
|
|| t.source.failure().is_some()
|
||||||
|
|| t.shared.write_failed.load(Ordering::Relaxed)
|
||||||
|
})
|
||||||
.map(|t| t.name)
|
.map(|t| t.name)
|
||||||
.collect()
|
.collect()
|
||||||
}
|
}
|
||||||
@@ -377,11 +381,13 @@ mod tests {
|
|||||||
|
|
||||||
/// Test source: a thread that pushes a constant-amplitude 20 ms chunk
|
/// Test source: a thread that pushes a constant-amplitude 20 ms chunk
|
||||||
/// every 20 ms, except inside the `silent` window (relative to start),
|
/// every 20 ms, except inside the `silent` window (relative to start),
|
||||||
/// where it sends nothing at all.
|
/// where it sends nothing at all. With `dies_after`, the thread ends on
|
||||||
|
/// its own at that time, like a source whose device went away.
|
||||||
struct FakeSource {
|
struct FakeSource {
|
||||||
format: StreamFormat,
|
format: StreamFormat,
|
||||||
amplitude: f32,
|
amplitude: f32,
|
||||||
silent: Option<(Duration, Duration)>,
|
silent: Option<(Duration, Duration)>,
|
||||||
|
dies_after: Option<Duration>,
|
||||||
fail: bool,
|
fail: bool,
|
||||||
stop: Arc<AtomicBool>,
|
stop: Arc<AtomicBool>,
|
||||||
thread: Option<JoinHandle<()>>,
|
thread: Option<JoinHandle<()>>,
|
||||||
@@ -393,6 +399,7 @@ mod tests {
|
|||||||
format: StreamFormat { rate, channels },
|
format: StreamFormat { rate, channels },
|
||||||
amplitude,
|
amplitude,
|
||||||
silent: None,
|
silent: None,
|
||||||
|
dies_after: None,
|
||||||
fail: false,
|
fail: false,
|
||||||
stop: Arc::new(AtomicBool::new(false)),
|
stop: Arc::new(AtomicBool::new(false)),
|
||||||
thread: None,
|
thread: None,
|
||||||
@@ -404,6 +411,11 @@ mod tests {
|
|||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn dies_after(mut self, after: Duration) -> Self {
|
||||||
|
self.dies_after = Some(after);
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
fn failing() -> Self {
|
fn failing() -> Self {
|
||||||
let mut source = Self::new(16_000, 1, 0.0);
|
let mut source = Self::new(16_000, 1, 0.0);
|
||||||
source.fail = true;
|
source.fail = true;
|
||||||
@@ -420,6 +432,7 @@ mod tests {
|
|||||||
let frames = (format.rate as u128 * CHUNK.as_millis() / 1000) as usize;
|
let frames = (format.rate as u128 * CHUNK.as_millis() / 1000) as usize;
|
||||||
let chunk = vec![self.amplitude; frames * format.channels as usize];
|
let chunk = vec![self.amplitude; frames * format.channels as usize];
|
||||||
let silent = self.silent;
|
let silent = self.silent;
|
||||||
|
let dies_after = self.dies_after;
|
||||||
let stop = self.stop.clone();
|
let stop = self.stop.clone();
|
||||||
self.thread = Some(thread::spawn(move || {
|
self.thread = Some(thread::spawn(move || {
|
||||||
let started = Instant::now();
|
let started = Instant::now();
|
||||||
@@ -430,6 +443,9 @@ mod tests {
|
|||||||
thread::sleep(wait);
|
thread::sleep(wait);
|
||||||
}
|
}
|
||||||
sent += 1;
|
sent += 1;
|
||||||
|
if dies_after.is_some_and(|after| due > after) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
let muted = silent.is_some_and(|(from, to)| due > from && due <= to);
|
let muted = silent.is_some_and(|(from, to)| due > from && due <= to);
|
||||||
if !muted {
|
if !muted {
|
||||||
on_data(&chunk);
|
on_data(&chunk);
|
||||||
@@ -445,6 +461,11 @@ mod tests {
|
|||||||
thread.join().unwrap();
|
thread.join().unwrap();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn failure(&self) -> Option<String> {
|
||||||
|
let ended = self.thread.as_ref().is_some_and(JoinHandle::is_finished);
|
||||||
|
(ended && !self.stop.load(Ordering::SeqCst)).then(|| "fake source died".into())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn read_wav(path: &Path) -> Vec<i16> {
|
fn read_wav(path: &Path) -> Vec<i16> {
|
||||||
@@ -588,6 +609,33 @@ mod tests {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn recorder_marks_track_failed_when_source_dies_mid_recording() {
|
||||||
|
let dir = tempfile::tempdir().unwrap();
|
||||||
|
let mic_path = dir.path().join("mic.wav");
|
||||||
|
let out_path = dir.path().join("out.wav");
|
||||||
|
let out = FakeSource::new(16_000, 1, 0.25).dies_after(Duration::from_millis(200));
|
||||||
|
let recorder = Recorder::start(
|
||||||
|
Box::new(FakeSource::new(16_000, 1, 0.25)),
|
||||||
|
Box::new(out),
|
||||||
|
&mic_path,
|
||||||
|
&out_path,
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
thread::sleep(Duration::from_millis(100));
|
||||||
|
assert!(recorder.failed_tracks().is_empty(), "healthy at first");
|
||||||
|
thread::sleep(Duration::from_millis(400));
|
||||||
|
|
||||||
|
assert_eq!(recorder.failed_tracks(), vec!["out"]);
|
||||||
|
recorder.stop().unwrap();
|
||||||
|
|
||||||
|
let mic_samples = read_wav(&mic_path);
|
||||||
|
let out_samples = read_wav(&out_path);
|
||||||
|
assert_eq!(mic_samples.len(), out_samples.len());
|
||||||
|
let tail = &mic_samples[(0.4 * 16_000.0) as usize..];
|
||||||
|
assert!(tail.iter().any(|&s| s != 0), "mic keeps recording");
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn recorder_fails_when_both_sources_fail() {
|
fn recorder_fails_when_both_sources_fail() {
|
||||||
let dir = tempfile::tempdir().unwrap();
|
let dir = tempfile::tempdir().unwrap();
|
||||||
|
|||||||
Reference in New Issue
Block a user