Skip to main content

rustcv_core/
builder.rs

1use crate::pixel_format::PixelFormat;
2
3#[derive(Debug, Clone)]
4pub struct CameraConfig {
5    pub resolution_req: Vec<(u32, u32, Priority)>,
6    pub fps_req: Option<(u32, Priority)>,
7    pub format_req: Vec<(PixelFormat, Priority)>,
8    pub buffer_count: usize,         // Ring Buffer 大小,默认 3
9    pub align_stride: Option<usize>, // 强制内存对齐 (如 256字节)
10}
11
12#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
13pub enum Priority {
14    Low = 0,
15    Medium = 50,
16    High = 100,
17    Required = 255, // 必须满足,否则报错
18}
19
20impl Default for CameraConfig {
21    fn default() -> Self {
22        Self::new()
23    }
24}
25
26impl CameraConfig {
27    pub fn new() -> Self {
28        Self {
29            resolution_req: vec![],
30            fps_req: None,
31            format_req: vec![],
32            buffer_count: 3,
33            align_stride: Some(256), // 默认对齐以利于 SIMD
34        }
35    }
36
37    /// 添加分辨率要求
38    pub fn resolution(mut self, w: u32, h: u32, p: Priority) -> Self {
39        self.resolution_req.push((w, h, p));
40        self
41    }
42
43    /// 添加帧率要求
44    pub fn fps(mut self, fps: u32, p: Priority) -> Self {
45        self.fps_req = Some((fps, p));
46        self
47    }
48
49    /// 添加像素格式要求
50    /// 支持传入 PixelFormat 或 FourCC (会自动转换)
51    pub fn format<T: Into<PixelFormat>>(mut self, fmt: T, p: Priority) -> Self {
52        self.format_req.push((fmt.into(), p));
53        self
54    }
55
56    /// 设置缓冲区数量 (默认 3)
57    pub fn buffer_count(mut self, count: usize) -> Self {
58        self.buffer_count = count;
59        self
60    }
61}