media_core/
media.rs

1#[cfg(feature = "audio")]
2use crate::audio::AudioFrameDescriptor;
3#[cfg(feature = "video")]
4use crate::video::VideoFrameDescriptor;
5use crate::{data::DataFrameDescriptor, frame::Frame, Result};
6
7#[derive(Clone, Copy, Debug, Eq, PartialEq)]
8pub enum MediaType {
9    Audio = 0,
10    Video,
11    Data,
12}
13
14pub trait FrameDescriptorSpec: Clone + PartialEq + Send + Sync + Into<FrameDescriptor> + 'static {
15    fn media_type(&self) -> MediaType;
16    fn create_frame(&self) -> Result<Frame<'static, Self>>;
17    #[cfg(feature = "audio")]
18    fn as_audio(&self) -> Option<&AudioFrameDescriptor> {
19        None
20    }
21    #[cfg(feature = "video")]
22    fn as_video(&self) -> Option<&VideoFrameDescriptor> {
23        None
24    }
25    fn as_data(&self) -> Option<&DataFrameDescriptor> {
26        None
27    }
28}
29
30#[derive(Clone, Debug, Eq, PartialEq)]
31pub enum FrameDescriptor {
32    #[cfg(feature = "audio")]
33    Audio(AudioFrameDescriptor),
34    #[cfg(feature = "video")]
35    Video(VideoFrameDescriptor),
36    Data(DataFrameDescriptor),
37}
38
39impl FrameDescriptor {
40    pub fn media_type(&self) -> MediaType {
41        match self {
42            #[cfg(feature = "audio")]
43            FrameDescriptor::Audio(_) => MediaType::Audio,
44            #[cfg(feature = "video")]
45            FrameDescriptor::Video(_) => MediaType::Video,
46            FrameDescriptor::Data(_) => MediaType::Data,
47        }
48    }
49
50    #[cfg(feature = "audio")]
51    pub fn as_audio(&self) -> Option<&AudioFrameDescriptor> {
52        if let FrameDescriptor::Audio(desc) = self {
53            Some(desc)
54        } else {
55            None
56        }
57    }
58
59    #[cfg(feature = "video")]
60    pub fn as_video(&self) -> Option<&VideoFrameDescriptor> {
61        if let FrameDescriptor::Video(desc) = self {
62            Some(desc)
63        } else {
64            None
65        }
66    }
67
68    pub fn as_data(&self) -> Option<&DataFrameDescriptor> {
69        #[allow(irrefutable_let_patterns)]
70        if let FrameDescriptor::Data(desc) = self {
71            Some(desc)
72        } else {
73            None
74        }
75    }
76
77    #[cfg(feature = "audio")]
78    pub fn is_audio(&self) -> bool {
79        matches!(self, FrameDescriptor::Audio(_))
80    }
81
82    #[cfg(feature = "video")]
83    pub fn is_video(&self) -> bool {
84        matches!(self, FrameDescriptor::Video(_))
85    }
86
87    pub fn is_data(&self) -> bool {
88        matches!(self, FrameDescriptor::Data(_))
89    }
90}
91
92impl FrameDescriptorSpec for FrameDescriptor {
93    fn media_type(&self) -> MediaType {
94        self.media_type()
95    }
96
97    fn create_frame(&self) -> Result<Frame<'static, Self>> {
98        Frame::new_with_generic_descriptor(self.clone())
99    }
100
101    #[cfg(feature = "audio")]
102    fn as_audio(&self) -> Option<&AudioFrameDescriptor> {
103        self.as_audio()
104    }
105
106    #[cfg(feature = "video")]
107    fn as_video(&self) -> Option<&VideoFrameDescriptor> {
108        self.as_video()
109    }
110
111    fn as_data(&self) -> Option<&DataFrameDescriptor> {
112        self.as_data()
113    }
114}