Skip to content

Instantly share code, notes, and snippets.

@tugstugi
Created May 7, 2012 13:03
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tugstugi/2627647 to your computer and use it in GitHub Desktop.
Save tugstugi/2627647 to your computer and use it in GitHub Desktop.
v4l: camera capability and control query
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <linux/videodev2.h>
int main() {
int N = 100;
char* deviceName = "/dev/video0";
int fd = open(deviceName, O_RDWR);
if (fd == -1) {
printf("Couldn't open video device!\n");
return -1;
}
printf("Quering camera capabilities...\n");
struct v4l2_capability vid_cap;
if(ioctl(fd, VIDIOC_QUERYCAP, &vid_cap) >= 0) {
printf(" Driver: %s\n", vid_cap.driver);
printf(" Card: %s\n", vid_cap.card);
printf(" Bus Info: %s\n", vid_cap.bus_info);
printf(" Version: %2d\n", vid_cap.version);
printf(" Capabilities: \n");
if (vid_cap.capabilities & V4L2_CAP_VIDEO_CAPTURE) {
printf(" Video capture support: Yes\n");
} else {
printf(" Video capture support: No\n");
}
if (vid_cap.capabilities & V4L2_CAP_VIDEO_OUTPUT) {
printf(" Video output support: Yes\n");
} else {
printf(" Video output support: No\n");
}
if (vid_cap.capabilities & V4L2_CAP_VIDEO_OVERLAY) {
printf(" Video overlay support: Yes\n");
} else {
printf(" Video overlay support: No\n");
}
if (vid_cap.capabilities & V4L2_CAP_VBI_CAPTURE) {
printf(" Raw VBI capture support: Yes\n");
} else {
printf(" Raw VBI capture support: No\n");
}
if (vid_cap.capabilities & V4L2_CAP_VBI_OUTPUT) {
printf(" RAW VBI output support: Yes\n");
} else {
printf(" RAW VBI output support: No\n");
}
if (vid_cap.capabilities & V4L2_CAP_SLICED_VBI_CAPTURE){
printf(" Sliced VBI capture support: Yes\n");
} else {
printf(" Sliced VBI capture support: No\n");
}
if (vid_cap.capabilities & V4L2_CAP_SLICED_VBI_OUTPUT){
printf(" Sliced VBI capture support: Yes\n");
} else {
printf(" Sliced VBI capture support: No\n");
}
if (vid_cap.capabilities & V4L2_CAP_RDS_CAPTURE){
printf(" RDS capture support: Yes\n");
} else {
printf(" RDS capture support: No\n");
}
if (vid_cap.capabilities & V4L2_CAP_TUNER){
printf(" Device has a tuner: Yes\n");
} else {
printf(" Device has a tuner: No\n");
}
if (vid_cap.capabilities & V4L2_CAP_AUDIO){
printf(" Audio support: Yes\n");
} else {
printf(" Audio support: No\n");
}
if (vid_cap.capabilities & V4L2_CAP_RADIO){
printf(" Device has radio support: Yes\n");
} else {
printf(" Device has radio support: No\n");
}
if (vid_cap.capabilities & V4L2_CAP_READWRITE){
printf(" Supports read/write system calls: Yes\n");
} else {
printf(" Supports read/write system calls: No\n");
}
if (vid_cap.capabilities & V4L2_CAP_ASYNCIO){
printf(" Supports async i/o: Yes\n");
} else {
printf(" Supports async i/o: No\n");
}
if (vid_cap.capabilities & V4L2_CAP_STREAMING){
printf(" Supports streaming: Yes\n");
} else {
printf(" Supports streaming: No\n");
}
} else {
printf("Couldn't connect to the video device!\n");
return -1;
}
printf("\n\n\n");
printf("Discovering camera controls...\n");
struct v4l2_queryctrl queryctrl;
struct v4l2_querymenu querymenu;
memset (&queryctrl, 0, sizeof (queryctrl));
for (queryctrl.id = V4L2_CID_BASE; queryctrl.id < V4L2_CID_BASE+N; queryctrl.id++) {
if (ioctl (fd, VIDIOC_QUERYCTRL, &queryctrl) >= 0) {
if (queryctrl.flags & V4L2_CTRL_FLAG_DISABLED)
continue;
printf("VIDIOC_QUERYCTRL(V4L2_CID_BASE+%d)\n", queryctrl.id-V4L2_CID_BASE);
printf(" id: %d\n", queryctrl.id);
switch (queryctrl.type){
case V4L2_CTRL_TYPE_INTEGER:
printf(" type: INTEGER\n");
break;
case V4L2_CTRL_TYPE_BOOLEAN:
printf(" type: BOOLEAN\n");
break;
case V4L2_CTRL_TYPE_MENU:
printf(" type: MENU\n");
querymenu.id = queryctrl.id;
for (querymenu.index = queryctrl.minimum; querymenu.index < queryctrl.maximum; querymenu.index++){
printf(" menu id: %d\n", querymenu.index);
printf(" menu name: %s\n", querymenu.name);
}
break;
case V4L2_CTRL_TYPE_BUTTON:
printf(" type: BUTTON\n");
break;
} /* end switch */
printf(" name: %s\n", queryctrl.name);
printf(" minimum: %d\n", queryctrl.minimum);
printf(" maximum: %d\n", queryctrl.maximum);
printf(" step: %d\n", queryctrl.step);
printf(" default_value: %d\n", queryctrl.default_value);
printf(" flags: %d\n", queryctrl.flags);
} else {
if (errno == EINVAL)
continue;
perror("VIDIOC_QUERYCTRL");
break;
} /* end if*/
} /* end for */
return 0;
}
@patrickelectric
Copy link

It's missing #include <sys/ioctl.h>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment