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
use dma_boom::DmaBuf;
use parking_lot::{ArcRwLockWriteGuard, RwLock, RawRwLock};
use std::fmt;
use std::{io, mem};
use std::ops;
use std::os::fd::{FromRawFd, AsRawFd};
use std::sync::Arc;
use tracing::error;

use crate::buffer;
use crate::device::Handle;
use crate::memory::Memory;
use crate::v4l2;
use crate::v4l_sys::{v4l2_format, v4l2_requestbuffers, v4l2_exportbuffer};

/// Implements all IO operations
pub struct Hardware;

impl Hardware {
    unsafe fn g_fmt(handle: &Handle, v4l2_fmt: &mut v4l2_format) -> Result<(), io::Error> {
        v4l2::ioctl(
            handle.as_raw_fd(),
            v4l2::vidioc::VIDIOC_G_FMT,
            v4l2_fmt as *mut _ as *mut std::os::raw::c_void,
        )
    }
    
    unsafe fn reqbufs(handle: &Handle, v4l2_reqbufs: &mut v4l2_requestbuffers) -> Result<(), io::Error> {
        v4l2::ioctl(
            handle.as_raw_fd(),
            v4l2::vidioc::VIDIOC_REQBUFS,
            v4l2_reqbufs as *mut _ as *mut std::os::raw::c_void,
        )
    }
    unsafe fn expbuf(handle: &Handle, v4l2_exportbuf: &mut v4l2_exportbuffer) -> Result<(), io::Error> {
        v4l2::ioctl(
            handle.as_raw_fd(),
            v4l2::vidioc::VIDIOC_EXPBUF,
            v4l2_exportbuf as *mut _ as *mut std::os::raw::c_void,
        )
    }
}

struct Common {
    handle: Arc<Handle>,
    buf_type: buffer::Type,
    _io: Hardware,
}

impl Common {
    fn requestbuffers_desc(&self) -> v4l2_requestbuffers {
        v4l2_requestbuffers {
            type_: self.buf_type as u32,
            ..unsafe { mem::zeroed() }
        }
    }

    /// Returns the number of buffers present in the arena (same as the number in `self.bufs`).
    fn allocate(&mut self, count: u32) -> io::Result<Vec<DmaBuf>> {
        // we need to get the maximum buffer size from the format first
        let mut v4l2_fmt = v4l2_format {
            type_: self.buf_type as u32,
            ..unsafe { mem::zeroed() }
        };

        unsafe { Hardware::g_fmt(&self.handle, &mut v4l2_fmt) }?;

        let mut v4l2_reqbufs = v4l2_requestbuffers {
            count,
            memory: Memory::Mmap as u32,
            ..self.requestbuffers_desc()
        };
        unsafe { Hardware::reqbufs(&self.handle, &mut v4l2_reqbufs) }?;

        let mut bufs = Vec::new();
        for index in 0..v4l2_reqbufs.count {
            let mut v4l2_exportbuf = v4l2_exportbuffer {
                index,
                type_: self.buf_type as u32,
                flags: libc::O_RDWR as _,
                ..unsafe { mem::zeroed() }
            };
            let buf = unsafe {
                Hardware::expbuf(&self.handle, &mut v4l2_exportbuf)?;
                DmaBuf::from_raw_fd(v4l2_exportbuf.fd)
            };
            bufs.push(buf);
        }
        
        let mut v4l2_reqbufs = v4l2_requestbuffers {
            count,
            memory: Memory::DmaBuf as u32,
            ..self.requestbuffers_desc()
        };
        // FIXME: how do the buffers above need to be cleaned up if this fails?
        unsafe { Hardware::reqbufs(&self.handle, &mut v4l2_reqbufs) }?;
        
        
        Ok(bufs)
    }
    
    fn release(&mut self) -> io::Result<()> {
        // free all buffers by requesting 0
        let mut v4l2_reqbufs = v4l2_requestbuffers {
            count: 0,
            memory: Memory::DmaBuf as u32,
            ..self.requestbuffers_desc()
        };
        unsafe { Hardware::reqbufs(&self.handle, &mut v4l2_reqbufs) }
    }
}

impl Drop for Common {
    fn drop(&mut self) {
        if let Err(e) = self.release() {
            if let Some(code) = e.raw_os_error() {
                // ENODEV means the file descriptor wrapped in the handle became invalid, most
                // likely because the device was unplugged or the connection (USB, PCI, ..)
                // broke down. Handle this case gracefully by ignoring it.
                if code == 19 {
                    /* ignore */
                    return;
                }
            }

            error!("DMABUF leak: {:?}", e);
        }
    }
}

/// Manage dmabuf buffers
///
/// All buffers are released in the Drop impl.
pub struct Arena {
    common: Common,
    bufs: Vec<DmaBuf>,
}

impl Arena {
    /// Returns a new buffer manager instance
    ///
    /// You usually do not need to use this directly.
    /// A UserBufferStream creates its own manager instance by default.
    ///
    /// # Arguments
    ///
    /// * `handle` - Device handle to get its file descriptor
    /// * `buf_type` - Type of the buffers
    pub fn new(handle: Arc<Handle>, buf_type: buffer::Type) -> Self {
        Self {
            common: Common {
                handle,
                buf_type,
                _io: Hardware,
            },
            bufs: Vec::new(),
        }
    }
    
    /// Returns the number of allocated buffers
    pub fn allocate(&mut self, count: u32) -> io::Result<usize> {
        self.bufs = self.common.allocate(count)?;
        Ok(self.bufs.len())
    }
    
    pub fn len(&self) -> usize {
        self.bufs.len()
    }
}


impl Arena {
    /// Returns the buffer for this index
    pub fn get_dmabuf(&mut self, index: usize) -> Result<&DmaBuf, &'static str> {
        self.bufs.get(index)
            .ok_or("Index higher than available buffers")
    }
    
    /// Returns the buffer for this index
    pub fn get_dmabuf_mut(&mut self, index: usize) -> Result<&mut DmaBuf, &'static str> {
        self.bufs.get_mut(index)
            .ok_or("Index higher than available buffers")
    }
}

/// A DMA-BUF buffer which is handed out to the user as if owned, but cannot be accessed while it's enqueued at the device for writing.
///
/// Watch out for deadlocks!
///
/// ```no_run
/// use v4l::io::dmabuf::DmaBufProtected;
/// use v4l::dummy;
///
/// let buf: DmaBufProtected = dummy::new_stub();
/// let guard = buf.read();
/// let mmap = guard.memory_map_ro();
/// ```
pub type DmaBufProtected = BufProtected<DmaBuf>;

pub struct BufProtected<T>(Arc<RwLock<T>>);

impl<T> Clone for BufProtected<T> {
    fn clone(&self) -> Self {
        Self(self.0.clone())
    }
}

impl<T> ops::Deref for BufProtected<T> {
    type Target = Arc<RwLock<T>>;
    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl<T> fmt::Debug for BufProtected<T> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_tuple("BufProtected")
            .field(&self.0.data_ptr())
            .finish()
    }
}

trait UniqueId {
    type Target;
    fn unique_id(&self) -> Self::Target;
}

impl<T> UniqueId for RwLock<T> {
    type Target = *mut T;
    fn unique_id(&self) -> *mut T {
        self.data_ptr()
    }
}

pub struct LockedBuffer<T> {
    /// This serves as a stable identifier of a buffer corresponding to this slot, meaning a buffer with this ID has the same index known by the kernel and the same device.
    id: *mut T,
    /// The buffer which may be handed out to the user after it's ready.
    /// RwLock locked for writing ensures no other reference can try to use it while it's waiting to be filled.
    buf: Option<ArcRwLockWriteGuard<RawRwLock, T>>,
}

impl<T> From<T> for LockedBuffer<T> {
    fn from(value: T) -> Self {
        let buf = Arc::new(RwLock::new(value));
        Self {
            id: buf.data_ptr(),
            buf: Some(buf.write_arc()),
        }
    }
}


impl<T> fmt::Debug for LockedBuffer<T> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_tuple("LockedBuffer")
            .field(&self.id)
            .field(&self.buf.as_ref().map(|_| ()))
            .finish()
    }
}

/// Doesn't cleanup buffers in any way, so you have to ensure that
struct BufferSwapper<T>(Vec<LockedBuffer<T>>);

impl<T> BufferSwapper<T> {
    fn new(bufs: Vec<T>) ->Self {
        Self(bufs.into_iter().map(LockedBuffer::from).collect())
    }
    
    fn len(&self) -> usize {
        self.0.len()
    }

    fn all_locked(&self) -> bool {
        self.0.iter()
            .find(|LockedBuffer { buf, .. }| buf.is_none())
            .is_none()
    }
    
    /// Finds the kernel index for this buffer and places it there.
    /// Doesn't check if the buffer was there already. Returns the kernel index.
    pub fn replace_buffer(&mut self, buf: BufProtected<T>) -> Result<usize, (BufProtected<T>, &'static str)> {
        let index = self.0.iter().position(|entry| entry.id == buf.0.unique_id());

        match index {
            None => Err((buf, "Buffer not from this stream")),
            Some(index) => {
                self.0[index] = LockedBuffer {
                    id: buf.0.unique_id(),
                    buf: Some(buf.0.write_arc()),
                };
                Ok(index)
            }
        }
    }
    
    /// Removes and returns buffer at this kernel index.
    pub fn take_buffer(&mut self, index: usize) -> Result<BufProtected<T>, &'static str> {
        let rw_buf = self.0.get_mut(index)
            .ok_or("Index higher than available buffers")?
            .buf.take()
            .ok_or("No buffer was stored at this index")?;

        Ok(BufProtected(ArcRwLockWriteGuard::rwlock(&rw_buf).clone()))
    }

    /// Returns a writeably-locked buffer for this index
    pub fn get_buf_mut(&mut self, index: usize) -> Result<&mut T, &'static str> {
        Ok(
            self.0.get_mut(index)
                .ok_or("Index higher than available buffers")?
                .buf.as_mut()
                .ok_or("No buffer was stored at this index")?
        )
    }
}

/// Allows the user to manage buffers manually. Buffers must be explicitly replaced. Release will only succeed if all buffers were returned with replace_buffer.
pub struct ManuallyManaged {
    common: Common,
    bufs: BufferSwapper<DmaBuf>,
}


impl ManuallyManaged {
    /// Returns a new buffer manager instance
    ///
    /// You usually do not need to use this directly.
    /// A UserBufferStream creates its own manager instance by default.
    ///
    /// # Arguments
    ///
    /// * `handle` - Device handle to get its file descriptor
    /// * `buf_type` - Type of the buffers
    pub fn new(handle: Arc<Handle>, buf_type: buffer::Type, count: usize) -> io::Result<Self> {
        let mut common = Common {
            handle,
            buf_type,
            _io: Hardware,
        };
        Ok(Self {
            bufs: BufferSwapper::new(common.allocate(count as u32)?),
            common,
        })
    }
    
    pub fn len(&self) -> usize {
        self.bufs.len()
    }
    
    pub fn take_buffer(&mut self, index:usize)
        -> Result<BufProtected<DmaBuf>, &'static str>
    {
        self.bufs.take_buffer(index)
    }
    
    pub fn replace_buffer(&mut self, buf: BufProtected<DmaBuf>)
        -> Result<usize, (BufProtected<DmaBuf>, &'static str)>
    {
        self.bufs.replace_buffer(buf)
    }
    
    /// Returns a writeably-locked buffer for this index
    pub fn get_buf_mut(&mut self, index: usize) -> Result<&mut DmaBuf, &'static str> {
        self.bufs.get_buf_mut(index)
    }
}

impl ManuallyManaged {
    /// Release buffers without checking if any buffers are gone missing.
    ///
    /// What makes this unsafe is that the buffers out there may become unuseable (TODO: check semantics).
    /// See [V4L2_BUF_CAP_SUPPORTS_ORPHANED_BUFS](https://docs.kernel.org/userspace-api/media/v4l/vidioc-reqbufs.html#description).
    pub unsafe fn force_release(&mut self) -> io::Result<()> {
        self.common.release()
    }

    /// Safely release buffers
    pub fn release(&mut self) -> io::Result<()> {
        if !self.bufs.all_locked() {
            return Err(io::Error::other("Busy: not all buffers were returned"));
        }
        
        unsafe { self.force_release() }
    }
}

impl Drop for ManuallyManaged {
    fn drop(&mut self) {
        // This exists just to give user the error message before giving up
        if let Err(e) = self.release() {
            if let Some(code) = e.raw_os_error() {
                // ENODEV means the file descriptor wrapped in the handle became invalid, most
                // likely because the device was unplugged or the connection (USB, PCI, ..)
                // broke down. Handle this case gracefully by ignoring it.
                if code == 19 {
                    /* ignore */
                    return;
                }
            }

            error!("DMABUF leak: {:?}", e);
        }
    }
}


#[cfg(test)]
mod test {
    use super::*;

    /// The buffer handed out to the user must not be already locked
    #[test]
    fn return_unlocked() {
        let mut arena = BufferSwapper::new((0..4).collect());
        
        let b = arena.take_buffer(0).unwrap();
        assert!(b.is_locked() == false);
        
        arena.replace_buffer(b.clone()).unwrap();
        assert!(b.is_locked() == true);
        let b = arena.take_buffer(1).unwrap();
        assert!(b.is_locked() == false);
        
        arena.replace_buffer(b.clone()).unwrap();
        assert!(b.is_locked() == true);
        let b = arena.take_buffer(2).unwrap();
        assert!(b.is_locked() == false);
        
        arena.replace_buffer(b.clone()).unwrap();
        assert!(b.is_locked() == true);
        let b = arena.take_buffer(3).unwrap();
        assert!(b.is_locked() == false);
    }
}