TinyUVC 0.1.0
Header-only USB Video Class (UVC) device: turn an MCU into a USB webcam
Loading...
Searching...
No Matches
Public Types | Public Member Functions | Static Public Member Functions | Protected Member Functions | Static Protected Member Functions | Protected Attributes | List of all members
tinyuvc::UVCDevice Class Reference

USB Video Class (UVC 1.1) device - a USB webcam. More...

#include <UVCDevice.h>

Collaboration diagram for tinyuvc::UVCDevice:
Collaboration graph
[legend]

Public Types

using XferFn = bool(*)(uint8_t ep, uint8_t *buf, uint16_t len)
 Transfer function: starts an IN transfer, returns false on error.
 

Public Member Functions

 UVCDevice ()=default
 
 ~UVCDevice ()
 
 UVCDevice (const UVCDevice &)=delete
 
UVCDevice & operator= (const UVCDevice &)=delete
 
bool addResolution (UVCFormat format, uint16_t width, uint16_t height, uint8_t fps, uint32_t maxFrameSize=0)
 
bool addResolution (UVCFormat format, uint16_t width, uint16_t height, std::initializer_list< uint8_t > fpsList, uint32_t maxFrameSize=0)
 Adds a resolution with several frame rates; the first is the default.
 
bool addResolution (UVCFormat format, uint16_t width, uint16_t height, const uint8_t *fpsList, size_t fpsCount, uint32_t maxFrameSize=0)
 Adds a resolution with fpsCount frame rates; the first is the default.
 
void clearResolutions ()
 Removes all formats/resolutions (before begin())
 
void setName (const char *name)
 
const char * name () const
 
void setPayloadSize (uint32_t size)
 
uint32_t payloadSize () const
 
void setTimestamps (bool enabled)
 Adds a presentation time stamp (PTS, microseconds) to every payload.
 
void setStreamCallback (void(*cb)(bool streaming, const UVCStreamSettings &settings, void *ref), void *ref=nullptr)
 
bool begin ()
 
void end ()
 Stops the camera function.
 
bool isActive () const
 true if begin() succeeded
 
bool isStreaming () const
 true while the host is streaming (between commit and stop)
 
bool isReady () const
 
bool isSending () const
 
UVCStreamSettings settings () const
 Format, resolution and frame rate the host selected.
 
uint32_t streamGeneration () const
 Incremented whenever the host starts or stops a stream.
 
bool writeFrame (const uint8_t *data, size_t len, PixelFormat fmt)
 Sends a frame. Returns false (and the frame is not sent) if the device is not ready or the frame does not fit the format the host selected:
 
bool waitReady (uint32_t timeoutMs)
 Waits until isReady() (true) or the timeout or end of streaming (false)
 
uint32_t framesSent () const
 Number of frames that were completely sent.
 
uint32_t framesDropped () const
 Number of frames that were rejected or aborted.
 
const UVCFormatSpec * formats () const
 Formats as defined with addResolution()
 
uint8_t formatCount () const
 
uint16_t descriptorLength () const
 Length of the function descriptors.
 
uint16_t buildDescriptor (uint8_t *buf, uint16_t bufLen, uint8_t controlItf, uint8_t endpointIn, uint16_t maxPacketSize, uint8_t stringIndex)
 
uint8_t controlInterface () const
 
uint8_t streamingInterface () const
 
uint8_t endpoint () const
 
void usbOpened (XferFn xfer, uint16_t maxPacketSize)
 The host configured the device: the bulk endpoint is open.
 
void usbReset ()
 Bus reset / unconfigured.
 
UVCControlResult handleControlSetup (const UVCRequest &r, uint8_t *&data, uint16_t &len)
 Handles the setup stage of a request to our interfaces/endpoint.
 
bool handleControlData (const UVCRequest &r, uint16_t len)
 
void usbTransferComplete (bool ok)
 A transfer on our endpoint completed.
 

Static Public Member Functions

static UVCDevice *& instance ()
 The active device.
 

Protected Member Functions

UVCFormatSpec * findFormat (UVCFormat format)
 
UVCControlResult stall (uint8_t error)
 
UVCControlResult reply (const void *src, uint16_t n, const UVCRequest &r, uint8_t *&data, uint16_t &len)
 
UVCControlResult replyProbe (const UVCProbeCommit &pc, const UVCRequest &r, uint8_t *&data, uint16_t &len)
 
UVCControlResult handleStandard (const UVCRequest &r, uint8_t *&data, uint16_t &len)
 
UVCProbeCommit negotiate (const UVCProbeCommit &in) const
 Validates/completes a probe or commit structure from the host.
 
UVCProbeCommit limit (bool max) const
 GET_MIN / GET_MAX values.
 
void startStream (const UVCProbeCommit &pc)
 
void stopStream ()
 
void finishFrame (bool complete)
 
bool startTransfer ()
 
void kick ()
 Starts a transfer if none is in flight (callable from any context)
 

Static Protected Member Functions

static uint32_t effectivePayloadSize (uint32_t requested, uint16_t mps)
 

Protected Attributes

UVCFormatSpec formats_ [2]
 
uint8_t formatCount_ = 0
 
const char * name_ = "TinyUVC Camera"
 
uint32_t payloadSizeCfg_ = 16384
 
uint32_t payloadSize_ = 0
 
uint16_t maxPacketSize_ = 64
 
bool withPts_ = false
 
bool active_ = false
 
uint8_t * buffer_ = nullptr
 
uint32_t bufferSize_ = 0
 
XferFn xfer_ = nullptr
 
uint8_t controlItf_ = 0
 
uint8_t endpoint_ = 0x81
 
uint8_t errorCode_ = uvc_spec::ERR_NONE
 
uint8_t ctrlBuf_ [48] = {}
 
UVCProbeCommit probe_
 
UVCProbeCommit commit_
 
UVCStreamSettings settings_
 
uint32_t maxFrameSize_ = 0
 
void(* streamCb_ )(bool, const UVCStreamSettings &, void *) = nullptr
 
void * streamCbRef_ = nullptr
 
std::atomic< bool > streaming_ {false}
 
std::atomic< bool > framePending_ {false}
 
std::atomic< bool > inFlight_ {false}
 
std::atomic< uint32_t > generation_ {0}
 
const uint8_t *volatile frameData_ = nullptr
 
volatile size_t frameLen_ = 0
 
volatile PixelFormat frameFmt_ = PixelFormat::MJPEG
 
volatile uint32_t framePts_ = 0
 
volatile uint32_t sent_ = 0
 
volatile uint32_t dropped_ = 0
 
UVCPayloadEncoder encoder_
 
uint32_t encoderGeneration_ = 0
 
bool fid_ = false
 
bool zlpPending_ = false
 

Detailed Description

USB Video Class (UVC 1.1) device - a USB webcam.

Declare the formats/resolutions/frame rates you can deliver, call begin(), then hand over frames with writeFrame() whenever isReady():

void setup() {
}
void loop() {
if (uvc.isReady()) uvc.writeFrame(jpeg, jpegLen, PixelFormat::MJPEG);
}
USB Video Class (UVC 1.1) device - a USB webcam.
Definition UVCDevice.h:95
TinyGPU DisplayDriver that sends the written surfaces as webcam frames to the USB host: uncompressed ...
Definition TinyGPU.h:110
bool begin() override
Starts the UVC device (if it is not running yet)
Definition TinyGPU.h:159
bool addResolution(UVCFormat format, uint16_t width, uint16_t height, std::initializer_list< uint8_t > fps, uint32_t maxJpegSize=0)
Definition TinyGPU.h:127
@ MJPEG
Motion JPEG: every frame is a complete JPEG image.
@ MJPEG
one complete JPEG image (for UVCFormat::MJPEG)

The frame data must stay valid until isReady() is true again (it is copied payload by payload into the transfer buffer while it is sent). Frames are sent over a bulk endpoint, which all major operating systems support (Linux, Windows, macOS).

Only one UVCDevice can be active.

Member Typedef Documentation

◆ XferFn

Transfer function: starts an IN transfer, returns false on error.

Constructor & Destructor Documentation

◆ UVCDevice() [1/2]

tinyuvc::UVCDevice::UVCDevice ( )
default

◆ ~UVCDevice()

tinyuvc::UVCDevice::~UVCDevice ( )
inline

◆ UVCDevice() [2/2]

tinyuvc::UVCDevice::UVCDevice ( const UVCDevice &  )
delete

Member Function Documentation

◆ addResolution() [1/3]

bool tinyuvc::UVCDevice::addResolution ( UVCFormat  format,
uint16_t  width,
uint16_t  height,
const uint8_t *  fpsList,
size_t  fpsCount,
uint32_t  maxFrameSize = 0 
)
inline

Adds a resolution with fpsCount frame rates; the first is the default.

◆ addResolution() [2/3]

bool tinyuvc::UVCDevice::addResolution ( UVCFormat  format,
uint16_t  width,
uint16_t  height,
std::initializer_list< uint8_t >  fpsList,
uint32_t  maxFrameSize = 0 
)
inline

Adds a resolution with several frame rates; the first is the default.

◆ addResolution() [3/3]

bool tinyuvc::UVCDevice::addResolution ( UVCFormat  format,
uint16_t  width,
uint16_t  height,
uint8_t  fps,
uint32_t  maxFrameSize = 0 
)
inline

Adds a resolution with one frame rate. The format is created on first use; the first resolution of a format is the host's default. maxFrameSize: max bytes of one frame (0 = width * height * 2).

◆ begin()

bool tinyuvc::UVCDevice::begin ( )
inline

Registers the camera with the USB stack. See the README for the platform specific requirements (USB stack / USB mode menu settings).

◆ buildDescriptor()

uint16_t tinyuvc::UVCDevice::buildDescriptor ( uint8_t *  buf,
uint16_t  bufLen,
uint8_t  controlItf,
uint8_t  endpointIn,
uint16_t  maxPacketSize,
uint8_t  stringIndex 
)
inline

Builds the function descriptors and remembers the assigned interface number and endpoint

◆ clearResolutions()

void tinyuvc::UVCDevice::clearResolutions ( )
inline

Removes all formats/resolutions (before begin())

◆ controlInterface()

uint8_t tinyuvc::UVCDevice::controlInterface ( ) const
inline

◆ descriptorLength()

uint16_t tinyuvc::UVCDevice::descriptorLength ( ) const
inline

Length of the function descriptors.

◆ effectivePayloadSize()

static uint32_t tinyuvc::UVCDevice::effectivePayloadSize ( uint32_t  requested,
uint16_t  mps 
)
inlinestaticprotected

Payload size actually used: a multiple of the max packet size, and at most 32 packets. Linux (uvcvideo) reads bulk endpoints with URBs of at most 32 packets: a full payload that ends inside a URB has no short packet that terminates it, so the next payload would be glued to it.

◆ end()

void tinyuvc::UVCDevice::end ( )
inline

Stops the camera function.

◆ endpoint()

uint8_t tinyuvc::UVCDevice::endpoint ( ) const
inline

◆ findFormat()

UVCFormatSpec * tinyuvc::UVCDevice::findFormat ( UVCFormat  format)
inlineprotected

◆ finishFrame()

void tinyuvc::UVCDevice::finishFrame ( bool  complete)
inlineprotected

◆ formatCount()

uint8_t tinyuvc::UVCDevice::formatCount ( ) const
inline

◆ formats()

const UVCFormatSpec * tinyuvc::UVCDevice::formats ( ) const
inline

Formats as defined with addResolution()

◆ framesDropped()

uint32_t tinyuvc::UVCDevice::framesDropped ( ) const
inline

Number of frames that were rejected or aborted.

◆ framesSent()

uint32_t tinyuvc::UVCDevice::framesSent ( ) const
inline

Number of frames that were completely sent.

◆ handleControlData()

bool tinyuvc::UVCDevice::handleControlData ( const UVCRequest &  r,
uint16_t  len 
)
inline

Handles the data of a SET request (received into the buffer that handleControlSetup() returned); false = stall

◆ handleControlSetup()

UVCControlResult tinyuvc::UVCDevice::handleControlSetup ( const UVCRequest &  r,
uint8_t *&  data,
uint16_t &  len 
)
inline

Handles the setup stage of a request to our interfaces/endpoint.

◆ handleStandard()

UVCControlResult tinyuvc::UVCDevice::handleStandard ( const UVCRequest &  r,
uint8_t *&  data,
uint16_t &  len 
)
inlineprotected

◆ instance()

static UVCDevice *& tinyuvc::UVCDevice::instance ( )
inlinestatic

The active device.

◆ isActive()

bool tinyuvc::UVCDevice::isActive ( ) const
inline

true if begin() succeeded

◆ isReady()

bool tinyuvc::UVCDevice::isReady ( ) const
inline

true if a new frame can be written: streaming and the previous frame was completely handed to the USB stack

◆ isSending()

bool tinyuvc::UVCDevice::isSending ( ) const
inline

true while the device still reads the data passed to writeFrame(): the data must not be changed or freed before this is false

◆ isStreaming()

bool tinyuvc::UVCDevice::isStreaming ( ) const
inline

true while the host is streaming (between commit and stop)

◆ kick()

void tinyuvc::UVCDevice::kick ( )
inlineprotected

Starts a transfer if none is in flight (callable from any context)

◆ limit()

UVCProbeCommit tinyuvc::UVCDevice::limit ( bool  max) const
inlineprotected

GET_MIN / GET_MAX values.

◆ name()

const char * tinyuvc::UVCDevice::name ( ) const
inline

◆ negotiate()

UVCProbeCommit tinyuvc::UVCDevice::negotiate ( const UVCProbeCommit &  in) const
inlineprotected

Validates/completes a probe or commit structure from the host.

◆ operator=()

UVCDevice & tinyuvc::UVCDevice::operator= ( const UVCDevice &  )
delete

◆ payloadSize()

uint32_t tinyuvc::UVCDevice::payloadSize ( ) const
inline

◆ reply()

UVCControlResult tinyuvc::UVCDevice::reply ( const void *  src,
uint16_t  n,
const UVCRequest &  r,
uint8_t *&  data,
uint16_t &  len 
)
inlineprotected

◆ replyProbe()

UVCControlResult tinyuvc::UVCDevice::replyProbe ( const UVCProbeCommit &  pc,
const UVCRequest &  r,
uint8_t *&  data,
uint16_t &  len 
)
inlineprotected

◆ setName()

void tinyuvc::UVCDevice::setName ( const char *  name)
inline

Name shown by the host (function and interface string); the string must stay valid

◆ setPayloadSize()

void tinyuvc::UVCDevice::setPayloadSize ( uint32_t  size)
inline

Size of one payload transfer (see TINYUVC_PAYLOAD_SIZE); call before begin()

◆ setStreamCallback()

void tinyuvc::UVCDevice::setStreamCallback ( void(*)(bool streaming, const UVCStreamSettings &settings, void *ref)  cb,
void *  ref = nullptr 
)
inline

Callback invoked from the USB task when the host starts (streaming = true) or stops a stream. Keep it short.

◆ setTimestamps()

void tinyuvc::UVCDevice::setTimestamps ( bool  enabled)
inline

Adds a presentation time stamp (PTS, microseconds) to every payload.

◆ settings()

UVCStreamSettings tinyuvc::UVCDevice::settings ( ) const
inline

Format, resolution and frame rate the host selected.

◆ stall()

UVCControlResult tinyuvc::UVCDevice::stall ( uint8_t  error)
inlineprotected

◆ startStream()

void tinyuvc::UVCDevice::startStream ( const UVCProbeCommit &  pc)
inlineprotected

◆ startTransfer()

bool tinyuvc::UVCDevice::startTransfer ( )
inlineprotected

Starts the next transfer if there is something to send; the caller owns inFlight_. Returns false if nothing was started.

◆ stopStream()

void tinyuvc::UVCDevice::stopStream ( )
inlineprotected

◆ streamGeneration()

uint32_t tinyuvc::UVCDevice::streamGeneration ( ) const
inline

Incremented whenever the host starts or stops a stream.

◆ streamingInterface()

uint8_t tinyuvc::UVCDevice::streamingInterface ( ) const
inline

◆ usbOpened()

void tinyuvc::UVCDevice::usbOpened ( XferFn  xfer,
uint16_t  maxPacketSize 
)
inline

The host configured the device: the bulk endpoint is open.

◆ usbReset()

void tinyuvc::UVCDevice::usbReset ( )
inline

Bus reset / unconfigured.

◆ usbTransferComplete()

void tinyuvc::UVCDevice::usbTransferComplete ( bool  ok)
inline

A transfer on our endpoint completed.

◆ waitReady()

bool tinyuvc::UVCDevice::waitReady ( uint32_t  timeoutMs)
inline

Waits until isReady() (true) or the timeout or end of streaming (false)

◆ writeFrame()

bool tinyuvc::UVCDevice::writeFrame ( const uint8_t *  data,
size_t  len,
PixelFormat  fmt 
)
inline

Sends a frame. Returns false (and the frame is not sent) if the device is not ready or the frame does not fit the format the host selected:

  • MJPEG: fmt must be PixelFormat::MJPEG
  • YUY2: any raw format with exactly width * height pixels; it is converted to YUY2 while it is sent

The data must stay valid until isReady() is true again.

Member Data Documentation

◆ active_

bool tinyuvc::UVCDevice::active_ = false
protected

◆ buffer_

uint8_t* tinyuvc::UVCDevice::buffer_ = nullptr
protected

◆ bufferSize_

uint32_t tinyuvc::UVCDevice::bufferSize_ = 0
protected

◆ commit_

UVCProbeCommit tinyuvc::UVCDevice::commit_
protected

◆ controlItf_

uint8_t tinyuvc::UVCDevice::controlItf_ = 0
protected

◆ ctrlBuf_

uint8_t tinyuvc::UVCDevice::ctrlBuf_[48] = {}
protected

◆ dropped_

volatile uint32_t tinyuvc::UVCDevice::dropped_ = 0
protected

◆ encoder_

UVCPayloadEncoder tinyuvc::UVCDevice::encoder_
protected

◆ encoderGeneration_

uint32_t tinyuvc::UVCDevice::encoderGeneration_ = 0
protected

◆ endpoint_

uint8_t tinyuvc::UVCDevice::endpoint_ = 0x81
protected

◆ errorCode_

uint8_t tinyuvc::UVCDevice::errorCode_ = uvc_spec::ERR_NONE
protected

◆ fid_

bool tinyuvc::UVCDevice::fid_ = false
protected

◆ formatCount_

uint8_t tinyuvc::UVCDevice::formatCount_ = 0
protected

◆ formats_

UVCFormatSpec tinyuvc::UVCDevice::formats_[2]
protected

◆ frameData_

const uint8_t* volatile tinyuvc::UVCDevice::frameData_ = nullptr
protected

◆ frameFmt_

volatile PixelFormat tinyuvc::UVCDevice::frameFmt_ = PixelFormat::MJPEG
protected

◆ frameLen_

volatile size_t tinyuvc::UVCDevice::frameLen_ = 0
protected

◆ framePending_

std::atomic<bool> tinyuvc::UVCDevice::framePending_ {false}
protected

◆ framePts_

volatile uint32_t tinyuvc::UVCDevice::framePts_ = 0
protected

◆ generation_

std::atomic<uint32_t> tinyuvc::UVCDevice::generation_ {0}
protected

◆ inFlight_

std::atomic<bool> tinyuvc::UVCDevice::inFlight_ {false}
protected

◆ maxFrameSize_

uint32_t tinyuvc::UVCDevice::maxFrameSize_ = 0
protected

◆ maxPacketSize_

uint16_t tinyuvc::UVCDevice::maxPacketSize_ = 64
protected

◆ name_

const char* tinyuvc::UVCDevice::name_ = "TinyUVC Camera"
protected

◆ payloadSize_

uint32_t tinyuvc::UVCDevice::payloadSize_ = 0
protected

◆ payloadSizeCfg_

uint32_t tinyuvc::UVCDevice::payloadSizeCfg_ = 16384
protected

◆ probe_

UVCProbeCommit tinyuvc::UVCDevice::probe_
protected

◆ sent_

volatile uint32_t tinyuvc::UVCDevice::sent_ = 0
protected

◆ settings_

UVCStreamSettings tinyuvc::UVCDevice::settings_
protected

◆ streamCb_

void(* tinyuvc::UVCDevice::streamCb_) (bool, const UVCStreamSettings &, void *) = nullptr
protected

◆ streamCbRef_

void* tinyuvc::UVCDevice::streamCbRef_ = nullptr
protected

◆ streaming_

std::atomic<bool> tinyuvc::UVCDevice::streaming_ {false}
protected

◆ withPts_

bool tinyuvc::UVCDevice::withPts_ = false
protected

◆ xfer_

XferFn tinyuvc::UVCDevice::xfer_ = nullptr
protected

◆ zlpPending_

bool tinyuvc::UVCDevice::zlpPending_ = false
protected

The documentation for this class was generated from the following file: