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
43    fn init() -> Result<Self>
44    where
45        Self: Sized;
46    fn uninit(&mut self);
47    fn list(&self) -> Vec<&Self::DeviceType>;
48    fn index(&self, index: usize) -> Option<&Self::DeviceType>;
49    fn index_mut(&mut self, index: usize) -> Option<&mut Self::DeviceType>;
50    fn lookup(&self, id: &str) -> Option<&Self::DeviceType>;
51    fn lookup_mut(&mut self, id: &str) -> Option<&mut Self::DeviceType>;
52    fn refresh(&mut self) -> Result<()>;
53    fn set_change_handler<F>(&mut self, handler: F) -> Result<()>
54    where
55        F: Fn(&DeviceEvent) + Send + Sync + 'static;
56}