From 67f0b0b38a894fc5b4136acf827b396e1108cd61 Mon Sep 17 00:00:00 2001 From: Michael Eden Date: Fri, 26 May 2023 11:57:52 -0400 Subject: [PATCH] allow manually dequeueing buffers --- src/io/mmap/stream.rs | 13 ++++++------- src/io/traits.rs | 3 +++ src/io/userptr/stream.rs | 13 ++++++------- 3 files changed, 15 insertions(+), 14 deletions(-) diff --git a/src/io/mmap/stream.rs b/src/io/mmap/stream.rs index 6db2c43..3205db6 100644 --- a/src/io/mmap/stream.rs +++ b/src/io/mmap/stream.rs @@ -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 @@ -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) } } diff --git a/src/io/traits.rs b/src/io/traits.rs index 8234016..37d0fb8 100644 --- a/src/io/traits.rs +++ b/src/io/traits.rs @@ -20,6 +20,9 @@ pub trait CaptureStream<'a>: Stream { /// Remove a buffer from the drivers' outgoing queue fn dequeue(&mut self) -> io::Result; + /// 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)>; diff --git a/src/io/userptr/stream.rs b/src/io/userptr/stream.rs index 5261806..272aaad 100644 --- a/src/io/userptr/stream.rs +++ b/src/io/userptr/stream.rs @@ -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 @@ -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) } }