Fix VIDIOC_S_FMT EBUSY error on video stream restart

V4L2 buffers were not explicitly freed before closing the device, causing VIDIOC_S_FMT to fail with EBUSY on restart. Added  VIDIOC_REQBUFS(count=0) call in cleanup to free buffer queue synchronously before close().

This ensures subsequent opens can set format immediately without EBUSY errors, eliminating 100ms+ retry delays.
This commit is contained in:
Adam Shiervani 2025-10-17 11:14:53 +00:00 committed by Siyuan
parent 38a987469f
commit 0f4e073a45
1 changed files with 20 additions and 8 deletions

View File

@ -606,6 +606,18 @@ void *run_video_stream(void *arg)
log_error("VIDIOC_STREAMOFF failed: %s", strerror(errno)); log_error("VIDIOC_STREAMOFF failed: %s", strerror(errno));
} }
// Explicitly free V4L2 buffer queue
struct v4l2_requestbuffers req_free;
memset(&req_free, 0, sizeof(req_free));
req_free.count = 0;
req_free.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
req_free.memory = V4L2_MEMORY_DMABUF;
if (ioctl(video_dev_fd, VIDIOC_REQBUFS, &req_free) < 0)
{
log_error("Failed to free V4L2 buffers: %s", strerror(errno));
}
venc_stop(); venc_stop();
for (int i = 0; i < input_buffer_count; i++) for (int i = 0; i < input_buffer_count; i++)
@ -619,7 +631,7 @@ void *run_video_stream(void *arg)
log_info("closing video capture device %s", VIDEO_DEV); log_info("closing video capture device %s", VIDEO_DEV);
close(video_dev_fd); close(video_dev_fd);
} }
log_info("video stream thread exiting"); log_info("video stream thread exiting");
streaming_stopped = true; streaming_stopped = true;
@ -648,7 +660,7 @@ void video_shutdown()
RK_MPI_MB_DestroyPool(memPool); RK_MPI_MB_DestroyPool(memPool);
} }
log_info("Destroyed memory pool"); log_info("Destroyed memory pool");
pthread_mutex_destroy(&streaming_mutex); pthread_mutex_destroy(&streaming_mutex);
log_info("Destroyed streaming mutex"); log_info("Destroyed streaming mutex");
} }
@ -665,14 +677,14 @@ void video_start_streaming()
log_warn("video streaming already started"); log_warn("video streaming already started");
return; return;
} }
pthread_t *new_thread = malloc(sizeof(pthread_t)); pthread_t *new_thread = malloc(sizeof(pthread_t));
if (new_thread == NULL) if (new_thread == NULL)
{ {
log_error("Failed to allocate memory for streaming thread"); log_error("Failed to allocate memory for streaming thread");
return; return;
} }
set_streaming_flag(true); set_streaming_flag(true);
int result = pthread_create(new_thread, NULL, run_video_stream, NULL); int result = pthread_create(new_thread, NULL, run_video_stream, NULL);
if (result != 0) if (result != 0)
@ -682,7 +694,7 @@ void video_start_streaming()
free(new_thread); free(new_thread);
return; return;
} }
// Only set streaming_thread after successful creation // Only set streaming_thread after successful creation
streaming_thread = new_thread; streaming_thread = new_thread;
} }
@ -693,7 +705,7 @@ void video_stop_streaming()
log_info("video streaming already stopped"); log_info("video streaming already stopped");
return; return;
} }
log_info("stopping video streaming"); log_info("stopping video streaming");
set_streaming_flag(false); set_streaming_flag(false);
@ -711,7 +723,7 @@ void video_stop_streaming()
free(streaming_thread); free(streaming_thread);
streaming_thread = NULL; streaming_thread = NULL;
log_info("video streaming stopped"); log_info("video streaming stopped");
} }
void video_restart_streaming() void video_restart_streaming()
@ -818,4 +830,4 @@ void video_set_quality_factor(float factor)
float video_get_quality_factor() { float video_get_quality_factor() {
return quality_factor; return quality_factor;
} }