3
votes

So all I need is simple - a list of currently avaliable video capture devices (web cameras). I need it in simple C or C++ console app. By list I mean something like such console output:

1) Asus Web Camera
2) Sony Web Camera

So I know how to get cam props such as W, H etc using code like:

#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/videodev.h>

int main(){
    int fd;
    struct video_capability video_cap;
    struct video_window     video_win;
    struct video_picture   video_pic;

    if((fd = open("/dev/video0", O_RDONLY)) == -1){
        perror("cam_info: Can't open device");
        return 1;
    }

    if(ioctl(fd, VIDIOCGCAP, &video_cap) == -1)
        perror("cam_info: Can't get capabilities");
    else {
        printf("Name:\t\t '%s'\n", video_cap.name);
        printf("Minimum size:\t%d x %d\n", video_cap.minwidth, video_cap.minheight);
        printf("Maximum size:\t%d x %d\n", video_cap.maxwidth, video_cap.maxheight);
    }

    if(ioctl(fd, VIDIOCGWIN, &video_win) == -1)
        perror("cam_info: Can't get window information");
    else
        printf("Current size:\t%d x %d\n", video_win.width, video_win.height);

    if(ioctl(fd, VIDIOCGPICT, &video_pic) == -1)
        perror("cam_info: Can't get picture information");
    else
        printf("Current depth:\t%d\n", video_pic.depth);

    close(fd);
    return 0;
}

but not name( How to get Names?

So It seems simple but I have one requirement - use of native OS apis as much as possible - no external libs - after all - all we want is to print out a a list - not to fly onto the moon!)

How to do such thing?


also from this series:

2
/sys/class/video4linux/video*/nameIgnacio Vazquez-Abrams
@Ignacio Vazquez-Abrams could you provide code that would be any how integrated into mine for how to print out names?Rella

2 Answers

6
votes

You are using the V4L1 API which is deprecated - V4L2 is the preferred API for new code.

In any case, the VIDIOC_QUERYCAP ioctl() is probably what you are looking for. You will want to have a look at the .card field of the returned struct v4l2_capability structure.

EDIT:

You could have a look at the source code for the v4l-info utility, which does exactly what you want.

0
votes

V4L2 documentation says that there can be 64 allowed devices for each type. For instance for path /dev/video there can be 64 devices addresssed as /dev/video0, /dev/video1, /dev/video2 ...

Iterate over 64 devices until the ioctl retuens ENIVAL which specifies end of the tree.