Skip to content

Commit a26078f

Browse files
authored
recognize the end of a podcast correctly
the event audio_eof_mp3() is raised and webstream stops
1 parent cc1a493 commit a26078f

File tree

2 files changed

+32
-7
lines changed

2 files changed

+32
-7
lines changed

src/Audio.cpp

+30-6
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* Audio.cpp
33
*
44
* Created on: Oct 26,2018
5-
* Updated on: Apr 03,2019
5+
* Updated on: Apr 04,2019
66
* Author: Wolle
77
*
88
* This library plays mp3 files from SD card or icy-webstream via I2S
@@ -71,7 +71,7 @@ bool Audio::connecttohost(String host){
7171
if(audio_info) audio_info(chbuf);
7272

7373
// initializationsequence
74-
74+
m_f_podcast=false;
7575
m_f_ctseen=false; // Contents type not seen yet
7676
m_f_mp3=false; // Set if stream is mp3
7777
m_f_aac=false; // Set if stream is aac
@@ -181,6 +181,7 @@ bool Audio::connecttoSD(String sdfile){
181181
m_f_webstream=false;
182182
m_f_stream=false;
183183
m_f_mp3=true;
184+
m_f_podcast=false;
184185
memset(m_outBuff, 0, sizeof(m_outBuff)); //Clear OutputBuffer
185186
if(!sdfile.startsWith("/")) sdfile="/"+sdfile;
186187
while(sdfile[i] != 0){ //convert UTF8 to ASCII
@@ -428,7 +429,7 @@ void Audio::readID3Metadata(){
428429
char frameid[5];
429430
int framesize=0;
430431
bool compressed;
431-
char value[256];
432+
char value[256+65]; // tag (max65) + value (max 256)
432433
bool bitorder=false;
433434
uint8_t uni_h=0;
434435
uint8_t uni_l=0;
@@ -796,8 +797,15 @@ void Audio::loop() {
796797
}
797798
}
798799
if(m_f_firststream_ready==true){
799-
if(m_av==0){ // empty buffer, broken stream or bad bitrate?
800+
if(m_av==0){ // empty buffer, broken stream or bad bitrate?
800801
i++;
802+
if(m_f_podcast==true && i>3000){ // or end of podcast
803+
m_f_podcast=false;
804+
m_f_webstream=false;
805+
if(audio_info) audio_info("end of stream");
806+
if(audio_eof_mp3) audio_eof_mp3("Podcast");
807+
return;
808+
}
801809
if(i>200000){ // wait several seconds
802810
i=0;
803811
if(audio_info) audio_info("Stream lost -> try new connection");
@@ -1284,11 +1292,12 @@ bool Audio::chkhdrline(const char* str){
12841292
//---------------------------------------------------------------------------------------------------------------------
12851293
int Audio::sendBytes(uint8_t *data, size_t len) {
12861294
if(m_validSamples>0) {playChunk(); return 0;} //outputbuffer full or not ready, try again?
1287-
static int lastret=0, count=0;
1295+
static int lastret=0, count=0, ehsz=0;
12881296
if(!m_f_playing){
12891297

12901298
if ((data[0] == 'I') && (data[1] == 'D') && (data[2] == '3')){
12911299
// it is not a usual radio stream, it is a podcast
1300+
m_f_podcast=true;
12921301
log_i("Podcast found!");
12931302
m_id3Size = data[6]; m_id3Size = m_id3Size << 7;
12941303
m_id3Size |= data[7]; m_id3Size = m_id3Size << 7;
@@ -1311,8 +1320,23 @@ int Audio::sendBytes(uint8_t *data, size_t len) {
13111320
if(audio_info) audio_info(chbuf);
13121321
sprintf(chbuf,"ID3 framesSize=%i", m_id3Size);
13131322
if(audio_info) audio_info(chbuf);
1323+
if (m_f_exthdr) {
1324+
if(audio_info) audio_info("ID3 extended header");
1325+
ehsz = (data[10] << 24) | (data[11] << 16)
1326+
| (data[12] << 8) | (data[13]);
1327+
m_id3Size-=4;
1328+
return 14;
1329+
}
1330+
else{
1331+
if(audio_info) audio_info("ID3 normal frames");
1332+
return 10;
1333+
}
13141334
}
1315-
// skip ID3
1335+
if(ehsz>1024){ehsz-=1024; m_id3Size-=1024; return 1024;} // Throw exthdr away
1336+
if(ehsz> 0){int tmp=ehsz; m_id3Size-=ehsz; ehsz=0; return tmp;}
1337+
// TODO analyze ID3tags, not skip them
1338+
1339+
// skip ID3tags
13161340
if(m_id3Size>1000){
13171341
m_id3Size-=1000;
13181342
return 1000;

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 26,2018
5-
* Updated on: Apr 03,2019
5+
* Updated on: Apr 04,2019
66
* Author: Wolle (schreibfaul1)
77
*/
88

@@ -141,6 +141,7 @@ class Audio {
141141
String m_icystreamtitle ; // Streamtitle from metadata
142142
boolean m_ctseen=false; // First line of header seen or not
143143
boolean m_f_unsync = false;
144+
boolean m_f_podcast=false; // set if found ID3Header in stream
144145
boolean m_f_exthdr = false; // ID3 extended header
145146
boolean m_f_localfile = false ; // Play from local mp3-file
146147
boolean m_f_webstream = false ; // Play from URL

0 commit comments

Comments
 (0)