arduino-audio-tools
Loading...
Searching...
No Matches
RTSPAudioSource.h
Go to the documentation of this file.
1#pragma once
2
3#include "AudioTools/CoreAudio/AudioStreams.h"
4#include "IAudioSource.h"
5#include "RTSPFormat.h"
6#include "RTSPPlatform.h"
7
16namespace audio_tools {
17
31 public:
32 RTSPAudioSource() = default;
33
37 virtual ~RTSPAudioSource() {
38 TRACEI();
39 stop(); // Ensure we're properly stopped
40 m_magic = 0xDEADFACE; // Invalidate magic number
41 }
42
47 RTSPAudioSource(AudioStream &stream) { setInput(stream); }
48
54 RTSPAudioSource(Stream &stream, AudioInfo info) { setInput(stream, info); }
55
61 RTSPAudioSource(Stream &stream, RTSPFormat &format) {
62 setInput(stream);
63 setFormat(format);
64 }
65
70 void setInput(AudioStream &stream) {
71 p_stream = &stream;
72 p_audiostream = &stream;
73 }
74
80 void setInput(Stream &stream, AudioInfo info) {
81 p_stream = &stream;
82 p_audiostream = nullptr;
83 setAudioInfo(info);
84 }
85
90 void setInput(Stream &stream) {
91 p_stream = &stream;
92 p_audiostream = nullptr;
93 }
94
104 TRACEI();
105 default_format.begin(info);
106 if (p_audiostream) {
107 p_audiostream->setAudioInfo(info);
108 }
109 }
110
117 int readBytes(void *dest, int byteCount) override {
118 // Validate object integrity
119 if (m_magic != MAGIC_NUMBER) {
120 LOGE("RTSPAudioSource: invalid magic number 0x%08x, object corrupted",
121 m_magic);
122 return 0;
123 }
124
125 // Validate parameters
126 if (dest == nullptr || byteCount <= 0) {
127 LOGW("RTSPAudioSource: invalid parameters dest=%p byteCount=%d", dest,
128 byteCount);
129 return 0;
130 }
131
132 time_of_last_read = millis();
133
134 int result = 0;
135 LOGD("readDataTo: %d", byteCount);
136 if (started && p_stream) {
137 result = p_stream->readBytes((uint8_t *)dest, byteCount);
138 }
139 return result;
140 }
141
148 void start() override {
149 TRACEI();
150
151 // Validate object integrity
152 if (m_magic != MAGIC_NUMBER) {
153 LOGE("RTSPAudioSource: start called on corrupted object, magic=0x%08x",
154 m_magic);
155 return;
156 }
157
159 if (p_audiostream) {
160 p_audiostream->begin();
161 }
162 started = true;
163 }
164
171 void stop() override {
172 TRACEI();
173
174 // Validate object integrity (allow stop even if corrupted for cleanup)
175 if (m_magic != MAGIC_NUMBER) {
176 LOGW(
177 "RTSPAudioSource: stop called on corrupted object, magic=0x%08x, "
178 "proceeding anyway",
179 m_magic);
180 }
181
183 started = false;
184 if (p_audiostream) {
185 p_audiostream->end();
186 }
187 }
188
190 void setFragmentSize(int fragmentSize) {
191 getFormat().setFragmentSize(fragmentSize);
192 }
193
195 void setTimerPeriod(int period) { getFormat().setTimerPeriodUs(period); }
196
201 bool isActive() {
202 if (!started) return false;
203 if (timeout > 0) {
204 return millis() - time_of_last_read < timeout;
205 }
206 return true;
207 }
208
210 bool isStarted() { return started; }
211
212 void setFormat(RTSPFormat &format) { p_format = &format; }
213
214 RTSPFormat &getFormat() override { return *p_format; }
215
216 void setTimeout(uint16_t to) { timeout = to; }
217
218 protected:
219 const uint32_t MAGIC_NUMBER =
220 0xFEEDFACE; // Magic number for object validation
221 uint32_t m_magic = MAGIC_NUMBER; // Object validity marker
222 Stream *p_stream = nullptr;
223 AudioStream *p_audiostream = nullptr;
224 uint32_t time_of_last_read = 0;
225 bool started = false;
226 RTSPFormatPCM default_format; // Used for AudioStream sources
227 RTSPFormat *p_format = &default_format; // kept internally as pointer;
228 // externally exposed as reference
229 uint16_t timeout = 0;
230};
231
232} // 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:23
virtual void start()
Initialize audio source for streaming.
Definition IAudioSource.h:69
virtual void stop()
Cleanup audio source after streaming.
Definition IAudioSource.h:82
Unified RTSP Audio Source - Works with both Stream and AudioStream.
Definition RTSPAudioSource.h:30
RTSPFormat & getFormat() override
Get the audio format configuration.
Definition RTSPAudioSource.h:214
RTSPAudioSource(Stream &stream, RTSPFormat &format)
Construct with custom format.
Definition RTSPAudioSource.h:61
void start() override
Start the audio source.
Definition RTSPAudioSource.h:148
void setAudioInfo(AudioInfo info)
Set audio configuration manually.
Definition RTSPAudioSource.h:103
bool isStarted()
Returns true after start() has been called.
Definition RTSPAudioSource.h:210
void setInput(Stream &stream, AudioInfo info)
Set input from generic Stream with explicit audio info.
Definition RTSPAudioSource.h:80
void setTimerPeriod(int period)
Defines the timer period.
Definition RTSPAudioSource.h:195
void setFragmentSize(int fragmentSize)
Defines the fragment size.
Definition RTSPAudioSource.h:190
RTSPAudioSource(Stream &stream, AudioInfo info)
Construct from generic Stream with explicit audio info.
Definition RTSPAudioSource.h:54
bool isActive()
Check if source is actively being read (AudioStream only)
Definition RTSPAudioSource.h:201
RTSPAudioSource(AudioStream &stream)
Construct from AudioStream with automatic audio info detection.
Definition RTSPAudioSource.h:47
void stop() override
Stop the audio source.
Definition RTSPAudioSource.h:171
virtual ~RTSPAudioSource()
Destructor - Clean up resources.
Definition RTSPAudioSource.h:37
void setInput(AudioStream &stream)
Set input from AudioStream (with auto-detection)
Definition RTSPAudioSource.h:70
void setInput(Stream &stream)
Set input from generic Stream (audio info must be set separately)
Definition RTSPAudioSource.h:90
int readBytes(void *dest, int byteCount) override
Read audio data for RTSP streaming.
Definition RTSPAudioSource.h:117
Audio Format Definition - Base class for RTSP audio formats.
Definition RTSPFormat.h:41
void setTimerPeriodUs(int period)
Defines the timer period in microseconds.
Definition RTSPFormat.h:76
void setFragmentSize(int fragmentSize)
Defines the fragment size in bytes.
Definition RTSPFormat.h:62
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:53