rename device -> video capture

This commit is contained in:
2025-09-22 22:26:24 +02:00
parent 8bfcbfa128
commit 12565aab08
7 changed files with 192 additions and 174 deletions
+2 -1
View File
@@ -40,7 +40,8 @@ static void print_help(int status_code) {
" -f, --frag fragment shaders directory (default: TODO)\n" " -f, --frag fragment shaders directory (default: TODO)\n"
" -fc, --frag-config fragment shaders config file (default: " " -fc, --frag-config fragment shaders config file (default: "
"TODO)\n" "TODO)\n"
" -v, --video-in path to video device (multiple allowed)\n" " -v, --video-in path to video capture device (multiple "
"allowed)\n"
" -is, --internal-size internal texture height (default: 720)\n" " -is, --internal-size internal texture height (default: 720)\n"
"(default: " "(default: "
"3)\n" "3)\n"
+21 -19
View File
@@ -19,7 +19,7 @@ static Context context;
static ShaderProgram program; static ShaderProgram program;
static Window *window_output; static Window *window_output;
static Window *window_monitor; static Window *window_monitor;
static VideoDevice *devices; static VideoCapture *video_captures;
static File *fragment_shaders; static File *fragment_shaders;
static File common_shader_code; static File common_shader_code;
static Timer timer; static Timer timer;
@@ -145,37 +145,38 @@ static void free_files(unsigned int frag_count) {
file_free(&common_shader_code, true); file_free(&common_shader_code, true);
} }
static void init_devices(char *video_in[MAX_VIDEO], unsigned int video_count, static void init_video_captures(char *video_in[MAX_VIDEO],
unsigned int internal_size) { unsigned int video_count,
unsigned int internal_size) {
unsigned int i; unsigned int i;
devices = malloc(video_count * sizeof(VideoDevice)); video_captures = malloc(video_count * sizeof(VideoCapture));
for (i = 0; i < video_count; i++) { for (i = 0; i < video_count; i++) {
devices[i] = video_init(video_in[i], internal_size); video_captures[i] = video_init(video_in[i], internal_size);
} }
} }
static void start_devices(unsigned int video_count) { static void start_video_captures(unsigned int video_count) {
unsigned int i; unsigned int i;
for (i = 0; i < video_count; i++) { for (i = 0; i < video_count; i++) {
if (!devices[i].error) { if (!video_captures[i].error) {
video_background_read(&devices[i], &stop); video_background_read(&video_captures[i], &stop);
} }
} }
} }
static void free_devices(unsigned int video_count) { static void free_video_captures(unsigned int video_count) {
unsigned int i; unsigned int i;
for (i = 0; i < video_count; i++) { for (i = 0; i < video_count; i++) {
shaders_free_video(program, devices[i]); shaders_free_video(program, video_captures[i]);
video_free(devices[i]); video_free(video_captures[i]);
} }
free(devices); free(video_captures);
} }
static void error_callback(int error, const char *description) { static void error_callback(int error, const char *description) {
@@ -243,7 +244,8 @@ void forge_run(Parameters params) {
context.internal_height = params.internal_size; context.internal_height = params.internal_size;
init_devices(params.video_in, params.video_count, params.internal_size); init_video_captures(params.video_in, params.video_count,
params.internal_size);
if (params.output) { if (params.output) {
window_output = window_init(PACKAGE " " VERSION, params.output_screen, window_output = window_init(PACKAGE " " VERSION, params.output_screen,
@@ -251,8 +253,8 @@ void forge_run(Parameters params) {
window_use(window_output, &context); window_use(window_output, &context);
program = shaders_init(fragment_shaders, shader_config, context, devices, program = shaders_init(fragment_shaders, shader_config, context,
params.video_count, NULL); video_captures, params.video_count, NULL);
} else { } else {
window_output = NULL; window_output = NULL;
} }
@@ -264,8 +266,8 @@ void forge_run(Parameters params) {
window_use(window_monitor, &context); window_use(window_monitor, &context);
program = shaders_init(fragment_shaders, shader_config, context, devices, program = shaders_init(fragment_shaders, shader_config, context,
params.video_count, video_captures, params.video_count,
window_output != NULL ? &program : NULL); window_output != NULL ? &program : NULL);
} else { } else {
window_monitor = NULL; window_monitor = NULL;
@@ -280,7 +282,7 @@ void forge_run(Parameters params) {
timer = timer_init(30); timer = timer_init(30);
start_devices(params.video_count); start_video_captures(params.video_count);
log_info("Initialized"); log_info("Initialized");
@@ -307,7 +309,7 @@ void forge_run(Parameters params) {
shaders_free_window(program, params.output); shaders_free_window(program, params.output);
} }
free_devices(params.video_count); free_video_captures(params.video_count);
free_context(); free_context();
+27 -25
View File
@@ -69,30 +69,31 @@ static void rebind_textures(ShaderProgram *program) {
} }
} }
static void link_video_to_texture(ShaderProgram *program, VideoDevice *device, static void link_video_to_texture(ShaderProgram *program,
VideoCapture *video_capture,
unsigned int texture_index) { unsigned int texture_index) {
device->dma_image = EGL_NO_IMAGE_KHR; video_capture->dma_image = EGL_NO_IMAGE_KHR;
const EGLint attrib_list[] = {EGL_WIDTH, const EGLint attrib_list[] = {EGL_WIDTH,
device->width, video_capture->width,
EGL_HEIGHT, EGL_HEIGHT,
device->height, video_capture->height,
EGL_LINUX_DRM_FOURCC_EXT, EGL_LINUX_DRM_FOURCC_EXT,
device->pixelformat, video_capture->pixelformat,
EGL_DMA_BUF_PLANE0_FD_EXT, EGL_DMA_BUF_PLANE0_FD_EXT,
device->exp_fd, video_capture->exp_fd,
EGL_DMA_BUF_PLANE0_OFFSET_EXT, EGL_DMA_BUF_PLANE0_OFFSET_EXT,
0, 0,
EGL_DMA_BUF_PLANE0_PITCH_EXT, EGL_DMA_BUF_PLANE0_PITCH_EXT,
device->bytesperline, video_capture->bytesperline,
EGL_NONE}; EGL_NONE};
device->dma_image = eglCreateImageKHR(program->egl_display, EGL_NO_CONTEXT, video_capture->dma_image = eglCreateImageKHR(
EGL_LINUX_DMA_BUF_EXT, program->egl_display, EGL_NO_CONTEXT, EGL_LINUX_DMA_BUF_EXT,
(EGLClientBuffer)NULL, attrib_list); (EGLClientBuffer)NULL, attrib_list);
if (device->dma_image == EGL_NO_IMAGE_KHR) { if (video_capture->dma_image == EGL_NO_IMAGE_KHR) {
log_error("(%s) eglCreateImageKHR failed %04x", device->name, log_error("(%s) eglCreateImageKHR failed %04x", video_capture->name,
eglGetError()); eglGetError());
return; return;
} }
@@ -101,27 +102,28 @@ static void link_video_to_texture(ShaderProgram *program, VideoDevice *device,
glBindTexture(GL_TEXTURE_2D, program->textures[texture_index]); glBindTexture(GL_TEXTURE_2D, program->textures[texture_index]);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, device->width, device->height, 0, glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, video_capture->width,
GL_RGB, GL_UNSIGNED_BYTE, 0); video_capture->height, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
// https://registry.khronos.org/OpenGL/extensions/EXT/EXT_EGL_image_storage.txt // https://registry.khronos.org/OpenGL/extensions/EXT/EXT_EGL_image_storage.txt
glEGLImageTargetTextureStorageEXT(program->textures[texture_index], glEGLImageTargetTextureStorageEXT(program->textures[texture_index],
(GLeglImageOES)device->dma_image, NULL); (GLeglImageOES)video_capture->dma_image,
NULL);
log_info("Texture %d linked to %s", texture_index, device->name); log_info("Texture %d linked to %s", texture_index, video_capture->name);
} }
static void init_videos(ShaderProgram *program, ConfigFile shader_config, static void init_videos(ShaderProgram *program, ConfigFile shader_config,
VideoDevice *devices, unsigned int device_count) { VideoCapture *video_captures, unsigned int count) {
unsigned int i; unsigned int i;
unsigned tex_i; unsigned tex_i;
char name[32]; char name[32];
for (i = 0; i < program->in_count; i++) { for (i = 0; i < program->in_count; i++) {
if (i < device_count && !devices[i].error) { if (i < count && !video_captures[i].error) {
sprintf(name, "IN_%d_OUT", i + 1); sprintf(name, "IN_%d_OUT", i + 1);
tex_i = config_file_get_int(shader_config, name, 0); tex_i = config_file_get_int(shader_config, name, 0);
link_video_to_texture(program, &devices[i], tex_i); link_video_to_texture(program, &video_captures[i], tex_i);
} else { } else {
log_warn("Cannot link input %d", i + 1); log_warn("Cannot link input %d", i + 1);
} }
@@ -353,8 +355,8 @@ static void init_programs(ShaderProgram *program, ConfigFile shader_config) {
} }
ShaderProgram shaders_init(File *fragment_shaders, ConfigFile shader_config, ShaderProgram shaders_init(File *fragment_shaders, ConfigFile shader_config,
Context context, VideoDevice *devices, Context context, VideoCapture *video_captures,
unsigned int device_count, ShaderProgram *previous) { unsigned int count, ShaderProgram *previous) {
ShaderProgram program; ShaderProgram program;
if (previous == NULL) { if (previous == NULL) {
@@ -383,7 +385,7 @@ ShaderProgram shaders_init(File *fragment_shaders, ConfigFile shader_config,
init_textures(&program, context); init_textures(&program, context);
init_videos(&program, shader_config, devices, device_count); init_videos(&program, shader_config, video_captures, count);
init_framebuffers(&program, shader_config); init_framebuffers(&program, shader_config);
@@ -548,8 +550,8 @@ void shaders_free_window(ShaderProgram program, bool secondary) {
glDeleteVertexArrays(1, &program.vertex_array[secondary ? 1 : 0]); glDeleteVertexArrays(1, &program.vertex_array[secondary ? 1 : 0]);
} }
void shaders_free_video(ShaderProgram program, VideoDevice device) { void shaders_free_video(ShaderProgram program, VideoCapture video_capture) {
if (!device.error && device.dma_image != EGL_NO_IMAGE_KHR) { if (!video_capture.error && video_capture.dma_image != EGL_NO_IMAGE_KHR) {
eglDestroyImageKHR(program.egl_display, device.dma_image); eglDestroyImageKHR(program.egl_display, video_capture.dma_image);
} }
} }
+3 -3
View File
@@ -4,8 +4,8 @@
#define SHADERS_H #define SHADERS_H
ShaderProgram shaders_init(File *fragment_shaders, ConfigFile shader_config, ShaderProgram shaders_init(File *fragment_shaders, ConfigFile shader_config,
Context context, VideoDevice *devices, Context context, VideoCapture *video_captures,
unsigned int device_count, ShaderProgram *previous); unsigned int count, ShaderProgram *previous);
void shaders_update(ShaderProgram program, File *fragment_shaders, void shaders_update(ShaderProgram program, File *fragment_shaders,
unsigned int i); unsigned int i);
@@ -17,6 +17,6 @@ void shaders_free(ShaderProgram program);
void shaders_free_window(ShaderProgram program, bool secondary); void shaders_free_window(ShaderProgram program, bool secondary);
void shaders_free_video(ShaderProgram program, VideoDevice device); void shaders_free_video(ShaderProgram program, VideoCapture video_capture);
#endif /* SHADERS_H */ #endif /* SHADERS_H */
+2 -2
View File
@@ -85,7 +85,7 @@ typedef struct ShaderProgram {
EGLDisplay egl_display; EGLDisplay egl_display;
} ShaderProgram; } ShaderProgram;
typedef struct VideoDevice { typedef struct VideoCapture {
char *name; char *name;
bool error; bool error;
int fd; int fd;
@@ -97,7 +97,7 @@ typedef struct VideoDevice {
bool output; bool output;
struct v4l2_buffer buf; struct v4l2_buffer buf;
EGLImageKHR dma_image; EGLImageKHR dma_image;
} VideoDevice; } VideoCapture;
typedef GLFWwindow Window; typedef GLFWwindow Window;
+134 -121
View File
@@ -11,89 +11,99 @@
#include "video.h" #include "video.h"
#include "window.h" #include "window.h"
static void ioctl_error(VideoDevice *device, const char *operation, static void ioctl_error(VideoCapture *video_capture, const char *operation,
const char *default_msg) { const char *default_msg) {
if (errno == EINVAL) { if (errno == EINVAL) {
log_warn("(%s) %s -> EINVAL: %s", device->name, operation, default_msg); log_warn("(%s) %s -> EINVAL: %s", video_capture->name, operation,
default_msg);
} else if (errno == EAGAIN) { } else if (errno == EAGAIN) {
log_warn("(%s) %s -> EAGAIN: device state invalid", operation, log_warn("(%s) %s -> EAGAIN: device state invalid", operation,
device->name); video_capture->name);
} else if (errno == EBADF) { } else if (errno == EBADF) {
log_warn("(%s) %s -> EBADF: file descriptor invalid", operation, log_warn("(%s) %s -> EBADF: file descriptor invalid", operation,
device->name); video_capture->name);
} else if (errno == EBUSY) { } else if (errno == EBUSY) {
log_warn("(%s) %s -> EBUSY: device is busy", device->name, operation); log_warn("(%s) %s -> EBUSY: device is busy", video_capture->name,
operation);
} else if (errno == EFAULT) { } else if (errno == EFAULT) {
log_warn("(%s) %s -> EFAULT: invalid pointer", device->name, operation); log_warn("(%s) %s -> EFAULT: invalid pointer", video_capture->name,
operation);
} else if (errno == ENODEV) { } else if (errno == ENODEV) {
log_warn("(%s) %s -> ENODEV: device not found", device->name, operation); log_warn("(%s) %s -> ENODEV: device not found", video_capture->name,
operation);
} else if (errno == ENOMEM) { } else if (errno == ENOMEM) {
log_warn("(%s) %s -> ENOMEM: not enough memory", device->name, operation); log_warn("(%s) %s -> ENOMEM: not enough memory", video_capture->name,
operation);
} else if (errno == ENOTTY) { } else if (errno == ENOTTY) {
log_warn("(%s) %s -> ENOTTY: ioctl not supported by file descriptor", log_warn("(%s) %s -> ENOTTY: ioctl not supported by file descriptor",
device->name, operation); video_capture->name, operation);
} else if (errno == ENOSPC) { } else if (errno == ENOSPC) {
log_warn("(%s) %s -> ENOSPC: USB bandwidth error", device->name, operation); log_warn("(%s) %s -> ENOSPC: USB bandwidth error", video_capture->name,
operation);
} else if (errno == EPERM) { } else if (errno == EPERM) {
log_warn("(%s) %s -> EPERM: permission denied", device->name, operation); log_warn("(%s) %s -> EPERM: permission denied", video_capture->name,
operation);
} else if (errno == EIO) { } else if (errno == EIO) {
log_warn("(%s) %s -> EIO: I/O error", device->name, operation); log_warn("(%s) %s -> EIO: I/O error", video_capture->name, operation);
} else if (errno == ENXIO) { } else if (errno == ENXIO) {
log_warn("(%s) %s -> ENXIO: no device exists", device->name, operation); log_warn("(%s) %s -> ENXIO: no device exists", video_capture->name,
operation);
} else if (errno == EPIPE) { } else if (errno == EPIPE) {
log_warn("(%s) %s -> EPIPE: pipeline error", device->name, operation); log_warn("(%s) %s -> EPIPE: pipeline error", video_capture->name,
operation);
} else if (errno == ENOLINK) { } else if (errno == ENOLINK) {
log_warn("(%s) %s -> ENOLINK: pipeline configuration invalid for Media " log_warn("(%s) %s -> ENOLINK: pipeline configuration invalid for Media "
"Controller interface", "Controller interface",
device->name, operation); video_capture->name, operation);
} else { } else {
log_error("(%s) %s unknown error %d", device->name, operation, errno); log_error("(%s) %s unknown error %d", video_capture->name, operation,
errno);
} }
device->error = true; video_capture->error = true;
} }
static VideoDevice open_device(char *name) { static VideoCapture open_device(char *name) {
VideoDevice device; VideoCapture video_capture;
device.name = name; video_capture.name = name;
device.error = false; video_capture.error = false;
device.fd = -1; video_capture.fd = -1;
device.fd = open(name, O_RDWR); video_capture.fd = open(name, O_RDWR);
if (device.fd == -1) { if (video_capture.fd == -1) {
log_warn("(%s) Cannot open device", name); log_warn("(%s) Cannot open device", name);
device.error = true; video_capture.error = true;
} }
return device; return video_capture;
} }
static bool check_device_caps(VideoDevice *device) { static bool check_caps(VideoCapture *video_capture) {
struct v4l2_capability cap; struct v4l2_capability cap;
memset(&cap, 0, sizeof(cap)); memset(&cap, 0, sizeof(cap));
if (ioctl(device->fd, VIDIOC_QUERYCAP, &cap) == -1) { if (ioctl(video_capture->fd, VIDIOC_QUERYCAP, &cap) == -1) {
ioctl_error(device, "VIDIOC_QUERYCAP", "Not a V4L2 device"); ioctl_error(video_capture, "VIDIOC_QUERYCAP", "Not a V4L2 device");
return false; return false;
} }
if (!(cap.capabilities & V4L2_CAP_VIDEO_CAPTURE)) { if (!(cap.capabilities & V4L2_CAP_VIDEO_CAPTURE)) {
log_warn("(%s) Not a video capture device", device->name); log_warn("(%s) Not a video capture device", video_capture->name);
device->error = true; video_capture->error = true;
return false; return false;
} }
if (!(cap.capabilities & V4L2_CAP_STREAMING)) { if (!(cap.capabilities & V4L2_CAP_STREAMING)) {
log_warn("(%s) No streaming i/o support", device->name); log_warn("(%s) No streaming i/o support", video_capture->name);
device->error = true; video_capture->error = true;
return false; return false;
} }
return true; return true;
} }
static bool get_available_sizes(VideoDevice *device, static bool get_available_sizes(VideoCapture *video_capture,
unsigned int preferred_height) { unsigned int preferred_height) {
struct v4l2_frmsizeenum fmt_enum; struct v4l2_frmsizeenum fmt_enum;
unsigned int index; unsigned int index;
@@ -104,23 +114,24 @@ static bool get_available_sizes(VideoDevice *device,
fmt_enum.index = index; fmt_enum.index = index;
fmt_enum.pixel_format = V4L2_PIX_FMT_YUYV; fmt_enum.pixel_format = V4L2_PIX_FMT_YUYV;
device->width = 0; video_capture->width = 0;
device->height = 0; video_capture->height = 0;
while (ioctl(device->fd, VIDIOC_ENUM_FRAMESIZES, &fmt_enum) == 0) { while (ioctl(video_capture->fd, VIDIOC_ENUM_FRAMESIZES, &fmt_enum) == 0) {
if (fmt_enum.type == V4L2_FRMSIZE_TYPE_DISCRETE) { if (fmt_enum.type == V4L2_FRMSIZE_TYPE_DISCRETE) {
log_trace("(%s) %d: %dx%d", device->name, index, fmt_enum.discrete.width, log_trace("(%s) %d: %dx%d", video_capture->name, index,
fmt_enum.discrete.height); fmt_enum.discrete.width, fmt_enum.discrete.height);
if (fmt_enum.discrete.height == preferred_height) { if (fmt_enum.discrete.height == preferred_height) {
device->height = preferred_height; video_capture->height = preferred_height;
if (device->width == 0 || device->width < fmt_enum.discrete.width) { if (video_capture->width == 0 ||
device->width = fmt_enum.discrete.width; video_capture->width < fmt_enum.discrete.width) {
video_capture->width = fmt_enum.discrete.width;
} }
} else if (fmt_enum.discrete.height < preferred_height && } else if (fmt_enum.discrete.height < preferred_height &&
fmt_enum.discrete.height > device->height) { fmt_enum.discrete.height > video_capture->height) {
device->height = fmt_enum.discrete.height; video_capture->height = fmt_enum.discrete.height;
device->width = fmt_enum.discrete.width; video_capture->width = fmt_enum.discrete.width;
} }
} }
@@ -129,104 +140,104 @@ static bool get_available_sizes(VideoDevice *device,
fmt_enum.pixel_format = V4L2_PIX_FMT_YUYV; fmt_enum.pixel_format = V4L2_PIX_FMT_YUYV;
} }
if (device->height == 0) { if (video_capture->height == 0) {
device->error = true; video_capture->error = true;
return false; return false;
} }
return true; return true;
} }
static bool set_device_format(VideoDevice *device) { static bool set_format(VideoCapture *video_capture) {
struct v4l2_format fmt; struct v4l2_format fmt;
device->output = false; video_capture->output = false;
memset(&fmt, 0, sizeof(fmt)); memset(&fmt, 0, sizeof(fmt));
fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
fmt.fmt.pix.width = device->width; fmt.fmt.pix.width = video_capture->width;
fmt.fmt.pix.height = device->height; fmt.fmt.pix.height = video_capture->height;
fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV; fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
fmt.fmt.pix.field = V4L2_FIELD_INTERLACED; fmt.fmt.pix.field = V4L2_FIELD_INTERLACED;
if (ioctl(device->fd, VIDIOC_S_FMT, &fmt) == -1) { if (ioctl(video_capture->fd, VIDIOC_S_FMT, &fmt) == -1) {
fmt.type = V4L2_BUF_TYPE_VIDEO_OUTPUT; fmt.type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
device->output = true; video_capture->output = true;
if (ioctl(device->fd, VIDIOC_S_FMT, &fmt) == -1) { if (ioctl(video_capture->fd, VIDIOC_S_FMT, &fmt) == -1) {
ioctl_error(device, "VIDIOC_S_FMT", ioctl_error(video_capture, "VIDIOC_S_FMT",
"Requested buffer type not supported"); "Requested buffer type not supported");
return false; return false;
} }
} }
device->width = fmt.fmt.pix.width; video_capture->width = fmt.fmt.pix.width;
device->height = fmt.fmt.pix.height; video_capture->height = fmt.fmt.pix.height;
device->pixelformat = fmt.fmt.pix.pixelformat; video_capture->pixelformat = fmt.fmt.pix.pixelformat;
device->bytesperline = fmt.fmt.pix.bytesperline; video_capture->bytesperline = fmt.fmt.pix.bytesperline;
log_info("(%s) Format fourcc: %c%c%c%c", device->name, log_info("(%s) Format fourcc: %c%c%c%c", video_capture->name,
fmt.fmt.pix.pixelformat, fmt.fmt.pix.pixelformat >> 8, fmt.fmt.pix.pixelformat, fmt.fmt.pix.pixelformat >> 8,
fmt.fmt.pix.pixelformat >> 16, fmt.fmt.pix.pixelformat >> 24); fmt.fmt.pix.pixelformat >> 16, fmt.fmt.pix.pixelformat >> 24);
log_info("(%s) Resolution: %dx%d", device->name, fmt.fmt.pix.width, log_info("(%s) Resolution: %dx%d", video_capture->name, fmt.fmt.pix.width,
fmt.fmt.pix.height); fmt.fmt.pix.height);
return true; return true;
} }
static bool request_buffers(VideoDevice *device) { static bool request_buffers(VideoCapture *video_capture) {
struct v4l2_requestbuffers reqbuf; struct v4l2_requestbuffers reqbuf;
memset(&reqbuf, 0, sizeof(reqbuf)); memset(&reqbuf, 0, sizeof(reqbuf));
reqbuf.type = reqbuf.type = video_capture->output ? V4L2_BUF_TYPE_VIDEO_OUTPUT
device->output ? V4L2_BUF_TYPE_VIDEO_OUTPUT : V4L2_BUF_TYPE_VIDEO_CAPTURE; : V4L2_BUF_TYPE_VIDEO_CAPTURE;
reqbuf.memory = V4L2_MEMORY_MMAP; reqbuf.memory = V4L2_MEMORY_MMAP;
reqbuf.count = 1; reqbuf.count = 1;
if (ioctl(device->fd, VIDIOC_REQBUFS, &reqbuf) == -1) { if (ioctl(video_capture->fd, VIDIOC_REQBUFS, &reqbuf) == -1) {
ioctl_error(device, "VIDIOC_REQBUFS", ioctl_error(video_capture, "VIDIOC_REQBUFS",
"Buffer type or I/O method not supported"); "Buffer type or I/O method not supported");
return false; return false;
} }
log_info("(%s) V4L2 Buffer Count: %d", device->name, reqbuf.count); log_info("(%s) V4L2 Buffer Count: %d", video_capture->name, reqbuf.count);
return true; return true;
} }
static bool export_buffer(VideoDevice *device) { static bool export_buffer(VideoCapture *video_capture) {
struct v4l2_exportbuffer expbuf; struct v4l2_exportbuffer expbuf;
device->exp_fd = -1; video_capture->exp_fd = -1;
memset(&expbuf, 0, sizeof(expbuf)); memset(&expbuf, 0, sizeof(expbuf));
expbuf.type = expbuf.type = video_capture->output ? V4L2_BUF_TYPE_VIDEO_OUTPUT
device->output ? V4L2_BUF_TYPE_VIDEO_OUTPUT : V4L2_BUF_TYPE_VIDEO_CAPTURE; : V4L2_BUF_TYPE_VIDEO_CAPTURE;
expbuf.index = 0; expbuf.index = 0;
expbuf.flags = O_RDONLY; expbuf.flags = O_RDONLY;
if (ioctl(device->fd, VIDIOC_EXPBUF, &expbuf) == -1) { if (ioctl(video_capture->fd, VIDIOC_EXPBUF, &expbuf) == -1) {
ioctl_error( ioctl_error(
device, "VIDIOC_EXPBUF", video_capture, "VIDIOC_EXPBUF",
"A queue is not in MMAP mode or DMABUF exporting is not supported"); "A queue is not in MMAP mode or DMABUF exporting is not supported");
return false; return false;
} }
device->exp_fd = expbuf.fd; video_capture->exp_fd = expbuf.fd;
return true; return true;
} }
static const enum v4l2_buf_type buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE; static const enum v4l2_buf_type buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
static bool open_stream(VideoDevice *device) { static bool open_stream(VideoCapture *video_capture) {
if (ioctl(device->fd, VIDIOC_STREAMON, &buf_type) == -1) { if (ioctl(video_capture->fd, VIDIOC_STREAMON, &buf_type) == -1) {
ioctl_error( ioctl_error(
device, "VIDIOC_STREAMON", video_capture, "VIDIOC_STREAMON",
"Buffer type not supported or no buffer allocated or enqueued yet"); "Buffer type not supported or no buffer allocated or enqueued yet");
return false; return false;
} }
@@ -234,69 +245,69 @@ static bool open_stream(VideoDevice *device) {
return true; return true;
} }
static void create_image_buffer(VideoDevice *device) { static void create_image_buffer(VideoCapture *video_capture) {
memset(&device->buf, 0, sizeof(device->buf)); memset(&video_capture->buf, 0, sizeof(video_capture->buf));
device->buf.type = video_capture->buf.type = video_capture->output ? V4L2_BUF_TYPE_VIDEO_OUTPUT
device->output ? V4L2_BUF_TYPE_VIDEO_OUTPUT : V4L2_BUF_TYPE_VIDEO_CAPTURE; : V4L2_BUF_TYPE_VIDEO_CAPTURE;
device->buf.memory = V4L2_MEMORY_MMAP; video_capture->buf.memory = V4L2_MEMORY_MMAP;
device->buf.index = 0; video_capture->buf.index = 0;
ioctl(device->fd, VIDIOC_QBUF, &device->buf); ioctl(video_capture->fd, VIDIOC_QBUF, &video_capture->buf);
} }
static void close_stream(VideoDevice device) { static void close_stream(VideoCapture video_capture) {
ioctl(device.fd, VIDIOC_STREAMOFF, &buf_type); ioctl(video_capture.fd, VIDIOC_STREAMOFF, &buf_type);
} }
VideoDevice video_init(char *name, unsigned int preferred_height) { VideoCapture video_init(char *name, unsigned int preferred_height) {
VideoDevice device; VideoCapture video_capture;
device = open_device(name); video_capture = open_device(name);
if (device.error) { if (video_capture.error) {
return device; return video_capture;
} }
if (!check_device_caps(&device)) { if (!check_caps(&video_capture)) {
return device; return video_capture;
} }
if (!get_available_sizes(&device, preferred_height)) { if (!get_available_sizes(&video_capture, preferred_height)) {
return device; return video_capture;
} }
if (!set_device_format(&device)) { if (!set_format(&video_capture)) {
return device; return video_capture;
} }
if (!request_buffers(&device)) { if (!request_buffers(&video_capture)) {
return device; return video_capture;
} }
if (!export_buffer(&device)) { if (!export_buffer(&video_capture)) {
return device; return video_capture;
} }
if (!open_stream(&device)) { if (!open_stream(&video_capture)) {
return device; return video_capture;
} }
create_image_buffer(&device); create_image_buffer(&video_capture);
return device; return video_capture;
} }
static bool read_video(VideoDevice *device) { static bool read_video(VideoCapture *video_capture) {
if (ioctl(device->fd, VIDIOC_DQBUF, &device->buf) == -1) { if (ioctl(video_capture->fd, VIDIOC_DQBUF, &video_capture->buf) == -1) {
ioctl_error(device, "VIDIOC_DQBUF", ioctl_error(video_capture, "VIDIOC_DQBUF",
"buffer type not supported or no buffer allocated or the index " "buffer type not supported or no buffer allocated or the index "
"is out of bounds"); "is out of bounds");
return false; return false;
} }
if (ioctl(device->fd, VIDIOC_QBUF, &device->buf) == -1) { if (ioctl(video_capture->fd, VIDIOC_QBUF, &video_capture->buf) == -1) {
ioctl_error(device, "VIDIOC_QBUF", ioctl_error(video_capture, "VIDIOC_QBUF",
"buffer type not supported or no buffer allocated or the index " "buffer type not supported or no buffer allocated or the index "
"is out of bounds"); "is out of bounds");
return false; return false;
@@ -305,7 +316,7 @@ static bool read_video(VideoDevice *device) {
return true; return true;
} }
void video_background_read(VideoDevice *device, bool *stop) { void video_background_read(VideoCapture *video_capture, bool *stop) {
pid_t pid; pid_t pid;
pid = fork(); pid = fork();
if (pid < 0) { if (pid < 0) {
@@ -315,23 +326,25 @@ void video_background_read(VideoDevice *device, bool *stop) {
if (pid == 0) { if (pid == 0) {
return; return;
} }
log_info("%s background acquisition started (pid: %d)", device->name, pid); log_info("%s background acquisition started (pid: %d)", video_capture->name,
while (!*stop && read_video(device)) { pid);
while (!*stop && read_video(video_capture)) {
// repeat infinitely // repeat infinitely
} }
log_info("%s background acquisition stopped (pid: %d)", device->name, pid); log_info("%s background acquisition stopped (pid: %d)", video_capture->name,
pid);
window_terminate(); window_terminate();
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
} }
void video_free(VideoDevice device) { void video_free(VideoCapture video_capture) {
if (!device.error) { if (!video_capture.error) {
close_stream(device); close_stream(video_capture);
} }
if (device.exp_fd != -1) { if (video_capture.exp_fd != -1) {
close(device.exp_fd); close(video_capture.exp_fd);
} }
if (device.fd != -1) { if (video_capture.fd != -1) {
close(device.fd); close(video_capture.fd);
} }
} }
+3 -3
View File
@@ -3,10 +3,10 @@
#ifndef VIDEO_H #ifndef VIDEO_H
#define VIDEO_H #define VIDEO_H
VideoDevice video_init(char *name, unsigned int preferred_height); VideoCapture video_init(char *name, unsigned int preferred_height);
void video_background_read(VideoDevice *device, bool *stop); void video_background_read(VideoCapture *video_capture, bool *stop);
void video_free(VideoDevice device); void video_free(VideoCapture video_capture);
#endif /* VIDEO_H */ #endif /* VIDEO_H */