@@ -142,12 +142,30 @@ class RTSPServer {
142142 return ok;
143143 }
144144
145+
146+ /* *
147+ * @brief Get the server task handle
148+ * @return Reference to the server task
149+ */
145150 audio_tools::Task& getTaskHandle () { return serverTask; };
146151
152+ /* *
153+ * @brief Get the number of connected clients
154+ * @return Number of clients
155+ */
147156 int clientCount () { return client_count;}
148157
158+ /* *
159+ * @brief Returns true if there is at least one connected client
160+ */
149161 operator bool () { return client_count > 0 ;}
150162
163+ /* *
164+ * @brief Set the session timeout in milliseconds
165+ * @param ms Timeout in milliseconds
166+ */
167+ void setSessionTimoutMs (unsigned long ms){ sessionTimeoutMs = ms;}
168+
151169 protected:
152170 int port; // port that the RTSP Server listens on
153171 int core; // the core number the RTSP runs on (platform-specific)
@@ -160,6 +178,7 @@ class RTSPServer {
160178 // source for data streams
161179 bool (*onSessionPathCb)(const char *, void *) = nullptr ; // session path callback
162180 void * onSessionPathRef = nullptr ;
181+ unsigned long sessionTimeoutMs = 20000 ; // 20 seconds
163182
164183 /* *
165184 * @brief Start RTSP server asynchronously
@@ -277,12 +296,26 @@ class RTSPServer {
277296
278297 LOGI (" Session ready" );
279298
299+
300+ // Session timeout logic
301+ unsigned long lastRequestTime = millis ();
302+
280303 while (rtsp->isSessionOpen ()) {
281304 uint32_t timeout = 30 ;
282- if (! rtsp->handleRequests (timeout)) {
283- LOGI ( " Request handling timed out or no data yet " );
284- } else {
305+ bool gotRequest = rtsp->handleRequests (timeout);
306+ if (gotRequest) {
307+ lastRequestTime = millis ();
285308 LOGD (" Request handling successful" );
309+ } else {
310+ LOGI (" Request handling timed out or no data yet" );
311+ }
312+
313+ // If streaming, check for session timeout
314+ if (rtsp->isStreaming ()) {
315+ if ((millis () - lastRequestTime) > sessionTimeoutMs) {
316+ LOGW (" Session timeout: no client request received for 20 seconds, closing session" );
317+ break ;
318+ }
286319 }
287320
288321 int time = timeout - (millis () - lastCheck);
0 commit comments