DataSources: Implemented ID3 tag skipping for HTTPSDataSources.

This commit is contained in:
Fabian Schlenz 2019-12-04 06:07:20 +01:00
parent fa208858d9
commit 84530f76fd
4 changed files with 46 additions and 1 deletions

View File

@ -41,6 +41,8 @@ private:
WiFiClient* _stream = NULL; WiFiClient* _stream = NULL;
HTTPClientWrapper* _http = NULL; HTTPClientWrapper* _http = NULL;
uint32_t _position; uint32_t _position;
String _url;
void _init(String url, uint32_t offset);
public: public:
HTTPSDataSource(String url, uint32_t offset=0); HTTPSDataSource(String url, uint32_t offset=0);
~HTTPSDataSource(); ~HTTPSDataSource();
@ -51,4 +53,6 @@ public:
size_t size(); size_t size();
void close(); void close();
bool usable(); bool usable();
int peek(int offset=0);
void skip_id3_tag();
}; };

View File

@ -34,4 +34,5 @@ public:
uint32_t getSize(); uint32_t getSize();
String readUntil(String sep); String readUntil(String sep);
String readLine(); String readLine();
int peek(int offset=0);
}; };

View File

@ -39,6 +39,12 @@ void SDDataSource::skip_id3_tag() {
////////////// HTTPSDataSource ////////////// ////////////// HTTPSDataSource //////////////
HTTPSDataSource::HTTPSDataSource(String url, uint32_t offset) { HTTPSDataSource::HTTPSDataSource(String url, uint32_t offset) {
_url = url;
_init(url, offset);
}
void HTTPSDataSource::_init(String url, uint32_t offset) {
_url = url;
_http = new HTTPClientWrapper(); _http = new HTTPClientWrapper();
if (!_http->get(url, offset)) return; if (!_http->get(url, offset)) return;
_position = 0; _position = 0;
@ -52,6 +58,31 @@ bool HTTPSDataSource::usable() { return _http; }
size_t HTTPSDataSource::read(uint8_t* buf, size_t len) { size_t result = _http->read(buf, len); _position += result; return result; } size_t HTTPSDataSource::read(uint8_t* buf, size_t len) { size_t result = _http->read(buf, len); _position += result; return result; }
int HTTPSDataSource::read() { int b = _http->read(); if (b>=0) _position++; return b; } int HTTPSDataSource::read() { int b = _http->read(); if (b>=0) _position++; return b; }
size_t HTTPSDataSource::position() { return _position; } size_t HTTPSDataSource::position() { return _position; }
void HTTPSDataSource::seek(size_t position) { return; /* TODO */ } void HTTPSDataSource::seek(size_t position) { _http->close(); delete _http; _init(_url, position); }
size_t HTTPSDataSource::size() { return _http->getSize(); } size_t HTTPSDataSource::size() { return _http->getSize(); }
void HTTPSDataSource::close() { _http->close(); } void HTTPSDataSource::close() { _http->close(); }
int HTTPSDataSource::peek(int offset) { return _http->peek(offset); }
void HTTPSDataSource::skip_id3_tag() {
uint32_t offset = 0;
if (_http->peek(0)=='I' && _http->peek(1)=='D' && _http->peek(2)=='3') {
DEBUG("ID3 tag found\n");
// Skip ID3 tag marker
_http->read(); _http->read(); _http->read();
// Skip ID3 tag version
_http->read(); _http->read();
byte tags = _http->read();
bool footer_present = tags & 0x10;
DEBUG("ID3 footer found: %d\n", footer_present);
for (byte i=0; i<4; i++) {
offset <<= 7;
offset |= (0x7F & _http->read());
}
offset += 10;
if (footer_present) offset += 10;
DEBUG("ID3 tag length is %d bytes.\n", offset);
seek(offset);
} else {
DEBUG("No ID3 tag found\n");
}
}

View File

@ -211,3 +211,12 @@ String HTTPClientWrapper::readUntil(String sep) {
String HTTPClientWrapper::readLine() { String HTTPClientWrapper::readLine() {
return readUntil("\n\r"); return readUntil("\n\r");
} }
int HTTPClientWrapper::peek(int offset) {
if (_buffer_position >= _buffer_length) _fill_buffer();
if (_buffer_position + offset < 0 || _buffer_position + offset >= _buffer_length) {
return -1;
}
return _buffer[_buffer_position + offset];
}