loyd / rscam

Rust wrapper for v4l2
Other
116 stars 22 forks source link

Pushing a frame to a vector causing unbounded blocking #27

Open seanybaggins opened 5 years ago

seanybaggins commented 5 years ago

I want to create a vector of 10 frames. The following code will block.

fn main() {
    let mut camera = rscam::new("/dev/video0").unwrap();

    camera.start(&rscam::Config {
        interval: (1, 30),      // 30 fps.
        resolution: (1280, 720),
        format: b"MJPG",
        ..Default::default()
    }).unwrap();

    let mut vec = Vec::with_capacity(10);

    for i in 0..vec.capacity() {
        let frame = camera.capture().unwrap();
        vec.push(frame);
    }
}
leo60228 commented 4 years ago

Due to the lack of a standardized way to asynchronously wait on file descriptors, I wouldn't call this rscam's responsibility. If Camera.fd was public, you could do something like this:

use mio::unix::EventedFd;
use mio::Ready;
use tokio::io::PollEvented;
use futures::future::poll_fn;

#[tokio::main]
async fn main() {
    let mut camera = rscam::new("/dev/video0").unwrap();

    camera.start(&rscam::Config {
        interval: (1, 30),      // 30 fps.
        resolution: (1280, 720),
        format: b"MJPG",
        ..Default::default()
    }).unwrap();

    let mut readiness = Ready::all();
    readiness.remove(Ready::writable());

    let poll = PollEvented::new(EventedFd(&camera.fd));

    let mut vec = Vec::with_capacity(10);

    for i in 0..vec.capacity() {
        poll_fn(move |cx| poll.poll_read_ready(cx, readiness)).await.unwrap();
        let frame = camera.capture().unwrap();
        vec.push(frame);
    }
}