arduino-audio-tools
Loading...
Searching...
No Matches
RTSPServerBase.h
Go to the documentation of this file.
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:
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;
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()) {
113 client_count++;
114 // Create session
116 if (onSessionPathCb) {
117 rtspSession->setOnSessionPath(onSessionPathCb, onSessionPathRef);
118 }
120 }
121 }
122 }
123
126 if (client_count > 0 && rtspSession) {
127 uint32_t timeout = 30;
128 bool gotRequest = rtspSession->handleRequests(timeout);
129 if (gotRequest) {
131 }
132 // If streaming, check for session timeout
133 if (sessionTimeoutMs > 0 && rtspSession->isStreaming()) {
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
#define LOGI(...)
Definition AudioLoggerIDF.h:28
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
void * onSessionPathRef
Definition RTSPServerBase.h:103
int clientCount()
Get number of connected clients.
Definition RTSPServerBase.h:89
RtspSession< Platform > * rtspSession
Definition RTSPServerBase.h:101
Platform::TcpClientType client
Definition RTSPServerBase.h:98
virtual bool begin()
Start server.
Definition RTSPServerBase.h:66
int port
Definition RTSPServerBase.h:96
void setSessionTimeoutMs(unsigned long ms)
Set session timeout in milliseconds.
Definition RTSPServerBase.h:93
~RTSPServerBase()
Destructor.
Definition RTSPServerBase.h:38
unsigned long lastRequestTime
Definition RTSPServerBase.h:105
Platform::TcpServerType * server
Definition RTSPServerBase.h:97
void end()
Stop server and clean up.
Definition RTSPServerBase.h:76
int client_count
Definition RTSPServerBase.h:100
void acceptClient()
Accept new client if none is active.
Definition RTSPServerBase.h:108
unsigned long sessionTimeoutMs
Definition RTSPServerBase.h:104
void setOnSessionPath(bool(*cb)(const char *path, void *ref), void *ref=nullptr)
Set callback for session path.
Definition RTSPServerBase.h:41
RTSPAudioStreamerBase< Platform > * streamer
Definition RTSPServerBase.h:99
bool(* onSessionPathCb)(const char *, void *)
Definition RTSPServerBase.h:102
RTSP Session Handler - Individual Client Protocol Management.
Definition RTSPSession.h:77
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
void delay(unsigned long ms)
Definition Time.h:23
static HardwareSerial Serial
Definition NoArduino.h:186
uint32_t millis()
Returns the milliseconds since the start.
Definition Time.h:12
size_t writeData(Print *p_out, T *data, int samples, int maxSamples=512)
Definition AudioTypes.h:512