Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pushing a frame to a vector causing unbounded blocking #27

Open
seanybaggins opened this issue Nov 28, 2019 · 1 comment
Open

Pushing a frame to a vector causing unbounded blocking #27

seanybaggins opened this issue Nov 28, 2019 · 1 comment

Comments

@seanybaggins
Copy link

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);
    }
}
@seanybaggins seanybaggins changed the title Pushing a frame to a vector causing blocking Pushing a frame to a vector causing unbounded blocking Nov 28, 2019
@leo60228
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants