TinyUVC 0.1.0
Header-only USB Video Class (UVC) device: turn an MCU into a USB webcam
Loading...
Searching...
No Matches
AudioTools.h
Go to the documentation of this file.
1#pragma once
17#include <AudioToolsConfig.h>
18#include <AudioTools/Video/Video.h>
19
20#include "../TinyUVC.h"
21
22namespace tinyuvc {
23
25inline bool toPixelFormat(audio_tools::VideoFormat format, PixelFormat &out) {
26 using audio_tools::VideoFormat;
27 switch (format) {
28 case VideoFormat::MJPEG:
30 return true;
31 case VideoFormat::YUV422:
33 return true;
34 case VideoFormat::RGB565:
36 return true;
37 case VideoFormat::RGB888:
39 return true;
40 case VideoFormat::RAW: // 24 bit BGR
42 return true;
43 default:
44 return false; // H264/MPEG need a decoder first
45 }
46}
47
50inline bool addResolution(UVCDevice &uvc, const audio_tools::VideoInfo &info,
51 uint32_t maxJpegSize = 0) {
52 PixelFormat pf;
53 if (!info || !toPixelFormat(info.format, pf)) return false;
54 const uint8_t fps = info.fps <= 0 ? 15 : info.fps >= 255 ? 255 : (uint8_t)(info.fps + 0.5f);
55 const bool jpeg = pf == PixelFormat::MJPEG;
56 return uvc.addResolution(jpeg ? UVCFormat::MJPEG : UVCFormat::YUY2, info.width,
57 info.height, fps, jpeg ? maxJpegSize : 0);
58}
59
79class UVCVideoOutput : public audio_tools::VideoOutput {
80 public:
82 : uvc_(uvc), format_(format) {}
83
85 void setPixelFormat(PixelFormat format) { format_ = format; }
86 PixelFormat pixelFormat() const { return format_; }
87
89 bool setVideoFormat(audio_tools::VideoFormat format) {
90 return toPixelFormat(format, format_);
91 }
92
95 void setTimeout(uint32_t ms) { timeoutMs_ = ms; }
96
98 void setMaxFrameSize(size_t size) { maxFrameSize_ = size; }
99
104 release();
105 for (auto &b : buffers_) b.setMemoryType(type);
106 }
107
111 bool reserve(size_t size) {
112 return buffers_[0].reserve(size) && buffers_[1].reserve(size);
113 }
114
117 void release() {
119 if (!uvc_.isSending()) buffers_[1 - fill_].release();
120 overflow_ = false;
121 }
122
127 void setPacing(bool pacing) { pacing_ = pacing; }
128
130 void setFlushOnWrite(bool flushOnWrite) { flushOnWrite_ = flushOnWrite; }
131
132 size_t write(const uint8_t *data, size_t len) override {
134 const bool complete = flushOnWrite_ || (buf.empty() && isCompleteJpeg(data, len));
135 if (overflow_ || buf.size() + len > maxFrameSize_) {
136 overflow_ = true;
137 } else if (!buf.append(data, len)) {
138 TINYUVC_LOGW("UVCVideoOutput: out of memory (frame of %u bytes)",
139 (unsigned)(buf.size() + len));
140 overflow_ = true;
141 }
142 if (complete) flush();
143 return len;
144 }
145
147 void flush() override {
149 if (buf.empty() && !overflow_) return; // e.g. flush() after a complete JPEG
150 hadOutput_ = false;
151 if (buf.empty()) {
152 overflow_ = false;
153 dropped_++;
154 return;
155 }
156 bool ok = !overflow_ && uvc_.isStreaming();
157 if (ok && pacing_ && sent_) {
158 const uint32_t intervalUs = uvc_.settings().interval / 10;
159 while (uvc_.isStreaming() && platform::micros() - lastUs_ < intervalUs)
161 }
162 if (ok && !uvc_.isReady()) {
163 const uint32_t timeout = timeoutMs_ > 0 ? timeoutMs_ : (pacing_ ? 1000 : 0);
164 ok = timeout > 0 && uvc_.waitReady(timeout);
165 }
166 if (ok && uvc_.writeFrame(buf.data(), buf.size(), format_)) {
167 // keep this buffer until it is sent, write the next frame to the other
168 fill_ = 1 - fill_;
169 hadOutput_ = true;
170 frames_++;
171 sent_ = true;
173 } else {
174 dropped_++;
175 }
177 overflow_ = false;
178 }
179
180 bool hadOutput() const override { return hadOutput_; }
181
183 bool isCompleteJpeg(const uint8_t *data, size_t len) const {
184 return format_ == PixelFormat::MJPEG && len >= 4 && data[0] == 0xFF &&
185 data[1] == 0xD8 && data[len - 2] == 0xFF && data[len - 1] == 0xD9;
186 }
187
189 uint32_t frames() const { return frames_; }
190 uint32_t dropped() const { return dropped_; }
191
192 protected:
196 int fill_ = 0;
197 size_t maxFrameSize_ = 512 * 1024;
198 uint32_t timeoutMs_ = 0;
199 bool pacing_ = false;
200 bool flushOnWrite_ = false;
201 bool sent_ = false;
202 uint32_t lastUs_ = 0;
203 bool overflow_ = false;
204 bool hadOutput_ = false;
205 uint32_t frames_ = 0;
206 uint32_t dropped_ = 0;
207};
208
225 public:
226 UVCVideoCopy(UVCDevice &uvc, audio_tools::VideoFrameSource &source)
227 : uvc_(uvc), source_(source) {}
228
231 bool begin(uint32_t maxJpegSize = 0) {
232 const audio_tools::VideoInfo info = source_.videoInfo();
233 if (!toPixelFormat(info.format, format_)) {
234 TINYUVC_LOGE("UVCVideoCopy: unsupported video format %d", (int)info.format);
235 return false;
236 }
237 if (uvc_.isActive()) return true;
238 if (!addResolution(uvc_, info, maxJpegSize)) return false;
239 return uvc_.begin();
240 }
241
243 void setPixelFormat(PixelFormat format) { format_ = format; }
244
248 bool copy() {
249 if (!uvc_.isReady()) return false;
250 const uint32_t now = platform::micros();
251 if (sent_ && now - lastUs_ < uvc_.settings().interval / 10) return false;
252 size_t len = 0;
253 // the previous frame stays valid until this call: it was sent completely
254 const uint8_t *data = source_.nextFrame(len);
255 if (data == nullptr || len == 0) return false;
256 if (!uvc_.writeFrame(data, len, format_)) return false;
257 lastUs_ = now;
258 sent_ = true;
259 return true;
260 }
261
262 protected:
264 audio_tools::VideoFrameSource &source_;
266 uint32_t lastUs_ = 0;
267 bool sent_ = false;
268};
269
270} // namespace tinyuvc
#define TINYUVC_LOGE(...)
Definition UVCLogger.h:75
#define TINYUVC_LOGW(...)
Definition UVCLogger.h:77
USB Video Class (UVC 1.1) device - a USB webcam.
Definition UVCDevice.h:95
bool isStreaming() const
true while the host is streaming (between commit and stop)
Definition UVCDevice.h:261
bool isActive() const
true if begin() succeeded
Definition UVCDevice.h:258
UVCStreamSettings settings() const
Format, resolution and frame rate the host selected.
Definition UVCDevice.h:272
bool addResolution(UVCFormat format, uint16_t width, uint16_t height, uint8_t fps, uint32_t maxFrameSize=0)
Definition UVCDevice.h:107
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
bool waitReady(uint32_t timeoutMs)
Waits until isReady() (true) or the timeout or end of streaming (false)
Definition UVCDevice.h:323
bool isSending() const
Definition UVCDevice.h:269
bool begin()
Definition UVCDevice.h:198
bool isReady() const
Definition UVCDevice.h:265
A growing byte buffer for one frame, allocated with UVCMemory (so in PSRAM where available)....
Definition UVCMemory.h:95
bool reserve(size_t size)
Makes sure that size bytes fit without a new allocation.
Definition UVCMemory.h:106
void release()
Frees the memory.
Definition UVCMemory.h:136
size_t size() const
Definition UVCMemory.h:144
uint8_t * data()
Definition UVCMemory.h:142
bool append(const uint8_t *data, size_t len)
Appends data; false if no memory is left (the buffer is unchanged)
Definition UVCMemory.h:118
void clear()
Definition UVCMemory.h:133
bool empty() const
Definition UVCMemory.h:146
Streams the frames of an audio-tools VideoFrameSource (e.g. CameraFrameSourceTinyCamera) to a UVCDevi...
Definition AudioTools.h:224
bool copy()
Definition AudioTools.h:248
UVCVideoCopy(UVCDevice &uvc, audio_tools::VideoFrameSource &source)
Definition AudioTools.h:226
UVCDevice & uvc_
Definition AudioTools.h:263
uint32_t lastUs_
Definition AudioTools.h:266
bool sent_
Definition AudioTools.h:267
void setPixelFormat(PixelFormat format)
Pixel format of the source frames (set by begin())
Definition AudioTools.h:243
bool begin(uint32_t maxJpegSize=0)
Definition AudioTools.h:231
PixelFormat format_
Definition AudioTools.h:265
audio_tools::VideoFrameSource & source_
Definition AudioTools.h:264
VideoOutput that sends every frame (write() calls up to flush()) to a UVCDevice. Two frame buffers ar...
Definition AudioTools.h:79
bool setVideoFormat(audio_tools::VideoFormat format)
Same with an audio-tools VideoFormat; false if not supported.
Definition AudioTools.h:89
bool overflow_
Definition AudioTools.h:203
void setMemoryType(UVCMemoryType type)
Definition AudioTools.h:103
int fill_
Definition AudioTools.h:196
bool pacing_
Definition AudioTools.h:199
uint32_t dropped() const
Definition AudioTools.h:190
UVCFrameBuffer buffers_[2]
Definition AudioTools.h:195
UVCDevice & uvc_
Definition AudioTools.h:193
void setMaxFrameSize(size_t size)
Max frame size: bigger frames are dropped (default 512 KB)
Definition AudioTools.h:98
bool hadOutput() const override
Definition AudioTools.h:180
UVCVideoOutput(UVCDevice &uvc, PixelFormat format=PixelFormat::MJPEG)
Definition AudioTools.h:81
bool isCompleteJpeg(const uint8_t *data, size_t len) const
true if data is a whole JPEG image (SOI ... EOI)
Definition AudioTools.h:183
void setPixelFormat(PixelFormat format)
Pixel format of the frames written to this output.
Definition AudioTools.h:85
bool hadOutput_
Definition AudioTools.h:204
void release()
Definition AudioTools.h:117
uint32_t dropped_
Definition AudioTools.h:206
void flush() override
Completes the frame and hands it to the UVC device.
Definition AudioTools.h:147
PixelFormat pixelFormat() const
Definition AudioTools.h:86
uint32_t frames_
Definition AudioTools.h:205
PixelFormat format_
Definition AudioTools.h:194
bool flushOnWrite_
Definition AudioTools.h:200
bool sent_
Definition AudioTools.h:201
void setPacing(bool pacing)
Definition AudioTools.h:127
void setFlushOnWrite(bool flushOnWrite)
Every write() is a complete frame (for writers that never call flush())
Definition AudioTools.h:130
uint32_t timeoutMs_
Definition AudioTools.h:198
size_t maxFrameSize_
Definition AudioTools.h:197
uint32_t lastUs_
Definition AudioTools.h:202
uint32_t frames() const
Frames handed to the device / dropped.
Definition AudioTools.h:189
bool reserve(size_t size)
Definition AudioTools.h:111
size_t write(const uint8_t *data, size_t len) override
Definition AudioTools.h:132
void setTimeout(uint32_t ms)
Definition AudioTools.h:95
void idle()
Called while waiting: lets the USB stack make progress.
Definition UVCPlatformDesktop.h:33
uint32_t micros()
Definition UVCPlatformDesktop.h:47
TinyUVC: USB Video Class (UVC) device for Arduino and the desktop.
bool toPixelFormat(audio_tools::VideoFormat format, PixelFormat &out)
Maps an audio-tools VideoFormat to the matching PixelFormat.
Definition AudioTools.h:25
UVCMemoryType
Definition UVCMemory.h:29
bool addResolution(UVCDevice &uvc, const audio_tools::VideoInfo &info, uint32_t maxJpegSize=0)
Definition AudioTools.h:50
@ 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)
@ RGB888
24 bit, byte order R G B
@ RGB565_LE
16 bit RGB565, little endian (low byte first)
@ BGR888
24 bit, byte order B G R
@ YUY2
packed YUV 4:2:2, byte order Y0 U Y1 V
uint32_t interval
Frame interval in 100ns units.
Definition UVCDefs.h:206