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
+6
View File
@@ -69,4 +69,10 @@ pub trait CaptureSource: Send {
fn start(&mut self, on_data: DataCallback) -> Result<StreamFormat, CaptureError>;
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>;
}
+52 -4
View File
@@ -317,12 +317,16 @@ impl Recorder {
(self.tracks[0].shared.level(), self.tracks[1].shared.level())
}
/// Names ("mic" / "out") of tracks whose source failed to start or whose
/// file could not be written.
/// Names ("mic" / "out") of tracks whose source failed to start, died
/// mid-recording, or whose file could not be written.
pub fn failed_tracks(&self) -> Vec<&'static str> {
self.tracks
.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)
.collect()
}
@@ -377,11 +381,13 @@ mod tests {
/// Test source: a thread that pushes a constant-amplitude 20 ms chunk
/// 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 {
format: StreamFormat,
amplitude: f32,
silent: Option<(Duration, Duration)>,
dies_after: Option<Duration>,
fail: bool,
stop: Arc<AtomicBool>,
thread: Option<JoinHandle<()>>,
@@ -393,6 +399,7 @@ mod tests {
format: StreamFormat { rate, channels },
amplitude,
silent: None,
dies_after: None,
fail: false,
stop: Arc::new(AtomicBool::new(false)),
thread: None,
@@ -404,6 +411,11 @@ mod tests {
self
}
fn dies_after(mut self, after: Duration) -> Self {
self.dies_after = Some(after);
self
}
fn failing() -> Self {
let mut source = Self::new(16_000, 1, 0.0);
source.fail = true;
@@ -420,6 +432,7 @@ mod tests {
let frames = (format.rate as u128 * CHUNK.as_millis() / 1000) as usize;
let chunk = vec![self.amplitude; frames * format.channels as usize];
let silent = self.silent;
let dies_after = self.dies_after;
let stop = self.stop.clone();
self.thread = Some(thread::spawn(move || {
let started = Instant::now();
@@ -430,6 +443,9 @@ mod tests {
thread::sleep(wait);
}
sent += 1;
if dies_after.is_some_and(|after| due > after) {
return;
}
let muted = silent.is_some_and(|(from, to)| due > from && due <= to);
if !muted {
on_data(&chunk);
@@ -445,6 +461,11 @@ mod tests {
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> {
@@ -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]
fn recorder_fails_when_both_sources_fail() {
let dir = tempfile::tempdir().unwrap();