loyd/rscam

Pushing a frame to a vector causing unbounded blocking

Opened this issue · 1 comments

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);
    }
}

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);
    }
}