Expand description
A cross-platform camera library for Rust, built with data-oriented design.
cameras exposes plain data (Device, Capabilities, FormatDescriptor,
StreamConfig, Frame) and free functions that operate on that data. Every
public type has public fields. Format negotiation is explicit: you probe, you pick,
you open. Errors are typed. Platform dispatch happens at compile time via cfg and
the associated-type Backend trait; there are zero trait objects anywhere in the
library.
§Platform support
| Platform | USB / Built-in | RTSP (rtsp feature) |
|---|---|---|
| macOS | AVFoundation via objc2 | retina + VideoToolbox (H.264 / H.265 / MJPEG) |
| Windows | Media Foundation via windows | retina + Media Foundation (H.264 / H.265 / MJPEG) |
| Linux | V4L2 via v4l | not supported |
§Quick Start
use std::time::Duration;
fn main() -> Result<(), cameras::Error> {
let devices = cameras::devices()?;
let device = devices.first().expect("no cameras");
let capabilities = cameras::probe(device)?;
println!("{} formats available", capabilities.formats.len());
let config = cameras::StreamConfig {
resolution: cameras::Resolution { width: 1280, height: 720 },
framerate: 30,
pixel_format: cameras::PixelFormat::Bgra8,
};
let camera = cameras::open(device, config)?;
for _ in 0..30 {
let frame = cameras::next_frame(&camera, Duration::from_secs(2))?;
let rgb = cameras::to_rgb8(&frame)?;
println!("{}x{} -> {} bytes rgb", frame.width, frame.height, rgb.len());
}
Ok(())
}Dropping the Camera stops the stream. Dropping the DeviceMonitor joins its
polling worker.
§Higher-level primitives
Two modules layer on top of the Camera / next_frame core. They are optional;
callers who want full control can stick with the core API.
source: aCameraSourceenum that unifies USB and RTSP, plusopen_sourcewhich dispatches toopenoropen_rtspautomatically. Useful for UIs and config files that want a single “where do frames come from” value type.pump: a long-running background worker that pulls frames and hands them to a caller-provided sink closure. Supportspump::set_active(pause / resume without closing the camera),pump::capture_frame(single fresh frame on demand, works while paused), andpump::stop_and_join(deterministic teardown). This is the primitive higher-level integrations (for example, thedioxus-camerashook) are built on.analysis(feature-gated, seeanalysis): blur-variance sharpness metrics and a smallanalysis::Ringfor “take the sharpest frame of the last N” capture flows. Scores are relative; calibrate thresholds per camera.
§Design
- Data-oriented: Types hold data. Functions operate on data. No
implblocks with hidden accessors, no trait objects, no inheritance. - Explicit format negotiation:
probereturns every format a device supports. You pick one and pass it toopenviaStreamConfig. If you want a fallback,best_formatpicks the closest match. - Push-based delivery: Each
Cameraowns a worker thread and a bounded crossbeam channel. The consumer pulls frames with a timeout vianext_frame. If the consumer falls behind, old frames are dropped, not buffered. - Typed errors: See
Error. - Pluggable pixel conversion:
to_rgb8/to_rgba8decode from BGRA, RGBA, YUYV, NV12, and MJPEG (viazune-jpeg), honoring stride. - Hotplug:
monitor()returns aDeviceMonitorthat emitsDeviceEvent::Added/DeviceEvent::Removedas cameras appear and disappear. - Unified opening:
open_source+CameraSourcelet you treat USB and RTSP cameras uniformly in higher-level code. - Background pump with pause + capture:
pump::spawnruns the frame loop off the calling thread, withpump::set_activefor pause / resume andpump::capture_framefor single-shot snapshots while paused. - Compile-time backend contract: Platform backends are selected with
cfg. Each is aDriverstruct that implementsBackend. NoBox<dyn Backend>; the compiler verifies every platform implements the same surface.
Re-exports§
pub use analysis::Rect;pub use backend::Backend;pub use backend::BackendControls;pub use camera::Camera;pub use camera::next_frame;pub use camera::try_next_frame;pub use controls::ControlCapabilities;pub use controls::ControlKind;pub use controls::ControlRange;pub use controls::Controls;pub use controls::PowerLineFrequency;pub use controls::PowerLineFrequencyCapability;pub use controls::apply_controls;pub use controls::control_capabilities;pub use controls::default_controls;pub use controls::read_controls;pub use controls::reset_to_defaults;pub use convert::to_rgb8;pub use convert::to_rgba8;pub use error::Error;pub use monitor::DeviceMonitor;pub use monitor::next_event;pub use monitor::try_next_event;pub use source::CameraSource;pub use source::open_source;pub use source::source_label;pub use types::Capabilities;pub use types::Credentials;pub use types::Device;pub use types::DeviceEvent;pub use types::DeviceId;pub use types::FormatDescriptor;pub use types::Frame;pub use types::FrameQuality;pub use types::FramerateRange;pub use types::PixelFormat;pub use types::Position;pub use types::Resolution;pub use types::StreamConfig;pub use types::Transport;
Modules§
- analysis
- Frame analysis helpers.
- backend
- Compile-time contract every platform backend must implement.
- camera
- The
Cameraresource handle and frame-reading free functions. - controls
- Runtime camera controls — focus, exposure, white-balance, PTZ, and related image adjustments.
- convert
- Pixel format conversion helpers.
- error
- Typed error enum for every failure path in the library.
- monitor
- Hotplug monitor that emits
DeviceEventas cameras appear and disappear. - pump
- Long-running background pump that pulls frames from a
Cameraand hands each one to a caller-provided sink closure. - source
- Unified source enum covering both USB and RTSP cameras, plus
open_sourcefor opening either through one call. - types
- Plain data types describing devices, formats, and frames.
Constants§
- DEFAULT_
FRAME_ TIMEOUT - A reasonable default timeout for
next_framewhen you don’t want to hand-pick one.
Functions§
- best_
format - Pick the closest supported format to a requested
StreamConfig. - devices
- Enumerate every video capture device the platform currently sees.
- monitor
- Start a hotplug monitor.
- open
- Open a camera with the given configuration and start streaming.
- probe
- Inspect a device’s full set of supported formats without opening a stream.