Skip to content

Commit 1037680

Browse files
authored
readMetadata() rewritten
1 parent beb5ea4 commit 1037680

File tree

2 files changed

+44
-80
lines changed

2 files changed

+44
-80
lines changed

src/Audio.cpp

Lines changed: 39 additions & 75 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.4d
7-
* Updated on: Jul 11.2022
6+
* Version 2.0.4e
7+
* Updated on: Jul 16.2022
88
* Author: Wolle (schreibfaul1)
99
*
1010
*/
@@ -440,7 +440,7 @@ bool Audio::connecttohost(const char* host, const char* user, const char* pwd) {
440440

441441
strcat(rqh, "GET ");
442442
strcat(rqh, extension);
443-
strcat(rqh, " HTTP/1.0\r\n");
443+
strcat(rqh, " HTTP/1.1\r\n");
444444
strcat(rqh, "Host: ");
445445
strcat(rqh, hostwoext);
446446
strcat(rqh, "\r\n");
@@ -2338,7 +2338,7 @@ const char* Audio::parsePlaylist_M3U(){
23382338
// AUDIO_INFO("Entry in playlist found: %s", pl);
23392339
pos = indexOf(m_playlistContent[i], "http", 0); // Search for "http"
23402340
if(pos >= 0) { // Does URL contain "http://"?
2341-
log_e("%s pos=%i", m_playlistContent[i], pos);
2341+
// log_e("%s pos=%i", m_playlistContent[i], pos);
23422342
host = m_playlistContent[i] + pos; // Yes, set new host
23432343
break;
23442344
}
@@ -2755,7 +2755,7 @@ void Audio::processLocalFile() {
27552755
void Audio::processWebStream() {
27562756

27572757
const uint16_t maxFrameSize = InBuff.getMaxBlockSize(); // every mp3/aac frame is not bigger
2758-
int32_t availableBytes; // available bytes in stream
2758+
uint32_t availableBytes; // available bytes in stream
27592759
static bool f_tmr_1s;
27602760
static bool f_stream; // first audio data received
27612761
static bool f_webFileDataComplete; // all file data received
@@ -2765,7 +2765,6 @@ void Audio::processWebStream() {
27652765
static uint32_t chunksize; // chunkcount read from stream
27662766
static uint32_t tmr_1s; // timer 1 sec
27672767
static uint32_t loopCnt; // count loops if clientbuffer is empty
2768-
static uint32_t metacount; // counts down bytes between metadata
27692768
static size_t audioDataCount; // counts the decoded audiodata only
27702769

27712770
// first call, set some values to default - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
@@ -2781,7 +2780,7 @@ void Audio::processWebStream() {
27812780
audioDataCount = 0;
27822781
tmr_1s = millis();
27832782
m_t0 = millis();
2784-
metacount = m_metaint;
2783+
m_metacount = m_metaint;
27852784
readMetadata(0, true); // reset all static vars
27862785
}
27872786

@@ -2840,51 +2839,14 @@ void Audio::processWebStream() {
28402839
}
28412840

28422841
// if we have metadata: get them - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
2843-
if(!metacount && !m_f_swm && availableBytes){
2844-
static uint16_t pos_ml = 0; // determines the current position in metaline
2845-
static uint16_t metalen = 0;
2846-
2847-
if(!metalen) { // First byte of metadata?
2848-
int b = _client->read();
2849-
if(m_f_chunked) m_chunkcount--;
2850-
metalen = b * 16; // New count for metadata including length byte
2851-
if(metalen >512){
2852-
AUDIO_INFO("Metadata block to long! Skipping all Metadata from now on.");
2853-
m_f_swm = true; // expect stream without metadata
2854-
}
2855-
pos_ml = 0; chbuf[pos_ml] = 0; // Prepare for new line
2856-
}
2857-
int i = 0;
2858-
if(m_f_chunked) i = min((uint32_t)metalen, m_chunkcount);
2859-
else i = metalen;
2860-
int bytes = _client->readBytes(&chbuf[pos_ml], i);
2861-
if(m_f_chunked) m_chunkcount -= bytes;
2862-
pos_ml += bytes;
2863-
2864-
if(pos_ml == metalen) {
2865-
metalen = 0;
2866-
chbuf[pos_ml] = '\0';
2867-
pos_ml = 0;
2868-
metacount = m_metaint;
2869-
if(strlen(chbuf)) { // Any info present?
2870-
// metaline contains artist and song name. For example:
2871-
// "StreamTitle='Don McLean - American Pie';StreamUrl='';"
2872-
// Sometimes it is just other info like:
2873-
// "StreamTitle='60s 03 05 Magic60s';StreamUrl='';"
2874-
// Isolate the StreamTitle, remove leading and trailing quotes if present.
2875-
// log_i("ST %s", metaline);
2876-
2877-
latinToUTF8(chbuf, sizeof(chbuf)); // convert to UTF-8 if necessary
2878-
2879-
int pos = indexOf(chbuf, "song_spot", 0); // remove some irrelevant infos
2880-
if(pos > 3) { // e.g. song_spot="T" MediaBaseId="0" itunesTrackId="0"
2881-
chbuf[pos] = 0;
2882-
}
2883-
log_e("%s", chbuf);
2884-
if(!m_f_localfile) showstreamtitle(chbuf); // Show artist and title if present in metadata
2885-
}
2886-
}
2887-
return;
2842+
if(!m_metacount && !m_f_swm){
2843+
int bytes = 0;
2844+
int res = 0;
2845+
if(m_f_chunked) bytes = min(m_chunkcount, availableBytes);
2846+
else bytes = availableBytes;
2847+
res = readMetadata(bytes);
2848+
if(m_f_chunked) m_chunkcount -= res;
2849+
if(!m_metacount) return;
28882850
}
28892851

28902852
// if the buffer is often almost empty issue a warning - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
@@ -2913,7 +2875,7 @@ void Audio::processWebStream() {
29132875
// buffer fill routine - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
29142876
if(true) { // statement has no effect
29152877
uint32_t bytesCanBeWritten = InBuff.writeSpace();
2916-
if(!m_f_swm) bytesCanBeWritten = min(metacount, bytesCanBeWritten);
2878+
if(!m_f_swm) bytesCanBeWritten = min(m_metacount, bytesCanBeWritten);
29172879
if(m_f_chunked) bytesCanBeWritten = min(m_chunkcount, bytesCanBeWritten);
29182880

29192881
int16_t bytesAddedToBuffer = 0;
@@ -2942,7 +2904,7 @@ void Audio::processWebStream() {
29422904

29432905
if(bytesAddedToBuffer > 0) {
29442906
if(m_f_webfile) byteCounter += bytesAddedToBuffer; // Pull request #42
2945-
if(!m_f_swm) metacount -= bytesAddedToBuffer;
2907+
if(!m_f_swm) m_metacount -= bytesAddedToBuffer;
29462908
if(m_f_chunked) m_chunkcount -= bytesAddedToBuffer;
29472909
InBuff.bytesWritten(bytesAddedToBuffer);
29482910
}
@@ -3032,7 +2994,7 @@ void Audio::processWebStream() {
30322994
if(byteCounter == m_contentlength){
30332995
if(m_playlistFormat == FORMAT_M3U8){
30342996
byteCounter = 0;
3035-
metacount = m_metaint;
2997+
m_metacount = m_metaint;
30362998
m_f_continue = true;
30372999
return;
30383000
}
@@ -3130,7 +3092,7 @@ bool Audio::parseHttpResponseHeader() { // this is the response to a GET / reque
31303092

31313093
idx = indexOf(rhl, "content-type:", 0); // content-type: text/html; charset=UTF-8
31323094
if(idx >= 0) {
3133-
AUDIO_INFO("%s", rhl);
3095+
// AUDIO_INFO("%s", rhl);
31343096
idx = indexOf(rhl + 13, ";");
31353097
if(idx >0) rhl[13 + idx] = '\0';
31363098
if(parseContentType(rhl + 13)) ct_seen = true;
@@ -3250,7 +3212,6 @@ bool Audio::parseHttpResponseHeader() { // this is the response to a GET / reque
32503212
} // outer while
32513213

32523214
exit: // termination condition
3253-
log_e("exit");
32543215
if(audio_showstation) audio_showstation("");
32553216
if(audio_icydescription) audio_icydescription("");
32563217
if(audio_icyurl) audio_icyurl("");
@@ -3325,53 +3286,56 @@ bool Audio:: initializeDecoder(){
33253286
return false;
33263287
}
33273288
//---------------------------------------------------------------------------------------------------------------------
3328-
bool Audio::readMetadata(uint8_t b, bool first) {
3289+
uint16_t Audio::readMetadata(uint16_t maxBytes, bool first) {
33293290

33303291
static uint16_t pos_ml = 0; // determines the current position in metaline
33313292
static uint16_t metalen = 0;
3293+
uint16_t res = 0;
33323294
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
33333295
if(first){
33343296
pos_ml = 0;
33353297
metalen = 0;
3336-
return true;
3298+
return 0;
33373299
}
33383300
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
3339-
if(!metalen) { // First byte of metadata?
3340-
int b = _client->read();
3341-
metalen = b * 16 + 1; // New count for metadata including length byte
3342-
if(metalen >512){
3301+
if(!maxBytes) return 0; // guard
3302+
3303+
if(!metalen) {
3304+
int b = _client->read(); // First byte of metadata?
3305+
metalen = b * 16 ; // New count for metadata including length byte
3306+
if(metalen > 512){
33433307
AUDIO_INFO("Metadata block to long! Skipping all Metadata from now on.");
33443308
m_f_swm = true; // expect stream without metadata
33453309
}
33463310
pos_ml = 0; chbuf[pos_ml] = 0; // Prepare for new line
3311+
res = 1;
33473312
}
3348-
chbuf[pos_ml] = (char) b; // Put new char in +++++
3349-
if(pos_ml < 510) pos_ml ++;
3350-
chbuf[pos_ml] = 0;
3351-
if(pos_ml == 509) { log_e("metaline overflow in AUDIO_METADATA! metaline=%s", chbuf); }
3352-
if(pos_ml == 510) { ; /* last current char in b */}
3353-
3313+
if(!metalen) {m_metacount = m_metaint; return res;}
33543314

3355-
if(--metalen == 0) {
3315+
uint16_t a = _client->readBytes(&chbuf[pos_ml], min(metalen, (uint16_t)(maxBytes -1)));
3316+
res += a;
3317+
pos_ml += a;
3318+
if(pos_ml == metalen) {
3319+
metalen = 0;
3320+
chbuf[pos_ml] = '\0';
3321+
m_metacount = m_metaint;
33563322
if(strlen(chbuf)) { // Any info present?
33573323
// metaline contains artist and song name. For example:
33583324
// "StreamTitle='Don McLean - American Pie';StreamUrl='';"
33593325
// Sometimes it is just other info like:
33603326
// "StreamTitle='60s 03 05 Magic60s';StreamUrl='';"
33613327
// Isolate the StreamTitle, remove leading and trailing quotes if present.
33623328
// log_i("ST %s", metaline);
3363-
33643329
latinToUTF8(chbuf, sizeof(chbuf)); // convert to UTF-8 if necessary
3365-
33663330
int pos = indexOf(chbuf, "song_spot", 0); // remove some irrelevant infos
33673331
if(pos > 3) { // e.g. song_spot="T" MediaBaseId="0" itunesTrackId="0"
33683332
chbuf[pos] = 0;
33693333
}
3370-
if(!m_f_localfile) showstreamtitle(chbuf); // Show artist and title if present in metadata
3334+
showstreamtitle(chbuf); // Show artist and title if present in metadata
33713335
}
3372-
return true ;
3336+
pos_ml = 0;
33733337
}
3374-
return false;// end_METADATA
3338+
return res;
33753339
}
33763340
//---------------------------------------------------------------------------------------------------------------------
33773341
bool Audio::parseContentType(char* ct) {

src/Audio.h

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

@@ -248,7 +248,7 @@ class Audio : private AudioBuffer{
248248
bool parseContentType(char* ct);
249249
bool parseHttpResponseHeader();
250250
bool initializeDecoder();
251-
bool readMetadata(uint8_t b, bool first = false);
251+
uint16_t readMetadata(uint16_t b, bool first = false);
252252
esp_err_t I2Sstart(uint8_t i2s_num);
253253
esp_err_t I2Sstop(uint8_t i2s_num);
254254
void urlencode(char* buff, uint16_t buffLen, bool spacesOnly = false);
@@ -459,13 +459,13 @@ class Audio : private AudioBuffer{
459459
uint32_t m_sampleRate=16000;
460460
uint32_t m_bitRate=0; // current bitrate given fom decoder
461461
uint32_t m_avr_bitrate = 0; // average bitrate, median computed by VBR
462-
int m_readbytes=0; // bytes read
463-
int m_metalen=0; // Number of bytes in metadata
462+
int m_readbytes = 0; // bytes read
463+
uint32_t m_metacount = 0; // counts down bytes between metadata
464464
int m_controlCounter = 0; // Status within readID3data() and readWaveHeader()
465465
int8_t m_balance = 0; // -16 (mute left) ... +16 (mute right)
466466
uint8_t m_vol=64; // volume
467467
uint8_t m_bitsPerSample = 16; // bitsPerSample
468-
uint8_t m_channels=2;
468+
uint8_t m_channels = 2;
469469
uint8_t m_i2s_num = I2S_NUM_0; // I2S_NUM_0 or I2S_NUM_1
470470
uint8_t m_playlistFormat = 0; // M3U, PLS, ASX
471471
uint8_t m_m3u8codec = CODEC_NONE; // M4A

0 commit comments

Comments
 (0)