|
| 1 | +use crate::audio::audio_error::AudioError; |
| 2 | +use crate::audio::list_output_devices; |
| 3 | +use rodio::cpal::traits::{DeviceTrait, HostTrait}; |
| 4 | +use std::panic::AssertUnwindSafe; |
| 5 | +use std::sync::mpsc; |
| 6 | +use std::time::Duration; |
| 7 | +use tracing::{debug, warn}; |
| 8 | + |
| 9 | +/// Print device names as JSON to stdout and exit. |
| 10 | +/// Called when the process is spawned in enumeration mode (`MT_ENUMERATE_DEVICES=1`). |
| 11 | +pub fn enumerate_devices_to_stdout() { |
| 12 | + match list_output_devices() { |
| 13 | + Ok(devices) => { |
| 14 | + let json = serde_json::to_string(&devices).unwrap_or_else(|_| "[]".to_string()); |
| 15 | + println!("{json}"); |
| 16 | + std::process::exit(0); |
| 17 | + } |
| 18 | + Err(e) => { |
| 19 | + eprintln!("Device enumeration failed: {e}"); |
| 20 | + std::process::exit(1); |
| 21 | + } |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +/// List output devices via a subprocess for crash isolation. |
| 26 | +/// |
| 27 | +/// Spawns the current executable with `MT_ENUMERATE_DEVICES=1`. The subprocess |
| 28 | +/// enumerates CoreAudio devices, prints JSON to stdout, and exits. If CoreAudio |
| 29 | +/// crashes (SIGSEGV) during enumeration, only the subprocess dies — the parent |
| 30 | +/// process receives an error rather than crashing. |
| 31 | +/// |
| 32 | +/// In test builds, calls `list_output_devices()` directly because the test |
| 33 | +/// binary's harness does not handle `MT_ENUMERATE_DEVICES`, and spawning it |
| 34 | +/// would fork-bomb the test runner. |
| 35 | +pub fn safe_list_output_devices(timeout: Duration) -> Result<Vec<String>, AudioError> { |
| 36 | + #[cfg(test)] |
| 37 | + { |
| 38 | + let _ = timeout; |
| 39 | + return list_output_devices(); |
| 40 | + } |
| 41 | + |
| 42 | + #[cfg(not(test))] |
| 43 | + safe_list_output_devices_subprocess(timeout) |
| 44 | +} |
| 45 | + |
| 46 | +#[cfg(not(test))] |
| 47 | +fn safe_list_output_devices_subprocess(timeout: Duration) -> Result<Vec<String>, AudioError> { |
| 48 | + let exe = std::env::current_exe() |
| 49 | + .map_err(|e| AudioError::Device(format!("Failed to get executable path: {e}")))?; |
| 50 | + |
| 51 | + let mut child = std::process::Command::new(exe) |
| 52 | + .env("MT_ENUMERATE_DEVICES", "1") |
| 53 | + .stdout(std::process::Stdio::piped()) |
| 54 | + .stderr(std::process::Stdio::piped()) |
| 55 | + .spawn() |
| 56 | + .map_err(|e| AudioError::Device(format!("Failed to spawn device enumerator: {e}")))?; |
| 57 | + |
| 58 | + let stdout = child |
| 59 | + .stdout |
| 60 | + .take() |
| 61 | + .ok_or_else(|| AudioError::Device("Failed to capture subprocess stdout".into()))?; |
| 62 | + let stderr = child |
| 63 | + .stderr |
| 64 | + .take() |
| 65 | + .ok_or_else(|| AudioError::Device("Failed to capture subprocess stderr".into()))?; |
| 66 | + |
| 67 | + let stdout_thread = std::thread::spawn(move || { |
| 68 | + use std::io::Read; |
| 69 | + let mut buf = String::new(); |
| 70 | + std::io::BufReader::new(stdout) |
| 71 | + .read_to_string(&mut buf) |
| 72 | + .ok(); |
| 73 | + buf |
| 74 | + }); |
| 75 | + let stderr_thread = std::thread::spawn(move || { |
| 76 | + use std::io::Read; |
| 77 | + let mut buf = String::new(); |
| 78 | + std::io::BufReader::new(stderr) |
| 79 | + .read_to_string(&mut buf) |
| 80 | + .ok(); |
| 81 | + buf |
| 82 | + }); |
| 83 | + |
| 84 | + let start = std::time::Instant::now(); |
| 85 | + loop { |
| 86 | + match child.try_wait() { |
| 87 | + Ok(Some(status)) => { |
| 88 | + let stdout_text = stdout_thread.join().unwrap_or_default(); |
| 89 | + let stderr_text = stderr_thread.join().unwrap_or_default(); |
| 90 | + |
| 91 | + if status.success() { |
| 92 | + let devices: Vec<String> = serde_json::from_str(&stdout_text).map_err(|e| { |
| 93 | + AudioError::Device(format!("Failed to parse device list: {e}")) |
| 94 | + })?; |
| 95 | + debug!(count = devices.len(), "Enumerated devices via subprocess"); |
| 96 | + return Ok(devices); |
| 97 | + } else { |
| 98 | + warn!( |
| 99 | + exit_code = ?status.code(), |
| 100 | + stderr = %stderr_text.trim(), |
| 101 | + "Device enumerator subprocess failed" |
| 102 | + ); |
| 103 | + return Err(AudioError::Device(format!( |
| 104 | + "Device enumeration subprocess failed: {}", |
| 105 | + stderr_text.trim() |
| 106 | + ))); |
| 107 | + } |
| 108 | + } |
| 109 | + Ok(None) => { |
| 110 | + if start.elapsed() > timeout { |
| 111 | + let _ = child.kill(); |
| 112 | + let _ = child.wait(); |
| 113 | + return Err(AudioError::Device("Device enumeration timed out".into())); |
| 114 | + } |
| 115 | + std::thread::sleep(Duration::from_millis(50)); |
| 116 | + } |
| 117 | + Err(e) => { |
| 118 | + return Err(AudioError::Device(format!( |
| 119 | + "Failed to wait for enumerator: {e}" |
| 120 | + ))); |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +/// Resolve a named or default output device on a disposable thread. |
| 127 | +/// |
| 128 | +/// CoreAudio device enumeration (`output_devices()`, `default_output_device()`) |
| 129 | +/// runs on a short-lived thread, not the caller's thread. The resolved |
| 130 | +/// `cpal::Device` (which is `Send`) is returned to the caller. |
| 131 | +/// |
| 132 | +/// Panics on the disposable thread are caught; SIGSEGV still kills the process |
| 133 | +/// (use `safe_list_output_devices` for full crash isolation when only names are |
| 134 | +/// needed). |
| 135 | +pub fn resolve_device( |
| 136 | + name: Option<&str>, |
| 137 | + timeout: Duration, |
| 138 | +) -> Result<rodio::cpal::Device, AudioError> { |
| 139 | + let name_owned = name.map(|s| s.to_string()); |
| 140 | + let (tx, rx) = mpsc::channel(); |
| 141 | + |
| 142 | + std::thread::spawn(move || { |
| 143 | + let result = std::panic::catch_unwind(AssertUnwindSafe(|| { |
| 144 | + let host = rodio::cpal::default_host(); |
| 145 | + match name_owned { |
| 146 | + Some(device_name) => { |
| 147 | + let devices = host.output_devices().map_err(|e| { |
| 148 | + AudioError::Device(format!("Failed to enumerate devices: {e}")) |
| 149 | + })?; |
| 150 | + devices |
| 151 | + .into_iter() |
| 152 | + .find(|d| d.name().ok().as_deref() == Some(device_name.as_str())) |
| 153 | + .ok_or_else(|| { |
| 154 | + AudioError::Device(format!("Device not found: {device_name}")) |
| 155 | + }) |
| 156 | + } |
| 157 | + None => host |
| 158 | + .default_output_device() |
| 159 | + .ok_or_else(|| AudioError::Device("No default output device found".into())), |
| 160 | + } |
| 161 | + })); |
| 162 | + |
| 163 | + let result = match result { |
| 164 | + Ok(inner) => inner, |
| 165 | + Err(_) => Err(AudioError::Device("Device resolution panicked".into())), |
| 166 | + }; |
| 167 | + let _ = tx.send(result); |
| 168 | + }); |
| 169 | + |
| 170 | + rx.recv_timeout(timeout).map_err(|e| match e { |
| 171 | + mpsc::RecvTimeoutError::Timeout => AudioError::Device("Device resolution timed out".into()), |
| 172 | + mpsc::RecvTimeoutError::Disconnected => { |
| 173 | + AudioError::Device("Device resolution thread terminated unexpectedly".into()) |
| 174 | + } |
| 175 | + })? |
| 176 | +} |
| 177 | + |
| 178 | +#[cfg(test)] |
| 179 | +mod tests { |
| 180 | + use super::*; |
| 181 | + |
| 182 | + #[test] |
| 183 | + fn test_resolve_device_default() { |
| 184 | + let result = resolve_device(None, Duration::from_secs(5)); |
| 185 | + match result { |
| 186 | + Ok(device) => { |
| 187 | + assert!(device.name().is_ok(), "Resolved device should have a name"); |
| 188 | + } |
| 189 | + Err(_) => { |
| 190 | + // Acceptable on headless CI without audio hardware |
| 191 | + } |
| 192 | + } |
| 193 | + } |
| 194 | + |
| 195 | + #[test] |
| 196 | + fn test_resolve_device_nonexistent_returns_error() { |
| 197 | + let result = resolve_device(Some("__nonexistent_device_12345__"), Duration::from_secs(5)); |
| 198 | + // Should be Err(Device not found) or Err(enumerate failed) on headless CI |
| 199 | + match result { |
| 200 | + Ok(_) => panic!("Should not find a nonexistent device"), |
| 201 | + Err(e) => { |
| 202 | + let msg = e.to_string(); |
| 203 | + assert!( |
| 204 | + msg.contains("not found") |
| 205 | + || msg.contains("enumerate") |
| 206 | + || msg.contains("Device"), |
| 207 | + "Unexpected error: {msg}" |
| 208 | + ); |
| 209 | + } |
| 210 | + } |
| 211 | + } |
| 212 | + |
| 213 | + #[test] |
| 214 | + fn test_resolve_device_timeout() { |
| 215 | + // A very short timeout should still work for a real device resolution |
| 216 | + // (or fail gracefully) |
| 217 | + let result = resolve_device(None, Duration::from_millis(1)); |
| 218 | + // Either succeeds quickly or times out — both are acceptable |
| 219 | + match result { |
| 220 | + Ok(device) => assert!(device.name().is_ok()), |
| 221 | + Err(e) => { |
| 222 | + let msg = e.to_string(); |
| 223 | + assert!( |
| 224 | + msg.contains("timed out") |
| 225 | + || msg.contains("Device") |
| 226 | + || msg.contains("terminated"), |
| 227 | + "Unexpected error: {msg}" |
| 228 | + ); |
| 229 | + } |
| 230 | + } |
| 231 | + } |
| 232 | + |
| 233 | + #[test] |
| 234 | + fn test_safe_list_output_devices_roundtrip() { |
| 235 | + let result = safe_list_output_devices(Duration::from_secs(15)); |
| 236 | + match result { |
| 237 | + Ok(devices) => { |
| 238 | + for name in &devices { |
| 239 | + assert!(!name.is_empty(), "Device name should not be empty"); |
| 240 | + } |
| 241 | + } |
| 242 | + Err(e) => { |
| 243 | + // May fail on CI without audio or if subprocess launch fails |
| 244 | + let msg = e.to_string(); |
| 245 | + assert!( |
| 246 | + msg.contains("Device") |
| 247 | + || msg.contains("failed") |
| 248 | + || msg.contains("timed out") |
| 249 | + || msg.contains("subprocess"), |
| 250 | + "Unexpected error: {msg}" |
| 251 | + ); |
| 252 | + } |
| 253 | + } |
| 254 | + } |
| 255 | +} |
0 commit comments