()
| 14 | use rustcv_camera::Camera; |
| 15 | |
| 16 | fn main() -> std::result::Result<(), Box<dyn std::error::Error>> { |
| 17 | println!("=== rustcv-camera: Zero-Copy Demo ==="); |
| 18 | |
| 19 | // Open camera 0 with default settings. |
| 20 | // 使用默认设置打开 0 号摄像头。 |
| 21 | let mut cam = Camera::open(0)?; |
| 22 | let config = cam.config(); |
| 23 | println!( |
| 24 | "Camera opened: {}x{} {:?} ({}fps, {} buffers)", |
| 25 | config.width, config.height, config.pixel_format, config.fps, config.buffer_count, |
| 26 | ); |
| 27 | |
| 28 | let mut start: Option<Instant> = None; |
| 29 | let mut count: u64 = 0; |
| 30 | let mut last_seq: u64 = 0; |
| 31 | let mut dropped: u64 = 0; |
| 32 | |
| 33 | println!("Capturing... Press Ctrl+C to stop.\n"); |
| 34 | |
| 35 | for _ in 0..100 { |
| 36 | // next_frame() returns Frame<'_> — zero-copy borrow of mmap buffer. |
| 37 | // The frame MUST be dropped before calling next_frame() again. |
| 38 | // next_frame() 返回 Frame<'_> —— mmap 缓冲区的零拷贝借用。 |
| 39 | // 帧必须在再次调用 next_frame() 前被 drop。 |
| 40 | let frame = cam.next_frame()?; |
| 41 | |
| 42 | // Start the timer on the first frame to exclude camera startup latency |
| 43 | // (~0.5–1s on macOS before AVFoundation delivers the first frame). |
| 44 | // 第一帧到达后才启动计时器,排除摄像头启动延迟(macOS 上约 0.5–1s)。 |
| 45 | if start.is_none() { |
| 46 | start = Some(Instant::now()); |
| 47 | } |
| 48 | |
| 49 | // Detect dropped frames via sequence number gaps. |
| 50 | // 通过序号间隔检测丢帧。 |
| 51 | if count > 0 && frame.sequence() > last_seq + 1 { |
| 52 | let gap = frame.sequence() - last_seq - 1; |
| 53 | dropped += gap; |
| 54 | } |
| 55 | last_seq = frame.sequence(); |
| 56 | |
| 57 | // frame.data() is zero-copy — points directly to kernel memory. |
| 58 | // frame.data() 是零拷贝 —— 直接指向内核内存。 |
| 59 | let data = frame.data(); |
| 60 | |
| 61 | if count.is_multiple_of(10) { |
| 62 | println!( |
| 63 | "Frame {:3}: {:?} {}x{} | {} bytes | seq={} | ts={}us", |
| 64 | count, |
| 65 | frame.pixel_format(), |
| 66 | frame.width(), |
| 67 | frame.height(), |
| 68 | data.len(), |
| 69 | frame.sequence(), |
| 70 | frame.timestamp_us(), |
| 71 | ); |
| 72 | } |
| 73 |
nothing calls this directly
no test coverage detected