3 #include "AudioStreamer.h"
4 #include "AudioTools/CoreAudio/AudioPlayer.h"
5 #include "AudioTools/CoreAudio/AudioStreams.h"
6 #include "IAudioSource.h"
7 #include "RTSPServer.h"
30 virtual void begin(
AudioStream &stream) { p_stream = &stream; }
33 int getSampleSizeBytes()
override {
53 virtual void begin(
AudioInfo info) { this->info = info; }
54 int getSampleRate()
override {
return info.
sample_rate; }
55 int getChannels()
override {
return info.
channels; }
57 virtual void setAudioInfo(
AudioInfo ai) { info = ai; }
90 void setInput(AudioStream &stream) {
91 p_audiostream = &stream;
92 pcmInfo.begin(stream);
113 virtual int readBytes(
void *dest,
int byteCount)
override {
114 time_of_last_read =
millis();
116 LOGD(
"readDataTo: %d", byteCount);
118 result = p_audiostream->readBytes((uint8_t *)dest, byteCount);
123 virtual RTSPFormat *getFormat()
override {
return &format; }
130 IAudioSource::start();
131 p_audiostream->begin();
141 p_audiostream->end();
144 void setFragmentSize(
int fragmentSize) {
145 format.setFragmentSize(fragmentSize);
148 void setTimerPeriod(
int period) { format.setTimerPeriod(period); }
159 uint32_t time_of_last_read = 0;
184 rtp_info.begin(info);
208 rtp_info.setAudioInfo(info);
212 virtual RTSPFormat *getFormat()
override {
return &format; }
218 virtual int readBytes(
void *dest,
int byteCount)
override {
220 LOGD(
"readDataTo: %d", byteCount);
222 result = p_stream->readBytes((uint8_t *)dest, byteCount);
241 void setFragmentSize(
int fragmentSize) {
242 format.setFragmentSize(fragmentSize);
245 void setTimerPeriod(
int period) { format.setTimerPeriod(period); }
248 Stream *p_stream =
nullptr;
250 RTSPPCMAudioInfo rtp_info;
251 RTSPFormatPCM format{rtp_info};
261 virtual void begin(
AudioInfo info) { cfg = info; }
263 virtual const char *
format(
char *buffer,
int len) = 0;
275 void setFragmentSize(
int fragmentSize) { RTSPFormat::setFragmentSize(fragmentSize);}
276 int fragmentSize() {
return RTSPFormat::fragmentSize(); }
278 void setTimerPeriod(
int period) { RTSPFormat::setTimerPeriod(period); }
279 int timerPeriod() {
return RTSPFormat::timerPeriod(); }
283 const char* name_str=
"RTSP-Demo";;
298 const char *
format(
char *buffer,
int len)
override {
300 snprintf(buffer, len,
302 "c=IN IP4 0.0.0.0\r\n"
304 "m=audio 0 RTP/AVP 101\r\n"
305 "a=rtpmap:101 opus/%d/2\r\n"
306 "a=fmtp:101 stereo=1; sprop-stereo=%d\r\n",
308 return (
const char *)buffer;
328 const char *
format(
char *buffer,
int len)
override {
330 snprintf(buffer, len,
332 "c=IN IP4 0.0.0.0\r\n"
334 "m=audio 0 RTP/AVP 98\r\n"
335 "a=rtpmap:98 aptx/%d/%d\r\n"
336 "a=fmtp:98 variant=standard; bitresolution=%d\r\n",
338 return (
const char *)buffer;
355 const char *
format(
char *buffer,
int len)
override {
360 snprintf(buffer, len,
362 "c=IN IP4 0.0.0.0\r\n"
364 "m=audio 0 RTP/AVP 3\r\n"
366 return (
const char *)buffer;
385 const char *
format(
char *buffer,
int len)
override {
390 snprintf(buffer, len,
392 "c=IN IP4 0.0.0.0\r\n"
394 "m=audio 0 RTP/AVP %d\r\n"
395 ,
name(), getFormat());
396 return (
const char *)buffer;
409 uint8_t getFormat() {
return is_ulaw ? 0 : 8; }
421 const char *
format(
char *buffer,
int len)
override {
423 snprintf(buffer, len,
425 "c=IN IP4 0.0.0.0\r\n"
427 "m=audio 0 RTP/AVP %d\r\n"
432 return (
const char *)buffer;
441 char payload_fromat[30];
444 int format(
int channels) {
454 LOGE(
"unsupported audio type");
460 const char *payloadFormat(
int sampleRate,
int channels) {
466 snprintf(payload_fromat, 30,
"%d L16/%i/%i",
format(channels), sampleRate,
470 snprintf(payload_fromat, 30,
"%d L16/%i/%i",
format(channels), sampleRate,
474 LOGE(
"unsupported audio type");
477 return payload_fromat;
489 const char *
format(
char *buffer,
int len)
override {
491 snprintf(buffer, len,
493 "c=IN IP4 0.0.0.0\r\n"
495 "m=audio 0 RTP/AVP 96\r\n"
496 "a=rtpmap:96 l8/%d/%d\r\n",
498 return (
const char *)buffer;
520 int buffer_size = 1024 * 2) {
521 buffer.resize(buffer_size);
523 p_encoder = &encoder;
524 p_encoder->setOutput(buffer);
529 buffer.resize(buffer_size);
530 p_encoder->setOutput(buffer);
533 AudioStreamer *streamer() {
return &rtsp_streamer; }
535 bool begin(AudioInfo info) {
542 if (p_input ==
nullptr) {
543 LOGE(
"input is null");
546 if (p_encoder ==
nullptr) {
547 LOGE(
"encoder is null");
550 if (p_format ==
nullptr) {
551 LOGE(
"format is null");
556 p_format->begin(cfg);
558 rtsp_streamer.setAudioSource(&rtps_source);
560 rtps_source.setInput(*p_input);
561 rtps_source.setFormat(p_format);
567 void end() { rtps_source.
stop(); }
574 return rtps_source.
isStarted() ? buffer.available() : 0;
581 size_t write(
const uint8_t *data,
size_t len)
override {
583 size_t result = p_encoder->write(data, len);
595 AudioStream *p_input = &buffer;
596 AudioEncoder *p_encoder = ©_encoder;
597 RTSPFormatAudioTools *p_format = &pcm;
598 AudioStreamer rtsp_streamer;
603 using RTSPStream = RTSPOutput;
void stop()
Public generic methods.
Definition: AudioRuntime.h:27