diff --git a/src/AudioFileSourceHTTPStream.cpp b/src/AudioFileSourceHTTPStream.cpp index 3983668..787fbc7 100644 --- a/src/AudioFileSourceHTTPStream.cpp +++ b/src/AudioFileSourceHTTPStream.cpp @@ -23,14 +23,14 @@ AudioFileSourceHTTPStream::AudioFileSourceHTTPStream() { pos = 0; - reconnect = false; + reconnectTries = 0; saveURL = NULL; } AudioFileSourceHTTPStream::AudioFileSourceHTTPStream(const char *url) { saveURL = NULL; - reconnect = false; + reconnectTries = 0; open(url); } @@ -67,21 +67,27 @@ uint32_t AudioFileSourceHTTPStream::readNonBlock(void *data, uint32_t len) uint32_t AudioFileSourceHTTPStream::readInternal(void *data, uint32_t len, bool nonBlock) { +retry: if (!http.connected()) { Serial.println("Stream disconnected\n"); Serial.flush(); http.end(); - if (reconnect) { - if (!open(saveURL)) { - return 0; - } else { + for (int i = 0; i < reconnectTries; i++) { + Serial.printf("Attempting to reconnect, try %d\n", i); + Serial.flush(); + delay(reconnectDelayMs); + if (open(saveURL)) { Serial.println("Reconnected to stream"); + break; } - } else { + } + if (!http.connected()) { + Serial.printf("Unable to reconnect\n"); + Serial.flush(); return 0; } } - if ((size>0) && (pos >= size)) return 0; + if ((size > 0) && (pos >= size)) return 0; WiFiClient *stream = http.getStreamPtr(); @@ -94,7 +100,12 @@ uint32_t AudioFileSourceHTTPStream::readInternal(void *data, uint32_t len, bool } size_t avail = stream->available(); - if (!avail) return 0; + if (!nonBlock && !avail) { + Serial.printf("No stream data available\n"); + http.end(); + goto retry; + } + if (avail == 0) return 0; if (avail < len) len = avail; int read = stream->readBytes(reinterpret_cast(data), len); diff --git a/src/AudioFileSourceHTTPStream.h b/src/AudioFileSourceHTTPStream.h index 4e79a6b..4376055 100644 --- a/src/AudioFileSourceHTTPStream.h +++ b/src/AudioFileSourceHTTPStream.h @@ -43,14 +43,15 @@ class AudioFileSourceHTTPStream : public AudioFileSource virtual bool isOpen() override; virtual uint32_t getSize() override; virtual uint32_t getPos() override; - bool SetReconnect(bool val) { reconnect = val; return true; } + bool SetReconnect(int tries, int delayms) { reconnectTries = tries; reconnectDelayMs = delayms; return true; } private: virtual uint32_t readInternal(void *data, uint32_t len, bool nonBlock); HTTPClient http; int pos; int size; - bool reconnect; + int reconnectTries; + int reconnectDelayMs; char *saveURL; }; diff --git a/src/AudioFileSourceICYStream.cpp b/src/AudioFileSourceICYStream.cpp index 9a82865..374200b 100644 --- a/src/AudioFileSourceICYStream.cpp +++ b/src/AudioFileSourceICYStream.cpp @@ -23,14 +23,14 @@ AudioFileSourceICYStream::AudioFileSourceICYStream() { pos = 0; - reconnect = false; + reconnectTries = 0; saveURL = NULL; } AudioFileSourceICYStream::AudioFileSourceICYStream(const char *url) { saveURL = NULL; - reconnect = false; + reconnectTries = 0; open(url); } @@ -69,21 +69,27 @@ AudioFileSourceICYStream::~AudioFileSourceICYStream() uint32_t AudioFileSourceICYStream::readInternal(void *data, uint32_t len, bool nonBlock) { +retry: if (!http.connected()) { Serial.println("Stream disconnected\n"); Serial.flush(); http.end(); - if (reconnect) { - if (!open(saveURL)) { - return 0; - } else { + for (int i = 0; i < reconnectTries; i++) { + Serial.printf("Attempting to reconnect, try %d\n", i); + Serial.flush(); + delay(reconnectDelayMs); + if (open(saveURL)) { Serial.println("Reconnected to stream"); + break; } - } else { + } + if (!http.connected()) { + Serial.printf("Unable to reconnect\n"); + Serial.flush(); return 0; } } - if ((size>0) && (pos >= size)) return 0; + if ((size > 0) && (pos >= size)) return 0; WiFiClient *stream = http.getStreamPtr(); @@ -96,7 +102,13 @@ uint32_t AudioFileSourceICYStream::readInternal(void *data, uint32_t len, bool n } size_t avail = stream->available(); - if (!avail) return 0; + if (!nonBlock && !avail) { + Serial.printf("No stream data available\n"); + Serial.flush(); + http.end(); + goto retry; + } + if (avail == 0) return 0; if (avail < len) len = avail; int read = 0; @@ -134,24 +146,3 @@ uint32_t AudioFileSourceICYStream::readInternal(void *data, uint32_t len, bool n icyByteCount += ret; return read; } -extern "C" { -void flush() { Serial.flush(); } -extern "C" { - #include - extern cont_t g_cont; - - void stack(const char *s, const char *t, int i) { - (void) t; - (void) i; - register uint32_t *sp asm("a1"); - int freestack = 4 * (sp - g_cont.stack); - int freeheap = ESP.getFreeHeap(); - static int laststack, lastheap; - if (laststack!=freestack|| lastheap !=freeheap) - Serial.printf("%s: FREESTACK=%d, FREEHEAP=%d\n", s, /*t, i,*/ freestack, /*cont_get_free_stack(&g_cont),*/ freeheap); Serial.flush(); - if (freestack < 256) {Serial.printf("out of stack!\n"); while (1); } - if (freeheap < 1024) {Serial.printf("out of heap!\n"); while (1); } - laststack=freestack;lastheap=freeheap; - } -} -}; diff --git a/src/AudioGeneratorMP3.cpp b/src/AudioGeneratorMP3.cpp index fba7ba5..451ebb2 100644 --- a/src/AudioGeneratorMP3.cpp +++ b/src/AudioGeneratorMP3.cpp @@ -80,13 +80,18 @@ enum mad_flow AudioGeneratorMP3::Input() unused = buffLen - (stream.next_frame - buff); memmove(buff, stream.next_frame, unused); } - if (unused == buffLen) - return MAD_FLOW_STOP; + if (unused == buffLen) { + // Something wicked this way came, throw it all out and try again + unused = 0; + } lastReadPos = file->getPos() - unused; int len = buffLen - unused; len = file->read(buff + unused, len); - if (len == 0) return MAD_FLOW_STOP; + if (len == 0) { + Serial.println("MP3 stop, len==0"); + return MAD_FLOW_STOP; + } mad_stream_buffer(&stream, buff, len + unused); @@ -112,14 +117,17 @@ bool AudioGeneratorMP3::DecodeNextFrame() } inInnerDecode = false; } while (stream.error == MAD_ERROR_BUFLEN); - + Serial.println("stream.error != mad_Err_bufflen"); return false; } bool AudioGeneratorMP3::GetOneSample(int16_t sample[2]) { if ( (samplePtr >= synth.pcm.length) && (nsCount >= nsCountMax) ) { - if (!DecodeNextFrame()) return false; + if (!DecodeNextFrame()) { + Serial.println("DNF failed"); + return false; + } samplePtr = 9999; nsCount = 0; } @@ -143,7 +151,7 @@ bool AudioGeneratorMP3::GetOneSample(int16_t sample[2]) switch ( mad_synth_frame_onens(&synth, &frame, nsCount++) ) { case MAD_FLOW_STOP: - case MAD_FLOW_BREAK: + case MAD_FLOW_BREAK: Serial.println("msf1ns failed\n"); return false; // Either way we're done default: break; // Do nothing @@ -167,6 +175,7 @@ bool AudioGeneratorMP3::loop() do { if (!GetOneSample(lastSample)) { + Serial.println("G1S failed\n"); running = false; return false; }