UVC Camera Stream Example
This example demonstrates how to capture video frames from a USB camera using the USB Video Class (UVC) protocol. The example shows basic camera initialization, frame callbacks, and stream control.What This Example Demonstrates
- Initializing USB camera streaming
- Allocating frame buffers efficiently
- Configuring camera resolution and frame rate
- Receiving frames via callback function
- Controlling camera stream (suspend/resume)
Hardware Setup
Required Components:- ESP32-S2 or ESP32-S3 development board
- USB camera with UVC support (most webcams work)
- USB OTG cable or adapter
- Connect the USB camera to the ESP32’s USB port
- Ensure proper power supply (cameras can draw significant current)
- Connect ESP32 to computer via serial for monitoring
Complete Code
Code Explanation
1. Frame Callback Function
static void onCameraFrameCallback(uvc_frame *frame, void *user_ptr)- Callback function triggered when a new frame arrivesframe->frame_format- Format of the frame (MJPEG, YUYV, etc.)frame->sequence- Frame sequence number (useful for detecting dropped frames)frame->widthandframe->height- Actual frame dimensionsframe->data_bytes- Size of frame data in bytesuser_ptr- Custom user data passed during registration
2. Buffer Allocation
_xferBufferAand_xferBufferB- Double buffering for USB transfers (prevents frame drops)_frameBuffer- Assembled complete frame buffer- Size: 55KB - Supports MJPEG frames at moderate resolutions
assert()ensures allocation succeeded (critical for stability)
3. Camera Configuration
FRAME_RESOLUTION_ANY(width) - Accept any resolution width from cameraFRAME_RESOLUTION_ANY(height) - Accept any resolution height from cameraFRAME_INTERVAL_FPS_15- Request 15 frames per second55 * 1024- Transfer buffer size (55KB)_xferBufferA,_xferBufferB- Double buffer pointers55 * 1024- Frame buffer size (55KB)_frameBuffer- Complete frame buffer pointer
4. Stream Control
Expected Serial Output
frame_format = 6- MJPEG formatseq- Increments with each framewidth/height- Actual resolution negotiated with cameralength- Varies due to MJPEG compression
Performance Considerations
Buffer Size Selection:- 55KB handles most MJPEG frames at 640x480
- For higher resolutions (720p+), increase to 100KB or more
- Monitor
lengthin callback to verify frames fit
- 15 FPS is conservative for stability
- Higher rates (30 FPS) require faster processing in callback
- Keep callback execution time minimal
Next Steps: Processing Frames
Save Frame to SPIFFS
Send Frame Over WiFi
Decode and Process Pixels
Troubleshooting
No frames received:- Check USB cable connection
- Verify camera is UVC-compatible
- Increase buffer sizes if frames are too large
- Check power supply (some cameras need external power)
- Reduce frame rate
- Minimize callback processing time
- Increase buffer sizes
- Use lower resolution
- Reduce buffer sizes
- Free unused memory before initialization
- Use PSRAM if available on ESP32-S3
Related Examples
Combined Streaming
Use UVC camera with UAC audio simultaneously
UAC Microphone
Capture audio from USB microphones