From 4b2bbe7ad517646b27a4414c996e48aa98c37123 Mon Sep 17 00:00:00 2001 From: "Earle F. Philhower, III" Date: Tue, 21 Nov 2017 11:48:20 -0800 Subject: [PATCH] Preliminary ICY HTTP stream support Add a subclass of the HTTPStream that can extract the ICY metadata. Needs to implement a callback function or parse it internally and expose a class variable to let the UI see the current stream information. --- src/AudioFileSourceHTTPStream.cpp | 3 +- src/AudioFileSourceHTTPStream.h | 4 +- src/AudioFileSourceICYStream.cpp | 130 ++++++++++++++++++++++++++++++ src/AudioFileSourceICYStream.h | 54 +++++++++++++ 4 files changed, 189 insertions(+), 2 deletions(-) create mode 100644 src/AudioFileSourceICYStream.cpp create mode 100644 src/AudioFileSourceICYStream.h diff --git a/src/AudioFileSourceHTTPStream.cpp b/src/AudioFileSourceHTTPStream.cpp index 42aa75d..3983668 100644 --- a/src/AudioFileSourceHTTPStream.cpp +++ b/src/AudioFileSourceHTTPStream.cpp @@ -68,8 +68,9 @@ uint32_t AudioFileSourceHTTPStream::readNonBlock(void *data, uint32_t len) uint32_t AudioFileSourceHTTPStream::readInternal(void *data, uint32_t len, bool nonBlock) { if (!http.connected()) { - http.end(); Serial.println("Stream disconnected\n"); + Serial.flush(); + http.end(); if (reconnect) { if (!open(saveURL)) { return 0; diff --git a/src/AudioFileSourceHTTPStream.h b/src/AudioFileSourceHTTPStream.h index 8e40fcd..4e79a6b 100644 --- a/src/AudioFileSourceHTTPStream.h +++ b/src/AudioFileSourceHTTPStream.h @@ -28,6 +28,8 @@ class AudioFileSourceHTTPStream : public AudioFileSource { + friend class AudioFileSourceICYStream; + public: AudioFileSourceHTTPStream(); AudioFileSourceHTTPStream(const char *url); @@ -44,7 +46,7 @@ class AudioFileSourceHTTPStream : public AudioFileSource bool SetReconnect(bool val) { reconnect = val; return true; } private: - uint32_t readInternal(void *data, uint32_t len, bool nonBlock); + virtual uint32_t readInternal(void *data, uint32_t len, bool nonBlock); HTTPClient http; int pos; int size; diff --git a/src/AudioFileSourceICYStream.cpp b/src/AudioFileSourceICYStream.cpp new file mode 100644 index 0000000..f5efe7c --- /dev/null +++ b/src/AudioFileSourceICYStream.cpp @@ -0,0 +1,130 @@ +/* + AudioFileSourceICYStream + Streaming Shoutcast ICY source + + Copyright (C) 2017 Earle F. Philhower, III + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +#include "AudioFileSourceICYStream.h" + +AudioFileSourceICYStream::AudioFileSourceICYStream() +{ + pos = 0; + reconnect = false; + saveURL = NULL; +} + +AudioFileSourceICYStream::AudioFileSourceICYStream(const char *url) +{ + saveURL = NULL; + reconnect = false; + open(url); +} + +bool AudioFileSourceICYStream::open(const char *url) +{ + static const char *hdr[] = { "icy-metaint" }; + pos = 0; + http.begin(url); + http.addHeader("Icy-MetaData", "1"); + http.collectHeaders( hdr, 1 ); + http.setReuse(true); + int code = http.GET(); + if (code != HTTP_CODE_OK) { + http.end(); + return false; + } + if (http.hasHeader(hdr[0])) { + String ret = http.header(hdr[0]); + icyMetaInt = ret.toInt(); + Serial.printf("ICY metaint = %d\n", icyMetaInt); + } else { + icyMetaInt = 0; + } + icyByteCount = 0; + size = http.getSize(); + Serial.printf("Stream size: %d\n", size); + free(saveURL); + saveURL = strdup(url); + return true; +} + +AudioFileSourceICYStream::~AudioFileSourceICYStream() +{ + http.end(); +} + +uint32_t AudioFileSourceICYStream::readInternal(void *data, uint32_t len, bool nonBlock) +{ + if (!http.connected()) { + Serial.println("Stream disconnected\n"); + Serial.flush(); + http.end(); + if (reconnect) { + if (!open(saveURL)) { + return 0; + } else { + Serial.println("Reconnected to stream"); + } + } else { + return 0; + } + } + if ((size>0) && (pos >= size)) return 0; + + WiFiClient *stream = http.getStreamPtr(); + + // Can't read past EOF... + if ( (size > 0) && (len > (uint32_t)(pos - size)) ) len = pos - size; + + if (!nonBlock) { + int start = millis(); + while ((stream->available() < (int)len) && (millis() - start < 500)) yield(); + } + + size_t avail = stream->available(); + if (!avail) return 0; + if (avail < len) len = avail; + + int read = 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 = stream->readBytes(reinterpret_cast(data), beforeIcy); + read += ret; + pos += ret; + len -= ret; + icyByteCount += ret; + uint8_t mdSize; + int mdret = stream->readBytes(&mdSize, 1); + if ((mdret == 1) && (mdSize > 0)) { + char *mdBuff = (char*)malloc(mdSize * 16); + mdret = stream->readBytes(reinterpret_cast(mdBuff), mdSize * 16); + if (mdret == mdSize * 16) { + Serial.printf("ICY metadata:\n%s\n---end---\n", mdBuff); + free(mdBuff); + } + } + icyByteCount = 0; + } + + int ret = stream->readBytes(reinterpret_cast(data), len); + read += ret; + pos += ret; + icyByteCount += ret; + return read; +} + diff --git a/src/AudioFileSourceICYStream.h b/src/AudioFileSourceICYStream.h new file mode 100644 index 0000000..1a4bb3b --- /dev/null +++ b/src/AudioFileSourceICYStream.h @@ -0,0 +1,54 @@ +/* + AudioFileSourceHTTPStream + Connect to a HTTP based streaming service + + Copyright (C) 2017 Earle F. Philhower, III + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +#ifndef _AUDIOFILESOURCEICYSTREAM_H +#define _AUDIOFILESOURCEICYSTREAM_H + +#include +#include + +#include "AudioFileSourceHTTPStream.h" + +class AudioFileSourceICYStream : public AudioFileSourceHTTPStream +{ + public: + AudioFileSourceICYStream(); + AudioFileSourceICYStream(const char *url); + virtual ~AudioFileSourceICYStream() override; + + virtual bool open(const char *url) override; +// virtual uint32_t read(void *data, uint32_t len) override; +// virtual uint32_t readNonBlock(void *data, uint32_t len) override; +// virtual bool seek(int32_t pos, int dir) override; +// virtual bool close() override; +// virtual bool isOpen() override; +// virtual uint32_t getSize() override; +// virtual uint32_t getPos() override; +// bool SetReconnect(bool val) { reconnect = val; return true; } + + private: + virtual uint32_t readInternal(void *data, uint32_t len, bool nonBlock) override; + int icyMetaInt; + int icyByteCount; +}; + + +#endif +