mirror of
https://github.com/pikvm/ustreamer.git
synced 2026-02-27 04:06:30 +00:00
client ttl; some refactoring
This commit is contained in:
@@ -23,6 +23,7 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdbool.h>
|
||||
#include <unistd.h>
|
||||
#include <signal.h>
|
||||
#include <getopt.h>
|
||||
#include <errno.h>
|
||||
@@ -200,7 +201,7 @@ static int _dump_sink(const char *sink_name, unsigned sink_timeout, _output_cont
|
||||
frame_s *frame = frame_init("input");
|
||||
memsink_s *sink = NULL;
|
||||
|
||||
if ((sink = memsink_init("input", sink_name, false, 0, false, sink_timeout)) == NULL) {
|
||||
if ((sink = memsink_init("input", sink_name, false, 0, false, 0, sink_timeout)) == NULL) {
|
||||
goto error;
|
||||
}
|
||||
|
||||
@@ -235,7 +236,9 @@ static int _dump_sink(const char *sink_name, unsigned sink_timeout, _output_cont
|
||||
if (ctx->v_output) {
|
||||
ctx->write(ctx->v_output, frame);
|
||||
}
|
||||
} else if (error != -2) {
|
||||
} else if (error == -2) {
|
||||
usleep(1000);
|
||||
} else {
|
||||
goto error;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,13 +26,17 @@
|
||||
static int _flock_timedwait_monotonic(int fd, long double timeout);
|
||||
|
||||
|
||||
memsink_s *memsink_init(const char *name, const char *obj, bool server, mode_t mode, bool rm, unsigned timeout) {
|
||||
memsink_s *memsink_init(
|
||||
const char *name, const char *obj, bool server,
|
||||
mode_t mode, bool rm, unsigned client_ttl, unsigned timeout) {
|
||||
|
||||
memsink_s *sink;
|
||||
A_CALLOC(sink, 1);
|
||||
sink->name = name;
|
||||
sink->obj = obj;
|
||||
sink->server = server;
|
||||
sink->rm = rm;
|
||||
sink->client_ttl = client_ttl;
|
||||
sink->timeout = timeout;
|
||||
sink->fd = -1;
|
||||
sink->mem = MAP_FAILED;
|
||||
@@ -99,7 +103,7 @@ int memsink_server_check_clients(memsink_s *sink) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
sink->has_clients = (sink->mem->last_client_ts + 10 > get_now_monotonic());
|
||||
sink->has_clients = (sink->mem->last_client_ts + sink->client_ttl > get_now_monotonic());
|
||||
|
||||
if (flock(sink->fd, LOCK_UN) < 0) {
|
||||
LOG_PERROR("%s-sink: Can't unlock memory", sink->name);
|
||||
@@ -134,7 +138,7 @@ int memsink_server_put(memsink_s *sink, const frame_s *frame) {
|
||||
COPY(grab_ts);
|
||||
COPY(encode_begin_ts);
|
||||
COPY(encode_end_ts);
|
||||
sink->has_clients = (sink->mem->last_client_ts + 10 > get_now_monotonic());
|
||||
sink->has_clients = (sink->mem->last_client_ts + sink->client_ttl > get_now_monotonic());
|
||||
memcpy(sink->mem->data, frame->data, frame->used);
|
||||
# undef COPY
|
||||
|
||||
@@ -166,12 +170,8 @@ int memsink_client_get(memsink_s *sink, frame_s *frame) { // cppcheck-suppress u
|
||||
return -1;
|
||||
}
|
||||
|
||||
bool same = false;
|
||||
|
||||
sink->mem->last_client_ts = get_now_monotonic();
|
||||
if (sink->mem->id == sink->last_id) {
|
||||
same = true;
|
||||
} else {
|
||||
bool updated = (sink->mem->id != sink->last_id);
|
||||
if (updated) {
|
||||
# define COPY(_field) frame->_field = sink->mem->_field
|
||||
sink->last_id = sink->mem->id;
|
||||
COPY(width);
|
||||
@@ -185,17 +185,13 @@ int memsink_client_get(memsink_s *sink, frame_s *frame) { // cppcheck-suppress u
|
||||
frame_set_data(frame, sink->mem->data, sink->mem->used);
|
||||
# undef COPY
|
||||
}
|
||||
sink->mem->last_client_ts = get_now_monotonic();
|
||||
|
||||
if (flock(sink->fd, LOCK_UN) < 0) {
|
||||
LOG_PERROR("%s-sink: Can't unlock memory", sink->name);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (same) {
|
||||
usleep(1000);
|
||||
return -2;
|
||||
}
|
||||
return 0;
|
||||
return (updated ? 0 : -2);
|
||||
}
|
||||
|
||||
static int _flock_timedwait_monotonic(int fd, long double timeout) {
|
||||
|
||||
@@ -65,6 +65,7 @@ typedef struct {
|
||||
const char *obj;
|
||||
bool server;
|
||||
bool rm;
|
||||
unsigned client_ttl; // Only for server
|
||||
unsigned timeout;
|
||||
|
||||
int fd;
|
||||
@@ -74,7 +75,10 @@ typedef struct {
|
||||
} memsink_s;
|
||||
|
||||
|
||||
memsink_s *memsink_init(const char *name, const char *obj, bool server, mode_t mode, bool rm, unsigned timeout);
|
||||
memsink_s *memsink_init(
|
||||
const char *name, const char *obj, bool server,
|
||||
mode_t mode, bool rm, unsigned client_ttl, unsigned timeout);
|
||||
|
||||
void memsink_destroy(memsink_s *sink);
|
||||
|
||||
int memsink_server_check_clients(memsink_s *sink);
|
||||
|
||||
@@ -86,6 +86,7 @@ enum _OPT_VALUES {
|
||||
_O_##_prefix, \
|
||||
_O_##_prefix##_MODE, \
|
||||
_O_##_prefix##_RM, \
|
||||
_O_##_prefix##_CLIENT_TTL, \
|
||||
_O_##_prefix##_TIMEOUT,
|
||||
ADD_SINK(SINK)
|
||||
# ifdef WITH_OMX
|
||||
@@ -174,10 +175,11 @@ static const struct option _LONG_OPTS[] = {
|
||||
{"server-timeout", required_argument, NULL, _O_SERVER_TIMEOUT},
|
||||
|
||||
# define ADD_SINK(_opt, _prefix) \
|
||||
{_opt "sink", required_argument, NULL, _O_##_prefix}, \
|
||||
{_opt "sink-mode", required_argument, NULL, _O_##_prefix##_MODE}, \
|
||||
{_opt "sink-rm", no_argument, NULL, _O_##_prefix##_RM}, \
|
||||
{_opt "sink-timeout", required_argument, NULL, _O_##_prefix##_TIMEOUT},
|
||||
{_opt "sink", required_argument, NULL, _O_##_prefix}, \
|
||||
{_opt "sink-mode", required_argument, NULL, _O_##_prefix##_MODE}, \
|
||||
{_opt "sink-rm", no_argument, NULL, _O_##_prefix##_RM}, \
|
||||
{_opt "sink-client-ttl", required_argument, NULL, _O_##_prefix##_CLIENT_TTL}, \
|
||||
{_opt "sink-timeout", required_argument, NULL, _O_##_prefix##_TIMEOUT},
|
||||
ADD_SINK("", SINK)
|
||||
# ifdef WITH_OMX
|
||||
ADD_SINK("h264-", H264_SINK)
|
||||
@@ -334,6 +336,7 @@ int options_parse(options_s *options, device_s *dev, encoder_s *enc, stream_s *s
|
||||
char *_prefix##_name = NULL; \
|
||||
mode_t _prefix##_mode = 0660; \
|
||||
bool _prefix##_rm = false; \
|
||||
unsigned _prefix##_client_ttl = 10; \
|
||||
unsigned _prefix##_timeout = 1;
|
||||
ADD_SINK(sink);
|
||||
# ifdef WITH_OMX
|
||||
@@ -431,6 +434,7 @@ int options_parse(options_s *options, device_s *dev, encoder_s *enc, stream_s *s
|
||||
case _O_##_up: OPT_SET(_lp##_name, optarg); \
|
||||
case _O_##_up##_MODE: OPT_NUMBER("--" #_opt "sink-mode", _lp##_mode, INT_MIN, INT_MAX, 8); \
|
||||
case _O_##_up##_RM: OPT_SET(_lp##_rm, true); \
|
||||
case _O_##_up##_CLIENT_TTL: OPT_NUMBER("--" #_opt "sink-client-ttl", _lp##_client_ttl, 1, 60, 0); \
|
||||
case _O_##_up##_TIMEOUT: OPT_NUMBER("--" #_opt "sink-timeout", _lp##_timeout, 1, 60, 0);
|
||||
ADD_SINK("", sink, SINK)
|
||||
# ifdef WITH_OMX
|
||||
@@ -487,6 +491,7 @@ int options_parse(options_s *options, device_s *dev, encoder_s *enc, stream_s *s
|
||||
true, \
|
||||
_prefix##_mode, \
|
||||
_prefix##_rm, \
|
||||
_prefix##_client_ttl, \
|
||||
_prefix##_timeout \
|
||||
); \
|
||||
} \
|
||||
@@ -661,15 +666,16 @@ static void _help(FILE *fp, device_s *dev, encoder_s *enc, stream_s *stream, ser
|
||||
# define ADD_SINK(_name, _opt) \
|
||||
SAY(_name " sink options:"); \
|
||||
SAY("══════════════════"); \
|
||||
SAY(" --" _opt "sink <name> ──────── Use the shared memory to sink " _name " frames. Default: disabled.\n"); \
|
||||
SAY(" --" _opt "sink-mode <mode> ─── Set " _name " sink permissions (like 777). Default: 660.\n"); \
|
||||
SAY(" --" _opt "sink-rm ──────────── Remove shared memory on stop. Default: disabled.\n"); \
|
||||
SAY(" --" _opt "sink-timeout <sec> ─ Timeout for lock. Default: 1.\n");
|
||||
SAY(" --" _opt "sink <name> ─────────── Use the shared memory to sink " _name " frames. Default: disabled.\n"); \
|
||||
SAY(" --" _opt "sink-mode <mode> ────── Set " _name " sink permissions (like 777). Default: 660.\n"); \
|
||||
SAY(" --" _opt "sink-rm ─────────────── Remove shared memory on stop. Default: disabled.\n"); \
|
||||
SAY(" --" _opt "sink-client-ttl <sec> ─ Client TTL. Default: 10.\n"); \
|
||||
SAY(" --" _opt "sink-timeout <sec> ──── Timeout for lock. Default: 1.\n");
|
||||
ADD_SINK("JPEG", "")
|
||||
# ifdef WITH_OMX
|
||||
ADD_SINK("H264", "h264-")
|
||||
SAY(" --h264-bitrate <kbps> ───── H264 bitrate in Kbps. Default: %u.\n", stream->h264_bitrate);
|
||||
SAY(" --h264-gop <N> ──────────── Intarval between keyframes. Default: %u.\n", stream->h264_gop);
|
||||
SAY(" --h264-bitrate <kbps> ──────── H264 bitrate in Kbps. Default: %u.\n", stream->h264_bitrate);
|
||||
SAY(" --h264-gop <N> ─────────────── Intarval between keyframes. Default: %u.\n", stream->h264_gop);
|
||||
# endif
|
||||
# undef ADD_SINK
|
||||
# ifdef WITH_GPIO
|
||||
|
||||
Reference in New Issue
Block a user