7#if defined(USE_WIFI) && defined(USE_WIFI_RUNTIME_ROAMING_SUPPRESSION)
11#include <freertos/FreeRTOS.h>
12#include <freertos/task.h>
21static constexpr size_t DEFAULT_TRANSFER_BUFFER_SIZE = 8 * 1024;
22static constexpr uint32_t HTTP_TIMEOUT_MS = 5000;
23static constexpr uint32_t AUDIO_WRITE_TIMEOUT_MS = 50;
24static constexpr uint32_t READER_WRITE_TIMEOUT_MS = 50;
25static constexpr uint8_t READER_TASK_PRIORITY = 2;
26static constexpr uint8_t DECODER_TASK_PRIORITY = 2;
29static constexpr size_t READER_TASK_STACK_SIZE = 6144;
30static constexpr size_t DECODER_TASK_STACK_SIZE = 5120;
31static constexpr uint32_t PAUSE_POLL_DELAY_MS = 20;
32static constexpr const char *
const HTTP_URI_PREFIX =
"http://";
33static constexpr const char *
const HTTPS_URI_PREFIX =
"https://";
37 "Audio HTTP Media Source:\n"
38 " Buffer Size: %zu bytes\n"
39 " Persistent Ring Buffer: %s\n"
40 " Decoder Task Stack in PSRAM: %s",
42#ifdef USE_AUDIO_HTTP_CA_CERTIFICATE
50 micro_decoder::DecoderConfig config;
55 config.transfer_buffer_size = std::min(DEFAULT_TRANSFER_BUFFER_SIZE, this->
buffer_size_ / 2);
56 config.http_timeout_ms = HTTP_TIMEOUT_MS;
57 config.audio_write_timeout_ms = AUDIO_WRITE_TIMEOUT_MS;
58 config.reader_write_timeout_ms = READER_WRITE_TIMEOUT_MS;
59 config.reader_priority = READER_TASK_PRIORITY;
60 config.decoder_priority = DECODER_TASK_PRIORITY;
61 config.reader_stack_size = READER_TASK_STACK_SIZE;
62 config.decoder_stack_size = DECODER_TASK_STACK_SIZE;
64#ifdef USE_AUDIO_HTTP_CA_CERTIFICATE
71 this->
decoder_ = std::make_unique<micro_decoder::DecoderSource>(config);
73 ESP_LOGE(TAG,
"Failed to allocate decoder");
83 return uri.starts_with(HTTP_URI_PREFIX) || uri.starts_with(HTTPS_URI_PREFIX);
94 ESP_LOGE(TAG,
"Cannot play '%s': source is busy", uri.c_str());
99 if (!uri.starts_with(HTTP_URI_PREFIX) && !uri.starts_with(HTTPS_URI_PREFIX)) {
100 ESP_LOGE(TAG,
"Invalid URI: '%s'", uri.c_str());
104 if (this->
decoder_->play_url(uri)) {
105 this->
pause_.store(
false, std::memory_order_relaxed);
110 ESP_LOGE(TAG,
"Failed to start playback of '%s'", uri.c_str());
130 this->
pause_.store(
true, std::memory_order_relaxed);
137 this->
pause_.store(
false, std::memory_order_relaxed);
147 if (this->
pause_.load(std::memory_order_relaxed)) {
148 vTaskDelay(pdMS_TO_TICKS(PAUSE_POLL_DELAY_MS));
163 case micro_decoder::DecoderState::IDLE:
169 case micro_decoder::DecoderState::PLAYING:
172 case micro_decoder::DecoderState::FAILED:
181#if defined(USE_WIFI) && defined(USE_WIFI_RUNTIME_ROAMING_SUPPRESSION)
185 const bool now_streaming = streaming(
state);
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.
void set_roaming_suppressed(bool suppressed)
Add or release one request to suppress post-connect roaming scans.
ESPHOME_LOG_TAG(TAG, "audio_http_media_source")
WiFiComponent * global_wifi_component