ccap-rs (CameraCapture / ccap Rust Bindings)
Safe Rust bindings for CameraCapture (ccap) — a high-performance, lightweight, cross-platform webcam/camera capture library with hardware-accelerated pixel format conversion (Windows DirectShow, macOS/iOS AVFoundation, Linux V4L2).
Note: The published package name on crates.io is
ccap-rs, but the crate name in code isccap.
Features
- High Performance: Hardware-accelerated pixel format conversion with up to 10x speedup (AVX2, Apple Accelerate, NEON)
- Cross Platform: Windows (DirectShow), macOS/iOS (AVFoundation), Linux (V4L2)
- Multiple Formats: RGB, BGR, YUV (NV12/I420) with automatic conversion
- Zero Dependencies: Uses only system frameworks
- Memory Safe: Safe Rust API with automatic resource management
Quick Start
Add dependency
Option A (simple):
Option B (recommended): keep the crate name as ccap in your code:
[]
= { = "ccap-rs", = "<latest>" }
Tip: Replace
<latest>with the latest version shown on https://crates.io/crates/ccap-rs
Basic Usage
use ;
Examples
The crate includes several examples:
# List available cameras and their info
# Minimal capture example
# Capture frames using grab mode
# Capture frames using callback mode
Building
Feature Modes
This crate supports two build modes:
- Distribution mode (default):
build-source— Builds the native C/C++ implementation via thecccrate (intended for crates.io users). - Development mode:
static-link— Links against a pre-built native library from a CameraCapture checkout (e.g.build/Debug/libccap.a) (intended for developing this repository).
Prerequisites
If you are using development mode (static-link), you need to build the native library first:
# From the root of the CameraCapture project
Or manually:
Building the Rust Crate
# From bindings/rust directory
Using development mode (static-link)
# Link against pre-built build/Debug or build/Release from the repo
AddressSanitizer (ASan) and static-link
The CameraCapture repo's test scripts may build the native library with ASan enabled (e.g. Debug functional tests).
An ASan-instrumented libccap.a requires the ASan runtime at link/run time.
- When using
static-link,build.rswill only link the ASan runtime if it detects ASan symbols inside the prebuiltlibccap.a. - This does not affect the default crates.io build (
build-source). - You can disable the auto-link behavior by setting
CCAP_RUST_NO_ASAN_LINK=1.
Feature flags
build-source(default): build the C/C++ ccap sources duringcargo build(best for crates.io usage).static-link: link against a pre-built static library from a CameraCapture checkout (best for development). If you use this mode, make sure you have built the C/C++ project first, and setCCAP_SOURCE_DIRwhen needed.
Platform notes
- Camera capture: Windows (DirectShow), macOS/iOS (AVFoundation), Linux (V4L2)
- Video file playback support depends on the underlying C/C++ library backend (currently Windows/macOS only).
API Documentation
Core Types
Provider: Main camera capture interfaceVideoFrame: Represents a captured video frameDeviceInfo: Camera device informationPixelFormat: Supported pixel formats (RGB24, BGR24, NV12, I420, etc.)Resolution: Frame resolution specification
Error Handling
All operations return Result<T, CcapError> for comprehensive error handling.
For frame capture, grab_frame(timeout_ms) returns Result<Option<VideoFrame>, CcapError>:
match provider.grab_frame
Thread Safety
VideoFrameimplementsSendso frames can be moved across threads (e.g. processed/dropped on a worker thread)ProviderimplementsSendbut the underlying C++ API is not thread-safe- Use
Providerfrom a single thread, or wrap it withArc<Mutex<Provider>>for multi-threaded access
- Use
- Frame data remains valid until the
VideoFrameis dropped
Note: The C++ layer's thread-safety is based on code inspection. If you encounter issues with cross-thread frame usage, please report them.
Platform Support
| Platform | Backend | Status |
|---|---|---|
| Windows | DirectShow | ✅ Supported |
| macOS | AVFoundation | ✅ Supported |
| iOS | AVFoundation | ✅ Supported |
| Linux | V4L2 | ✅ Supported |
System Requirements
- Rust 1.65+
- CMake 3.14+
- Platform-specific camera frameworks (automatically linked)
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Related Projects
- ccap (C/C++) - The underlying C/C++ library
- OpenCV - Alternative computer vision library with camera support