ESPHome 2026.6.0-dev
Loading...
Searching...
No Matches
audio_reader.cpp
Go to the documentation of this file.
1#include "audio_reader.h"
2
3#ifdef USE_ESP32
4
6#include "esphome/core/hal.h"
8#include "esphome/core/log.h"
9
10#if CONFIG_MBEDTLS_CERTIFICATE_BUNDLE
11#include "esp_crt_bundle.h"
12#endif
13
14namespace esphome::audio {
15
16static const uint32_t READ_WRITE_TIMEOUT_MS = 20;
17
18static const uint32_t CONNECTION_TIMEOUT_MS = 5000;
19static const uint8_t MAX_FETCHING_HEADER_ATTEMPTS = 6;
20
21static const size_t HTTP_STREAM_BUFFER_SIZE = 2048;
22
23static const uint8_t MAX_REDIRECTIONS = 5;
24
25static const char *const TAG = "audio_reader";
26
27// Some common HTTP status codes - borrowed from http_request component accessed 20241224
54
56
57esp_err_t AudioReader::add_sink(const std::weak_ptr<ring_buffer::RingBuffer> &output_ring_buffer) {
58 if (current_audio_file_ != nullptr) {
59 // A transfer buffer isn't ncessary for a local file
60 this->file_ring_buffer_ = output_ring_buffer.lock();
61 return ESP_OK;
62 }
63
64 if (this->output_transfer_buffer_ != nullptr) {
65 this->output_transfer_buffer_->set_sink(output_ring_buffer);
66 return ESP_OK;
67 }
68
69 return ESP_ERR_INVALID_STATE;
70}
71
72esp_err_t AudioReader::start(AudioFile *audio_file, AudioFileType &file_type) {
73 file_type = AudioFileType::NONE;
74
75 this->current_audio_file_ = audio_file;
76
77 this->file_current_ = audio_file->data;
78 file_type = audio_file->file_type;
79
80 return ESP_OK;
81}
82
83esp_err_t AudioReader::start(const std::string &uri, AudioFileType &file_type) {
84 file_type = AudioFileType::NONE;
85
86 this->cleanup_connection_();
87
88 if (uri.empty()) {
89 return ESP_ERR_INVALID_ARG;
90 }
91
92 esp_http_client_config_t client_config = {};
93
94 client_config.url = uri.c_str();
95 client_config.cert_pem = nullptr;
96 client_config.disable_auto_redirect = false;
97 client_config.max_redirection_count = MAX_REDIRECTIONS;
98 client_config.event_handler = http_event_handler;
99 client_config.user_data = this;
100 client_config.buffer_size = HTTP_STREAM_BUFFER_SIZE;
101 client_config.keep_alive_enable = true;
102 client_config.timeout_ms = CONNECTION_TIMEOUT_MS; // Shouldn't trigger watchdog resets if caller runs in a task
103
104#if CONFIG_MBEDTLS_CERTIFICATE_BUNDLE
105 if (uri.find("https:") != std::string::npos) {
106 client_config.crt_bundle_attach = esp_crt_bundle_attach;
107 }
108#endif
109
110 this->client_ = esp_http_client_init(&client_config);
111
112 if (this->client_ == nullptr) {
113 return ESP_FAIL;
114 }
115
116 esp_err_t err = esp_http_client_open(this->client_, 0);
117
118 if (err != ESP_OK) {
119 ESP_LOGE(TAG, "Failed to open URL");
120 this->cleanup_connection_();
121 return err;
122 }
123
124 int64_t header_length = esp_http_client_fetch_headers(this->client_);
125 uint8_t reattempt_count = 0;
126 while ((header_length < 0) && (reattempt_count < MAX_FETCHING_HEADER_ATTEMPTS)) {
127 this->cleanup_connection_();
128 if (header_length != -ESP_ERR_HTTP_EAGAIN) {
129 // Serious error, no recovery
130 return ESP_FAIL;
131 } else {
132 // Reconnect from a fresh state to avoid a bug where it never reads the headers even if made available
133 this->client_ = esp_http_client_init(&client_config);
134 esp_http_client_open(this->client_, 0);
135 header_length = esp_http_client_fetch_headers(this->client_);
136 ++reattempt_count;
137 }
138 }
139
140 if (header_length < 0) {
141 ESP_LOGE(TAG, "Failed to fetch headers");
142 this->cleanup_connection_();
143 return ESP_FAIL;
144 }
145
146 int status_code = esp_http_client_get_status_code(this->client_);
147
148 if ((status_code < HTTP_STATUS_OK) || (status_code > HTTP_STATUS_PERMANENT_REDIRECT)) {
149 this->cleanup_connection_();
150 return ESP_FAIL;
151 }
152
153 ssize_t redirect_count = 0;
154
155 while ((esp_http_client_set_redirection(this->client_) == ESP_OK) && (redirect_count < MAX_REDIRECTIONS)) {
156 err = esp_http_client_open(this->client_, 0);
157 if (err != ESP_OK) {
158 this->cleanup_connection_();
159 return ESP_FAIL;
160 }
161
162 header_length = esp_http_client_fetch_headers(this->client_);
163 if (header_length < 0) {
164 this->cleanup_connection_();
165 return ESP_FAIL;
166 }
167
168 status_code = esp_http_client_get_status_code(this->client_);
169
170 if ((status_code < HTTP_STATUS_OK) || (status_code > HTTP_STATUS_PERMANENT_REDIRECT)) {
171 this->cleanup_connection_();
172 return ESP_FAIL;
173 }
174
175 ++redirect_count;
176 }
177
179 // Failed to determine the file type from the header, fallback to using the url
180 char url[500];
181 err = esp_http_client_get_url(this->client_, url, 500);
182 if (err != ESP_OK) {
183 this->cleanup_connection_();
184 return err;
185 }
186
187 file_type = detect_audio_file_type(nullptr, url);
188 if (file_type == AudioFileType::NONE) {
189 this->cleanup_connection_();
190 return ESP_ERR_NOT_SUPPORTED;
191 }
192 } else {
193 file_type = this->audio_file_type_;
194 }
195
196 this->last_data_read_ms_ = millis();
197
199 if (this->output_transfer_buffer_ == nullptr) {
200 return ESP_ERR_NO_MEM;
201 }
202
203 return ESP_OK;
204}
205
207 if (this->client_ != nullptr) {
208 return this->http_read_();
209 } else if (this->current_audio_file_ != nullptr) {
210 return this->file_read_();
211 }
212
214}
215
216esp_err_t AudioReader::http_event_handler(esp_http_client_event_t *evt) {
217 // Based on https://github.com/maroc81/WeatherLily/tree/main/main/net accessed 20241224
218 AudioReader *this_reader = (AudioReader *) evt->user_data;
219
220 switch (evt->event_id) {
221 case HTTP_EVENT_ON_HEADER:
222 if (strcasecmp(evt->header_key, "Content-Type") == 0) {
223 this_reader->audio_file_type_ = detect_audio_file_type(evt->header_value, nullptr);
224 }
225 break;
226 default:
227 break;
228 }
229 return ESP_OK;
230}
231
233 size_t remaining_bytes = this->current_audio_file_->length - (this->file_current_ - this->current_audio_file_->data);
234 if (remaining_bytes > 0) {
235 size_t bytes_written = this->file_ring_buffer_->write_without_replacement(this->file_current_, remaining_bytes,
236 pdMS_TO_TICKS(READ_WRITE_TIMEOUT_MS));
237 this->file_current_ += bytes_written;
238
240 }
241
243}
244
246 this->output_transfer_buffer_->transfer_data_to_sink(pdMS_TO_TICKS(READ_WRITE_TIMEOUT_MS), false);
247
248 if (esp_http_client_is_complete_data_received(this->client_)) {
249 if (this->output_transfer_buffer_->available() == 0) {
250 this->cleanup_connection_();
252 }
253 } else if (this->output_transfer_buffer_->free() > 0) {
254 int received_len = esp_http_client_read(this->client_, (char *) this->output_transfer_buffer_->get_buffer_end(),
255 this->output_transfer_buffer_->free());
256
257 if (received_len > 0) {
258 this->output_transfer_buffer_->increase_buffer_length(received_len);
259 this->last_data_read_ms_ = millis();
261 } else if (received_len <= 0) {
262 // HTTP read error
263 if (received_len == -1) {
264 // A true connection error occured, no chance at recovery
265 this->cleanup_connection_();
267 }
268
269 // Read timed out, manually verify if it has been too long since the last successful read
270 if ((millis() - this->last_data_read_ms_) > MAX_FETCHING_HEADER_ATTEMPTS * CONNECTION_TIMEOUT_MS) {
271 ESP_LOGE(TAG, "Timed out");
272 this->cleanup_connection_();
274 }
275
276 delay(READ_WRITE_TIMEOUT_MS);
277 }
278 }
279
281}
282
284 if (this->client_ != nullptr) {
285 esp_http_client_close(this->client_);
286 esp_http_client_cleanup(this->client_);
287 this->client_ = nullptr;
288 }
289}
290
291} // namespace esphome::audio
292
293#endif
AudioReaderState http_read_()
AudioReaderState file_read_()
std::unique_ptr< AudioSinkTransferBuffer > output_transfer_buffer_
const uint8_t * file_current_
esp_err_t add_sink(const std::weak_ptr< ring_buffer::RingBuffer > &output_ring_buffer)
Adds a sink ring buffer for audio data.
AudioReaderState read()
Reads new file data from the source and sends to the ring buffer sink.
static esp_err_t http_event_handler(esp_http_client_event_t *evt)
Monitors the http client events to attempt determining the file type from the Content-Type header.
std::shared_ptr< ring_buffer::RingBuffer > file_ring_buffer_
esp_http_client_handle_t client_
esp_err_t start(const std::string &uri, AudioFileType &file_type)
Starts reading an audio file from an http source.
static std::unique_ptr< AudioSinkTransferBuffer > create(size_t buffer_size)
Creates a new sink transfer buffer.
__int64 ssize_t
Definition httplib.h:178
AudioFileType detect_audio_file_type(const char *content_type, const char *url)
Detect audio file type from a Content-Type header value and/or URL extension.
Definition audio.cpp:66
void HOT delay(uint32_t ms)
Definition hal.cpp:85
uint32_t IRAM_ATTR HOT millis()
Definition hal.cpp:28
static void uint32_t
const uint8_t * data
Definition audio.h:125
AudioFileType file_type
Definition audio.h:127