From ca07a9155be75a1b5774d4f5d5df996315fa1dce Mon Sep 17 00:00:00 2001 From: Devaev Maxim Date: Sun, 28 Oct 2018 12:30:40 +0300 Subject: [PATCH] --device-persistent --- src/device.c | 2 +- src/device.h | 3 ++- src/main.c | 13 ++++++++----- src/stream.c | 21 +++++++++++++++++---- 4 files changed, 28 insertions(+), 11 deletions(-) diff --git a/src/device.c b/src/device.c index aa28c8f..1003f0e 100644 --- a/src/device.c +++ b/src/device.c @@ -85,7 +85,7 @@ struct device_t *device_init() { dev->n_buffers = max_u(sysconf(_SC_NPROCESSORS_ONLN), 1) + 1; dev->n_workers = dev->n_buffers; dev->timeout = 1; - dev->error_timeout = 1; + dev->error_delay = 1; dev->run = run; return dev; } diff --git a/src/device.h b/src/device.h index ee7ab75..443b5e2 100644 --- a/src/device.h +++ b/src/device.h @@ -74,8 +74,9 @@ struct device_t { unsigned n_workers; unsigned every_frame; unsigned min_frame_size; + bool persistent; unsigned timeout; - unsigned error_timeout; + unsigned error_delay; struct device_runtime_t *run; sig_atomic_t volatile stop; diff --git a/src/main.c b/src/main.c index 1bf9f8e..52d7aa5 100644 --- a/src/main.c +++ b/src/main.c @@ -59,7 +59,8 @@ static const struct option _long_opts[] = { {"encoder-omx-use-ijg", required_argument, NULL, 500}, # endif {"device-timeout", required_argument, NULL, 1000}, - {"device-error-timeout", required_argument, NULL, 1001}, + {"device-persistent", no_argument, NULL, 1001}, + {"device-error-delay", required_argument, NULL, 1002}, {"host", required_argument, NULL, 's'}, {"port", required_argument, NULL, 'p'}, @@ -122,8 +123,9 @@ static void _help(struct device_t *dev, struct encoder_t *encoder, struct http_s printf(" Default: disabled.\n\n"); # endif printf(" --device-timeout -- Timeout for device querying. Default: %d\n\n", dev->timeout); - printf(" --device-error-timeout -- Delay before trying to connect to the device again\n"); - printf(" after a timeout. Default: %d\n\n", dev->error_timeout); + printf(" --device-persistent -- Don't re-initialize device on timeout. Default: disabled.\n\n"); + printf(" --device-error-delay -- Delay before trying to connect to the device again\n"); + printf(" after a timeout. Default: %d\n\n", dev->error_delay); printf("HTTP server options:\n"); printf("--------------------\n"); printf(" --host
-- Listen on Hostname or IP. Default: %s\n\n", server->host); @@ -189,8 +191,9 @@ static int _parse_options(int argc, char *argv[], struct device_t *dev, struct e # ifdef OMX_ENCODER case 500: OPT_SET(encoder->omx_use_ijg, true); # endif - case 1000: OPT_UNSIGNED(dev->timeout, "--timeout", 1, 60); - case 1001: OPT_UNSIGNED(dev->error_timeout, "--error-timeout", 1, 60); + case 1000: OPT_UNSIGNED(dev->timeout, "--device-timeout", 1, 60); + case 1001: OPT_SET(dev->persistent, true); + case 1002: OPT_UNSIGNED(dev->error_delay, "--device-error-delay", 1, 60); case 's': OPT_SET(server->host, optarg); case 'p': OPT_UNSIGNED(server->port, "--port", 1, 65535); diff --git a/src/stream.c b/src/stream.c index f39d550..9702ac9 100644 --- a/src/stream.c +++ b/src/stream.c @@ -87,10 +87,13 @@ void stream_loop(struct stream_t *stream) { unsigned fluency_passed = 0; unsigned captured_fps_accum = 0; long long captured_fps_second = 0; + bool persistent_timeout_reported = false; LOG_DEBUG("Allocation memory for stream picture ..."); A_CALLOC(stream->picture.data, stream->dev->run->max_picture_size); + LOG_INFO("Capturing ..."); + while (!stream->dev->stop) { int free_worker_number = -1; @@ -154,10 +157,20 @@ void stream_loop(struct stream_t *stream) { } } else if (retval == 0) { - LOG_ERROR("Mainloop select() timeout"); - break; + if (stream->dev->persistent) { + if (!persistent_timeout_reported) { + LOG_ERROR("Mainloop select() timeout, polling ...") + persistent_timeout_reported = true; + } + continue; + } else { + LOG_ERROR("Mainloop select() timeout"); + break; + } } else { + persistent_timeout_reported = false; + if (FD_ISSET(stream->dev->run->fd, &read_fds)) { LOG_DEBUG("Frame is ready"); @@ -327,8 +340,8 @@ static int _stream_init_loop(struct device_t *dev, struct workers_pool_t *pool) LOG_DEBUG("%s: *dev->stop = %d", __FUNCTION__, dev->stop); while (!dev->stop) { if ((retval = _stream_init(dev, pool)) < 0) { - LOG_INFO("Sleeping %d seconds before new stream init ...", dev->error_timeout); - sleep(dev->error_timeout); + LOG_INFO("Sleeping %d seconds before new stream init ...", dev->error_delay); + sleep(dev->error_delay); } else { break; }