Include buffer caching

This commit is contained in:
Kamil Trzcinski 2022-04-06 12:22:52 +02:00
parent 237e87c90e
commit 0e1ea755d9
5 changed files with 17 additions and 10 deletions

View File

@ -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"

View File

@ -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"

View File

@ -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);

View File

@ -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);

View File

@ -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);