uvc 0.2.0

Safe and ergonomic wrapper around libuvc, allowing capture of webcam streams
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
use std::ffi::CStr;
use std::marker::PhantomData;
use std::ptr::NonNull;
use std::slice;
use std::time::Duration;

use crate::error::{Error, Result};
use crate::formats::{FrameFormat, StreamFormat};
use crate::streaming::StreamHandle;
use uvc_sys::*;

unsafe impl<'a> Send for DeviceList<'a> {}
unsafe impl<'a> Sync for DeviceList<'a> {}
#[derive(Debug)]
/// List of camera devices, iterate to get the device(s)
pub struct DeviceList<'a> {
    start: *mut *mut uvc_device,
    list: NonNull<*mut uvc_device>,
    _ph: PhantomData<&'a &'a uvc_device>,
}

impl<'a> Drop for DeviceList<'a> {
    fn drop(&mut self) {
        unsafe { uvc_free_device_list(self.start, false as u8) }
    }
}

impl<'a> DeviceList<'a> {
    pub(crate) fn new(list: NonNull<*mut uvc_device>) -> Self {
        Self {
            start: list.as_ptr(),
            list,
            _ph: PhantomData,
        }
    }
}

impl<'a> Iterator for DeviceList<'a> {
    type Item = Device<'a>;

    fn next(&mut self) -> Option<Device<'a>> {
        let item = self.list.as_ptr();
        if unsafe { (*item).is_null() } {
            return None;
        }

        let device = unsafe { Device::from_raw(*item) };
        self.list = unsafe { NonNull::new(self.list.as_ptr().add(1)).unwrap() };

        Some(device)
    }
}

unsafe impl<'a> Send for Device<'a> {}
unsafe impl<'a> Sync for Device<'a> {}
#[derive(Debug)]
/// Device that can be opened
pub struct Device<'a> {
    dev: NonNull<uvc_device>,
    _dev: PhantomData<&'a uvc_device>,
}

impl<'a> Drop for Device<'a> {
    fn drop(&mut self) {
        unsafe { uvc_unref_device(self.dev.as_ptr()) };
    }
}

impl<'a> Device<'a> {
    pub(crate) unsafe fn from_raw(dev: *mut uvc_device) -> Self {
        Device {
            dev: NonNull::new(dev).unwrap(),
            _dev: PhantomData,
        }
    }
    /// Create handle to a device
    pub fn open(&'a self) -> Result<DeviceHandle<'a>> {
        unsafe {
            let mut devh = std::mem::MaybeUninit::uninit();
            let err = uvc_open(self.dev.as_ptr(), devh.as_mut_ptr()).into();
            match err {
                Error::Success => Ok(DeviceHandle {
                    devh: NonNull::new(devh.assume_init()).unwrap(),
                    _devh: PhantomData,
                }),
                err => Err(err),
            }
        }
    }
    /// Get the description of a device
    pub fn description(&self) -> Result<DeviceDescription> {
        unsafe {
            let mut desc = std::mem::MaybeUninit::uninit();
            let err = uvc_get_device_descriptor(self.dev.as_ptr(), desc.as_mut_ptr()).into();
            if err != Error::Success {
                return Err(err);
            }

            let desc = desc.assume_init();

            let vendor_id = (*desc).idVendor;
            let product_id = (*desc).idProduct;
            let bcd_uvc = (*desc).bcdUVC;

            let serial_number_c_str = (*desc).serialNumber;
            let serial_number = if serial_number_c_str.is_null() {
                None
            } else {
                Some(
                    CStr::from_ptr(serial_number_c_str)
                        .to_owned()
                        .into_string()
                        .unwrap(),
                )
            };
            let manufacturer_c_str = (*desc).manufacturer;
            let manufacturer = if manufacturer_c_str.is_null() {
                None
            } else {
                Some(
                    CStr::from_ptr(manufacturer_c_str)
                        .to_owned()
                        .into_string()
                        .unwrap(),
                )
            };
            let product_c_str = (*desc).product;
            let product = if product_c_str.is_null() {
                None
            } else {
                Some(
                    CStr::from_ptr(product_c_str)
                        .to_owned()
                        .into_string()
                        .unwrap(),
                )
            };
            let descp = Ok(DeviceDescription {
                vendor_id,
                product_id,
                bcd_uvc,
                serial_number,
                manufacturer,
                product,
            });

            uvc_free_device_descriptor(desc);

            descp
        }
    }

    /// Bus number of which this device is connected
    #[must_use]
    pub fn bus_number(&self) -> u8 {
        unsafe { uvc_get_bus_number(self.dev.as_ptr()) }
    }

    /// Device address within the bus
    #[must_use]
    pub fn device_address(&self) -> u8 {
        unsafe { uvc_get_device_address(self.dev.as_ptr()) }
    }
}

unsafe impl<'a> Send for DeviceHandle<'a> {}
unsafe impl<'a> Sync for DeviceHandle<'a> {}
#[derive(Debug)]
/// Open handle to a device
pub struct DeviceHandle<'a> {
    pub(crate) devh: NonNull<uvc_device_handle>,
    _devh: PhantomData<&'a uvc_device_handle>,
}

impl<'a, 'b> DeviceHandle<'a> {
    /// List all supported formats
    #[must_use]
    pub fn supported_formats(&self) -> FormatDescriptors<'a> {
        unsafe {
            let format_descs = uvc_get_format_descs(self.devh.as_ptr());

            FormatDescriptors {
                head: format_descs,
                _ph: PhantomData,
            }
        }
    }

    /// Iterates over all available formats to select the best format.
    ///
    /// f should compare (x, y) and return the preferred format.
    pub fn get_preferred_format<F>(&self, f: F) -> Option<StreamFormat>
    where
        F: Fn(StreamFormat, StreamFormat) -> StreamFormat,
    {
        let mut pref_format = None;
        for i in self.supported_formats() {
            for j in i.supported_formats() {
                for k in j.intervals() {
                    let format = StreamFormat {
                        width: u32::from(j.width()),
                        height: u32::from(j.height()),
                        fps: 10_000_000 / *k,
                        format: match j.subtype() {
                            DescriptionSubtype::FormatMJPEG | DescriptionSubtype::FrameMJPEG => {
                                FrameFormat::MJPEG
                            }
                            DescriptionSubtype::FormatUncompressed
                            | DescriptionSubtype::FrameUncompressed => FrameFormat::Uncompressed,
                            _ => FrameFormat::Any,
                        },
                    };
                    pref_format = Some(pref_format.map_or(format, |x| f(x, format)));
                }
            }
        }
        pref_format
    }

    /// Creates a stream handle
    pub fn get_stream_handle_with_format_size_and_fps(
        &'a self,
        format: FrameFormat,
        width: u32,
        height: u32,
        fps: u32,
    ) -> Result<StreamHandle<'a>> {
        unsafe {
            let mut handle = std::mem::MaybeUninit::uninit();
            let err = uvc_get_stream_ctrl_format_size(
                self.devh.as_ptr(),
                handle.as_mut_ptr(),
                format.into(),
                width as i32,
                height as i32,
                fps as i32,
            )
            .into();
            if err == Error::Success {
                Ok(StreamHandle {
                    handle: handle.assume_init(),
                    devh: self,
                })
            } else {
                Err(err)
            }
        }
    }

    /// Creates a stream handle
    pub fn get_stream_handle_with_format(
        &'a self,
        format: StreamFormat,
    ) -> Result<StreamHandle<'a>> {
        self.get_stream_handle_with_format_size_and_fps(
            format.format,
            format.width,
            format.height,
            format.fps,
        )
    }
}

impl<'a> Drop for DeviceHandle<'a> {
    fn drop(&mut self) {
        unsafe {
            uvc_close(self.devh.as_ptr());
        }
    }
}

#[derive(Debug)]
/// Describes the device
pub struct DeviceDescription {
    pub vendor_id: u16,
    pub product_id: u16,
    pub bcd_uvc: u16,
    pub serial_number: Option<String>,
    pub manufacturer: Option<String>,
    pub product: Option<String>,
}

unsafe impl<'a> Send for FormatDescriptor<'a> {}
unsafe impl<'a> Sync for FormatDescriptor<'a> {}
/// Describes possible formats
pub struct FormatDescriptor<'a> {
    format_desc: NonNull<uvc_format_desc_t>,
    _ph: PhantomData<&'a uvc_format_desc_t>,
}

#[derive(Debug, PartialEq)]
/// Describes what frame or format is supported
pub enum DescriptionSubtype {
    Undefined,
    InputHeader,
    OutputHeader,
    StillImageFrame,
    FormatUncompressed,
    FrameUncompressed,
    FormatMJPEG,
    FrameMJPEG,
    FormatMPEG2TS,
    FormatDV,
    ColorFormat,
    FormatFrameBased,
    FrameFrameBased,
    FormatStreamBased,
}

impl From<uvc_vs_desc_subtype> for DescriptionSubtype {
    fn from(x: uvc_vs_desc_subtype) -> DescriptionSubtype {
        #[allow(non_upper_case_globals)]
        match x {
            uvc_vs_desc_subtype_UVC_VS_UNDEFINED => DescriptionSubtype::Undefined,
            uvc_vs_desc_subtype_UVC_VS_INPUT_HEADER => DescriptionSubtype::InputHeader,
            uvc_vs_desc_subtype_UVC_VS_OUTPUT_HEADER => DescriptionSubtype::OutputHeader,
            uvc_vs_desc_subtype_UVC_VS_STILL_IMAGE_FRAME => DescriptionSubtype::StillImageFrame,
            uvc_vs_desc_subtype_UVC_VS_FORMAT_UNCOMPRESSED => {
                DescriptionSubtype::FormatUncompressed
            }
            uvc_vs_desc_subtype_UVC_VS_FRAME_UNCOMPRESSED => DescriptionSubtype::FrameUncompressed,
            uvc_vs_desc_subtype_UVC_VS_FORMAT_MJPEG => DescriptionSubtype::FormatMJPEG,
            uvc_vs_desc_subtype_UVC_VS_FRAME_MJPEG => DescriptionSubtype::FrameMJPEG,
            uvc_vs_desc_subtype_UVC_VS_FORMAT_MPEG2TS => DescriptionSubtype::FormatMPEG2TS,
            uvc_vs_desc_subtype_UVC_VS_FORMAT_DV => DescriptionSubtype::FormatDV,
            uvc_vs_desc_subtype_UVC_VS_COLORFORMAT => DescriptionSubtype::ColorFormat,
            uvc_vs_desc_subtype_UVC_VS_FORMAT_FRAME_BASED => DescriptionSubtype::FormatFrameBased,
            uvc_vs_desc_subtype_UVC_VS_FRAME_FRAME_BASED => DescriptionSubtype::FrameFrameBased,
            uvc_vs_desc_subtype_UVC_VS_FORMAT_STREAM_BASED => DescriptionSubtype::FormatStreamBased,
            _ => DescriptionSubtype::Undefined,
        }
    }
}

impl<'a> FormatDescriptor<'a> {
    #[must_use]
    pub fn supported_formats(&self) -> FrameDescriptors {
        FrameDescriptors {
            head: unsafe { (*self.format_desc.as_ptr()).frame_descs },
            _ph: PhantomData,
        }
    }

    #[must_use]
    pub fn subtype(&self) -> DescriptionSubtype {
        unsafe { (*self.format_desc.as_ptr()).bDescriptorSubtype }.into()
    }
}

unsafe impl<'a> Send for FormatDescriptors<'a> {}
unsafe impl<'a> Sync for FormatDescriptors<'a> {}
/// Iterate to get a `FormatDescriptor`
pub struct FormatDescriptors<'a> {
    head: *const uvc_format_desc_t,
    _ph: PhantomData<&'a uvc_format_desc_t>,
}

impl<'a> Iterator for FormatDescriptors<'a> {
    type Item = FormatDescriptor<'a>;

    fn next(&mut self) -> Option<FormatDescriptor<'a>> {
        match NonNull::new(self.head as *mut _) {
            None => None,
            Some(x) => {
                let current = FormatDescriptor {
                    format_desc: x,
                    _ph: PhantomData,
                };
                self.head = unsafe { (*self.head).next };
                Some(current)
            }
        }
    }
}

unsafe impl<'a> Send for FrameDescriptor<'a> {}
unsafe impl<'a> Sync for FrameDescriptor<'a> {}
#[derive(Debug)]
/// Describes possible frames
pub struct FrameDescriptor<'a> {
    frame_desc: NonNull<uvc_frame_desc_t>,
    _ph: PhantomData<&'a uvc_frame_desc_t>,
}

impl<'a> FrameDescriptor<'a> {
    #[must_use]
    pub fn width(&self) -> u16 {
        unsafe { (*self.frame_desc.as_ptr()).wWidth }
    }
    #[must_use]
    pub fn height(&self) -> u16 {
        unsafe { (*self.frame_desc.as_ptr()).wHeight }
    }
    /// Type of frame
    #[must_use]
    pub fn subtype(&self) -> DescriptionSubtype {
        unsafe { (*self.frame_desc.as_ptr()).bDescriptorSubtype }.into()
    }
    /// Time in 100ns
    #[must_use]
    pub fn intervals(&self) -> &[u32] {
        unsafe {
            let intervals: *const u32 = (*self.frame_desc.as_ptr()).intervals;
            if intervals.is_null() {
                return &[];
            }
            let mut len = 0;
            loop {
                let x = *intervals.add(len);
                if x == 0 {
                    return slice::from_raw_parts::<'a>(intervals, len);
                }
                len += 1;
            }
        }
    }

    /// Duration between captures
    #[must_use]
    pub fn intervals_duration(&self) -> Vec<Duration> {
        let times = self.intervals();
        let mut durations = Vec::with_capacity(times.len());

        for i in times {
            durations.push(Duration::from_nanos(u64::from(*i) * 100));
        }

        durations
    }
}

unsafe impl<'a> Send for FrameDescriptors<'a> {}
unsafe impl<'a> Sync for FrameDescriptors<'a> {}
/// Iterate to get a `FrameDescriptor`
pub struct FrameDescriptors<'a> {
    head: *mut uvc_frame_desc_t,
    _ph: PhantomData<&'a uvc_frame_desc_t>,
}

impl<'a> Iterator for FrameDescriptors<'a> {
    type Item = FrameDescriptor<'a>;

    fn next(&mut self) -> Option<FrameDescriptor<'a>> {
        match NonNull::new(self.head) {
            None => None,
            Some(x) => {
                let current = FrameDescriptor {
                    frame_desc: x,
                    _ph: PhantomData,
                };
                unsafe { self.head = (*self.head).next };
                Some(current)
            }
        }
    }
}