Skip to content

Commit bd04edb

Browse files
committed
RTSPServer: session timeout
1 parent 0003dea commit bd04edb

File tree

2 files changed

+38
-5
lines changed

2 files changed

+38
-5
lines changed

src/AudioTools/AudioCodecs/AudioCodecsBase.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,9 @@ class AudioEncoder : public AudioWriter {
107107
AudioInfo audioInfo() override { return info; }
108108
/// Default output assignment (encoders may override to store Print reference)
109109
virtual void setOutput(Print &out_stream) override { (void)out_stream; }
110-
/// Optioinal rtsp function: provide the frame duration in microseconds
110+
/// Optional rtsp function: provide the frame duration in microseconds
111111
virtual uint32_t frameDurationUs() { return 0;};
112-
/// Optioinal rtsp function: provide samples per the frame
112+
/// Optional rtsp function: provide samples per the frame
113113
virtual uint16_t samplesPerFrame() { return 0;};
114114

115115
protected:

src/AudioTools/Communication/RTSP/RTSPServer.h

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)