11#include "RTSPSession.h"
15#include "AudioTools/Concurrency/RTOS.h"
46template <
typename Platform>
71 this->streamer = &streamer;
75 serverTask.
create(
"RTSPServerThread", 10000, 5, core);
76 sessionTask.
create(
"RTSPSessionTask", 8000, 8, core);
96 onSessionPathRef = ref;
99#if defined(USE_RTSP_LOGIN)
116 bool begin(
const char* ssid,
const char* password) {
118 WiFi.begin(ssid, password);
119 while (WiFi.status() != WL_CONNECTED) {
123 WiFi.setSleep(WIFI_PS_NONE);
126 Serial.print(
"connect to rtsp://");
127 Serial.print(WiFi.localIP());
129 Serial.println(port);
140 LOGE(
"Couldn't start RTSP server");
161 operator bool() {
return client_count > 0;}
174 typename Platform::TcpServerType* server =
nullptr;
175 typename Platform::TcpClientType client;
176 int client_count = 0;
177 streamer_t* streamer =
nullptr;
179 bool (*onSessionPathCb)(
const char*,
void*) =
nullptr;
180 void* onSessionPathRef =
nullptr;
181 unsigned long sessionTimeoutMs = 20000;
198 LOGI(
"Running RTSP server on port %d", port);
199 streamer->initAudioSource();
200 if (server ==
nullptr) {
201 server = Platform::createServer(port);
202 LOGI(
"RTSP server started on port %d", port);
204 if (!serverTask.begin([
this]() { serverThreadLoop(); })) {
205 LOGE(
"Couldn't start server thread");
223 unsigned long lastCheck =
millis();
225 LOGD(
"Server thread listening... (numClients: %d)", client_count);
228 if (client_count == 0) {
229 auto newClient = Platform::getAvailableClient(server);
230 if (newClient.connected()) {
232 LOGI(
"Client connected");
233 if (!sessionTask.begin([
this]() { sessionThreadLoop(); })) {
234 LOGE(
"Couldn't start sessionThread");
235 Platform::closeSocket(&client);
238 LOGI(
"Number of clients: %d", client_count);
242 LOGD(
"Waiting for current session to end (numClients: %d)", client_count);
244 int time = 200 - (
millis() - lastCheck);
245 if (time < 0) time = 0;
253 LOGI(
"Stopping RTSP server");
267 LOGI(
"RTSP server stopped");
284 typename Platform::TcpClientType* s = &client;
285 unsigned long lastCheck =
millis();
288 LOGD(
"RTSP Task running");
292 if (onSessionPathCb) {
295 assert(rtsp !=
nullptr);
297 LOGI(
"Session ready");
301 unsigned long lastRequestTime =
millis();
303 while (rtsp->isSessionOpen()) {
304 uint32_t timeout = 30;
307 lastRequestTime =
millis();
308 LOGD(
"Request handling successful");
310 LOGI(
"Request handling timed out or no data yet");
314 if (rtsp->isStreaming()) {
315 if ((
millis() - lastRequestTime) > sessionTimeoutMs) {
316 LOGW(
"Session timeout: no client request received for 20 seconds, closing session");
321 int time = timeout - (
millis() - lastCheck);
322 if (time < 0) time = 0;
326 LOGI(
"Session loop exited - session no longer open");
329 LOGI(
"sessionThread stopped, cleaning up");
333 if (client.connected()) {
334 Platform::closeSocket(&client);
341 LOGI(
"Session cleaned up: (numClients: %d)", client_count);