raymanfx / libv4l-rs

Video4Linux2 bindings for Rust
MIT License
152 stars 65 forks source link

v4l2_buffer's bytesuesd field #13

Closed EtomicBomb closed 4 years ago

EtomicBomb commented 4 years ago

Hey, I've been checking out different rust libraries for v4l, and I noticed this one doesn't use the bytesused field for v4l2_buffer. I'm trying to send webcam frames over a network, so I'm trying to get the smallest possible frame sizes. In the standard example:

use v4l::prelude::*;

let mut dev = CaptureDevice::new(0).expect("Failed to open device");

let mut stream =
    MmapStream::with_buffers(&mut dev, 4).expect("Failed to create buffer stream");

loop {
    let frame = stream.next().unwrap();
    println!(
        "Buffer size: {}, seq: {}, timestamp: {}",
       frame.len(),
       frame.meta().seq,
       frame.meta().timestamp
   );
}

The len value is reported as 1843789 on my machine, even though I know that only about 33K of those bytes are used to describe an actual frame. Is there a way in this library to find how many bytes are framedata? I know that rscam is able to give me the real sizes through the v4l2_buffer.bytesused. It seems like that is more intuitive for the user.

raymanfx commented 4 years ago

This is an issue indeed, thanks for spotting! I wonder what would be the better course of action:

  1. Make slices take the "real" length, i.e. the bytesused of the underlying buffer here: https://github.com/raymanfx/libv4l-rs/blob/master/src/io/mmap/arena.rs#L93 (so v4l2_buf.bytesused)
  2. Expose the 'bytesused' attribute in the Metadata struct of the buffer
raymanfx commented 4 years ago

Should be fixed by https://github.com/raymanfx/libv4l-rs/commit/102d4697515804c48e1b500d8c57243341cf8875, which will land tonight on master on crates.io.