@@ -17,10 +17,12 @@ extern "C" {
1717#include " util/http/json.hh"
1818
1919#define DEFAULT_PING_INTERVAL_US (1 * 1000 * 1000 )
20- #define DEFAULT_DISCONNECT_INTERVAL_US (30 * 1000 * 1000 )
20+ #define DEFAULT_PONG_INTERVAL_US (30 * 1000 * 1000 )
21+ #define DEFAULT_TIMEOUT_S (60 * 60 )
2122
2223#ifdef USE_LIBDATACHANNEL
2324
25+ #include < inttypes.h>
2426#include < string>
2527#include < memory>
2628#include < optional>
@@ -122,18 +124,23 @@ class Client
122124
123125 bool keepAlive ()
124126 {
127+ uint64_t now_us = get_monotonic_time_us (NULL , NULL );
128+
129+ if (deadline_us > 0 && now_us > deadline_us) {
130+ LOG_INFO (this , " The stream reached the deadline." );
131+ return false ;
132+ }
133+
125134 if (!dc_keepAlive)
126135 return true ;
127136
128- uint64_t now_us = get_monotonic_time_us (NULL , NULL );
129-
130137 if (dc_keepAlive->isOpen () && now_us - last_ping_us >= DEFAULT_PING_INTERVAL_US) {
131138 LOG_DEBUG (this , " Checking if client still alive." );
132139 dc_keepAlive->send (" ping" );
133140 last_ping_us = now_us;
134141 }
135142
136- if (now_us - last_pong_us >= DEFAULT_DISCONNECT_INTERVAL_US ) {
143+ if (now_us - last_pong_us >= DEFAULT_PONG_INTERVAL_US ) {
137144 LOG_INFO (this , " No heartbeat from client." );
138145 return false ;
139146 }
@@ -213,6 +220,7 @@ class Client
213220 bool requested_key_frame = false ;
214221 uint64_t last_ping_us = 0 ;
215222 uint64_t last_pong_us = 0 ;
223+ uint64_t deadline_us = 0 ;
216224};
217225
218226std::shared_ptr<Client> webrtc_find_client (std::string id)
@@ -324,6 +332,12 @@ static std::shared_ptr<Client> webrtc_peer_connection(rtc::Configuration config,
324332 LOG_INFO (client.get (), " Client does not support Keep-Alives. This might result in stale streams." );
325333 }
326334
335+ int64_t timeout_s = message.value (" timeout_s" , DEFAULT_TIMEOUT_S);
336+ if (timeout_s > 0 ) {
337+ LOG_INFO (client.get (), " The stream will auto-close in %" PRId64 " s." , timeout_s);
338+ client->deadline_us = get_monotonic_time_us (NULL , NULL ) + timeout_s * 1000 * 1000 ;
339+ }
340+
327341 pc->onTrack ([wclient](std::shared_ptr<rtc::Track> track) {
328342 if (auto client = wclient.lock ()) {
329343 LOG_DEBUG (client.get (), " onTrack: %s" , track->mid ().c_str ());
0 commit comments