mirror of
https://github.com/esphome/esphome.git
synced 2025-04-13 14:20:29 +01:00
[audio] Determine http timeout based on duration since last successful read (#8341)
This commit is contained in:
parent
aed5020a83
commit
23e04e18f8
@ -15,6 +15,8 @@ namespace audio {
|
|||||||
|
|
||||||
static const uint32_t READ_WRITE_TIMEOUT_MS = 20;
|
static const uint32_t READ_WRITE_TIMEOUT_MS = 20;
|
||||||
|
|
||||||
|
static const uint32_t CONNECTION_TIMEOUT_MS = 5000;
|
||||||
|
|
||||||
// The number of times the http read times out with no data before throwing an error
|
// The number of times the http read times out with no data before throwing an error
|
||||||
static const uint32_t ERROR_COUNT_NO_DATA_READ_TIMEOUT = 100;
|
static const uint32_t ERROR_COUNT_NO_DATA_READ_TIMEOUT = 100;
|
||||||
|
|
||||||
@ -97,7 +99,7 @@ esp_err_t AudioReader::start(const std::string &uri, AudioFileType &file_type) {
|
|||||||
client_config.user_data = this;
|
client_config.user_data = this;
|
||||||
client_config.buffer_size = HTTP_STREAM_BUFFER_SIZE;
|
client_config.buffer_size = HTTP_STREAM_BUFFER_SIZE;
|
||||||
client_config.keep_alive_enable = true;
|
client_config.keep_alive_enable = true;
|
||||||
client_config.timeout_ms = 5000; // Shouldn't trigger watchdog resets if caller runs in a task
|
client_config.timeout_ms = CONNECTION_TIMEOUT_MS; // Shouldn't trigger watchdog resets if caller runs in a task
|
||||||
|
|
||||||
#if CONFIG_MBEDTLS_CERTIFICATE_BUNDLE
|
#if CONFIG_MBEDTLS_CERTIFICATE_BUNDLE
|
||||||
if (uri.find("https:") != std::string::npos) {
|
if (uri.find("https:") != std::string::npos) {
|
||||||
@ -189,7 +191,7 @@ esp_err_t AudioReader::start(const std::string &uri, AudioFileType &file_type) {
|
|||||||
file_type = this->audio_file_type_;
|
file_type = this->audio_file_type_;
|
||||||
}
|
}
|
||||||
|
|
||||||
this->no_data_read_count_ = 0;
|
this->last_data_read_ms_ = millis();
|
||||||
|
|
||||||
this->output_transfer_buffer_ = AudioSinkTransferBuffer::create(this->buffer_size_);
|
this->output_transfer_buffer_ = AudioSinkTransferBuffer::create(this->buffer_size_);
|
||||||
if (this->output_transfer_buffer_ == nullptr) {
|
if (this->output_transfer_buffer_ == nullptr) {
|
||||||
@ -271,8 +273,7 @@ AudioReaderState AudioReader::http_read_() {
|
|||||||
|
|
||||||
if (received_len > 0) {
|
if (received_len > 0) {
|
||||||
this->output_transfer_buffer_->increase_buffer_length(received_len);
|
this->output_transfer_buffer_->increase_buffer_length(received_len);
|
||||||
|
this->last_data_read_ms_ = millis();
|
||||||
this->no_data_read_count_ = 0;
|
|
||||||
} else if (received_len < 0) {
|
} else if (received_len < 0) {
|
||||||
// HTTP read error
|
// HTTP read error
|
||||||
this->cleanup_connection_();
|
this->cleanup_connection_();
|
||||||
@ -280,12 +281,11 @@ AudioReaderState AudioReader::http_read_() {
|
|||||||
} else {
|
} else {
|
||||||
if (bytes_to_read > 0) {
|
if (bytes_to_read > 0) {
|
||||||
// Read timed out
|
// Read timed out
|
||||||
++this->no_data_read_count_;
|
if ((millis() - this->last_data_read_ms_) > CONNECTION_TIMEOUT_MS) {
|
||||||
if (this->no_data_read_count_ >= ERROR_COUNT_NO_DATA_READ_TIMEOUT) {
|
|
||||||
// Timed out with no data read too many times, so the http read has failed
|
|
||||||
this->cleanup_connection_();
|
this->cleanup_connection_();
|
||||||
return AudioReaderState::FAILED;
|
return AudioReaderState::FAILED;
|
||||||
}
|
}
|
||||||
|
|
||||||
delay(READ_WRITE_TIMEOUT_MS);
|
delay(READ_WRITE_TIMEOUT_MS);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -71,7 +71,7 @@ class AudioReader {
|
|||||||
void cleanup_connection_();
|
void cleanup_connection_();
|
||||||
|
|
||||||
size_t buffer_size_;
|
size_t buffer_size_;
|
||||||
uint32_t no_data_read_count_;
|
uint32_t last_data_read_ms_;
|
||||||
|
|
||||||
esp_http_client_handle_t client_{nullptr};
|
esp_http_client_handle_t client_{nullptr};
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user