YARP
Yet Another Robot Platform
list.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2020 Istituto Italiano di Tecnologia (IIT)
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 
19 
20 #include "list.h"
21 #include "USBcameraLogComponent.h"
22 
23 #include <yarp/os/LogStream.h>
24 
25 #include <cerrno>
26 #include <cstdio>
27 #include <cstring>
28 #include <linux/videodev2.h>
29 #include <sys/ioctl.h>
30 
31 
32 void enum_image_fmt_v4l2(int fd)
33 {
34  struct v4l2_fmtdesc fmtd;
35 
36  yCInfo(USBCAMERA, "============================================");
37  yCInfo(USBCAMERA, "Querying image format");
39 
40  memset(&fmtd, 0, sizeof(struct v4l2_fmtdesc));
41  fmtd.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
42  fmtd.index = 0;
43 
44  while (ioctl(fd, VIDIOC_ENUM_FMT, &fmtd) >= 0) {
46  "%d - %s (compressed : %d) (%#x)",
47  fmtd.index,
48  fmtd.description,
49  fmtd.flags,
50  fmtd.pixelformat);
51  fmtd.index++;
52  }
53 
55 }
56 
58 {
59  struct v4l2_format fmt;
60  struct v4l2_fmtdesc fmtd; //to find a text description of the image format
61 
62  memset(&fmt, 0, sizeof(struct v4l2_format));
63  fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
64  memset(&fmtd, 0, sizeof(struct v4l2_fmtdesc));
65  fmtd.index = 0;
66  fmtd.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
67 
68  yCInfo(USBCAMERA, "============================================");
69  yCInfo(USBCAMERA, "Querying current image format settings");
71 
72  if (-1 == ioctl(fd, VIDIOC_G_FMT, &fmt)) {
73  yCError(USBCAMERA, "Failed to get image format: %d, %s", errno, strerror(errno));
74  } else {
75  yCInfo(USBCAMERA, "Current width: %d", fmt.fmt.pix.width);
76  yCInfo(USBCAMERA, "Current height: %d", fmt.fmt.pix.height);
77  yCInfo(USBCAMERA, "Current bytes per line: %d", fmt.fmt.pix.bytesperline);
78  yCInfo(USBCAMERA, "Current image size: %d", fmt.fmt.pix.sizeimage);
79  yCInfo(USBCAMERA, "Current color space: %d", fmt.fmt.pix.colorspace);
80  yCInfo(USBCAMERA, "Current pixel format: ");
81  while (ioctl(fd, VIDIOC_ENUM_FMT, &fmtd) >= 0) {
82  if (fmt.fmt.pix.pixelformat == fmtd.pixelformat) {
83  yCInfo(USBCAMERA, "%s", fmtd.description);
84  break;
85  }
86  fmtd.index++;
87  }
88  }
89 
91 }
92 
94 {
95  struct v4l2_input vin;
96  struct v4l2_tuner tun;
97  struct v4l2_frequency freq;
98 
99  memset(&vin, 0, sizeof(struct v4l2_input));
100  vin.index = 0;
101 
102  yCInfo(USBCAMERA, "============================================");
103  yCInfo(USBCAMERA, "Querying capture capabilities");
104  yCInfo(USBCAMERA);
105 
106  while (ioctl(fd, VIDIOC_ENUMINPUT, &vin) >= 0) {
107  yCInfo(USBCAMERA, "Input number: %d", vin.index);
108  yCInfo(USBCAMERA, "Name: %s", vin.name);
109  yCInfo(USBCAMERA, "Type: (%d) ", vin.type);
110  if (vin.type & V4L2_INPUT_TYPE_TUNER) {
111  yCInfo(USBCAMERA, "Tuner");
112  yCInfo(USBCAMERA, "Tuner index: %d", vin.tuner);
113  memset(&tun, 0, sizeof(struct v4l2_tuner));
114  tun.index = vin.tuner;
115  if (ioctl(fd, VIDIOC_G_TUNER, &tun) == 0) {
116  yCInfo(USBCAMERA, "Name: %s", tun.name);
117  if (tun.type == V4L2_TUNER_RADIO) {
118  yCInfo(USBCAMERA, "It is a RADIO tuner");
119  }
120  if (tun.type == V4L2_TUNER_ANALOG_TV) {
121  yCInfo(USBCAMERA, "It is a TV tuner");
122  }
123  if (tun.capability & V4L2_TUNER_CAP_LOW) {
124  yCInfo(USBCAMERA, "Frequencies in units of 62.5Hz");
125  } else {
126  yCInfo(USBCAMERA, "Frequencies in units of 62.5kHz");
127  }
128 
129  if (tun.capability & V4L2_TUNER_CAP_NORM) {
130  yCInfo(USBCAMERA, "Multi-standard tuner");
131  }
132  if (tun.capability & V4L2_TUNER_CAP_STEREO) {
133  yCInfo(USBCAMERA, "Stereo reception supported");
134  }
135  /* More flags here */
137  "lowest tunable frequency: %.2f %s",
138  tun.rangelow * 62.5,
139  (tun.capability & V4L2_TUNER_CAP_LOW) ? "Hz" : "kHz");
141  "highest tunable frequency: %.2f %s",
142  tun.rangehigh * 62.5,
143  (tun.capability & V4L2_TUNER_CAP_LOW) ? "Hz" : "kHz");
144  memset(&freq, 0, sizeof(struct v4l2_frequency));
145  freq.tuner = vin.tuner;
146  if (ioctl(fd, VIDIOC_G_FREQUENCY, &freq) == 0) {
148  "Current frequency: %.2f %s",
149  freq.frequency * 62.5,
150  (tun.capability & V4L2_TUNER_CAP_LOW) ? "Hz" : "kHz");
151  }
152  }
153  }
154  if (vin.type & V4L2_INPUT_TYPE_CAMERA) {
155  yCInfo(USBCAMERA, "Camera");
156  }
157  yCInfo(USBCAMERA, "Supported standards: (%d) ", (int)vin.std);
158  if (vin.std & V4L2_STD_PAL) {
159  yCInfo(USBCAMERA, "PAL ");
160  }
161  if (vin.std & V4L2_STD_NTSC) {
162  yCInfo(USBCAMERA, "NTSC ");
163  }
164  if (vin.std & V4L2_STD_SECAM) {
165  yCInfo(USBCAMERA, "SECAM ");
166  }
167  yCInfo(USBCAMERA);
168  vin.index++;
169  }
170 }
171 
173 {
174  struct v4l2_frmsizeenum frms;
175  struct v4l2_fmtdesc fmtd;
176 
177  memset(&frms, 0, sizeof(struct v4l2_frmsizeenum));
178  memset(&fmtd, 0, sizeof(struct v4l2_fmtdesc));
179  fmtd.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
180  fmtd.index = 0;
181 
182 
183  yCInfo(USBCAMERA, "============================================");
184  yCInfo(USBCAMERA, "Querying supported frame sizes");
185  yCInfo(USBCAMERA);
186 
187  while (ioctl(fd, VIDIOC_ENUM_FMT, &fmtd) >= 0) {
188  yCInfo(USBCAMERA, "Image format: %s", fmtd.description);
189  frms.index = 0;
190  frms.pixel_format = fmtd.pixelformat;
191  while (ioctl(fd, VIDIOC_ENUM_FRAMESIZES, &frms) >= 0) {
192  if (frms.type == V4L2_FRMSIZE_TYPE_DISCRETE) {
194  "index %2d: Width: %4d - Height: %d",
195  frms.index,
196  frms.discrete.width,
197  frms.discrete.height);
198  frms.index++;
199  } else {
201  "index %2d\tMin, max & step height: %d - %d - %d Min, max & step width: %d - %d - %d",
202  frms.index,
203  frms.stepwise.min_height,
204  frms.stepwise.max_height,
205  frms.stepwise.step_height,
206  frms.stepwise.min_width,
207  frms.stepwise.max_width,
208  frms.stepwise.step_width);
209  break;
210  }
211  }
212  fmtd.index++;
213  }
214 }
215 
216 void print_v4l2_control(struct v4l2_queryctrl* qc)
217 {
219  "Control: id: 0x%x - name: %s - min: %d -max: %d - step: %d - type: %d(%s) - flags: %d (%s%s%s%s%s%s)",
220  qc->id,
221  (char*)&qc->name,
222  qc->minimum,
223  qc->maximum,
224  qc->step,
225  qc->type,
226  (qc->type == V4L2_CTRL_TYPE_INTEGER ? "Integer" :
227  qc->type == V4L2_CTRL_TYPE_BOOLEAN ? "Boolean" :
228  qc->type == V4L2_CTRL_TYPE_MENU ? "Menu" :
229  qc->type == V4L2_CTRL_TYPE_BUTTON ? "Button" :
230  qc->type == V4L2_CTRL_TYPE_INTEGER64 ? "Integer64" :
231  qc->type == V4L2_CTRL_TYPE_CTRL_CLASS ? "Class" :
232  ""),
233  qc->flags,
234  qc->flags & V4L2_CTRL_FLAG_DISABLED ? "Disabled " : "",
235  qc->flags & V4L2_CTRL_FLAG_GRABBED ? "Grabbed " : "",
236  qc->flags & V4L2_CTRL_FLAG_READ_ONLY ? "ReadOnly " : "",
237  qc->flags & V4L2_CTRL_FLAG_UPDATE ? "Update " : "",
238  qc->flags & V4L2_CTRL_FLAG_INACTIVE ? "Inactive " : "",
239  qc->flags & V4L2_CTRL_FLAG_SLIDER ? "slider " : "");
240 }
241 
242 // void query_controls_v4l2(int fd)
243 // {
244 // int i;
245 // struct v4l2_queryctrl qctrl;
246 // CLEAR(qctrl);
247 // struct v4lconvert_data *d = v4lconvert_create(fd);
248 //
249 // yCInfo(USBCAMERA, "============================================");
250 // yCInfo(USBCAMERA, "Querying standard controls");
251 // yCInfo(USBCAMERA);
252 //
253 // // std ctrls
254 // for( i = V4L2_CID_BASE; i< V4L2_CID_LASTP1; i++) {
255 // qctrl.id = i;
256 // //if((ioctl(fd, VIDIOC_QUERYCTRL, &qctrl) == 0))
257 // if(v4lconvert_vidioc_queryctrl(d,&qctrl)==0) {
258 // print_v4l2_control(&qctrl);
259 // }
260 // }
261 //
262 // yCInfo(USBCAMERA, "============================================");
263 // yCInfo(USBCAMERA, "Querying private controls");
264 // yCInfo(USBCAMERA);
265 //
266 // // priv ctrls
267 // for (qctrl.id = V4L2_CID_PRIVATE_BASE;; qctrl.id++) {
268 // if(v4lconvert_vidioc_queryctrl(d,&qctrl)==0) {
269 // print_v4l2_control(&qctrl);
270 // } else {
271 // if (errno == EINVAL)
272 // break;
273 // yCError(USBCAMERA, "we shouldnt be here...");
274 // }
275 // }
276 //
277 // yCInfo(USBCAMERA, "============================================");
278 // yCInfo(USBCAMERA, "Querying extended controls");
279 // yCInfo(USBCAMERA);
280 //
281 // //checking extended controls
282 // qctrl.id = V4L2_CTRL_FLAG_NEXT_CTRL;
283 // while (v4lconvert_vidioc_queryctrl(d,&qctrl)==0) {
284 // print_v4l2_control(&qctrl);
285 // qctrl.id |= V4L2_CTRL_FLAG_NEXT_CTRL;
286 // }
287 // v4lconvert_destroy(d);
288 // }
289 
290 
291 void list_cap_v4l2(int fd)
292 {
293  struct v4l2_capability cap;
294 
295  yCInfo(USBCAMERA, "============================================");
296  yCInfo(USBCAMERA, "Querying general capabilities");
297  yCInfo(USBCAMERA);
298 
299  if (ioctl(fd, VIDIOC_QUERYCAP, &cap) < 0) {
300  yCError(USBCAMERA, "v4l2 not supported. Maybe a v4l1 device ...");
301  } else {
302  //print capabilities
303  yCInfo(USBCAMERA, "Driver name: %s", cap.driver);
304  yCInfo(USBCAMERA, "Device name: %s", cap.card);
305  yCInfo(USBCAMERA, "bus_info: %s", cap.bus_info);
307  "version: %u.%u.%u",
308  (cap.version >> 16) & 0xFF,
309  (cap.version >> 8) & 0xFF,
310  cap.version & 0xFF);
311 
312  yCInfo(USBCAMERA, "%s capture capability", (cap.capabilities & V4L2_CAP_VIDEO_CAPTURE) ? "Has" : "Does NOT have");
313  yCInfo(USBCAMERA, "%s output capability", (cap.capabilities & V4L2_CAP_VIDEO_OUTPUT) ? "Has" : "Does NOT have");
314  yCInfo(USBCAMERA, "%s overlay capability", (cap.capabilities & V4L2_CAP_VIDEO_OVERLAY) ? "Has" : "Does NOT have");
315  yCInfo(USBCAMERA, "%s VBI capture capability", (cap.capabilities & V4L2_CAP_VBI_CAPTURE) ? "Has" : "Does NOT have");
316  yCInfo(USBCAMERA, "%s VBI output capability", (cap.capabilities & V4L2_CAP_VBI_OUTPUT) ? "Has" : "Does NOT have");
317  yCInfo(USBCAMERA, "%s SLICED VBI capture capability", (cap.capabilities & V4L2_CAP_SLICED_VBI_CAPTURE) ? "Has" : "Does NOT have");
318  yCInfo(USBCAMERA, "%s SLICED VBI output capability", (cap.capabilities & V4L2_CAP_SLICED_VBI_OUTPUT) ? "Has" : "Does NOT have");
319  yCInfo(USBCAMERA, "%s RDS capability", (cap.capabilities & V4L2_CAP_RDS_CAPTURE) ? "Has" : "Does NOT have");
320  yCInfo(USBCAMERA, "%s tuner capability", (cap.capabilities & V4L2_CAP_TUNER) ? "Has" : "Does NOT have");
321  yCInfo(USBCAMERA, "%s audio capability", (cap.capabilities & V4L2_CAP_AUDIO) ? "Has" : "Does NOT have");
322  yCInfo(USBCAMERA, "%s radio capability", (cap.capabilities & V4L2_CAP_RADIO) ? "Has" : "Does NOT have");
323  yCInfo(USBCAMERA, "%s read/write capability", (cap.capabilities & V4L2_CAP_READWRITE) ? "Has" : "Does NOT have");
324  yCInfo(USBCAMERA, "%s async IO capability", (cap.capabilities & V4L2_CAP_ASYNCIO) ? "Has" : "Does NOT have");
325  yCInfo(USBCAMERA, "%s streaming capability", (cap.capabilities & V4L2_CAP_STREAMING) ? "Has" : "Does NOT have");
326  yCInfo(USBCAMERA);
327 
328  if (cap.capabilities & V4L2_CAP_VIDEO_CAPTURE) {
330  }
331  // FIXME Enumerate other capabilites (output, overlay,...
332 
336  // query_controls_v4l2(fd);
337  }
338 }
LogStream.h
list_cap_v4l2
void list_cap_v4l2(int fd)
Definition: list.cpp:291
query_frame_sizes_v4l2
void query_frame_sizes_v4l2(int fd)
Definition: list.cpp:172
print_v4l2_control
void print_v4l2_control(struct v4l2_queryctrl *qc)
Definition: list.cpp:216
USBcameraLogComponent.h
query_current_image_fmt_v4l2
void query_current_image_fmt_v4l2(int fd)
Definition: list.cpp:57
yarp::os::LogComponent::name
constexpr const char * name() const
Definition: LogComponent.h:42
query_capture_intf_v4l2
void query_capture_intf_v4l2(int fd)
Definition: list.cpp:93
yCError
#define yCError(component,...)
Definition: LogComponent.h:157
yCInfo
#define yCInfo(component,...)
Definition: LogComponent.h:135
USBCAMERA
const yarp::os::LogComponent & USBCAMERA()
Definition: USBcameraLogComponent.cpp:11
enum_image_fmt_v4l2
void enum_image_fmt_v4l2(int fd)
Definition: list.cpp:32
list.h