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

Desktop only: a simulated USB host (modeled on Linux' uvcvideo) that enumerates, negotiates and streams from a UVCDevice. More...

#include <UVCHostSimulator.h>

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

Classes

struct  Frame
 A frame received from the device. More...
 
struct  HostFormat
 Format descriptor as parsed by the host. More...
 
struct  HostFrame
 Frame descriptor as parsed by the host. More...
 

Public Member Functions

 UVCHostSimulator (bool withCdc=true)
 
 ~UVCHostSimulator ()
 
bool connect (UVCDevice &dev)
 
void disconnect ()
 Simulates unplugging (bus reset)
 
bool isConnected () const
 
const std::vector< HostFormat > & formats () const
 Formats/resolutions as parsed from the descriptors.
 
const std::vector< uint8_t > & configDescriptor () const
 The complete configuration descriptor.
 
const std::string & error () const
 Last error message.
 
bool startStream (UVCFormat fmt, uint16_t width, uint16_t height, uint32_t fps=0)
 Streams format/resolution/frame rate (fps 0 = default rate)
 
bool startStream (uint8_t formatIndex, uint8_t frameIndex, uint32_t interval)
 Negotiates like Linux: GET_DEF, SET_CUR/GET_CUR probe, SET_CUR commit.
 
bool stopStream (bool useSetInterface=false)
 
bool poll ()
 
size_t pollAll (size_t maxTransfers=1000000)
 Polls until no transfer is pending (or max transfers)
 
void onFrame (std::function< void(const Frame &)> cb)
 Callback for every received frame.
 
uint32_t framesReceived () const
 
uint32_t framesWithError () const
 
uint32_t payloads () const
 
uint32_t droppedPayloads () const
 
uint32_t transfers () const
 
uint32_t zeroLengthPackets () const
 
uint16_t maxPacketSize () const
 
uint32_t urbSize () const
 
bool isStreaming () const
 
bool control (const UVCRequest &r, const uint8_t *out, uint16_t outLen, std::vector< uint8_t > *in)
 
bool getProbe (uint8_t request, bool commit, UVCProbeCommit &pc)
 Class request GET_xxx of the probe (or commit) control.
 
bool setProbe (bool commit, const UVCProbeCommit &pc)
 Class request SET_CUR of the probe (or commit) control.
 
uint8_t requestErrorCode ()
 VC_REQUEST_ERROR_CODE_CONTROL (0xFF if the request failed)
 

Protected Member Functions

bool fail (const std::string &msg)
 
const HostFormat * findFormat (uint8_t index) const
 
bool buildConfiguration (UVCDevice &dev)
 
bool parseConfiguration ()
 Parses and validates the configuration like a UVC host.
 
bool openDrivers ()
 
void resetPayload ()
 
void packet (const uint8_t *data, uint16_t len)
 One USB packet arrives at the host controller.
 
void urbComplete (const uint8_t *mem, size_t len, bool shortUrb)
 uvc_video_decode_bulk()
 
int decodeStart (const uint8_t *data, size_t len)
 uvc_video_decode_start(): returns the header length or -1 (skip)
 
void decodeData (const uint8_t *mem, size_t len)
 uvc_video_decode_data()
 
void decodeEnd (const uint8_t *header)
 uvc_video_decode_end()
 
void completeFrame ()
 

Static Protected Member Functions

static const HostFrame * findFrame (const HostFormat &f, uint8_t index)
 

Protected Attributes

bool withCdc_
 
UVCDevice * dev_ = nullptr
 
usbd_class_driver_t const * driver_ = nullptr
 
bool connected_ = false
 
bool streaming_ = false
 
std::string error_
 
std::vector< uint8_t > config_
 
std::vector< HostFormat > formats_
 
uint8_t ep_ = 0
 
uint16_t mps_ = 64
 
uint8_t vcItf_ = 0
 
UVCStreamSettings settings_
 
uint32_t maxVideoFrameSize_ = 0
 
uint32_t maxPayload_ = 0
 
uint32_t urbSize_ = 0
 
std::vector< uint8_t > urb_
 
uint32_t payloadSize_ = 0
 
uint8_t headerSize_ = 0
 
uint8_t header_ [256] = {}
 
bool skipPayload_ = false
 
int lastFid_ = -1
 
bool frameActive_ = false
 
bool frameError_ = false
 
std::vector< uint8_t > frame_
 
bool framePts_ = false
 
uint32_t framePtsValue_ = 0
 
std::function< void(const Frame &)> onFrame_
 
uint32_t frames_ = 0
 
uint32_t errorFrames_ = 0
 
uint32_t payloads_ = 0
 
uint32_t droppedPayloads_ = 0
 
uint32_t transfers_ = 0
 
uint32_t zlps_ = 0
 

Detailed Description

Desktop only: a simulated USB host (modeled on Linux' uvcvideo) that enumerates, negotiates and streams from a UVCDevice.

Constructor & Destructor Documentation

◆ UVCHostSimulator()

tinyuvc::UVCHostSimulator::UVCHostSimulator ( bool  withCdc = true)
inlineexplicit
Parameters
withCdcputs a CDC serial function in front of the camera, like on a board that also uses USB Serial (tests interface numbering)

◆ ~UVCHostSimulator()

tinyuvc::UVCHostSimulator::~UVCHostSimulator ( )
inline

Member Function Documentation

◆ buildConfiguration()

bool tinyuvc::UVCHostSimulator::buildConfiguration ( UVCDevice &  dev)
inlineprotected

◆ completeFrame()

void tinyuvc::UVCHostSimulator::completeFrame ( )
inlineprotected

◆ configDescriptor()

const std::vector< uint8_t > & tinyuvc::UVCHostSimulator::configDescriptor ( ) const
inline

The complete configuration descriptor.

◆ connect()

bool tinyuvc::UVCHostSimulator::connect ( UVCDevice &  dev)
inline

Enumerates the device: builds and validates the configuration descriptor and lets the class driver open its interfaces

◆ control()

bool tinyuvc::UVCHostSimulator::control ( const UVCRequest &  r,
const uint8_t *  out,
uint16_t  outLen,
std::vector< uint8_t > *  in 
)
inline

Runs a control request through the class driver like the TinyUSB core: setup stage, data stage, status stage. Returns false on stall.

◆ decodeData()

void tinyuvc::UVCHostSimulator::decodeData ( const uint8_t *  mem,
size_t  len 
)
inlineprotected

uvc_video_decode_data()

◆ decodeEnd()

void tinyuvc::UVCHostSimulator::decodeEnd ( const uint8_t *  header)
inlineprotected

uvc_video_decode_end()

◆ decodeStart()

int tinyuvc::UVCHostSimulator::decodeStart ( const uint8_t *  data,
size_t  len 
)
inlineprotected

uvc_video_decode_start(): returns the header length or -1 (skip)

◆ disconnect()

void tinyuvc::UVCHostSimulator::disconnect ( )
inline

Simulates unplugging (bus reset)

◆ droppedPayloads()

uint32_t tinyuvc::UVCHostSimulator::droppedPayloads ( ) const
inline

◆ error()

const std::string & tinyuvc::UVCHostSimulator::error ( ) const
inline

Last error message.

◆ fail()

bool tinyuvc::UVCHostSimulator::fail ( const std::string &  msg)
inlineprotected

◆ findFormat()

const HostFormat * tinyuvc::UVCHostSimulator::findFormat ( uint8_t  index) const
inlineprotected

◆ findFrame()

static const HostFrame * tinyuvc::UVCHostSimulator::findFrame ( const HostFormat &  f,
uint8_t  index 
)
inlinestaticprotected

◆ formats()

const std::vector< HostFormat > & tinyuvc::UVCHostSimulator::formats ( ) const
inline

Formats/resolutions as parsed from the descriptors.

◆ framesReceived()

uint32_t tinyuvc::UVCHostSimulator::framesReceived ( ) const
inline

◆ framesWithError()

uint32_t tinyuvc::UVCHostSimulator::framesWithError ( ) const
inline

◆ getProbe()

bool tinyuvc::UVCHostSimulator::getProbe ( uint8_t  request,
bool  commit,
UVCProbeCommit &  pc 
)
inline

Class request GET_xxx of the probe (or commit) control.

◆ isConnected()

bool tinyuvc::UVCHostSimulator::isConnected ( ) const
inline

◆ isStreaming()

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

◆ maxPacketSize()

uint16_t tinyuvc::UVCHostSimulator::maxPacketSize ( ) const
inline

◆ onFrame()

void tinyuvc::UVCHostSimulator::onFrame ( std::function< void(const Frame &)>  cb)
inline

Callback for every received frame.

◆ openDrivers()

bool tinyuvc::UVCHostSimulator::openDrivers ( )
inlineprotected

Like process_set_config() of TinyUSB: offers each interface to the application driver; interfaces it does not claim belong to others

◆ packet()

void tinyuvc::UVCHostSimulator::packet ( const uint8_t *  data,
uint16_t  len 
)
inlineprotected

One USB packet arrives at the host controller.

◆ parseConfiguration()

bool tinyuvc::UVCHostSimulator::parseConfiguration ( )
inlineprotected

Parses and validates the configuration like a UVC host.

◆ payloads()

uint32_t tinyuvc::UVCHostSimulator::payloads ( ) const
inline

◆ poll()

bool tinyuvc::UVCHostSimulator::poll ( )
inline

Reads the pending IN transfer (if any) like the host controller and returns true if there was one

◆ pollAll()

size_t tinyuvc::UVCHostSimulator::pollAll ( size_t  maxTransfers = 1000000)
inline

Polls until no transfer is pending (or max transfers)

◆ requestErrorCode()

uint8_t tinyuvc::UVCHostSimulator::requestErrorCode ( )
inline

VC_REQUEST_ERROR_CODE_CONTROL (0xFF if the request failed)

◆ resetPayload()

void tinyuvc::UVCHostSimulator::resetPayload ( )
inlineprotected

◆ setProbe()

bool tinyuvc::UVCHostSimulator::setProbe ( bool  commit,
const UVCProbeCommit &  pc 
)
inline

Class request SET_CUR of the probe (or commit) control.

◆ startStream() [1/2]

bool tinyuvc::UVCHostSimulator::startStream ( uint8_t  formatIndex,
uint8_t  frameIndex,
uint32_t  interval 
)
inline

Negotiates like Linux: GET_DEF, SET_CUR/GET_CUR probe, SET_CUR commit.

◆ startStream() [2/2]

bool tinyuvc::UVCHostSimulator::startStream ( UVCFormat  fmt,
uint16_t  width,
uint16_t  height,
uint32_t  fps = 0 
)
inline

Streams format/resolution/frame rate (fps 0 = default rate)

◆ stopStream()

bool tinyuvc::UVCHostSimulator::stopStream ( bool  useSetInterface = false)
inline

Stops streaming like Linux (clear halt of the bulk endpoint) or with SET_INTERFACE(0) (useSetInterface)

◆ transfers()

uint32_t tinyuvc::UVCHostSimulator::transfers ( ) const
inline

◆ urbComplete()

void tinyuvc::UVCHostSimulator::urbComplete ( const uint8_t *  mem,
size_t  len,
bool  shortUrb 
)
inlineprotected

uvc_video_decode_bulk()

◆ urbSize()

uint32_t tinyuvc::UVCHostSimulator::urbSize ( ) const
inline

◆ zeroLengthPackets()

uint32_t tinyuvc::UVCHostSimulator::zeroLengthPackets ( ) const
inline

Member Data Documentation

◆ config_

std::vector<uint8_t> tinyuvc::UVCHostSimulator::config_
protected

◆ connected_

bool tinyuvc::UVCHostSimulator::connected_ = false
protected

◆ dev_

UVCDevice* tinyuvc::UVCHostSimulator::dev_ = nullptr
protected

◆ driver_

usbd_class_driver_t const* tinyuvc::UVCHostSimulator::driver_ = nullptr
protected

◆ droppedPayloads_

uint32_t tinyuvc::UVCHostSimulator::droppedPayloads_ = 0
protected

◆ ep_

uint8_t tinyuvc::UVCHostSimulator::ep_ = 0
protected

◆ error_

std::string tinyuvc::UVCHostSimulator::error_
protected

◆ errorFrames_

uint32_t tinyuvc::UVCHostSimulator::errorFrames_ = 0
protected

◆ formats_

std::vector<HostFormat> tinyuvc::UVCHostSimulator::formats_
protected

◆ frame_

std::vector<uint8_t> tinyuvc::UVCHostSimulator::frame_
protected

◆ frameActive_

bool tinyuvc::UVCHostSimulator::frameActive_ = false
protected

◆ frameError_

bool tinyuvc::UVCHostSimulator::frameError_ = false
protected

◆ framePts_

bool tinyuvc::UVCHostSimulator::framePts_ = false
protected

◆ framePtsValue_

uint32_t tinyuvc::UVCHostSimulator::framePtsValue_ = 0
protected

◆ frames_

uint32_t tinyuvc::UVCHostSimulator::frames_ = 0
protected

◆ header_

uint8_t tinyuvc::UVCHostSimulator::header_[256] = {}
protected

◆ headerSize_

uint8_t tinyuvc::UVCHostSimulator::headerSize_ = 0
protected

◆ lastFid_

int tinyuvc::UVCHostSimulator::lastFid_ = -1
protected

◆ maxPayload_

uint32_t tinyuvc::UVCHostSimulator::maxPayload_ = 0
protected

◆ maxVideoFrameSize_

uint32_t tinyuvc::UVCHostSimulator::maxVideoFrameSize_ = 0
protected

◆ mps_

uint16_t tinyuvc::UVCHostSimulator::mps_ = 64
protected

◆ onFrame_

std::function<void(const Frame &)> tinyuvc::UVCHostSimulator::onFrame_
protected

◆ payloads_

uint32_t tinyuvc::UVCHostSimulator::payloads_ = 0
protected

◆ payloadSize_

uint32_t tinyuvc::UVCHostSimulator::payloadSize_ = 0
protected

◆ settings_

UVCStreamSettings tinyuvc::UVCHostSimulator::settings_
protected

◆ skipPayload_

bool tinyuvc::UVCHostSimulator::skipPayload_ = false
protected

◆ streaming_

bool tinyuvc::UVCHostSimulator::streaming_ = false
protected

◆ transfers_

uint32_t tinyuvc::UVCHostSimulator::transfers_ = 0
protected

◆ urb_

std::vector<uint8_t> tinyuvc::UVCHostSimulator::urb_
protected

◆ urbSize_

uint32_t tinyuvc::UVCHostSimulator::urbSize_ = 0
protected

◆ vcItf_

uint8_t tinyuvc::UVCHostSimulator::vcItf_ = 0
protected

◆ withCdc_

bool tinyuvc::UVCHostSimulator::withCdc_
protected

◆ zlps_

uint32_t tinyuvc::UVCHostSimulator::zlps_ = 0
protected

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