UVC Camera Guide#
Overview#
This article describes how to use the uvc-gadget demo, including how to configure the K230 Linux device as a UVC Gadget (USB camera), and how to view real-time video streams via USB on a PC.
Feature Demonstration#
Hardware Connection#
+---------+ +---------+
| K230 |---USB---| PC |
| Linux | | (Host) |
+---------+ +---------+
|
| UART
|
+---------+
| PC |
| (Serial |
| Debug) |
+---------+
Starting uvc-gadget on K230#
Run the following command on the K230 Linux command line to start uvc-gadget:
# Start the uvc-gadget service
/root/app/uvc-gadget/uvc-gadget.sh start
# Or run the uvc-gadget program directly
uvc-gadget -v /dev/video1
Tip: Use the
v4l2-ctl --list-devicescommand to confirm the camera device, which is generally/dev/video1.
Viewing Video on PC#
Open a video application on the PC (such as PotPlayer, OBS, Cheese, etc.)
Select the corresponding camera device for K230
Set the video format to MJPEG 1080p
Open the camera to view the video stream
Note: The video stream latency is approximately 220ms.
Implementation Explanation#
Configuration Instructions#
The BR2_PACKAGE_UVC_GADGET needs to be enabled in the Buildroot configuration:
# buildroot-overlay/package/uvc-gadget/Config.in
config BR2_PACKAGE_UVC_GADGET
bool "uvc-gadget"
select BR2_PACKAGE_LIBV4L
If the uvc-gadget program is not found, please make sure the above configuration is enabled.
uvc-gadget.sh Script Description#
Script Location: buildroot-overlay/package/uvc-gadget/uvc-gadget.sh
This script is used to configure the device as a USB camera device in an embedded Linux system.
Main Functions#
Function |
Description |
|---|---|
|
Creates video frame configuration, supporting three formats (uncompressed/YUYV, MJPEG) and three resolutions (360p, 720p, 1080p) |
|
Starts the UVC device, loads kernel modules, mounts configfs, and creates the USB Gadget configuration |
|
Stops the UVC device, terminates processes, unbinds the UDC, and unloads kernel modules |
Usage#
./uvc-gadget.sh start # Start the UVC device
./uvc-gadget.sh stop # Stop the UVC device
./uvc-gadget.sh restart # Restart the UVC device
Core Configuration#
USB ID: Vendor ID:
0x29f1, Product ID:0x0104Vendor Information: Canaan
USB Controller:
91500000.usb
uvc-gadget Program Description#
K230’s uvc-gadget is based on the uvc-gadget open-source project, with added support for K230 VPU hardware encoding.
Code location: buildroot-overlay/package/uvc-gadget/
Data Flow#
Camera (V4L2) → FFmpeg → VPU Encoder → Packet Queue → UVC Streamer → USB Host
Camera Capture: Read raw frames from the V4L2 device via
libavformatHardware Encoding: Use the
mjpeg_v4l2m2mencoder for VPU-accelerated encodingPacket Queue: Encoded data is stored in a circular queue (default size 2-4)
UVC Transfer: Data is taken from the queue and written to the UVC sink buffer
Thread Model#
Thread |
Responsibilities |
|---|---|
Main |
Handles the UVC events loop, responding to sink buffer dequeue events |
VPU |
Continuously captures camera data and feeds it to the encoder |
Threads are synchronized via mutex + condition variable.
