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 " Decoder Task Stack in PSRAM: %s",
40 micro_decoder::DecoderConfig config;
44 config.transfer_buffer_size = std::min(DEFAULT_TRANSFER_BUFFER_SIZE, this->
buffer_size_ / 2);
45 config.http_timeout_ms = HTTP_TIMEOUT_MS;
46 config.audio_write_timeout_ms = AUDIO_WRITE_TIMEOUT_MS;
47 config.reader_write_timeout_ms = READER_WRITE_TIMEOUT_MS;
48 config.reader_priority = READER_TASK_PRIORITY;
49 config.decoder_priority = DECODER_TASK_PRIORITY;
50 config.reader_stack_size = READER_TASK_STACK_SIZE;
51 config.decoder_stack_size = DECODER_TASK_STACK_SIZE;
54 this->
decoder_ = std::make_unique<micro_decoder::DecoderSource>(config);
56 ESP_LOGE(TAG,
"Failed to allocate decoder");
66 return uri.starts_with(HTTP_URI_PREFIX) || uri.starts_with(HTTPS_URI_PREFIX);
77 ESP_LOGE(TAG,
"Cannot play '%s': source is busy", uri.c_str());
82 if (!uri.starts_with(HTTP_URI_PREFIX) && !uri.starts_with(HTTPS_URI_PREFIX)) {
83 ESP_LOGE(TAG,
"Invalid URI: '%s'", uri.c_str());
88 this->
pause_.store(
false, std::memory_order_relaxed);
93 ESP_LOGE(TAG,
"Failed to start playback of '%s'", uri.c_str());
113 this->
pause_.store(
true, std::memory_order_relaxed);
120 this->
pause_.store(
false, std::memory_order_relaxed);
130 if (this->
pause_.load(std::memory_order_relaxed)) {
131 vTaskDelay(pdMS_TO_TICKS(PAUSE_POLL_DELAY_MS));
146 case micro_decoder::DecoderState::IDLE:
150 case micro_decoder::DecoderState::PLAYING:
153 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.