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
108
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
132 p_encoder->setOutput(memory_stream);
133 p_encoder->setAudioInfo(cfg);
134 p_encoder->begin();
135 // setup the RTSPSourceFromAudioStream
136 rtsp_source.setInput(memory_stream);
137 rtsp_source.setFormat(*p_format);
138 rtsp_source.setAudioInfo(cfg);
139 rtsp_source.start();
140
141 memory_stream.setConsumeOnRead(true);
142 memory_stream.begin();
143
144 // Initialize format with audio info (PCM or codec-specific)
145 p_format->begin(cfg);
146
147 return true;
148 }
149
153 void end() {
154 if (p_video_encoder != nullptr) {
155 p_video_encoder->stop();
156 return;
157 }
158 rtsp_source.stop();
159 memory_stream.end();
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 }
173 return rtsp_source.isStarted() ? memory_stream.availableForWrite() : 0;
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);
196 return rtsp_source.isActive() && memory_stream.availableForWrite() > 0;
197 }
198
199 protected:
200 // Core Components
205 &copy_encoder;
207 nullptr;
212};
213
214} // namespace audio_tools
#define TRACEI()
#define TRACED()
#define LOGE(...)
Encoding of PCM data.
Definition AudioCodecsBase.h:101
Abstract Audio Ouptut class.
Definition AudioOutput.h:25
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
Audio Format Definition - Base class for RTSP audio formats.
Definition RTSPFormat.h:57
Linear PCM Format for RTSP Streaming.
Definition RTSPFormat.h:169
Unified RTSP Media Source - Works with both Stream and AudioStream.
Definition RTSPMediaSource.h:31
RTSPMediaStreamer - Timer-driven RTP Media Streaming Engine.
Definition RTSPMediaStreamer.h:708
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
AudioInfo cfg
Definition CodecOpusOgg.h:88
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