arduino-audio-tools
Loading...
Searching...
No Matches
RTSPOutput.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 */
9#pragma once
10
13#include "RTSPMediaSource.h"
14#include "RTSPMediaStreamer.h"
15#include "RTSPFormat.h"
16#include "RTSPVideoEncoder.h"
17
18namespace audio_tools {
19
34template <typename Platform>
35class RTSPOutput : public AudioOutput {
36 public:
45 RTSPOutput(RTSPFormat &format, AudioEncoder &encoder) {
46 setFormat(format);
47 rtsp_streamer.setMediaSource(&rtsp_source);
48 p_encoder = &encoder;
49 }
50
66 setFormat(format);
67 p_encoder = &encoder;
68 p_video_encoder = &encoder;
69 encoder.setFormat(format);
70 }
71
72 void setFormat(RTSPFormat &format) {
73 TRACEI();
74 p_format = &format;
75 rtsp_source.setFormat(format);
76 }
77
83 RTSPOutput() = default;
84
91
98 bool begin(AudioInfo info) {
99 cfg = info;
100 return begin();
101 }
102
109 bool begin() {
110 TRACEI();
111 if (p_encoder == nullptr) {
112 LOGE("encoder is null");
113 return false;
114 }
115 if (p_format == nullptr) {
116 LOGE("format is null");
117 return false;
118 }
119
120 if (p_video_encoder != nullptr) {
121 // Packetized (video) path: the encoder owns fragmentation, framing
122 // and the RTP marker bit; the streamer only forwards its already
123 // ready-to-send fragments.
124 p_encoder->begin();
125 rtsp_streamer.setMediaSource(p_video_encoder);
126 return true;
127 }
128
129 // setup the RTSPMediaStreamer
130 cfg.logInfo();
131
134 p_encoder->begin();
135 // setup the RTSPSourceFromAudioStream
140
143
144 // Initialize format with audio info (PCM or codec-specific)
146
147 return true;
148 }
149
153 void end() {
154 if (p_video_encoder != nullptr) {
156 return;
157 }
160 }
161
168 if (p_video_encoder != nullptr) {
169 // Video frames are pushed on capture, not rate-limited by an
170 // intermediate buffer; report "ready" whenever the encoder is started.
171 return (bool)(*p_video_encoder) ? 0x7FFFFFFF : 0;
172 }
174 }
175
183 size_t write(const uint8_t *data, size_t len) override {
184 TRACED();
185 size_t result = p_encoder->write(data, len);
186 return result;
187 }
188
194 operator bool() {
195 if (p_video_encoder != nullptr) return (bool)(*p_video_encoder);
197 }
198
199 protected:
200 // Core Components
205 &copy_encoder;
207 nullptr;
212};
213
214} // namespace audio_tools
#define TRACEI()
Definition AudioLoggerIDF.h:32
#define TRACED()
Definition AudioLoggerIDF.h:31
#define LOGE(...)
Definition AudioLoggerIDF.h:30
Encoding of PCM data.
Definition AudioCodecsBase.h:101
void setAudioInfo(AudioInfo from) override
Defines the sample rate, number of channels and bits per sample.
Definition AudioCodecsBase.h:110
virtual void setOutput(Print &out_stream) override
Default output assignment (encoders may override to store Print reference)
Definition AudioCodecsBase.h:113
Abstract Audio Ouptut class.
Definition AudioOutput.h:25
AudioInfo cfg
Definition AudioOutput.h:88
virtual size_t write(const uint8_t *data, size_t len)=0
virtual bool begin()=0
Dummy Encoder which just copies the provided data to the output.
Definition CodecCopy.h:73
MemoryStream which is written and read using the internal RAM. For each write the data is allocated o...
Definition BaseStream.h:522
virtual bool begin() override
Intializes the processing.
Definition BaseStream.h:544
virtual int availableForWrite() override
Definition BaseStream.h:611
void setConsumeOnRead(bool consume)
Enable or disable consuming reads (remove records as they are read)
Definition BaseStream.h:560
virtual void end() override
Definition BaseStream.h:550
virtual void stop()
Cleanup media source after streaming.
Definition IMediaSource.h:78
Audio Format Definition - Base class for RTSP audio formats.
Definition RTSPFormat.h:57
virtual void begin(AudioInfo info)
Definition RTSPFormat.h:67
Linear PCM Format for RTSP Streaming.
Definition RTSPFormat.h:169
Unified RTSP Media Source - Works with both Stream and AudioStream.
Definition RTSPMediaSource.h:31
void start() override
Start the audio source.
Definition RTSPMediaSource.h:149
void setAudioInfo(AudioInfo info)
Set audio configuration manually.
Definition RTSPMediaSource.h:104
bool isStarted()
Returns true after start() has been called.
Definition RTSPMediaSource.h:211
bool isActive()
Check if source is actively being read (AudioStream only)
Definition RTSPMediaSource.h:202
void stop() override
Stop the audio source.
Definition RTSPMediaSource.h:172
void setInput(AudioStream &stream)
Set input from AudioStream (with auto-detection)
Definition RTSPMediaSource.h:71
void setFormat(RTSPFormat &format)
Definition RTSPMediaSource.h:213
RTSPMediaStreamer - Timer-driven RTP Media Streaming Engine.
Definition RTSPMediaStreamer.h:708
RTSPOutput - Audio Output Stream for RTSP Streaming.
Definition RTSPOutput.h:35
CopyEncoder copy_encoder
Pass-through encoder for PCM mode.
Definition RTSPOutput.h:201
bool begin()
Initialize RTSPOutput with current audio configuration.
Definition RTSPOutput.h:109
RTSPFormatPCM pcm
Default PCM format handler (merged class)
Definition RTSPOutput.h:208
RTSPOutput()=default
Construct RTSPOutput with default PCM format (no encoding)
RTSPMediaStreamer< Platform > rtsp_streamer
Handles RTP packet transmission.
Definition RTSPOutput.h:211
size_t write(const uint8_t *data, size_t len) override
Write PCM audio data for encoding and streaming.
Definition RTSPOutput.h:183
int availableForWrite()
Get available space for writing audio data.
Definition RTSPOutput.h:167
RTSPMediaStreamer< Platform > & streamer()
Get access to the underlying RTSP streamer.
Definition RTSPOutput.h:90
DynamicMemoryStream memory_stream
Memory stream for internal buffer.
Definition RTSPOutput.h:203
RTSPOutput(RTSPFormat &format, RTSPVideoEncoder &encoder)
Construct RTSPOutput for video streaming via any RTSPVideoEncoder (JPEGRtpEncoder,...
Definition RTSPOutput.h:65
RTSPVideoEncoder * p_video_encoder
Set for a packetized (video) constructor.
Definition RTSPOutput.h:206
void end()
Stop RTSP streaming and cleanup resources.
Definition RTSPOutput.h:153
RTSPOutput(RTSPFormat &format, AudioEncoder &encoder)
Construct RTSPOutput with specific encoder and format.
Definition RTSPOutput.h:45
AudioEncoder * p_encoder
Active encoder (PCM or codec-specific)
Definition RTSPOutput.h:204
RTSPMediaSource rtsp_source
Provides encoded audio to streamer.
Definition RTSPOutput.h:202
void setFormat(RTSPFormat &format)
Definition RTSPOutput.h:72
RTSPFormat * p_format
Active format handler.
Definition RTSPOutput.h:209
bool begin(AudioInfo info)
Initialize RTSPOutput with specific audio configuration.
Definition RTSPOutput.h:98
Common base for RTP video payloaders (JPEGRtpEncoder, H264RtpEncoder, ...): an AudioEncoder that acce...
Definition RTSPVideoEncoder.h:31
virtual void setFormat(RTSPFormat &format)=0
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition LMSEchoCancellationStream.h:6
Basic Audio information which drives e.g. I2S.
Definition AudioTypes.h:56
virtual void logInfo(const char *source="")
Definition AudioTypes.h:127