arduino-audio-tools
Loading...
Searching...
No Matches
RTSPServerBase.h
1/*
2 * Author: Phil Schatzmann
3 *
4 * Based on Micro-RTSP library:
5 * https://github.com/geeksville/Micro-RTSP
6 * https://github.com/Tomp0801/Micro-RTSP-Audio
7 */
8#pragma once
9#include "RTSPSession.h"
10#include "RTSPAudioStreamer.h"
11#ifdef ESP32
12#include <WiFi.h>
13#include <esp_wifi.h>
14#endif
15
16namespace audio_tools {
17
28template <typename Platform>
30 public:
33 : streamer(&streamer), port(port) {
34 server = nullptr;
35 }
36
39
41 void setOnSessionPath(bool (*cb)(const char* path, void* ref), void* ref = nullptr) {
42 onSessionPathCb = cb;
43 onSessionPathRef = ref;
44 }
45
46#ifdef ESP32
48 bool begin(const char* ssid, const char* password) {
49 WiFi.begin(ssid, password);
50 while (WiFi.status() != WL_CONNECTED) {
51 delay(500);
52 Serial.print(".");
53 }
54 WiFi.setSleep(WIFI_PS_NONE);
55 Serial.println();
56 Serial.print("connect to rtsp://");
57 Serial.print(WiFi.localIP());
58 Serial.print(":");
59 Serial.println(port);
60 Serial.println();
61 return begin();
62 }
63#endif
64
66 virtual bool begin() {
67 streamer->initAudioSource();
68 if (server == nullptr) {
69 server = Platform::createServer(port);
70 LOGI("RTSP server started on port %d", port);
71 }
72 return server != nullptr;
73 }
74
76 void end() {
77 if (server) {
78 delete server;
79 server = nullptr;
80 }
81 client_count = 0;
82 if (rtspSession) {
83 delete rtspSession;
84 rtspSession = nullptr;
85 }
86 }
87
89 int clientCount() { return client_count; }
91 operator bool() { return client_count > 0; }
93 void setSessionTimeoutMs(unsigned long ms) { sessionTimeoutMs = ms; }
94
95 protected:
96 int port = 554;
97 typename Platform::TcpServerType* server = nullptr;
98 typename Platform::TcpClientType client;
99 RTSPAudioStreamerBase<Platform>* streamer = nullptr;
100 int client_count = 0;
101 RtspSession<Platform>* rtspSession = nullptr;
102 bool (*onSessionPathCb)(const char*, void*) = nullptr;
103 void* onSessionPathRef = nullptr;
104 unsigned long sessionTimeoutMs = 60000; // 60 seconds
105 unsigned long lastRequestTime = 0;
106
109 if (client_count == 0 && server) {
110 auto newClient = Platform::getAvailableClient(server);
111 if (newClient.connected()) {
112 client = newClient;
113 client_count++;
114 // Create session
115 rtspSession = new RtspSession<Platform>(client, *streamer);
116 if (onSessionPathCb) {
117 rtspSession->setOnSessionPath(onSessionPathCb, onSessionPathRef);
118 }
119 lastRequestTime = millis();
120 }
121 }
122 }
123
126 if (client_count > 0 && rtspSession) {
127 uint32_t timeout = 30;
128 bool gotRequest = rtspSession->handleRequests(timeout);
129 if (gotRequest) {
130 lastRequestTime = millis();
131 }
132 // If streaming, check for session timeout
133 if (sessionTimeoutMs > 0 && rtspSession->isStreaming()) {
134 if ((millis() - lastRequestTime) > sessionTimeoutMs) {
135 // Timeout, mark session closed
136 rtspSession->closeSession();
137 }
138 }
139 // Clean up if session closed
140 if (!rtspSession->isSessionOpen()) {
141 delete rtspSession;
142 rtspSession = nullptr;
143 if (client.connected()) {
144 Platform::closeSocket(&client);
145 }
146 client_count--;
147 }
148 }
149 }
150};
151
152} // namespace audio_tools
RTSPAudioStreamerBase - Core RTP Audio Streaming Engine.
Definition RTSPAudioStreamer.h:50
RTSPServerBase - Shared logic for RTSPServer and RTSPServerTaskless.
Definition RTSPServerBase.h:29
void handleSession()
Handle requests session if active.
Definition RTSPServerBase.h:125
bool begin(const char *ssid, const char *password)
Start server and connect to WiFi (ESP32 only)
Definition RTSPServerBase.h:48
RTSPServerBase(RTSPAudioStreamerBase< Platform > &streamer, int port=8554)
Constructor.
Definition RTSPServerBase.h:32
int clientCount()
Get number of connected clients.
Definition RTSPServerBase.h:89
virtual bool begin()
Start server.
Definition RTSPServerBase.h:66
void setSessionTimeoutMs(unsigned long ms)
Set session timeout in milliseconds.
Definition RTSPServerBase.h:93
~RTSPServerBase()
Destructor.
Definition RTSPServerBase.h:38
void end()
Stop server and clean up.
Definition RTSPServerBase.h:76
void acceptClient()
Accept new client if none is active.
Definition RTSPServerBase.h:108
void setOnSessionPath(bool(*cb)(const char *path, void *ref), void *ref=nullptr)
Set callback for session path.
Definition RTSPServerBase.h:41
RTSP Session Handler - Individual Client Protocol Management.
Definition RTSPSession.h:77
void closeSession()
Mark session as closed (for server timeout/teardown)
Definition RTSPSession.h:233
bool handleRequests(uint32_t readTimeoutMs)
Process incoming RTSP requests from the client.
Definition RTSPSession.h:148
void setOnSessionPath(bool(*cb)(const char *path, void *ref), void *ref=nullptr)
Set a callback to receive the RTSP URL path that opened the session. The callback is invoked once,...
Definition RTSPSession.h:225
void stop()
stops any further processing by spinning in an endless loop
Definition AudioRuntime.h:12
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition AudioCodecsBase.h:10
uint32_t millis()
Returns the milliseconds since the start.
Definition Time.h:12