TinyUVC 0.1.0
Header-only USB Video Class (UVC) device: turn an MCU into a USB webcam
Loading...
Searching...
No Matches
UVCHostSimulator.h
Go to the documentation of this file.
1#pragma once
15#include <functional>
16#include <string>
17#include <vector>
18
19#include "../UVCDevice.h"
20#include "TinyUSBSim.h"
21
22namespace tinyuvc {
23
27 public:
29 struct Frame {
30 const uint8_t *data = nullptr;
31 size_t size = 0;
34 bool error = false;
35 bool hasPts = false;
36 uint32_t pts = 0;
38 uint32_t index = 0;
39 };
40
42 struct HostFrame {
43 uint8_t index = 0;
44 uint16_t width = 0, height = 0;
45 uint32_t maxFrameSize = 0;
46 uint32_t defaultInterval = 0;
47 std::vector<uint32_t> intervals;
48 };
49
51 struct HostFormat {
52 uint8_t index = 0;
54 std::vector<HostFrame> frames;
55 };
56
59 explicit UVCHostSimulator(bool withCdc = true) : withCdc_(withCdc) {}
61
64 bool connect(UVCDevice &dev) {
65 error_.clear();
66 dev_ = &dev;
67 if (!dev.isActive()) return fail("device not started: call begin() first");
68 uint8_t count = 0;
70 if (driver_ == nullptr || count < 1) return fail("no application driver");
71 driver_->init();
73 if (!buildConfiguration(dev)) return false;
74 if (!parseConfiguration()) return false;
75 if (!openDrivers()) return false;
76 simBus().pump = [](void *ref) { ((UVCHostSimulator *)ref)->poll(); };
77 simBus().pumpRef = this;
78 connected_ = true;
79 return true;
80 }
81
83 void disconnect() {
84 if (!connected_) return;
85 if (driver_ && driver_->reset) driver_->reset(0);
87 simBus().pump = nullptr;
88 connected_ = false;
89 streaming_ = false;
90 }
91
92 bool isConnected() const { return connected_; }
93
95 const std::vector<HostFormat> &formats() const { return formats_; }
96
98 const std::vector<uint8_t> &configDescriptor() const { return config_; }
99
101 const std::string &error() const { return error_; }
102
104 bool startStream(UVCFormat fmt, uint16_t width, uint16_t height,
105 uint32_t fps = 0) {
106 for (auto &f : formats_) {
107 if (f.format != fmt) continue;
108 for (auto &fr : f.frames)
109 if (fr.width == width && fr.height == height)
110 return startStream(f.index, fr.index, fps ? fpsToInterval(fps) : 0);
111 }
112 return fail("format/resolution not offered by the device");
113 }
114
116 bool startStream(uint8_t formatIndex, uint8_t frameIndex, uint32_t interval) {
117 if (!connected_) return fail("not connected");
119 if (!getProbe(uvc_spec::GET_DEF, false, pc)) return fail("GET_DEF probe failed");
120 pc.setHint(1);
121 pc.setFormatIndex(formatIndex);
122 pc.setFrameIndex(frameIndex);
123 pc.setFrameInterval(interval);
124 if (!setProbe(false, pc)) return fail("SET_CUR probe failed");
125 UVCProbeCommit cur;
126 if (!getProbe(uvc_spec::GET_CUR, false, cur)) return fail("GET_CUR probe failed");
127 if (cur.formatIndex() != formatIndex || cur.frameIndex() != frameIndex)
128 return fail("device changed the format/frame in the probe");
129 if (cur.maxPayloadTransferSize() == 0) return fail("dwMaxPayloadTransferSize is 0");
130 if (!setProbe(true, cur)) return fail("SET_CUR commit failed");
131 const HostFormat *f = findFormat(formatIndex);
132 const HostFrame *fr = f ? findFrame(*f, frameIndex) : nullptr;
133 if (fr == nullptr) return fail("invalid format/frame index");
135 settings_.width = fr->width;
136 settings_.height = fr->height;
138 settings_.formatIndex = formatIndex;
139 settings_.frameIndex = frameIndex;
142 uint32_t packets = (maxPayload_ + mps_ - 1) / mps_;
143 if (packets > 32) packets = 32; // UVC_MAX_PACKETS of uvcvideo
144 urbSize_ = packets * mps_;
145 urb_.clear();
146 resetPayload();
147 frameActive_ = false;
148 lastFid_ = -1;
149 streaming_ = true;
150 return true;
151 }
152
155 bool stopStream(bool useSetInterface = false) {
156 if (!connected_) return false;
157 streaming_ = false;
158 UVCRequest r;
159 if (useSetInterface) {
160 r.bmRequestType = 0x01; // standard, interface
161 r.bRequest = 0x0B; // SET_INTERFACE
163 } else {
164 r.bmRequestType = 0x02; // standard, endpoint
165 r.bRequest = 0x01; // CLEAR_FEATURE
166 r.wValue = 0; // ENDPOINT_HALT
167 r.wIndex = ep_;
168 // the host side aborts its URBs: a pending transfer stays queued on
169 // the device until the next read
170 }
171 return control(r, nullptr, 0, nullptr);
172 }
173
176 bool poll() {
177 UVCSimBus &b = simBus();
178 if (!connected_ || !b.busy || !streaming_) return false;
179 uint8_t *buf = b.xferBuf;
180 uint16_t len = b.xferLen;
181 transfers_++;
182 if (len == 0) {
183 zlps_++;
184 packet(nullptr, 0);
185 } else {
186 for (uint16_t off = 0; off < len; off += mps_) {
187 uint16_t n = (uint16_t)(len - off < mps_ ? len - off : mps_);
188 packet(buf + off, n);
189 }
190 }
191 b.busy = false;
192 b.claimed = false;
193 b.xferBuf = nullptr;
195 return true;
196 }
197
199 size_t pollAll(size_t maxTransfers = 1000000) {
200 size_t n = 0;
201 while (n < maxTransfers && poll()) n++;
202 return n;
203 }
204
206 void onFrame(std::function<void(const Frame &)> cb) { onFrame_ = cb; }
207
208 uint32_t framesReceived() const { return frames_; }
209 uint32_t framesWithError() const { return errorFrames_; }
210 uint32_t payloads() const { return payloads_; }
211 uint32_t droppedPayloads() const { return droppedPayloads_; }
212 uint32_t transfers() const { return transfers_; }
213 uint32_t zeroLengthPackets() const { return zlps_; }
214 uint16_t maxPacketSize() const { return mps_; }
215 uint32_t urbSize() const { return urbSize_; }
216 bool isStreaming() const { return streaming_; }
217
218 // ------------------------------------------------- control transfers
219
222 bool control(const UVCRequest &r, const uint8_t *out, uint16_t outLen,
223 std::vector<uint8_t> *in) {
224 UVCSimBus &b = simBus();
225 b.resetControl();
227 req.bmRequestType = r.bmRequestType;
228 req.bRequest = r.bRequest;
229 req.wValue = r.wValue;
230 req.wIndex = r.wIndex;
231 req.wLength = r.isIn() ? r.wLength : outLen;
232 bool ok = driver_->control_xfer_cb(0, CONTROL_STAGE_SETUP, &req);
233 if (!ok) {
234 // the TinyUSB core answers GET/SET_INTERFACE itself if the driver
235 // does not
236 return r.type() == UVCRequest::Standard &&
238 (r.bRequest == 0x0A || r.bRequest == 0x0B);
239 }
240 if (b.ctrlXfer) {
241 if (r.isIn()) {
242 uint16_t n = b.ctrlLen < r.wLength ? b.ctrlLen : r.wLength;
243 if (in) in->assign(b.ctrlBuf, b.ctrlBuf + n);
244 } else {
245 if (b.ctrlLen < outLen) return false;
246 memcpy(b.ctrlBuf, out, outLen);
247 }
248 if (req.wLength > 0 &&
250 return false;
251 }
253 return true;
254 }
255
257 bool getProbe(uint8_t request, bool commit, UVCProbeCommit &pc) {
258 std::vector<uint8_t> data;
259 UVCRequest r;
260 r.bmRequestType = 0xA1; // in, class, interface
261 r.bRequest = request;
262 r.wValue = (uint16_t)((commit ? uvc_spec::VS_COMMIT_CONTROL : uvc_spec::VS_PROBE_CONTROL) << 8);
265 if (!control(r, nullptr, 0, &data)) return false;
266 if (data.size() != uvc_spec::PROBE_COMMIT_SIZE) return fail("short probe reply");
267 memcpy(pc.raw, data.data(), data.size());
268 return true;
269 }
270
272 bool setProbe(bool commit, const UVCProbeCommit &pc) {
273 UVCRequest r;
274 r.bmRequestType = 0x21; // out, class, interface
276 r.wValue = (uint16_t)((commit ? uvc_spec::VS_COMMIT_CONTROL : uvc_spec::VS_PROBE_CONTROL) << 8);
278 return control(r, pc.raw, uvc_spec::PROBE_COMMIT_SIZE, nullptr);
279 }
280
283 std::vector<uint8_t> data;
284 UVCRequest r;
285 r.bmRequestType = 0xA1;
289 r.wLength = 1;
290 if (!control(r, nullptr, 0, &data) || data.size() != 1) return 0xFF;
291 return data[0];
292 }
293
294 protected:
296 UVCDevice *dev_ = nullptr;
297 usbd_class_driver_t const *driver_ = nullptr;
298 bool connected_ = false;
299 bool streaming_ = false;
300 std::string error_;
301 std::vector<uint8_t> config_;
302 std::vector<HostFormat> formats_;
303 uint8_t ep_ = 0;
304 uint16_t mps_ = 64;
305 uint8_t vcItf_ = 0;
306
308 uint32_t maxVideoFrameSize_ = 0;
309 uint32_t maxPayload_ = 0;
310 uint32_t urbSize_ = 0;
311 std::vector<uint8_t> urb_;
312 // payload state (uvcvideo's stream->bulk)
313 uint32_t payloadSize_ = 0;
314 uint8_t headerSize_ = 0;
315 uint8_t header_[256] = {};
316 bool skipPayload_ = false;
317 // frame state
318 int lastFid_ = -1;
319 bool frameActive_ = false;
320 bool frameError_ = false;
321 std::vector<uint8_t> frame_;
322 bool framePts_ = false;
323 uint32_t framePtsValue_ = 0;
324
325 std::function<void(const Frame &)> onFrame_;
326 uint32_t frames_ = 0, errorFrames_ = 0, payloads_ = 0, droppedPayloads_ = 0;
327 uint32_t transfers_ = 0, zlps_ = 0;
328
329 bool fail(const std::string &msg) {
330 error_ = msg;
331 return false;
332 }
333
334 const HostFormat *findFormat(uint8_t index) const {
335 for (auto &f : formats_)
336 if (f.index == index) return &f;
337 return nullptr;
338 }
339 static const HostFrame *findFrame(const HostFormat &f, uint8_t index) {
340 for (auto &fr : f.frames)
341 if (fr.index == index) return &fr;
342 return nullptr;
343 }
344
345 // ------------------------------------------------ enumeration
346
348 // what a board with USB serial + camera reports
349 static const uint8_t cdc[] = {
350 8, 0x0B, 0, 2, 0x02, 0x02, 0x00, 0, // IAD
351 9, 0x04, 0, 0, 1, 0x02, 0x02, 0x00, 0, // CDC control interface
352 5, 0x24, 0x00, 0x20, 0x01, // header
353 5, 0x24, 0x01, 0x00, 1, // call management
354 4, 0x24, 0x02, 0x02, // ACM
355 5, 0x24, 0x06, 0, 1, // union
356 7, 0x05, 0x81, 0x03, 8, 0, 16, // notification endpoint
357 9, 0x04, 1, 0, 2, 0x0A, 0x00, 0x00, 0, // CDC data interface
358 7, 0x05, 0x02, 0x02, 64, 0, 0, // bulk out
359 7, 0x05, 0x82, 0x02, 64, 0, 0, // bulk in
360 };
361 const uint8_t firstItf = withCdc_ ? 2 : 0;
362 const uint8_t ep = withCdc_ ? 0x83 : 0x81;
364 uint16_t uvcLen = dev.descriptorLength();
365 config_.assign(9, 0);
366 if (withCdc_) config_.insert(config_.end(), cdc, cdc + sizeof(cdc));
367 size_t off = config_.size();
368 config_.resize(off + uvcLen);
369 uint16_t len = dev.buildDescriptor(config_.data() + off, uvcLen, firstItf,
370 ep, mps_, 4);
371 if (len != uvcLen) return fail("descriptor length mismatch");
372 config_[0] = 9;
373 config_[1] = 0x02;
374 le::put16(&config_[2], (uint16_t)config_.size());
375 config_[4] = (uint8_t)(firstItf + 2); // bNumInterfaces
376 config_[5] = 1; // bConfigurationValue
377 config_[6] = 0;
378 config_[7] = 0x80;
379 config_[8] = 250;
380 return true;
381 }
382
385 formats_.clear();
386 const uint8_t *d = config_.data();
387 const size_t n = config_.size();
388 if (le::get16(d + 2) != n) return fail("config wTotalLength wrong");
389 // check that the descriptors exactly tile the configuration
390 size_t pos = 0;
391 int interfaces = 0;
392 while (pos < n) {
393 if (d[pos] < 2 || pos + d[pos] > n) return fail("bad bLength in configuration");
394 if (d[pos + 1] == uvc_spec::DESC_INTERFACE && d[pos + 3] == 0) interfaces++;
395 pos += d[pos];
396 }
397 if (interfaces != d[4]) return fail("bNumInterfaces wrong");
398
399 // find our IAD
400 pos = 9;
401 size_t iad = 0;
402 while (pos < n) {
403 if (d[pos + 1] == uvc_spec::DESC_IAD && d[pos + 4] == uvc_spec::CC_VIDEO) {
404 iad = pos;
405 break;
406 }
407 pos += d[pos];
408 }
409 if (!iad) return fail("no video interface association");
410 if (d[iad + 5] != uvc_spec::SC_VIDEO_INTERFACE_COLLECTION || d[iad + 3] != 2)
411 return fail("bad video IAD");
412 vcItf_ = d[iad + 2];
413 pos = iad + d[iad];
414 // VideoControl interface
415 if (d[pos + 1] != uvc_spec::DESC_INTERFACE || d[pos + 2] != vcItf_ ||
416 d[pos + 5] != uvc_spec::CC_VIDEO || d[pos + 6] != uvc_spec::SC_VIDEOCONTROL)
417 return fail("IAD not followed by VideoControl interface");
418 pos += d[pos];
419 if (d[pos + 1] != uvc_spec::CS_INTERFACE || d[pos + 2] != uvc_spec::VC_HEADER)
420 return fail("no VC header");
421 if (le::get16(d + pos + 3) != uvc_spec::BCD_UVC) return fail("bcdUVC is not 1.10");
422 const uint16_t vcTotal = le::get16(d + pos + 5);
423 const uint8_t vsItf = d[pos + 12];
424 if (d[pos + 11] != 1 || vsItf != vcItf_ + 1) return fail("VC header baInterfaceNr wrong");
425 size_t vcEnd = pos;
426 while (vcEnd < n && d[vcEnd + 1] == uvc_spec::CS_INTERFACE) vcEnd += d[vcEnd];
427 if (vcEnd - pos != vcTotal) return fail("VC header wTotalLength wrong");
428 pos = vcEnd;
429 // VideoStreaming interface
430 if (d[pos + 1] != uvc_spec::DESC_INTERFACE || d[pos + 2] != vsItf ||
431 d[pos + 6] != uvc_spec::SC_VIDEOSTREAMING || d[pos + 4] != 1)
432 return fail("no VideoStreaming interface with one endpoint");
433 pos += d[pos];
434 if (d[pos + 1] != uvc_spec::CS_INTERFACE || d[pos + 2] != uvc_spec::VS_INPUT_HEADER)
435 return fail("no VS input header");
436 const uint8_t numFormats = d[pos + 3];
437 const uint16_t vsTotal = le::get16(d + pos + 4);
438 ep_ = d[pos + 6];
439 if (d[pos + 8] != uvc_spec::ID_OUTPUT_TERMINAL) return fail("bTerminalLink wrong");
440 if (d[pos] != 13 + numFormats * d[pos + 12]) return fail("VS header bLength wrong");
441 size_t vsStart = pos, vsEnd = pos;
442 while (vsEnd < n && d[vsEnd + 1] == uvc_spec::CS_INTERFACE) vsEnd += d[vsEnd];
443 if (vsEnd - vsStart != vsTotal) return fail("VS header wTotalLength wrong");
444 // formats and frames
445 pos += d[pos];
446 HostFormat *cur = nullptr;
447 int framesLeft = 0;
448 while (pos < vsEnd) {
449 const uint8_t sub = d[pos + 2];
451 if (framesLeft != 0) return fail("bNumFrameDescriptors wrong");
452 HostFormat f;
453 f.index = d[pos + 3];
455 if (f.index != formats_.size() + 1) return fail("bFormatIndex not sequential");
457 (d[pos] != 27 || memcmp(d + pos + 5, "YUY2", 4) != 0 || d[pos + 21] != 16))
458 return fail("bad YUY2 format descriptor");
459 if (sub == uvc_spec::VS_FORMAT_MJPEG && d[pos] != 11) return fail("bad MJPEG format descriptor");
460 framesLeft = d[pos + 4];
461 formats_.push_back(f);
462 cur = &formats_.back();
463 } else if (sub == uvc_spec::VS_FRAME_MJPEG || sub == uvc_spec::VS_FRAME_UNCOMPRESSED) {
464 if (cur == nullptr || framesLeft <= 0) return fail("unexpected frame descriptor");
465 if ((sub == uvc_spec::VS_FRAME_MJPEG) != (cur->format == UVCFormat::MJPEG))
466 return fail("frame descriptor type does not match format");
467 HostFrame fr;
468 fr.index = d[pos + 3];
469 fr.width = le::get16(d + pos + 5);
470 fr.height = le::get16(d + pos + 7);
471 fr.maxFrameSize = le::get32(d + pos + 17);
472 fr.defaultInterval = le::get32(d + pos + 21);
473 const uint8_t nInt = d[pos + 25];
474 if (nInt == 0 || d[pos] != 26 + 4 * nInt) return fail("bad frame descriptor length");
475 for (int i = 0; i < nInt; i++) fr.intervals.push_back(le::get32(d + pos + 26 + 4 * i));
476 bool hasDefault = false;
477 for (auto iv : fr.intervals) hasDefault |= iv == fr.defaultInterval;
478 if (!hasDefault) return fail("default interval not in the list");
479 if (fr.index != cur->frames.size() + 1) return fail("bFrameIndex not sequential");
480 if (cur->format == UVCFormat::YUY2 &&
481 fr.maxFrameSize != (uint32_t)fr.width * fr.height * 2)
482 return fail("YUY2 dwMaxVideoFrameBufferSize wrong");
483 cur->frames.push_back(fr);
484 framesLeft--;
485 } else if (sub != uvc_spec::VS_COLORFORMAT) {
486 return fail("unknown VS descriptor");
487 }
488 pos += d[pos];
489 }
490 if (framesLeft != 0) return fail("bNumFrameDescriptors wrong");
491 if (formats_.size() != numFormats) return fail("bNumFormats wrong");
492 // bulk endpoint
493 if (pos >= n || d[pos + 1] != uvc_spec::DESC_ENDPOINT || d[pos + 2] != ep_ ||
494 (d[pos + 3] & 3) != 2)
495 return fail("no bulk endpoint after the VS descriptors");
496 if (le::get16(d + pos + 4) != mps_) return fail("wMaxPacketSize wrong");
497 return true;
498 }
499
502 bool openDrivers() {
503 const uint8_t *d = config_.data();
504 const size_t n = config_.size();
505 size_t pos = 9;
506 bool opened = false;
507 while (pos < n) {
508 if (d[pos + 1] == uvc_spec::DESC_INTERFACE) {
509 uint16_t len = driver_->open(0, (const tusb_desc_interface_t *)(d + pos),
510 (uint16_t)(n - pos));
511 if (len > 0) {
512 if (d[pos + 2] != vcItf_) return fail("driver claimed a foreign interface");
513 opened = true;
514 pos += len;
515 continue;
516 }
517 }
518 pos += d[pos];
519 }
520 if (!opened) return fail("the class driver did not open the video function");
521 if (simBus().openEndpoint != ep_) return fail("bulk endpoint not opened");
522 return true;
523 }
524
525 // ------------------------------------------------ streaming
526
528 payloadSize_ = 0;
529 headerSize_ = 0;
530 skipPayload_ = false;
531 }
532
534 void packet(const uint8_t *data, uint16_t len) {
535 if (len) urb_.insert(urb_.end(), data, data + len);
536 if (len < mps_ || urb_.size() >= urbSize_) {
537 urbComplete(urb_.data(), urb_.size(), urb_.size() < urbSize_);
538 urb_.clear();
539 }
540 }
541
543 void urbComplete(const uint8_t *mem, size_t len, bool shortUrb) {
544 if (len == 0 && headerSize_ == 0) return; // ZLP between payloads
545 payloadSize_ += (uint32_t)len;
546 if (headerSize_ == 0 && !skipPayload_) {
547 int ret = decodeStart(mem, len);
548 if (ret < 0) {
549 skipPayload_ = true;
550 } else {
551 headerSize_ = (uint8_t)ret;
552 memcpy(header_, mem, ret);
553 mem += ret;
554 len -= ret;
555 }
556 }
557 if (!skipPayload_ && frameActive_) decodeData(mem, len);
558 if (shortUrb || payloadSize_ >= maxPayload_) {
560 resetPayload();
561 }
562 }
563
565 int decodeStart(const uint8_t *data, size_t len) {
566 if (len < 2 || data[0] < 2 || data[0] > len) {
568 return -1;
569 }
570 payloads_++;
571 const uint8_t flags = data[1];
572 const int fid = flags & uvc_spec::HDR_FID;
573 if (flags & uvc_spec::HDR_ERR) frameError_ = true;
574 // FID toggled while a frame was active: the previous one is complete
575 if (frameActive_ && fid != lastFid_ && !frame_.empty()) completeFrame();
576 if (!frameActive_) {
577 if (fid == lastFid_) {
578 droppedPayloads_++; // out of sync, wait for the next frame
579 return -1;
580 }
581 frameActive_ = true;
582 frameError_ = (flags & uvc_spec::HDR_ERR) != 0;
583 frame_.clear();
584 framePts_ = (flags & uvc_spec::HDR_PTS) && data[0] >= 6;
585 framePtsValue_ = framePts_ ? le::get32(data + 2) : 0;
586 }
587 lastFid_ = fid;
588 return data[0];
589 }
590
592 void decodeData(const uint8_t *mem, size_t len) {
593 if (len == 0) return;
594 if (frame_.size() + len > maxVideoFrameSize_) {
595 frameError_ = true; // overflow: data is dropped
596 len = maxVideoFrameSize_ - frame_.size();
597 }
598 frame_.insert(frame_.end(), mem, mem + len);
599 }
600
602 void decodeEnd(const uint8_t *header) {
603 if ((header[1] & uvc_spec::HDR_EOF) && frameActive_ && !frame_.empty()) completeFrame();
604 }
605
607 // uvc_video_validate_buffer(): uncompressed frames must be complete
609 frameError_ = true;
610 Frame f;
611 f.data = frame_.data();
612 f.size = frame_.size();
614 f.error = frameError_;
615 f.hasPts = framePts_;
617 f.index = frames_;
618 frames_++;
620 if (onFrame_) onFrame_(f);
621 frameActive_ = false;
622 frameError_ = false;
623 frame_.clear();
624 }
625};
626
627} // namespace tinyuvc
Desktop stand-in for the parts of the TinyUSB device API that UVCTinyUSB.h uses. The calls are record...
tusb_control_request_t
Definition TinyUSBSim.h:65
usbd_class_driver_t const * usbd_app_driver_get_cb(uint8_t *driver_count)
TinyUSB asks for application class drivers once, in tud_init()
Definition UVCTinyUSB.h:197
@ CONTROL_STAGE_ACK
Definition TinyUSBSim.h:79
@ CONTROL_STAGE_DATA
Definition TinyUSBSim.h:78
@ CONTROL_STAGE_SETUP
Definition TinyUSBSim.h:77
tusb_desc_interface_t
Definition TinyUSBSim.h:48
@ XFER_RESULT_SUCCESS
Definition TinyUSBSim.h:68
USB Video Class (UVC 1.1) device - a USB webcam.
Definition UVCDevice.h:95
uint8_t controlInterface() const
Definition UVCDevice.h:376
bool isActive() const
true if begin() succeeded
Definition UVCDevice.h:258
uint16_t descriptorLength() const
Length of the function descriptors.
Definition UVCDevice.h:352
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
uint8_t streamingInterface() const
Definition UVCDevice.h:377
Desktop only: a simulated USB host (modeled on Linux' uvcvideo) that enumerates, negotiates and strea...
Definition UVCHostSimulator.h:26
std::string error_
Definition UVCHostSimulator.h:300
bool startStream(uint8_t formatIndex, uint8_t frameIndex, uint32_t interval)
Negotiates like Linux: GET_DEF, SET_CUR/GET_CUR probe, SET_CUR commit.
Definition UVCHostSimulator.h:116
void disconnect()
Simulates unplugging (bus reset)
Definition UVCHostSimulator.h:83
bool frameError_
Definition UVCHostSimulator.h:320
bool control(const UVCRequest &r, const uint8_t *out, uint16_t outLen, std::vector< uint8_t > *in)
Definition UVCHostSimulator.h:222
bool connected_
Definition UVCHostSimulator.h:298
uint8_t requestErrorCode()
VC_REQUEST_ERROR_CODE_CONTROL (0xFF if the request failed)
Definition UVCHostSimulator.h:282
void decodeEnd(const uint8_t *header)
uvc_video_decode_end()
Definition UVCHostSimulator.h:602
uint8_t headerSize_
Definition UVCHostSimulator.h:314
UVCHostSimulator(bool withCdc=true)
Definition UVCHostSimulator.h:59
uint32_t maxVideoFrameSize_
Definition UVCHostSimulator.h:308
bool connect(UVCDevice &dev)
Definition UVCHostSimulator.h:64
bool isConnected() const
Definition UVCHostSimulator.h:92
uint32_t errorFrames_
Definition UVCHostSimulator.h:326
const HostFormat * findFormat(uint8_t index) const
Definition UVCHostSimulator.h:334
bool openDrivers()
Definition UVCHostSimulator.h:502
uint32_t transfers() const
Definition UVCHostSimulator.h:212
int decodeStart(const uint8_t *data, size_t len)
uvc_video_decode_start(): returns the header length or -1 (skip)
Definition UVCHostSimulator.h:565
uint32_t framesReceived() const
Definition UVCHostSimulator.h:208
UVCStreamSettings settings_
Definition UVCHostSimulator.h:307
uint32_t frames_
Definition UVCHostSimulator.h:326
uint32_t zlps_
Definition UVCHostSimulator.h:327
~UVCHostSimulator()
Definition UVCHostSimulator.h:60
UVCDevice * dev_
Definition UVCHostSimulator.h:296
bool buildConfiguration(UVCDevice &dev)
Definition UVCHostSimulator.h:347
const std::vector< uint8_t > & configDescriptor() const
The complete configuration descriptor.
Definition UVCHostSimulator.h:98
bool framePts_
Definition UVCHostSimulator.h:322
bool startStream(UVCFormat fmt, uint16_t width, uint16_t height, uint32_t fps=0)
Streams format/resolution/frame rate (fps 0 = default rate)
Definition UVCHostSimulator.h:104
void resetPayload()
Definition UVCHostSimulator.h:527
uint8_t ep_
Definition UVCHostSimulator.h:303
uint32_t urbSize_
Definition UVCHostSimulator.h:310
uint32_t droppedPayloads() const
Definition UVCHostSimulator.h:211
uint32_t payloads_
Definition UVCHostSimulator.h:326
bool skipPayload_
Definition UVCHostSimulator.h:316
std::vector< uint8_t > urb_
Definition UVCHostSimulator.h:311
uint32_t maxPayload_
Definition UVCHostSimulator.h:309
std::vector< uint8_t > config_
Definition UVCHostSimulator.h:301
uint8_t vcItf_
Definition UVCHostSimulator.h:305
void completeFrame()
Definition UVCHostSimulator.h:606
void onFrame(std::function< void(const Frame &)> cb)
Callback for every received frame.
Definition UVCHostSimulator.h:206
void decodeData(const uint8_t *mem, size_t len)
uvc_video_decode_data()
Definition UVCHostSimulator.h:592
uint8_t header_[256]
Definition UVCHostSimulator.h:315
bool parseConfiguration()
Parses and validates the configuration like a UVC host.
Definition UVCHostSimulator.h:384
uint32_t urbSize() const
Definition UVCHostSimulator.h:215
bool isStreaming() const
Definition UVCHostSimulator.h:216
bool stopStream(bool useSetInterface=false)
Definition UVCHostSimulator.h:155
void urbComplete(const uint8_t *mem, size_t len, bool shortUrb)
uvc_video_decode_bulk()
Definition UVCHostSimulator.h:543
uint32_t payloadSize_
Definition UVCHostSimulator.h:313
uint16_t mps_
Definition UVCHostSimulator.h:304
const std::string & error() const
Last error message.
Definition UVCHostSimulator.h:101
uint32_t framesWithError() const
Definition UVCHostSimulator.h:209
uint32_t transfers_
Definition UVCHostSimulator.h:327
usbd_class_driver_t const * driver_
Definition UVCHostSimulator.h:297
uint32_t payloads() const
Definition UVCHostSimulator.h:210
int lastFid_
Definition UVCHostSimulator.h:318
static const HostFrame * findFrame(const HostFormat &f, uint8_t index)
Definition UVCHostSimulator.h:339
bool getProbe(uint8_t request, bool commit, UVCProbeCommit &pc)
Class request GET_xxx of the probe (or commit) control.
Definition UVCHostSimulator.h:257
std::vector< uint8_t > frame_
Definition UVCHostSimulator.h:321
bool fail(const std::string &msg)
Definition UVCHostSimulator.h:329
bool poll()
Definition UVCHostSimulator.h:176
std::vector< HostFormat > formats_
Definition UVCHostSimulator.h:302
const std::vector< HostFormat > & formats() const
Formats/resolutions as parsed from the descriptors.
Definition UVCHostSimulator.h:95
uint16_t maxPacketSize() const
Definition UVCHostSimulator.h:214
void packet(const uint8_t *data, uint16_t len)
One USB packet arrives at the host controller.
Definition UVCHostSimulator.h:534
bool setProbe(bool commit, const UVCProbeCommit &pc)
Class request SET_CUR of the probe (or commit) control.
Definition UVCHostSimulator.h:272
uint32_t framePtsValue_
Definition UVCHostSimulator.h:323
uint32_t zeroLengthPackets() const
Definition UVCHostSimulator.h:213
bool frameActive_
Definition UVCHostSimulator.h:319
uint32_t droppedPayloads_
Definition UVCHostSimulator.h:326
std::function< void(const Frame &)> onFrame_
Definition UVCHostSimulator.h:325
bool withCdc_
Definition UVCHostSimulator.h:295
size_t pollAll(size_t maxTransfers=1000000)
Polls until no transfer is pending (or max transfers)
Definition UVCHostSimulator.h:199
bool streaming_
Definition UVCHostSimulator.h:299
uint16_t get16(const uint8_t *p)
Definition UVCDefs.h:235
uint32_t get32(const uint8_t *p)
Definition UVCDefs.h:236
void put16(uint8_t *p, uint16_t v)
Definition UVCDefs.h:225
uint16_t maxPacketSize()
Max packet size of the bulk endpoint (64 full speed, 512 high speed)
Definition UVCPlatformDesktop.h:31
constexpr uint8_t GET_CUR
Definition UVCDefs.h:43
constexpr uint8_t GET_DEF
Definition UVCDefs.h:49
constexpr uint8_t SC_VIDEOSTREAMING
Definition UVCDefs.h:20
constexpr uint8_t VS_FRAME_UNCOMPRESSED
Definition UVCDefs.h:34
constexpr uint8_t HDR_EOF
Definition UVCDefs.h:62
constexpr uint8_t VS_FORMAT_MJPEG
Definition UVCDefs.h:35
constexpr uint8_t VC_REQUEST_ERROR_CODE_CONTROL
Definition UVCDefs.h:51
constexpr uint8_t SC_VIDEOCONTROL
Definition UVCDefs.h:19
constexpr uint8_t VS_COMMIT_CONTROL
Definition UVCDefs.h:53
constexpr uint8_t VS_FRAME_MJPEG
Definition UVCDefs.h:36
constexpr uint8_t DESC_INTERFACE
Definition UVCDefs.h:23
constexpr uint8_t CC_VIDEO
Definition UVCDefs.h:18
constexpr uint8_t HDR_PTS
Definition UVCDefs.h:63
constexpr uint16_t BCD_UVC
bcdUVC reported in the VideoControl header
Definition UVCDefs.h:74
constexpr uint8_t VS_INPUT_HEADER
Definition UVCDefs.h:32
constexpr uint8_t HDR_ERR
Definition UVCDefs.h:66
constexpr uint8_t DESC_IAD
Definition UVCDefs.h:25
constexpr uint8_t VS_COLORFORMAT
Definition UVCDefs.h:37
constexpr uint8_t VS_FORMAT_UNCOMPRESSED
Definition UVCDefs.h:33
constexpr uint8_t SC_VIDEO_INTERFACE_COLLECTION
Definition UVCDefs.h:21
constexpr uint8_t DESC_ENDPOINT
Definition UVCDefs.h:24
constexpr uint16_t PROBE_COMMIT_SIZE
Size of the probe/commit control structure for UVC 1.1.
Definition UVCDefs.h:72
constexpr uint8_t HDR_FID
Definition UVCDefs.h:61
constexpr uint8_t VC_HEADER
Definition UVCDefs.h:28
constexpr uint8_t VS_PROBE_CONTROL
Definition UVCDefs.h:52
constexpr uint8_t CS_INTERFACE
Definition UVCDefs.h:26
constexpr uint8_t ID_OUTPUT_TERMINAL
Definition UVCDefs.h:70
constexpr uint8_t SET_CUR
Definition UVCDefs.h:42
TinyUVC: USB Video Class (UVC) device for Arduino and the desktop.
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.
constexpr uint32_t fpsToInterval(uint32_t fps)
Frame interval in 100ns units for a frame rate.
Definition UVCDefs.h:138
UVCSimBus & simBus()
Definition TinyUSBSim.h:138
A frame received from the device.
Definition UVCHostSimulator.h:29
UVCStreamSettings settings
Definition UVCHostSimulator.h:32
bool error
true if the frame is corrupt (Linux would drop it)
Definition UVCHostSimulator.h:34
const uint8_t * data
Definition UVCHostSimulator.h:30
uint32_t index
running number of received frames (starting at 0)
Definition UVCHostSimulator.h:38
bool hasPts
Definition UVCHostSimulator.h:35
size_t size
Definition UVCHostSimulator.h:31
uint32_t pts
Definition UVCHostSimulator.h:36
Format descriptor as parsed by the host.
Definition UVCHostSimulator.h:51
UVCFormat format
Definition UVCHostSimulator.h:53
std::vector< HostFrame > frames
Definition UVCHostSimulator.h:54
uint8_t index
Definition UVCHostSimulator.h:52
Frame descriptor as parsed by the host.
Definition UVCHostSimulator.h:42
uint32_t maxFrameSize
Definition UVCHostSimulator.h:45
uint16_t height
Definition UVCHostSimulator.h:44
uint32_t defaultInterval
Definition UVCHostSimulator.h:46
uint8_t index
Definition UVCHostSimulator.h:43
std::vector< uint32_t > intervals
Definition UVCHostSimulator.h:47
uint16_t width
Definition UVCHostSimulator.h:44
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 maxPayloadTransferSize() const
Definition UVCDefs.h:255
uint32_t frameInterval() const
Definition UVCDefs.h:253
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 setHint(uint16_t v)
Definition UVCDefs.h:259
uint32_t maxVideoFrameSize() const
Definition UVCDefs.h:254
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
@ Interface
Definition UVCDevice.h:57
@ Standard
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
What the device side did, as seen by the simulated USB core.
Definition TinyUSBSim.h:106
void(* pump)(void *ref)
Definition TinyUSBSim.h:122
bool busy
Definition TinyUSBSim.h:111
void resetEndpoint()
Definition TinyUSBSim.h:130
bool claimed
Definition TinyUSBSim.h:110
uint16_t xferLen
Definition TinyUSBSim.h:113
void resetControl()
Definition TinyUSBSim.h:125
uint8_t * ctrlBuf
Definition TinyUSBSim.h:118
uint16_t ctrlLen
Definition TinyUSBSim.h:119
bool ctrlXfer
Definition TinyUSBSim.h:116
uint8_t * xferBuf
Definition TinyUSBSim.h:112
void * pumpRef
Definition TinyUSBSim.h:123
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
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
Desktop stand-in for TinyUSB's class driver table entry.
Definition TinyUSBSim.h:83
void(* reset)(uint8_t rhport)
Definition TinyUSBSim.h:87
void(* init)(void)
Definition TinyUSBSim.h:85
bool(* xfer_cb)(uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint32_t xferred_bytes)
Definition TinyUSBSim.h:92
bool(* control_xfer_cb)(uint8_t rhport, uint8_t stage, tusb_control_request_t const *request)
Definition TinyUSBSim.h:90
uint16_t(* open)(uint8_t rhport, tusb_desc_interface_t const *desc_intf, uint16_t max_len)
Definition TinyUSBSim.h:88