Skip to content

Commit c589cff

Browse files
authored
Rtsp (#2174)
* CopyEncoder: prevent NPE * AudioInfo: bool() check for range * EncodedAudioStream: invalid AudioInfo * rtsp corrections * RTSP: integrate server * RTSP namespace * clean up platform * RTSP use template class fro platform * delete DefaultRTSPPlatform * Remove WiFiClient * AudioTimerESP32 * Cleanup * RTSPAudioStreamer: cleanup * RTSP: closing and restart fix * RtspSession: cleanup * FrameDurationSource: new class * RTSP dynamic timer update * MP3ParserDecoder, MP3ParserEncoder * RTSPFormatAudioTools audioInfo() * OpusAudioEncoder: trace on setAudioInfo() * USE_LEGACY_I2S;: ifndef * USE_LEGACY_I2S: make it idf version dependent * OpusAudioEncoder: rollback setAudioInfo trace * RTSPOutput: use of DynamicMemoryStream * RTSP compile errors * RTSP compile errors * RTSP cleanup * AudioTimerLinux * fix cmake build errors * cmake SD build errors * VSF documentation * MetaDataFilterEncoder * RTSP: compile errors * RTSP * RTSP: user WiFiServer * Task: Desktop support * rtsp * RTSPServer compile error Ethernet.h * RTSPAudioStreamerUsingTask: throttle interval * RTSPAudioStreamerUsingTask: throttling true by default * test: mp3-parser * HeaderParserMP3: corrections * rtsp * RTSP: mp3 corrections * RTSP: test * Task: prevent deadlock * RTSP: corrections for ffplay * RTSP: automitic rfc2250 header * RTPSession: cleanup
1 parent f7f96e2 commit c589cff

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+25743
-10675
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ build/
33
.vscode/
44
_deps/
55
.ipynb_checkpoints/
6-
.DS_Store
6+
.DS_Store
7+
miniaudio.h

examples/examples-communication/rtsp/communication-audiokit-rtsp/communication-audiokit-rtsp.ino

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,13 @@
1111

1212
#include "AudioTools.h"
1313
#include "AudioTools/AudioLibs/AudioBoardStream.h"
14-
#include "AudioTools/AudioLibs/RTSP.h"
15-
#include "AudioStreamer.h"
16-
#include "RTSPServer.h"
14+
#include "AudioTools/Communication/RTSP.h"
1715

1816
int port = 554;
1917
AudioBoardStream kit(AudioKitEs8388V1); // Audio source
20-
RTSPSourceFromAudioStream source(kit); // IAudioSource for RTSP
21-
AudioStreamer streamer = AudioStreamer(&source); // Stream audio via RTSP
22-
RTSPServer rtsp = RTSPServer(&streamer, port);
18+
RTSPAudioSource source(kit); // IAudioSource for RTSP
19+
RTSPAudioStreamer<RTSPPlatformWiFi> streamer(source); // Stream audio via RTSP
20+
RTSPServer<RTSPPlatformWiFi> rtsp(streamer, port);
2321

2422
const char* wifi = "wifi";
2523
const char* password = "password";

examples/examples-communication/rtsp/communication-codec-rtsp/communication-codec-rtsp.ino

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,8 @@
99
*
1010
*/
1111
#include "AudioTools.h"
12-
#include "AudioTools/AudioLibs/RTSP.h" // https://github.com/pschatzmann/Micro-RTSP-Audio
1312
#include "AudioTools/AudioCodecs/CodecG7xx.h" // https://github.com/pschatzmann/arduino-libg7xx.git
14-
#include "RTSPServer.h"
13+
#include "AudioTools/Communication/RTSP.h"
1514

1615
int port = 554;
1716
AudioInfo info(8000, 1, 16);
@@ -24,10 +23,10 @@ GeneratedSoundStream<int16_t> sound(sineWave); // Stream generated from sine wa
2423
// rtsp
2524
RTSPFormatG711 format;
2625
G711_ULAWEncoder encoder;
27-
RTSPOutput rtsp_stream(format, encoder);
26+
RTSPOutput<RTSPPlatformWiFi> rtsp_stream(format, encoder);
2827
StreamCopy copier(rtsp_stream, sound); // rtsp to sine
2928
// Server
30-
RTSPServer rtsp(rtsp_stream.streamer(), port);
29+
RTSPServer<RTSPPlatformWiFi> rtsp(*rtsp_stream.streamer(), port);
3130

3231

3332
void setup() {

examples/examples-communication/rtsp/communication-generator-rtsp/communication-generator-rtsp.ino

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,18 @@
1010
*/
1111

1212
#include "AudioTools.h"
13-
#include "AudioTools/AudioLibs/RTSP.h"
14-
#include "AudioStreamer.h"
15-
#include "RTSPServer.h"
13+
#include "AudioTools/Communication/RTSP.h"
1614

1715
int port = 554;
18-
int channels = 1;
19-
int sample_rate = 16000;
20-
int bits_per_sample = 16;
16+
AudioInfo info(16000,1,16); // AudioInfo for RTSP
2117
const char* wifi = "ssid";
2218
const char* password = "password";
2319

2420
SineFromTable<int16_t> sineWave(32000); // subclass of SoundGenerator with max amplitude of 32000
2521
GeneratedSoundStream<int16_t> sound(sineWave); // Stream generated from sine wave
26-
RTSPSourceFromAudioStream source(sound); // Stream sound via RTSP
27-
AudioStreamer streamer = AudioStreamer(&source);
28-
RTSPServer rtsp = RTSPServer(&streamer, port);
22+
RTSPAudioSource source(sound, info); // Stream sound via RTSP
23+
RTSPAudioStreamer<RTSPPlatformWiFi> streamer(source);
24+
RTSPServer<RTSPPlatformWiFi> rtsp(streamer, port);
2925

3026

3127
void setup() {
@@ -34,9 +30,7 @@ void setup() {
3430

3531
// Setup sine wave
3632
auto cfgS = sineWave.defaultConfig();
37-
cfgS.channels = channels;
38-
cfgS.sample_rate = sample_rate;
39-
cfgS.bits_per_sample = bits_per_sample;
33+
cfgS.copyFrom(info);
4034
sineWave.begin(cfgS, N_B4);
4135

4236
// Start Wifi & rtsp server
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
#pragma once
2+
3+
#include <stdint.h>
4+
5+
#include "AudioTools/Communication/RTSP.h"
6+
7+
class AudioTestSource : public IAudioSource {
8+
9+
public:
10+
AudioTestSource(){};
11+
12+
int readBytes(void* dest, int maxBytes) override {
13+
// Ensure we only read complete 16-bit samples
14+
int bytesToRead = (maxBytes / 2) * 2;
15+
int16_t* destSamples = static_cast<int16_t*>(dest);
16+
int samplesToRead = bytesToRead / 2;
17+
18+
for (int i = 0; i < samplesToRead; i++) {
19+
destSamples[i] = testData[index];
20+
index = (index + 1) % testDataSamples; // Safer modulo operation
21+
}
22+
return bytesToRead;
23+
}
24+
25+
void start() {
26+
log_i("AudioTestSource start");
27+
index = 0; // Reset to beginning for clean start
28+
}
29+
30+
void stop() {
31+
log_i("AudioTestSource stop");
32+
}
33+
34+
protected:
35+
int index = 0;
36+
static const int testDataSamples = 1024;
37+
38+
const int16_t testData[testDataSamples] = {
39+
0, 6398, 12551, 18220, 23188, 27263, 30288, 32146, 32767,
40+
32127, 30249, 27207, 23117, 18136, 12458, 6300, -100, -6497,
41+
-12644, -18304, -23259, -27318, -30326, -32166, -32767, -32107, -30210,
42+
-27150, -23045, -18052, -12365, -6201, 201, 6596, 12737, 18387,
43+
23330, 27374, 30364, 32185, 32767, 32087, 30171, 27094, 22973,
44+
17968, 12271, 6102, -301, -6694, -12829, -18470, -23400, -27429,
45+
-30402, -32204, -32766, -32066, -30132, -27037, -22902, -17884, -12178,
46+
-6003, 402, 6793, 12922, 18553, 23470, 27484, 30439, 32222,
47+
32764, 32045, 30092, 26980, 22830, 17800, 12085, 5904, -503,
48+
-6891, -13014, -18636, -23541, -27538, -30476, -32240, -32763, -32024,
49+
-30052, -26923, -22757, -17715, -11991, -5805, 603, 6989, 13106,
50+
18719, 23610, 27593, 30513, 32258, 32761, 32003, 30012, 26866,
51+
22685, 17630, 11897, 5706, -704, -7088, -13199, -18801, -23680,
52+
-27647, -30549, -32275, -32759, -31981, -29971, -26808, -22612, -17546,
53+
-11804, -5607, 804, 7186, 13291, 18884, 23750, 27701, 30586,
54+
32293, 32756, 31959, 29930, 26750, 22539, 17461, 11710, 5508,
55+
-905, -7284, -13383, -18966, -23819, -27755, -30622, -32310, -32754,
56+
-31936, -29889, -26692, -22466, -17375, -11616, -5409, 1006, 7382,
57+
13474, 19048, 23888, 27808, 30657, 32326, 32750, 31914, 29848,
58+
26633, 22393, 17290, 11521, 5309, -1106, -7480, -13566, -19130,
59+
-23956, -27861, -30693, -32343, -32747, -31891, -29806, -26574, -22319,
60+
-17204, -11427, -5210, 1207, 7578, 13658, 19211, 24025, 27914,
61+
30728, 32359, 32743, 31867, 29764, 26515, 22245, 17119, 11333,
62+
5111, -1307, -7676, -13749, -19293, -24093, -27966, -30763, -32374,
63+
-32739, -31844, -29722, -26456, -22171, -17033, -11238, -5011, 1408,
64+
7774, 13840, 19374, 24161, 28019, 30797, 32390, 32735, 31820,
65+
29680, 26397, 22097, 16947, 11144, 4912, -1508, -7871, -13931,
66+
-19455, -24229, -28071, -30831, -32405, -32730, -31796, -29637, -26337,
67+
-22023, -16860, -11049, -4812, 1609, 7969, 14022, 19536, 24297,
68+
28123, 30865, 32419, 32725, 31771, 29594, 26277, 21948, 16774,
69+
10954, 4713, -1709, -8067, -14113, -19616, -24364, -28174, -30899,
70+
-32434, -32720, -31747, -29550, -26217, -21873, -16688, -10859, -4613,
71+
1810, 8164, 14204, 19697, 24432, 28225, 30932, 32448, 32715,
72+
31721, 29507, 26156, 21798, 16601, 10764, 4513, -1910, -8262,
73+
-14295, -19777, -24498, -28276, -30965, -32462, -32709, -31696, -29463,
74+
-26095, -21723, -16514, -10669, -4414, 2011, 8359, 14385, 19857,
75+
24565, 28327, 30998, 32476, 32703, 31670, 29419, 26034, 21647,
76+
16427, 10574, 4314, -2111, -8456, -14475, -19937, -24632, -28377,
77+
-31031, -32489, -32696, -31644, -29374, -25973, -21572, -16340, -10479,
78+
-4214, 2212, 8553, 14566, 20017, 24698, 28428, 31063, 32502,
79+
32689, 31618, 29330, 25912, 21496, 16253, 10383, 4114, -2312,
80+
-8650, -14656, -20097, -24764, -28478, -31095, -32514, -32682, -31592,
81+
-29285, -25850, -21420, -16165, -10288, -4015, 2412, 8747, 14746,
82+
20176, 24830, 28527, 31126, 32527, 32675, 31565, 29239, 25788,
83+
21344, 16078, 10192, 3915, -2513, -8844, -14835, -20255, -24895,
84+
-28577, -31157, -32539, -32667, -31538, -29194, -25726, -21267, -15990,
85+
-10097, -3815, 2613, 8941, 14925, 20334, 24961, 28626, 31189,
86+
32550, 32659, 31510, 29148, 25663, 21191, 15902, 10001, 3715,
87+
-2713, -9038, -15015, -20413, -25026, -28674, -31219, -32562, -32651,
88+
-31482, -29102, -25601, -21114, -15814, -9905, -3615, 2814, 9135,
89+
15104, 20492, 25090, 28723, 31250, 32573, 32642, 31454, 29055,
90+
25538, 21037, 15726, 9809, 3515, -2914, -9231, -15193, -20570,
91+
-25155, -28771, -31280, -32584, -32633, -31426, -29009, -25475, -20959,
92+
-15637, -9713, -3415, 3014, 9328, 15282, 20648, 25219, 28819,
93+
31310, 32594, 32624, 31397, 28962, 25411, 20882, 15549, 9617,
94+
3315, -3114, -9424, -15371, -20726, -25284, -28867, -31339, -32604,
95+
-32614, -31368, -28915, -25347, -20804, -15460, -9521, -3214, 3214,
96+
9521, 15460, 20804, 25347, 28915, 31368, 32614, 32604, 31339,
97+
28867, 25284, 20726, 15371, 9424, 3114, -3315, -9617, -15549,
98+
-20882, -25411, -28962, -31397, -32624, -32594, -31310, -28819, -25219,
99+
-20648, -15282, -9328, -3014, 3415, 9713, 15637, 20959, 25475,
100+
29009, 31426, 32633, 32584, 31280, 28771, 25155, 20570, 15193,
101+
9231, 2914, -3515, -9809, -15726, -21037, -25538, -29055, -31454,
102+
-32642, -32573, -31250, -28723, -25090, -20492, -15104, -9135, -2814,
103+
3615, 9905, 15814, 21114, 25601, 29102, 31482, 32651, 32562,
104+
31219, 28674, 25026, 20413, 15015, 9038, 2713, -3715, -10001,
105+
-15902, -21191, -25663, -29148, -31510, -32659, -32550, -31189, -28626,
106+
-24961, -20334, -14925, -8941, -2613, 3815, 10097, 15990, 21267,
107+
25726, 29194, 31538, 32667, 32539, 31157, 28577, 24895, 20255,
108+
14835, 8844, 2513, -3915, -10192, -16078, -21344, -25788, -29239,
109+
-31565, -32675, -32527, -31126, -28527, -24830, -20176, -14746, -8747,
110+
-2412, 4015, 10288, 16165, 21420, 25850, 29285, 31592, 32682,
111+
32514, 31095, 28478, 24764, 20097, 14656, 8650, 2312, -4114,
112+
-10383, -16253, -21496, -25912, -29330, -31618, -32689, -32502, -31063,
113+
-28428, -24698, -20017, -14566, -8553, -2212, 4214, 10479, 16340,
114+
21572, 25973, 29374, 31644, 32696, 32489, 31031, 28377, 24632,
115+
19937, 14475, 8456, 2111, -4314, -10574, -16427, -21647, -26034,
116+
-29419, -31670, -32703, -32476, -30998, -28327, -24565, -19857, -14385,
117+
-8359, -2011, 4414, 10669, 16514, 21723, 26095, 29463, 31696,
118+
32709, 32462, 30965, 28276, 24498, 19777, 14295, 8262, 1910,
119+
-4513, -10764, -16601, -21798, -26156, -29507, -31721, -32715, -32448,
120+
-30932, -28225, -24432, -19697, -14204, -8164, -1810, 4613, 10859,
121+
16688, 21873, 26217, 29550, 31747, 32720, 32434, 30899, 28174,
122+
24364, 19616, 14113, 8067, 1709, -4713, -10954, -16774, -21948,
123+
-26277, -29594, -31771, -32725, -32419, -30865, -28123, -24297, -19536,
124+
-14022, -7969, -1609, 4812, 11049, 16860, 22023, 26337, 29637,
125+
31796, 32730, 32405, 30831, 28071, 24229, 19455, 13931, 7871,
126+
1508, -4912, -11144, -16947, -22097, -26397, -29680, -31820, -32735,
127+
-32390, -30797, -28019, -24161, -19374, -13840, -7774, -1408, 5011,
128+
11238, 17033, 22171, 26456, 29722, 31844, 32739, 32374, 30763,
129+
27966, 24093, 19293, 13749, 7676, 1307, -5111, -11333, -17119,
130+
-22245, -26515, -29764, -31867, -32743, -32359, -30728, -27914, -24025,
131+
-19211, -13658, -7578, -1207, 5210, 11427, 17204, 22319, 26574,
132+
29806, 31891, 32747, 32343, 30693, 27861, 23956, 19130, 13566,
133+
7480, 1106, -5309, -11521, -17290, -22393, -26633, -29848, -31914,
134+
-32750, -32326, -30657, -27808, -23888, -19048, -13474, -7382, -1006,
135+
5409, 11616, 17375, 22466, 26692, 29889, 31936, 32754, 32310,
136+
30622, 27755, 23819, 18966, 13383, 7284, 905, -5508, -11710,
137+
-17461, -22539, -26750, -29930, -31959, -32756, -32293, -30586, -27701,
138+
-23750, -18884, -13291, -7186, -804, 5607, 11804, 17546, 22612,
139+
26808, 29971, 31981, 32759, 32275, 30549, 27647, 23680, 18801,
140+
13199, 7088, 704, -5706, -11897, -17630, -22685, -26866, -30012,
141+
-32003, -32761, -32258, -30513, -27593, -23610, -18719, -13106, -6989,
142+
-603, 5805, 11991, 17715, 22757, 26923, 30052, 32024, 32763,
143+
32240, 30476, 27538, 23541, 18636, 13014, 6891, 503, -5904,
144+
-12085, -17800, -22830, -26980, -30092, -32045, -32764, -32222, -30439,
145+
-27484, -23470, -18553, -12922, -6793, -402, 6003, 12178, 17884,
146+
22902, 27037, 30132, 32066, 32766, 32204, 30402, 27429, 23400,
147+
18470, 12829, 6694, 301, -6102, -12271, -17968, -22973, -27094,
148+
-30171, -32087, -32767, -32185, -30364, -27374, -23330, -18387, -12737,
149+
-6596, -201, 6201, 12365, 18052, 23045, 27150, 30210, 32107,
150+
32767, 32166, 30326, 27318, 23259, 18304, 12644, 6497, 100,
151+
-6300, -12458, -18136, -23117, -27207, -30249, -32127, -32767, -32146,
152+
-30288, -27263, -23188, -18220, -12551, -6398, 0,
153+
};
154+
};
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// a minimal RTSP server example
2+
#include "AudioTools.h"
3+
#include "AudioTools/Communication/RTSP.h"
4+
#include "AudioTestSource.h"
5+
6+
const char* ssid = "ssid";
7+
const char* password = "password";
8+
9+
int port = 554;
10+
AudioTestSource testSource;
11+
DefaultRTSPAudioStreamer<RTSPPlatformWiFi> streamer(testSource);
12+
DefaultRTSPServer<RTSPPlatformWiFi> rtsp(streamer, port);
13+
14+
void setup() {
15+
Serial.begin(114200);
16+
rtsp.begin(ssid, password);
17+
}
18+
19+
void loop() { delay(1000); }
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
2+
// Use the AudioPlayer to publish mp3 data as-is
3+
4+
#include "AudioTools.h"
5+
#include "AudioTools/Disk/AudioSourceSDMMC.h"
6+
#include "AudioTools/Communication/RTSP.h"
7+
#include "AudioTools/AudioCodecs/MP3Parser.h"
8+
9+
int port = 554;
10+
const char* wifi = "SSID";
11+
const char* password = "password";
12+
13+
// rtsp
14+
RTSPFormatMP3 mp3format; // RTSP mp3
15+
MP3ParserEncoder enc; // mp3 packaging
16+
MetaDataFilterEncoder filter(enc);
17+
RTSPOutput<RTSPPlatformWiFi> rtsp_out(mp3format, filter);
18+
AudioSourceSDMMC source("/", ".mp3");
19+
CopyDecoder dec; // no decoding, just copy
20+
AudioPlayer player(source, rtsp_out, dec);
21+
RTSPServer<RTSPPlatformWiFi> rtsp(rtsp_out.streamer(), port);
22+
23+
24+
void setup() {
25+
Serial.begin(115200);
26+
AudioToolsLogger.begin(Serial, AudioToolsLogLevel::Warning);
27+
28+
// no delay between mp3 files
29+
source.setTimeoutAutoNext(0);
30+
31+
// start the player
32+
player.begin();
33+
34+
// Start Output Stream
35+
rtsp_out.begin();
36+
37+
// Start Wifi & rtsp server
38+
rtsp.begin(wifi, password);
39+
40+
}
41+
42+
void loop() {
43+
if (rtsp_out) {
44+
player.copy();
45+
}
46+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
2+
// Use the AudioPlayer to decode mp3 and publish as adpcm data
3+
4+
#include "AudioTools.h"
5+
#include "AudioTools/Disk/AudioSourceSDMMC.h"
6+
#include "AudioTools/AudioCodecs/CodecMP3Helix.h"
7+
#include "AudioTools/AudioCodecs/CodecADPCM.h"
8+
#include "AudioTools/Communication/RTSP.h"
9+
10+
int port = 554;
11+
const char* wifi = "SSID";
12+
const char* password = "password";
13+
14+
// mp3 data
15+
AudioSourceSDMMC source("/", ".mp3");
16+
MP3DecoderHelix mp3; // no decoding, just copy
17+
// rtsp
18+
ADPCMEncoder adpcm(AV_CODEC_ID_ADPCM_IMA_WAV, 512); // ima adpcm encoder
19+
RTSPFormatADPCM<ADPCMEncoder> adpcm_format(adpcm); // RTSP adpcm: provide info from encoder
20+
RTSPOutput<RTSPPlatformWiFi> rtsp_out(adpcm_format, adpcm);
21+
FormatConverterStream convert(rtsp_out);
22+
RTSPServer<RTSPPlatformWiFi> rtsp(rtsp_out.streamer(), port);
23+
// player
24+
AudioPlayer player(source, convert, mp3);
25+
26+
27+
void setup() {
28+
Serial.begin(115200);
29+
AudioToolsLogger.begin(Serial, AudioToolsLogLevel::Warning);
30+
31+
// no delay between mp3 files
32+
source.setTimeoutAutoNext(0);
33+
34+
// start the player
35+
player.begin();
36+
37+
// Start Output Stream
38+
rtsp_out.begin();
39+
40+
// convert the data format
41+
convert.begin(mp3.audioInfo(), rtsp_out.audioInfo());
42+
43+
// Start Wifi & rtsp server
44+
rtsp.begin(wifi, password);
45+
46+
}
47+
48+
void loop() {
49+
if (rtsp_out) {
50+
player.copy();
51+
}
52+
}

src/AudioTools/AudioCodecs/AudioCodecsBase.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ class ContainerDecoder : public AudioDecoder {
8787
bool isResultPCM() override { return true; }
8888
};
8989

90+
9091
/**
9192
* @brief Encoding of PCM data
9293
* @ingroup codecs
@@ -104,6 +105,12 @@ class AudioEncoder : public AudioWriter {
104105
/// Defines the sample rate, number of channels and bits per sample
105106
void setAudioInfo(AudioInfo from) override { info = from; }
106107
AudioInfo audioInfo() override { return info; }
108+
/// Default output assignment (encoders may override to store Print reference)
109+
virtual void setOutput(Print &out_stream) override { (void)out_stream; }
110+
/// Optioinal rtsp function: provide the frame duration in microseconds
111+
virtual uint32_t frameDurationUs() { return 0;};
112+
/// Optioinal rtsp function: provide samples per the frame
113+
virtual uint16_t samplesPerFrame() { return 0;};
107114

108115
protected:
109116
AudioInfo info;

0 commit comments

Comments
 (0)