| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | use std::sync::atomic::{AtomicBool, AtomicI64, AtomicU32, AtomicU64, Ordering}; |
| 14 | use std::sync::LazyLock; |
| 15 | use std::time::Instant; |
| 16 | |
| 17 | use crate::pulse::{self, Playback}; |
| 18 | |
| 19 | static DEVICES_OPENED: AtomicU64 = AtomicU64::new(0); |
| 20 | |
| 21 | fn env_is(name: &str, value: &str) -> bool { |
| 22 | crate::envcfg::var_os(name).is_some_and(|v| v == value) |
| 23 | } |
| 24 | |
| 25 | |
| 26 | |
| 27 | |
| 28 | fn cargo_test_binary() -> bool { |
| 29 | std::env::current_exe() |
| 30 | .ok() |
| 31 | .and_then(|p| p.parent().map(|d| d.ends_with("deps"))) |
| 32 | .unwrap_or(false) |
| 33 | } |
| 34 | |
| 35 | |
| 36 | static NO_AO: AtomicBool = AtomicBool::new(false); |
| 37 | |
| 38 | |
| 39 | |
| 40 | |
| 41 | pub fn set_silent_output() { |
| 42 | NO_AO.store(true, Ordering::SeqCst); |
| 43 | } |
| 44 | |
| 45 | fn silent_output() -> bool { |
| 46 | if env_is("VE_AO", "1") { |
| 47 | return false; |
| 48 | } |
| 49 | NO_AO.load(Ordering::SeqCst) || env_is("VE_NO_AO", "1") || cargo_test_binary() |
| 50 | } |
| 51 | |
| 52 | |
| 53 | pub struct AoSession { |
| 54 | backend: Backend, |
| 55 | } |
| 56 | |
| 57 | enum Backend { |
| 58 | Server(Playback), |
| 59 | |
| 60 | |
| 61 | |
| 62 | Silent(Silent), |
| 63 | } |
| 64 | |
| 65 | struct Silent { |
| 66 | rate: f64, |
| 67 | |
| 68 | base: AtomicI64, |
| 69 | |
| 70 | origin_ms: AtomicU64, |
| 71 | |
| 72 | |
| 73 | |
| 74 | |
| 75 | gain: AtomicU32, |
| 76 | } |
| 77 | |
| 78 | fn epoch() -> Instant { |
| 79 | static EPOCH: LazyLock<Instant> = LazyLock::new(Instant::now); |
| 80 | *EPOCH |
| 81 | } |
| 82 | |
| 83 | fn now_ms() -> u64 { |
| 84 | epoch().elapsed().as_millis() as u64 |
| 85 | } |
| 86 | |
| 87 | impl Silent { |
| 88 | fn new(sample_rate: u32) -> Self { |
| 89 | Self { |
| 90 | rate: f64::from(sample_rate), |
| 91 | base: AtomicI64::new(-1), |
| 92 | origin_ms: AtomicU64::new(0), |
| 93 | gain: AtomicU32::new(1.0f32.to_bits()), |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | fn gain(&self) -> f32 { |
| 98 | f32::from_bits(self.gain.load(Ordering::Relaxed)) |
| 99 | } |
| 100 | |
| 101 | fn set_gain(&self, gain: f32) { |
| 102 | self.gain.store(gain.to_bits(), Ordering::Relaxed); |
| 103 | } |
| 104 | |
| 105 | fn extra(&self) -> i64 { |
| 106 | match self.origin_ms.load(Ordering::Relaxed) { |
| 107 | 0 => 0, |
| 108 | origin => { |
| 109 | let ms = now_ms().saturating_sub(origin) as f64; |
| 110 | (ms / 1000.0 * self.rate).round() as i64 |
| 111 | } |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | fn position(&self) -> Option<i64> { |
| 116 | let base = self.base.load(Ordering::Relaxed); |
| 117 | let extra = self.extra(); |
| 118 | if base < 0 && self.origin_ms.load(Ordering::Relaxed) == 0 { |
| 119 | None |
| 120 | } else { |
| 121 | Some(base.max(0) + extra.max(0)) |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | fn set_active(&self, active: bool) { |
| 126 | if active { |
| 127 | if self.origin_ms.load(Ordering::Relaxed) == 0 { |
| 128 | self.base |
| 129 | .compare_exchange(-1, 0, Ordering::Relaxed, Ordering::Relaxed) |
| 130 | .ok(); |
| 131 | self.origin_ms.store(now_ms().max(1), Ordering::Relaxed); |
| 132 | } |
| 133 | } else { |
| 134 | let origin = self.origin_ms.swap(0, Ordering::Relaxed); |
| 135 | if origin != 0 { |
| 136 | let ms = now_ms().saturating_sub(origin) as f64; |
| 137 | let extra = (ms / 1000.0 * self.rate).round() as i64; |
| 138 | self.base |
| 139 | .compare_exchange(-1, 0, Ordering::Relaxed, Ordering::Relaxed) |
| 140 | .ok(); |
| 141 | self.base.fetch_add(extra.max(0), Ordering::Relaxed); |
| 142 | } |
| 143 | } |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | impl AoSession { |
| 148 | |
| 149 | |
| 150 | |
| 151 | pub fn open(sample_rate: u32, channels: u32) -> Option<Self> { |
| 152 | if silent_output() { |
| 153 | return Some(Self { |
| 154 | backend: Backend::Silent(Silent::new(sample_rate)), |
| 155 | }); |
| 156 | } |
| 157 | let playback = Playback::open(sample_rate, channels)?; |
| 158 | DEVICES_OPENED.fetch_add(1, Ordering::Relaxed); |
| 159 | Some(Self { |
| 160 | backend: Backend::Server(playback), |
| 161 | }) |
| 162 | } |
| 163 | |
| 164 | |
| 165 | |
| 166 | pub fn devices_opened() -> u64 { |
| 167 | DEVICES_OPENED.load(Ordering::Relaxed) |
| 168 | } |
| 169 | |
| 170 | |
| 171 | |
| 172 | |
| 173 | |
| 174 | pub fn is_silent(&self) -> bool { |
| 175 | matches!(self.backend, Backend::Silent(_)) |
| 176 | } |
| 177 | |
| 178 | |
| 179 | |
| 180 | |
| 181 | pub fn probe() -> bool { |
| 182 | pulse::server_path().is_some_and(|path| path.exists()) |
| 183 | } |
| 184 | |
| 185 | |
| 186 | |
| 187 | |
| 188 | pub fn write(&mut self, samples: &[f32]) -> Option<usize> { |
| 189 | match &mut self.backend { |
| 190 | Backend::Silent(_) => Some(samples.len()), |
| 191 | Backend::Server(playback) => playback.write(samples), |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | |
| 196 | |
| 197 | pub fn position(&self) -> Option<i64> { |
| 198 | match &self.backend { |
| 199 | Backend::Silent(s) => s.position(), |
| 200 | Backend::Server(playback) => playback.position(), |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | |
| 205 | |
| 206 | |
| 207 | pub fn stream_ended(&self) { |
| 208 | if let Backend::Server(playback) = &self.backend { |
| 209 | playback.stream_ended(); |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | |
| 214 | pub fn set_active(&self, active: bool) -> bool { |
| 215 | match &self.backend { |
| 216 | Backend::Silent(s) => { |
| 217 | s.set_active(active); |
| 218 | true |
| 219 | } |
| 220 | Backend::Server(playback) => playback.set_active(active), |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | |
| 225 | |
| 226 | |
| 227 | |
| 228 | pub fn set_volume(&self, gain: f32) -> bool { |
| 229 | if !(0.0..=1.0).contains(&gain) { |
| 230 | return false; |
| 231 | } |
| 232 | match &self.backend { |
| 233 | Backend::Silent(s) => { |
| 234 | s.set_gain(gain); |
| 235 | true |
| 236 | } |
| 237 | Backend::Server(playback) => playback.set_volume(gain), |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | |
| 242 | |
| 243 | pub fn gain(&self) -> Option<f32> { |
| 244 | match &self.backend { |
| 245 | Backend::Silent(s) => Some(s.gain()), |
| 246 | Backend::Server(playback) => Some(playback.gain()), |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | |
| 251 | |
| 252 | pub fn flush(&self) -> bool { |
| 253 | match &self.backend { |
| 254 | Backend::Silent(_) => true, |
| 255 | Backend::Server(playback) => playback.flush(), |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | |
| 260 | |
| 261 | |
| 262 | pub fn underruns(&self) -> (u64, Option<i64>) { |
| 263 | match &self.backend { |
| 264 | Backend::Silent(_) => (0, None), |
| 265 | Backend::Server(playback) => playback.underruns(), |
| 266 | } |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | #[cfg(test)] |
| 271 | mod tests { |
| 272 | use super::AoSession; |
| 273 | |
| 274 | |
| 275 | |
| 276 | |
| 277 | |
| 278 | |
| 279 | fn counters_serial() -> std::sync::MutexGuard<'static, ()> { |
| 280 | static LOCK: std::sync::Mutex<()> = std::sync::Mutex::new(()); |
| 281 | LOCK.lock().unwrap_or_else(|poisoned| poisoned.into_inner()) |
| 282 | } |
| 283 | |
| 284 | #[test] |
| 285 | fn a_silent_pin_never_opens_a_device() { |
| 286 | let _serial = counters_serial(); |
| 287 | super::set_silent_output(); |
| 288 | let before = AoSession::devices_opened(); |
| 289 | let mut ao = AoSession::open(48_000, 2).expect("silent opens"); |
| 290 | |
| 291 | |
| 292 | |
| 293 | assert!( |
| 294 | ao.is_silent(), |
| 295 | "the pin must route the open to the silent sink" |
| 296 | ); |
| 297 | assert_eq!( |
| 298 | AoSession::devices_opened(), |
| 299 | before, |
| 300 | "the silent open moved the process's device counter" |
| 301 | ); |
| 302 | assert!(ao.set_volume(0.0)); |
| 303 | assert!(!ao.set_volume(1.5)); |
| 304 | assert!(ao.set_active(true)); |
| 305 | assert_eq!(ao.write(&[0.0; 64]), Some(64)); |
| 306 | assert!(ao.flush()); |
| 307 | ao.stream_ended(); |
| 308 | assert_eq!(ao.underruns(), (0, None)); |
| 309 | } |
| 310 | } |