Skip to content

Commit

Permalink
allow manually dequeueing buffers
Browse files Browse the repository at this point in the history
  • Loading branch information
illegalprime committed May 26, 2023
1 parent 7391c14 commit 67f0b0b
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 14 deletions.
13 changes: 6 additions & 7 deletions src/io/mmap/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,10 @@ impl<'a, 'b> CaptureStream<'b> for Stream<'a> {
Ok(self.arena_index)
}

fn get(&self, index: usize) -> io::Result<(&Self::Item, &Metadata)> {
Ok((&self.arena.bufs[index], &self.buf_meta[index]))
}

fn next(&'b mut self) -> io::Result<(&Self::Item, &Metadata)> {
if !self.active {
// Enqueue all buffers once on stream start
Expand All @@ -208,13 +212,8 @@ impl<'a, 'b> CaptureStream<'b> for Stream<'a> {
CaptureStream::queue(self, self.arena_index)?;
}

self.arena_index = CaptureStream::dequeue(self)?;

// The index used to access the buffer elements is given to us by v4l2, so we assume it
// will always be valid.
let bytes = &self.arena.bufs[self.arena_index];
let meta = &self.buf_meta[self.arena_index];
Ok((bytes, meta))
let index = CaptureStream::dequeue(self)?;
CaptureStream::get(self, index)
}
}

Expand Down
3 changes: 3 additions & 0 deletions src/io/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ pub trait CaptureStream<'a>: Stream {
/// Remove a buffer from the drivers' outgoing queue
fn dequeue(&mut self) -> io::Result<usize>;

/// Access the buffer at the specified index.
fn get(&self, index: usize) -> io::Result<(&Self::Item, &Metadata)>;

/// Fetch a new frame by first queueing and then dequeueing.
/// First time initialization is performed if necessary.
fn next(&'a mut self) -> io::Result<(&Self::Item, &Metadata)>;
Expand Down
13 changes: 6 additions & 7 deletions src/io/userptr/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,10 @@ impl<'a> CaptureStream<'a> for Stream {
Ok(self.arena_index)
}

fn get(&self, index: usize) -> io::Result<(&Self::Item, &Metadata)> {
Ok((&self.arena.bufs[index], &self.buf_meta[index]))
}

fn next(&'a mut self) -> io::Result<(&Self::Item, &Metadata)> {
if !self.active {
// Enqueue all buffers once on stream start
Expand All @@ -203,12 +207,7 @@ impl<'a> CaptureStream<'a> for Stream {
self.queue(self.arena_index)?;
}

self.arena_index = self.dequeue()?;

// The index used to access the buffer elements is given to us by v4l2, so we assume it
// will always be valid.
let bytes = &mut self.arena.bufs[self.arena_index];
let meta = &self.buf_meta[self.arena_index];
Ok((bytes, meta))
let index = self.dequeue()?;
self.get(index)
}
}

0 comments on commit 67f0b0b

Please sign in to comment.