Tests never open a playback device
Browse files at this commit429bf040acb35ce4bf9f7471f4a9332ae41cd6a8Parent: 0775641- fix
- tests never open a playback device
4 changed files +252 -28
- crates/app/src/tests/mod.rs +31 -1
- crates/engine-audio/src/lib.rs +3 -0
- crates/engine/src/ao.rs +209 -27
- crates/engine/tests/ao_output.rs +9 -0
crates/app/src/tests/mod.rsmodified+31 -1
| @@ -65,7 +65,7 @@ use std::path::{Path, PathBuf}; | |||
| 65 | 65 | ||
| 66 | use std::time::{Duration, Instant}; | 66 | use std::time::{Duration, Instant}; |
| 67 | 67 | ||
| 68 | -use std::sync::{LazyLock, Mutex, MutexGuard}; | 68 | +use std::sync::{LazyLock, Mutex, MutexGuard, Once}; |
| 69 | 69 | ||
| 70 | static CONFIG_ENV_LOCK: LazyLock<Mutex<()>> = LazyLock::new(|| Mutex::new(())); | 70 | static CONFIG_ENV_LOCK: LazyLock<Mutex<()>> = LazyLock::new(|| Mutex::new(())); |
| 71 | 71 | ||
| @@ -75,6 +75,19 @@ fn config_env_lock() -> MutexGuard<'static, ()> { | |||
| 75 | .expect("config environment lock poisoned") | 75 | .expect("config environment lock poisoned") |
| 76 | } | 76 | } |
| 77 | 77 | ||
| 78 | +/// Tests never open PipeWire. The output seam also refuses a cargo-test | ||
| 79 | +/// binary under `target/*/deps/`; this pin is the documented door and the | ||
| 80 | +/// source-scan guard below. | ||
| 81 | +fn pin_silent_output() { | ||
| 82 | + static PIN: Once = Once::new(); | ||
| 83 | + PIN.call_once(|| { | ||
| 84 | + // SAFETY: this process is the test binary; no other thread has | ||
| 85 | + // opened a device yet because `asset()` (every session fixture) | ||
| 86 | + // calls this first. | ||
| 87 | + unsafe { std::env::set_var("VE_NO_AO", "1") }; | ||
| 88 | + }); | ||
| 89 | +} | ||
| 90 | + | ||
| 78 | /// Every line of the window's source there is, as one string. The regions have | 91 | /// Every line of the window's source there is, as one string. The regions have |
| 79 | /// moved house twice -- one `main.rs`, then `ui/`, then the tests out of it -- | 92 | /// moved house twice -- one `main.rs`, then `ui/`, then the tests out of it -- |
| 80 | /// and a scan that names its files passes a rule by simply not looking where | 93 | /// and a scan that names its files passes a rule by simply not looking where |
| @@ -157,6 +170,7 @@ fn source_files() -> Vec<PathBuf> { | |||
| 157 | } | 170 | } |
| 158 | 171 | ||
| 159 | fn asset(name: &str) -> PathBuf { | 172 | fn asset(name: &str) -> PathBuf { |
| 173 | + pin_silent_output(); | ||
| 160 | Path::new(env!("CARGO_MANIFEST_DIR")) | 174 | Path::new(env!("CARGO_MANIFEST_DIR")) |
| 161 | .join("../../assets") | 175 | .join("../../assets") |
| 162 | .join(name) | 176 | .join(name) |
| @@ -247,3 +261,19 @@ fn sub(path: &str, track: Option<u64>, label: &str) -> engine::subtitle::Subtitl | |||
| 247 | refused: None, | 261 | refused: None, |
| 248 | } | 262 | } |
| 249 | } | 263 | } |
| 264 | + | ||
| 265 | +#[test] | ||
| 266 | +fn the_suite_pins_a_silent_output() { | ||
| 267 | + pin_silent_output(); | ||
| 268 | + assert!( | ||
| 269 | + src_text("tests/mod.rs").contains("VE_NO_AO"), | ||
| 270 | + "the fixture no longer pins silent output" | ||
| 271 | + ); | ||
| 272 | + let session = PlaybackSession::open(asset("test_av.mp4")).expect("open"); | ||
| 273 | + let _ = session.set_gain(0.0); | ||
| 274 | + assert_eq!( | ||
| 275 | + engine::ao::AoSession::devices_opened(), | ||
| 276 | + 0, | ||
| 277 | + "a test opened the speakers" | ||
| 278 | + ); | ||
| 279 | +} | ||
crates/engine-audio/src/lib.rsmodified+3 -0
| @@ -426,6 +426,9 @@ impl Drop for Session { | |||
| 426 | #[unsafe(no_mangle)] | 426 | #[unsafe(no_mangle)] |
| 427 | pub extern "C" fn ao_open(sample_rate: u32, channels: u32) -> *mut c_void { | 427 | pub extern "C" fn ao_open(sample_rate: u32, channels: u32) -> *mut c_void { |
| 428 | catch_unwind(AssertUnwindSafe(|| { | 428 | catch_unwind(AssertUnwindSafe(|| { |
| 429 | + if std::env::var_os("VE_NO_AO").is_some_and(|v| v == "1") { | ||
| 430 | + return std::ptr::null_mut(); | ||
| 431 | + } | ||
| 429 | if !(8_000..=768_000).contains(&sample_rate) | 432 | if !(8_000..=768_000).contains(&sample_rate) |
| 430 | || channels == 0 | 433 | || channels == 0 |
| 431 | || channels as usize > spa::param::audio::MAX_CHANNELS | 434 | || channels as usize > spa::param::audio::MAX_CHANNELS |
crates/engine/src/ao.rsmodified+209 -27
| @@ -4,14 +4,43 @@ | |||
| 4 | //! only coupling is this C ABI plus a `dlopen`. Anything that goes wrong -- | 4 | //! only coupling is this C ABI plus a `dlopen`. Anything that goes wrong -- |
| 5 | //! plugin missing, no PipeWire daemon, an unusable format -- leaves us with | 5 | //! plugin missing, no PipeWire daemon, an unusable format -- leaves us with |
| 6 | //! `None` and the caller plays silently. | 6 | //! `None` and the caller plays silently. |
| 7 | +//! | ||
| 8 | +//! `VE_NO_AO=1` (and cargo-test binaries under `target/*/deps/`) open a silent | ||
| 9 | +//! sink instead: the session, the clock and the feeder stay, PipeWire is never | ||
| 10 | +//! touched. `VE_AO=1` forces the real device, which is what the ignored | ||
| 11 | +//! `ao_output` suite needs. | ||
| 7 | 12 | ||
| 8 | use std::ffi::c_void; | 13 | use std::ffi::c_void; |
| 9 | use std::path::Path; | 14 | use std::path::Path; |
| 10 | -use std::sync::OnceLock; | 15 | +use std::sync::atomic::{AtomicI64, AtomicU64, Ordering}; |
| 16 | +use std::sync::{LazyLock, OnceLock}; | ||
| 17 | +use std::time::Instant; | ||
| 11 | 18 | ||
| 12 | use libloading::Library; | 19 | use libloading::Library; |
| 13 | 20 | ||
| 14 | const LIB_NAME: &str = "libengine_audio.so"; | 21 | const LIB_NAME: &str = "libengine_audio.so"; |
| 22 | +static DEVICES_OPENED: AtomicU64 = AtomicU64::new(0); | ||
| 23 | + | ||
| 24 | +fn env_is(name: &str, value: &str) -> bool { | ||
| 25 | + std::env::var_os(name).is_some_and(|v| v == value) | ||
| 26 | +} | ||
| 27 | + | ||
| 28 | +/// A cargo-test binary lives in `target/<profile>/deps/<name>-<hash>`. The | ||
| 29 | +/// editor itself is `target/<profile>/edith`, which is what the harness | ||
| 30 | +/// launches, so this never mutes a real session. | ||
| 31 | +fn cargo_test_binary() -> bool { | ||
| 32 | + std::env::current_exe() | ||
| 33 | + .ok() | ||
| 34 | + .and_then(|p| p.parent().map(|d| d.ends_with("deps"))) | ||
| 35 | + .unwrap_or(false) | ||
| 36 | +} | ||
| 37 | + | ||
| 38 | +fn silent_output() -> bool { | ||
| 39 | + if env_is("VE_AO", "1") { | ||
| 40 | + return false; | ||
| 41 | + } | ||
| 42 | + env_is("VE_NO_AO", "1") || cargo_test_binary() | ||
| 43 | +} | ||
| 15 | 44 | ||
| 16 | struct Plugin { | 45 | struct Plugin { |
| 17 | open: unsafe extern "C" fn(u32, u32) -> *mut c_void, | 46 | open: unsafe extern "C" fn(u32, u32) -> *mut c_void, |
| @@ -88,21 +117,114 @@ fn load() -> Option<Plugin> { | |||
| 88 | 117 | ||
| 89 | /// An open playback stream. Dropping it stops playback and closes the session. | 118 | /// An open playback stream. Dropping it stops playback and closes the session. |
| 90 | pub struct AoSession { | 119 | pub struct AoSession { |
| 91 | - plugin: &'static Plugin, | 120 | + backend: Backend, |
| 92 | - handle: *mut c_void, | 121 | +} |
| 122 | + | ||
| 123 | +enum Backend { | ||
| 124 | + Plugin { | ||
| 125 | + plugin: &'static Plugin, | ||
| 126 | + handle: *mut c_void, | ||
| 127 | + }, | ||
| 128 | + /// Wall-clock paced, samples discarded. Same contract as the plugin | ||
| 129 | + /// (paused until `set_active`, position is samples per channel) without | ||
| 130 | + /// a sink-input on the user's speakers. | ||
| 131 | + Silent(Silent), | ||
| 132 | +} | ||
| 133 | + | ||
| 134 | +struct Silent { | ||
| 135 | + rate: f64, | ||
| 136 | + /// Samples already accounted for; `-1` until the stream has run. | ||
| 137 | + base: AtomicI64, | ||
| 138 | + /// 0 = paused; otherwise milliseconds from [`epoch`] at the last resume. | ||
| 139 | + origin_ms: AtomicU64, | ||
| 140 | +} | ||
| 141 | + | ||
| 142 | +fn epoch() -> Instant { | ||
| 143 | + static EPOCH: LazyLock<Instant> = LazyLock::new(Instant::now); | ||
| 144 | + *EPOCH | ||
| 145 | +} | ||
| 146 | + | ||
| 147 | +fn now_ms() -> u64 { | ||
| 148 | + epoch().elapsed().as_millis() as u64 | ||
| 149 | +} | ||
| 150 | + | ||
| 151 | +impl Silent { | ||
| 152 | + fn new(sample_rate: u32) -> Self { | ||
| 153 | + Self { | ||
| 154 | + rate: f64::from(sample_rate), | ||
| 155 | + base: AtomicI64::new(-1), | ||
| 156 | + origin_ms: AtomicU64::new(0), | ||
| 157 | + } | ||
| 158 | + } | ||
| 159 | + | ||
| 160 | + fn extra(&self) -> i64 { | ||
| 161 | + match self.origin_ms.load(Ordering::Relaxed) { | ||
| 162 | + 0 => 0, | ||
| 163 | + origin => { | ||
| 164 | + let ms = now_ms().saturating_sub(origin) as f64; | ||
| 165 | + (ms / 1000.0 * self.rate).round() as i64 | ||
| 166 | + } | ||
| 167 | + } | ||
| 168 | + } | ||
| 169 | + | ||
| 170 | + fn position(&self) -> Option<i64> { | ||
| 171 | + let base = self.base.load(Ordering::Relaxed); | ||
| 172 | + let extra = self.extra(); | ||
| 173 | + if base < 0 && self.origin_ms.load(Ordering::Relaxed) == 0 { | ||
| 174 | + None | ||
| 175 | + } else { | ||
| 176 | + Some(base.max(0) + extra.max(0)) | ||
| 177 | + } | ||
| 178 | + } | ||
| 179 | + | ||
| 180 | + fn set_active(&self, active: bool) { | ||
| 181 | + if active { | ||
| 182 | + if self.origin_ms.load(Ordering::Relaxed) == 0 { | ||
| 183 | + self.base | ||
| 184 | + .compare_exchange(-1, 0, Ordering::Relaxed, Ordering::Relaxed) | ||
| 185 | + .ok(); | ||
| 186 | + self.origin_ms.store(now_ms().max(1), Ordering::Relaxed); | ||
| 187 | + } | ||
| 188 | + } else { | ||
| 189 | + let origin = self.origin_ms.swap(0, Ordering::Relaxed); | ||
| 190 | + if origin != 0 { | ||
| 191 | + let ms = now_ms().saturating_sub(origin) as f64; | ||
| 192 | + let extra = (ms / 1000.0 * self.rate).round() as i64; | ||
| 193 | + self.base | ||
| 194 | + .compare_exchange(-1, 0, Ordering::Relaxed, Ordering::Relaxed) | ||
| 195 | + .ok(); | ||
| 196 | + self.base.fetch_add(extra.max(0), Ordering::Relaxed); | ||
| 197 | + } | ||
| 198 | + } | ||
| 199 | + } | ||
| 93 | } | 200 | } |
| 94 | 201 | ||
| 95 | impl AoSession { | 202 | impl AoSession { |
| 96 | /// Connects a playback stream for interleaved f32 at `sample_rate`. | 203 | /// Connects a playback stream for interleaved f32 at `sample_rate`. |
| 97 | /// `None` covers everything: no plugin, no daemon, unusable format. | 204 | /// `None` covers everything: no plugin, no daemon, unusable format. |
| 205 | + /// A silent pin still returns `Some`: the timeline keeps an audio clock. | ||
| 98 | pub fn open(sample_rate: u32, channels: u32) -> Option<Self> { | 206 | pub fn open(sample_rate: u32, channels: u32) -> Option<Self> { |
| 207 | + if silent_output() { | ||
| 208 | + return Some(Self { | ||
| 209 | + backend: Backend::Silent(Silent::new(sample_rate)), | ||
| 210 | + }); | ||
| 211 | + } | ||
| 99 | let plugin = plugin()?; | 212 | let plugin = plugin()?; |
| 100 | // SAFETY: plain scalars in, a session pointer or null out. | 213 | // SAFETY: plain scalars in, a session pointer or null out. |
| 101 | let handle = unsafe { (plugin.open)(sample_rate, channels) }; | 214 | let handle = unsafe { (plugin.open)(sample_rate, channels) }; |
| 102 | if handle.is_null() { | 215 | if handle.is_null() { |
| 103 | return None; | 216 | return None; |
| 104 | } | 217 | } |
| 105 | - Some(Self { plugin, handle }) | 218 | + DEVICES_OPENED.fetch_add(1, Ordering::Relaxed); |
| 219 | + Some(Self { | ||
| 220 | + backend: Backend::Plugin { plugin, handle }, | ||
| 221 | + }) | ||
| 222 | + } | ||
| 223 | + | ||
| 224 | + /// How many times this process has opened a real PipeWire stream. The | ||
| 225 | + /// silent sink does not count: that is the whole point of the pin. | ||
| 226 | + pub fn devices_opened() -> u64 { | ||
| 227 | + DEVICES_OPENED.load(Ordering::Relaxed) | ||
| 106 | } | 228 | } |
| 107 | 229 | ||
| 108 | /// Whether the plugin itself is loadable. Says nothing about the daemon -- | 230 | /// Whether the plugin itself is loadable. Says nothing about the daemon -- |
| @@ -116,20 +238,30 @@ impl AoSession { | |||
| 116 | /// count means the ring is full and the caller should come back later. | 238 | /// count means the ring is full and the caller should come back later. |
| 117 | /// `None` once the output has died (daemon gone). | 239 | /// `None` once the output has died (daemon gone). |
| 118 | pub fn write(&mut self, samples: &[f32]) -> Option<usize> { | 240 | pub fn write(&mut self, samples: &[f32]) -> Option<usize> { |
| 119 | - // SAFETY: `handle` is live and `samples` is a valid slice for the call. | 241 | + match &mut self.backend { |
| 120 | - match unsafe { (self.plugin.write)(self.handle, samples.as_ptr(), samples.len()) } { | 242 | + Backend::Silent(_) => Some(samples.len()), |
| 121 | - n if n < 0 => None, | 243 | + Backend::Plugin { plugin, handle } => { |
| 122 | - n => Some(n as usize), | 244 | + // SAFETY: `handle` is live and `samples` is a valid slice for the call. |
| 245 | + match unsafe { (plugin.write)(*handle, samples.as_ptr(), samples.len()) } { | ||
| 246 | + n if n < 0 => None, | ||
| 247 | + n => Some(n as usize), | ||
| 248 | + } | ||
| 249 | + } | ||
| 123 | } | 250 | } |
| 124 | } | 251 | } |
| 125 | 252 | ||
| 126 | /// Samples per channel actually played at the device -- the master clock. | 253 | /// Samples per channel actually played at the device -- the master clock. |
| 127 | /// `None` until the stream has run its first cycle. | 254 | /// `None` until the stream has run its first cycle. |
| 128 | pub fn position(&self) -> Option<i64> { | 255 | pub fn position(&self) -> Option<i64> { |
| 129 | - // SAFETY: `handle` came from `ao_open` and is still open. | 256 | + match &self.backend { |
| 130 | - match unsafe { (self.plugin.position)(self.handle) } { | 257 | + Backend::Silent(s) => s.position(), |
| 131 | - n if n < 0 => None, | 258 | + Backend::Plugin { plugin, handle } => { |
| 132 | - n => Some(n), | 259 | + // SAFETY: `handle` came from `ao_open` and is still open. |
| 260 | + match unsafe { (plugin.position)(*handle) } { | ||
| 261 | + n if n < 0 => None, | ||
| 262 | + n => Some(n), | ||
| 263 | + } | ||
| 264 | + } | ||
| 133 | } | 265 | } |
| 134 | } | 266 | } |
| 135 | 267 | ||
| @@ -137,16 +269,26 @@ impl AoSession { | |||
| 137 | /// as it is, and the silence after it is not counted against a decoder that | 269 | /// as it is, and the silence after it is not counted against a decoder that |
| 138 | /// has already finished. Nothing at all on a plugin without the symbol. | 270 | /// has already finished. Nothing at all on a plugin without the symbol. |
| 139 | pub fn stream_ended(&self) { | 271 | pub fn stream_ended(&self) { |
| 140 | - if let Some(ended) = self.plugin.stream_ended { | 272 | + if let Backend::Plugin { plugin, handle } = &self.backend { |
| 141 | - // SAFETY: `handle` came from `ao_open` and is still open. | 273 | + if let Some(ended) = plugin.stream_ended { |
| 142 | - unsafe { ended(self.handle) }; | 274 | + // SAFETY: `handle` came from `ao_open` and is still open. |
| 275 | + unsafe { ended(*handle) }; | ||
| 276 | + } | ||
| 143 | } | 277 | } |
| 144 | } | 278 | } |
| 145 | 279 | ||
| 146 | /// Pauses or resumes playback; while paused the position stays put. | 280 | /// Pauses or resumes playback; while paused the position stays put. |
| 147 | pub fn set_active(&self, active: bool) -> bool { | 281 | pub fn set_active(&self, active: bool) -> bool { |
| 148 | - // SAFETY: `handle` came from `ao_open` and is still open. | 282 | + match &self.backend { |
| 149 | - unsafe { (self.plugin.set_active)(self.handle, active as u32) == 0 } | 283 | + Backend::Silent(s) => { |
| 284 | + s.set_active(active); | ||
| 285 | + true | ||
| 286 | + } | ||
| 287 | + Backend::Plugin { plugin, handle } => { | ||
| 288 | + // SAFETY: `handle` came from `ao_open` and is still open. | ||
| 289 | + unsafe { (plugin.set_active)(*handle, active as u32) == 0 } | ||
| 290 | + } | ||
| 291 | + } | ||
| 150 | } | 292 | } |
| 151 | 293 | ||
| 152 | /// Sets the output gain, 0.0 (silent) to 1.0 (as written): the editor's | 294 | /// Sets the output gain, 0.0 (silent) to 1.0 (as written): the editor's |
| @@ -154,15 +296,28 @@ impl AoSession { | |||
| 154 | /// clock is unaffected -- a silenced stream still plays, so the picture | 296 | /// clock is unaffected -- a silenced stream still plays, so the picture |
| 155 | /// keeps running against it. `false` for a gain outside the range. | 297 | /// keeps running against it. `false` for a gain outside the range. |
| 156 | pub fn set_volume(&self, gain: f32) -> bool { | 298 | pub fn set_volume(&self, gain: f32) -> bool { |
| 157 | - // SAFETY: `handle` came from `ao_open` and is still open. | 299 | + if !(0.0..=1.0).contains(&gain) { |
| 158 | - unsafe { (self.plugin.set_volume)(self.handle, gain) == 0 } | 300 | + return false; |
| 301 | + } | ||
| 302 | + match &self.backend { | ||
| 303 | + Backend::Silent(_) => true, | ||
| 304 | + Backend::Plugin { plugin, handle } => { | ||
| 305 | + // SAFETY: `handle` came from `ao_open` and is still open. | ||
| 306 | + unsafe { (plugin.set_volume)(*handle, gain) == 0 } | ||
| 307 | + } | ||
| 308 | + } | ||
| 159 | } | 309 | } |
| 160 | 310 | ||
| 161 | /// Drops everything still queued, so the next samples written play right | 311 | /// Drops everything still queued, so the next samples written play right |
| 162 | /// away -- what a seek needs. The played position keeps counting. | 312 | /// away -- what a seek needs. The played position keeps counting. |
| 163 | pub fn flush(&self) -> bool { | 313 | pub fn flush(&self) -> bool { |
| 164 | - // SAFETY: `handle` came from `ao_open` and is still open. | 314 | + match &self.backend { |
| 165 | - unsafe { (self.plugin.flush)(self.handle) == 0 } | 315 | + Backend::Silent(_) => true, |
| 316 | + Backend::Plugin { plugin, handle } => { | ||
| 317 | + // SAFETY: `handle` came from `ao_open` and is still open. | ||
| 318 | + unsafe { (plugin.flush)(*handle) == 0 } | ||
| 319 | + } | ||
| 320 | + } | ||
| 166 | } | 321 | } |
| 167 | 322 | ||
| 168 | /// Starved callbacks so far, and the device position (samples per channel) | 323 | /// Starved callbacks so far, and the device position (samples per channel) |
| @@ -170,20 +325,47 @@ impl AoSession { | |||
| 170 | /// dry. Two atomic loads inside the plugin, so it is cheap enough to poll | 325 | /// dry. Two atomic loads inside the plugin, so it is cheap enough to poll |
| 171 | /// and it never touches the RT thread's path. | 326 | /// and it never touches the RT thread's path. |
| 172 | pub fn underruns(&self) -> (u64, Option<i64>) { | 327 | pub fn underruns(&self) -> (u64, Option<i64>) { |
| 173 | - let mut last = -1i64; | 328 | + match &self.backend { |
| 174 | - // SAFETY: `handle` came from `ao_open`, and `last` is a live i64 of ours. | 329 | + Backend::Silent(_) => (0, None), |
| 175 | - let count = unsafe { (self.plugin.underruns)(self.handle, &raw mut last) }; | 330 | + Backend::Plugin { plugin, handle } => { |
| 176 | - (count.max(0) as u64, (last >= 0).then_some(last)) | 331 | + let mut last = -1i64; |
| 332 | + // SAFETY: `handle` came from `ao_open`, and `last` is a live i64 of ours. | ||
| 333 | + let count = unsafe { (plugin.underruns)(*handle, &raw mut last) }; | ||
| 334 | + (count.max(0) as u64, (last >= 0).then_some(last)) | ||
| 335 | + } | ||
| 336 | + } | ||
| 177 | } | 337 | } |
| 178 | } | 338 | } |
| 179 | 339 | ||
| 180 | impl Drop for AoSession { | 340 | impl Drop for AoSession { |
| 181 | fn drop(&mut self) { | 341 | fn drop(&mut self) { |
| 182 | - // SAFETY: `handle` came from `ao_open` and is closed exactly once. | 342 | + if let Backend::Plugin { plugin, handle } = &self.backend { |
| 183 | - unsafe { (self.plugin.close)(self.handle) } | 343 | + // SAFETY: `handle` came from `ao_open` and is closed exactly once. |
| 344 | + unsafe { (plugin.close)(*handle) } | ||
| 345 | + } | ||
| 184 | } | 346 | } |
| 185 | } | 347 | } |
| 186 | 348 | ||
| 187 | // The session is used from one thread at a time (the audio feeder) and never | 349 | // The session is used from one thread at a time (the audio feeder) and never |
| 188 | // shared; the PipeWire state behind it is not `Sync`. | 350 | // shared; the PipeWire state behind it is not `Sync`. |
| 189 | unsafe impl Send for AoSession {} | 351 | unsafe impl Send for AoSession {} |
| 352 | + | ||
| 353 | +#[cfg(test)] | ||
| 354 | +mod tests { | ||
| 355 | + use super::AoSession; | ||
| 356 | + | ||
| 357 | + #[test] | ||
| 358 | + fn a_silent_pin_never_opens_a_device() { | ||
| 359 | + // SAFETY: this is the engine's own unit-test process. | ||
| 360 | + unsafe { std::env::set_var("VE_NO_AO", "1") }; | ||
| 361 | + let mut ao = AoSession::open(48_000, 2).expect("silent opens"); | ||
| 362 | + assert_eq!(AoSession::devices_opened(), 0); | ||
| 363 | + assert!(ao.set_volume(0.0)); | ||
| 364 | + assert!(!ao.set_volume(1.5)); | ||
| 365 | + assert!(ao.set_active(true)); | ||
| 366 | + assert_eq!(ao.write(&[0.0; 64]), Some(64)); | ||
| 367 | + assert!(ao.flush()); | ||
| 368 | + ao.stream_ended(); | ||
| 369 | + assert_eq!(ao.underruns(), (0, None)); | ||
| 370 | + } | ||
| 371 | +} | ||
crates/engine/tests/ao_output.rsmodified+9 -0
| @@ -40,11 +40,18 @@ fn tone() -> Vec<f32> { | |||
| 40 | /// bytes alone) and a person with the app open. The upgrade path is recording | 40 | /// bytes alone) and a person with the app open. The upgrade path is recording |
| 41 | /// a null sink and asserting on the samples that come back. | 41 | /// a null sink and asserting on the samples that come back. |
| 42 | fn open_silent() -> AoSession { | 42 | fn open_silent() -> AoSession { |
| 43 | + want_real_device(); | ||
| 43 | let ao = AoSession::open(RATE, CHANNELS).expect("no PipeWire playback available"); | 44 | let ao = AoSession::open(RATE, CHANNELS).expect("no PipeWire playback available"); |
| 44 | assert!(ao.set_volume(0.0), "mute rejected"); | 45 | assert!(ao.set_volume(0.0), "mute rejected"); |
| 45 | ao | 46 | ao |
| 46 | } | 47 | } |
| 47 | 48 | ||
| 49 | +fn want_real_device() { | ||
| 50 | + // SAFETY: this ignored suite is --test-threads=1 and is the one | ||
| 51 | + // process that is allowed a real sink. | ||
| 52 | + unsafe { std::env::set_var("VE_AO", "1") }; | ||
| 53 | +} | ||
| 54 | + | ||
| 48 | /// Waits up to `limit` for the clock to report a played position past zero. | 55 | /// Waits up to `limit` for the clock to report a played position past zero. |
| 49 | fn wait_for_playback(ao: &AoSession, limit: Duration) -> i64 { | 56 | fn wait_for_playback(ao: &AoSession, limit: Duration) -> i64 { |
| 50 | let start = Instant::now(); | 57 | let start = Instant::now(); |
| @@ -160,11 +167,13 @@ fn muting_silences_without_stopping_the_clock() { | |||
| 160 | #[test] | 167 | #[test] |
| 161 | #[ignore = "needs libengine_audio.so and a PipeWire daemon"] | 168 | #[ignore = "needs libengine_audio.so and a PipeWire daemon"] |
| 162 | fn no_daemon_opens_nothing() { | 169 | fn no_daemon_opens_nothing() { |
| 170 | + want_real_device(); | ||
| 163 | assert!(AoSession::probe(), "plugin not loadable"); | 171 | assert!(AoSession::probe(), "plugin not loadable"); |
| 164 | // SAFETY: the suite is documented to run with --test-threads=1. | 172 | // SAFETY: the suite is documented to run with --test-threads=1. |
| 165 | unsafe { std::env::set_var("PIPEWIRE_REMOTE", "/nonexistent") }; | 173 | unsafe { std::env::set_var("PIPEWIRE_REMOTE", "/nonexistent") }; |
| 166 | let start = Instant::now(); | 174 | let start = Instant::now(); |
| 167 | let session = AoSession::open(RATE, CHANNELS); | 175 | let session = AoSession::open(RATE, CHANNELS); |
| 176 | + | ||
| 168 | let elapsed = start.elapsed(); | 177 | let elapsed = start.elapsed(); |
| 169 | unsafe { std::env::remove_var("PIPEWIRE_REMOTE") }; | 178 | unsafe { std::env::remove_var("PIPEWIRE_REMOTE") }; |
| 170 | 179 | ||