Revert "Simplification no longer works"

This reverts commit 36de2488e2.
This commit is contained in:
Thomas Farstrike
2025-05-14 08:44:23 +02:00
parent 36de2488e2
commit 0e314c2f32
+23 -15
View File
@@ -23,7 +23,9 @@
#define CAPTURE_HEIGHT 480
#define OUTPUT_WIDTH 240 // Resize to 240x240
#define OUTPUT_HEIGHT 240
#define NUM_BUFFERS 2 // Keep 2 buffers, stable for continuous captures
#define NUM_BUFFERS 2 // Revert to 2 buffers, as it achieved 2 captures
#define QUEUE_RETRIES 10 // Increase retries for robustness
#define QUEUE_RETRY_DELAY_US 200000 // 200ms delay between retries
// Webcam object type
typedef struct _webcam_obj_t {
@@ -96,28 +98,34 @@ static void webcam_reset_streaming(webcam_obj_t *self) {
static mp_obj_t webcam_capture_grayscale(mp_obj_t self_in) {
webcam_obj_t *self = MP_OBJ_TO_PTR(self_in);
// Try to queue a buffer
// Try to queue a buffer with retries
struct v4l2_buffer buf = {0};
bool queued = false;
for (size_t i = 0; i < self->num_buffers; i++) {
memset(&buf, 0, sizeof(buf));
buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
buf.memory = V4L2_MEMORY_MMAP;
buf.index = i;
for (int attempt = 0; attempt < QUEUE_RETRIES && !queued; attempt++) {
for (size_t i = 0; i < self->num_buffers; i++) {
memset(&buf, 0, sizeof(buf));
buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
buf.memory = V4L2_MEMORY_MMAP;
buf.index = i;
WEBCAM_DEBUG_PRINT("webcam: Attempting to queue buffer (index=%zu)\n", i);
if (ioctl(self->fd, VIDIOC_QBUF, &buf) == 0) {
WEBCAM_DEBUG_PRINT("webcam: Successfully queued buffer (index=%zu)\n", i);
queued = true;
break;
WEBCAM_DEBUG_PRINT("webcam: Attempting to queue buffer (index=%zu, attempt=%d)\n", i, attempt + 1);
if (ioctl(self->fd, VIDIOC_QBUF, &buf) == 0) {
WEBCAM_DEBUG_PRINT("webcam: Successfully queued buffer (index=%zu)\n", i);
queued = true;
break;
}
WEBCAM_DEBUG_PRINT("webcam: Failed to queue buffer (index=%zu, errno=%d)\n", i, errno);
}
if (!queued && attempt < QUEUE_RETRIES - 1) {
WEBCAM_DEBUG_PRINT("webcam: No buffers available, retrying after delay\n");
usleep(QUEUE_RETRY_DELAY_US); // Wait 200ms
}
WEBCAM_DEBUG_PRINT("webcam: Failed to queue buffer (index=%zu, errno=%d)\n", i, errno);
}
if (!queued) {
WEBCAM_DEBUG_PRINT("webcam: No buffers available, resetting streaming\n");
WEBCAM_DEBUG_PRINT("webcam: No buffers available after %d retries, resetting streaming\n", QUEUE_RETRIES);
webcam_reset_streaming(self);
// Try queuing again after reset
// Retry queuing one more time after reset
for (size_t i = 0; i < self->num_buffers; i++) {
memset(&buf, 0, sizeof(buf));
buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;