diff --git a/src/AudioFileSourceICYStream.cpp b/src/AudioFileSourceICYStream.cpp
index 551619a..cc7cc5e 100644
--- a/src/AudioFileSourceICYStream.cpp
+++ b/src/AudioFileSourceICYStream.cpp
@@ -17,8 +17,10 @@
You should have received a copy of the GNU General Public License
along with this program. If not, see .
*/
+#define _GNU_SOURCE
#include "AudioFileSourceICYStream.h"
+#include
AudioFileSourceICYStream::AudioFileSourceICYStream()
{
@@ -36,11 +38,11 @@ AudioFileSourceICYStream::AudioFileSourceICYStream(const char *url)
bool AudioFileSourceICYStream::open(const char *url)
{
- static const char *hdr[] = { "icy-metaint" };
+ static const char *hdr[] = { "icy-metaint", "icy-name", "icy-genre", "icy-br" };
pos = 0;
http.begin(client, url);
http.addHeader("Icy-MetaData", "1");
- http.collectHeaders( hdr, 1 );
+ http.collectHeaders( hdr, 4 );
http.setReuse(true);
int code = http.GET();
if (code != HTTP_CODE_OK) {
@@ -54,6 +56,19 @@ bool AudioFileSourceICYStream::open(const char *url)
} else {
icyMetaInt = 0;
}
+ if (http.hasHeader(hdr[1])) {
+ String ret = http.header(hdr[1]);
+// cb.md("SiteName", false, ret.c_str());
+ }
+ if (http.hasHeader(hdr[2])) {
+ String ret = http.header(hdr[2]);
+// cb.md("Genre", false, ret.c_str());
+ }
+ if (http.hasHeader(hdr[3])) {
+ String ret = http.header(hdr[3]);
+// cb.md("Bitrate", false, ret.c_str());
+ }
+
icyByteCount = 0;
size = http.getSize();
strncpy(saveURL, url, sizeof(saveURL));
@@ -66,58 +81,6 @@ AudioFileSourceICYStream::~AudioFileSourceICYStream()
http.end();
}
-class ICYMDReader {
- public:
- ICYMDReader(WiFiClient *str, uint16_t bytes) {
- stream = str;
- avail = bytes;
- ptr = sizeof(buff)+1; // Cause read next time
- saved = -1;
- }
- ~ICYMDReader() {
- // Get rid of any remaining bytes in the MD block
- char xxx[16];
- if (saved>=0) avail--; // Throw away any unread bytes
- while (avail > 16) {
- stream->read(reinterpret_cast(xxx), 16);
- avail -= 16;
- }
- stream->read(reinterpret_cast(xxx), avail);
- }
- int read(uint8_t *dest, int len) {
- if (!len) return 0;
- int ret = 0;
- if (saved >= 0) { *(dest++) = (uint8_t)saved; saved = -1; len--; avail--; ret++; }
- while ((len>0) && (avail>0)) {
- // Always copy from bounce buffer first
- while ((ptr < sizeof(buff)) && (len>0) && (avail>0)) {
- *(dest++) = buff[ptr++];
- avail--;
- ret++;
- len--;
- }
- // refill the bounce buffer
- if ((avail>0) && (len>0)) {
- ptr = 0;
- int toRead = (sizeof(buff)>avail)? avail : sizeof(buff);
- int read = stream->read(buff, toRead);
- if (read != toRead) return 0; // Error, short read!
- }
- }
- return ret;
- }
- bool eof() { return (avail==0); }
- bool unread(uint8_t c) { if (saved>=0) return false; saved = c; avail++; return true; }
-
- private:
- WiFiClient *stream;
- uint16_t avail;
- uint8_t ptr;
- uint8_t buff[16];
- int saved;
-};
-
-
uint32_t AudioFileSourceICYStream::readInternal(void *data, uint32_t len, bool nonBlock)
{
retry:
@@ -161,10 +124,10 @@ retry:
if (avail < len) len = avail;
int read = 0;
+ int ret = 0;
// If the read would hit an ICY block, split it up...
if (((int)(icyByteCount + len) > (int)icyMetaInt) && (icyMetaInt > 0)) {
int beforeIcy = icyMetaInt - icyByteCount;
- int ret = 0;
if (beforeIcy > 0) {
ret = stream->read(reinterpret_cast(data), beforeIcy);
if (ret < 0) ret = 0;
@@ -176,59 +139,72 @@ retry:
if (ret != beforeIcy) return read; // Partial read
}
- uint8_t mdSize;
- int mdret = stream->read(&mdSize, 1);
+ // ICY MD handling
+ int mdSize;
+ uint8_t c;
+ int mdret = stream->read(&c, 1);
+ if (mdret==0) return read;
+ mdSize = c * 16;
if ((mdret == 1) && (mdSize > 0)) {
- ICYMDReader mdr(stream, mdSize * 16);
- // Break out (potentially multiple) NAME='xxxx'
- char type[32];
- char value[64];
- while (!mdr.eof()) {
- memset(type, 0, sizeof(type));
- memset(value, 0, sizeof(value));
- uint8_t c;
- char *p = type;
- for (size_t i=0; i 256 ? 256 : mdSize;
+ int ret = stream->read((uint8_t*)readInto, toRead);
+ if (ret < 0) return read;
+ if (ret == 0) { delay(1); continue; }
+ mdSize -= ret;
+ // At this point we have 0...15 = last 15 chars read from prior read plus new data
+ int end = 16 + ret; // The last byte of valid data
+ char *header = (char *)memmem((void*)icyBuff, end, (void*)"StreamTitle=", 12);
+ if (!header) {
+ // No match, so move the last 16 bytes back to the start and continue
+ memmove(icyBuff, icyBuff+end-16, 16);
+ delay(1);
+ continue;
}
- if (c != '=') {
- // MD type was too long, read remainder and throw away
- while ((c != '=') && !mdr.eof()) mdr.read(&c, 1);
+ // Found header, now move it to the front
+ int lastValidByte = end - (header -icyBuff) + 1;
+ memmove(icyBuff, header, lastValidByte);
+ // Now fill the buffer to the end with read data
+ while (mdSize && lastValidByte < 255) {
+ int toRead = mdSize > (256 - lastValidByte) ? (256 - lastValidByte) : mdSize;
+ ret = stream->read((uint8_t*)icyBuff + lastValidByte, toRead);
+ if (ret==-1) return read; // error
+ if (ret == 0) { delay(1); continue; }
+ mdSize -= ret;
+ lastValidByte += ret;
}
- mdr.read(&c, 1);
- if (c=='\'') {
- // Got start of string value, read that, too
- p = value;
- for (size_t i=0; i 256 ? 256 : mdSize;
+ ret = stream->read((uint8_t*)icyBuff, toRead);
+ if (ret < 0) return read;
+ if (ret == 0) { delay(1); continue; }
+ mdSize -= ret;
}
}
}
icyByteCount = 0;
}
- int ret = stream->read(reinterpret_cast(data), len);
+ ret = stream->read(reinterpret_cast(data), len);
if (ret < 0) ret = 0;
read += ret;
pos += ret;