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
17pub(crate) type OutputHandler = Arc<dyn Fn(Frame) -> Result<()> + Send + Sync>;
18
19pub trait Device {
20    fn name(&self) -> &str;
21    fn id(&self) -> &str;
22    fn start(&mut self) -> Result<()>;
23    fn stop(&mut self) -> Result<()>;
24    fn configure(&mut self, options: Variant) -> Result<()>;
25    fn control(&mut self, action: Variant) -> Result<()>;
26    fn running(&self) -> bool;
27    fn formats(&self) -> Result<Variant>;
28}
29
30pub trait OutputDevice: Device {
31    fn set_output_handler<F>(&mut self, handler: F) -> Result<()>
32    where
33        F: Fn(Frame) -> Result<()> + Send + Sync + 'static;
34}
35
36pub(crate) type DeviceEventHandler = Box<dyn Fn(&DeviceEvent) + Send + Sync>;
37
38pub trait DeviceManager {
39    type DeviceType: Device;
40
41    fn init() -> Result<Self>
42    where
43        Self: Sized;
44    fn uninit(&mut self);
45    fn list(&self) -> Vec<&Self::DeviceType>;
46    fn index(&self, index: usize) -> Option<&Self::DeviceType>;
47    fn index_mut(&mut self, index: usize) -> Option<&mut Self::DeviceType>;
48    fn lookup(&self, id: &str) -> Option<&Self::DeviceType>;
49    fn lookup_mut(&mut self, id: &str) -> Option<&mut Self::DeviceType>;
50    fn refresh(&mut self) -> Result<()>;
51    fn set_change_handler<F>(&mut self, handler: F) -> Result<()>
52    where
53        F: Fn(&DeviceEvent) + Send + Sync + 'static;
54}