# luxonis/uvc-gadget architecture

> Architecture diagram of the GitHub repository luxonis/uvc-gadget, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-04-20). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/luxonis/uvc-gadget
- Repository: https://github.com/luxonis/uvc-gadget

## Overview

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: [`lib/video-source.c`](https://github.com/luxonis/uvc-gadget/blob/luxonis-develop/lib/video-source.c) (Source Glue), [`lib/uvc.c`](https://github.com/luxonis/uvc-gadget/blob/luxonis-develop/lib/uvc.c) (UVC Core), [`lib/stream.c`](https://github.com/luxonis/uvc-gadget/blob/luxonis-develop/lib/stream.c) (Stream Runtime), [`src/main.c`](https://github.com/luxonis/uvc-gadget/blob/luxonis-develop/src/main.c) (uvc-gadget)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_public_api["Public API"]
  node_include_uvc_api["UVC API<br/>public headers"]
  node_include_source_api["Source API<br/>stream contracts<br/>[video-source.h]"]
  node_include_mjpeg["MJPEG Encoder<br/>encode header<br/>[mjpeg_encoder.hpp]"]
  node_include_compat["Compat Layer<br/>linux shims"]
  node_linux_uvc_hdr["Linux UVC<br/>kernel header<br/>[g_uvc.h]"]
end

subgraph group_core_lib["Core Library"]
  node_uvc_core["UVC Core<br/>gadget runtime<br/>[uvc.c]"]
  node_configfs["ConfigFS<br/>gadget config<br/>[configfs.c]"]
  node_stream_runtime["Stream Runtime<br/>pipeline<br/>[stream.c]"]
  node_video_source_glue["Source Glue<br/>adapter layer<br/>[video-source.c]"]
  node_video_buffers[("Frame Buffers<br/>buffering<br/>[video-buffers.c]")]
  node_events_timer(("Events & Timer<br/>runtime support<br/>[events.c]"))
  node_v4l2_core["V4L2 Core<br/>device integration<br/>[v4l2.c]"]
  node_mjpeg_encoder{{"MJPEG Codec<br/>encoder<br/>[mjpeg_encoder.cpp]"}}
end

subgraph group_sources["Source Backends"]
  node_v4l2_source["V4L2 Source<br/>source backend<br/>[v4l2-source.c]"]
  node_libcamera_source["libcamera Source<br/>source backend"]
  node_depthai_source["DepthAI Source<br/>source backend<br/>[depthai-source.c]"]
  node_jpg_source["JPEG Source<br/>source backend<br/>[jpg-source.c]"]
  node_slideshow_source["Slideshow Source<br/>source backend<br/>[slideshow-source.c]"]
  node_test_source["Test Source<br/>source backend<br/>[test-source.c]"]
end

subgraph group_app["Sample App"]
  node_sample_app["uvc-gadget<br/>cli utility<br/>[main.c]"]
end

subgraph group_tooling["Build & Release"]
  node_build_release["Build & Release<br/>tooling<br/>[meson.build]"]
end

node_sample_app -->|"uses API"| node_include_uvc_api
node_sample_app -->|"selects source"| node_include_source_api
node_include_uvc_api -->|"implements"| node_uvc_core
node_include_source_api -->|"defines contract"| node_video_source_glue
node_uvc_core -->|"configures"| node_configfs
node_uvc_core -->|"streams through"| node_stream_runtime
node_stream_runtime -->|"uses"| node_video_buffers
node_stream_runtime -->|"coordinates"| node_events_timer
node_video_source_glue -->|"feeds"| node_video_buffers
node_video_source_glue -->|"encodes via"| node_mjpeg_encoder
node_video_source_glue -->|"adapts"| node_v4l2_core
node_v4l2_source -->|"implements"| node_video_source_glue
node_libcamera_source -->|"implements"| node_video_source_glue
node_depthai_source -->|"implements"| node_video_source_glue
node_jpg_source -->|"implements"| node_video_source_glue
node_slideshow_source -->|"implements"| node_video_source_glue
node_test_source -->|"implements"| node_video_source_glue
node_include_mjpeg -->|"declares"| node_mjpeg_encoder
node_include_compat -->|"supports"| node_configfs
node_linux_uvc_hdr -->|"defines ABI"| node_uvc_core
node_build_release -.->|"builds"| node_sample_app
node_build_release -.->|"builds"| node_uvc_core

click node_include_uvc_api "https://github.com/luxonis/uvc-gadget/tree/luxonis-develop/include/uvcgadget"
click node_include_source_api "https://github.com/luxonis/uvc-gadget/blob/luxonis-develop/include/uvcgadget/video-source.h"
click node_include_mjpeg "https://github.com/luxonis/uvc-gadget/blob/luxonis-develop/include/uvcgadget/mjpeg_encoder.hpp"
click node_include_compat "https://github.com/luxonis/uvc-gadget/tree/luxonis-develop/include/compat"
click node_linux_uvc_hdr "https://github.com/luxonis/uvc-gadget/blob/luxonis-develop/include/linux/usb/g_uvc.h"
click node_uvc_core "https://github.com/luxonis/uvc-gadget/blob/luxonis-develop/lib/uvc.c"
click node_configfs "https://github.com/luxonis/uvc-gadget/blob/luxonis-develop/lib/configfs.c"
click node_stream_runtime "https://github.com/luxonis/uvc-gadget/blob/luxonis-develop/lib/stream.c"
click node_video_source_glue "https://github.com/luxonis/uvc-gadget/blob/luxonis-develop/lib/video-source.c"
click node_video_buffers "https://github.com/luxonis/uvc-gadget/blob/luxonis-develop/lib/video-buffers.c"
click node_events_timer "https://github.com/luxonis/uvc-gadget/blob/luxonis-develop/lib/events.c"
click node_v4l2_core "https://github.com/luxonis/uvc-gadget/blob/luxonis-develop/lib/v4l2.c"
click node_mjpeg_encoder "https://github.com/luxonis/uvc-gadget/blob/luxonis-develop/lib/mjpeg_encoder.cpp"
click node_v4l2_source "https://github.com/luxonis/uvc-gadget/blob/luxonis-develop/lib/v4l2-source.c"
click node_libcamera_source "https://github.com/luxonis/uvc-gadget/blob/luxonis-develop/lib/libcamera-source.cpp"
click node_depthai_source "https://github.com/luxonis/uvc-gadget/blob/luxonis-develop/lib/depthai-source.c"
click node_jpg_source "https://github.com/luxonis/uvc-gadget/blob/luxonis-develop/lib/jpg-source.c"
click node_slideshow_source "https://github.com/luxonis/uvc-gadget/blob/luxonis-develop/lib/slideshow-source.c"
click node_test_source "https://github.com/luxonis/uvc-gadget/blob/luxonis-develop/lib/test-source.c"
click node_sample_app "https://github.com/luxonis/uvc-gadget/blob/luxonis-develop/src/main.c"
click node_build_release "https://github.com/luxonis/uvc-gadget/blob/luxonis-develop/meson.build"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_include_uvc_api,node_include_source_api,node_include_mjpeg,node_include_compat,node_linux_uvc_hdr toneBlue
class node_uvc_core,node_configfs,node_stream_runtime,node_video_source_glue,node_video_buffers,node_events_timer,node_v4l2_core,node_mjpeg_encoder toneAmber
class node_v4l2_source,node_libcamera_source,node_depthai_source,node_jpg_source,node_slideshow_source,node_test_source toneMint
class node_sample_app toneRose
class node_build_release toneIndigo
```

## Components

### Public API

Headers and compatibility surface exposed to applications and source backends.

- [UVC API](https://github.com/luxonis/uvc-gadget/tree/luxonis-develop/include/uvcgadget): public headers · Public gadget-facing API for configuring and controlling the UVC device. · `include/uvcgadget`
- [Source API](https://github.com/luxonis/uvc-gadget/blob/luxonis-develop/include/uvcgadget/video-source.h): stream contracts · Common interfaces for video sources, events, streaming, and timing. · `include/uvcgadget/video-source.h`
- [MJPEG Encoder](https://github.com/luxonis/uvc-gadget/blob/luxonis-develop/include/uvcgadget/mjpeg_encoder.hpp): encode header · Public encoder interface for compressed frame output. · `include/uvcgadget/mjpeg_encoder.hpp`
- [Compat Layer](https://github.com/luxonis/uvc-gadget/tree/luxonis-develop/include/compat): linux shims · Compatibility headers for Linux kernel and libc variations. · `include/compat`
- [Linux UVC](https://github.com/luxonis/uvc-gadget/blob/luxonis-develop/include/linux/usb/g_uvc.h): kernel header · 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](https://github.com/luxonis/uvc-gadget/blob/luxonis-develop/lib/uvc.c): gadget runtime · Coordinates gadget lifecycle and frame delivery to the USB host. · `lib/uvc.c`
- [ConfigFS](https://github.com/luxonis/uvc-gadget/blob/luxonis-develop/lib/configfs.c): gadget config · Builds and configures the Linux USB gadget through configfs. · `lib/configfs.c`
- [Stream Runtime](https://github.com/luxonis/uvc-gadget/blob/luxonis-develop/lib/stream.c): pipeline · Runtime streaming orchestration and transport coordination. · `lib/stream.c`
- [Source Glue](https://github.com/luxonis/uvc-gadget/blob/luxonis-develop/lib/video-source.c): adapter layer · Connects source implementations to the shared streaming and buffer pipeline. · `lib/video-source.c`
- [Frame Buffers](https://github.com/luxonis/uvc-gadget/blob/luxonis-develop/lib/video-buffers.c): buffering · Manages frame buffer lifecycle and stable handoff for streaming. · `lib/video-buffers.c`
- [Events & Timer](https://github.com/luxonis/uvc-gadget/blob/luxonis-develop/lib/events.c): runtime support · Event handling and timing primitives used by streaming control. · `lib/events.c`
- [V4L2 Core](https://github.com/luxonis/uvc-gadget/blob/luxonis-develop/lib/v4l2.c): device integration · Direct V4L2 support used for device interaction and passthrough. · `lib/v4l2.c`
- [MJPEG Codec](https://github.com/luxonis/uvc-gadget/blob/luxonis-develop/lib/mjpeg_encoder.cpp): encoder · 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](https://github.com/luxonis/uvc-gadget/blob/luxonis-develop/lib/v4l2-source.c): source backend · Frames from a V4L2 device. · `lib/v4l2-source.c`
- [libcamera Source](https://github.com/luxonis/uvc-gadget/blob/luxonis-develop/lib/libcamera-source.cpp): source backend · Frames from libcamera. · `lib/libcamera-source.cpp`
- [DepthAI Source](https://github.com/luxonis/uvc-gadget/blob/luxonis-develop/lib/depthai-source.c): source backend · Frames from DepthAI hardware. · `lib/depthai-source.c`
- [JPEG Source](https://github.com/luxonis/uvc-gadget/blob/luxonis-develop/lib/jpg-source.c): source backend · JPEG-based frame input source. · `lib/jpg-source.c`
- [Slideshow Source](https://github.com/luxonis/uvc-gadget/blob/luxonis-develop/lib/slideshow-source.c): source backend · Synthetic slideshow frame generator. · `lib/slideshow-source.c`
- [Test Source](https://github.com/luxonis/uvc-gadget/blob/luxonis-develop/lib/test-source.c): source backend · Dummy source for testing and validation. · `lib/test-source.c`

### Sample App

Thin executable that drives the library.

- [uvc-gadget](https://github.com/luxonis/uvc-gadget/blob/luxonis-develop/src/main.c): cli utility · Sample executable that initializes the gadget and starts streaming. · `src/main.c`

### Build & Release

Build system and release automation.

- [Build & Release](https://github.com/luxonis/uvc-gadget/blob/luxonis-develop/meson.build): tooling · Meson, CMake, Make, and release scripts for building and packaging. · `meson.build`

## Connections

- 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.
