Skip to content

Commit 78aa4af

Browse files
webcam: 5 works
1 parent d57aa55 commit 78aa4af

File tree

1 file changed

+24
-16
lines changed

1 file changed

+24
-16
lines changed

c_mpos/src/webcam.c

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@
2323
#define CAPTURE_HEIGHT 480
2424
#define OUTPUT_WIDTH 240 // Resize to 240x240
2525
#define OUTPUT_HEIGHT 240
26-
#define NUM_BUFFERS 2 // Use 2 buffers, as it worked for two captures
26+
#define NUM_BUFFERS 5 // Use 2 buffers, as it worked for two captures
27+
#define QUEUE_RETRIES 5 // Number of retry attempts for queueing
28+
#define QUEUE_RETRY_DELAY_US 100000 // 50ms delay between retries
2729

2830
// Webcam object type
2931
typedef struct _webcam_obj_t {
@@ -72,27 +74,33 @@ static void resize_640x480_to_240x240(uint8_t *src, uint8_t *dst) {
7274
static mp_obj_t webcam_capture_grayscale(mp_obj_t self_in) {
7375
webcam_obj_t *self = MP_OBJ_TO_PTR(self_in);
7476

75-
// Try to queue a buffer
77+
// Try to queue a buffer with retries
7678
struct v4l2_buffer buf = {0};
7779
bool queued = false;
78-
for (size_t i = 0; i < self->num_buffers; i++) {
79-
memset(&buf, 0, sizeof(buf));
80-
buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
81-
buf.memory = V4L2_MEMORY_MMAP;
82-
buf.index = i;
83-
84-
WEBCAM_DEBUG_PRINT("webcam: Attempting to queue buffer (index=%zu)\n", i);
85-
if (ioctl(self->fd, VIDIOC_QBUF, &buf) == 0) {
86-
WEBCAM_DEBUG_PRINT("webcam: Successfully queued buffer (index=%zu)\n", i);
87-
queued = true;
88-
break;
80+
for (int attempt = 0; attempt < QUEUE_RETRIES && !queued; attempt++) {
81+
for (size_t i = 0; i < self->num_buffers; i++) {
82+
memset(&buf, 0, sizeof(buf));
83+
buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
84+
buf.memory = V4L2_MEMORY_MMAP;
85+
buf.index = i;
86+
87+
WEBCAM_DEBUG_PRINT("webcam: Attempting to queue buffer (index=%zu, attempt=%d)\n", i, attempt + 1);
88+
if (ioctl(self->fd, VIDIOC_QBUF, &buf) == 0) {
89+
WEBCAM_DEBUG_PRINT("webcam: Successfully queued buffer (index=%zu)\n", i);
90+
queued = true;
91+
break;
92+
}
93+
WEBCAM_DEBUG_PRINT("webcam: Failed to queue buffer (index=%zu, errno=%d)\n", i, errno);
94+
}
95+
if (!queued && attempt < QUEUE_RETRIES - 1) {
96+
WEBCAM_DEBUG_PRINT("webcam: No buffers available, retrying after delay\n");
97+
usleep(QUEUE_RETRY_DELAY_US); // Wait 50ms before retrying
8998
}
90-
WEBCAM_DEBUG_PRINT("webcam: Failed to queue buffer (index=%zu, errno=%d)\n", i, errno);
9199
}
92100

93101
if (!queued) {
94-
WEBCAM_DEBUG_PRINT("webcam: No buffers available for queuing\n");
95-
mp_raise_OSError(EAGAIN); // Try again later
102+
WEBCAM_DEBUG_PRINT("webcam: No buffers available after %d retries\n", QUEUE_RETRIES);
103+
mp_raise_OSError(EAGAIN);
96104
}
97105

98106
// Start streaming if not already started

0 commit comments

Comments
 (0)