v4l2loopback 0.1.0

Safe bindings to v4l2loopback
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
//! Safe binding to interact with [v4l2loopback].
//!
//! [v4l2loopback] allows your to manage virtual video devices on linux.
//! Application are able to pass video content to those devices, and this video feed will be
//! available as a camera.
//!
//! <div class="example-wrap" style="display:inline-block">
//! <pre class="compile_fail" style="white-space:normal;font:inherit;">
//!
//! **⚠️ Warning**:
//!
//! > This crate is based on an unreleased version of [v4l2loopback].
//! > To use this crate you will need to build and install [v4l2loopback] from source.
//! > (If you are on archlinux, you can install [v4l2loopback-dkms-git] from the aur)
//!
//! </pre>
//! </div>
//!
//! # Usage
//!
//! ```
//! use std::path::Path;
//! use v4l2loopback::{add_device, delete_device, query_device, DeviceConfig};
//!
//! // Device configuration
//! // Here you declare informations about the camera device that will be created.
//! // It should be matching the content that you will want to pass through
//! let device_config = DeviceConfig {
//!     label: "Test Device".to_string(),
//!     min_width: 100,
//!     max_width: 4000,
//!     min_height: 100,
//!     max_height: 4000,
//!     max_buffers: 9,
//!     max_openers: 3,
//! };
//! // Create a device
//! let device_num =
//!     add_device(None, device_config.clone()).expect("Error when creating the device");
//!
//! // Querying informations about a device
//! // This returns the matchin device's configuration
//! let cfg =
//!     query_device(device_num).expect("Error when querying the device");
//!
//! // When you are done with you processing, don't forget to delete the device, otherwise the
//! // device `/dev/videoN` will not be removed.
//! delete_device(device_num).expect("Error when removing device");
//! assert!(!Path::new(&format!("/dev/video{}", device_num)).exists());
//! ```
//!
//! [v4l2loopback]: https://github.com/umlaeute/v4l2loopback
//! [v4l2loopback-dkms-git]: https://aur.archlinux.org/packages/v4l2loopback-dkms-git

use std::{
    ffi::{CStr, CString},
    fs::OpenOptions,
    io::ErrorKind,
    os::fd::{IntoRawFd, RawFd},
    slice::from_raw_parts,
};

use nix::{errno::Errno, ioctl_read_bad, ioctl_readwrite_bad, ioctl_write_int_bad};
use thiserror::Error;

mod ffi {
    #![allow(non_upper_case_globals)]
    #![allow(non_camel_case_types)]
    #![allow(non_snake_case)]

    include!(concat!(env!("OUT_DIR"), "/v4l2loopback.rs"));

    impl Default for v4l2_loopback_config {
        fn default() -> Self {
            Self {
                output_nr: -1,
                card_label: [0; 32],
                min_width: 0,
                max_width: 0,
                min_height: 0,
                max_height: 0,
                max_buffers: 0,
                max_openers: 0,
                announce_all_caps: 0,

                unused: 0,
                debug: 0,
            }
        }
    }
}

pub use ffi::V4L2LOOPBACK_VERSION_BUGFIX;
pub use ffi::V4L2LOOPBACK_VERSION_MAJOR;
pub use ffi::V4L2LOOPBACK_VERSION_MINOR;

/// Wrapper type describing a v4l2loopback device.
#[derive(Debug, Default, PartialEq, Eq, Clone)]
pub struct DeviceConfig {
    /// A nice name for you device.
    /// If empty, v4l2loopback will choose a generic name
    pub label: String,

    /// Allowed minimum frame witdh.
    /// Setting this value below 48 as no effets as it is the minimal accepted value.
    pub min_width: u32,
    /// Allowed maximum frame witdh.
    /// Setting this value above 8192 as no effets as it is the maximal accepted value.
    pub max_width: u32,
    /// Allowed minimum frame height.
    /// Setting this value below 32 as no effets as it is the minimal accepted value.
    pub min_height: u32,
    /// Allowed maximum frame height.
    /// Setting this value above 8192 as no effets as it is the maximal accepted value.
    pub max_height: u32,

    /// Number of buffers to allocate for the queue.
    /// If 0, then a default value is picked by v4l2loopback.
    pub max_buffers: u32,

    /// How many consumers are allowed to open this device concurrently.
    /// If 0, then a default value is picked by v4l2loopback.
    pub max_openers: u32,
}

impl TryInto<ffi::v4l2_loopback_config> for DeviceConfig {
    type Error = Box<dyn std::error::Error>;

    fn try_into(self) -> Result<ffi::v4l2_loopback_config, Self::Error> {
        let mut cfg = ffi::v4l2_loopback_config::default();

        let mut slice: [i8; 32] = [0; 32];
        unsafe { from_raw_parts(CString::new(self.label)?.as_ptr(), 32) }
            .iter()
            .enumerate()
            .for_each(|(i, v)| slice[i] = *v);
        cfg.card_label = slice;

        cfg.min_width = self.min_width;
        cfg.max_width = self.max_width;
        cfg.min_height = self.min_height;
        cfg.max_height = self.max_height;
        cfg.max_buffers = self.max_buffers.try_into()?;
        cfg.max_openers = self.max_openers.try_into()?;

        Ok(cfg)
    }
}

impl TryFrom<ffi::v4l2_loopback_config> for DeviceConfig {
    type Error = Box<dyn std::error::Error>;

    fn try_from(value: ffi::v4l2_loopback_config) -> Result<Self, Self::Error> {
        let ffi::v4l2_loopback_config {
            output_nr: _,
            unused: _,
            card_label,
            min_width,
            max_width,
            min_height,
            max_height,
            max_buffers,
            max_openers,
            debug: _,
            announce_all_caps: _,
        } = value;

        let label = unsafe { CStr::from_ptr(card_label.as_ptr()) }
            .to_str()?
            .to_string();

        Ok(Self {
            label,
            min_width,
            max_width,
            min_height,
            max_height,
            max_buffers: max_buffers.try_into()?,
            max_openers: max_openers.try_into()?,
        })
    }
}

/// Error generated when accessing the control device fails
///
/// The control device usually is `/dev/v4l2loopback`.
#[derive(Debug, Error)]
pub enum ControlDeviceError {
    /// You don't have permissions to open the control device.
    /// Your may require root permissions.
    #[error("You don't have the right permissions")]
    PermissionDenied,

    /// The control device couldn't be found.
    /// Verify if the kernel module is properly loaded.
    #[error("Can't find control device /dev/v4l2loopback, check if the kernel module is properly loaded")]
    NotFound,

    /// An error resulting from trying to access the control device.
    #[error("Error when opening the control device: {0}")]
    Other(Box<dyn std::error::Error>),
}

fn open_control_device() -> Result<RawFd, ControlDeviceError> {
    match OpenOptions::new().read(true).open("/dev/v4l2loopback") {
        Ok(f) => Ok(f.into_raw_fd()),
        Err(e) => match e.kind() {
            ErrorKind::NotFound => Err(ControlDeviceError::NotFound),
            ErrorKind::PermissionDenied => Err(ControlDeviceError::PermissionDenied),
            _ => Err(ControlDeviceError::Other(Box::new(e))),
        },
    }
}

/// Error which can occure when calling a function from this crate
#[derive(Debug, Error)]
pub enum Error {
    /// An error occured when accessing the control device.
    /// See [`ControlDeviceError`] for more details
    #[error("Couldn't open control device: {0}")]
    ControlDevice(#[from] ControlDeviceError),

    /// An error resulting from an ioctl function call
    #[error("Error returned from ioctl: {0}")]
    Ioctl(#[from] Errno),

    /// Unable to create a device
    #[error("Failed to create device")]
    DeviceCreationFailed,

    /// Couldn't find the specified device
    #[error("Device /dev/video{0} not found")]
    DeviceNotFound(u32),

    /// Unable to properly convert the config.
    ///
    /// Something went wrong when converting a [`DeviceConfig`] from/to the v4l2loopback device
    /// config format.
    /// This can be caused by the following:
    /// - The label containing null bytes
    /// - a too high value for `max_buffers` and `max_openers` (above [`i32::MAX`])
    #[error("Failed to convert device configuration: {0}")]
    ConfigConversionError(Box<dyn std::error::Error>),

    /// Any other error
    #[error(transparent)]
    Other(Box<dyn std::error::Error>),
}

/// Create a new v4l2loopback device.
///
/// If you pass [`None`] to `num`, the device will be created using the next available device
/// number.
///
/// This function returns a result containing the device number is it is [`Ok`], and one of the
/// following error if it is [`Err`].
///
/// # Errors
///
/// This function will return the following errors:
/// - [`ConfigConversionError`] if the label given in `config` contains null bytes.
/// - [`ControlDevice`] if it is unable to open the control device
/// - [`Ioctl`] if the underlying ioctl call fails
/// - [`DeviceCreationFailed`] if v4l2loopback was unable to create a device. This generally
/// happens when you specify an explicit number in `num`.
///
/// [`ConfigConversionError`]: Error::ConfigConversionError
/// [`ControlDevice`]: Error::ControlDevice
/// [`Ioctl`]: Error::Ioctl
/// [`DeviceCreationFailed`]: Error::DeviceCreationFailed
///
/// # Example
///
/// ```
/// use std::path::Path;
/// use v4l2loopback::{add_device, delete_device, DeviceConfig};
///
/// // We create the device without specifying a number
/// let device_num = add_device(None, DeviceConfig::default()).expect("Error when creating the device");
/// assert!(Path::new(&format!("/dev/video{}", device_num)).exists());
///
/// // Don't forget to delete it when you don't need it anymore
/// delete_device(device_num.try_into().unwrap()).expect("Error when removing device");
/// assert!(!Path::new(&format!("/dev/video{}", device_num)).exists());
/// ```
pub fn add_device(num: Option<u32>, config: DeviceConfig) -> Result<u32, Error> {
    let mut cfg: ffi::v4l2_loopback_config = match config.try_into() {
        Ok(cfg) => cfg,
        Err(e) => return Err(Error::ConfigConversionError(e)),
    };
    cfg.output_nr = num.map(i32::try_from).and_then(Result::ok).unwrap_or(-1);

    let fd = open_control_device()?;

    ioctl_readwrite_bad!(
        v4l2loopback_ctl_add,
        ffi::V4L2LOOPBACK_CTL_ADD,
        ffi::v4l2_loopback_config
    );

    let dev = unsafe { v4l2loopback_ctl_add(fd, &mut cfg as *mut ffi::v4l2_loopback_config) }?;

    if dev.is_negative() {
        return Err(Error::DeviceCreationFailed);
    }

    Ok(dev as u32)
}

/// Delete a v4l2loopback device.
///
/// Given the device number, this function will attempt to delete thev4l2loopback device.
///
/// # Errors
///
/// This function will return the following errors:
/// - [`ControlDevice`] if it is unable to open the control device
/// - [`Ioctl`] if the underlying ioctl call fails
/// - [`DeviceNotFound`] if the specified device is not recognized by v4l2loopback.
/// - [`Other`] for other errors
///
/// [`ControlDevice`]: Error::ControlDevice
/// [`Ioctl`]: Error::Ioctl
/// [`DeviceNotFound`]: Error::DeviceNotFound
/// [`Other`]: Error::Other
///
/// # Example
///
/// ```
/// use std::path::Path;
/// use v4l2loopback::{add_device, delete_device, DeviceConfig};
///
/// // You created a device earlier...
/// let device_num = add_device(None, DeviceConfig::default()).expect("Error when creating the device");
/// assert!(Path::new(&format!("/dev/video{}", device_num)).exists());
///
/// // ... Some fancy processing is being done ...
///
/// // ...And now you want to delete it.
/// delete_device(device_num.try_into().unwrap()).expect("Error when removing device");
/// assert!(!Path::new(&format!("/dev/video{}", device_num)).exists());
/// ```
pub fn delete_device(device_num: u32) -> Result<(), Error> {
    let fd = open_control_device()?;

    let converted_num = match device_num.try_into() {
        Ok(n) => n,
        Err(e) => return Err(Error::Other(Box::new(e))),
    };

    ioctl_write_int_bad!(v4l2loopback_ctl_remove, ffi::V4L2LOOPBACK_CTL_REMOVE);

    let res = unsafe { v4l2loopback_ctl_remove(fd, converted_num) }?;

    if res.is_negative() {
        return Err(Error::DeviceNotFound(device_num));
    }

    Ok(())
}

/// Queries the configuration for a specified device.
///
/// Given the device number, this function will fetch the corresponding device configuration
///
/// This function returns a result containing a [`DeviceConfig`] is it is [`Ok`], and one of the
/// following error if it is [`Err`].
///
/// # Errors
///
/// This function will return the following errors:
/// - [`ControlDevice`] if it is unable to open the control device
/// - [`Ioctl`] if the underlying ioctl call fails
/// - [`DeviceNotFound`] if the specified device is not recognized by v4l2loopback.
/// - [`ConfigConversionError`] if the label returned by v4l2loopback contains null bytes.
/// - [`Other`] for other errors
///
/// [`ControlDevice`]: Error::ControlDevice
/// [`Ioctl`]: Error::Ioctl
/// [`DeviceNotFound`]: Error::DeviceNotFound
/// [`ConfigConversionError`]: Error::ConfigConversionError
/// [`Other`]: Error::Other
///
/// # Example
///
/// ```
/// use std::path::Path;
/// use v4l2loopback::{add_device, delete_device, query_device, DeviceConfig};
///
/// // We specify our desired config
/// let device_config = DeviceConfig {
///     label: "Test Device".to_string(),
///     min_width: 100,
///     max_width: 4000,
///     min_height: 100,
///     max_height: 4000,
///     max_buffers: 9,
///     max_openers: 3,
/// };
/// // Device creation
/// let device_num =
///     add_device(None, device_config.clone()).expect("Error when creating the device");
/// assert!(Path::new(&format!("/dev/video{}", device_num)).exists());
///
/// // Querying the informations
/// let cfg =
///     query_device(device_num).expect("Error when querying the device");
/// assert_eq!(cfg, device_config);
///
/// // Don't forget to remove the device !
/// delete_device(device_num).expect("Error when removing device");
/// assert!(!Path::new(&format!("/dev/video{}", device_num)).exists());
/// ```
pub fn query_device(device_num: u32) -> Result<DeviceConfig, Error> {
    let mut cfg = ffi::v4l2_loopback_config {
        output_nr: match device_num.try_into() {
            Ok(n) => n,
            Err(e) => return Err(Error::Other(Box::new(e))),
        },
        ..Default::default()
    };

    let fd = open_control_device()?;

    ioctl_read_bad!(
        v4l2loopback_ctl_query,
        ffi::V4L2LOOPBACK_CTL_QUERY,
        ffi::v4l2_loopback_config
    );

    let res = unsafe { v4l2loopback_ctl_query(fd, &mut cfg as *mut ffi::v4l2_loopback_config) }?;

    if res.is_negative() {
        return Err(Error::DeviceNotFound(device_num));
    }

    let device_config = match DeviceConfig::try_from(cfg) {
        Ok(cfg) => cfg,
        Err(e) => return Err(Error::ConfigConversionError(e)),
    };

    Ok(device_config)
}

#[cfg(test)]
mod tests {
    use std::path::Path;

    use crate::{add_device, delete_device};

    #[test]
    fn device_with_num() {
        // Getting the next unused device num
        let mut next_num = 0;
        while Path::new(&format!("/dev/video{}", next_num)).exists() {
            next_num += 1;
        }

        // Device creation
        let device_num =
            add_device(Some(next_num), Default::default()).expect("Error when creating the device");
        assert!(Path::new(&format!("/dev/video{}", device_num)).exists());

        // Device removal
        delete_device(device_num).expect("Error when removing device");
        assert!(!Path::new(&format!("/dev/video{}", device_num)).exists());
    }

    #[test]
    fn device_with_used_num() {
        let create_device_0 = !Path::new("/dev/video0").exists();
        if create_device_0 {
            add_device(Some(0), Default::default()).expect("Error when creating the device");
        }
        assert!(Path::new("/dev/video0").exists());

        // Let's try to create an already existing device
        let res = add_device(Some(0), Default::default());
        assert!(res.is_err());

        if create_device_0 {
            delete_device(0).expect("Error when removing device");
            assert!(!Path::new("/dev/video0").exists());
        }
    }
}