TinyUVC 0.1.0
Header-only USB Video Class (UVC) device: turn an MCU into a USB webcam
Loading...
Searching...
No Matches
TinyCamera.h
Go to the documentation of this file.
1#pragma once
9// angle brackets: "TinyCamera.h" would be this file
10#include <TinyCamera.h>
11
12#include "../TinyUVC.h"
13
14namespace tinyuvc {
15
37class UVCCamera {
38 public:
39 explicit UVCCamera(UVCDevice &uvc) : uvc_(uvc) {}
40
43 bool addFrameSize(framesize_t size) {
44 if (sizeCount_ >= TINYUVC_MAX_FRAMES) return false;
45 sizes_[sizeCount_++].size = size;
46 return true;
47 }
48
51 void setFps(std::initializer_list<uint8_t> fps) {
52 fpsCount_ = 0;
53 for (uint8_t f : fps)
54 if (f > 0 && fpsCount_ < TINYUVC_MAX_INTERVALS) fps_[fpsCount_++] = f;
55 }
56 void setFps(uint8_t fps) { setFps({fps}); }
57
61 void setFormats(bool mjpeg, bool yuy2) {
62 mjpeg_ = mjpeg;
63 yuy2_ = yuy2;
64 formatsSet_ = true;
65 }
66
69 void setRawPixelFormat(pixformat_t fmt) { raw_ = fmt; }
70
73 void setRgb565BigEndian(bool bigEndian) { rgb565BE_ = bigEndian; }
74
76 void setMaxJpegSize(uint32_t size) { maxJpegSize_ = size; }
77
81 bool begin(const camera_config_t &config, bool startDevice = true) {
82 config_ = config;
83 if (!formatsSet_) {
84 mjpeg_ = config.pixel_format == PIXFORMAT_JPEG;
85 yuy2_ = !mjpeg_;
86 }
87 if (!mjpeg_ && !yuy2_) return false;
88 if (sizeCount_ == 0) addFrameSize(config.frame_size);
89 if (fpsCount_ == 0) setFps(15);
90 // probe every frame size
91 const pixformat_t probeFmt = mjpeg_ ? PIXFORMAT_JPEG : raw_;
92 for (int i = 0; i < sizeCount_; i++) {
93 if (!configure(sizes_[i].size, probeFmt)) return false;
94 tiny_camera::TinyCameraFrame frame = camera_.captureFrame();
95 if (!frame) {
96 TINYUVC_LOGE("UVCCamera: probe capture failed");
97 return false;
98 }
99 sizes_[i].width = (uint16_t)frame.width();
100 sizes_[i].height = (uint16_t)frame.height();
101 TINYUVC_LOGI("UVCCamera: frame size %d is %ux%u", (int)sizes_[i].size,
102 sizes_[i].width, sizes_[i].height);
103 }
104 if (!uvc_.isActive()) {
106 for (int f = 0; f < 2; f++) {
107 const bool isMjpeg = f == 0;
108 if ((isMjpeg && !mjpeg_) || (!isMjpeg && !yuy2_)) continue;
109 for (int i = 0; i < sizeCount_; i++) {
111 sizes_[i].width, sizes_[i].height, fps_,
112 fpsCount_, isMjpeg ? maxJpegSize_ : 0))
113 return false;
114 }
115 }
116 if (startDevice && !uvc_.begin()) return false;
117 }
119 return true;
120 }
121
123 void end() {
124 frame_.release();
125 camera_.end();
126 }
127
134 bool copy() {
135 if (!uvc_.isStreaming()) {
136 if (uvc_.isActive() && frame_) frame_.release();
137 return false;
138 }
139 const uint32_t gen = uvc_.streamGeneration();
140 if (gen != generation_) {
141 generation_ = gen;
142 frame_.release();
143 if (!applySettings(uvc_.settings())) return false;
144 }
145 const uint32_t intervalUs = uvc_.settings().interval / 10;
146 const uint32_t now = platform::micros();
147 if (sentOnce_ && now - lastFrameUs_ < intervalUs) return false;
148 if (!uvc_.isReady()) return false;
149 frame_.release(); // the previous frame was completely sent
150 frame_ = camera_.captureFrame();
151 if (!frame_) return false;
152 TINYUVC_LOGD("UVCCamera: frame %u bytes, %ux%u, t=%ld.%06ld", (unsigned)frame_.size(),
153 (unsigned)frame_.width(), (unsigned)frame_.height(),
154 (long)frame_.timestamp().tv_sec, (long)frame_.timestamp().tv_usec);
155 PixelFormat fmt;
156 if (!toPixelFormat(frame_.format(), fmt)) {
157 TINYUVC_LOGW("UVCCamera: unsupported camera pixel format %d",
158 (int)frame_.format());
159 return false;
160 }
161 if (!uvc_.writeFrame(frame_.data(), frame_.size(), fmt)) return false;
162 lastFrameUs_ = now;
163 sentOnce_ = true;
164 frames_++;
165 return true;
166 }
167
169 tiny_camera::TinyCamera &camera() { return camera_; }
170
172 uint32_t frames() const { return frames_; }
173
174 protected:
176 struct Size {
177 framesize_t size;
178 uint16_t width = 0;
179 uint16_t height = 0;
180 };
182 tiny_camera::TinyCamera camera_;
183 tiny_camera::TinyCameraFrame frame_;
184 camera_config_t config_{};
186 int sizeCount_ = 0;
188 int fpsCount_ = 0;
189 bool mjpeg_ = false;
190 bool yuy2_ = true;
191 bool formatsSet_ = false;
192 pixformat_t raw_ = PIXFORMAT_RGB565;
193#if defined(ESP32) && !defined(TINY_CAMERA_ESP_VIDEO)
194 bool rgb565BE_ = true;
195#else
196 bool rgb565BE_ = false;
197#endif
198 uint32_t maxJpegSize_ = 0;
199 uint32_t generation_ = 0;
200 uint32_t lastFrameUs_ = 0;
201 bool sentOnce_ = false;
202 uint32_t frames_ = 0;
203 bool configured_ = false;
204 framesize_t curSize_{};
205 pixformat_t curFormat_{};
206
208 bool configure(framesize_t size, pixformat_t fmt) {
209 if (configured_ && camera_.isActive() && size == curSize_ && fmt == curFormat_)
210 return true;
211 camera_config_t cfg = config_;
212 cfg.frame_size = size;
213 cfg.pixel_format = fmt;
214 camera_.end();
215 if (!camera_.begin(cfg)) {
216 configured_ = false;
217 TINYUVC_LOGE("UVCCamera: camera start failed");
218 return false;
219 }
220 curSize_ = size;
221 curFormat_ = fmt;
222 configured_ = true;
223 return true;
224 }
225
227 for (int i = 0; i < sizeCount_; i++) {
228 if (sizes_[i].width == s.width && sizes_[i].height == s.height) {
229 const pixformat_t fmt = s.format == UVCFormat::MJPEG ? PIXFORMAT_JPEG : raw_;
230 return configure(sizes_[i].size, fmt);
231 }
232 }
233 TINYUVC_LOGE("UVCCamera: no frame size for %ux%u", s.width, s.height);
234 return false;
235 }
236
237 bool toPixelFormat(pixformat_t in, PixelFormat &out) const {
238 switch (in) {
239 case PIXFORMAT_JPEG:
240 out = PixelFormat::MJPEG;
241 return true;
242 case PIXFORMAT_RGB565:
244 return true;
245#if !defined(ARDUINO_ARCH_STM32) // the STM32 backend only has RGB565/JPEG
246 case PIXFORMAT_YUV422:
247 out = PixelFormat::YUY2;
248 return true;
249 case PIXFORMAT_GRAYSCALE:
250 out = PixelFormat::GRAY8;
251 return true;
252#endif
253#if (defined(ESP32) || defined(TINY_CAMERA_DESKTOP))
254 case PIXFORMAT_RGB888:
256 return true;
257#endif
258 default:
259 return false;
260 }
261 }
262};
263
264} // namespace tinyuvc
Streams a camera of the TinyCamera library (https://github.com/pschatzmann/TinyCamera) as USB webcam:...
#define TINYUVC_MAX_FRAMES
Max number of resolutions (frame descriptors) per format.
Definition UVCConfig.h:16
#define TINYUVC_MAX_INTERVALS
Max number of frame rates per resolution.
Definition UVCConfig.h:21
#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
Connects a TinyCamera to a UVCDevice.
Definition TinyCamera.h:37
bool copy()
Definition TinyCamera.h:134
tiny_camera::TinyCameraFrame frame_
Definition TinyCamera.h:183
Size sizes_[6]
Definition TinyCamera.h:185
void setRawPixelFormat(pixformat_t fmt)
Definition TinyCamera.h:69
bool mjpeg_
Definition TinyCamera.h:189
int sizeCount_
Definition TinyCamera.h:186
tiny_camera::TinyCamera & camera()
The camera (e.g. for sensor settings: camera().sensor())
Definition TinyCamera.h:169
tiny_camera::TinyCamera camera_
Definition TinyCamera.h:182
pixformat_t raw_
Definition TinyCamera.h:192
bool begin(const camera_config_t &config, bool startDevice=true)
Definition TinyCamera.h:81
bool configured_
Definition TinyCamera.h:203
void setFps(std::initializer_list< uint8_t > fps)
Definition TinyCamera.h:51
void setFormats(bool mjpeg, bool yuy2)
Definition TinyCamera.h:61
bool yuy2_
Definition TinyCamera.h:190
void setFps(uint8_t fps)
Definition TinyCamera.h:56
bool configure(framesize_t size, pixformat_t fmt)
(Re)starts the camera with the frame size and pixel format if needed
Definition TinyCamera.h:208
bool applySettings(const UVCStreamSettings &s)
Definition TinyCamera.h:226
UVCCamera(UVCDevice &uvc)
Definition TinyCamera.h:39
bool toPixelFormat(pixformat_t in, PixelFormat &out) const
Definition TinyCamera.h:237
camera_config_t config_
Definition TinyCamera.h:184
uint32_t frames() const
Number of frames sent.
Definition TinyCamera.h:172
bool sentOnce_
Definition TinyCamera.h:201
UVCDevice & uvc_
Definition TinyCamera.h:181
framesize_t curSize_
Definition TinyCamera.h:204
int fpsCount_
Definition TinyCamera.h:188
uint32_t generation_
Definition TinyCamera.h:199
uint32_t lastFrameUs_
Definition TinyCamera.h:200
uint8_t fps_[4]
Definition TinyCamera.h:187
bool rgb565BE_
Definition TinyCamera.h:196
void end()
Stops the camera.
Definition TinyCamera.h:123
uint32_t maxJpegSize_
Definition TinyCamera.h:198
pixformat_t curFormat_
Definition TinyCamera.h:205
uint32_t frames_
Definition TinyCamera.h:202
void setMaxJpegSize(uint32_t size)
Max JPEG size reported to the host (0 = width * height * 2)
Definition TinyCamera.h:76
bool formatsSet_
Definition TinyCamera.h:191
bool addFrameSize(framesize_t size)
Definition TinyCamera.h:43
void setRgb565BigEndian(bool bigEndian)
Definition TinyCamera.h:73
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
void clearResolutions()
Removes all formats/resolutions (before begin())
Definition UVCDevice.h:169
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 begin()
Definition UVCDevice.h:198
bool isReady() const
Definition UVCDevice.h:265
uint32_t streamGeneration() const
Incremented whenever the host starts or stops a stream.
Definition UVCDevice.h:275
uint32_t micros()
Definition UVCPlatformDesktop.h:47
TinyUVC: USB Video Class (UVC) device for Arduino and the desktop.
@ 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)
@ GRAY8
8 bit luminance
@ YUY2
packed YUV 4:2:2, byte order Y0 U Y1 V
@ RGB565_BE
16 bit RGB565, big endian (high byte first, esp32-camera)
A camera frame size offered to the host.
Definition TinyCamera.h:176
uint16_t width
Definition TinyCamera.h:178
framesize_t size
Definition TinyCamera.h:177
uint16_t height
Definition TinyCamera.h:179
The format, resolution and frame rate the host selected.
Definition UVCDefs.h:201
uint16_t width
Definition UVCDefs.h:203
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