Refactor audio capture error handling and cleanup logic

- Removed console logging from the useSystemAudioCapture hook to streamline the code.
- Introduced error handling in the audio capture state to capture and report errors more effectively.
- Updated the cleanup logic to ensure proper handling of errors during audio capture on unmount.
- Enhanced error messages for better clarity when audio capture fails.
This commit is contained in:
Jamie Pine
2026-01-26 20:12:37 -08:00
parent b59c0f44e5
commit e59f86aa63
4 changed files with 29 additions and 17 deletions
+3
View File
@@ -18,6 +18,7 @@ pub struct AudioCaptureState {
pub sample_rate: Arc<Mutex<u32>>,
pub channels: Arc<Mutex<u16>>,
pub stop_tx: Arc<Mutex<Option<tokio::sync::mpsc::Sender<()>>>>,
pub error: Arc<Mutex<Option<String>>>,
#[cfg(target_os = "macos")]
pub stream: Arc<Mutex<Option<SCStream>>>,
}
@@ -29,6 +30,7 @@ impl AudioCaptureState {
sample_rate: Arc::new(Mutex::new(44100)),
channels: Arc::new(Mutex::new(2)),
stop_tx: Arc::new(Mutex::new(None)),
error: Arc::new(Mutex::new(None)),
#[cfg(target_os = "macos")]
stream: Arc::new(Mutex::new(None)),
}
@@ -36,5 +38,6 @@ impl AudioCaptureState {
pub fn reset(&self) {
*self.samples.lock().unwrap() = Vec::new();
*self.error.lock().unwrap() = None;
}
}
+25 -7
View File
@@ -19,6 +19,7 @@ pub async fn start_capture(
let sample_rate_arc = state.sample_rate.clone();
let channels_arc = state.channels.clone();
let stop_tx = state.stop_tx.clone();
let error_arc = state.error.clone();
// Use AtomicBool for stop signal (works with non-Send types)
let stop_flag = Arc::new(AtomicBool::new(false));
@@ -42,7 +43,9 @@ pub async fn start_capture(
{
Ok(d) => d,
Err(e) => {
eprintln!("Failed to get audio device: {}", e);
let error_msg = format!("Failed to get audio device: {}", e);
eprintln!("{}", error_msg);
*error_arc.lock().unwrap() = Some(error_msg);
return;
}
};
@@ -50,7 +53,9 @@ pub async fn start_capture(
let mut audio_client = match device.get_iaudioclient() {
Ok(client) => client,
Err(e) => {
eprintln!("Failed to get audio client: {}", e);
let error_msg = format!("Failed to get audio client: {}", e);
eprintln!("{}", error_msg);
*error_arc.lock().unwrap() = Some(error_msg);
return;
}
};
@@ -58,7 +63,9 @@ pub async fn start_capture(
let mix_format = match audio_client.get_mixformat() {
Ok(format) => format,
Err(e) => {
eprintln!("Failed to get mix format: {}", e);
let error_msg = format!("Failed to get mix format: {}", e);
eprintln!("{}", error_msg);
*error_arc.lock().unwrap() = Some(error_msg);
return;
}
};
@@ -76,20 +83,26 @@ pub async fn start_capture(
};
if let Err(e) = audio_client.initialize_client(&mix_format, &Direction::Capture, &stream_mode) {
eprintln!("Failed to initialize audio client: {}", e);
let error_msg = format!("Failed to initialize audio client: {}", e);
eprintln!("{}", error_msg);
*error_arc.lock().unwrap() = Some(error_msg);
return;
}
let capture_client = match audio_client.get_audiocaptureclient() {
Ok(client) => client,
Err(e) => {
eprintln!("Failed to get capture client: {}", e);
let error_msg = format!("Failed to get capture client: {}", e);
eprintln!("{}", error_msg);
*error_arc.lock().unwrap() = Some(error_msg);
return;
}
};
if let Err(e) = audio_client.start_stream() {
eprintln!("Failed to start stream: {}", e);
let error_msg = format!("Failed to start stream: {}", e);
eprintln!("{}", error_msg);
*error_arc.lock().unwrap() = Some(error_msg);
return;
}
@@ -176,13 +189,18 @@ pub async fn stop_capture(state: &AudioCaptureState) -> Result<String, String> {
// Wait a bit for capture to stop
tokio::time::sleep(tokio::time::Duration::from_millis(500)).await;
// Check if there was an error during capture
if let Some(error) = state.error.lock().unwrap().as_ref() {
return Err(error.clone());
}
// Get samples
let samples = state.samples.lock().unwrap().clone();
let sample_rate = *state.sample_rate.lock().unwrap();
let channels = *state.channels.lock().unwrap();
if samples.is_empty() {
return Err("No audio samples captured".to_string());
return Err("No audio samples captured. Make sure audio is playing on your system during recording.".to_string());
}
// Convert to WAV