Repository navigation
Expand file tree
/
Copy pathlist_cams.c
More file actions
48 lines (45 loc) · 1.59 KB
/
Copy pathlist_cams.c
File metadata and controls
48 lines (45 loc) · 1.59 KB
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
// list_cams.c : list webcams, their pixel-formats & resolutions
#define _GNU_SOURCE
#include <stdio.h>
#include <fcntl.h>
#include <linux/videodev2.h>
#include <dirent.h>
#include <string.h>
#include <unistd.h>
#include <sys/ioctl.h>
static void show_caps(int fd) {
struct v4l2_capability cap;
if (ioctl(fd, VIDIOC_QUERYCAP, &cap) == -1) return;
printf(" Driver: %-16s Card: %s\n", cap.driver, cap.card);
struct v4l2_fmtdesc fmtd = { .index = 0, .type = V4L2_BUF_TYPE_VIDEO_CAPTURE };
while (!ioctl(fd, VIDIOC_ENUM_FMT, &fmtd)) {
printf(" pixfmt: %c%c%c%c %s\n",
fmtd.pixelformat & 0xff, (fmtd.pixelformat >> 8) & 0xff,
(fmtd.pixelformat >> 16) & 0xff, (fmtd.pixelformat >> 24) & 0xff,
fmtd.description);
struct v4l2_frmsizeenum sz = { .pixel_format = fmtd.pixelformat, .index = 0 };
while (!ioctl(fd, VIDIOC_ENUM_FRAMESIZES, &sz)) {
if (sz.type == V4L2_FRMSIZE_TYPE_DISCRETE)
printf(" %ux%u\n", sz.discrete.width, sz.discrete.height);
sz.index++;
}
fmtd.index++;
}
}
int main(void) {
DIR *d = opendir("/dev");
struct dirent *ent;
if (!d) { perror("opendir"); return 1; }
while ((ent = readdir(d))) {
if (strncmp(ent->d_name, "video", 5) == 0) {
char path[64]; snprintf(path, sizeof(path), "/dev/%s", ent->d_name);
int fd = open(path, O_RDWR);
if (fd == -1) continue;
printf("%s:\n", path);
show_caps(fd);
close(fd);
}
}
closedir(d);
return 0;
}