Debian Codegraph @

cvd_src/uvcbuffer.cc

raw symbols SCIP
1 #include <cvd/video/uvcbuffer.h>
2
3 using namespace std;
4
5 namespace CVD
6 {
7
8 namespace Exceptions
9 {
10 namespace UVCBuffer
11 {
12 Init::Init(string, string err)
13 : All("UVCBuffer: failed to initialize: " + err)
14 {
15 }
16
17 DeviceOpen::DeviceOpen(string dev, string err)
18 : All("UVCBuffer: failed to open \"" + dev + "\": " + err)
19 {
20 }
21
22 FindDevice::FindDevice(string dev, string err)
23 : All("UVCBuffer: failed to find device \"" + dev + "\": " + err)
24 {
25 }
26
27 DeviceSetup::DeviceSetup(string dev, string err)
28 : All("UVCBuffer: failed to setup device \"" + dev + "\": " + err)
29 {
30 }
31
32 GetFrame::GetFrame(string dev, string err)
33 : All("UVCBuffer: failed to get frame on \"" + dev + "\": " + err)
34 {
35 }
36
37 PutFrame::PutFrame(string dev, string err)
38 : All("UVCBuffer: failed to put frame on \"" + dev + "\": " + err)
39 {
40 }
41
42 StreamOpen::StreamOpen(string dev, string err)
43 : All("UVCBuffer: failed open stream on \"" + dev + "\": " + err)
44 {
45 }
46 StreamStart::StreamStart(string dev, string err)
47 : All("UVCBuffer: failed start stream on \"" + dev + "\": " + err)
48 {
49 }
50 }
51 }
52
53 namespace UVC
54 {
55
56 ImageRef RawUVCBuffer::getSize()
57 {
58 return size;
59 }
60
61 double RawUVCBuffer::getRate()
62 {
63 return rate;
64 }
65
66 const std::string& RawUVCBuffer::device_name() const
67 {
68 return dev_str;
69 }
70
71 void RawUVCBuffer::fill_frame(void* data)
72 {
73 uvc_frame_t to;
74 to.data = data;
75
76 uvc_frame_t* from = NULL;
77
78 //This appears to get a frame which points to an
79 //internal structure. So, we duplicate the data.
80 //In theory, we could make a proper ringbuffer, but
81 //there seems to be no real need any more.
82 //
83 //Random number is a 1 second timeout, btw!
84 uvc_error e = uvc_stream_get_frame(strmh, &from, 1000000);
85
86 if(e != UVC_SUCCESS)
87 throw Exceptions::UVCBuffer::GetFrame(dev_str, uvc_strerror(e));
88
89 //If we're capturing RGB, then there might be a conversion
90 //involved (i.e. from MJPEG)
91 if(format == UVC_FRAME_FORMAT_RGB)
92 uvc_any2rgb(from, &to);
93 else
94 uvc_duplicate_frame(from, &to);
95 }
96
97 RawUVCBuffer::RawUVCBuffer(const std::string& sn_, unsigned int fmt, ImageRef size_, double frame_per_second, bool mjpeg, bool verbose)
98 : dev_str(sn_)
99 , size(size_)
100 , format(fmt)
101 , capture_mjpeg(mjpeg)
102 , rate(frame_per_second)
103 {
104 /* Initialize a UVC service context. Libuvc will set up its own libusb
105 * context. Replace NULL with a libusb_context pointer to run libuvc
106 * from an existing libusb context. */
107 uvc_error res = uvc_init(&ctx, NULL);
108
109 if(res < 0)
110 throw Exceptions::UVCBuffer::Init(dev_str, uvc_strerror(res));
111
112 const char* str = nullptr;
113 if(dev_str != "NULL" && dev_str != "/dev/null" && dev_str != "")
114 str = dev_str.c_str();
115
116 res = uvc_find_device(ctx, &dev, 0, 0, str);
117
118 if(res < 0)
119 throw Exceptions::UVCBuffer::FindDevice(dev_str, uvc_strerror(res));
120
121 /* Try to open the device: requires exclusive access */
122 res = uvc_open(dev, &devh);
123 if(res < 0)
124 throw Exceptions::UVCBuffer::DeviceOpen(dev_str, uvc_strerror(res));
125
126 if(verbose)
127 uvc_print_diag(devh, stderr);
128
129 if(mjpeg && fmt != UVC_FRAME_FORMAT_RGB)
130 throw Exceptions::UVCBuffer::DeviceSetup(dev_str, "MJPEG capture only valid for RGB");
131
132 int f = fmt;
133 if(mjpeg)
134 f = UVC_FRAME_FORMAT_MJPEG;
135
136 //f = UVC_FRAME_FORMAT_MJPEG;
137 /* Try to negotiate a 640x480 30 fps YUYV stream profile */
138 res = uvc_get_stream_ctrl_format_size(
139 devh, &ctrl, /* result stored in ctrl */
140 (uvc_frame_format)f, /* YUV 422, aka YUV 4:2:2. try _COMPRESSED */
141 size.x, size.y, frame_per_second /* width, height, fps */
142 );
143
144 if(res < 0)
145 throw Exceptions::UVCBuffer::DeviceSetup(dev_str, uvc_strerror(res));
146
147 res = uvc_stream_open_ctrl(devh, &strmh, &ctrl);
148 if(res < 0)
149 throw Exceptions::UVCBuffer::StreamOpen(dev_str, uvc_strerror(res));
150
151 /*Start stream with no callback, and no callback data */
152 res = uvc_stream_start(strmh, NULL, NULL, 0);
153 if(res < 0)
154 throw Exceptions::UVCBuffer::StreamStart(dev_str, uvc_strerror(res));
155 }
156
157 RawUVCBuffer::~RawUVCBuffer()
158 {
159 /* Release our handle on the device */
160 if(devh)
161 {
162 uvc_stop_streaming(devh);
163 uvc_close(devh);
164 devh = nullptr;
165 }
166
167 /* Release the device descriptor */
168 if(dev)
169 {
170 uvc_unref_device(dev);
171 dev = nullptr;
172 }
173
174 /* Close the UVC context. This closes and cleans up any existing device handles,
175 * and it closes the libusb context if one was not provided. */
176 if(ctx)
177 {
178 uvc_exit(ctx);
179 ctx = nullptr;
180 }
181 }
182
183 }
184 }