Skip to content

Commit

Permalink
example: rtsp client
Browse files Browse the repository at this point in the history
  • Loading branch information
pschatzmann committed Oct 17, 2023
1 parent 181a161 commit 190fdc0
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 27 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* @file communication-rtsp-i2s.ino
* @author Phil Schatzmann
* @brief Demo for RTSP Client that is playing mp3
* @version 0.1
* @date 2022-05-02
*
* @copyright Copyright (c) 2022
*
*/

#include "AudioTools.h" // https://github.com/pschatzmann/arduino-audio-tools
#include "AudioCodecs/CodecMP3Helix.h" // https://github.com/pschatzmann/arduino-libhelix
#include "RTSPSimpleClient.hh" // https://github.com/pschatzmann/arduino-live555.git

I2SStream i2s;
EncodedAudioStream out_mp3(&i2s, new MP3DecoderHelix()); // Decoding stream
RTSPSimpleClient rtsp;

void setup() {
Serial.begin(115200);
AudioLogger::instance().begin(Serial, AudioLogger::Info);

// setup output: make sure we can buffer 1 decoded frame
auto cfg_i2s = i2s.defaultConfig(TX_MODE);
cfg_i2s.buffer_size = 1024;
cfg_i2s.buffer_count = 10;
i2s.begin(cfg_i2s);

out_mp3.begin();

// setup rtsp data source
auto cfg = rtsp.defaultConfig();
cfg.ssid = "ssid";
cfg.password = "password";
cfg.url = "rtsp://192.168.1.38:8554/test.mp3";
cfg.output = &out_mp3;
cfg.buffer_size = 1024*2; // space for 1 encoded frame
//cfg.is_tcp = false; // use udp when false (default false)
//cfg.is_blocking = false; // call singleStep in loop if false (default false)
rtsp.begin(cfg);
}

void loop() {
rtsp.singleStep();
}
4 changes: 3 additions & 1 deletion src/AudioConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -700,4 +700,6 @@ using int24_t = audio_tools::int24_4bytes_t;
#pragma GCC diagnostic ignored "-Wsign-compare"
#ifdef USE_INITIALIZER_LIST
#pragma GCC diagnostic ignored "-Wnarrowing"
#endif
#endif

#undef rewind
56 changes: 30 additions & 26 deletions src/AudioHttp/HLSStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,26 @@ class URLLoader {

void end(){
TRACED();
stream.end();
p_stream->end();
p_stream = nullptr;
buffer.clear();
active = false;
}

/// Adds the next url to be played in sequence
void addUrl(const char* url){
LOGI("Adding %s", url);
Str url_str(url);
char *str = new char[url_str.length()+1];
memcpy(str, url_str.c_str(), url_str.length()+1);
urls.push_back(str);
//Str url_str(url);
//char *str = new char[url_str.length()+1];
//memcpy(str, url_str.c_str(), url_str.length()+1);
URLStream *p_stream = new URLStream();
p_stream->setWaitForData(false);
p_stream->setAutoCreateLines(false);
if (!p_stream->begin(url)){
TRACEE();
}

urls.push_back(p_stream);
}

/// Provides the number of open urls which can be played. Refills them, when min limit is reached.
Expand All @@ -71,13 +79,13 @@ class URLLoader {
}

const char *contentType() {
if (!stream) return nullptr;
return stream.httpRequest().reply().get(CONTENT_TYPE);
if (p_stream==nullptr) return nullptr;
return p_stream->httpRequest().reply().get(CONTENT_TYPE);
}

int contentLength() {
if (!stream) return 0;
return stream.contentLength();
if (p_stream==nullptr) return 0;
return p_stream->contentLength();
}

void setBuffer(int size, int count){
Expand All @@ -86,12 +94,12 @@ class URLLoader {
}

protected:
Vector<const char*> urls{10};
Vector<URLStream*> urls{10};
NBuffer<uint8_t> buffer{DEFAULT_BUFFER_SIZE, 50};
bool active = false;
int buffer_size = DEFAULT_BUFFER_SIZE;
int buffer_count = 10;
URLStream stream;
URLStream *p_stream = nullptr;


/// try to keep the buffer filled
Expand All @@ -108,34 +116,30 @@ class URLLoader {
}

// switch current stream if we have no more data
if ((!stream || stream.totalRead()==stream.contentLength()) && !urls.empty()) {
if ((p_stream==nullptr || p_stream->totalRead()==p_stream->contentLength()) && !urls.empty()) {
LOGD("Refilling");
const char* url = urls[0];
if (p_stream!=nullptr) {
p_stream->end();
delete p_stream;
}
p_stream = urls[0];
p_stream->waitForData();
urls.pop_front();
assert(urls[0]!=url);
//assert(urls[0]!=url);

#ifdef ESP32
LOGI("Free heap: %d", ESP.getFreeHeap());
#endif
LOGI("Playing %s of %d", url, urls.size());

stream.clear();
stream.setWaitForData(true);
stream.setAutoCreateLines(false);
if (!stream.begin(url)){
TRACEE();
}
// free memory
delete[] url;
LOGI("Playing %s of %d", p_stream->urlStr(), urls.size());
}

int to_write = min(buffer.availableForWrite(),DEFAULT_BUFFER_SIZE);
if (to_write>0){
int total = 0;
while(to_write>0){
if (stream.totalRead()==stream.contentLength()) break;
if (p_stream->totalRead()==p_stream->contentLength()) break;
uint8_t tmp[to_write]={0};
int read = stream.readBytes(tmp, to_write);
int read = p_stream->readBytes(tmp, to_write);
total += read;
if (read>0){
buffer.writeArray(tmp, read);
Expand Down

0 comments on commit 190fdc0

Please sign in to comment.