@@ -11,7 +11,7 @@ use std::{
1111
1212use v4l2_sys:: { v4l2_buffer, v4l2_format, v4l2_requestbuffers} ;
1313
14- use crate :: buffer:: { Metadata , Type } ;
14+ use crate :: buffer:: { Buffer , Type } ;
1515use crate :: device:: { Device , Handle } ;
1616use crate :: io:: traits:: { CaptureStream , OutputStream , Stream as StreamTrait } ;
1717use crate :: memory:: { Memory , Mmap , UserPtr } ;
@@ -23,7 +23,7 @@ use crate::v4l2;
2323/// In case of errors during unmapping, we panic because there is memory corruption going on.
2424pub ( crate ) struct Arena < T > {
2525 handle : Arc < Handle > ,
26- pub bufs : Vec < T > ,
26+ pub bufs : Vec < Buffer < T > > ,
2727 pub buf_mem : Memory ,
2828 pub buf_type : Type ,
2929}
@@ -119,7 +119,8 @@ impl<'a> Arena<Mmap<'a>> {
119119
120120 let slice =
121121 slice:: from_raw_parts_mut :: < u8 > ( ptr as * mut u8 , v4l2_buf. length as usize ) ;
122- self . bufs . push ( Mmap ( slice) ) ;
122+ let buf = Buffer :: new ( Mmap ( slice) ) ;
123+ self . bufs . push ( buf) ;
123124 }
124125 }
125126
@@ -171,8 +172,8 @@ You may want to use this crate with the raw v4l2 FFI bindings instead!\n"
171172 let count = self . request ( count) ?;
172173 for _ in 0 ..count {
173174 let size = unsafe { v4l2_fmt. fmt . pix . sizeimage } ;
174- let buf = vec ! [ 0u8 ; size as usize ] ;
175- self . bufs . push ( UserPtr ( buf) ) ;
175+ let buf = Buffer :: new ( UserPtr ( vec ! [ 0u8 ; size as usize ] ) ) ;
176+ self . bufs . push ( buf) ;
176177 }
177178
178179 Ok ( count)
@@ -187,7 +188,6 @@ pub struct Stream<T> {
187188 arena : Arena < T > ,
188189 arena_index : usize ,
189190 buf_type : Type ,
190- buf_meta : Vec < Metadata > ,
191191 timeout : Option < i32 > ,
192192
193193 active : bool ,
@@ -220,16 +220,13 @@ impl<'a> Stream<Mmap<'a>> {
220220
221221 pub fn with_buffers ( dev : & Device , buf_type : Type , buf_count : u32 ) -> io:: Result < Self > {
222222 let mut arena = Arena :: < Mmap < ' a > > :: new ( dev. handle ( ) , buf_type) ;
223- let count = arena. allocate ( buf_count) ?;
224- let mut buf_meta = Vec :: new ( ) ;
225- buf_meta. resize ( count as usize , Metadata :: default ( ) ) ;
223+ let _count = arena. allocate ( buf_count) ?;
226224
227225 Ok ( Stream {
228226 handle : dev. handle ( ) ,
229227 arena,
230228 arena_index : 0 ,
231229 buf_type,
232- buf_meta,
233230 active : false ,
234231 timeout : None ,
235232 } )
@@ -263,16 +260,13 @@ impl Stream<UserPtr> {
263260
264261 pub fn with_buffers ( dev : & Device , buf_type : Type , buf_count : u32 ) -> io:: Result < Self > {
265262 let mut arena = Arena :: < UserPtr > :: new ( dev. handle ( ) , buf_type) ;
266- let count = arena. allocate ( buf_count) ?;
267- let mut buf_meta = Vec :: new ( ) ;
268- buf_meta. resize ( count as usize , Metadata :: default ( ) ) ;
263+ let _count = arena. allocate ( buf_count) ?;
269264
270265 Ok ( Stream {
271266 handle : dev. handle ( ) ,
272267 arena,
273268 arena_index : 0 ,
274269 buf_type,
275- buf_meta,
276270 active : false ,
277271 timeout : None ,
278272 } )
@@ -323,7 +317,7 @@ impl<T> Drop for Stream<T> {
323317}
324318
325319impl < T > StreamTrait for Stream < T > {
326- type Item = [ u8 ] ;
320+ type Item = Buffer < T > ;
327321
328322 fn start ( & mut self ) -> io:: Result < ( ) > {
329323 unsafe {
@@ -394,18 +388,17 @@ where
394388 }
395389 self . arena_index = v4l2_buf. index as usize ;
396390
397- self . buf_meta [ self . arena_index ] = Metadata {
398- bytesused : v4l2_buf. bytesused ,
399- flags : v4l2_buf. flags . into ( ) ,
400- field : v4l2_buf. field ,
401- timestamp : v4l2_buf. timestamp . into ( ) ,
402- sequence : v4l2_buf. sequence ,
403- } ;
391+ let buf = & mut self . arena . bufs [ self . arena_index ] ;
392+ buf. bytesused = v4l2_buf. bytesused ;
393+ buf. flags = v4l2_buf. flags . into ( ) ;
394+ buf. field = v4l2_buf. field ;
395+ buf. timestamp = v4l2_buf. timestamp . into ( ) ;
396+ buf. sequence = v4l2_buf. sequence ;
404397
405398 Ok ( self . arena_index )
406399 }
407400
408- fn next ( & ' a mut self ) -> io:: Result < ( & Self :: Item , & Metadata ) > {
401+ fn next ( & ' a mut self ) -> io:: Result < & Self :: Item > {
409402 if !self . active {
410403 // Enqueue all buffers once on stream start
411404 for index in 0 ..self . arena . bufs . len ( ) {
@@ -421,9 +414,7 @@ where
421414
422415 // The index used to access the buffer elements is given to us by v4l2, so we assume it
423416 // will always be valid.
424- let bytes = & self . arena . bufs [ self . arena_index ] ;
425- let meta = & self . buf_meta [ self . arena_index ] ;
426- Ok ( ( bytes, meta) )
417+ Ok ( & self . arena . bufs [ self . arena_index ] )
427418 }
428419}
429420
@@ -442,8 +433,8 @@ where
442433 // MetaData.bytesused is initialized to 0. For an output device, when bytesused is
443434 // set to 0 v4l2 will set it to the size of the plane:
444435 // https://www.kernel.org/doc/html/v4.15/media/uapi/v4l/buffer.html#struct-v4l2-plane
445- v4l2_buf. bytesused = self . buf_meta [ index] . bytesused ;
446- v4l2_buf. field = self . buf_meta [ index] . field ;
436+ v4l2_buf. bytesused = self . arena . bufs [ index] . bytesused ;
437+ v4l2_buf. field = self . arena . bufs [ index] . field ;
447438
448439 if self
449440 . handle
@@ -476,18 +467,17 @@ where
476467 }
477468 self . arena_index = v4l2_buf. index as usize ;
478469
479- self . buf_meta [ self . arena_index ] = Metadata {
480- bytesused : v4l2_buf. bytesused ,
481- flags : v4l2_buf. flags . into ( ) ,
482- field : v4l2_buf. field ,
483- timestamp : v4l2_buf. timestamp . into ( ) ,
484- sequence : v4l2_buf. sequence ,
485- } ;
470+ let buf = & mut self . arena . bufs [ self . arena_index ] ;
471+ buf. bytesused = v4l2_buf. bytesused ;
472+ buf. flags = v4l2_buf. flags . into ( ) ;
473+ buf. field = v4l2_buf. field ;
474+ buf. timestamp = v4l2_buf. timestamp . into ( ) ;
475+ buf. sequence = v4l2_buf. sequence ;
486476
487477 Ok ( self . arena_index )
488478 }
489479
490- fn next ( & ' a mut self ) -> io:: Result < ( & mut Self :: Item , & mut Metadata ) > {
480+ fn next ( & ' a mut self ) -> io:: Result < & mut Self :: Item > {
491481 let init = !self . active ;
492482 if !self . active {
493483 self . start ( ) ?;
@@ -503,8 +493,6 @@ where
503493
504494 // The index used to access the buffer elements is given to us by v4l2, so we assume it
505495 // will always be valid.
506- let bytes = & mut self . arena . bufs [ self . arena_index ] ;
507- let meta = & mut self . buf_meta [ self . arena_index ] ;
508- Ok ( ( bytes, meta) )
496+ Ok ( & mut self . arena . bufs [ self . arena_index ] )
509497 }
510498}
0 commit comments