Skip to content

Commit

Permalink
ensure buffer state transitions atomic
Browse files Browse the repository at this point in the history
  • Loading branch information
deanlee committed Jan 25, 2025
1 parent 93a8a18 commit 9034a8f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
18 changes: 11 additions & 7 deletions system/loggerd/encoder/v4l_encoder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ static void checked_ioctl(int fd, unsigned long request, void *argp) {
}
}

static void dequeue_buffer(int fd, v4l2_buf_type buf_type, unsigned int *index=NULL, unsigned int *bytesused=NULL, unsigned int *flags=NULL, struct timeval *timestamp=NULL) {
void V4LEncoder::dequeue_buffer(v4l2_buf_type buf_type, uint32_t *index, uint32_t *bytesused, uint32_t *flags, struct timeval *timestamp) {
std::lock_guard lock(mutex);

v4l2_plane plane = {0};
v4l2_buffer v4l_buf = {
.type = buf_type,
Expand All @@ -49,7 +51,9 @@ static void dequeue_buffer(int fd, v4l2_buf_type buf_type, unsigned int *index=N
assert(v4l_buf.m.planes[0].data_offset == 0);
}

static void queue_buffer(int fd, v4l2_buf_type buf_type, unsigned int index, VisionBuf *buf, struct timeval timestamp={}) {
void V4LEncoder::queue_buffer(v4l2_buf_type buf_type, uint32_t index, VisionBuf *buf, struct timeval timestamp) {
std::lock_guard lock(mutex);

v4l2_plane plane = {
.length = (unsigned int)buf->len,
.m = { .userptr = (unsigned long)buf->addr, },
Expand Down Expand Up @@ -117,7 +121,7 @@ void V4LEncoder::dequeue_handler(V4LEncoder *e) {
if (pfd.revents & POLLIN) {
unsigned int bytesused, flags, index;
struct timeval timestamp;
dequeue_buffer(e->fd, V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE, &index, &bytesused, &flags, &timestamp);
e->dequeue_buffer(V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE, &index, &bytesused, &flags, &timestamp);
e->buf_out[index].sync(VISIONBUF_SYNC_FROM_DEVICE);
uint8_t *buf = (uint8_t*)e->buf_out[index].addr;
int64_t ts = timestamp.tv_sec * 1000000 + timestamp.tv_usec;
Expand All @@ -142,12 +146,12 @@ void V4LEncoder::dequeue_handler(V4LEncoder *e) {
}

// requeue the buffer
queue_buffer(e->fd, V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE, index, &e->buf_out[index]);
e->queue_buffer(V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE, index, &e->buf_out[index]);
}

if (pfd.revents & POLLOUT) {
unsigned int index;
dequeue_buffer(e->fd, V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE, &index);
e->dequeue_buffer(V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE, &index);
e->free_buf_in.push(index);
}
}
Expand Down Expand Up @@ -267,7 +271,7 @@ V4LEncoder::V4LEncoder(const EncoderInfo &encoder_info, int in_width, int in_hei
// queue up output buffers
for (unsigned int i = 0; i < BUF_OUT_COUNT; i++) {
buf_out[i].allocate(fmt_out.fmt.pix_mp.plane_fmt[0].sizeimage);
queue_buffer(fd, V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE, i, &buf_out[i]);
queue_buffer(V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE, i, &buf_out[i]);
}
// queue up input buffers
for (unsigned int i = 0; i < BUF_IN_COUNT; i++) {
Expand All @@ -293,7 +297,7 @@ int V4LEncoder::encode_frame(VisionBuf* buf, VisionIpcBufExtra *extra) {
// push buffer
extras.push(*extra);
//buf->sync(VISIONBUF_SYNC_TO_DEVICE);
queue_buffer(fd, V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE, buffer_in, buf, timestamp);
queue_buffer(V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE, buffer_in, buf, timestamp);

return this->counter++;
}
Expand Down
6 changes: 6 additions & 0 deletions system/loggerd/encoder/v4l_encoder.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#pragma once

#include <mutex>

#include "common/queue.h"
#include "system/loggerd/encoder/encoder.h"

Expand All @@ -14,6 +16,10 @@ class V4LEncoder : public VideoEncoder {
void encoder_open(const char* path);
void encoder_close();
private:
void queue_buffer(v4l2_buf_type buf_type, uint32_t index, VisionBuf *buf, struct timeval timestamp={});
void dequeue_buffer(v4l2_buf_type buf_type, uint32_t *index = nullptr, uint32_t *bytesused = nullptr,
uint32_t *flags = nullptr, struct timeval *timestamp = nullptr);
std::mutex mutex;
int fd;

bool is_open = false;
Expand Down

0 comments on commit 9034a8f

Please sign in to comment.