Expand description
Safe wrapper around libuvc
This crate gives access to webcams connected to the computer, allowing one to stream and capture video.
§How to use this crate
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Arc;
use std::time::Duration;
// Get a libuvc context
let ctx = uvc::Context::new().expect("Could not get context");
// Get a default device
let dev = ctx
.find_device(None, None, None)
.expect("Could not find device");
// Or create an iterator over all available devices
let mut _devices = ctx.devices().expect("Could not enumerate devices");
// The device must be opened to create a handle to the device
let devh = dev.open().expect("Could not open device");
// Most webcams support this format
let format = uvc::StreamFormat {
width: 640,
height: 480,
fps: 30,
format: uvc::FrameFormat::YUYV,
};
// Get the necessary stream information
let mut streamh = devh
.get_stream_handle_with_format(format)
.expect("Could not open a stream with this format");
// This is a counter, increasing by one for every frame
// This data must be 'static + Send + Sync to be used in
// the callback used in the stream
let counter = Arc::new(AtomicUsize::new(0));
// Get a stream, calling the closure as callback for every frame
let stream = streamh
.start_stream(
|_frame, count| {
count.fetch_add(1, Ordering::SeqCst);
},
counter.clone(),
).expect("Could not start stream");
// Wait 10 seconds
std::thread::sleep(Duration::new(10, 0));
// Explicitly stop the stream
// The stream would also be stopped
// when going out of scope (dropped)
stream.stop();
println!("Counter: {}", counter.load(Ordering::SeqCst));
See also mirror.rs
in the examples to get an example of how to capture and display a stream
Structs§
- Active stream
- Contains the
libuvc
context - Device that can be opened
- Describes the device
- Open handle to a device
- List of camera devices, iterate to get the device(s)
- Describes possible formats
- Iterate to get a
FormatDescriptor
- Frame containing the image data
- Describes possible frames
- Iterate to get a
FrameDescriptor
- Format one can request a stream to produce
- Stream handle
Enums§
- Describes what frame or format is supported
- Error codes from
libusb
- Format of a frame
Type Aliases§
- Result type of functions in this crate