From 3c3a41eb2b33ba2253eb7439ff6290c414143416 Mon Sep 17 00:00:00 2001 From: Vasyl Vavrychuk Date: Tue, 14 Sep 2021 07:08:22 +0200 Subject: [PATCH] Automatically detect camera capable v4l2 device. Bug-AGL: SPEC-4148 Change-Id: I42cf0adf9e55679069992d73f079b916684f8e8b Signed-off-by: Vasyl Vavrychuk --- app/main.cpp | 4 +--- app/utils.cpp | 47 +++++++++++++++++++++++++++++++++++++++++++++++ app/utils.h | 3 +++ 3 files changed, 51 insertions(+), 3 deletions(-) diff --git a/app/main.cpp b/app/main.cpp index fff9c4c..182489e 100644 --- a/app/main.cpp +++ b/app/main.cpp @@ -23,8 +23,6 @@ #include #include -#define DEFAULT_VIDEO_DEVICE "/dev/video0" - // these only applies if the window is a dialog/pop-up one // by default the compositor make the window maximized #define WINDOW_WIDTH_SIZE 640 @@ -669,7 +667,7 @@ int main(int argc, char *argv[]) memset(pipeline_str, 0, sizeof(pipeline_str)); snprintf(pipeline_str, sizeof(pipeline_str), "v4l2src device=%s ! video/x-raw,width=%d,height=%d ! waylandsink", - DEFAULT_VIDEO_DEVICE, WINDOW_WIDTH_SIZE, WINDOW_HEIGHT_SIZE); + get_camera_device(), WINDOW_WIDTH_SIZE, WINDOW_HEIGHT_SIZE); gst_init(&gargc, &gargv); setbuf(stdout, NULL); diff --git a/app/utils.cpp b/app/utils.cpp index f1c3eae..1cf71f2 100644 --- a/app/utils.cpp +++ b/app/utils.cpp @@ -1,10 +1,16 @@ #include +#include +#include #include #include +#include #include #include #include #include +#include +#include +#include #include "utils.h" @@ -148,3 +154,44 @@ os_create_anonymous_file(off_t size) return fd; } + +const char* +get_camera_device(void) +{ + DIR *dir = opendir("/dev"); + if (!dir) { + perror("Couldn't open the '/dev' directory"); + return NULL; + } + + static char device[PATH_MAX]; + bool found = false; + while (struct dirent *dirent = readdir(dir)) { + if (strncmp(dirent->d_name, "video", strlen("video"))) + continue; + if (!isdigit(dirent->d_name[strlen("video")])) + continue; + + strcpy(device, "/dev/"); + strncat(device, dirent->d_name, sizeof(device) - 1); + + int fd = open(device, O_RDWR); + if (fd == -1) + continue; + struct v4l2_capability vid_cap; + if (ioctl(fd, VIDIOC_QUERYCAP, &vid_cap) < 0) { + close(fd); + continue; + } + close(fd); + + if ((vid_cap.capabilities & V4L2_CAP_VIDEO_CAPTURE) || + (vid_cap.capabilities & V4L2_CAP_VIDEO_CAPTURE_MPLANE)) { + found = true; + break; + } + } + + closedir(dir); + return found ? device : NULL; +} diff --git a/app/utils.h b/app/utils.h index 99b2e30..8d6f0fd 100644 --- a/app/utils.h +++ b/app/utils.h @@ -8,6 +8,9 @@ extern "C" { int os_create_anonymous_file(off_t size); +const char* +get_camera_device(void); + #ifdef __cplusplus } #endif -- 2.16.6