10#include <initializer_list>
23bool begin(UVCDevice &dev);
25void end(UVCDevice &dev);
108 uint8_t fps, uint32_t maxFrameSize = 0) {
109 return addResolution(format, width, height, {fps}, maxFrameSize);
114 std::initializer_list<uint8_t> fpsList,
115 uint32_t maxFrameSize = 0) {
117 fpsList.size(), maxFrameSize);
122 const uint8_t *fpsList,
size_t fpsCount,
123 uint32_t maxFrameSize = 0) {
125 TINYUVC_LOGE(
"addResolution() must be called before begin()");
129 for (
size_t i = 0; i < fpsCount; i++) anyFps |= fpsList[i] != 0;
130 if (width == 0 || height == 0 || !anyFps) {
131 TINYUVC_LOGE(
"addResolution(): invalid size or no frame rate");
135 TINYUVC_LOGE(
"addResolution(): YUY2 needs an even width");
149 TINYUVC_LOGE(
"too many resolutions (TINYUVC_MAX_FRAMES)");
156 const uint32_t raw = (uint32_t)width * height * 2;
158 : maxFrameSize != 0 ? maxFrameSize
160 for (
size_t i = 0; i < fpsCount; i++) {
191 void *ref =
nullptr) {
201 TINYUVC_LOGE(
"begin(): no resolution defined - call addResolution()");
205 if (inst !=
nullptr && inst !=
this) {
206 TINYUVC_LOGE(
"begin(): only one UVCDevice can be active");
234 TINYUVC_LOGI(
"UVC device started: %d format(s), payload %u, packet %u",
289 if (data ==
nullptr || len == 0)
return false;
298 TINYUVC_LOGW(
"writeFrame(): JPEG of %u bytes exceeds max frame size %u",
306 TINYUVC_LOGW(
"writeFrame(): host expects %ux%u YUY2: got %u bytes %s",
362 uint8_t endpointIn, uint16_t maxPacketSize,
363 uint8_t stringIndex) {
365 endpoint_ = (uint8_t)(endpointIn | 0x80);
381 using XferFn = bool (*)(uint8_t ep, uint8_t *buf, uint16_t len);
390 TINYUVC_LOGI(
"USB configured (interfaces %d/%d, endpoint 0x%02x)",
412 const uint8_t itf = (uint8_t)(r.
wIndex & 0xFF);
413 const uint8_t entity = (uint8_t)(r.
wIndex >> 8);
414 const uint8_t selector = (uint8_t)(r.
wValue >> 8);
420 static const uint8_t info = 0x01;
421 return reply(&info, 1, r, data, len);
455 return reply(l, 2, r, data, len);
458 const uint8_t info = 0x03;
459 return reply(&info, 1, r, data, len);
473 const uint8_t itf = (uint8_t)(r.
wIndex & 0xFF);
474 const uint8_t selector = (uint8_t)(r.
wValue >> 8);
559 uint32_t size = requested;
560 if (size > 32u * mps) size = 32u * mps;
561 if (size < 2u * mps) size = 2u * mps;
562 return size - size % mps;
577 uint8_t *&data, uint16_t &len) {
587 uint8_t *&data, uint16_t &len) {
593 constexpr uint8_t GET_INTERFACE = 0x0A, SET_INTERFACE = 0x0B;
596 constexpr uint8_t CLEAR_FEATURE = 0x01;
607 TINYUVC_LOGI(
"host stopped the stream (set interface)");
613 const uint8_t alt = 0;
614 return reply(&alt, 1, r, data, len);
708 if (
xfer_ ==
nullptr ||
buffer_ ==
nullptr)
return false;
739 for (
int i = 0; i < 3; i++) {
740 bool expected =
false;
741 if (!
inFlight_.compare_exchange_strong(expected,
true))
return;
#define TINYUVC_MAX_FRAMES
Max number of resolutions (frame descriptors) per format.
Definition UVCConfig.h:16
#define TINYUVC_MAX_FORMATS
Max number of video formats (e.g. MJPEG + YUY2) a UVCDevice offers.
Definition UVCConfig.h:11
#define TINYUVC_MAX_INTERVALS
Max number of frame rates per resolution.
Definition UVCConfig.h:21
#define TINYUVC_DEFAULT_NAME
Definition UVCConfig.h:44
#define TINYUVC_PAYLOAD_SIZE
Definition UVCConfig.h:32
Builds the USB descriptors of a UVC 1.1 camera function: an Interface Association,...
Minimal printf-style logger. On Arduino it writes to a Print (default: Serial), on the desktop to std...
#define TINYUVC_LOGI(...)
Definition UVCLogger.h:79
#define TINYUVC_LOGE(...)
Definition UVCLogger.h:75
#define TINYUVC_LOGD(...)
Definition UVCLogger.h:81
#define TINYUVC_LOGW(...)
Definition UVCLogger.h:77
Splits one video frame into UVC payloads (payload header + data).
static uint16_t length(const UVCDescriptorParams &p)
Length in bytes of the descriptors for the given parameters.
Definition UVCDescriptor.h:39
static uint16_t build(const UVCDescriptorParams &p, uint8_t *buf, uint16_t bufLen)
Definition UVCDescriptor.h:45
static constexpr uint32_t kClockFrequency
dwClockFrequency reported in the headers: 1 MHz (PTS in microseconds)
Definition UVCDescriptor.h:138
USB Video Class (UVC 1.1) device - a USB webcam.
Definition UVCDevice.h:95
uint32_t bufferSize_
Definition UVCDevice.h:519
uint8_t formatCount() const
Definition UVCDevice.h:340
bool handleControlData(const UVCRequest &r, uint16_t len)
Definition UVCDevice.h:471
uint32_t payloadSizeCfg_
Definition UVCDevice.h:513
bool isStreaming() const
true while the host is streaming (between commit and stop)
Definition UVCDevice.h:261
void finishFrame(bool complete)
Definition UVCDevice.h:685
const UVCFormatSpec * formats() const
Formats as defined with addResolution()
Definition UVCDevice.h:339
UVCPayloadEncoder encoder_
Definition UVCDevice.h:547
static UVCDevice *& instance()
The active device.
Definition UVCDevice.h:346
uint32_t payloadSize_
Definition UVCDevice.h:514
void setPayloadSize(uint32_t size)
Definition UVCDevice.h:180
bool fid_
Definition UVCDevice.h:549
void usbOpened(XferFn xfer, uint16_t maxPacketSize)
The host configured the device: the bulk endpoint is open.
Definition UVCDevice.h:384
void usbTransferComplete(bool ok)
A transfer on our endpoint completed.
Definition UVCDevice.h:494
void * streamCbRef_
Definition UVCDevice.h:532
uint8_t controlItf_
Definition UVCDevice.h:522
UVCFormatSpec formats_[2]
Definition UVCDevice.h:510
uint8_t controlInterface() const
Definition UVCDevice.h:376
uint32_t encoderGeneration_
Definition UVCDevice.h:548
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.
Definition UVCDevice.h:121
std::atomic< bool > streaming_
Definition UVCDevice.h:535
uint8_t formatCount_
Definition UVCDevice.h:511
UVCFormatSpec * findFormat(UVCFormat format)
Definition UVCDevice.h:565
volatile uint32_t dropped_
Definition UVCDevice.h:544
std::atomic< bool > inFlight_
Definition UVCDevice.h:537
const char * name() const
Definition UVCDevice.h:176
UVCDevice & operator=(const UVCDevice &)=delete
uint32_t framesDropped() const
Number of frames that were rejected or aborted.
Definition UVCDevice.h:336
void clearResolutions()
Removes all formats/resolutions (before begin())
Definition UVCDevice.h:169
bool active_
Definition UVCDevice.h:517
uint32_t framesSent() const
Number of frames that were completely sent.
Definition UVCDevice.h:334
bool isActive() const
true if begin() succeeded
Definition UVCDevice.h:258
uint8_t endpoint_
Definition UVCDevice.h:523
static uint32_t effectivePayloadSize(uint32_t requested, uint16_t mps)
Definition UVCDevice.h:558
UVCStreamSettings settings() const
Format, resolution and frame rate the host selected.
Definition UVCDevice.h:272
uint8_t endpoint() const
Definition UVCDevice.h:378
void startStream(const UVCProbeCommit &pc)
Definition UVCDevice.h:657
uint16_t maxPacketSize_
Definition UVCDevice.h:515
uint32_t payloadSize() const
Definition UVCDevice.h:181
UVCDevice(const UVCDevice &)=delete
bool addResolution(UVCFormat format, uint16_t width, uint16_t height, uint8_t fps, uint32_t maxFrameSize=0)
Definition UVCDevice.h:107
volatile uint32_t framePts_
Definition UVCDevice.h:542
UVCControlResult handleControlSetup(const UVCRequest &r, uint8_t *&data, uint16_t &len)
Handles the setup stage of a request to our interfaces/endpoint.
Definition UVCDevice.h:404
void(* streamCb_)(bool, const UVCStreamSettings &, void *)
Definition UVCDevice.h:531
UVCProbeCommit probe_
Definition UVCDevice.h:526
volatile uint32_t sent_
Definition UVCDevice.h:543
XferFn xfer_
Definition UVCDevice.h:520
~UVCDevice()
Definition UVCDevice.h:98
uint16_t descriptorLength() const
Length of the function descriptors.
Definition UVCDevice.h:352
bool startTransfer()
Definition UVCDevice.h:695
UVCProbeCommit limit(bool max) const
GET_MIN / GET_MAX values.
Definition UVCDevice.h:645
uint8_t * buffer_
Definition UVCDevice.h:518
UVCControlResult stall(uint8_t error)
Definition UVCDevice.h:571
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...
Definition UVCDevice.h:287
const char * name_
Definition UVCDevice.h:512
volatile size_t frameLen_
Definition UVCDevice.h:540
void usbReset()
Bus reset / unconfigured.
Definition UVCDevice.h:395
uint8_t errorCode_
Definition UVCDevice.h:524
void stopStream()
Definition UVCDevice.h:677
UVCControlResult replyProbe(const UVCProbeCommit &pc, const UVCRequest &r, uint8_t *&data, uint16_t &len)
Definition UVCDevice.h:586
bool waitReady(uint32_t timeoutMs)
Waits until isReady() (true) or the timeout or end of streaming (false)
Definition UVCDevice.h:323
bool withPts_
Definition UVCDevice.h:516
UVCProbeCommit negotiate(const UVCProbeCommit &in) const
Validates/completes a probe or commit structure from the host.
Definition UVCDevice.h:621
uint16_t buildDescriptor(uint8_t *buf, uint16_t bufLen, uint8_t controlItf, uint8_t endpointIn, uint16_t maxPacketSize, uint8_t stringIndex)
Definition UVCDevice.h:361
void setTimestamps(bool enabled)
Adds a presentation time stamp (PTS, microseconds) to every payload.
Definition UVCDevice.h:184
std::atomic< bool > framePending_
Definition UVCDevice.h:536
bool zlpPending_
Definition UVCDevice.h:550
UVCStreamSettings settings_
Definition UVCDevice.h:528
void kick()
Starts a transfer if none is in flight (callable from any context)
Definition UVCDevice.h:738
bool isSending() const
Definition UVCDevice.h:269
bool begin()
Definition UVCDevice.h:198
uint8_t streamingInterface() const
Definition UVCDevice.h:377
uint8_t ctrlBuf_[48]
Definition UVCDevice.h:525
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.
Definition UVCDevice.h:113
void setStreamCallback(void(*cb)(bool streaming, const UVCStreamSettings &settings, void *ref), void *ref=nullptr)
Definition UVCDevice.h:188
bool(*)(uint8_t ep, uint8_t *buf, uint16_t len) XferFn
Transfer function: starts an IN transfer, returns false on error.
Definition UVCDevice.h:381
const uint8_t *volatile frameData_
Definition UVCDevice.h:539
UVCControlResult handleStandard(const UVCRequest &r, uint8_t *&data, uint16_t &len)
Definition UVCDevice.h:591
void setName(const char *name)
Definition UVCDevice.h:175
void end()
Stops the camera function.
Definition UVCDevice.h:240
std::atomic< uint32_t > generation_
Definition UVCDevice.h:538
bool isReady() const
Definition UVCDevice.h:265
volatile PixelFormat frameFmt_
Definition UVCDevice.h:541
UVCControlResult reply(const void *src, uint16_t n, const UVCRequest &r, uint8_t *&data, uint16_t &len)
Definition UVCDevice.h:576
uint32_t streamGeneration() const
Incremented whenever the host starts or stops a stream.
Definition UVCDevice.h:275
uint32_t maxFrameSize_
Definition UVCDevice.h:529
UVCProbeCommit commit_
Definition UVCDevice.h:527
Produces the UVC payloads of one frame. Each payload starts with a 2 byte header (6 bytes with a pres...
Definition UVCPayload.h:21
bool active() const
true while the frame still has data to send
Definition UVCPayload.h:76
void abort()
Aborts the current frame.
Definition UVCPayload.h:78
size_t fill(uint8_t *buf, size_t maxLen)
Definition UVCPayload.h:49
void begin(const uint8_t *data, size_t len, PixelFormat fmt, bool fid, bool withPts=false, uint32_t pts=0)
Definition UVCPayload.h:26
void put16(uint8_t *p, uint16_t v)
Definition UVCDefs.h:225
constexpr uint8_t GET_CUR
Definition UVCDefs.h:43
constexpr uint8_t GET_DEF
Definition UVCDefs.h:49
constexpr uint8_t VC_REQUEST_ERROR_CODE_CONTROL
Definition UVCDefs.h:51
constexpr uint8_t VS_COMMIT_CONTROL
Definition UVCDefs.h:53
constexpr uint8_t GET_MIN
Definition UVCDefs.h:44
constexpr uint8_t ERR_INVALID_REQUEST
Definition UVCDefs.h:59
constexpr uint8_t ERR_NONE
Definition UVCDefs.h:55
constexpr uint8_t GET_LEN
Definition UVCDefs.h:47
constexpr uint8_t GET_MAX
Definition UVCDefs.h:45
constexpr uint16_t PROBE_COMMIT_SIZE
Size of the probe/commit control structure for UVC 1.1.
Definition UVCDefs.h:72
constexpr uint8_t ERR_INVALID_UNIT
Definition UVCDefs.h:57
constexpr uint8_t ERR_INVALID_CONTROL
Definition UVCDefs.h:58
constexpr uint8_t VS_PROBE_CONTROL
Definition UVCDefs.h:52
constexpr uint8_t GET_RES
Definition UVCDefs.h:46
constexpr uint8_t ID_OUTPUT_TERMINAL
Definition UVCDefs.h:70
constexpr uint8_t SET_CUR
Definition UVCDefs.h:42
constexpr uint8_t GET_INFO
Definition UVCDefs.h:48
TinyUVC: USB Video Class (UVC) device for Arduino and the desktop.
const char * toString(PixelFormat fmt)
Name of a pixel format, for logging.
Definition UVCDefs.h:112
constexpr uint8_t bytesPerPixel(PixelFormat fmt)
Bytes per pixel of a raw pixel format (0 for JPEG)
Definition UVCDefs.h:103
UVCFormat
Video format offered to the USB host.
Definition UVCDefs.h:78
@ MJPEG
Motion JPEG: every frame is a complete JPEG image.
@ YUY2
Uncompressed YUV 4:2:2 (YUYV), 16 bits per pixel.
PixelFormat
Pixel format of the frame data handed to UVCDevice::writeFrame(). MJPEG (a JPEG image) is sent as is....
Definition UVCDefs.h:92
@ MJPEG
one complete JPEG image (for UVCFormat::MJPEG)
constexpr uint32_t fpsToInterval(uint32_t fps)
Frame interval in 100ns units for a frame rate.
Definition UVCDefs.h:138
UVCControlResult
What the USB glue has to do with a setup request.
Definition UVCDevice.h:64
@ DataIn
send data/len to the host
@ DataOut
receive wLength bytes into data, then call handleControlData()
@ Handled
nothing to do (the USB stack acknowledges itself)
@ Status
no data stage: just acknowledge
Input of UVCDescriptorBuilder::build()
Definition UVCDescriptor.h:14
uint8_t formatCount
Definition UVCDescriptor.h:26
uint8_t stringIndex
String index for the function and interface names (0 = none)
Definition UVCDescriptor.h:23
uint8_t controlInterface
Definition UVCDescriptor.h:17
const UVCFormatSpec * formats
The formats to offer.
Definition UVCDescriptor.h:25
uint16_t maxPacketSize
Max packet size of the bulk endpoint (64 full speed, 512 high speed)
Definition UVCDescriptor.h:21
uint8_t endpointIn
Address of the bulk IN endpoint (with bit 7 set)
Definition UVCDescriptor.h:19
One resolution (UVC frame descriptor) with its supported frame rates.
Definition UVCDefs.h:148
uint32_t closestInterval(uint32_t interval) const
Supported interval that is closest to the requested one.
Definition UVCDefs.h:165
uint32_t intervals[4]
Supported frame intervals in 100ns units; intervals[0] is the default.
Definition UVCDefs.h:156
uint16_t width
Definition UVCDefs.h:149
uint32_t minInterval() const
Definition UVCDefs.h:179
uint16_t height
Definition UVCDefs.h:150
uint32_t maxFrameSize
Max size of one frame in bytes, reported to the host for buffer sizing.
Definition UVCDefs.h:152
uint32_t maxInterval() const
Definition UVCDefs.h:185
uint8_t intervalCount
Number of valid entries in intervals[].
Definition UVCDefs.h:154
Video probe and commit control (UVC 1.1, 34 bytes). Kept as a byte array in USB (little endian) layou...
Definition UVCDefs.h:247
uint32_t frameInterval() const
Definition UVCDefs.h:253
void setVersions(uint8_t preferred, uint8_t min, uint8_t max)
Definition UVCDefs.h:272
void setFramingInfo(uint8_t v)
Definition UVCDefs.h:271
void setFrameInterval(uint32_t v)
Definition UVCDefs.h:262
uint8_t raw[uvc_spec::PROBE_COMMIT_SIZE]
Definition UVCDefs.h:248
void setFormatIndex(uint8_t v)
Definition UVCDefs.h:260
void setFrameIndex(uint8_t v)
Definition UVCDefs.h:261
uint8_t frameIndex() const
Definition UVCDefs.h:252
void setMaxPayloadTransferSize(uint32_t v)
Definition UVCDefs.h:269
void setMaxVideoFrameSize(uint32_t v)
Definition UVCDefs.h:268
void setHint(uint16_t v)
Definition UVCDefs.h:259
uint16_t bmHint() const
Definition UVCDefs.h:250
void setClockFrequency(uint32_t v)
Definition UVCDefs.h:270
uint8_t formatIndex() const
Definition UVCDefs.h:251
Setup request as seen by UVCDevice (independent of the USB stack)
Definition UVCDevice.h:49
uint8_t bRequest
Definition UVCDevice.h:51
bool isIn() const
Definition UVCDevice.h:60
Recipient
Definition UVCDevice.h:57
@ Endpoint
Definition UVCDevice.h:57
@ Interface
Definition UVCDevice.h:57
@ Device
Definition UVCDevice.h:57
Type
Definition UVCDevice.h:56
@ Standard
Definition UVCDevice.h:56
@ Class
Definition UVCDevice.h:56
@ Vendor
Definition UVCDevice.h:56
uint16_t wLength
Definition UVCDevice.h:54
uint16_t wValue
Definition UVCDevice.h:52
uint16_t wIndex
Definition UVCDevice.h:53
uint8_t type() const
Definition UVCDevice.h:58
uint8_t bmRequestType
Definition UVCDevice.h:50
uint8_t recipient() const
Definition UVCDevice.h:59
The format, resolution and frame rate the host selected.
Definition UVCDefs.h:201
uint8_t frameIndex
Definition UVCDefs.h:209
uint16_t width
Definition UVCDefs.h:203
uint8_t formatIndex
1-based UVC indexes as used in the probe/commit control
Definition UVCDefs.h:208
uint32_t fps() const
Frame rate (frames per second, rounded)
Definition UVCDefs.h:211
uint16_t height
Definition UVCDefs.h:204
UVCFormat format
Definition UVCDefs.h:202
uint32_t interval
Frame interval in 100ns units.
Definition UVCDefs.h:206