|
| 1 | +use std::vec::IntoIter as VecIntoIter; |
| 2 | + |
| 3 | +use cidre::sc; |
| 4 | + |
| 5 | +use crate::{BackendSpecificError, DevicesError, SupportedStreamConfigRange}; |
| 6 | + |
| 7 | +use super::Device; |
| 8 | + |
| 9 | +pub struct Devices(VecIntoIter<Device>); |
| 10 | + |
| 11 | +impl Devices { |
| 12 | + pub fn new() -> Result<Self, DevicesError> { |
| 13 | + let (tx, rx) = std::sync::mpsc::channel(); |
| 14 | + sc::ShareableContent::current_with_ch(move |a, b| { |
| 15 | + let mut res = Result::Err(BackendSpecificError { |
| 16 | + description: "Failed to get current shareable content".to_string(), |
| 17 | + }); |
| 18 | + if let Some(err) = b { |
| 19 | + res = Result::Err(BackendSpecificError { |
| 20 | + description: format!("{err}"), |
| 21 | + }); |
| 22 | + } |
| 23 | + if let Some(sc) = a { |
| 24 | + res = Result::Ok(sc.retained()); |
| 25 | + } |
| 26 | + tx.send(res).unwrap(); |
| 27 | + }); |
| 28 | + let sc_shareable_content = rx.recv().unwrap()?; |
| 29 | + |
| 30 | + let mut res = Vec::new(); |
| 31 | + for display in sc_shareable_content.displays().iter() { |
| 32 | + res.push(Device::new(display.retained())); |
| 33 | + } |
| 34 | + |
| 35 | + Ok(Devices(res.into_iter())) |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +unsafe impl Send for Devices {} |
| 40 | +unsafe impl Sync for Devices {} |
| 41 | + |
| 42 | +impl Iterator for Devices { |
| 43 | + type Item = Device; |
| 44 | + |
| 45 | + fn next(&mut self) -> Option<Self::Item> { |
| 46 | + self.0.next() |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +pub fn default_input_device() -> Option<Device> { |
| 51 | + let devices = Devices::new().ok()?; |
| 52 | + devices.into_iter().next() |
| 53 | +} |
| 54 | + |
| 55 | +pub fn default_output_device() -> Option<Device> { |
| 56 | + None |
| 57 | +} |
| 58 | + |
| 59 | +pub type SupportedInputConfigs = VecIntoIter<SupportedStreamConfigRange>; |
| 60 | +pub type SupportedOutputConfigs = VecIntoIter<SupportedStreamConfigRange>; |
0 commit comments