media_device/
device.rs

1use std::sync::Arc;
2
3use media_core::{frame::Frame, variant::Variant, Result};
4
5#[derive(Clone, Debug)]
6pub struct DeviceInformation {
7    pub id: String,
8    pub name: String,
9}
10
11pub enum DeviceEvent {
12    Added(DeviceInformation), // Device added
13    Removed(String),          // Device removed, removed device ID
14    Refreshed(usize),         // All devices refreshed, number of devices
15}
16
17#[allow(unused)]
18pub(crate) type OutputHandler = Arc<dyn Fn(Frame) -> Result<()> + Send + Sync>;
19
20pub trait Device {
21    fn name(&self) -> &str;
22    fn id(&self) -> &str;
23    fn start(&mut self) -> Result<()>;
24    fn stop(&mut self) -> Result<()>;
25    fn configure(&mut self, options: &Variant) -> Result<()>;
26    fn control(&mut self, action: &Variant) -> Result<()>;
27    fn running(&self) -> bool;
28    fn formats(&self) -> Result<Variant>;
29}
30
31pub trait OutputDevice: Device {
32    fn set_output_handler<F>(&mut self, handler: F) -> Result<()>
33    where
34        F: Fn(Frame) -> Result<()> + Send + Sync + 'static;
35}
36
37#[allow(unused)]
38pub(crate) type DeviceEventHandler = Box<dyn Fn(&DeviceEvent) + Send + Sync>;
39
40pub trait DeviceManager {
41    type DeviceType: Device;
42    type Iter<'a>: Iterator<Item = &'a Self::DeviceType>
43    where
44        Self: 'a;
45    type IterMut<'a>: Iterator<Item = &'a mut Self::DeviceType>
46    where
47        Self: 'a;
48
49    fn init() -> Result<Self>
50    where
51        Self: Sized;
52    fn deinit(&mut self);
53    fn index(&self, index: usize) -> Option<&Self::DeviceType>;
54    fn index_mut(&mut self, index: usize) -> Option<&mut Self::DeviceType>;
55    fn lookup(&self, id: &str) -> Option<&Self::DeviceType>;
56    fn lookup_mut(&mut self, id: &str) -> Option<&mut Self::DeviceType>;
57    fn iter(&self) -> Self::Iter<'_>;
58    fn iter_mut(&mut self) -> Self::IterMut<'_>;
59    fn refresh(&mut self) -> Result<()>;
60    fn set_change_handler<F>(&mut self, handler: F) -> Result<()>
61    where
62        F: Fn(&DeviceEvent) + Send + Sync + 'static;
63}