arduino-audio-tools
Loading...
Searching...
No Matches
RTSPAudioSource.h
1#pragma once
2
3#include "AudioTools/CoreAudio/AudioStreams.h"
4#include "IAudioSource.h"
5#include "RTSPFormat.h"
6#include "RTSPPlatform.h"
7
8
9namespace audio_tools {
10
24 public:
25 RTSPAudioSource() = default;
26
30 virtual ~RTSPAudioSource() {
31 TRACEI();
32 stop(); // Ensure we're properly stopped
33 m_magic = 0xDEADFACE; // Invalidate magic number
34 }
35
40 RTSPAudioSource(AudioStream &stream) { setInput(stream); }
41
47 RTSPAudioSource(Stream &stream, AudioInfo info) { setInput(stream, info); }
48
54 RTSPAudioSource(Stream &stream, RTSPFormat &format) {
55 setInput(stream);
56 setFormat(format);
57 }
58
63 void setInput(AudioStream &stream) {
64 p_stream = &stream;
65 p_audiostream = &stream;
66 }
67
73 void setInput(Stream &stream, AudioInfo info) {
74 p_stream = &stream;
75 p_audiostream = nullptr;
76 setAudioInfo(info);
77 }
78
83 void setInput(Stream &stream) {
84 p_stream = &stream;
85 p_audiostream = nullptr;
86 }
87
97 TRACEI();
98 default_format.begin(info);
99 if (p_audiostream) {
100 p_audiostream->setAudioInfo(info);
101 }
102 }
103
110 int readBytes(void *dest, int byteCount) override {
111 // Validate object integrity
112 if (m_magic != MAGIC_NUMBER) {
113 LOGE("RTSPAudioSource: invalid magic number 0x%08x, object corrupted",
114 m_magic);
115 return 0;
116 }
117
118 // Validate parameters
119 if (dest == nullptr || byteCount <= 0) {
120 LOGW("RTSPAudioSource: invalid parameters dest=%p byteCount=%d", dest,
121 byteCount);
122 return 0;
123 }
124
125 time_of_last_read = millis();
126
127 int result = 0;
128 LOGD("readDataTo: %d", byteCount);
129 if (started && p_stream) {
130 result = p_stream->readBytes((uint8_t *)dest, byteCount);
131 }
132 return result;
133 }
134
141 void start() override {
142 TRACEI();
143
144 // Validate object integrity
145 if (m_magic != MAGIC_NUMBER) {
146 LOGE("RTSPAudioSource: start called on corrupted object, magic=0x%08x",
147 m_magic);
148 return;
149 }
150
152 if (p_audiostream) {
153 p_audiostream->begin();
154 }
155 started = true;
156 }
157
164 void stop() override {
165 TRACEI();
166
167 // Validate object integrity (allow stop even if corrupted for cleanup)
168 if (m_magic != MAGIC_NUMBER) {
169 LOGW(
170 "RTSPAudioSource: stop called on corrupted object, magic=0x%08x, "
171 "proceeding anyway",
172 m_magic);
173 }
174
176 started = false;
177 if (p_audiostream) {
178 p_audiostream->end();
179 }
180 }
181
183 void setFragmentSize(int fragmentSize) {
184 getFormat().setFragmentSize(fragmentSize);
185 }
186
188 void setTimerPeriod(int period) { getFormat().setTimerPeriodUs(period); }
189
194 bool isActive() {
195 if (!started) return false;
196 if (timeout > 0) {
197 return millis() - time_of_last_read < timeout;
198 }
199 return true;
200 }
201
203 bool isStarted() { return started; }
204
205 void setFormat(RTSPFormat &format) { p_format = &format; }
206
207 RTSPFormat &getFormat() override { return *p_format; }
208
209 void setTimeout(uint16_t to) { timeout = to; }
210
211 protected:
212 const uint32_t MAGIC_NUMBER =
213 0xFEEDFACE; // Magic number for object validation
214 uint32_t m_magic = MAGIC_NUMBER; // Object validity marker
215 Stream *p_stream = nullptr;
216 AudioStream *p_audiostream = nullptr;
217 uint32_t time_of_last_read = 0;
218 bool started = false;
219 RTSPFormatPCM default_format; // Used for AudioStream sources
220 RTSPFormat *p_format = &default_format; // kept internally as pointer;
221 // externally exposed as reference
222 uint16_t timeout = 0;
223};
224
225} // namespace audio_tools
Base class for all Audio Streams. It support the boolean operator to test if the object is ready with...
Definition BaseStream.h:122
virtual void setAudioInfo(AudioInfo newInfo) override
Defines the input AudioInfo.
Definition BaseStream.h:130
Audio Source Interface - Contract for Audio Data Providers.
Definition IAudioSource.h:22
virtual void start()
Initialize audio source for streaming.
Definition IAudioSource.h:68
virtual void stop()
Cleanup audio source after streaming.
Definition IAudioSource.h:81
Unified RTSP Audio Source - Works with both Stream and AudioStream.
Definition RTSPAudioSource.h:23
RTSPFormat & getFormat() override
Get the audio format configuration.
Definition RTSPAudioSource.h:207
RTSPAudioSource(Stream &stream, RTSPFormat &format)
Construct with custom format.
Definition RTSPAudioSource.h:54
void start() override
Start the audio source.
Definition RTSPAudioSource.h:141
void setAudioInfo(AudioInfo info)
Set audio configuration manually.
Definition RTSPAudioSource.h:96
bool isStarted()
Returns true after start() has been called.
Definition RTSPAudioSource.h:203
void setInput(Stream &stream, AudioInfo info)
Set input from generic Stream with explicit audio info.
Definition RTSPAudioSource.h:73
void setTimerPeriod(int period)
Defines the timer period.
Definition RTSPAudioSource.h:188
void setFragmentSize(int fragmentSize)
Defines the fragment size.
Definition RTSPAudioSource.h:183
RTSPAudioSource(Stream &stream, AudioInfo info)
Construct from generic Stream with explicit audio info.
Definition RTSPAudioSource.h:47
bool isActive()
Check if source is actively being read (AudioStream only)
Definition RTSPAudioSource.h:194
RTSPAudioSource(AudioStream &stream)
Construct from AudioStream with automatic audio info detection.
Definition RTSPAudioSource.h:40
void stop() override
Stop the audio source.
Definition RTSPAudioSource.h:164
virtual ~RTSPAudioSource()
Destructor - Clean up resources.
Definition RTSPAudioSource.h:30
void setInput(AudioStream &stream)
Set input from AudioStream (with auto-detection)
Definition RTSPAudioSource.h:63
void setInput(Stream &stream)
Set input from generic Stream (audio info must be set separately)
Definition RTSPAudioSource.h:83
int readBytes(void *dest, int byteCount) override
Read audio data for RTSP streaming.
Definition RTSPAudioSource.h:110
Audio Format Definition - Base class for RTSP audio formats.
Definition RTSPFormat.h:40
void setTimerPeriodUs(int period)
Defines the timer period in microseconds.
Definition RTSPFormat.h:75
void setFragmentSize(int fragmentSize)
Defines the fragment size in bytes.
Definition RTSPFormat.h:61
Definition NoArduino.h:142
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
Basic Audio information which drives e.g. I2S.
Definition AudioTypes.h:55