How luxonis/uvc-gadget works
1. What this repository is
- uvcgadget is a Linux-focused library for building a USB UVC gadget: a device that presents itself as a USB webcam.
- The repo contains both the core library and a small sample utility (uvc-gadget) for exercising the library.
2. Top-level architecture
- The code is split into three main areas:
- include/ — public headers and API surface
- lib/ — library implementation
- src/ — the sample application entry point
- The library is the center of the architecture; the sample app is a thin consumer of that API.
3. Core UVC gadget layer
- The UVC gadget plumbing lives in lib/uvc.c, lib/configfs.c, and related headers under include/uvcgadget/.
- This layer is responsible for configuring the Linux gadget through configfs, exposing the UVC function, and coordinating how frames are presented to the USB host.
4. Streaming abstraction
- video-source.h, stream.h, events.h, and timer.h define the main abstraction boundaries for runtime streaming.
- The library separates:
- frame production from
- transport into the UVC gadget
- This keeps the gadget mechanics independent from the actual video input source.
5. Video source plug-ins
- The repo supports multiple source backends, each implemented as a distinct module:
- v4l2-source.c — frames from a V4L2 device
- libcamera-source.cpp — frames from libcamera
- depthai-source.c — frames from DepthAI hardware
- jpg-source.c — JPEG-based frame input
- slideshow-source.c — synthetic slideshow source
- test-source.c — test/dummy source
- These sources plug into the same streaming interface, so the rest of the system can remain source-agnostic.
6. Frame handling and buffering
- video-buffers.c and video-buffers.h manage the buffer lifecycle for streaming frames.
- video-source.c likely provides common source orchestration and glue between a source implementation and the UVC transport.
- This buffering layer is important because UVC gadget streaming needs stable handoff between frame producers and the USB transfer side.
7. Format conversion / encoding
- mjpeg_encoder.cpp and mjpeg_encoder.hpp indicate built-in MJPEG encoding support.
- That means some sources can produce raw frames while the library converts them into a UVC-friendly compressed stream before handoff to the gadget.
8. V4L2 integration
- lib/v4l2.c, lib/v4l2.h, and lib/v4l2-source.c form a dedicated V4L2 path.
- This suggests the library can both interact with V4L2 devices and adapt them as a UVC source, making it suitable for camera passthrough scenarios.
9. Linux- and gadget-specific headers
- The include/linux/usb/g_uvc.h header and include/compat/ layer show that the project depends on Linux USB gadget UVC interfaces but also carries compatibility shims.
- This keeps the code portable across kernel/header variations while still remaining Linux-specific at runtime.
10. Public API vs internal implementation
- include/uvcgadget/*.h exposes the library API and source contracts.
- lib/ contains the implementation behind those interfaces.
- This clean split is useful for diagramming: external applications call into include/-defined APIs, and only the library internals talk to configfs, V4L2, timers, and encoders.
11. Sample application
- src/main.c is the entry point for the uvc-gadget utility.
- It likely demonstrates initializing the gadget, selecting a source, and starting the streaming loop.
- For architecture purposes, it sits at the very edge: user input/CLI in, library calls out.
12. Build and release tooling
- The repo supports Meson, CMake, and a Makefile, which is unusual and suggests a need to support multiple build environments.
- Scripts like gen-version.sh, release.sh, semver, and uvc-gadget.sh support packaging and release workflows rather than runtime behavior.
13. Execution model
- At runtime, the flow is:
- sample app or caller selects a source
- library configures the UVC gadget via configfs
- source produces frames
- buffer/streaming layer hands frames to the gadget
- USB host receives the device as a webcam
- The main architectural boundary is between frame source selection and UVC transport/configuration.
14. What matters most for a diagram
- The diagram should center on one core library surrounded by interchangeable source adapters.
- Show Linux gadget/configfs as the hardware/OS boundary.
- Show V4L2, libcamera, DepthAI, JPEG, and slideshow/test as alternative inputs feeding a common streaming pipeline.
Main entry points: video-source.c Source Glue · uvc.c UVC Core · stream.c Stream Runtime · main.c uvc-gadget
Components 21
Public API
Headers and compatibility surface exposed to applications and source backends.
- UVC API · Public gadget-facing API for configuring and controlling the UVC device.
include/uvcgadget - Source API · Common interfaces for video sources, events, streaming, and timing.
include/uvcgadget/video-source.h - MJPEG Encoder · Public encoder interface for compressed frame output.
include/uvcgadget/mjpeg_encoder.hpp - Compat Layer · Compatibility headers for Linux kernel and libc variations.
include/compat - Linux UVC · Linux USB gadget UVC definitions used by the library.
include/linux/usb/g_uvc.h
Core Library
UVC gadget setup, streaming runtime, buffering, and hardware integration.
- UVC Core · Coordinates gadget lifecycle and frame delivery to the USB host.
lib/uvc.c - ConfigFS · Builds and configures the Linux USB gadget through configfs.
lib/configfs.c - Stream Runtime · Runtime streaming orchestration and transport coordination.
lib/stream.c - Source Glue · Connects source implementations to the shared streaming and buffer pipeline.
lib/video-source.c - Frame Buffers · Manages frame buffer lifecycle and stable handoff for streaming.
lib/video-buffers.c - Events & Timer · Event handling and timing primitives used by streaming control.
lib/events.c - V4L2 Core · Direct V4L2 support used for device interaction and passthrough.
lib/v4l2.c - MJPEG Codec · Encodes raw frames into MJPEG for UVC-friendly transport.
lib/mjpeg_encoder.cpp
Source Backends
Interchangeable frame producers that feed the common streaming pipeline.
- V4L2 Source · Frames from a V4L2 device.
lib/v4l2-source.c - libcamera Source · Frames from libcamera.
lib/libcamera-source.cpp - DepthAI Source · Frames from DepthAI hardware.
lib/depthai-source.c - JPEG Source · JPEG-based frame input source.
lib/jpg-source.c - Slideshow Source · Synthetic slideshow frame generator.
lib/slideshow-source.c - Test Source · Dummy source for testing and validation.
lib/test-source.c
Sample App
Thin executable that drives the library.
- uvc-gadget · Sample executable that initializes the gadget and starts streaming.
src/main.c
Build & Release
Build system and release automation.
- Build & Release · Meson, CMake, Make, and release scripts for building and packaging.
meson.build
Connections 22
- uvc-gadget → UVC API: uses API · Application calls the public gadget API.
- uvc-gadget → Source API: selects source · Application chooses a source backend through the shared contracts.
- UVC API → UVC Core: implements · Public gadget API is backed by the core runtime.
- Source API → Source Glue: defines contract · Shared source interfaces are implemented by the source glue layer.
- UVC Core → ConfigFS: configures · Core runtime sets up the Linux UVC gadget via configfs.
- UVC Core → Stream Runtime: streams through · Core runtime hands frame delivery to the streaming pipeline.
- Stream Runtime → Frame Buffers: uses · Streaming relies on frame buffering and handoff.
- Stream Runtime → Events & Timer: coordinates · Streaming control uses events and timers.
- Source Glue → Frame Buffers: feeds · Source glue moves producer output into the buffer layer.
- Source Glue → MJPEG Codec: encodes via · Source glue can route raw frames into MJPEG encoding.
- Source Glue → V4L2 Core: adapts · Source glue can bridge V4L2 devices into the streaming pipeline.
- V4L2 Source → Source Glue: implements · V4L2 backend plugs into the shared source interface.
- libcamera Source → Source Glue: implements · libcamera backend plugs into the shared source interface.
- DepthAI Source → Source Glue: implements · DepthAI backend plugs into the shared source interface.
- JPEG Source → Source Glue: implements · JPEG input backend plugs into the shared source interface.
- Slideshow Source → Source Glue: implements · Synthetic slideshow backend plugs into the shared source interface.
- Test Source → Source Glue: implements · Test backend plugs into the shared source interface.
- MJPEG Encoder → MJPEG Codec: declares · Public header exposes the encoder interface used by the implementation.
- Compat Layer → ConfigFS: supports · Compatibility shims help the Linux-specific gadget code build across environments.
- Linux UVC → UVC Core: defines ABI · Kernel UVC definitions underpin the gadget runtime.
- Build & Release → uvc-gadget: builds · Tooling builds the sample utility.
- Build & Release → UVC Core: builds · Tooling builds the core library.