Skip to content

Commit 2f7f5e8

Browse files
committed
RTSP: cleanup
1 parent c589cff commit 2f7f5e8

File tree

3 files changed

+59
-45
lines changed

3 files changed

+59
-45
lines changed

src/AudioTools/Communication/RTSP/RTSPFormat.h

Lines changed: 40 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ namespace audio_tools {
3434
*
3535
* @note Implementations must provide format() method for SDP generation
3636
* @ingroup rtsp
37-
* @author Thomas Pfitzinger
37+
* @author Phil Schatzmann
3838
* @version 0.1.1
3939
*/
4040

@@ -131,20 +131,6 @@ class RTSPFormatPCM : public RTSPFormat {
131131
setTimerPeriodUs(getTimerPeriod(this->fragment_size));
132132
}
133133

134-
/**
135-
* @brief Get the timer period for streaming
136-
* @return Timer period in microseconds between audio packets
137-
*/
138-
int getTimerPeriod(int fragmentSize) {
139-
// Calculate how many samples are in the fragment
140-
int samples_per_fragment = timestampIncrement();
141-
// Calculate how long it takes to play these samples at the given sample
142-
// rate
143-
int timer_period =
144-
(samples_per_fragment * 1000000) / cfg.sample_rate; // microseconds
145-
return timer_period;
146-
}
147-
148134
/**
149135
* @brief Provide format 10 or 11
150136
*
@@ -160,8 +146,7 @@ class RTSPFormatPCM : public RTSPFormat {
160146
"m=audio 0 RTP/AVP %d\r\n" // UDP sessions with format 10 or 11
161147
"a=rtpmap:%s\r\n"
162148
"a=rate:%i\r\n", // provide sample rate
163-
format(channels()), payloadFormat(sampleRate(), channels()),
164-
sampleRate());
149+
rtpPayloadType(), payloadFormat(), sampleRate());
165150
LOGI("ftsp format: %s", buffer);
166151
return (const char *)buffer;
167152
}
@@ -176,58 +161,70 @@ class RTSPFormatPCM : public RTSPFormat {
176161
// convert to network format (big endian)
177162
int16_t *pt_16 = (int16_t *)data;
178163
for (int j = 0; j < samples / 2; j++) {
179-
uint16_t v = (uint16_t)pt_16[j];
180-
v = (uint16_t)((v << 8) | (v >> 8));
181-
pt_16[j] = (int16_t)v;
164+
pt_16[j] = htons(pt_16[j]);
182165
}
183166
return samples;
184167
}
185168

186169
AudioInfo info() { return cfg; }
187170

188-
int sampleRate() { return cfg.sample_rate; }
189-
int channels() { return cfg.channels; }
190-
int bytesPerSample() { return cfg.bits_per_sample / 8; }
191-
192171
AudioInfo defaultConfig() override { return AudioInfo(16000, 1, 16); }
193172

194-
protected:
195-
char payload_fromat[30];
196-
197-
const char *payloadFormat(int sampleRate, int channels) {
198-
// see https://en.wikipedia.org/wiki/RTP_payload_formats
199-
// 11 L16/%i/%i
200-
201-
switch (channels) {
173+
int rtpPayloadType() override {
174+
int result = 0;
175+
switch (channels()) {
202176
case 1:
203-
snprintf(payload_fromat, 30, "%d L16/%i/%i", format(channels),
204-
sampleRate, channels);
177+
result = 11;
205178
break;
206179
case 2:
207-
snprintf(payload_fromat, 30, "%d L16/%i/%i", format(channels),
208-
sampleRate, channels);
180+
result = 10;
209181
break;
210182
default:
211183
LOGE("unsupported audio type");
212184
break;
213185
}
214-
return payload_fromat;
186+
return result;
215187
}
216188

217-
int format(int channels) {
218-
int result = 0;
219-
switch (channels) {
189+
protected:
190+
char payload_fromat[30];
191+
192+
int sampleRate() { return cfg.sample_rate; }
193+
int channels() { return cfg.channels; }
194+
int bytesPerSample() { return cfg.bits_per_sample / 8; }
195+
196+
/**
197+
* @brief Get the timer period for streaming
198+
* @return Timer period in microseconds between audio packets
199+
*/
200+
int getTimerPeriod(int fragmentSize) {
201+
// Calculate how many samples are in the fragment
202+
int samples_per_fragment = timestampIncrement();
203+
// Calculate how long it takes to play these samples at the given sample
204+
// rate
205+
int timer_period =
206+
(samples_per_fragment * 1000000) / cfg.sample_rate; // microseconds
207+
return timer_period;
208+
}
209+
210+
211+
// see https://en.wikipedia.org/wiki/RTP_payload_formats
212+
// 11 L16/%i/%i
213+
const char *payloadFormat() {
214+
switch (channels()) {
220215
case 1:
221-
result = 11;
216+
snprintf(payload_fromat, 30, "%d L16/%i/%i", rtpPayloadType(),
217+
sampleRate(), channels());
222218
break;
223219
case 2:
224-
result = 10;
220+
snprintf(payload_fromat, 30, "%d L16/%i/%i", rtpPayloadType(),
221+
sampleRate(), channels());
225222
break;
226223
default:
227224
LOGE("unsupported audio type");
228225
break;
229226
}
230-
return result;
227+
return payload_fromat;
231228
}
232229
};
233230

src/AudioTools/Communication/RTSP/RTSPPlatformEthernet.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,16 @@
55

66
namespace audio_tools {
77

8-
// Default platform specialization for Arduino WiFi
8+
/**
9+
* @brief RTSP platform binding for Arduino Ethernet
10+
*
11+
* Convenience type alias that binds the generic `RTSPPlatform` to the
12+
* Arduino Ethernet networking stack (`EthernetServer`, `EthernetClient`,
13+
* `EthernetUDP`). Use this when running the RTSP server over Ethernet-
14+
* capable boards.
15+
*
16+
* @ingroup rtsp
17+
*/
918
using RTSPPlatformEthernet = RTSPPlatform<EthernetServer, EthernetClient, EthernetUDP>;
1019

1120
} // namespace audio_tools

src/AudioTools/Communication/RTSP/RTSPPlatformWiFi.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,15 @@
44

55
namespace audio_tools {
66

7-
// Default platform specialization for Arduino WiFi
7+
/**
8+
* @brief RTSP platform binding for Arduino WiFi
9+
*
10+
* Convenience type alias that binds the generic `RTSPPlatform` to the
11+
* Arduino WiFi networking stack (`WiFiServer`, `WiFiClient`, `WiFiUDP`).
12+
* Use this when running the RTSP server over WiFi-capable boards.
13+
*
14+
* @ingroup rtsp
15+
*/
816
using RTSPPlatformWiFi = RTSPPlatform<WiFiServer, WiFiClient, WiFiUDP>;
917

1018
} // namespace audio_tools

0 commit comments

Comments
 (0)