| Lift freeq's AV media plane out of sleek 90f8b89 nandi 20d ago | 1 | use crate::traits::HostTrait; |
| 2 | use device::{init_devices, Class, Device, Devices}; |
| 3 | mod device; |
| 4 | mod stream; |
| 5 | mod utils; |
| 6 | |
| 7 | #[inline] |
| 8 | fn pipewire_available() -> bool { |
| 9 | let dir = std::env::var("PIPEWIRE_RUNTIME_DIR") |
| 10 | .or_else(|_| std::env::var("XDG_RUNTIME_DIR")) |
| 11 | .unwrap_or_default(); |
| 12 | std::path::Path::new(&dir).join("pipewire-0").exists() |
| 13 | } |
| 14 | |
| 15 | #[derive(Debug)] |
| 16 | pub struct Host(Vec<Device>); |
| 17 | |
| 18 | impl Host { |
| 19 | pub fn new() -> Result<Self, crate::HostUnavailable> { |
| 20 | let devices = init_devices().ok_or(crate::HostUnavailable)?; |
| 21 | Ok(Host(devices)) |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | impl HostTrait for Host { |
| 26 | type Devices = Devices; |
| 27 | type Device = Device; |
| 28 | fn is_available() -> bool { |
| 29 | pipewire_available() |
| 30 | } |
| 31 | fn devices(&self) -> Result<Self::Devices, crate::DevicesError> { |
| 32 | Ok(self.0.clone().into_iter()) |
| 33 | } |
| 34 | |
| 35 | fn default_input_device(&self) -> Option<Self::Device> { |
| 36 | self.0 |
| 37 | .iter() |
| 38 | .find(|device| matches!(device.class(), Class::DefaultInput)) |
| 39 | .cloned() |
| 40 | } |
| 41 | fn default_output_device(&self) -> Option<Self::Device> { |
| 42 | self.0 |
| 43 | .iter() |
| 44 | .find(|device| matches!(device.class(), Class::DefaultOutput)) |
| 45 | .cloned() |
| 46 | } |
| 47 | } |