use arrayvec::ArrayVec;
const MAX_FRAME_RATE_RANGES: usize = 8;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum PixelFormat {
Nv12,
Yuyv,
Uyvy,
Bgra32,
Jpeg,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct Size {
pub width: u32,
pub height: u32,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct Ratio {
pub numerator: u32,
pub denominator: u32,
}
impl Ratio {
pub fn as_f64(&self) -> f64 {
self.numerator as f64 / self.denominator as f64
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct FrameRateRange {
pub min: Ratio,
pub max: Ratio,
}
#[derive(Debug, Clone, PartialEq)]
pub struct FormatDescriptor {
pub pixel_format: PixelFormat,
pub size: Size,
frame_rate_ranges: ArrayVec<FrameRateRange, MAX_FRAME_RATE_RANGES>,
}
impl FormatDescriptor {
pub(crate) fn from_ranges(
pixel_format: PixelFormat,
size: Size,
frame_rate_ranges: impl IntoIterator<Item = FrameRateRange>,
) -> impl Iterator<Item = Self> {
let mut iter = frame_rate_ranges.into_iter();
core::iter::from_fn(move || {
let mut chunk = ArrayVec::new();
for range in iter.by_ref() {
chunk.push(range);
if chunk.is_full() {
break;
}
}
if chunk.is_empty() {
None
} else {
Some(FormatDescriptor {
pixel_format,
size,
frame_rate_ranges: chunk,
})
}
})
}
pub fn frame_rate_ranges(&self) -> &[FrameRateRange] {
&self.frame_rate_ranges
}
}
#[derive(Debug, Clone)]
pub struct StreamConfig {
pub pixel_format: PixelFormat,
pub size: Size,
pub frame_rate: Ratio,
}