From 0e1ea755d922541383821fd75f25c8368838bc32 Mon Sep 17 00:00:00 2001 From: Kamil Trzcinski Date: Wed, 6 Apr 2022 12:22:52 +0200 Subject: [PATCH] Include buffer caching --- http/http_h264.c | 2 +- http/http_jpeg.c | 2 +- hw/buffer_list.c | 3 ++- hw/buffer_lock.c | 8 +++----- hw/buffer_lock.h | 12 ++++++++++-- 5 files changed, 17 insertions(+), 10 deletions(-) diff --git a/http/http_h264.c b/http/http_h264.c index 315e038..1a12f43 100644 --- a/http/http_h264.c +++ b/http/http_h264.c @@ -7,7 +7,7 @@ #include "hw/buffer_list.h" #include "hw/device.h" -DEFINE_BUFFER_LOCK(http_h264); +DEFINE_BUFFER_LOCK(http_h264, 0); static const char *const VIDEO_HEADER = "HTTP/1.0 200 OK\r\n" "Access-Control-Allow-Origin: *\r\n" diff --git a/http/http_jpeg.c b/http/http_jpeg.c index b5c1cb2..c01cd34 100644 --- a/http/http_jpeg.c +++ b/http/http_jpeg.c @@ -5,7 +5,7 @@ #include "hw/buffer.h" #include "hw/buffer_lock.h" -DEFINE_BUFFER_LOCK(http_jpeg); +DEFINE_BUFFER_LOCK(http_jpeg, 1000); #define PART_BOUNDARY "123456789000000000000987654321" #define CONTENT_TYPE "image/jpeg" diff --git a/hw/buffer_list.c b/hw/buffer_list.c index 31884a7..a87100d 100644 --- a/hw/buffer_list.c +++ b/hw/buffer_list.c @@ -79,7 +79,8 @@ retry: // JPEG is in 16x16 blocks (shrink image to fit) (but adapt to 32x32) // And ISP output - if (strstr(buf_list->name, "JPEG") || strstr(buf_list->name, "H264") || buf_list->do_capture && strstr(buf_list->name, "ISP")) { + if (strstr(buf_list->name, "JPEG") || strstr(buf_list->name, "H264") || buf_list->do_capture && strstr(buf_list->name, "ISP") + || strstr(buf_list->name, "DECODER")) { width = shrink_to_block(width, 32); height = shrink_to_block(height, 32); E_LOG_VERBOSE(buf_list, "Adapting size to 32x32 block: %dx%d vs %dx%d", orig_width, orig_height, width, height); diff --git a/hw/buffer_lock.c b/hw/buffer_lock.c index 2744e79..090bbe6 100644 --- a/hw/buffer_lock.c +++ b/hw/buffer_lock.c @@ -20,15 +20,13 @@ void buffer_lock_use(buffer_lock_t *buf_lock, int ref) bool buffer_lock_needs_buffer(buffer_lock_t *buf_lock) { - struct timeval now; - gettimeofday(&now, NULL); + uint64_t now = get_monotonic_time_us(NULL, NULL); bool needs_buffer = false; pthread_mutex_lock(&buf_lock->lock); - if (now.tv_sec - buf_lock->buf_time.tv_sec > 1) { + if (buf_lock->timeout_us > 0 && now - buf_lock->buf_time_us > buf_lock->timeout_us) { buffer_consumed(buf_lock->buf, buf_lock->name); buf_lock->buf = NULL; - needs_buffer = true; } if (buf_lock->refs > 0) { needs_buffer = true; @@ -45,7 +43,7 @@ void buffer_lock_capture(buffer_lock_t *buf_lock, buffer_t *buf) buffer_use(buf); buf_lock->buf = buf; buf_lock->counter++; - gettimeofday(&buf_lock->buf_time, NULL); + buf_lock->buf_time_us = get_monotonic_time_us(NULL, NULL); E_LOG_DEBUG(buf_lock, "Captured buffer %s (refs=%d), frame=%d", dev_name(buf), buf ? buf->mmap_reflinks : 0, buf_lock->counter); pthread_cond_broadcast(&buf_lock->cond_wait); pthread_mutex_unlock(&buf_lock->lock); diff --git a/hw/buffer_lock.h b/hw/buffer_lock.h index acb7857..68cde8b 100644 --- a/hw/buffer_lock.h +++ b/hw/buffer_lock.h @@ -9,12 +9,20 @@ typedef struct buffer_lock_s { pthread_mutex_t lock; pthread_cond_t cond_wait; buffer_t *buf; - struct timeval buf_time; + uint64_t buf_time_us; int counter; int refs; + uint64_t timeout_us; } buffer_lock_t; -#define DEFINE_BUFFER_LOCK(name) static buffer_lock_t name = { #name, PTHREAD_MUTEX_INITIALIZER, PTHREAD_COND_INITIALIZER, NULL, 0 }; +#define DEFAULT_BUFFER_LOCK_TIMEOUT 16 // ~60fps + +#define DEFINE_BUFFER_LOCK(_name, _timeout_ms) static buffer_lock_t _name = { \ + .name = #_name, \ + .lock = PTHREAD_MUTEX_INITIALIZER, \ + .cond_wait = PTHREAD_COND_INITIALIZER, \ + .timeout_us = (_timeout_ms > DEFAULT_BUFFER_LOCK_TIMEOUT ? _timeout_ms : DEFAULT_BUFFER_LOCK_TIMEOUT) * 1000LL, \ + }; void buffer_lock_capture(buffer_lock_t *buf_lock, buffer_t *buf); buffer_t *buffer_lock_get(buffer_lock_t *buf_lock, int timeout_s, int *counter);