@@ -142,12 +142,30 @@ class RTSPServer {
142
142
return ok;
143
143
}
144
144
145
+
146
+ /* *
147
+ * @brief Get the server task handle
148
+ * @return Reference to the server task
149
+ */
145
150
audio_tools::Task& getTaskHandle () { return serverTask; };
146
151
152
+ /* *
153
+ * @brief Get the number of connected clients
154
+ * @return Number of clients
155
+ */
147
156
int clientCount () { return client_count;}
148
157
158
+ /* *
159
+ * @brief Returns true if there is at least one connected client
160
+ */
149
161
operator bool () { return client_count > 0 ;}
150
162
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
+
151
169
protected:
152
170
int port; // port that the RTSP Server listens on
153
171
int core; // the core number the RTSP runs on (platform-specific)
@@ -160,6 +178,7 @@ class RTSPServer {
160
178
// source for data streams
161
179
bool (*onSessionPathCb)(const char *, void *) = nullptr ; // session path callback
162
180
void * onSessionPathRef = nullptr ;
181
+ unsigned long sessionTimeoutMs = 20000 ; // 20 seconds
163
182
164
183
/* *
165
184
* @brief Start RTSP server asynchronously
@@ -277,12 +296,26 @@ class RTSPServer {
277
296
278
297
LOGI (" Session ready" );
279
298
299
+
300
+ // Session timeout logic
301
+ unsigned long lastRequestTime = millis ();
302
+
280
303
while (rtsp->isSessionOpen ()) {
281
304
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 ();
285
308
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
+ }
286
319
}
287
320
288
321
int time = timeout - (millis () - lastCheck);
0 commit comments