Overview
UVC (USB Video Class) is a standardized protocol for transmitting video data over USB. The ESP32_USB_STREAM library implements a UVC host driver that allows ESP32-S2 and ESP32-S3 to receive video streams from USB cameras.The library supports MJPEG format by default, which provides a good balance between image quality and bandwidth requirements.
How UVC Streaming Works
The library creates internal FreeRTOS tasks to handle USB data transfers from the camera’s streaming pipe. When a complete frame is assembled, it triggers a user-defined callback function.Frame Formats and Resolutions
Supported Frame Formats
The library supports multiple UVC frame formats:| Format | Description | Use Case |
|---|---|---|
UVC_FRAME_FORMAT_MJPEG | Motion-JPEG (default) | Most common, good compression |
UVC_FRAME_FORMAT_YUYV | YUV 4:2:2 encoding | Uncompressed, higher bandwidth |
UVC_FRAME_FORMAT_RGB | 24-bit RGB | Direct color format |
UVC_FRAME_FORMAT_H264 | H.264 encoded | Advanced compression |
UVC_FRAME_FORMAT_GRAY8 | 8-bit grayscale | Low bandwidth |
Resolution Configuration
You can specify exact resolutions or useFRAME_RESOLUTION_ANY to let the library negotiate with the camera:
Frame Intervals (FPS)
The library provides predefined frame intervals:| Constant | FPS | Frame Interval (100ns units) |
|---|---|---|
FRAME_INTERVAL_FPS_5 | 5 fps | 2,000,000 |
FRAME_INTERVAL_FPS_10 | 10 fps | 1,000,000 |
FRAME_INTERVAL_FPS_15 | 15 fps | 666,666 |
FRAME_INTERVAL_FPS_20 | 20 fps | 500,000 |
FRAME_INTERVAL_FPS_30 | 25 fps | 400,000 |
Double Buffering System
The library implements a double-buffering mechanism to ensure smooth video streaming without frame drops:Buffer A receives data
While Buffer A is being filled with incoming USB data packets, Buffer B processes the previous frame.
Buffer Size Requirements
Frame Callbacks and Processing
The frame callback is executed when a complete frame is ready:Frame Structure
Theuvc_frame_t structure contains:
| Field | Type | Description |
|---|---|---|
data | void* | Pointer to image data |
data_bytes | size_t | Size of image data in bytes |
width | uint32_t | Frame width in pixels |
height | uint32_t | Frame height in pixels |
frame_format | enum | Pixel format (MJPEG, YUYV, etc.) |
sequence | uint32_t | Frame sequence number |
capture_time | struct timeval | Capture timestamp |
Suspend and Resume Capabilities
You can temporarily pause video streaming without disconnecting the camera:Dynamic Frame Configuration
You can change resolution or frame rate while suspended:Frame configuration changes only take effect after calling
uvcCamResume().Querying Camera Capabilities
Get Available Frame Sizes
Transfer Modes
UVC supports two transfer types:Isochronous Transfer
- Default mode for most cameras
- Guaranteed bandwidth
- Time-sensitive delivery
- May have occasional data loss
Bulk Transfer
- Higher bandwidth potential
- Guaranteed delivery
- No time guarantees
- Better for high-resolution streams
Example: Complete UVC Setup
Best Practices
Memory Management
Memory Management
- Always check malloc() returns non-NULL before use
- Allocate buffers large enough for your target resolution
- Free buffers when streaming is stopped to prevent memory leaks
Callback Performance
Callback Performance
- Keep callback functions short and fast
- Avoid blocking operations (Serial.print, delay, etc.)
- Use queues to pass frame data to other tasks if needed
- Never call USB_STREAM functions from within callbacks
Frame Rate Optimization
Frame Rate Optimization
- Start with lower frame rates (5-15 fps) and increase if stable
- Monitor free heap memory during operation
- Consider reducing resolution if experiencing frame drops
- Use bulk transfer mode for high-resolution streams if supported
Related Topics
UAC Streaming
Learn about USB audio streaming
Hardware Requirements
ESP32 SoC and USB wiring requirements