7#include <freertos/FreeRTOS.h>
8#include <freertos/task.h>
14static const char *
const TAG =
"audio_http_media_source";
17static constexpr size_t DEFAULT_TRANSFER_BUFFER_SIZE = 8 * 1024;
18static constexpr uint32_t HTTP_TIMEOUT_MS = 5000;
19static constexpr uint32_t AUDIO_WRITE_TIMEOUT_MS = 50;
20static constexpr uint32_t READER_WRITE_TIMEOUT_MS = 50;
21static constexpr uint8_t READER_TASK_PRIORITY = 2;
22static constexpr uint8_t DECODER_TASK_PRIORITY = 2;
23static constexpr size_t READER_TASK_STACK_SIZE = 4096;
24static constexpr size_t DECODER_TASK_STACK_SIZE = 5120;
25static constexpr uint32_t PAUSE_POLL_DELAY_MS = 20;
26static constexpr const char *
const HTTP_URI_PREFIX =
"http://";
27static constexpr const char *
const HTTPS_URI_PREFIX =
"https://";
31 "Audio HTTP Media Source:\n"
32 " Buffer Size: %zu bytes\n"
33 " Persistent Ring Buffer: %s\n"
34 " Decoder Task Stack in PSRAM: %s",
41 micro_decoder::DecoderConfig config;
46 config.transfer_buffer_size = std::min(DEFAULT_TRANSFER_BUFFER_SIZE, this->
buffer_size_ / 2);
47 config.http_timeout_ms = HTTP_TIMEOUT_MS;
48 config.audio_write_timeout_ms = AUDIO_WRITE_TIMEOUT_MS;
49 config.reader_write_timeout_ms = READER_WRITE_TIMEOUT_MS;
50 config.reader_priority = READER_TASK_PRIORITY;
51 config.decoder_priority = DECODER_TASK_PRIORITY;
52 config.reader_stack_size = READER_TASK_STACK_SIZE;
53 config.decoder_stack_size = DECODER_TASK_STACK_SIZE;
56 this->
decoder_ = std::make_unique<micro_decoder::DecoderSource>(config);
58 ESP_LOGE(TAG,
"Failed to allocate decoder");
68 return uri.starts_with(HTTP_URI_PREFIX) || uri.starts_with(HTTPS_URI_PREFIX);
79 ESP_LOGE(TAG,
"Cannot play '%s': source is busy", uri.c_str());
84 if (!uri.starts_with(HTTP_URI_PREFIX) && !uri.starts_with(HTTPS_URI_PREFIX)) {
85 ESP_LOGE(TAG,
"Invalid URI: '%s'", uri.c_str());
90 this->
pause_.store(
false, std::memory_order_relaxed);
95 ESP_LOGE(TAG,
"Failed to start playback of '%s'", uri.c_str());
115 this->
pause_.store(
true, std::memory_order_relaxed);
122 this->
pause_.store(
false, std::memory_order_relaxed);
132 if (this->
pause_.load(std::memory_order_relaxed)) {
133 vTaskDelay(pdMS_TO_TICKS(PAUSE_POLL_DELAY_MS));
148 case micro_decoder::DecoderState::IDLE:
152 case micro_decoder::DecoderState::PLAYING:
155 case micro_decoder::DecoderState::FAILED:
void mark_failed()
Mark this component as failed.
void enable_loop()
Enable this component's loop.
bool status_has_error() const
void disable_loop()
Disable this component's loop.