Skip to content

Commit b004caf

Browse files
committed
first release
1 parent a07ac19 commit b004caf

File tree

3 files changed

+70
-93
lines changed

3 files changed

+70
-93
lines changed

src/Audio.cpp

Lines changed: 66 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -836,101 +836,75 @@ bool Audio::httpPrint(const char* host) {
836836
return true;
837837
}
838838
//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
839-
bool Audio::httpRange(const char* host, uint32_t range){
839+
bool Audio::httpRange(uint32_t range){
840840

841841
if(!m_f_running) return false;
842-
if(host == NULL) {
843-
AUDIO_INFO("Hostaddress is empty");
844-
stopSong();
845-
return false;
846-
}
847-
ps_ptr<char>c_host;
848-
c_host.copy_from(host);
849-
ps_ptr<char> h_host; // copy of host without http:// or https://
850-
851-
if(startsWith(host, "https")) m_f_ssl = true;
852-
else m_f_ssl = false;
853-
854-
if(m_f_ssl) h_host.assign(host + 8);
855-
else h_host.assign(host + 7);
856-
857-
int16_t pos_slash; // position of "/" in hostname
858-
int16_t pos_colon; // position of ":" in hostname
859-
int16_t pos_ampersand; // position of "&" in hostname
860-
uint16_t port = 80; // port number
861-
862-
// In the URL there may be an extension, like noisefm.ru:8000/play.m3u&t=.m3u
863-
pos_slash = h_host.index_of( "/", 0);
864-
pos_colon = h_host.index_of( ":", 0);
865-
if(isalpha(h_host[pos_colon + 1])) pos_colon = -1; // no portnumber follows
866-
pos_ampersand = h_host.index_of( "&", 0);
867-
868-
ps_ptr<char> hostwoext; // "skonto.ls.lv:8002" in "skonto.ls.lv:8002/mp3"
869-
ps_ptr<char> extension; // "/mp3" in "skonto.ls.lv:8002/mp3"
870-
871-
if(pos_slash > 1) {
872-
hostwoext.alloc(pos_slash + 1, "hostwoext");
873-
memcpy(hostwoext.get(), h_host.get(), pos_slash);
874-
hostwoext[pos_slash] = '\0';
875-
extension = urlencode(h_host.get() + pos_slash, true);
876-
}
877-
else { // url has no extension
878-
hostwoext.append(h_host.get());
879-
extension.append("/");
880-
}
881-
882-
if((pos_colon >= 0) && ((pos_ampersand == -1) || (pos_ampersand > pos_colon))) {
883-
port = atoi(h_host.get() + pos_colon + 1); // Get portnumber as integer
884-
hostwoext[pos_colon] = '\0'; // Host without portnumber
885-
}
886-
887-
char rqh[strlen(h_host.get()) + strlen(host) + 300]; // http request header
888-
rqh[0] = '\0';
889-
char ch_range[12];
890-
ltoa(range, ch_range, 10);
891-
AUDIO_INFO("skip to position: %li", (long int)range);
892-
strcat(rqh, "GET ");
893-
strcat(rqh, extension.c_get());
894-
strcat(rqh, " HTTP/1.1\r\n");
895-
strcat(rqh, "Host: ");
896-
strcat(rqh, hostwoext.c_get());
897-
strcat(rqh, "\r\n");
898-
strcat(rqh, "Icy-MetaData:1\r\n");
899-
strcat(rqh, "Icy-MetaData:2\r\n");
900-
strcat(rqh, "Range: bytes=");
901-
strcat(rqh, (const char*)ch_range);
902-
strcat(rqh, "-\r\n");
903-
strcat(rqh, "Referer: ");
904-
strcat(rqh, host);
905-
strcat(rqh, "\r\n");
906-
strcat(rqh, "User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36\r\n");
907-
strcat(rqh, "Connection: keep-alive\r\n\r\n");
908-
909-
if(m_f_ssl) { _client = static_cast<WiFiClient*>(&clientsecure); if(m_f_ssl && port == 80) port = 443;}
910-
else { _client = static_cast<WiFiClient*>(&client); }
911-
// AUDIO_INFO("The host has disconnected, reconnecting");
912842

913-
if(!_client->connect(hostwoext.get(), port)) {
914-
AUDIO_LOG_ERROR("connection lost %s", c_host.c_get());
915-
stopSong();
916-
return false;
843+
uint16_t port = 0; // port number
844+
ps_ptr<char> c_host; // copy of host
845+
ps_ptr<char> hwoe; // host without extension
846+
ps_ptr<char> extension; // extension
847+
ps_ptr<char> query_string; // parameter
848+
ps_ptr<char> path; // extension + '?' + parameter
849+
ps_ptr<char> rqh; // request header
850+
ps_ptr<char> cur_hwoe; // m_currenthost without extension
851+
852+
c_host.clone_from(m_currentHost);
853+
c_host.trim();
854+
auto dismantledHost = dismantle_host(c_host.get());
855+
856+
// https://edge.live.mp3.mdn.newmedia.nacamar.net:8000/ps-charivariwb/livestream.mp3;?user=ps-charivariwb;&pwd=ps-charivariwb-------
857+
// | | | | |
858+
// | | | | | (query string)
859+
// ssl?| |<-----host without extension-------->|port|<----- --extension----------->|<-first parameter->|<-second parameter->.......
860+
// |
861+
// |<-----------------------------path------------------------------------------->
862+
863+
m_f_ssl = dismantledHost.ssl;
864+
port = dismantledHost.port;
865+
if(dismantledHost.hwoe.valid()) hwoe.clone_from(dismantledHost.hwoe);
866+
if(dismantledHost.extension.valid()) extension.clone_from(dismantledHost.extension);
867+
if(dismantledHost.query_string.valid()) query_string.clone_from(dismantledHost.query_string);
868+
869+
if(extension.valid()) path.assign(extension.get());
870+
if(query_string.valid()){path.append("?"); path.append(query_string.get());}
871+
if(!hwoe.valid()) hwoe.assign("");
872+
if(!extension.valid()) extension.assign("");
873+
if(!path.valid()) path.assign("");
874+
875+
path = urlencode(path.get(), true);
876+
877+
if(!m_currentHost.valid()) m_currentHost.assign("");
878+
auto dismantledLastHost = dismantle_host(m_currentHost.get());
879+
cur_hwoe.clone_from(dismantledLastHost.hwoe);
880+
881+
rqh.assignf("GET /%s HTTP/1.1\r\n", path.get());
882+
rqh.appendf("Host: %s\r\n", hwoe.get());
883+
rqh.append("Accept: */*\r\n");
884+
rqh.append("Accept-Encoding: identity;q=1,*;q=0\r\n");
885+
rqh.append("Cache-Control: no-cache\r\n");
886+
rqh.append("Connection: keep-alive\r\n");
887+
rqh.appendf("Range: bytes=%i-\r\n",range);
888+
rqh.appendf("Referer: %s\r\n", m_currentHost.c_get());
889+
rqh.append("Sec-GPC: 1\r\n");
890+
rqh.append("User-Agent: VLC/3.0.21 LibVLC/3.0.21 AppleWebKit/537.36 (KHTML, like Gecko)\r\n\r\n");
891+
892+
if(!_client->connected()) {
893+
AUDIO_LOG_INFO("Verbindung geschlossen, stelle neu her");
894+
_client->stop(); // Schließe alte Verbindung
895+
if (!_client->connect(hwoe.get(), port)) {
896+
AUDIO_LOG_ERROR("Verbindung fehlgeschlagen");
897+
return false;
898+
}
917899
}
918-
;
919-
_client->print(rqh);
920900

921-
if(extension.ends_with_icase(".mp3")) m_expectedCodec = CODEC_MP3;
922-
if(extension.ends_with_icase(".aac")) m_expectedCodec = CODEC_AAC;
923-
if(extension.ends_with_icase(".wav")) m_expectedCodec = CODEC_WAV;
924-
if(extension.ends_with_icase(".m4a")) m_expectedCodec = CODEC_M4A;
925-
if(extension.ends_with_icase(".flac")) m_expectedCodec = CODEC_FLAC;
926-
if(extension.ends_with_icase(".asx")) m_expectedPlsFmt = FORMAT_ASX;
927-
if(extension.ends_with_icase(".m3u")) m_expectedPlsFmt = FORMAT_M3U;
928-
if(extension.contains(".m3u8")) m_expectedPlsFmt = FORMAT_M3U8;
929-
if(extension.ends_with_icase(".pls")) m_expectedPlsFmt = FORMAT_PLS;
901+
AUDIO_LOG_INFO("av %i", _client->available());
902+
AUDIO_LOG_INFO("rqh \n%s", rqh.get());
930903

931-
m_dataMode = HTTP_RANGE_HEADER; // Handle header
904+
_client->print(rqh.get());
905+
906+
m_dataMode = HTTP_RANGE_HEADER;
932907
m_streamType = ST_WEBFILE;
933-
m_f_chunked = false;
934908
return true;
935909
}
936910
//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
@@ -2019,18 +1993,20 @@ int Audio::read_ID3_Header(uint8_t* data, size_t len) {
20191993
uint32_t pos = m_pwf.byteCounter;
20201994
// log_w("m_audiofile.position() %i, m_ID3Hdr.SYLT.pos %i", pos, m_ID3Hdr.SYLT.pos);
20211995
bool res;
2022-
res = httpRange(m_currentHost.c_get(), m_ID3Hdr.SYLT.pos);
1996+
res = httpRange(m_ID3Hdr.SYLT.pos);
20231997
if(!res) AUDIO_LOG_ERROR("http range request was not successful");
1998+
if(res == false) return 0;
20241999
res = parseHttpRangeHeader();
20252000
if(!res) AUDIO_LOG_ERROR("http range response was not successful");
20262001
uint16_t bytesWritten = 0;
20272002
while(bytesWritten < m_ID3Hdr.SYLT.size){
20282003
bytesWritten += _client->read((uint8_t*)syltBuff.get() + bytesWritten, m_ID3Hdr.SYLT.size);
20292004
}
2030-
res = httpRange(m_currentHost.c_get(), pos);
2005+
res = httpRange(pos);
20312006
if(!res) AUDIO_LOG_ERROR("http range request was not successful");
20322007
res = parseHttpRangeHeader();
20332008
if(!res) AUDIO_LOG_ERROR("http range response was not successful");
2009+
// return 0;
20342010
}
20352011
if(m_dataMode == AUDIO_LOCALFILE){
20362012
uint32_t pos = m_audiofile.position();

src/Audio.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,7 @@ class Audio{
438438
void setDefaults(); // free buffers and set defaults
439439
void initInBuff();
440440
bool httpPrint(const char* host);
441-
bool httpRange(const char* host, uint32_t range);
441+
bool httpRange(uint32_t range);
442442
void processLocalFile();
443443
void processWebStream();
444444
void processWebFile();

src/psram_unique_ptr.hpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,7 @@ class ps_ptr {
422422
// Suffix anhängen
423423
std::memcpy(static_cast<char*>(mem.get()) + old_len, suffix, add_len + 1);
424424
allocated_size = new_len;
425+
static_cast<char*>(mem.get())[old_len + add_len] = '\0';
425426
}
426427

427428
// ps_ptr<char> text1; // like Strcat with automatic new allocation
@@ -1059,7 +1060,7 @@ void unicodeToUTF8(const char* src) {
10591060
while (*ptr) {
10601061
if (ptr[0] == '\\' && ptr[1] == 'u') {
10611062
uint32_t codepoint = 0;
1062-
if (sscanf(ptr + 2, "%4x", &codepoint) == 1) {
1063+
if (sscanf(ptr + 2, "%4lx", &codepoint) == 1) {
10631064
int len = encodeCodepointToUTF8(codepoint, utf8);
10641065
utf8[len] = 0;
10651066
this->append(utf8);
@@ -1502,7 +1503,7 @@ void unicodeToUTF8(const char* src) {
15021503
// 0x65 0x6E 0x67 0x0A 0x00 0x00
15031504
// e n g LF NUL NUL
15041505

1505-
void hex_dump(uint16_t n = 60) {
1506+
void hex_dump(uint16_t n = UINT16_MAX) {
15061507
if (!valid()) { printf("hex_dump: invalid buffer\n"); return; }
15071508
if (allocated_size < n) n = allocated_size;
15081509
if (n == 0) {

0 commit comments

Comments
 (0)