Repository navigation
Usage
#include "TinyUVC.h"
UVCDevice uvc;
void setup() {
uvc.addResolution(UVCFormat::YUY2, 160, 120, {15, 10}); // two frame rates
uvc.addResolution(UVCFormat::MJPEG, 640, 480, 15, 60000); // max JPEG size
uvc.begin();
}
void loop() {
if (uvc.isReady()) {
UVCStreamSettings s = uvc.settings(); // what the host selected
uvc.writeFrame(data, len, PixelFormat::RGB565_LE);
}
}The host picks format, resolution and frame rate; your sketch reads it
with settings() or is told with setStreamCallback().
writeFrame() rejects frames that do not match the host's selection: MJPEG
needs PixelFormat::MJPEG, YUY2 needs exactly width x height pixels in any
raw format (RGB565 in both byte orders, RGB888, BGR888, grayscale: they are
converted to YUY2 while they are sent). The data must stay valid until
isReady() is true again.
#include "TinyCameraPins.h"
#include "TinyUVCCamera.h"
UVCDevice uvc;
UVCCamera camera(uvc);
void setup() {
camera_config_t cfg = TinyCamera::defaultConfig();
setPinsXiaoEsp32S3(cfg);
cfg.pixel_format = PIXFORMAT_JPEG; // -> MJPEG, RGB565 -> YUY2
camera.addFrameSize(FRAMESIZE_QVGA);
camera.addFrameSize(FRAMESIZE_VGA);
camera.begin(cfg); // starts uvc as well
}
void loop() { camera.copy(); }The camera is reconfigured when the host switches resolution or format.
#include "AudioTools.h"
#include "AudioTools/Video/CodecJPEG.h"
#include "TinyUVCAudioTools.h"
UVCDevice uvc;
UVCVideoOutput out(uvc, PixelFormat::MJPEG);
MJPEGEncoder encoder(out); // RGB565 in, JPEG to the webcamThe video classes of arduino-audio-tools need FreeRTOS: on RP2040/RP2350
select Tools > Operating System > "FreeRTOS SMP" and add
#include "AudioTools/Concurrency/RTOS.h" after AudioTools.h (see the
AudioTools examples); on STM32 this needs the STM32FreeRTOS library.
UVCVideoOutput collects the write() calls of a frame and sends it on
flush() (a complete JPEG in a single write() is sent right away).
setPacing(true) plays files at the frame rate the host selected,
setTimeout() lets it wait for the USB transfer instead of dropping frames.
UVCVideoCopy streams any VideoFrameSource.
Frames passed to writeFrame() can be anywhere, also in PSRAM: they are
copied payload by payload into the USB transfer buffer, which TinyUVC keeps
in internal (DMA capable) RAM. That buffer is small (2 KB at full speed).
Frame sized memory is taken from PSRAM where the board has it (ESP32 with Tools > PSRAM enabled, RP2350 boards with PSRAM):
-
Camera frames (
UVCCamera): setfb_location = CAMERA_FB_IN_PSRAMin the camera configuration (see the ESP32CameraWebcam example). -
UVCVideoOutputkeeps two frame buffers (one is sent while the next is written). They grow to the size of the biggest frame and come from PSRAM by default (UVCMemoryType::Auto, RAM if there is no PSRAM).setMemoryType(UVCMemoryType::RAMOnly)orUVCMemoryType::PSRAMOnlyforces one of them,reserve(maxFrameSize)allocates both buffers up front andrelease()frees them. - Your own buffers:
UVCMemory::alloc(size)/UVCMemory::free(p), or the growingUVCFrameBuffer.
UVCVideoOutput out(uvc, PixelFormat::MJPEG);
void setup() {
...
if (!out.reserve(60000)) Serial.println("not enough memory");
}| Example | What it shows |
|---|---|
| TestPatternWebcam | No camera: moving color bars as YUY2 - start here |
| ESP32CameraWebcam | ESP32-S3 camera board as MJPEG webcam with two resolutions |
| RP2040CameraWebcam | Pico + OV7670/OV2640, RGB565 converted to YUY2 |
| STM32CameraWebcam | WeAct STM32H750 + OV7725 via DCMI |
| AudioToolsJpegWebcam | Software JPEG compression with MJPEGEncoder -> UVCVideoOutput
|
| AudioToolsAviWebcam | Plays an MJPEG AVI from SD as webcam picture (DemuxerAVI) |
| desktop/webcam_simulation | The whole chain on the PC, see Desktop |