33 *
44 * Created on: Oct 28.2018
55 *
6- * Version 3.0.13za
6+ * Version 3.0.13zb
77 * Updated on: Dec 26.2024
88 * Author: Wolle (schreibfaul1)
99 *
@@ -168,9 +168,8 @@ Audio::Audio(bool internalDAC /* = false */, uint8_t channelEnabled /* = I2S_SLO
168168
169169 m_chbuf = (char *) malloc (m_chbufSize);
170170 m_ibuff = (char *) malloc (m_ibuffSize);
171- m_lastHost = (char *) malloc (2048 );
172171 m_outBuff = (int16_t *)malloc (m_outbuffSize * sizeof (int16_t ));
173- if (!m_chbuf || !m_lastHost || ! m_outBuff || !m_ibuff) log_e (" oom" );
172+ if (!m_chbuf || !m_outBuff || !m_ibuff) log_e (" oom" );
174173
175174#ifdef AUDIO_LOG
176175 m_f_Log = true ;
@@ -269,12 +268,12 @@ Audio::~Audio() {
269268 i2s_driver_uninstall ((i2s_port_t )m_i2s_num); // #215 free I2S buffer
270269#endif
271270
272- if (m_playlistBuff) { free (m_playlistBuff); m_playlistBuff = NULL ;}
273- if (m_chbuf) { free (m_chbuf); m_chbuf = NULL ;}
274- if (m_lastHost) { free (m_lastHost); m_lastHost = NULL ;}
275- if (m_outBuff) { free (m_outBuff); m_outBuff = NULL ;}
276- if (m_ibuff) { free (m_ibuff); m_ibuff = NULL ;}
277- if (m_lastM3U8host) { free (m_lastM3U8host); m_lastM3U8host = NULL ;}
271+ x_ps_free (m_playlistBuff);
272+ x_ps_free (m_chbuf);
273+ x_ps_free (m_lastHost);
274+ x_ps_free (m_outBuff);
275+ x_ps_free (m_ibuff);
276+ x_ps_free (m_lastM3U8host);
278277
279278 stopAudioTask ();
280279 vSemaphoreDelete (mutex_playAudioData);
@@ -478,7 +477,8 @@ bool Audio::openai_speech(const String& api_key, const String& model, const Stri
478477 res = _client->connect (host, port, m_timeout_ms_ssl);
479478 if (res) {
480479 uint32_t dt = millis () - t;
481- strcpy (m_lastHost, host);
480+ x_ps_free (m_lastHost);
481+ m_lastHost = x_ps_strdup (host);
482482 AUDIO_INFO (" %s has been established in %lu ms, free Heap: %lu bytes" , " SSL" , (long unsigned int ) dt, (long unsigned int ) ESP.getFreeHeap ());
483483 m_f_running = true ;
484484 }
@@ -496,7 +496,7 @@ bool Audio::openai_speech(const String& api_key, const String& model, const Stri
496496 m_streamType = ST_WEBSTREAM;
497497 } else {
498498 AUDIO_INFO (" Request %s failed!" , host);
499- m_lastHost[ 0 ] = 0 ;
499+ x_ps_free ( m_lastHost) ;
500500 }
501501 xSemaphoreGiveRecursive (mutex_playAudioData);
502502 return res;
@@ -598,24 +598,16 @@ bool Audio::connecttohost(const char* host, const char* user, const char* pwd) {
598598
599599 if (pos_slash > 0 ) h_host[pos_slash] = ' /' ;
600600 if (pos_colon > 0 ) h_host[pos_colon] = ' :' ;
601- strcpy (m_lastHost, h_host);
602-
603- if (res) {
604- uint32_t dt = millis () - timestamp;
605- strcpy (m_lastHost, h_host);
606- AUDIO_INFO (" %s has been established in %lu ms, free Heap: %lu bytes" , m_f_ssl ? " SSL" : " Connection" , (long unsigned int )dt, (long unsigned int )ESP.getFreeHeap ());
607- m_f_running = true ;
608- }
609- else {
610- AUDIO_INFO (" %s could not be established, free Heap: %lu bytes" , m_f_ssl ? " SSL" : " Connection" , (long unsigned int )ESP.getFreeHeap ());
611- m_f_running = false ;
612- goto exit;
613- }
614601
615602 m_expectedCodec = CODEC_NONE;
616603 m_expectedPlsFmt = FORMAT_NONE;
617604
618605 if (res) {
606+ uint32_t dt = millis () - timestamp;
607+ x_ps_free (m_lastHost);
608+ m_lastHost = x_ps_strdup (host);
609+ AUDIO_INFO (" %s has been established in %lu ms, free Heap: %lu bytes" , m_f_ssl ? " SSL" : " Connection" , (long unsigned int )dt, (long unsigned int )ESP.getFreeHeap ());
610+ m_f_running = true ;
619611 _client->print (rqh);
620612 if (endsWith (h_host, " .mp3" )) m_expectedCodec = CODEC_MP3;
621613 if (endsWith (h_host, " .aac" )) m_expectedCodec = CODEC_AAC;
@@ -637,20 +629,20 @@ bool Audio::connecttohost(const char* host, const char* user, const char* pwd) {
637629 m_streamType = ST_WEBSTREAM;
638630 }
639631 else {
632+ AUDIO_INFO (" Request %s failed!" , host);
633+ m_f_running = false ;
640634 if (audio_showstation) audio_showstation (" " );
641635 if (audio_showstreamtitle) audio_showstreamtitle (" " );
642636 if (audio_icydescription) audio_icydescription (" " );
643637 if (audio_icyurl) audio_icyurl (" " );
644- AUDIO_INFO (" Request %s failed!" , host);
645- m_lastHost[0 ] = 0 ;
646638 }
647639
648640exit:
649641 xSemaphoreGiveRecursive (mutex_playAudioData);
650- free (h_host);
651- free (rqh);
652- free (authorization);
653- free (toEncode);
642+ x_ps_free (h_host);
643+ x_ps_free (rqh);
644+ x_ps_free (authorization);
645+ x_ps_free (toEncode);
654646 return res;
655647}
656648// ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
@@ -2428,9 +2420,6 @@ void Audio::playChunk() {
24282420}
24292421// ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
24302422void Audio::loop () {
2431- static uint8_t interval_short_cnt = 0 ;
2432- static uint32_t connect_host_time = millis ();
2433-
24342423 if (!m_f_running) return ;
24352424
24362425 if (m_playlistFormat != FORMAT_M3U8) { // normal process
@@ -2460,7 +2449,9 @@ void Audio::loop() {
24602449 }
24612450 else { // m3u8 datastream only
24622451 const char * host;
2463-
2452+ static uint8_t no_host_cnt = 0 ;
2453+ static uint32_t no_host_timer = millis ();
2454+ if (no_host_timer > millis ()) {return ;}
24642455 switch (m_dataMode) {
24652456 case HTTP_RESPONSE_HEADER:
24662457 static uint8_t count = 0 ;
@@ -2474,27 +2465,15 @@ void Audio::loop() {
24742465 case AUDIO_PLAYLISTINIT: readPlayListData (); break ;
24752466 case AUDIO_PLAYLISTDATA:
24762467 host = parsePlaylist_M3U8 ();
2468+ if (!host) no_host_cnt++; else {no_host_cnt = 0 ; no_host_timer = millis ();}
2469+ if (no_host_cnt == 2 ){no_host_timer = millis () + 2000 ;} // no new url? wait 2 seconds
24772470 if (host) { // host contains the next playlist URL
24782471 httpPrint (host);
24792472 m_dataMode = HTTP_RESPONSE_HEADER;
24802473 }
24812474 else { // host == NULL means connect to m3u8 URL
24822475 if (m_lastM3U8host) httpPrint (m_lastM3U8host);
24832476 else httpPrint (m_lastHost); // if url has no first redirection
2484-
2485- if (millis () - connect_host_time < 1000 ) {
2486- if (++interval_short_cnt >= 5 ) { // connect to host too often (maybe cause by temporary bad network quality)
2487- interval_short_cnt = 0 ;
2488-
2489- AUDIO_INFO (" connect host interval too short, try to reconnect..." );
2490- connecttohost (m_lastHost);
2491- }
2492- }
2493- else {
2494- interval_short_cnt = 0 ;
2495- }
2496- connect_host_time = millis ();
2497-
24982477 m_dataMode = HTTP_RESPONSE_HEADER; // we have a new playlist now
24992478 }
25002479 break ;
@@ -2754,6 +2733,7 @@ const char* Audio::parsePlaylist_M3U8() {
27542733 // #EXTINF:10,title="text=\"Spot Block End\" amgTrackId=\"9876543\"",artist=" ",url="length=\"00:00:00\""
27552734 // http://n3fa-e2.revma.ihrhls.com/zc7729/63_sdtszizjcjbz02/main/163374039.aac
27562735
2736+ if (!m_lastHost) {log_e (" m_lastHost is NULL" ); return NULL ;} // guard
27572737 static uint64_t xMedSeq = 0 ;
27582738 static boolean f_mediaSeq_found = false ;
27592739 boolean f_EXTINF_found = false ;
@@ -2965,6 +2945,7 @@ const char* Audio::m3u8redirection(uint8_t* codec) {
29652945 // #EXT-X-STREAM-INF:BANDWIDTH=37500,AVERAGE-BANDWIDTH=37000,CODECS="mp4a.40.29"
29662946 // 32/playlist.m3u8?hlssid=7562d0e101b84aeea0fa35f8b963a174
29672947
2948+ if (!m_lastHost) {log_e (" m_lastHost is NULL" ); return NULL ;} // guard
29682949 const char codecString[9 ][11 ]={
29692950 " mp4a.40.34" , // mp3 stream
29702951 " mp4a.40.01" , // AAC Main
@@ -3323,6 +3304,7 @@ void Audio::processWebStream() {
33233304}
33243305// ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
33253306void Audio::processWebFile () {
3307+ if (!m_lastHost) {log_e (" m_lastHost is NULL" ); return ;} // guard
33263308 const uint32_t maxFrameSize = InBuff.getMaxBlockSize (); // every mp3/aac frame is not bigger
33273309 static uint32_t chunkSize; // chunkcount read from stream
33283310 static size_t audioDataCount; // counts the decoded audiodata only
@@ -3691,6 +3673,7 @@ void Audio::playAudioData() {
36913673bool Audio::parseHttpResponseHeader () { // this is the response to a GET / request
36923674
36933675 if (m_dataMode != HTTP_RESPONSE_HEADER) return false ;
3676+ if (!m_lastHost) {log_e (" m_lastHost is NULL" ); return false ;}
36943677
36953678 uint32_t ctime = millis ();
36963679 uint32_t timeout = 4500 ; // ms
@@ -4790,11 +4773,10 @@ bool Audio::setPinout(uint8_t BCLK, uint8_t LRC, uint8_t DOUT, int8_t MCLK) {
47904773 x_ps_free (m_ibuff);
47914774 x_ps_free (m_outBuff);
47924775 x_ps_free (m_lastHost);
4793- m_lastHost = (char *) x_ps_malloc (2048 );
47944776 m_outBuff = (int16_t *)x_ps_malloc (m_outbuffSize * sizeof (int16_t ));
47954777 m_chbuf = (char *) x_ps_malloc (m_chbufSize);
47964778 m_ibuff = (char *) x_ps_malloc (m_ibuffSize);
4797- if (!m_chbuf || !m_lastHost || ! m_outBuff || !m_ibuff) log_e (" oom" );
4779+ if (!m_chbuf || !m_outBuff || !m_ibuff) log_e (" oom" );
47984780 }
47994781 esp_err_t result = ESP_OK;
48004782
@@ -5860,6 +5842,7 @@ bool Audio::readID3V1Tag() {
58605842}
58615843// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
58625844boolean Audio::streamDetection (uint32_t bytesAvail) {
5845+ if (!m_lastHost) {log_e (" m_lastHost is NULL" ); return false ;}
58635846 static uint32_t tmr_slow = millis ();
58645847 static uint32_t tmr_lost = millis ();
58655848 static uint8_t cnt_slow = 0 ;
0 commit comments