@@ -11,7 +11,7 @@ use std::{
11
11
12
12
use v4l2_sys:: { v4l2_buffer, v4l2_format, v4l2_requestbuffers} ;
13
13
14
- use crate :: buffer:: { Metadata , Type } ;
14
+ use crate :: buffer:: { Buffer , Type } ;
15
15
use crate :: device:: { Device , Handle } ;
16
16
use crate :: io:: traits:: { CaptureStream , OutputStream , Stream as StreamTrait } ;
17
17
use crate :: memory:: { Memory , Mmap , UserPtr } ;
@@ -23,7 +23,7 @@ use crate::v4l2;
23
23
/// In case of errors during unmapping, we panic because there is memory corruption going on.
24
24
pub ( crate ) struct Arena < T > {
25
25
handle : Arc < Handle > ,
26
- pub bufs : Vec < T > ,
26
+ pub bufs : Vec < Buffer < T > > ,
27
27
pub buf_mem : Memory ,
28
28
pub buf_type : Type ,
29
29
}
@@ -119,7 +119,8 @@ impl<'a> Arena<Mmap<'a>> {
119
119
120
120
let slice =
121
121
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) ;
123
124
}
124
125
}
125
126
@@ -171,8 +172,8 @@ You may want to use this crate with the raw v4l2 FFI bindings instead!\n"
171
172
let count = self . request ( count) ?;
172
173
for _ in 0 ..count {
173
174
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) ;
176
177
}
177
178
178
179
Ok ( count)
@@ -187,7 +188,6 @@ pub struct Stream<T> {
187
188
arena : Arena < T > ,
188
189
arena_index : usize ,
189
190
buf_type : Type ,
190
- buf_meta : Vec < Metadata > ,
191
191
timeout : Option < i32 > ,
192
192
193
193
active : bool ,
@@ -220,16 +220,13 @@ impl<'a> Stream<Mmap<'a>> {
220
220
221
221
pub fn with_buffers ( dev : & Device , buf_type : Type , buf_count : u32 ) -> io:: Result < Self > {
222
222
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) ?;
226
224
227
225
Ok ( Stream {
228
226
handle : dev. handle ( ) ,
229
227
arena,
230
228
arena_index : 0 ,
231
229
buf_type,
232
- buf_meta,
233
230
active : false ,
234
231
timeout : None ,
235
232
} )
@@ -263,16 +260,13 @@ impl Stream<UserPtr> {
263
260
264
261
pub fn with_buffers ( dev : & Device , buf_type : Type , buf_count : u32 ) -> io:: Result < Self > {
265
262
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) ?;
269
264
270
265
Ok ( Stream {
271
266
handle : dev. handle ( ) ,
272
267
arena,
273
268
arena_index : 0 ,
274
269
buf_type,
275
- buf_meta,
276
270
active : false ,
277
271
timeout : None ,
278
272
} )
@@ -323,7 +317,7 @@ impl<T> Drop for Stream<T> {
323
317
}
324
318
325
319
impl < T > StreamTrait for Stream < T > {
326
- type Item = [ u8 ] ;
320
+ type Item = Buffer < T > ;
327
321
328
322
fn start ( & mut self ) -> io:: Result < ( ) > {
329
323
unsafe {
@@ -394,18 +388,17 @@ where
394
388
}
395
389
self . arena_index = v4l2_buf. index as usize ;
396
390
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 ;
404
397
405
398
Ok ( self . arena_index )
406
399
}
407
400
408
- fn next ( & ' a mut self ) -> io:: Result < ( & Self :: Item , & Metadata ) > {
401
+ fn next ( & ' a mut self ) -> io:: Result < & Self :: Item > {
409
402
if !self . active {
410
403
// Enqueue all buffers once on stream start
411
404
for index in 0 ..self . arena . bufs . len ( ) {
@@ -421,9 +414,7 @@ where
421
414
422
415
// The index used to access the buffer elements is given to us by v4l2, so we assume it
423
416
// 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 ] )
427
418
}
428
419
}
429
420
@@ -442,8 +433,8 @@ where
442
433
// MetaData.bytesused is initialized to 0. For an output device, when bytesused is
443
434
// set to 0 v4l2 will set it to the size of the plane:
444
435
// 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 ;
447
438
448
439
if self
449
440
. handle
@@ -476,18 +467,17 @@ where
476
467
}
477
468
self . arena_index = v4l2_buf. index as usize ;
478
469
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 ;
486
476
487
477
Ok ( self . arena_index )
488
478
}
489
479
490
- fn next ( & ' a mut self ) -> io:: Result < ( & mut Self :: Item , & mut Metadata ) > {
480
+ fn next ( & ' a mut self ) -> io:: Result < & mut Self :: Item > {
491
481
let init = !self . active ;
492
482
if !self . active {
493
483
self . start ( ) ?;
@@ -503,8 +493,6 @@ where
503
493
504
494
// The index used to access the buffer elements is given to us by v4l2, so we assume it
505
495
// 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 ] )
509
497
}
510
498
}
0 commit comments