refactor: specify number of video input buffers
Clang Lint CI / lint-no-video (push) Successful in 1m5s
Clang Build CI / run-no-video (push) Successful in 1m5s
Clang Build CI / run-video (push) Successful in 1m5s
Clang Build CI / build-release (push) Successful in 2m24s
Clang Lint CI / lint-video (push) Successful in 2m22s

This commit is contained in:
2026-05-15 00:09:51 +02:00
parent 06d175c4fd
commit 37a492d00d
9 changed files with 172 additions and 150 deletions
+38 -41
View File
@@ -192,14 +192,15 @@ static bool set_format(VideoCapture *video_capture) {
return true;
}
static bool request_buffers(VideoCapture *video_capture) {
static bool request_buffers(VideoCapture *video_capture,
unsigned int buffer_count) {
struct v4l2_requestbuffers reqbuf;
memset(&reqbuf, 0, sizeof(reqbuf));
reqbuf.type = buf_type;
reqbuf.memory = V4L2_MEMORY_MMAP;
reqbuf.count = 2; // TODO buffer array with count from parameters
reqbuf.count = buffer_count;
if (ioctl(video_capture->fd, VIDIOC_REQBUFS, &reqbuf) == -1) {
ioctl_error(video_capture, "VIDIOC_REQBUFS",
@@ -209,7 +210,7 @@ static bool request_buffers(VideoCapture *video_capture) {
log_info("(%s) V4L2 Buffer Count: %d", video_capture->name, reqbuf.count);
video_capture->with_swap = reqbuf.count > 1;
video_capture->buf_count = reqbuf.count;
return true;
}
@@ -239,16 +240,15 @@ static bool export_buffer(VideoCapture *video_capture, int *fd,
}
static bool export_buffers(VideoCapture *video_capture) {
bool result;
unsigned int i;
result = export_buffer(video_capture, &video_capture->exp_fd, 0);
if (result && video_capture->with_swap) {
result =
result && export_buffer(video_capture, &video_capture->exp_fd_swap, 1);
for (i = 0; i < video_capture->buf_count; i++) {
if (!export_buffer(video_capture, &video_capture->exp_fd[i], i)) {
return false;
}
}
return result;
return true;
}
static bool open_stream(VideoCapture *video_capture) {
@@ -274,10 +274,10 @@ static void create_image_buffer(const VideoCapture *video_capture,
}
static void create_image_buffers(VideoCapture *video_capture) {
create_image_buffer(video_capture, &video_capture->buf, 0);
unsigned int i;
if (video_capture->with_swap) {
create_image_buffer(video_capture, &video_capture->buf_swap, 1);
for (i = 0; i < video_capture->buf_count; i++) {
create_image_buffer(video_capture, &video_capture->buf[i], i);
}
}
@@ -286,31 +286,26 @@ static void close_stream(const VideoCapture *video_capture) {
}
static unsigned int read_video(VideoCapture *video_capture) {
unsigned int result;
struct v4l2_capability cap;
result = 0;
if (ioctl(video_capture->fd, VIDIOC_DQBUF,
&video_capture->buf[video_capture->buf_index]) != -1) {
ioctl(video_capture->fd, VIDIOC_QBUF,
&video_capture->buf[video_capture->buf_index]);
video_capture->buf_index =
(video_capture->buf_index + 1) % video_capture->buf_count;
return true;
}
if ((video_capture->swap || !video_capture->with_swap) &&
ioctl(video_capture->fd, VIDIOC_DQBUF, &video_capture->buf) != -1) {
ioctl(video_capture->fd, VIDIOC_QBUF, &video_capture->buf);
result = 1;
} else if (!video_capture->swap && video_capture->with_swap &&
ioctl(video_capture->fd, VIDIOC_DQBUF, &video_capture->buf_swap) !=
-1) {
ioctl(video_capture->fd, VIDIOC_QBUF, &video_capture->buf_swap);
result = 2;
} else if (ioctl(video_capture->fd, VIDIOC_QUERYCAP, &cap) == -1) {
if (ioctl(video_capture->fd, VIDIOC_QUERYCAP, &cap) == -1) {
video_capture->error = true;
}
return result;
return false;
}
void video_init(VideoCapture *video_capture, const char *name,
unsigned int preferred_height) {
unsigned int preferred_height, unsigned int buffer_count) {
open_device(video_capture, name);
if (video_capture->error) {
@@ -329,7 +324,7 @@ void video_init(VideoCapture *video_capture, const char *name,
return;
}
if (!request_buffers(video_capture)) {
if (!request_buffers(video_capture, buffer_count)) {
return;
}
@@ -352,14 +347,15 @@ void *video_background_read(void *args) {
bool trace_fps = process_args->trace_fps;
Timer timer;
double fps;
unsigned int video_result;
bool result;
log_info("(%s) background acquisition started", video_capture->name);
timer_init(&timer, 30);
while (!context->stop && !video_capture->error) {
video_result = read_video(video_capture);
if (video_result > 0 && timer_inc(&timer)) {
result = read_video(video_capture);
if (result && timer_inc(&timer)) {
fps = timer_reset(&timer);
context->input_fps[input_index] = (unsigned int)round(fps);
@@ -367,9 +363,9 @@ void *video_background_read(void *args) {
log_trace("(%s) %.2ffps", video_capture->name, fps);
}
}
if (video_result > 0) {
context->input_swap[input_index] = video_capture->swap =
video_result == 2;
if (result) {
context->input_index[input_index] = video_capture->buf_index;
}
}
if (context->stop) {
@@ -385,13 +381,14 @@ void *video_background_read(void *args) {
}
void video_free(const VideoCapture *video_capture) {
unsigned int i;
close_stream(video_capture);
if (video_capture->exp_fd != -1) {
close(video_capture->exp_fd);
}
if (video_capture->exp_fd_swap != -1) {
close(video_capture->exp_fd_swap);
for (i = 0; i < video_capture->buf_count; i++) {
close(video_capture->exp_fd[i]);
}
if (video_capture->fd != -1) {
close(video_capture->fd);
}