Debian Codegraph @

cvd/video/uvcbuffer.h

raw symbols SCIP
1 //-*- c++ -*-
2 #ifndef CVD_VIDEO_UVCBUFFER_H
3 #define CVD_VIDEO_UVCBUFFER_H
4
5 #include <cvd/byte.h>
6 #include <cvd/colourspaces.h>
7 #include <cvd/localvideoframe.h>
8 #include <cvd/rgb.h>
9 #include <cvd/videobuffer.h>
10 #include <libuvc/libuvc.h>
11
12 namespace CVD
13 {
14
15 namespace Exceptions
16 {
17 /// @ingroup gException
18 namespace UVCBuffer
19 {
20 /// @ingroup gException
21 struct All : public CVD::Exceptions::VideoBuffer::All
22 {
23 using CVD::Exceptions::VideoBuffer::All::All;
24 };
25
26 /// Error opening the device
27 /// @ingroup gException
28 struct Init : public All
29 {
30 Init(std::string dev, std::string err); ///< Construct from the device name
31 };
32
33 /// Error opening the device
34 /// @ingroup gException
35 struct DeviceOpen : public All
36 {
37 DeviceOpen(std::string dev, std::string err); ///< Construct from the device name
38 };
39
40 /// Error opening the device
41 /// @ingroup gException
42 struct StreamOpen : public All
43 {
44 StreamOpen(std::string dev, std::string err); ///< Construct from the device name
45 };
46
47 /// Error opening the device
48 /// @ingroup gException
49 struct StreamStart : public All
50 {
51 StreamStart(std::string dev, std::string err); ///< Construct from the device name
52 };
53
54 /// Error opening the device
55 /// @ingroup gException
56 struct FindDevice : public All
57 {
58 FindDevice(std::string dev, std::string err); ///< Construct from the device name
59 };
60
61 /// Error setting up the device
62 /// @ingroup gException
63 struct DeviceSetup : public All
64 {
65 DeviceSetup(std::string dev, std::string action); ///< Construct from the device string and an error string
66 };
67 /// Error in a put_frame() call
68 /// @ingroup gException
69 struct PutFrame : public All
70 {
71 PutFrame(std::string dev, std::string msg); ///< Construct from the device name
72 };
73 /// Error in a get_frame() call
74 /// @ingroup gException
75 struct GetFrame : public All
76 {
77 GetFrame(std::string dev, std::string msg); ///< Construct from the device name
78 };
79 }
80 }
81
82 namespace UVC
83 {
84 #ifndef DOXYGEN_IGNORE_INTERNAL
85 template <class C>
86 struct format;
87
88 //libUVC only supports a rather small subset of the possible
89 //types, but these seem to be the only common ones anyway.
90
91 template <>
92 struct format<yuv422>
93 {
94 static const unsigned int fmt = UVC_FRAME_FORMAT_YUYV;
95 };
96
97 template <>
98 struct format<Rgb<byte>>
99 {
100 static const unsigned int fmt = UVC_FRAME_FORMAT_RGB;
101 };
102
103 #endif
104
105 class RawUVCBuffer : public virtual RawVideoBuffer
106 {
107 public:
108 RawUVCBuffer(const std::string& dev, unsigned int fmt, ImageRef size, double frame_per_second, bool mjpeg, bool verbose);
109 ImageRef getSize();
110 void fill_frame(void*);
111 double getRate();
112 virtual ~RawUVCBuffer();
113 const std::string& device_name() const;
114
115 RawUVCBuffer(const RawUVCBuffer&) = delete;
116 void operator=(const RawUVCBuffer&) = delete;
117
118 private:
119 std::string dev_str;
120 uvc_context_t* ctx = 0;
121 uvc_device_t* dev = 0;
122 uvc_device_handle_t* devh = 0;
123 uvc_stream_ctrl_t ctrl = {};
124 uvc_stream_handle_t* strmh = 0;
125 ImageRef size;
126 int format;
127 bool capture_mjpeg;
128 double rate;
129 };
130
131 class V4L1Client;
132
133 };
134
135 /// A live video buffer which uses the Video for Linux 2 (V4L2) API.
136 /// A replacement for the (deprecated?) V4L2Buffer
137 /// @ingroup gVideoBuffer
138 template <class T>
139 class UVCBuffer : public VideoBuffer<T>, public UVC::RawUVCBuffer
140 {
141 public:
142 UVCBuffer(const std::string& dev, ImageRef size, int frames_per_second = 0, bool mjpeg = 0, bool verbose = 0)
143 : VideoBuffer<T>(VideoBufferType::Live)
144 , RawUVCBuffer(dev, UVC::format<T>::fmt, size, frames_per_second, mjpeg, verbose)
145 {
146 }
147
148 virtual ImageRef size()
149 {
150 return getSize();
151 }
152
153 virtual VideoFrame<T>* get_frame()
154 {
155
156 Image<T> frame(size());
157 fill_frame(frame.data());
158 return new UVCFrame(0.0, std::move(frame));
159 }
160
161 virtual void put_frame(VideoFrame<T>* f)
162 {
163 UVCFrame* vf = dynamic_cast<UVCFrame*>(f);
164 if(vf == 0)
165 throw Exceptions::UVCBuffer::PutFrame(device_name(), "Invalid VideoFrame");
166 delete vf;
167 }
168
169 virtual bool frame_pending()
170 {
171 return true;
172 }
173
174 virtual double frame_rate()
175 {
176 return getRate();
177 }
178
179 int num_buffers()
180 {
181 return num_buffers();
182 }
183
184 private:
185 struct UVCFrame : public LocalVideoFrame<T>
186 {
187 UVCFrame(double t, CVD::Image<T>&& local)
188 : LocalVideoFrame<T>(t, std::move(local))
189 {
190 }
191
192 int id;
193 friend class UVCBuffer<T>;
194 };
195
196 UVCBuffer(const UVCBuffer&) = delete;
197 void operator=(const UVCBuffer&) = delete;
198 };
199
200 };
201 #endif