Skip to main content

Overview

The USB_STREAM class is the main C++ wrapper for the ESP32 USB Stream library. It provides a high-level interface for managing USB video (UVC) and audio (UAC) streaming from connected USB devices.

Class Definition

class USB_STREAM {
  // Public interface
};

Constructor

Creates a new USB_STREAM object.
USB_STREAM();

Example

#include "USB_STREAM.h"

USB_STREAM usb;

void setup() {
  // Constructor is called automatically when object is created
}

Destructor

Destroys the USB_STREAM object and cleans up resources.
~USB_STREAM();
The destructor is called automatically when the object goes out of scope. It ensures proper cleanup of internal resources.

Public Member Variables

The class exposes several public member variables for storing user-defined callback functions and arguments:

Callback Storage

void *_user_mic_frame_cb_arg
Storage for microphone frame callback argument.
void *_user_frame_cb_arg
Storage for video frame callback argument.
uvc_frame_callback_t _user_frame_cb
User-defined callback function for handling UVC video frames.
mic_callback_t _user_mic_frame_cb
User-defined callback function for handling microphone audio frames.

State Callback Type

typedef void (*StateChangeCallback)(usb_stream_state_t event, void *arg);
Callback function type for USB device connection state changes.
event
usb_stream_state_t
Connection state event:
  • STREAM_CONNECTED (0): Device connected
  • STREAM_DISCONNECTED: Device disconnected
arg
void*
User-defined argument passed during callback registration

Private Member Variables

The class maintains internal state for video and audio configurations:
uint16_t _frame_width;           // Video frame width
uint16_t _frame_height;          // Video frame height
uint32_t _frame_interval;        // Video frame interval
uint8_t _spk_ch_num;            // Speaker channel numbers
uint16_t _spk_bit_resolution;   // Speaker bit resolution
uint32_t _spk_samples_frequency; // Speaker sample frequency
uint32_t _spk_buf_size;         // Speaker buffer size
uint8_t _mic_ch_num;            // Microphone channel numbers
uint16_t _mic_bit_resolution;   // Microphone bit resolution
uint32_t _mic_samples_frequency; // Microphone sample frequency
uint32_t _mic_buf_size;         // Microphone buffer size
These private variables are managed internally by the class methods. Users should not access them directly.

State Management

The USB_STREAM class manages the lifecycle of USB streaming through state transitions:
  1. Initial State: Object created but not configured
  2. Configured State: Video/audio parameters set via configuration methods
  3. Running State: Streaming active after calling start()
  4. Stopped State: Streaming stopped, resources freed
Always call stop() before destroying the USB_STREAM object to ensure proper resource cleanup.

Thread Safety

Callback functions are executed in the context of internal FreeRTOS tasks. Keep callback execution time minimal and avoid blocking operations.

Build docs developers (and LLMs) love