Skip to main content

Crate cameras

Crate cameras 

Source
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

PlatformUSB / Built-inRTSP (rtsp feature)
macOSAVFoundation via objc2retina + VideoToolbox (H.264 / H.265 / MJPEG)
WindowsMedia Foundation via windowsretina + Media Foundation (H.264 / H.265 / MJPEG)
LinuxV4L2 via v4lnot 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: a CameraSource enum that unifies USB and RTSP, plus open_source which dispatches to open or open_rtsp automatically. 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. Supports pump::set_active (pause / resume without closing the camera), pump::capture_frame (single fresh frame on demand, works while paused), and pump::stop_and_join (deterministic teardown). This is the primitive higher-level integrations (for example, the dioxus-cameras hook) are built on.
  • analysis (feature-gated, see analysis): blur-variance sharpness metrics and a small analysis::Ring for “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 impl blocks with hidden accessors, no trait objects, no inheritance.
  • Explicit format negotiation: probe returns every format a device supports. You pick one and pass it to open via StreamConfig. If you want a fallback, best_format picks the closest match.
  • Push-based delivery: Each Camera owns a worker thread and a bounded crossbeam channel. The consumer pulls frames with a timeout via next_frame. If the consumer falls behind, old frames are dropped, not buffered.
  • Typed errors: See Error.
  • Pluggable pixel conversion: to_rgb8 / to_rgba8 decode from BGRA, RGBA, YUYV, NV12, and MJPEG (via zune-jpeg), honoring stride.
  • Hotplug: monitor() returns a DeviceMonitor that emits DeviceEvent::Added / DeviceEvent::Removed as cameras appear and disappear.
  • Unified opening: open_source + CameraSource let you treat USB and RTSP cameras uniformly in higher-level code.
  • Background pump with pause + capture: pump::spawn runs the frame loop off the calling thread, with pump::set_active for pause / resume and pump::capture_frame for single-shot snapshots while paused.
  • Compile-time backend contract: Platform backends are selected with cfg. Each is a Driver struct that implements Backend. No Box<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 Camera resource 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 DeviceEvent as cameras appear and disappear.
pump
Long-running background pump that pulls frames from a Camera and hands each one to a caller-provided sink closure.
source
Unified source enum covering both USB and RTSP cameras, plus open_source for opening either through one call.
types
Plain data types describing devices, formats, and frames.

Constants§

DEFAULT_FRAME_TIMEOUT
A reasonable default timeout for next_frame when 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.