v4l2r 0.0.7

Safe and flexible abstraction over V4L2
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
//! High-level interface for a [V4L2 video
//! encoder](https://www.kernel.org/doc/html/latest/userspace-api/media/v4l/dev-encoder.html).
use crate::{
    device::{
        poller::{DeviceEvent, PollError, PollEvent, Poller, Waker},
        queue::{
            direction::{Capture, Output},
            dqbuf::DqBuffer,
            handles_provider::HandlesProvider,
            BuffersAllocated, CanceledBuffer, CaptureQueueable, CreateQueueError, FormatBuilder,
            GetCaptureBufferByIndex, GetFreeBufferError, GetFreeCaptureBuffer, GetFreeOutputBuffer,
            OutputQueueableProvider, Queue, QueueInit, RequestBuffersError,
        },
        AllocatedQueue, Device, DeviceConfig, DeviceOpenError, Stream, TryDequeue,
    },
    ioctl::{
        self, DqBufError, DqBufIoctlError, EncoderCommand, FormatFlags, GFmtError,
        V4l2BufferFromError,
    },
    memory::{BufferHandles, PrimitiveBufferHandles},
    Format,
};

use log::warn;
use std::{
    any::Any,
    io,
    path::Path,
    sync::{atomic::AtomicUsize, Arc},
    task::Wake,
    thread::JoinHandle,
};
use thiserror::Error;

/// Trait implemented by all states of the encoder.
pub trait EncoderState {}

pub struct Encoder<S: EncoderState> {
    // Make sure to keep the device alive as long as we are.
    device: Arc<Device>,
    state: S,
}

pub struct AwaitingCaptureFormat {
    output_queue: Queue<Output, QueueInit>,
    capture_queue: Queue<Capture, QueueInit>,
}
impl EncoderState for AwaitingCaptureFormat {}

#[derive(Debug, Error)]
pub enum EncoderOpenError {
    #[error("error while opening device")]
    DeviceOpenError(#[from] DeviceOpenError),
    #[error("error while creating queue")]
    CreateQueueError(#[from] CreateQueueError),
    #[error("specified device is not an encoder")]
    NotAnEncoder,
}

impl Encoder<AwaitingCaptureFormat> {
    pub fn open(path: &Path) -> Result<Self, EncoderOpenError> {
        let config = DeviceConfig::new().non_blocking_dqbuf();
        let device = Arc::new(Device::open(path, config)?);

        // Check that the device is indeed an encoder.
        let capture_queue = Queue::get_capture_mplane_queue(device.clone())?;
        let output_queue = Queue::get_output_mplane_queue(device.clone())?;

        // On an encoder, the OUTPUT formats are not compressed, but the CAPTURE ones are.
        // Return an error if our device does not satisfy these conditions.
        output_queue
            .format_iter()
            .find(|fmt| !fmt.flags.contains(FormatFlags::COMPRESSED))
            .and(
                capture_queue
                    .format_iter()
                    .find(|fmt| fmt.flags.contains(FormatFlags::COMPRESSED)),
            )
            .ok_or(EncoderOpenError::NotAnEncoder)
            .map(|_| ())?;

        Ok(Encoder {
            device,
            state: AwaitingCaptureFormat {
                output_queue,
                capture_queue,
            },
        })
    }

    pub fn set_capture_format<F>(mut self, f: F) -> anyhow::Result<Encoder<AwaitingOutputFormat>>
    where
        F: FnOnce(FormatBuilder) -> anyhow::Result<()>,
    {
        let builder = self.state.capture_queue.change_format()?;
        f(builder)?;

        Ok(Encoder {
            device: self.device,
            state: AwaitingOutputFormat {
                output_queue: self.state.output_queue,
                capture_queue: self.state.capture_queue,
            },
        })
    }
}

pub struct AwaitingOutputFormat {
    output_queue: Queue<Output, QueueInit>,
    capture_queue: Queue<Capture, QueueInit>,
}
impl EncoderState for AwaitingOutputFormat {}

impl Encoder<AwaitingOutputFormat> {
    pub fn set_output_format<F>(mut self, f: F) -> anyhow::Result<Encoder<AwaitingOutputBuffers>>
    where
        F: FnOnce(FormatBuilder) -> anyhow::Result<()>,
    {
        let builder = self.state.output_queue.change_format()?;
        f(builder)?;

        Ok(Encoder {
            device: self.device,
            state: AwaitingOutputBuffers {
                output_queue: self.state.output_queue,
                capture_queue: self.state.capture_queue,
            },
        })
    }
}

pub struct AwaitingOutputBuffers {
    output_queue: Queue<Output, QueueInit>,
    capture_queue: Queue<Capture, QueueInit>,
}
impl EncoderState for AwaitingOutputBuffers {}

impl Encoder<AwaitingOutputBuffers> {
    pub fn allocate_output_buffers_generic<OP: BufferHandles>(
        self,
        memory_type: OP::SupportedMemoryType,
        num_output: usize,
    ) -> Result<Encoder<AwaitingCaptureBuffers<OP>>, RequestBuffersError> {
        Ok(Encoder {
            device: self.device,
            state: AwaitingCaptureBuffers {
                output_queue: self
                    .state
                    .output_queue
                    .request_buffers_generic::<OP>(memory_type, num_output as u32)?,
                capture_queue: self.state.capture_queue,
            },
        })
    }

    pub fn allocate_output_buffers<OP: PrimitiveBufferHandles>(
        self,
        num_output: usize,
    ) -> Result<Encoder<AwaitingCaptureBuffers<OP>>, RequestBuffersError> {
        self.allocate_output_buffers_generic(OP::MEMORY_TYPE, num_output)
    }

    pub fn get_output_format(&self) -> Result<Format, GFmtError> {
        self.state.output_queue.get_format()
    }

    pub fn get_capture_format(&self) -> Result<Format, GFmtError> {
        self.state.capture_queue.get_format()
    }
}

pub struct AwaitingCaptureBuffers<OP: BufferHandles> {
    output_queue: Queue<Output, BuffersAllocated<OP>>,
    capture_queue: Queue<Capture, QueueInit>,
}
impl<OP: BufferHandles> EncoderState for AwaitingCaptureBuffers<OP> {}

impl<OP: BufferHandles> Encoder<AwaitingCaptureBuffers<OP>> {
    pub fn allocate_capture_buffers_generic<P: HandlesProvider>(
        self,
        memory_type: <P::HandleType as BufferHandles>::SupportedMemoryType,
        num_capture: usize,
        capture_memory_provider: P,
    ) -> Result<Encoder<ReadyToEncode<OP, P>>, RequestBuffersError> {
        Ok(Encoder {
            device: self.device,
            state: ReadyToEncode {
                output_queue: self.state.output_queue,
                capture_queue: self
                    .state
                    .capture_queue
                    .request_buffers_generic::<P::HandleType>(memory_type, num_capture as u32)?,
                capture_memory_provider,
                poll_wakeups_counter: None,
            },
        })
    }

    pub fn allocate_capture_buffers<P: HandlesProvider>(
        self,
        num_capture: usize,
        capture_memory_provider: P,
    ) -> Result<Encoder<ReadyToEncode<OP, P>>, RequestBuffersError>
    where
        P::HandleType: PrimitiveBufferHandles,
    {
        self.allocate_capture_buffers_generic(
            P::HandleType::MEMORY_TYPE,
            num_capture,
            capture_memory_provider,
        )
    }
}

pub struct ReadyToEncode<OP: BufferHandles, P: HandlesProvider> {
    output_queue: Queue<Output, BuffersAllocated<OP>>,
    capture_queue: Queue<Capture, BuffersAllocated<P::HandleType>>,
    capture_memory_provider: P,
    poll_wakeups_counter: Option<Arc<AtomicUsize>>,
}
impl<OP: BufferHandles, P: HandlesProvider> EncoderState for ReadyToEncode<OP, P> {}

impl<OP: BufferHandles, P: HandlesProvider> Encoder<ReadyToEncode<OP, P>>
where
    for<'a> Queue<Capture, BuffersAllocated<P::HandleType>>:
        GetFreeCaptureBuffer<'a, P::HandleType> + GetCaptureBufferByIndex<'a, P::HandleType>,
{
    pub fn set_poll_counter(mut self, poll_wakeups_counter: Arc<AtomicUsize>) -> Self {
        self.state.poll_wakeups_counter = Some(poll_wakeups_counter);
        self
    }

    pub fn start<InputDoneCb, OutputReadyCb>(
        self,
        input_done_cb: InputDoneCb,
        output_ready_cb: OutputReadyCb,
    ) -> io::Result<Encoder<Encoding<OP, P, InputDoneCb, OutputReadyCb>>>
    where
        InputDoneCb: Fn(CompletedOutputBuffer<OP>),
        OutputReadyCb: FnMut(DqBuffer<Capture, P::HandleType>) + Send + 'static,
    {
        self.state.output_queue.stream_on().unwrap();
        self.state.capture_queue.stream_on().unwrap();

        let mut output_poller = Poller::new(Arc::clone(&self.device))?;
        output_poller.enable_event(DeviceEvent::OutputReady)?;

        let mut encoder_thread = EncoderThread::new(
            &self.device,
            self.state.capture_queue,
            self.state.capture_memory_provider,
            output_ready_cb,
        )?;

        if let Some(counter) = &self.state.poll_wakeups_counter {
            output_poller.set_poll_counter(Arc::clone(counter));
            encoder_thread.set_poll_counter(Arc::clone(counter));
        }

        let handle = std::thread::Builder::new()
            .name("V4L2 Encoder".into())
            .spawn(move || encoder_thread.run())?;

        Ok(Encoder {
            device: self.device,
            state: Encoding {
                output_queue: self.state.output_queue,
                input_done_cb,
                output_poller,
                handle,
            },
        })
    }
}

pub struct Encoding<OP: BufferHandles, P, InputDoneCb, OutputReadyCb>
where
    P: HandlesProvider,
    InputDoneCb: Fn(CompletedOutputBuffer<OP>),
    OutputReadyCb: FnMut(DqBuffer<Capture, P::HandleType>) + Send,
{
    output_queue: Queue<Output, BuffersAllocated<OP>>,
    input_done_cb: InputDoneCb,
    output_poller: Poller,

    handle: JoinHandle<EncoderThread<P, OutputReadyCb>>,
}
impl<OP, P, InputDoneCb, OutputReadyCb> EncoderState for Encoding<OP, P, InputDoneCb, OutputReadyCb>
where
    OP: BufferHandles,
    P: HandlesProvider,
    InputDoneCb: Fn(CompletedOutputBuffer<OP>),
    OutputReadyCb: FnMut(DqBuffer<Capture, P::HandleType>) + Send,
{
}

// Safe because all Rcs are internal and never leaked outside of the struct.
unsafe impl<S: EncoderState> Send for Encoder<S> {}

pub enum CompletedOutputBuffer<OP: BufferHandles> {
    Dequeued(DqBuffer<Output, OP>),
    Canceled(CanceledBuffer<OP>),
}

#[derive(Debug, Error)]
pub enum GetBufferError {
    #[error("error while dequeueing buffer")]
    DequeueError(#[from] DqBufError<V4l2BufferFromError>),
    #[error("error during poll")]
    PollError(#[from] PollError),
    #[error("error while obtaining buffer")]
    GetFreeBufferError(#[from] GetFreeBufferError),
}

#[derive(Debug, Error)]
pub enum EncoderStopError {
    #[error("error while sending STOP command")]
    EncoderCmdError(#[from] ioctl::EncoderCmdError),
    #[error("thread has panicked")]
    ThreadPanickedError(Box<dyn Any + Send + 'static>),
    #[error("cannot streamoff capture queue")]
    CaptureQueueStreamoffError(ioctl::StreamOffError),
    #[error("cannot streamoff output queue")]
    OutputQueueStreamoffError(ioctl::StreamOffError),
}

impl<OP, P, InputDoneCb, OutputReadyCb> Encoder<Encoding<OP, P, InputDoneCb, OutputReadyCb>>
where
    OP: BufferHandles,
    P: HandlesProvider,
    InputDoneCb: Fn(CompletedOutputBuffer<OP>),
    OutputReadyCb: FnMut(DqBuffer<Capture, P::HandleType>) + Send,
{
    /// Stop the encoder, and returns the encoder ready to be started again.
    pub fn stop(self) -> Result<Encoder<ReadyToEncode<OP, P>>, EncoderStopError> {
        ioctl::encoder_cmd::<_, ()>(&*self.device, &EncoderCommand::Stop(false))?;

        // The encoder thread should receive the LAST buffer and exit on its own.
        let encoding_thread = self
            .state
            .handle
            .join()
            .map_err(EncoderStopError::ThreadPanickedError)?;

        encoding_thread
            .capture_queue
            .stream_off()
            .map_err(EncoderStopError::CaptureQueueStreamoffError)?;
        /* Return all canceled buffers to the client */
        let canceled_buffers = self
            .state
            .output_queue
            .stream_off()
            .map_err(EncoderStopError::OutputQueueStreamoffError)?;
        for buffer in canceled_buffers {
            (self.state.input_done_cb)(CompletedOutputBuffer::Canceled(buffer));
        }

        Ok(Encoder {
            device: self.device,
            state: ReadyToEncode {
                output_queue: self.state.output_queue,
                capture_queue: encoding_thread.capture_queue,
                capture_memory_provider: encoding_thread.capture_memory_provider,
                poll_wakeups_counter: None,
            },
        })
    }

    /// Attempts to dequeue and release output buffers that the driver is done with.
    fn dequeue_output_buffers(&self) -> Result<(), DqBufError<V4l2BufferFromError>> {
        let output_queue = &self.state.output_queue;

        while output_queue.num_queued_buffers() > 0 {
            match output_queue.try_dequeue() {
                Ok(buf) => {
                    (self.state.input_done_cb)(CompletedOutputBuffer::Dequeued(buf));
                }
                Err(DqBufError::IoctlError(DqBufIoctlError::NotReady)) => break,
                // TODO buffers with the error flag set should not result in
                // a fatal error!
                Err(e) => return Err(e),
            }
        }

        Ok(())
    }

    // Make this thread sleep until at least one OUTPUT buffer is ready to be
    // obtained through `try_get_buffer()`, dequeuing buffers if necessary.
    fn wait_for_output_buffer(&mut self) -> Result<(), GetBufferError> {
        for event in self.state.output_poller.poll(None)? {
            match event {
                PollEvent::Device(DeviceEvent::OutputReady) => {
                    self.dequeue_output_buffers()?;
                }
                _ => panic!("Unexpected return from OUTPUT queue poll!"),
            }
        }

        Ok(())
    }
}

impl<'a, OP, P, InputDoneCb, OutputReadyCb> OutputQueueableProvider<'a, OP>
    for Encoder<Encoding<OP, P, InputDoneCb, OutputReadyCb>>
where
    Queue<Output, BuffersAllocated<OP>>: OutputQueueableProvider<'a, OP>,
    OP: BufferHandles,
    P: HandlesProvider,
    InputDoneCb: Fn(CompletedOutputBuffer<OP>),
    OutputReadyCb: FnMut(DqBuffer<Capture, P::HandleType>) + Send,
{
    type Queueable =
        <Queue<Output, BuffersAllocated<OP>> as OutputQueueableProvider<'a, OP>>::Queueable;
}

/// Let the encoder provide the buffers from the OUTPUT queue.
impl<'a, OP, P, InputDoneCb, OutputReadyCb> GetFreeOutputBuffer<'a, OP, GetBufferError>
    for Encoder<Encoding<OP, P, InputDoneCb, OutputReadyCb>>
where
    Queue<Output, BuffersAllocated<OP>>: GetFreeOutputBuffer<'a, OP>,
    OP: BufferHandles,
    P: HandlesProvider,
    InputDoneCb: Fn(CompletedOutputBuffer<OP>),
    OutputReadyCb: FnMut(DqBuffer<Capture, P::HandleType>) + Send,
{
    /// Returns a V4L2 buffer to be filled with a frame to encode if one
    /// is available.
    ///
    /// This method will return None immediately if all the allocated buffers
    /// are currently queued.
    fn try_get_free_buffer(&'a self) -> Result<Self::Queueable, GetBufferError> {
        self.dequeue_output_buffers()?;
        Ok(self.state.output_queue.try_get_free_buffer()?)
    }
}

// If `GetFreeBuffer` is implemented, we can also provide a blocking `get_buffer`
// method.
impl<'a, OP, P, InputDoneCb, OutputReadyCb> Encoder<Encoding<OP, P, InputDoneCb, OutputReadyCb>>
where
    Self: GetFreeOutputBuffer<'a, OP, GetBufferError>,
    OP: BufferHandles,
    P: HandlesProvider,
    InputDoneCb: Fn(CompletedOutputBuffer<OP>),
    OutputReadyCb: FnMut(DqBuffer<Capture, P::HandleType>) + Send,
{
    /// Returns a V4L2 buffer to be filled with a frame to encode, waiting for
    /// one to be available if needed.
    ///
    /// Contrary to `try_get_free_buffer(), this method will wait for a buffer
    /// to be available if needed.
    pub fn get_buffer(
        &'a mut self,
    ) -> Result<<Self as OutputQueueableProvider<'a, OP>>::Queueable, GetBufferError> {
        let output_queue = &self.state.output_queue;

        // If all our buffers are queued, wait until we can dequeue some.
        if output_queue.num_queued_buffers() == output_queue.num_buffers() {
            self.wait_for_output_buffer()?;
        }

        self.try_get_free_buffer()
    }
}

struct EncoderThread<P, OutputReadyCb>
where
    P: HandlesProvider,
    OutputReadyCb: FnMut(DqBuffer<Capture, P::HandleType>) + Send,
{
    capture_queue: Queue<Capture, BuffersAllocated<P::HandleType>>,
    capture_memory_provider: P,
    poller: Poller,
    waker: Arc<Waker>,
    output_ready_cb: OutputReadyCb,
}

impl<P, OutputReadyCb> EncoderThread<P, OutputReadyCb>
where
    P: HandlesProvider,
    OutputReadyCb: FnMut(DqBuffer<Capture, P::HandleType>) + Send,
    for<'a> Queue<Capture, BuffersAllocated<P::HandleType>>:
        GetFreeCaptureBuffer<'a, P::HandleType> + GetCaptureBufferByIndex<'a, P::HandleType>,
{
    fn new(
        device: &Arc<Device>,
        capture_queue: Queue<Capture, BuffersAllocated<P::HandleType>>,
        capture_memory_provider: P,
        output_ready_cb: OutputReadyCb,
    ) -> io::Result<Self> {
        let mut poller = Poller::new(Arc::clone(device))?;

        poller.enable_event(DeviceEvent::CaptureReady)?;
        let waker = poller.add_waker(0)?;

        Ok(EncoderThread {
            capture_queue,
            capture_memory_provider,
            poller,
            waker,
            output_ready_cb,
        })
    }

    fn set_poll_counter(&mut self, poll_wakeups_counter: Arc<AtomicUsize>) {
        self.poller.set_poll_counter(poll_wakeups_counter);
    }

    fn run(mut self) -> Self {
        self.enqueue_capture_buffers();

        'polling: loop {
            match self.capture_queue.num_queued_buffers() {
                // If there are no buffers on the CAPTURE queue, poll() will return
                // immediately with EPOLLERR and we would loop indefinitely.
                // Prevent this by temporarily disabling polling the device in such
                // cases.
                0 => {
                    self.poller
                        .disable_event(DeviceEvent::CaptureReady)
                        .unwrap();
                }
                // If device polling was disabled and we have buffers queued, we
                // can reenable it as poll will now wait for a CAPTURE buffer to
                // be ready for dequeue.
                _ => {
                    self.poller.enable_event(DeviceEvent::CaptureReady).unwrap();
                }
            }

            // TODO handle errors - this system call can be interrupted and we
            // should leave in this case.
            for event in self.poller.poll(None).unwrap() {
                match event {
                    // A CAPTURE buffer has been released by the client.
                    PollEvent::Waker(0) => {
                        // Requeue all available CAPTURE buffers.
                        self.enqueue_capture_buffers();
                    }
                    // A CAPTURE buffer is ready to be dequeued.
                    PollEvent::Device(DeviceEvent::CaptureReady) => {
                        // Get the encoded buffer
                        // TODO Manage errors here, including corrupted buffers!
                        if let Ok(mut cap_buf) = self.capture_queue.try_dequeue() {
                            let is_last = cap_buf.data.is_last();
                            let is_empty = *cap_buf.data.get_first_plane().bytesused == 0;

                            // Add a drop callback to the dequeued buffer so we
                            // re-queue it as soon as it is dropped.
                            let cap_waker = Arc::clone(&self.waker);
                            cap_buf.add_drop_callback(move |_dqbuf| {
                                cap_waker.wake();
                            });

                            // Empty buffers do not need to be passed to the client.
                            if !is_empty {
                                (self.output_ready_cb)(cap_buf);
                            }

                            // Last buffer of the stream? Time for us to terminate.
                            if is_last {
                                break 'polling;
                            }
                        } else {
                            // TODO we should not crash here.
                            panic!("Expected a CAPTURE buffer but none available!");
                        }
                    }
                    _ => panic!("Unexpected return from CAPTURE queue poll!"),
                }
            }
        }

        self
    }

    fn enqueue_capture_buffers(&mut self) {
        'enqueue: while let Some(handles) = self.capture_memory_provider.get_handles(&self.waker) {
            if let Ok(buffer) = self
                .capture_memory_provider
                .get_suitable_buffer_for(&handles, &self.capture_queue)
            {
                buffer.queue_with_handles(handles).unwrap();
            } else {
                warn!("Handles potentially lost due to no V4L2 buffer being available");
                break 'enqueue;
            }
        }
    }
}