From 31c837c88062072071935076ed455ae9e5a79349 Mon Sep 17 00:00:00 2001 From: Nita Vesa Date: Tue, 26 Aug 2025 19:12:05 +0300 Subject: [PATCH] feat: make WebRTC timeout user-configurable While commit #a4bcc14283d5a00f6c6ea5247953cbeb832e0eae added a default WebRTC stream timeout and made it possible to override it, the commit did not provide any meaningful mechanism for users to do that. This commit adds two ways for users to override the default timeout with a `timeout_s` parameter in the URL, ie. `/webrtc?timeout_s=somevaluehere` or via `--webrtc-timeout_s` command-line argument. Signed-off-by: Nita Vesa --- cmd/camera-streamer/opts.c | 1 + html/index.html | 2 ++ html/webrtc.html | 11 +++++++++-- output/webrtc/webrtc.cc | 3 +-- output/webrtc/webrtc.h | 1 + util/opts/opts.h | 2 ++ 6 files changed, 16 insertions(+), 4 deletions(-) diff --git a/cmd/camera-streamer/opts.c b/cmd/camera-streamer/opts.c index d5352224..fed6da5d 100644 --- a/cmd/camera-streamer/opts.c +++ b/cmd/camera-streamer/opts.c @@ -135,6 +135,7 @@ option_t all_options[] = { DEFINE_OPTION_PTR(webrtc, ice_servers, list, "Specify ICE servers: [(stun|turn|turns)(:|://)][username:password@]hostname[:port][?transport=udp|tcp|tls)]."), DEFINE_OPTION_DEFAULT(webrtc, disable_client_ice, bool, "1", "Ignore ICE servers provided in '/webrtc' request."), + DEFINE_OPTION_DEFAULT(webrtc, timeout_s, ulong, "3600", "Set how long to keep a WebRTC stream alive before disconnecting by default."), DEFINE_OPTION_DEFAULT(log, debug, bool, "1", "Enable debug logging."), DEFINE_OPTION_DEFAULT(log, verbose, bool, "1", "Enable verbose logging."), diff --git a/html/index.html b/html/index.html index 1245c239..1a465ac6 100644 --- a/html/index.html +++ b/html/index.html @@ -37,6 +37,8 @@
diff --git a/html/webrtc.html b/html/webrtc.html index 599b35aa..78fabba2 100644 --- a/html/webrtc.html +++ b/html/webrtc.html @@ -57,11 +57,18 @@ const params = Object.fromEntries(urlSearchParams.entries()); fetch(window.location.href, { - body: JSON.stringify({ + body: JSON.stringify( + params.hasOwnProperty('timeout_s')? { type: 'request', res: params.res, iceServers: iceServers, - keepAlive: true + keepAlive: true, + timeout_s: parseInt(params.timeout_s) + } : { + type: 'request', + res: params.res, + iceServers: iceServers, + keepAlive: true, }), headers: { 'Content-Type': 'application/json' diff --git a/output/webrtc/webrtc.cc b/output/webrtc/webrtc.cc index 4eedcea9..211c8332 100644 --- a/output/webrtc/webrtc.cc +++ b/output/webrtc/webrtc.cc @@ -18,7 +18,6 @@ extern "C" { #define DEFAULT_PING_INTERVAL_US (1 * 1000 * 1000) #define DEFAULT_PONG_INTERVAL_US (30 * 1000 * 1000) -#define DEFAULT_TIMEOUT_S (60 * 60) #ifdef USE_LIBDATACHANNEL @@ -332,7 +331,7 @@ static std::shared_ptr webrtc_peer_connection(rtc::Configuration config, LOG_INFO(client.get(), "Client does not support Keep-Alives. This might result in stale streams."); } - int64_t timeout_s = message.value("timeout_s", DEFAULT_TIMEOUT_S); + int64_t timeout_s = message.value("timeout_s", webrtc_options->timeout_s); if (timeout_s > 0) { LOG_INFO(client.get(), "The stream will auto-close in %" PRId64 "s.", timeout_s); client->deadline_us = get_monotonic_time_us(NULL, NULL) + timeout_s * 1000 * 1000; diff --git a/output/webrtc/webrtc.h b/output/webrtc/webrtc.h index ebb85192..c545a183 100644 --- a/output/webrtc/webrtc.h +++ b/output/webrtc/webrtc.h @@ -11,6 +11,7 @@ typedef struct webrtc_options_s { bool disabled; char ice_servers[WEBRTC_OPTIONS_LENGTH]; bool disable_client_ice; + unsigned long timeout_s; } webrtc_options_t; // WebRTC diff --git a/util/opts/opts.h b/util/opts/opts.h index 81fa35e9..6d86cf3f 100644 --- a/util/opts/opts.h +++ b/util/opts/opts.h @@ -14,6 +14,7 @@ typedef struct options_s { union { unsigned *value; unsigned *value_uint; + unsigned long *value_ulong; unsigned *value_hex; bool *value_bool; float *value_float; @@ -30,6 +31,7 @@ typedef struct options_s { #define OPTION_VALUE_LIST_SEP ";" #define OPTION_FORMAT_uint "%u" +#define OPTION_FORMAT_ulong "%lu" #define OPTION_FORMAT_hex "%08x" #define OPTION_FORMAT_bool "%d" #define OPTION_FORMAT_float "%f"