10#if CONFIG_MBEDTLS_CERTIFICATE_BUNDLE
11#include "esp_crt_bundle.h"
17static const uint32_t READ_WRITE_TIMEOUT_MS = 20;
19static const uint32_t CONNECTION_TIMEOUT_MS = 5000;
20static const uint8_t MAX_FETCHING_HEADER_ATTEMPTS = 6;
22static const size_t HTTP_STREAM_BUFFER_SIZE = 2048;
24static const uint8_t MAX_REDIRECTIONS = 5;
26static const char *
const TAG =
"audio_reader";
70 return ESP_ERR_INVALID_STATE;
90 return ESP_ERR_INVALID_ARG;
93 esp_http_client_config_t client_config = {};
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;
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;
105#if CONFIG_MBEDTLS_CERTIFICATE_BUNDLE
106 if (uri.find(
"https:") != std::string::npos) {
107 client_config.crt_bundle_attach = esp_crt_bundle_attach;
111 this->
client_ = esp_http_client_init(&client_config);
113 if (this->
client_ ==
nullptr) {
117 esp_err_t err = esp_http_client_open(this->
client_, 0);
120 ESP_LOGE(TAG,
"Failed to open URL");
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)) {
129 if (header_length != -ESP_ERR_HTTP_EAGAIN) {
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_);
141 if (header_length < 0) {
142 ESP_LOGE(TAG,
"Failed to fetch headers");
147 int status_code = esp_http_client_get_status_code(this->
client_);
156 while ((esp_http_client_set_redirection(this->
client_) == ESP_OK) && (redirect_count < MAX_REDIRECTIONS)) {
157 err = esp_http_client_open(this->
client_, 0);
163 header_length = esp_http_client_fetch_headers(this->
client_);
164 if (header_length < 0) {
169 status_code = esp_http_client_get_status_code(this->
client_);
182 err = esp_http_client_get_url(this->
client_, url, 500);
193#ifdef USE_AUDIO_MP3_SUPPORT
198#ifdef USE_AUDIO_FLAC_SUPPORT
206 return ESP_ERR_NOT_SUPPORTED;
216 return ESP_ERR_NO_MEM;
223 if (this->
client_ !=
nullptr) {
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) {
239 if (strcasecmp(content_type,
"audio/wav") == 0) {
242#ifdef USE_AUDIO_FLAC_SUPPORT
243 if (strcasecmp(content_type,
"audio/flac") == 0 || strcasecmp(content_type,
"audio/x-flac") == 0) {
254 switch (evt->event_id) {
255 case HTTP_EVENT_ON_HEADER:
256 if (strcasecmp(evt->header_key,
"Content-Type") == 0) {
268 if (remaining_bytes > 0) {
270 pdMS_TO_TICKS(READ_WRITE_TIMEOUT_MS));
282 if (esp_http_client_is_complete_data_received(this->
client_)) {
289 this->output_transfer_buffer_->free());
291 if (received_len > 0) {
295 }
else if (received_len <= 0) {
297 if (received_len == -1) {
305 ESP_LOGE(TAG,
"Timed out");
310 delay(READ_WRITE_TIMEOUT_MS);
318 if (this->
client_ !=
nullptr) {
319 esp_http_client_close(this->
client_);
320 esp_http_client_cleanup(this->
client_);
uint32_t last_data_read_ms_
AudioReaderState http_read_()
void cleanup_connection_()
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.
AudioFileType audio_file_type_
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.
AudioFile * current_audio_file_
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.
@ HTTP_STATUS_INTERNAL_ERROR
@ HTTP_STATUS_MULTIPLE_CHOICES
@ HTTP_STATUS_MOVED_PERMANENTLY
@ HTTP_STATUS_BAD_REQUEST
@ HTTP_STATUS_TEMPORARY_REDIRECT
@ HTTP_STATUS_PERMANENT_REDIRECT
@ HTTP_STATUS_METHOD_NOT_ALLOWED
@ HTTP_STATUS_PARTIAL_CONTENT
@ HTTP_STATUS_NOT_MODIFIED
@ HTTP_STATUS_LENGTH_REQUIRED
@ HTTP_STATUS_UNAUTHORIZED
@ HTTP_STATUS_NOT_ACCEPTABLE
Providing packet encoding functions for exchanging data with a remote host.
std::string str_lower_case(const std::string &str)
Convert the string to lower case.
void IRAM_ATTR HOT delay(uint32_t ms)
uint32_t IRAM_ATTR HOT millis()
bool str_endswith(const std::string &str, const std::string &end)
Check whether a string ends with a value.