From e7f69ebc5f1c46c788ecb425639b38669dc96100 Mon Sep 17 00:00:00 2001 From: Christian Baars Date: Mon, 20 May 2024 09:12:14 +0200 Subject: [PATCH] bugfix: webradio crash with invalid url (#21446) --- .../src/AudioFileSourceICYStream.cpp | 10 +++----- .../xdrv_42_0_i2s_audio_idf51.ino | 1 - .../xdrv_42_7_i2s_webradio_idf51.ino | 25 ++++++++++--------- 3 files changed, 17 insertions(+), 19 deletions(-) diff --git a/lib/lib_audio/ESP8266Audio/src/AudioFileSourceICYStream.cpp b/lib/lib_audio/ESP8266Audio/src/AudioFileSourceICYStream.cpp index 25e641705d4b..b05a051cc130 100644 --- a/lib/lib_audio/ESP8266Audio/src/AudioFileSourceICYStream.cpp +++ b/lib/lib_audio/ESP8266Audio/src/AudioFileSourceICYStream.cpp @@ -45,17 +45,15 @@ bool AudioFileSourceICYStream::open(const char *url) { static const char *hdr[] = { "icy-metaint", "icy-name", "icy-genre", "icy-br" }; pos = 0; - http.begin(client, url); + if (!http.begin(client, url)) { + cb.st(STATUS_HTTPFAIL, PSTR("Can't connect to url")); + return false; + } http.addHeader("Icy-MetaData", "1"); http.collectHeaders( hdr, 4 ); http.setReuse(true); http.setFollowRedirects(HTTPC_FORCE_FOLLOW_REDIRECTS); int code = http.GET(); - if (code != HTTP_CODE_OK) { - http.end(); - cb.st(STATUS_HTTPFAIL, PSTR("Can't open HTTP request")); - return false; - } if (http.hasHeader(hdr[0])) { String ret = http.header(hdr[0]); icyMetaInt = ret.toInt(); diff --git a/tasmota/tasmota_xdrv_driver/xdrv_42_0_i2s_audio_idf51.ino b/tasmota/tasmota_xdrv_driver/xdrv_42_0_i2s_audio_idf51.ino index 5104e2ab9bba..f4a840044629 100644 --- a/tasmota/tasmota_xdrv_driver/xdrv_42_0_i2s_audio_idf51.ino +++ b/tasmota/tasmota_xdrv_driver/xdrv_42_0_i2s_audio_idf51.ino @@ -847,7 +847,6 @@ void I2sMp3WrTask(void *arg){ if (audio_i2s_mp3.decoder && audio_i2s_mp3.decoder->isRunning()) { if (!audio_i2s_mp3.decoder->loop()) { audio_i2s_mp3.task_running = false; - //retryms = millis() + 2000; } vTaskDelay(pdMS_TO_TICKS(1)); } diff --git a/tasmota/tasmota_xdrv_driver/xdrv_42_7_i2s_webradio_idf51.ino b/tasmota/tasmota_xdrv_driver/xdrv_42_7_i2s_webradio_idf51.ino index 370bebb9db86..c023fb1975a0 100644 --- a/tasmota/tasmota_xdrv_driver/xdrv_42_7_i2s_webradio_idf51.ino +++ b/tasmota/tasmota_xdrv_driver/xdrv_42_7_i2s_webradio_idf51.ino @@ -27,7 +27,6 @@ struct AUDIO_I2S_WEBRADIO_t { char wr_title[64]; void *preallocateBuffer = NULL; void *preallocateCodec = NULL; - uint32_t retryms = 0; } Audio_webradio; void I2sMDCallback(void *cbData, const char *type, bool isUnicode, const char *str) { @@ -43,6 +42,10 @@ void I2sMDCallback(void *cbData, const char *type, bool isUnicode, const char *s } } +void I2SWrStatusCB(void *cbData, int code, const char *str){ + AddLog(LOG_LEVEL_INFO, "I2S: status: %s",str); +} + void Webradio(const char *url) { // allocate buffers if not already done if (Audio_webradio.preallocateBuffer == NULL) { @@ -65,21 +68,22 @@ void Webradio(const char *url) { return; } - // if (audio_i2s_mp3.decoder || audio_i2s_mp3.mp3) return; - if (!audio_i2s.out) return; - I2SAudioPower(true); - Audio_webradio.ifile = new AudioFileSourceICYStream(url); + Audio_webradio.ifile = new AudioFileSourceICYStream(); Audio_webradio.ifile->RegisterMetadataCB(I2sMDCallback, NULL); + Audio_webradio.ifile->RegisterStatusCB(I2SWrStatusCB, NULL); + if(!Audio_webradio.ifile->open(url)){ + I2sWebRadioStopPlaying(); + return; + } + + I2SAudioPower(true); Audio_webradio.buff = new AudioFileSourceBuffer(Audio_webradio.ifile, Audio_webradio.preallocateBuffer, preallocateBufferSize); Audio_webradio.buff->RegisterStatusCB(I2sStatusCallback, NULL); audio_i2s_mp3.decoder = new AudioGeneratorMP3(Audio_webradio.preallocateCodec, preallocateCodecSize); audio_i2s_mp3.decoder->RegisterStatusCB(I2sStatusCallback, NULL); audio_i2s_mp3.decoder->begin(Audio_webradio.buff, audio_i2s.out); if (!audio_i2s_mp3.decoder->isRunning()) { - // Serial.printf_P(PSTR("Can't connect to URL")); I2sStopPlaying(); - // strcpy_P(status, PSTR("Unable to connect to URL")); - Audio_webradio.retryms = millis() + 2000; } AddLog(LOG_LEVEL_DEBUG,PSTR("I2S: will launch webradio task")); @@ -102,11 +106,8 @@ void I2sWrShow(bool json) { #endif // USE_WEBSERVER void CmndI2SWebRadio(void) { - if (!audio_i2s.out) return; + if (I2SPrepareTx() != I2S_OK) return; - if (audio_i2s_mp3.decoder) { - I2sStopPlaying(); - } if (XdrvMailbox.data_len > 0) { Webradio(XdrvMailbox.data); ResponseCmndChar(XdrvMailbox.data);