-
Notifications
You must be signed in to change notification settings - Fork 60
/
stream_capture_userptr.rs
60 lines (48 loc) · 1.69 KB
/
stream_capture_userptr.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
use std::io;
use std::time::Instant;
use v4l::buffer::Type;
use v4l::io::traits::CaptureStream;
use v4l::prelude::*;
use v4l::video::Capture;
fn main() -> io::Result<()> {
let path = "/dev/video0";
println!("Using device: {}\n", path);
// Capture 4 frames by default
let count = 4;
// Allocate 4 buffers by default
let buffer_count = 4;
let dev = Device::with_path(path)?;
let format = dev.format()?;
let params = dev.params()?;
println!("Active format:\n{}", format);
println!("Active parameters:\n{}", params);
// Setup a buffer stream and grab a frame, then print its data
let mut stream = UserptrStream::with_buffers(&dev, Type::VideoCapture, buffer_count)?;
// warmup
stream.next()?;
let start = Instant::now();
let mut megabytes_ps: f64 = 0.0;
for i in 0..count {
let t0 = Instant::now();
let (buf, meta) = stream.next()?;
let duration_us = t0.elapsed().as_micros();
let cur = buf.len() as f64 / 1_048_576.0 * 1_000_000.0 / duration_us as f64;
if i == 0 {
megabytes_ps = cur;
} else {
// ignore the first measurement
let prev = megabytes_ps * (i as f64 / (i + 1) as f64);
let now = cur * (1.0 / (i + 1) as f64);
megabytes_ps = prev + now;
}
println!("Buffer");
println!(" sequence : {}", meta.sequence);
println!(" timestamp : {}", meta.timestamp);
println!(" flags : {}", meta.flags);
println!(" length : {}", buf.len());
}
println!();
println!("FPS: {}", count as f64 / start.elapsed().as_secs_f64());
println!("MB/s: {}", megabytes_ps);
Ok(())
}