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