arduino-audio-tools
All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends Modules Pages
ICYStreamT.h
1#pragma once
2#include "AudioConfig.h"
3#include "AudioTools/CoreAudio/AudioHttp/AbstractURLStream.h"
4#include "AudioTools/CoreAudio/AudioMetaData/MetaDataICY.h"
5
6namespace audio_tools {
7
25 template<class T>
27 public:
28 ICYStreamT(int readBufferSize = DEFAULT_BUFFER_SIZE) {
29 TRACEI();
30 setReadBufferSize(readBufferSize);
31 }
32
34 ICYStreamT(const char* ssid, const char* password,
35 int readBufferSize = DEFAULT_BUFFER_SIZE) : ICYStreamT(readBufferSize) {
36 TRACEI();
37 setSSID(ssid);
38 setPassword(password);
39 }
40
41 ICYStreamT(Client& clientPar, int readBufferSize = DEFAULT_BUFFER_SIZE) : ICYStreamT(readBufferSize) {
42 TRACEI();
44 }
45
47 virtual bool setMetadataCallback(void (*fn)(MetaDataType info,
48 const char* str,
49 int len)) override {
50 TRACED();
51 callback = fn;
52 icy.setCallback(fn);
53 return true;
54 }
55
56 // Icy http get request to the indicated url
57 virtual bool begin(const char* urlStr, const char* acceptMime = nullptr,
58 MethodID action = GET, const char* reqMime = "",
59 const char* reqData = "") override {
60 TRACED();
61 // accept metadata
62 addRequestHeader("Icy-MetaData", "1");
63 bool result = url.begin(urlStr, acceptMime, action, reqMime, reqData);
64
65 if (result) {
66 // setup icy
68 int iceMetaint = icySetup.setup(*this);
69 // callbacks from http request
70 icySetup.executeCallback(callback);
72 icy.begin();
73
74 if (!icy.hasMetaData()) {
75 LOGW("url does not provide metadata");
76 }
77 }
78 return result;
79 }
80
82 virtual void end() override {
83 TRACED();
84 url.end();
85 icy.end();
86 }
87
89 virtual int available() override { return url.available(); }
90
92 virtual size_t readBytes(uint8_t* data, size_t len) override {
93 size_t result = 0;
94 if (icy.hasMetaData()) {
95 // get data
96 int read = url.readBytes(data, len);
97 // remove metadata from data
98 int pos = 0;
99 for (int j = 0; j < read; j++) {
100 icy.processChar(data[j]);
101 if (icy.isData()) {
102 data[pos++] = data[j];
103 }
104 }
105 result = pos;
106 } else {
107 // fast access if there is no metadata
108 result = url.readBytes(data, len);
109 }
110 LOGD("%s: %zu -> %zu", LOG_METHOD, len, result);
111 return result;
112 }
113
114 // Read character and processes using the MetaDataICY state engine
115 virtual int read() override {
116 int ch = -1;
117
118 // get next data byte
119 do {
120 ch = url.read();
121 if (ch == -1) {
122 break;
123 }
124
125 icy.processChar(ch);
126 } while (!icy.isData());
127 return ch;
128 }
129
130 operator bool() { return url; }
131
132 void setReadBufferSize(int readBufferSize) {
133 url.setReadBufferSize(readBufferSize);
134 }
135
137 void setSSID(const char* ssid) override { url.setSSID(ssid); }
138
140 void setPassword(const char* password) override { url.setPassword(password); }
141
144 void setPowerSave(bool active) { url.setPowerSave(active);}
145
147 void setCACert(const char* cert){
148 url.setCACert(cert);
149 }
151 void addRequestHeader(const char* key, const char* value) override {
152 url.addRequestHeader(key, value);
153 }
155 const char* getReplyHeader(const char* key) override {
156 return url.getReplyHeader(key);
157 }
158
160 virtual HttpRequest& httpRequest() override { return url.httpRequest(); }
161
163 void setClient(Client& client) override { url.setClient(client); }
164
165 protected:
166 T url;
167 MetaDataICY icy; // icy state machine
168 void (*callback)(MetaDataType info, const char* str, int len) = nullptr;
169};
170
171}
172
Abstract Base class for all URLStream implementations.
Definition AbstractURLStream.h:17
Definition NoArduino.h:167
Simple API to process get, put, post, del http requests I tried to use Arduino HttpClient,...
Definition HttpRequest.h:25
Icecast/Shoutcast Audio Stream which splits the data into metadata and audio data....
Definition ICYStreamT.h:26
virtual size_t readBytes(uint8_t *data, size_t len) override
reads the audio bytes
Definition ICYStreamT.h:92
void setPowerSave(bool active)
Definition ICYStreamT.h:144
void addRequestHeader(const char *key, const char *value) override
Adds/Updates a request header.
Definition ICYStreamT.h:151
void setCACert(const char *cert)
Define the Root PEM Certificate for SSL:
Definition ICYStreamT.h:147
void setSSID(const char *ssid) override
Sets the ssid that will be used for logging in (when calling begin)
Definition ICYStreamT.h:137
ICYStreamT(const char *ssid, const char *password, int readBufferSize=DEFAULT_BUFFER_SIZE)
Default constructor.
Definition ICYStreamT.h:34
void setClient(Client &client) override
(Re-)defines the client
Definition ICYStreamT.h:163
virtual bool setMetadataCallback(void(*fn)(MetaDataType info, const char *str, int len)) override
Defines the meta data callback function.
Definition ICYStreamT.h:47
void setPassword(const char *password) override
Sets the password that will be used for logging in (when calling begin)
Definition ICYStreamT.h:140
virtual void end() override
Ends the processing.
Definition ICYStreamT.h:82
virtual HttpRequest & httpRequest() override
provides access to the HttpRequest
Definition ICYStreamT.h:160
virtual int available() override
provides the available method from the URLStream
Definition ICYStreamT.h:89
const char * getReplyHeader(const char *key) override
Provides reply header info.
Definition ICYStreamT.h:155
Resolve icy-metaint from HttpRequest and execute metadata callbacks.
Definition MetaDataICY.h:235
Icecast/Shoutcast Metadata Handling. Metadata class which splits the data into audio and metadata....
Definition MetaDataICY.h:25
virtual void setCallback(void(*fn)(MetaDataType info, const char *str, int len)) override
Defines the metadata callback function.
Definition MetaDataICY.h:42
virtual void begin() override
Resets all counters and restarts the prcessing.
Definition MetaDataICY.h:57
virtual bool hasMetaData()
Returns true if the ICY stream contains metadata.
Definition MetaDataICY.h:83
virtual void end() override
Resets all counters and restarts the prcessing.
Definition MetaDataICY.h:63
virtual void setIcyMetaInt(int value) override
Defines the ICE metaint value which is provided by the web call!
Definition MetaDataICY.h:37
virtual bool isData()
returns true if the actual bytes is an audio data byte (e.g.mp3)
Definition MetaDataICY.h:80
virtual void processChar(char ch)
character based state engine
Definition MetaDataICY.h:89
Vector implementation which provides the most important methods as defined by std::vector....
Definition Vector.h:21
MetaDataType
Type of meta info.
Definition AbstractMetaData.h:11
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition AudioConfig.h:885