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