Register buffer_lock as a global output

This commit is contained in:
Kamil Trzcinski
2022-09-03 11:04:37 +02:00
parent 3c818772d1
commit 5031cd99f6
12 changed files with 128 additions and 108 deletions

View File

@ -34,6 +34,9 @@ bool buffer_lock_needs_buffer(buffer_lock_t *buf_lock)
if (buf_lock->refs > 0) {
needs_buffer = true;
}
for (int i = 0; !needs_buffer && buf_lock->notify_buffer[i] && i < BUFFER_LOCK_MAX_CALLBACKS; i++) {
needs_buffer = buf_lock->check_streaming[i](buf_lock);
}
pthread_mutex_unlock(&buf_lock->lock);
return needs_buffer;
@ -67,6 +70,10 @@ void buffer_lock_capture(buffer_lock_t *buf_lock, buffer_t *buf)
(now - buf_lock->buf_time_us) / 1000.0f);
buf_lock->buf_time_us = now;
pthread_cond_broadcast(&buf_lock->cond_wait);
for (int i = 0; buf_lock->notify_buffer[i] && i < BUFFER_LOCK_MAX_CALLBACKS; i++) {
buf_lock->notify_buffer[i](buf_lock, buf);
}
}
pthread_mutex_unlock(&buf_lock->lock);
@ -135,3 +142,37 @@ error:
buffer_lock_use(buf_lock, -1);
return -frames;
}
bool buffer_lock_register_check_streaming(buffer_lock_t *buf_lock, buffer_lock_check_streaming check_streaming)
{
bool ret = false;
pthread_mutex_lock(&buf_lock->lock);
for (int i = 0; i < BUFFER_LOCK_MAX_CALLBACKS; i++) {
if (!buf_lock->check_streaming[i]) {
buf_lock->check_streaming[i] = check_streaming;
ret = true;
break;
}
}
pthread_mutex_unlock(&buf_lock->lock);
return ret;
}
bool buffer_lock_register_notify_buffer(buffer_lock_t *buf_lock, buffer_lock_notify_buffer notify_buffer)
{
bool ret = false;
pthread_mutex_lock(&buf_lock->lock);
for (int i = 0; i < BUFFER_LOCK_MAX_CALLBACKS; i++) {
if (!buf_lock->notify_buffer[i]) {
buf_lock->notify_buffer[i] = notify_buffer;
ret = true;
break;
}
}
pthread_mutex_unlock(&buf_lock->lock);
return ret;
}

View File

@ -5,9 +5,22 @@
#include <pthread.h>
typedef struct buffer_s buffer_t;
typedef struct buffer_list_s buffer_list_t;
typedef struct buffer_lock_s buffer_lock_t;
typedef bool (*buffer_lock_check_streaming)(buffer_lock_t *buf_lock);
typedef void (*buffer_lock_notify_buffer)(buffer_lock_t *buf_lock, buffer_t *buf);
#define BUFFER_LOCK_MAX_CALLBACKS 10
typedef struct buffer_lock_s {
const char *name;
buffer_list_t *buf_list;
buffer_lock_check_streaming check_streaming[BUFFER_LOCK_MAX_CALLBACKS];
buffer_lock_notify_buffer notify_buffer[BUFFER_LOCK_MAX_CALLBACKS];
// private
pthread_mutex_t lock;
pthread_cond_t cond_wait;
buffer_t *buf;
@ -40,3 +53,5 @@ bool buffer_lock_needs_buffer(buffer_lock_t *buf_lock);
void buffer_lock_use(buffer_lock_t *buf_lock, int ref);
bool buffer_lock_is_used(buffer_lock_t *buf_lock);
int buffer_lock_write_loop(buffer_lock_t *buf_lock, int nframes, buffer_write_fn fn, void *data);
bool buffer_lock_register_check_streaming(buffer_lock_t *buf_lock, buffer_lock_check_streaming check_streaming);
bool buffer_lock_register_notify_buffer(buffer_lock_t *buf_lock, buffer_lock_notify_buffer notify_buffer);

View File

@ -3,6 +3,7 @@
#include "device/device.h"
#include "device/device_list.h"
#include "device/buffer_list.h"
#include "device/buffer_lock.h"
#include "device/links.h"
#include "util/opts/log.h"
#include "util/opts/fourcc.h"
@ -82,6 +83,10 @@ void camera_capture_set_callbacks(camera_t *camera, buffer_list_t *capture, link
{
link_t *link = camera_ensure_capture(camera, capture);
link->callbacks = callbacks;
if (callbacks.buf_lock) {
callbacks.buf_lock->buf_list = capture;
}
}
int camera_set_params(camera_t *camera)

View File

@ -18,8 +18,8 @@ static const char *jpeg_names[2] = {
};
static link_callbacks_t jpeg_callbacks[2] = {
{ "JPEG-CAPTURE", http_jpeg_capture, http_jpeg_needs_buffer },
{ "JPEG-LOW-CAPTURE", http_jpeg_lowres_capture, http_jpeg_needs_buffer }
{ .name = "JPEG-CAPTURE", .buf_lock = &http_jpeg },
{ .name = "JPEG-LOW-CAPTURE", .buf_lock = &http_jpeg_lowres }
};
static const char *h264_names[2] = {
@ -27,26 +27,9 @@ static const char *h264_names[2] = {
"H264-LOW"
};
static void h264_capture(buffer_t *buf)
{
http_h264_capture(buf);
rtsp_h264_capture(buf);
}
static void h264_lowres_capture(buffer_t *buf)
{
http_h264_lowres_capture(buf);
rtsp_h264_low_res_capture(buf);
}
static bool h264_needs_buffer()
{
return http_h264_needs_buffer() | rtsp_h264_needs_buffer();
}
static link_callbacks_t h264_callbacks[2] = {
{ "H264-CAPTURE", h264_capture, h264_needs_buffer },
{ "H264-LOW-CAPTURE", h264_lowres_capture, h264_needs_buffer }
{ .name = "H264-CAPTURE", .buf_lock = &http_h264 },
{ .name = "H264-LOW-CAPTURE", .buf_lock = &http_h264_lowres }
};
static int camera_configure_h264_output(camera_t *camera, buffer_list_t *src_capture, int res)

View File

@ -2,6 +2,7 @@
#include "device/device.h"
#include "device/buffer.h"
#include "device/buffer_list.h"
#include "device/buffer_lock.h"
#include "util/opts/log.h"
#include "util/opts/fourcc.h"
@ -34,6 +35,10 @@ int _build_fds(link_t *all_links, struct pollfd *fds, link_t **links, buffer_lis
paused = false;
}
if (link->callbacks.buf_lock && buffer_lock_needs_buffer(link->callbacks.buf_lock)) {
paused = false;
}
for (int j = 0; link->sinks[j]; j++) {
buffer_list_t *sink = link->sinks[j];
@ -107,6 +112,10 @@ int links_enqueue_from_source(buffer_list_t *buf_list, link_t *link)
link->callbacks.on_buffer(buf);
}
if (link->callbacks.buf_lock) {
buffer_lock_capture(link->callbacks.buf_lock, buf);
}
return 0;
error:
@ -296,10 +305,10 @@ void links_dump(link_t *all_links)
links_dump_buf_list(line, link->sinks[j]);
}
if (link->callbacks.callback_name) {
if (link->callbacks.name) {
if (link->sinks[0])
strcat(line, ", ");
strcat(line, link->callbacks.callback_name);
strcat(line, link->callbacks.name);
}
strcat(line, "]");

View File

@ -7,6 +7,7 @@
typedef struct buffer_s buffer_t;
typedef struct buffer_list_s buffer_list_t;
typedef struct buffer_lock_s buffer_lock_t;
typedef struct link_s link_t;
typedef void (*link_on_buffer)(buffer_t *buf);
@ -14,10 +15,11 @@ typedef bool (*link_check_streaming)();
typedef bool (*link_validate_buffer)(struct link_s *link, buffer_t *buf);
typedef struct link_callbacks_s {
const char *callback_name;
const char *name;
link_on_buffer on_buffer;
link_check_streaming check_streaming;
link_validate_buffer validate_buffer;
buffer_lock_t *buf_lock;
} link_callbacks_t;
typedef struct link_s {