feat(video): slightly faster video acquisition with O_NONBLOCK

This commit is contained in:
2025-11-23 12:58:49 +01:00
parent 01266e7823
commit 6b4630f255
3 changed files with 19 additions and 29 deletions
+1 -2
View File
@@ -136,5 +136,4 @@ make -f Makefile.dev release-arch
- [ ] Play from record text file
- [ ] Fixes
- [ ] Try to write NanoKontrol config
- [ ] Investigate video device fps loss (bad unregister ?)
- explore libv4l directly [github](https://github.com/philips/libv4l) (with `-lv4l2`)
- [ ] Investigate video device fps loss (bad unregister ?)
+8 -10
View File
@@ -57,7 +57,7 @@ static void compute_fps(bool trace_fps) {
}
}
static void init_context(const Parameters *params, unsigned int in_count) {
static void init_context(const Parameters *params) {
state_init(context, &project.state_config, params->demo, params->auto_random,
params->auto_random_cycle, params->base_tempo, params->load_state);
@@ -66,14 +66,6 @@ static void init_context(const Parameters *params, unsigned int in_count) {
memset(context->input_resolutions, 0, sizeof(context->input_resolutions));
memset(context->input_formats, 0, sizeof(context->input_formats));
memset(context->input_fps, 0, sizeof(context->input_fps));
for (unsigned int i = 0; i < in_count; i++) {
if (!inputs.values[i].error) {
context->input_resolutions[i][0] = inputs.values[i].width;
context->input_resolutions[i][1] = inputs.values[i].height;
context->input_formats[i] = inputs.values[i].pixelformat;
}
}
}
static void free_context() { shared_close_context(context); }
@@ -88,6 +80,12 @@ static void init_inputs(const StringArray *video_in, unsigned int video_size) {
for (unsigned int i = 0; i < video_in->length; i++) {
video_init(&inputs.values[i], video_in->values[i], video_size);
if (!inputs.values[i].error) {
context->input_resolutions[i][0] = inputs.values[i].width;
context->input_resolutions[i][1] = inputs.values[i].height;
context->input_formats[i] = inputs.values[i].pixelformat;
}
}
}
@@ -178,7 +176,7 @@ void forge_run(const Parameters *params) {
return;
}
init_context(params, project.in_count);
init_context(params);
#ifdef VIDEO_IN
init_inputs(&params->video_in, params->video_size);
+10 -17
View File
@@ -74,7 +74,7 @@ static void open_device(VideoCapture *video_capture, const char *name) {
video_capture->fd = -1;
video_capture->exp_fd = -1;
video_capture->fd = open(name, O_RDWR);
video_capture->fd = open(name, O_RDWR | O_NONBLOCK);
if (video_capture->fd == -1) {
log_warn("(%s) Cannot open device", name);
video_capture->error = true;
@@ -265,6 +265,8 @@ static void create_image_buffer(VideoCapture *video_capture) {
video_capture->buf.memory = V4L2_MEMORY_MMAP;
video_capture->buf.index = 0;
ioctl(video_capture->fd, VIDIOC_PREPARE_BUF);
ioctl(video_capture->fd, VIDIOC_QBUF, &video_capture->buf);
}
@@ -273,21 +275,13 @@ static void close_stream(const VideoCapture *video_capture) {
}
static bool read_video(VideoCapture *video_capture) {
if (ioctl(video_capture->fd, VIDIOC_DQBUF, &video_capture->buf) == -1) {
ioctl_error(video_capture, "VIDIOC_DQBUF",
"buffer type not supported or no buffer allocated or the index "
"is out of bounds");
return false;
}
bool result;
if (ioctl(video_capture->fd, VIDIOC_QBUF, &video_capture->buf) == -1) {
ioctl_error(video_capture, "VIDIOC_QBUF",
"buffer type not supported or no buffer allocated or the index "
"is out of bounds");
return false;
}
result = ioctl(video_capture->fd, VIDIOC_DQBUF, &video_capture->buf) != -1;
return true;
ioctl(video_capture->fd, VIDIOC_QBUF, &video_capture->buf);
return result;
}
void video_init(VideoCapture *video_capture, const char *name,
@@ -343,9 +337,8 @@ bool video_background_read(VideoCapture *video_capture, SharedContext *context,
pid);
timer_init(&timer, 30);
while (!context->stop && read_video(video_capture)) {
// repeat infinitely
if (timer_inc(&timer)) {
while (!context->stop) {
if (read_video(video_capture) && timer_inc(&timer)) {
fps = timer_reset(&timer);
context->input_fps[input_index] = (unsigned int)round(fps);