Skip to content

File tree

2 files changed

+47
-30
lines changed

2 files changed

+47
-30
lines changed

src/Audio.cpp

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
*
44
* Created on: Oct 26.2018
55
*
6-
* Version 2.0.5c
7-
* Updated on: Jul 31.2022
6+
* Version 2.0.5d
7+
* Updated on: Aug 01.2022
88
* Author: Wolle (schreibfaul1)
99
*
1010
*/
@@ -303,6 +303,7 @@ void Audio::setDefaults() {
303303
if(m_m3u8_lastEntry) {free(m_m3u8_lastEntry); m_m3u8_lastEntry = NULL;} // free if stream is not m3u8
304304
vector_clear_and_shrink(m_playlistURL);
305305
vector_clear_and_shrink(m_playlistContent);
306+
m_hashQueue.clear(); m_hashQueue.shrink_to_fit(); // uint32_t vector
306307
client.stop();
307308
client.flush(); // release memory
308309
clientsecure.stop();
@@ -582,6 +583,7 @@ bool Audio::httpPrint(const char* host) {
582583

583584
setDatamode(HTTP_RESPONSE_HEADER); // Handle header
584585
m_streamType = ST_WEBSTREAM;
586+
m_contentlength = 0;
585587

586588
if(hostwoext) {free(hostwoext); hostwoext = NULL;}
587589
if(extension) {free(extension); extension = NULL;}
@@ -2322,8 +2324,8 @@ void Audio::loop() {
23222324
}
23232325
break;
23242326
case AUDIO_DATA:
2325-
if(m_f_ts) processWebStreamTS();
2326-
else processWebStreamHLS();
2327+
if(m_f_ts) processWebStreamTS(); // aac or aacp with ts packets
2328+
else processWebStreamHLS(); // aac or aacp normal stream
23272329
if(f_noNewHost){
23282330
m_f_continue = false;
23292331
if(timestamp2 < millis()) {
@@ -2369,9 +2371,10 @@ bool Audio::readPlayListData() {
23692371
pl[pos] = _client->read();
23702372
if(pl[pos] == '\n') {pl[pos] = '\0'; pos++; break;}
23712373
// if(pl[pos] == '&' ) {pl[pos] = '\0'; pos++; break;}
2372-
if(pl[pos] == '\r') {pl[pos] = '\0'; pos++; break;}
2374+
if(pl[pos] == '\r') {pl[pos] = '\0'; pos++; continue;;}
23732375
pos++;
2374-
if(pos == 510) {pl[pos] = '\0'; break;}
2376+
if(pos == 511){ pos--; ctl++; continue;}
2377+
if(pos == 510) {pl[pos] = '\0';}
23752378
}
23762379
ctl += pos;
23772380
if(pos) {pl[pos] = '\0'; break;}
@@ -2629,26 +2632,30 @@ const char* Audio::parsePlaylist_M3U8(){
26292632
tmp = strdup(m_playlistContent[i]);
26302633
}
26312634

2632-
if(!m_m3u8_lastEntry){ // first init
2633-
m_playlistURL.insert(m_playlistURL.begin(), strdup((const char*)(tmp)));
2634-
m_m3u8_lastEntry = strdup(tmp);
2635-
if(m_f_Log) log_i("insert %s", tmp);
2636-
continue;
2637-
}
2638-
if(strcmp(tmp, m_m3u8_lastEntry) > 0 || strlen(tmp) > strlen(m_m3u8_lastEntry)){ // next sequence?,
2639-
//strcmp: '2.aac'>'1.aac' or strlen '10.aac'>'9.aac'
2640-
m_playlistURL.insert(m_playlistURL.begin(), strdup((const char*)(tmp)));
2641-
if(m_m3u8_lastEntry){free(m_m3u8_lastEntry); m_m3u8_lastEntry = strdup(tmp);}
2642-
assert(m_m3u8_lastEntry != tmp); // no free space in task?
2643-
if(m_f_Log) log_i("insert %s", tmp);
2635+
uint32_t hash = simpleHash(tmp);
2636+
if(m_hashQueue.size() == 0){
2637+
m_hashQueue.insert(m_hashQueue.begin(), hash);
2638+
m_playlistURL.insert(m_playlistURL.begin(), strdup(tmp));
26442639
}
26452640
else{
2646-
if(m_f_Log) log_i("file already known %s", m_playlistContent[i]);
2641+
bool known = false;
2642+
for(int i = 0; i< m_hashQueue.size(); i++){
2643+
if(hash == m_hashQueue[i]){
2644+
if(m_f_Log) log_i("file already known %s", tmp);
2645+
known = true;
2646+
}
2647+
}
2648+
if(!known){
2649+
m_hashQueue.insert(m_hashQueue.begin(), hash);
2650+
m_playlistURL.insert(m_playlistURL.begin(), strdup(tmp));
2651+
}
26472652
}
26482653

2654+
if(m_hashQueue.size() > 20) m_hashQueue.pop_back();
2655+
26492656
if(tmp){free(tmp); tmp = NULL;}
26502657

2651-
if(m_playlistURL.size() == 100){
2658+
if(m_playlistURL.size() == 20){
26522659
ESP_LOGD("", "can't stuff anymore");
26532660
break;
26542661
}
@@ -3445,10 +3452,10 @@ bool Audio::parseHttpResponseHeader() { // this is the response to a GET / reque
34453452
if(b < 0x20) continue;
34463453
rhl[pos] = b;
34473454
pos++;
3455+
if(pos == 511){pos = 510; continue;}
34483456
if(pos == 510){
34493457
rhl[pos] = '\0';
34503458
if(m_f_Log) log_i("responseHeaderline overflow");
3451-
break;
34523459
}
34533460
} // inner while
34543461

src/Audio.h

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* Audio.h
33
*
44
* Created on: Oct 28,2018
5-
* Updated on: Jul 31,2022
5+
* Updated on: Aug 01,2022
66
* Author: Wolle (schreibfaul1)
77
*/
88

@@ -399,6 +399,15 @@ class Audio : private AudioBuffer{
399399
vec.clear();
400400
vec.shrink_to_fit();
401401
}
402+
uint32_t simpleHash(const char* str){
403+
if(str == NULL) return 0;
404+
uint32_t hash = 0;
405+
for(int i=0; i<strlen(str); i++){
406+
if(str[i] < 32) continue; // ignore control sign
407+
hash += (str[i] - 31) * i * 32;
408+
}
409+
return hash;
410+
}
402411

403412
private:
404413
const char *codecname[9] = {"unknown", "WAV", "MP3", "AAC", "M4A", "FLAC", "OGG", "OGG FLAC", "OPUS"};
@@ -434,14 +443,15 @@ class Audio : private AudioBuffer{
434443
int pids[4];
435444
} pid_array;
436445

437-
File audiofile; // @suppress("Abstract class cannot be instantiated")
438-
WiFiClient client; // @suppress("Abstract class cannot be instantiated")
439-
WiFiClientSecure clientsecure; // @suppress("Abstract class cannot be instantiated")
440-
WiFiClient* _client = nullptr;
441-
i2s_config_t m_i2s_config = {}; // stores values for I2S driver
442-
i2s_pin_config_t m_pin_config = {};
443-
std::vector<char*> m_playlistContent; // m3u8 playlist buffer
444-
std::vector<char*> m_playlistURL; // m3u8 streamURLs buffer
446+
File audiofile; // @suppress("Abstract class cannot be instantiated")
447+
WiFiClient client; // @suppress("Abstract class cannot be instantiated")
448+
WiFiClientSecure clientsecure; // @suppress("Abstract class cannot be instantiated")
449+
WiFiClient* _client = nullptr;
450+
i2s_config_t m_i2s_config = {}; // stores values for I2S driver
451+
i2s_pin_config_t m_pin_config = {};
452+
std::vector<char*> m_playlistContent; // m3u8 playlist buffer
453+
std::vector<char*> m_playlistURL; // m3u8 streamURLs buffer
454+
std::vector<uint32_t> m_hashQueue;
445455

446456
const size_t m_frameSizeWav = 1600;
447457
const size_t m_frameSizeMP3 = 1600;

0 commit comments

Comments
 (0)