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);
99 bool begin(
const char* ssid,
const char* password) {
102 WiFi.begin(ssid, password);
103 while (WiFi.status() != WL_CONNECTED) {
108 esp_wifi_set_ps(WIFI_PS_NONE);
112 Serial.print(
"connect to rtsp://");
113 Serial.print(WiFi.localIP());
115 Serial.println(port);
124 LOGE(
"Couldn't start RTSP server");
131 int clientCount() {
return client_count;}
133 operator bool() {
return client_count > 0;}
140 typename Platform::TcpServerType* server =
nullptr;
141 typename Platform::TcpClientType client;
142 int client_count = 0;
143 streamer_t* streamer =
nullptr;
161 LOGI(
"Running RTSP server on port %d", port);
162 streamer->initAudioSource();
163 if (server ==
nullptr) {
164 server = Platform::createServer(port);
165 LOGI(
"RTSP server started on port %d", port);
167 if (!serverTask.begin([
this]() { serverThreadLoop(); })) {
168 LOGE(
"Couldn't start server thread");
186 unsigned long lastCheck =
millis();
188 LOGD(
"Server thread listening... (numClients: %d)", client_count);
191 if (client_count == 0) {
192 auto newClient = Platform::getAvailableClient(server);
193 if (newClient.connected()) {
195 LOGI(
"Client connected");
196 if (!sessionTask.begin([
this]() { sessionThreadLoop(); })) {
197 LOGE(
"Couldn't start sessionThread");
198 Platform::closeSocket(&client);
201 LOGI(
"Number of clients: %d", client_count);
205 LOGD(
"Waiting for current session to end (numClients: %d)", client_count);
207 int time = 200 - (
millis() - lastCheck);
208 if (time < 0) time = 0;
216 LOGI(
"Stopping RTSP server");
230 LOGI(
"RTSP server stopped");
247 typename Platform::TcpClientType* s = &client;
248 unsigned long lastCheck =
millis();
251 LOGD(
"RTSP Task running");
255 assert(rtsp !=
nullptr);
257 LOGI(
"Session ready");
259 while (rtsp->isSessionOpen()) {
260 uint32_t timeout = 30;
262 LOGI(
"Request handling timed out or no data yet");
264 LOGD(
"Request handling successful");
267 int time = timeout - (
millis() - lastCheck);
268 if (time < 0) time = 0;
272 LOGI(
"Session loop exited - session no longer open");
275 LOGI(
"sessionThread stopped, cleaning up");
279 if (client.connected()) {
280 Platform::closeSocket(&client);
287 LOGI(
"Session cleaned up: (numClients: %d)", client_count);