1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211 | #include "V4LCameraSettingsManager.h"
/**
Based on example from
https://www.kernel.org/doc/html/v4.14/media/uapi/v4l/control.html
*/
void V4LCameraSettingsManager::enumerate_menu(int fd, v4l2_queryctrl& queryctrl)
{
printf(" Menu items:\n");
struct v4l2_querymenu querymenu;
memset(&querymenu, 0, sizeof(querymenu));
querymenu.id = queryctrl.id;
for (querymenu.index = queryctrl.minimum; (int)querymenu.index <= queryctrl.maximum; querymenu.index++) {
if (0 == xioctl(fd, VIDIOC_QUERYMENU, &querymenu)) {
printf(" +- [%i] %s\n", querymenu.index, querymenu.name);
} else {
printf(" +- [%i] (%i) %s\n", querymenu.index, errno, strerror(errno));
}
}
}
void V4LCameraSettingsManager::enumerate_controls(int fd)
{
struct v4l2_queryctrl queryctrl;
memset(&queryctrl, 0, sizeof(queryctrl));
printf("=== Camera Controls BEGIN ===\n");
queryctrl.id = V4L2_CTRL_FLAG_NEXT_CTRL;
while (0 == xioctl(fd, VIDIOC_QUERYCTRL, &queryctrl))
{
if (!(queryctrl.flags & V4L2_CTRL_FLAG_DISABLED))
{
printf(" %i: %i, %s (min: %i, max: %i, step: %i) \n", queryctrl.id, queryctrl.type, queryctrl.name, queryctrl.minimum, queryctrl.maximum, queryctrl.step);
if (queryctrl.type == V4L2_CTRL_TYPE_MENU) {
enumerate_menu(fd, queryctrl);
}
}
queryctrl.id |= V4L2_CTRL_FLAG_NEXT_CTRL;
}
if (errno != EINVAL) {
perror("VIDIOC_QUERYCTRL");
exit(EXIT_FAILURE);
}
printf("=== Camera Controls END ===\n");
}
int V4LCameraSettingsManager::getSingleCameraParameterRaw(int cameraFd, const std::string& cameraName, uint32_t parameterID)<--- Technically the member function 'V4LCameraSettingsManager::getSingleCameraParameterRaw' can be static. [+]The member function 'V4LCameraSettingsManager::getSingleCameraParameterRaw' can be made a static function. Making a function static can bring a performance benefit since no 'this' instance is passed to the function. This change should not cause compiler errors but it does not necessarily make sense conceptually. Think about your design and the task of the function first - is it a function that must not access members of class instances?
{
// TODO: make it more general and maybe do it only once at the beginning
// NOTE: first query information regarding the parameter to verify it is avaliable and enabled
struct v4l2_queryctrl queryctrl;
memset(&queryctrl, 0, sizeof(queryctrl)); // is it necessary?
queryctrl.id = parameterID;
// check if query was successful
if (xioctl(cameraFd, VIDIOC_QUERYCTRL, &queryctrl) < 0) {
std::cerr << LOG << "VIDIOC_QUERYCTRL for parameter " << parameterID << " failed with code " << errno << " " << strerror(errno) << std::endl;
return -1;
}
// check if parameter is avaliable (enabled)
if (queryctrl.flags & V4L2_CTRL_FLAG_DISABLED) {
std::cerr << LOG << "V4L2_CTRL_FLAG_DISABLED failed. Parameter " << parameterID << " seems to be not avaliable." << std::endl;
return -1;
}
// check if parameter is supported
// NOTE: we only consider boolean, integer and menu types as supported
if (queryctrl.type != V4L2_CTRL_TYPE_BOOLEAN && queryctrl.type != V4L2_CTRL_TYPE_INTEGER && queryctrl.type != V4L2_CTRL_TYPE_MENU) {
std::cerr << LOG << "ERROR: type " << queryctrl.type << " of the parameter " << parameterID << " is not supported." << std::endl;
return -1;
}
/*
struct v4l2_control {
__u32 id; // Identifies the control, set by the application.
__s32 value; // New value or current value.
}
*/
struct v4l2_control control;
control.id = parameterID;
int errorOccured = xioctl(cameraFd, VIDIOC_G_CTRL, &control);
if (hasIOError(cameraName, errorOccured, errno, false)) {
// TODO: some parameter may have -1 as a valid value
return -1;
} else {
return control.value;
}
}
bool V4LCameraSettingsManager::setSingleCameraParameterRaw(int cameraFd, const std::string& cameraName, uint32_t parameterID, const std::string& parameterName, int value)<--- Technically the member function 'V4LCameraSettingsManager::setSingleCameraParameterRaw' can be static. [+]The member function 'V4LCameraSettingsManager::setSingleCameraParameterRaw' can be made a static function. Making a function static can bring a performance benefit since no 'this' instance is passed to the function. This change should not cause compiler errors but it does not necessarily make sense conceptually. Think about your design and the task of the function first - is it a function that must not access members of class instances?
{
// TODO: make it more general and maybe do it only once at the beginning
// NOTE: first query information regarding the parameter to verify it is avaliable and enabled
struct v4l2_queryctrl queryctrl;
memset(&queryctrl, 0, sizeof(queryctrl)); // is it necessary?
queryctrl.id = parameterID;
// check if query was successful
if (xioctl(cameraFd, VIDIOC_QUERYCTRL, &queryctrl) < 0) {
std::cerr << LOG << "VIDIOC_QUERYCTRL for parameter " << parameterName << " failed with code " << errno << " " << strerror(errno) << std::endl;
return false;
}
// check if parameter is avaliable (enabled)
if (queryctrl.flags & V4L2_CTRL_FLAG_DISABLED) {
std::cerr << LOG << "V4L2_CTRL_FLAG_DISABLED failed. Parameter " << parameterName << " seems to be not avaliable." << std::endl;
return false;
}
// check if parameter is supported
// NOTE: we only consider boolean, integer and menu types as supported
if (queryctrl.type != V4L2_CTRL_TYPE_BOOLEAN && queryctrl.type != V4L2_CTRL_TYPE_INTEGER && queryctrl.type != V4L2_CTRL_TYPE_MENU) {
std::cerr << LOG << "ERROR: type " << queryctrl.type << " of the parameter " << parameterName << " is not supported." << std::endl;
return false;
}
// clip value
if (value < queryctrl.minimum) {
std::cout << LOG << "Clipping control value " << parameterName << " from " << value << " to " << queryctrl.minimum << std::endl;
value = queryctrl.minimum;
} else if (value > queryctrl.maximum) {
std::cout << LOG << "Clipping control value " << parameterName << " from " << value << " to " << queryctrl.maximum << std::endl;
value = queryctrl.maximum;
}
/*
struct v4l2_control {
__u32 id; // Identifies the control, set by the application.
__s32 value; // New value or current value.
}
*/
struct v4l2_control control {
.id = parameterID,
.value = value
};
std::cout << LOG << "Setting control value " << parameterName << " to " << value << std::endl;
int error = xioctl(cameraFd, VIDIOC_S_CTRL, &control);
return !hasIOError(cameraName, error, errno, false);
}
bool V4LCameraSettingsManager::setRawIfChanged(int cameraFd, const std::string& cameraName, uint32_t parameterID,
const std::string& parameterName, int value, int &bufferedValue, bool force)
{
if ((force || bufferedValue != value) &&
setSingleCameraParameterRaw(cameraFd, cameraName, parameterID, parameterName, value))
{
bufferedValue = value;
return true;
}
return false;
}
// https://01.org/linuxgraphics/gfx-docs/drm/media/uapi/v4l/capture.c.html
int V4LCameraSettingsManager::xioctl(int fd, int request, void *arg)
{
int r;
// TODO: possibly endless loop?
do {
r = ioctl(fd, request, arg);
} while (-1 == r && EINTR == errno); // repeat if the call was interrupted
return r;
}
/**
// https://linuxtv.org/downloads/v4l-dvb-apis/v4l-drivers/uvcvideo.html
// https://elixir.bootlin.com/linux/latest/source/include/uapi/linux/uvcvideo.h#L61
struct uvc_xu_control_query {
__u8 unit; // Extension unit ID
__u8 selector; // Control selector
__u8 query; // Request code to send to the device (Video Class-Specific Request Code, defined in linux/usb/video.h A.8.)
__u16 size; // Control data size (in bytes)
__u8 __user *data; // Control value
};
*/
int V4LCameraSettingsManager::querySingleCameraParameterUVC(int cameraFd, uint8_t query, uint8_t selector, void* data, uint16_t size)
{
uvc_xu_control_query queryctrl {
.unit = 3,
.selector = selector,
.query = query,
.size = size,
.data = static_cast<uint8_t*>(data)
};
return xioctl(cameraFd, UVCIOC_CTRL_QUERY, &queryctrl);
}
bool V4LCameraSettingsManager::hasIOErrorPrint(int lineNumber, const std::string& cameraName, int errOccured, int errNo, bool exitByIOError, const std::string& paramName)
{
if (errOccured < 0 && errNo != EAGAIN)
{
std::cout << LOG << paramName << " [hasIOError:" << lineNumber << "]"
<< " failed with errno " << errNo << " (" << strerror(errNo) << ")"
<< (exitByIOError?" >> exiting":"") << std::endl;
if (exitByIOError) {
assert(errOccured >= 0);
}
return true;
}
return false;
}
|