From e86bedd95a049007f77d03cdf9e027f54b389c4c Mon Sep 17 00:00:00 2001 From: Kamil Trzcinski Date: Thu, 7 Apr 2022 12:57:02 +0200 Subject: [PATCH] Improve tracking --- hw/buffer_list.h | 1 + hw/buffer_queue.c | 2 +- hw/links.c | 27 +++++++++++---------------- hw/links.h | 2 +- 4 files changed, 14 insertions(+), 18 deletions(-) diff --git a/hw/buffer_list.h b/hw/buffer_list.h index 40d1510..5d62746 100644 --- a/hw/buffer_list.h +++ b/hw/buffer_list.h @@ -18,6 +18,7 @@ typedef struct buffer_list_s { unsigned fmt_width, fmt_height, fmt_format, fmt_bytesperline, fmt_interval_us; bool do_timestamps; + uint64_t last_enqueued_us; uint64_t last_dequeued_us; bool streaming; diff --git a/hw/buffer_queue.c b/hw/buffer_queue.c index dea45a2..d74da43 100644 --- a/hw/buffer_queue.c +++ b/hw/buffer_queue.c @@ -58,7 +58,7 @@ bool buffer_consumed(buffer_t *buf, const char *who) E_XIOCTL(buf, buf->buf_list->device->fd, VIDIOC_QBUF, &buf->v4l2_buffer, "Can't queue buffer."); buf->enqueued = true; - buf->enqueue_time_us = get_monotonic_time_us(NULL, NULL); + buf->enqueue_time_us = buf->buf_list->last_enqueued_us = get_monotonic_time_us(NULL, NULL); } pthread_mutex_unlock(&buffer_lock); diff --git a/hw/links.c b/hw/links.c index 631eca4..575408c 100644 --- a/hw/links.c +++ b/hw/links.c @@ -68,20 +68,9 @@ int _build_fds(link_t *all_links, struct pollfd *fds, link_t **links, buffer_lis int count_enqueued = buffer_list_count_enqueued(source); - bool can_dequeue = count_enqueued > 0; - int64_t since_last_us = now_us - source->last_dequeued_us; - if (since_last_us < source->fmt_interval_us) { - can_dequeue = false; - - int64_t since_last_ms = since_last_us / 1000; - if (since_last_ms < *max_timeout_ms) { - *max_timeout_ms = since_last_ms; - } - } - fds[n].fd = source->device->fd; fds[n].events = POLLHUP; - if (can_dequeue) + if (count_enqueued > 0) fds[n].events |= POLLIN; fds[n].revents = 0; buf_lists[n] = source; @@ -144,18 +133,20 @@ void print_pollfds(struct pollfd *fds, int n) printf("pollfds = %d\n", n); } -int links_step(link_t *all_links, int timeout_ms) +int links_step(link_t *all_links, int *timeout_ms) { struct pollfd fds[N_FDS] = {0}; link_t *links[N_FDS]; buffer_list_t *buf_lists[N_FDS]; buffer_t *buf; - int n = _build_fds(all_links, fds, links, buf_lists, N_FDS, &timeout_ms); + int n = _build_fds(all_links, fds, links, buf_lists, N_FDS, timeout_ms); print_pollfds(fds, n); - int ret = poll(fds, n, timeout_ms); + int ret = poll(fds, n, *timeout_ms); print_pollfds(fds, n); + uint64_t now_us = get_monotonic_time_us(NULL, NULL); + if (ret < 0 && errno != EINTR) { return errno; } @@ -241,11 +232,15 @@ int links_loop(link_t *all_links, bool *running) return -1; } + int timeout_ms = LINKS_LOOP_INTERVAL; + while(*running) { - if (links_step(all_links, LINKS_LOOP_INTERVAL) < 0) { + if (links_step(all_links, &timeout_ms) < 0) { links_stream(all_links, false); return -1; } + + timeout_ms = LINKS_LOOP_INTERVAL; } links_stream(all_links, false); diff --git a/hw/links.h b/hw/links.h index a064311..7248f6d 100644 --- a/hw/links.h +++ b/hw/links.h @@ -20,5 +20,5 @@ typedef struct link_s { } link_t; int links_init(link_t *all_links); -int links_step(link_t *all_links, int timeout); +int links_step(link_t *all_links, int *timeout_ms); int links_loop(link_t *all_links, bool *running);