From c5da8c306217dac8686a3476e245db8dfb3b63b8 Mon Sep 17 00:00:00 2001 From: "Earle F. Philhower, III" Date: Sun, 22 Oct 2017 09:38:50 -0700 Subject: [PATCH] Add timeout and HTTP size to streamer EOF checks HTTPStreamer assumed the server would drop the connection when no more data was available to be transferred from the source file. For individual files, though, the web server will send the expected length in the headers and leave the connection open (i.e. to recycle the connection and accelerate web browsing). In this case, the streamer would effectively hang waiting for more data than was being transmitted. Now check the read data against the expected data, and also add a 0.5s timeout no matter what to the reading. Tested on streaming and served files, seems to work fine. --- src/AudioFileSourceHTTPStream.cpp | 11 +++++++++-- src/AudioFileSourceHTTPStream.h | 3 ++- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/src/AudioFileSourceHTTPStream.cpp b/src/AudioFileSourceHTTPStream.cpp index 10c6b0c..9ee03da 100644 --- a/src/AudioFileSourceHTTPStream.cpp +++ b/src/AudioFileSourceHTTPStream.cpp @@ -40,6 +40,7 @@ bool AudioFileSourceHTTPStream::open(const char *url) http.end(); return false; } + size = http.getSize(); return true; } @@ -51,9 +52,15 @@ AudioFileSourceHTTPStream::~AudioFileSourceHTTPStream() uint32_t AudioFileSourceHTTPStream::read(void *data, uint32_t len) { if (!http.connected()) return 0; + if ((size>0) && (pos >= size)) return 0; WiFiClient *stream = http.getStreamPtr(); - while (stream->available() < (int)len) yield(); + + // Can't read past EOF... + if ( (size > 0) && (len > (uint32_t)(pos - size)) ) len = pos - size; + + int start = millis(); + while ((stream->available() < (int)len) && (millis() - start < 500)) yield(); size_t avail = stream->available(); if (!avail) return 0; @@ -84,7 +91,7 @@ bool AudioFileSourceHTTPStream::isOpen() uint32_t AudioFileSourceHTTPStream::getSize() { - return 1L<<31; + return size; } uint32_t AudioFileSourceHTTPStream::getPos() diff --git a/src/AudioFileSourceHTTPStream.h b/src/AudioFileSourceHTTPStream.h index ace7a94..7cd1428 100644 --- a/src/AudioFileSourceHTTPStream.h +++ b/src/AudioFileSourceHTTPStream.h @@ -43,7 +43,8 @@ class AudioFileSourceHTTPStream : public AudioFileSource private: HTTPClient http; - uint32_t pos; + int pos; + int size; };