7#include <freertos/FreeRTOS.h>
8#include <freertos/task.h>
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;
23#ifdef CONFIG_MBEDTLS_SSL_PROTO_TLS1_3
25static constexpr size_t READER_TASK_STACK_SIZE = 6144;
27static constexpr size_t READER_TASK_STACK_SIZE = 4096;
29static constexpr size_t DECODER_TASK_STACK_SIZE = 5120;
30static constexpr uint32_t PAUSE_POLL_DELAY_MS = 20;
31static constexpr const char *
const HTTP_URI_PREFIX =
"http://";
32static constexpr const char *
const HTTPS_URI_PREFIX =
"https://";
36 "Audio HTTP Media Source:\n"
37 " Buffer Size: %zu bytes\n"
38 " Persistent Ring Buffer: %s\n"
39 " Decoder Task Stack in PSRAM: %s",
41#ifdef USE_AUDIO_HTTP_CA_CERTIFICATE
49 micro_decoder::DecoderConfig config;
54 config.transfer_buffer_size = std::min(DEFAULT_TRANSFER_BUFFER_SIZE, this->
buffer_size_ / 2);
55 config.http_timeout_ms = HTTP_TIMEOUT_MS;
56 config.audio_write_timeout_ms = AUDIO_WRITE_TIMEOUT_MS;
57 config.reader_write_timeout_ms = READER_WRITE_TIMEOUT_MS;
58 config.reader_priority = READER_TASK_PRIORITY;
59 config.decoder_priority = DECODER_TASK_PRIORITY;
60 config.reader_stack_size = READER_TASK_STACK_SIZE;
61 config.decoder_stack_size = DECODER_TASK_STACK_SIZE;
63#ifdef USE_AUDIO_HTTP_CA_CERTIFICATE
70 this->
decoder_ = std::make_unique<micro_decoder::DecoderSource>(config);
72 ESP_LOGE(TAG,
"Failed to allocate decoder");
82 return uri.starts_with(HTTP_URI_PREFIX) || uri.starts_with(HTTPS_URI_PREFIX);
93 ESP_LOGE(TAG,
"Cannot play '%s': source is busy", uri.c_str());
98 if (!uri.starts_with(HTTP_URI_PREFIX) && !uri.starts_with(HTTPS_URI_PREFIX)) {
99 ESP_LOGE(TAG,
"Invalid URI: '%s'", uri.c_str());
103 if (this->
decoder_->play_url(uri)) {
104 this->
pause_.store(
false, std::memory_order_relaxed);
109 ESP_LOGE(TAG,
"Failed to start playback of '%s'", uri.c_str());
129 this->
pause_.store(
true, std::memory_order_relaxed);
136 this->
pause_.store(
false, std::memory_order_relaxed);
146 if (this->
pause_.load(std::memory_order_relaxed)) {
147 vTaskDelay(pdMS_TO_TICKS(PAUSE_POLL_DELAY_MS));
162 case micro_decoder::DecoderState::IDLE:
166 case micro_decoder::DecoderState::PLAYING:
169 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.
ESPHOME_LOG_TAG(TAG, "audio_http_media_source")