9#if CONFIG_MBEDTLS_CERTIFICATE_BUNDLE
10#include "esp_crt_bundle.h"
16static const uint32_t READ_WRITE_TIMEOUT_MS = 20;
18static const uint32_t CONNECTION_TIMEOUT_MS = 5000;
21static const uint32_t ERROR_COUNT_NO_DATA_READ_TIMEOUT = 100;
23static const size_t HTTP_STREAM_BUFFER_SIZE = 2048;
25static const uint8_t MAX_REDIRECTION = 5;
69 return ESP_ERR_INVALID_STATE;
89 return ESP_ERR_INVALID_ARG;
92 esp_http_client_config_t client_config = {};
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 = 10;
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;
104#if CONFIG_MBEDTLS_CERTIFICATE_BUNDLE
105 if (uri.find(
"https:") != std::string::npos) {
106 client_config.crt_bundle_attach = esp_crt_bundle_attach;
110 this->
client_ = esp_http_client_init(&client_config);
112 if (this->
client_ ==
nullptr) {
116 esp_err_t err = esp_http_client_open(this->
client_, 0);
123 int64_t header_length = esp_http_client_fetch_headers(this->
client_);
124 if (header_length < 0) {
129 int status_code = esp_http_client_get_status_code(this->
client_);
138 while ((esp_http_client_set_redirection(this->
client_) == ESP_OK) && (redirect_count < MAX_REDIRECTION)) {
139 err = esp_http_client_open(this->
client_, 0);
145 header_length = esp_http_client_fetch_headers(this->
client_);
146 if (header_length < 0) {
151 status_code = esp_http_client_get_status_code(this->
client_);
164 err = esp_http_client_get_url(this->
client_, url, 500);
175#ifdef USE_AUDIO_MP3_SUPPORT
180#ifdef USE_AUDIO_FLAC_SUPPORT
188 return ESP_ERR_NOT_SUPPORTED;
198 return ESP_ERR_NO_MEM;
205 if (this->
client_ !=
nullptr) {
215#ifdef USE_AUDIO_MP3_SUPPORT
216 if (strcasecmp(content_type,
"mp3") == 0 || strcasecmp(content_type,
"audio/mp3") == 0 ||
217 strcasecmp(content_type,
"audio/mpeg") == 0) {
221 if (strcasecmp(content_type,
"audio/wav") == 0) {
224#ifdef USE_AUDIO_FLAC_SUPPORT
225 if (strcasecmp(content_type,
"audio/flac") == 0 || strcasecmp(content_type,
"audio/x-flac") == 0) {
236 switch (evt->event_id) {
237 case HTTP_EVENT_ON_HEADER:
238 if (strcasecmp(evt->header_key,
"Content-Type") == 0) {
250 if (remaining_bytes > 0) {
252 pdMS_TO_TICKS(READ_WRITE_TIMEOUT_MS));
264 if (esp_http_client_is_complete_data_received(this->
client_)) {
274 if (received_len > 0) {
277 }
else if (received_len < 0) {
282 if (bytes_to_read > 0) {
289 delay(READ_WRITE_TIMEOUT_MS);
298 if (this->
client_ !=
nullptr) {
299 esp_http_client_close(this->
client_);
300 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.