2 #include "AudioConfig.h"
5 #include "AbstractMetaData.h"
6 #include "AudioTools/CoreAudio/AudioBasic/StrView.h"
7 #include "AudioTools/CoreAudio/AudioHttp/HttpRequest.h"
28 enum Status {ProcessData, ProcessMetaData, SetupSize};
39 if (metaData!=
nullptr)
delete[]metaData;
44 this->mp3_blocksize = value;
54 dataBuffer =
new uint8_t[bufferLen];
63 LOGI(
"mp3_blocksize: %d", mp3_blocksize);
67 virtual void end()
override {
72 virtual size_t write(
const uint8_t *data,
size_t len)
override {
73 if (callback!=
nullptr){
74 for (
size_t j=0;j<len;j++){
88 return currentStatus==ProcessData;
93 return this->mp3_blocksize>0;
105 currentStatus = ProcessData;
110 if (totalData>=mp3_blocksize){
113 nextStatus = SetupSize;
118 currentStatus = SetupSize;
122 LOGI(
"metaDataLen: %d", metaDataLen);
124 if (metaDataLen>200){
125 LOGI(
"Unexpected metaDataLen -> processed as data");
126 nextStatus = ProcessData;
128 LOGI(
"Metadata found");
130 nextStatus = ProcessMetaData;
134 nextStatus = ProcessData;
138 case ProcessMetaData:
139 currentStatus = ProcessMetaData;
140 metaData[metaDataPos++]=ch;
141 if (metaDataPos>=metaDataLen){
143 LOGI(
"Metadata ended")
144 nextStatus = ProcessData;
152 Status nextStatus = ProcessData;
153 Status currentStatus = ProcessData;
154 void (*callback)(
MetaDataType info,
const char* str,
int len) =
nullptr;
155 char* metaData=
nullptr;
157 int mp3_blocksize = 0;
158 int metaDataMaxLen = 0;
163 uint8_t *dataBuffer=
nullptr;
164 void (*dataCallback)(
const uint8_t* str,
int len) =
nullptr;
168 virtual void clear() {
169 nextStatus = ProcessData;
185 int m = l < 5 ? l : 10;
186 for (
int j=0; j<m; j++){
187 if (!isascii(result[j]))
return false;
196 if (metaData==
nullptr){
197 metaData =
new char[meta_size+1];
198 metaDataMaxLen = meta_size;
199 LOGD(
"metaDataMaxLen: %d", metaDataMaxLen);
201 if (meta_size>metaDataMaxLen){
203 metaData =
new char[meta_size+1];
204 metaDataMaxLen = meta_size;
205 LOGD(
"metaDataMaxLen: %d", metaDataMaxLen);
208 memset(metaData, 0, meta_size);
218 LOGI(
"%s", metaData);
219 StrView meta(metaData,len+1, len);
220 int start = meta.
indexOf(
"StreamTitle=");
225 if (start>=0 &&
end>start){
227 if (callback!=
nullptr){
228 callback(Title, (
const char*)metaData+start+1,
end-start);
234 LOGW(
"Unexpected Data: %s", metaData);
240 if (dataBuffer!=
nullptr){
241 dataBuffer[dataPos++] = ch;
243 if (dataPos>=dataLen){
244 dataCallback(dataBuffer, dataLen);
265 const char* iceMetaintStr = http.reply().get(
"icy-metaint");
267 LOGI(
"icy-metaint: %s", iceMetaintStr);
269 LOGE(
"icy-metaint not defined");
272 int iceMetaint = value.
toInt();
279 if (callback==
nullptr){
280 LOGW(
"callback not defined")
282 if (p_http==
nullptr){
283 LOGW(
"http not defined")
286 if (callback!=
nullptr && p_http!=
nullptr) {
288 StrView genre(p_http->reply().get(
"icy-genre"));
293 StrView descr(p_http->reply().get(
"icy-description"));
295 callback(Description, descr.
c_str(), descr.
length());
298 StrView name(p_http->reply().get(
"icy-name"));