Skip to content

Commit 63fd045

Browse files
authored
HLS, read ID3 header
1 parent 67264b7 commit 63fd045

File tree

2 files changed

+62
-15
lines changed

2 files changed

+62
-15
lines changed

src/Audio.cpp

+60-14
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
*
44
* Created on: Oct 26.2018
55
*
6-
* Version 2.0.5k
7-
* Updated on: Aug 23.2022
6+
* Version 2.0.6
7+
* Updated on: Aug 25.2022
88
* Author: Wolle (schreibfaul1)
99
*
1010
*/
@@ -994,7 +994,7 @@ void Audio::showID3Tag(const char* tag, const char* value){
994994
if(!strcmp(tag, "TRCK")) sprintf(chbuf, "Track: %s", value);
995995
if(!strcmp(tag, "TSSE")) sprintf(chbuf, "SettingsForEncoding: %s", value);
996996
if(!strcmp(tag, "TRDA")) sprintf(chbuf, "RecordingDates: %s", value);
997-
if(!strcmp(tag, "TXXX")) sprintf(chbuf, "UserDefinedText: %s", value);
997+
if(!m_f_m3u8data) if(!strcmp(tag, "TXXX")) sprintf(chbuf, "UserDefinedText: %s", value);
998998
if(!strcmp(tag, "TYER")) sprintf(chbuf, "Year: %s", value);
999999
if(!strcmp(tag, "USER")) sprintf(chbuf, "TermsOfUse: %s", value);
10001000
if(!strcmp(tag, "USLT")) sprintf(chbuf, "Lyrics: %s", value);
@@ -1503,9 +1503,9 @@ int Audio::read_ID3_Header(uint8_t *data, size_t len) {
15031503
headerSize = 0;
15041504
ehsz = 0;
15051505
if(specialIndexOf(data, "ID3", 4) != 0) { // ID3 not found
1506-
AUDIO_INFO("file has no mp3 tag, skip metadata");
1506+
if(!m_f_m3u8data) AUDIO_INFO("file has no mp3 tag, skip metadata");
15071507
m_audioDataSize = m_contentlength;
1508-
AUDIO_INFO("Audio-Length: %u", m_audioDataSize);
1508+
if(!m_f_m3u8data) AUDIO_INFO("Audio-Length: %u", m_audioDataSize);
15091509
return -1; // error, no ID3 signature found
15101510
}
15111511
ID3version = *(data + 3);
@@ -1524,9 +1524,8 @@ int Audio::read_ID3_Header(uint8_t *data, size_t len) {
15241524
id3Size += 10;
15251525

15261526
// Every read from now may be unsync'd
1527-
AUDIO_INFO("ID3 framesSize: %i", id3Size);
1528-
1529-
AUDIO_INFO("ID3 version: 2.%i", ID3version);
1527+
if(!m_f_m3u8data) AUDIO_INFO("ID3 framesSize: %i", id3Size);
1528+
if(!m_f_m3u8data) AUDIO_INFO("ID3 version: 2.%i", ID3version);
15301529

15311530
if(ID3version == 2){
15321531
m_controlCounter = 10;
@@ -1548,7 +1547,7 @@ int Audio::read_ID3_Header(uint8_t *data, size_t len) {
15481547
return 4;
15491548
}
15501549
else{
1551-
AUDIO_INFO("ID3 normal frames");
1550+
if(!m_f_m3u8data) AUDIO_INFO("ID3 normal frames");
15521551
return 0;
15531552
}
15541553
}
@@ -1723,7 +1722,7 @@ int Audio::read_ID3_Header(uint8_t *data, size_t len) {
17231722
else {
17241723
m_controlCounter = 100; // ok
17251724
m_audioDataSize = m_contentlength - m_audioDataStart;
1726-
AUDIO_INFO("Audio-Length: %u", m_audioDataSize);
1725+
if(!m_f_m3u8data) AUDIO_INFO("Audio-Length: %u", m_audioDataSize);
17271726
if(APIC_seen && audio_id3image){
17281727
size_t pos = audiofile.position();
17291728
audio_id3image(audiofile, APIC_pos, APIC_size);
@@ -3263,14 +3262,19 @@ void Audio::processWebStreamTS() {
32633262
void Audio::processWebStreamHLS() {
32643263

32653264
const uint16_t maxFrameSize = InBuff.getMaxBlockSize(); // every mp3/aac frame is not bigger
3265+
const uint16_t ID3BuffSize = 1024;
32663266
uint32_t availableBytes; // available bytes in stream
32673267
static bool f_tmr_1s;
32683268
static bool f_stream; // first audio data received
32693269
static int bytesDecoded;
3270+
static bool firstBytes;
32703271
static uint32_t byteCounter; // count received data
32713272
static size_t chunkSize = 0;
32723273
static uint32_t tmr_1s; // timer 1 sec
32733274
static uint32_t loopCnt; // count loops if clientbuffer is empty
3275+
static uint16_t ID3WritePtr;
3276+
static uint16_t ID3ReadPtr;
3277+
static uint8_t* ID3Buff;
32743278

32753279
// first call, set some values to default - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
32763280
if(m_f_firstCall) { // runs only ont time per connection, prepare for start
@@ -3279,17 +3283,54 @@ void Audio::processWebStreamHLS() {
32793283
bytesDecoded = 0;
32803284
chunkSize = 0;
32813285
loopCnt = 0;
3286+
ID3WritePtr = 0;
3287+
ID3ReadPtr = 0;
32823288
tmr_1s = millis();
32833289
m_t0 = millis();
3284-
m_f_firstCall = false;
3290+
m_f_firstCall = false;
3291+
firstBytes = true;
3292+
ID3Buff = (uint8_t*)malloc(ID3BuffSize);
3293+
m_controlCounter = 0;
32853294
}
32863295

32873296
if(getDatamode() != AUDIO_DATA) return; // guard
32883297

32893298
availableBytes = _client->available();
3290-
if(availableBytes){
3299+
if(availableBytes){ // an ID3 header could come here
32913300
uint8_t readedBytes = 0;
3292-
if(m_f_chunked) chunkSize = chunkedDataTransfer(&readedBytes);
3301+
3302+
if(m_f_chunked && !chunkSize) {chunkSize = chunkedDataTransfer(&readedBytes); byteCounter += readedBytes;}
3303+
3304+
if(firstBytes){
3305+
if(ID3WritePtr < ID3BuffSize){
3306+
ID3WritePtr += _client->readBytes(&ID3Buff[ID3WritePtr], ID3BuffSize - ID3WritePtr);
3307+
return;
3308+
}
3309+
if(m_controlCounter < 100){
3310+
int res = read_ID3_Header(&ID3Buff[ID3ReadPtr], ID3BuffSize - ID3ReadPtr);
3311+
if(res >= 0) ID3ReadPtr += res;
3312+
if(ID3ReadPtr > ID3BuffSize) {log_e("buffer overflow"); stopSong(); return;}
3313+
return;
3314+
}
3315+
if(m_controlCounter != 100) return;
3316+
3317+
size_t ws = InBuff.writeSpace();
3318+
if(ws >= ID3BuffSize - ID3ReadPtr){
3319+
memcpy(InBuff.getWritePtr(), &ID3Buff[ID3ReadPtr], ID3BuffSize - ID3ReadPtr);
3320+
InBuff.bytesWritten(ID3BuffSize - ID3ReadPtr);
3321+
}
3322+
else{
3323+
memcpy(InBuff.getWritePtr(), &ID3Buff[ID3ReadPtr], ws);
3324+
InBuff.bytesWritten(ws);
3325+
memcpy(InBuff.getWritePtr(), &ID3Buff[ws + ID3ReadPtr], ID3BuffSize - (ID3ReadPtr + ws));
3326+
InBuff.bytesWritten(ID3BuffSize - (ID3ReadPtr + ws));
3327+
}
3328+
if(ID3Buff) free(ID3Buff);
3329+
byteCounter += ID3BuffSize;
3330+
ID3Buff = NULL;
3331+
firstBytes = false;
3332+
}
3333+
32933334
size_t bytesWasWritten = 0;
32943335
if(InBuff.writeSpace() >= availableBytes){
32953336
bytesWasWritten = _client->read(InBuff.getWritePtr(), availableBytes);
@@ -3298,6 +3339,7 @@ void Audio::processWebStreamHLS() {
32983339
bytesWasWritten = _client->read(InBuff.getWritePtr(), InBuff.writeSpace());
32993340
}
33003341
InBuff.bytesWritten(bytesWasWritten);
3342+
33013343
byteCounter += bytesWasWritten;
33023344
if(byteCounter == m_contentlength || byteCounter == chunkSize){
33033345
byteCounter = 0;
@@ -3342,7 +3384,11 @@ void Audio::processWebStreamHLS() {
33423384
}
33433385

33443386
// play audio data - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
3345-
if(f_stream) playAudioData();
3387+
if(f_stream){
3388+
static uint8_t cnt = 0;
3389+
cnt++;
3390+
if(cnt == 1){playAudioData(); cnt = 0;}
3391+
}
33463392
return;
33473393
}
33483394
//---------------------------------------------------------------------------------------------------------------------

src/Audio.h

+2-1
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: Aug 23,2022
5+
* Updated on: Aug 25,2022
66
* Author: Wolle (schreibfaul1)
77
*/
88

@@ -163,6 +163,7 @@ class Audio : private AudioBuffer{
163163
~Audio();
164164
void setBufsize(int rambuf_sz, int psrambuf_sz);
165165
bool connecttohost(const char* host, const char* user = "", const char* pwd = "");
166+
166167
bool connecttospeech(const char* speech, const char* lang);
167168
bool connecttomarytts(const char* speech, const char* lang, const char* voice);
168169
bool connecttoFS(fs::FS &fs, const char* path, uint32_t resumeFilePos = 0);

0 commit comments

Comments
 (0)