ESPHome 2025.6.3
Loading...
Searching...
No Matches
audio_reader.cpp
Go to the documentation of this file.
1#include "audio_reader.h"
2
3#ifdef USE_ESP_IDF
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 std::string url_string = str_lower_case(url);
189
190 if (str_endswith(url_string, ".wav")) {
191 file_type = AudioFileType::WAV;
192 }
193#ifdef USE_AUDIO_MP3_SUPPORT
194 else if (str_endswith(url_string, ".mp3")) {
195 file_type = AudioFileType::MP3;
196 }
197#endif
198#ifdef USE_AUDIO_FLAC_SUPPORT
199 else if (str_endswith(url_string, ".flac")) {
200 file_type = AudioFileType::FLAC;
201 }
202#endif
203 else {
204 file_type = AudioFileType::NONE;
205 this->cleanup_connection_();
206 return ESP_ERR_NOT_SUPPORTED;
207 }
208 } else {
209 file_type = this->audio_file_type_;
210 }
211
212 this->last_data_read_ms_ = millis();
213
215 if (this->output_transfer_buffer_ == nullptr) {
216 return ESP_ERR_NO_MEM;
217 }
218
219 return ESP_OK;
220}
221
223 if (this->client_ != nullptr) {
224 return this->http_read_();
225 } else if (this->current_audio_file_ != nullptr) {
226 return this->file_read_();
227 }
228
230}
231
233#ifdef USE_AUDIO_MP3_SUPPORT
234 if (strcasecmp(content_type, "mp3") == 0 || strcasecmp(content_type, "audio/mp3") == 0 ||
235 strcasecmp(content_type, "audio/mpeg") == 0) {
236 return AudioFileType::MP3;
237 }
238#endif
239 if (strcasecmp(content_type, "audio/wav") == 0) {
240 return AudioFileType::WAV;
241 }
242#ifdef USE_AUDIO_FLAC_SUPPORT
243 if (strcasecmp(content_type, "audio/flac") == 0 || strcasecmp(content_type, "audio/x-flac") == 0) {
244 return AudioFileType::FLAC;
245 }
246#endif
247 return AudioFileType::NONE;
248}
249
250esp_err_t AudioReader::http_event_handler(esp_http_client_event_t *evt) {
251 // Based on https://github.com/maroc81/WeatherLily/tree/main/main/net accessed 20241224
252 AudioReader *this_reader = (AudioReader *) evt->user_data;
253
254 switch (evt->event_id) {
255 case HTTP_EVENT_ON_HEADER:
256 if (strcasecmp(evt->header_key, "Content-Type") == 0) {
257 this_reader->audio_file_type_ = get_audio_type(evt->header_value);
258 }
259 break;
260 default:
261 break;
262 }
263 return ESP_OK;
264}
265
267 size_t remaining_bytes = this->current_audio_file_->length - (this->file_current_ - this->current_audio_file_->data);
268 if (remaining_bytes > 0) {
269 size_t bytes_written = this->file_ring_buffer_->write_without_replacement(this->file_current_, remaining_bytes,
270 pdMS_TO_TICKS(READ_WRITE_TIMEOUT_MS));
271 this->file_current_ += bytes_written;
272
274 }
275
277}
278
280 this->output_transfer_buffer_->transfer_data_to_sink(pdMS_TO_TICKS(READ_WRITE_TIMEOUT_MS), false);
281
282 if (esp_http_client_is_complete_data_received(this->client_)) {
283 if (this->output_transfer_buffer_->available() == 0) {
284 this->cleanup_connection_();
286 }
287 } else if (this->output_transfer_buffer_->free() > 0) {
288 int received_len = esp_http_client_read(this->client_, (char *) this->output_transfer_buffer_->get_buffer_end(),
289 this->output_transfer_buffer_->free());
290
291 if (received_len > 0) {
292 this->output_transfer_buffer_->increase_buffer_length(received_len);
293 this->last_data_read_ms_ = millis();
295 } else if (received_len <= 0) {
296 // HTTP read error
297 if (received_len == -1) {
298 // A true connection error occured, no chance at recovery
299 this->cleanup_connection_();
301 }
302
303 // Read timed out, manually verify if it has been too long since the last successful read
304 if ((millis() - this->last_data_read_ms_) > MAX_FETCHING_HEADER_ATTEMPTS * CONNECTION_TIMEOUT_MS) {
305 ESP_LOGE(TAG, "Timed out");
306 this->cleanup_connection_();
308 }
309
310 delay(READ_WRITE_TIMEOUT_MS);
311 }
312 }
313
315}
316
318 if (this->client_ != nullptr) {
319 esp_http_client_close(this->client_);
320 esp_http_client_cleanup(this->client_);
321 this->client_ = nullptr;
322 }
323}
324
325} // namespace audio
326} // namespace esphome
327
328#endif
AudioReaderState http_read_()
AudioReaderState file_read_()
std::unique_ptr< AudioSinkTransferBuffer > output_transfer_buffer_
const uint8_t * file_current_
static AudioFileType get_audio_type(const char *content_type)
Determines the audio file type from the http header's Content-Type key.
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:175
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
std::string str_lower_case(const std::string &str)
Convert the string to lower case.
Definition helpers.cpp:290
void IRAM_ATTR HOT delay(uint32_t ms)
Definition core.cpp:29
uint32_t IRAM_ATTR HOT millis()
Definition core.cpp:28
bool str_endswith(const std::string &str, const std::string &end)
Check whether a string ends with a value.
Definition helpers.cpp:267
const uint8_t * data
Definition audio.h:120
AudioFileType file_type
Definition audio.h:122