2 #include "AudioConfig.h"
3 #if defined(USE_CONCURRENCY) && defined(USE_URL_ARDUINO)
5 #include "AudioTools/AudioLibs/Concurrency.h"
6 #include "AudioTools/CoreAudio/AudioStreams.h"
8 #ifndef URL_STREAM_CORE
9 # define URL_STREAM_CORE 0
12 #ifndef URL_STREAM_PRIORITY
13 # define URL_STREAM_PRIORITY 2
16 #ifndef URL_STREAM_BUFFER_COUNT
17 # define URL_STREAM_BUFFER_COUNT 10
21 # define STACK_SIZE 30000
50 buffers.resize(bufferSize, bufferCount);
53 virtual void begin(
bool wait =
true) {
57 task.begin(std::bind(&BufferedTaskStream::processTask,
this));
58 if (!wait) ready =
true;
68 virtual void setInput(AudioStream &input) {
74 virtual size_t write(uint8_t c)
override {
return 0; }
77 virtual size_t write(
const uint8_t *data,
size_t len)
override {
return 0; }
80 virtual void flush()
override {}
83 virtual int read()
override {
86 result = buffers.
read();
91 virtual int peek()
override {
94 result = buffers.
peek();
99 virtual size_t readBytes(uint8_t *data,
size_t len)
override {
103 LOGD(
"%s: %zu -> %zu", LOG_METHOD, len, result);
119 Task task{
"BufferedTaskStream", STACK_SIZE, URL_STREAM_PRIORITY,
121 SynchronizedNBuffer<uint8_t> buffers{DEFAULT_BUFFER_SIZE,
122 URL_STREAM_BUFFER_COUNT};
127 if (*(this->p_stream) && available_to_write > 0) {
128 size_t to_read = min(available_to_write, (
size_t)512);
129 uint8_t buffer[to_read];
130 size_t avail_read = this->p_stream->readBytes((uint8_t *)buffer, to_read);
131 size_t written = this->buffers.
writeArray(buffer, avail_read);
133 if (written != avail_read) {
134 LOGE(
"DATA Lost! %zu reqested, %zu written!", avail_read, written);
142 if (available_to_write == 0) {
160 urlStream.setReadBufferSize(readBufferSize);
161 taskStream.setInput(urlStream);
165 int readBufferSize = DEFAULT_BUFFER_SIZE) {
167 urlStream.setReadBufferSize(readBufferSize);
169 taskStream.setInput(urlStream);
173 int readBufferSize = DEFAULT_BUFFER_SIZE) {
175 urlStream.setReadBufferSize(readBufferSize);
178 taskStream.setInput(urlStream);
181 void setBufferSize(
int bufferSize,
int bufferCount){
185 bool begin(
const char *urlStr,
const char *acceptMime =
nullptr,
186 MethodID action = GET,
const char *reqMime =
"",
187 const char *reqData =
"") {
190 bool result = urlStream.
begin(urlStr, acceptMime, action, reqMime, reqData);
196 virtual int available() {
return taskStream.
available(); }
198 virtual size_t readBytes(uint8_t *data,
size_t len) {
199 size_t result = taskStream.
readBytes(data, len);
200 LOGD(
"%s: %zu -> %zu", LOG_METHOD, len, result);
204 virtual int read() {
return taskStream.
read(); }
206 virtual int peek() {
return taskStream.
peek(); }
208 virtual void flush() {}
void stop()
Public generic methods.
Definition: AudioRuntime.h:27