This commit is contained in:
Kamil Trzcinski 2022-04-04 16:37:38 +02:00
parent 61f2122768
commit 35262d1655
5 changed files with 34 additions and 103 deletions

View File

@ -16,6 +16,8 @@ typedef struct buffer_list_s {
bool do_capture;
unsigned fmt_width, fmt_height, fmt_format;
int frames;
} buffer_list_t;
buffer_list_t *buffer_list_open(const char *name, struct device_s *dev, unsigned type, bool do_mmap);
@ -24,8 +26,6 @@ void buffer_list_close(buffer_list_t *buf_list);
int buffer_list_set_format(buffer_list_t *buffer_list, unsigned width, unsigned height, unsigned format);
int buffer_list_request(buffer_list_t *buf_list, int nbufs);
bool buffer_list_wait_pool(buffer_list_t *buf_list, int timeout, int mmap);
int buffer_list_stream(buffer_list_t *buf_list, bool do_on);
buffer_t *buffer_list_dequeue(buffer_list_t *buf_list, int mmap);

View File

@ -112,6 +112,7 @@ buffer_t *_buffer_list_enqueue_dmabuf(buffer_list_t *buf_list, buffer_t *dma_buf
E_XIOCTL(buf_list, buf_list->device->fd, VIDIOC_QBUF, &v4l2_buf, "Can't push DMA buffer");
buf->mmap_source = dma_buf;
buf_list->frames++;
return buf;
error:
return NULL;
@ -164,11 +165,12 @@ buffer_t *buffer_list_dequeue(buffer_list_t *buf_list, int mmap)
buf->used = buf->v4l2_buffer.bytesused;
}
E_LOG_DEBUG(buf_list, "Grabbed mmap buffer=%u, bytes=%d, used=%d",
buf->index, buf->length, buf->used);
E_LOG_DEBUG(buf_list, "Grabbed mmap buffer=%u, bytes=%d, used=%d, frame=%d",
buf->index, buf->length, buf->used, buf_list->frames);
if (buf_list->do_mmap) {
buf->mmap_reflinks = 1;
buf_list->frames++;
} else {
buf->mmap_reflinks = 0;
}
@ -178,37 +180,3 @@ buffer_t *buffer_list_dequeue(buffer_list_t *buf_list, int mmap)
error:
return NULL;
}
bool buffer_list_wait_pool(buffer_list_t *buf_list, int timeout, int mmap) {
struct pollfd fds = {buf_list->device->fd, mmap ? POLLIN : POLLOUT, 0};
if (poll(&fds, 1, timeout) < 0 && errno != EINTR) {
E_LOG_ERROR(buf_list, "Can't poll encoder");
}
E_LOG_DEBUG(buf_list, "Polling encoder %d, %d...", errno, fds.revents);
if (fds.revents & POLLIN) {
return true;
}
if (fds.revents & POLLPRI) {
E_LOG_DEBUG(buf_list, "fd POLLPRI");
}
if (fds.revents & POLLOUT) {
return true;
}
if (fds.revents & POLLERR) {
E_LOG_DEBUG(buf_list, "fd POLLERR");
device_consume_event(buf_list->device);
}
if (fds.revents & POLLHUP) {
E_LOG_DEBUG(buf_list, "fd POLLHUP");
}
if (fds.revents & POLLNVAL) {
E_LOG_DEBUG(buf_list, "fd POLLNVAL");
}
return false;
error:
return false;
}

View File

@ -31,7 +31,7 @@ int _build_fds(link_t *all_links, struct pollfd *fds, link_t **links, buffer_lis
if (!output || !output->output_list || n >= max_n) {
return -EINVAL;
}
if (!output->output_list->do_mmap) {
if (output->output_list->do_mmap) {
continue;
}
@ -59,10 +59,11 @@ int handle_links(link_t *all_links, int timeout)
}
for (int i = 0; i < n; i++) {
buffer_list_t *buf_list = buf_lists[n];
link_t *link = links[n];
buffer_list_t *buf_list = buf_lists[i];
link_t *link = links[i];
if (fds[i].revents & POLLIN) {
E_LOG_DEBUG(buf_list, "POLLIN");
if (buf = buffer_list_dequeue(buf_list, true)) {
for (int j = 0; link->outputs[j]; j++) {
buffer_list_enqueue(link->outputs[j]->output_list, buf);
@ -76,6 +77,7 @@ int handle_links(link_t *all_links, int timeout)
}
}
if (fds[i].revents & POLLOUT) {
E_LOG_DEBUG(buf_list, "POLLOUT");
if (buf = buffer_list_dequeue(buf_list, false)) {
buffer_consumed(buf);
}

View File

@ -3,9 +3,9 @@
#include "v4l2.h"
typedef struct link_s {
device_t *capture; // capture_list
device_t *outputs[10];
void (*on_buffer)(buffer_t *buf);
struct device_s *capture; // capture_list
struct device_s *outputs[10];
void (*on_buffer)(struct buffer_s *buf);
} link_t;
int handle_links(link_t *all_links, int timeout);

79
main.c
View File

@ -63,46 +63,6 @@ int open_jpeg(buffer_list_t *src, const char *jpeg_codec)
return 0;
}
void connect_links(device_t **links) {
buffer_t *buf, *output_buf;
device_t *src = links[0];
if (src->capture_list->do_mmap) {
if (buffer_list_wait_pool(src->capture_list, 100, true)) {
if (buf = buffer_list_dequeue(src->capture_list, true)) {
for (int i = 1; links[i]; i++) {
buffer_list_enqueue(links[i]->output_list, buf);
if (buffer_list_wait_pool(links[i]->output_list, 10, false)) {
// consume dma-bufs
if (output_buf = buffer_list_dequeue(links[i]->output_list, false)) {
buffer_consumed(output_buf);
}
}
}
buffer_consumed(buf);
}
}
} else {
for (int i = 1; links[i]; i++) {
if (buffer_list_wait_pool(links[i]->output_list, 100, true)) {
if (output_buf = buffer_list_dequeue(links[i]->output_list, true)) {
buffer_list_enqueue(src->capture_list, output_buf);
buffer_consumed(output_buf);
}
}
}
// consume dma-bufs
if (buffer_list_wait_pool(src->capture_list, 100, false)) {
if (buf = buffer_list_dequeue(src->capture_list, false)) {
buffer_consumed(output_buf);
}
}
}
}
int main(int argc, char *argv[])
{
if (open_camera("/dev/video0") < 0) {
@ -115,8 +75,6 @@ int main(int argc, char *argv[])
goto error;
}
// return;
if (device_stream(camera, true) < 0) {
goto error;
}
@ -133,31 +91,34 @@ int main(int argc, char *argv[])
goto error;
}
device_t *links[][10] = {
link_t links[] = {
{
camera,
isp_srgb,
NULL
}, {
isp_yuuv,
codec_jpeg,
NULL
}, {
isp_yuuv_low,
NULL
}, {
codec_jpeg,
{isp_srgb},
NULL
},
NULL
{
isp_yuuv,
{codec_jpeg},
NULL
},
{
isp_yuuv_low,
{},
NULL
},
{
codec_jpeg,
{},
NULL
},
{ NULL }
};
while(true) {
for (int i = 0; links[i] && links[i][0]; i++) {
connect_links(links[i]);
}
handle_links(links, 100);
usleep(100*1000);
usleep(10*1000);
}
error: