Skip to content

Commit c6daab2

Browse files
committed
more unique pointer
1 parent 560baed commit c6daab2

File tree

1 file changed

+60
-68
lines changed

1 file changed

+60
-68
lines changed

src/Audio.cpp

Lines changed: 60 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -483,9 +483,9 @@ bool Audio::connecttohost(const char* host, const char* user, const char* pwd) {
483483
uint32_t timestamp = 0; // timeout surveillance
484484
uint16_t hostwoext_begin = 0;
485485

486-
ps_ptr<char> h_host;
487-
char* rqh = NULL; // request header
488-
char* toEncode = NULL; // temporary memory for base64 encoding
486+
ps_ptr<char> h_host = nullptr;
487+
ps_ptr<char> rqh = nullptr; // request header
488+
char* toEncode = nullptr; // temporary memory for base64 encoding
489489

490490

491491
// https://edge.live.mp3.mdn.newmedia.nacamar.net:8000/ps-charivariwb/livestream.mp3;&user=ps-charivariwb;&pwd=ps-charivariwb-------
@@ -497,14 +497,14 @@ bool Audio::connecttohost(const char* host, const char* user, const char* pwd) {
497497

498498
// optional basic authorization
499499
if(user && pwd) authLen = strlen(user) + strlen(pwd);
500-
char authorization[base64_encode_expected_len(authLen + 1) + 1];
500+
auto authorization = audio_malloc<char>(base64_encode_expected_len(authLen + 1) + 1);
501501
authorization[0] = '\0';
502502
if(authLen > 0) {
503-
char toEncode[authLen + 4];
504-
strcpy(toEncode, user);
505-
strcat(toEncode, ":");
506-
strcat(toEncode, pwd);
507-
b64encode((const char*)toEncode, strlen(toEncode), authorization);
503+
auto toEncode = audio_malloc<char>(authLen + 4);
504+
strcpy(toEncode.get(), user);
505+
strcat(toEncode.get(), ":");
506+
strcat(toEncode.get(), pwd);
507+
b64encode((const char*)toEncode.get(), strlen(toEncode.get()), authorization.get());
508508
}
509509

510510
if (host == NULL) { log_info("Hostaddress is empty"); stopSong(); goto exit;}
@@ -532,24 +532,24 @@ bool Audio::connecttohost(const char* host, const char* user, const char* pwd) {
532532
h_host[pos_colon] = '\0';
533533
}
534534
setDefaults();
535-
rqh = (char*)x_ps_calloc(lenHost + strlen(authorization) + 330, 1); // http request header
535+
rqh = audio_calloc<char>(lenHost + strlen(authorization.get()) + 330); // http request header
536536
if(!rqh) {log_info("out of memory"); stopSong(); goto exit;}
537537

538-
strcat(rqh, "GET /");
539-
if(pos_slash > 0){ strcat(rqh, h_host.get() + pos_slash + 1);}
540-
strcat(rqh, " HTTP/1.1\r\n");
541-
strcat(rqh, "Host: ");
542-
strcat(rqh, h_host.get() + hostwoext_begin);
543-
strcat(rqh, "\r\n");
544-
strcat(rqh, "Icy-MetaData:1\r\n");
545-
strcat(rqh, "Icy-MetaData:2\r\n");
546-
strcat(rqh, "Accept:*/*\r\n");
547-
strcat(rqh, "User-Agent: VLC/3.0.21 LibVLC/3.0.21 AppleWebKit/537.36 (KHTML, like Gecko)\r\n");
548-
if(authLen > 0) { strcat(rqh, "Authorization: Basic ");
549-
strcat(rqh, authorization);
550-
strcat(rqh, "\r\n"); }
551-
strcat(rqh, "Accept-Encoding: identity;q=1,*;q=0\r\n");
552-
strcat(rqh, "Connection: keep-alive\r\n\r\n");
538+
strcat(rqh.get(), "GET /");
539+
if(pos_slash > 0){ strcat(rqh.get(), h_host.get() + pos_slash + 1);}
540+
strcat(rqh.get(), " HTTP/1.1\r\n");
541+
strcat(rqh.get(), "Host: ");
542+
strcat(rqh.get(), h_host.get() + hostwoext_begin);
543+
strcat(rqh.get(), "\r\n");
544+
strcat(rqh.get(), "Icy-MetaData:1\r\n");
545+
strcat(rqh.get(), "Icy-MetaData:2\r\n");
546+
strcat(rqh.get(), "Accept:*/*\r\n");
547+
strcat(rqh.get(), "User-Agent: VLC/3.0.21 LibVLC/3.0.21 AppleWebKit/537.36 (KHTML, like Gecko)\r\n");
548+
if(authLen > 0) { strcat(rqh.get(), "Authorization: Basic ");
549+
strcat(rqh.get(), authorization.get());
550+
strcat(rqh.get(), "\r\n"); }
551+
strcat(rqh.get(), "Accept-Encoding: identity;q=1,*;q=0\r\n");
552+
strcat(rqh.get(), "Connection: keep-alive\r\n\r\n");
553553

554554
if(m_f_ssl) { _client = static_cast<WiFiClient*>(&clientsecure);}
555555
else { _client = static_cast<WiFiClient*>(&client); }
@@ -571,7 +571,7 @@ bool Audio::connecttohost(const char* host, const char* user, const char* pwd) {
571571
m_lastHost = audio_strdup(c_host);
572572
log_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());
573573
m_f_running = true;
574-
_client->print(rqh);
574+
_client->print(rqh.get());
575575
if(endsWith(h_host.get(), ".mp3" )) m_expectedCodec = CODEC_MP3;
576576
if(endsWith(h_host.get(), ".aac" )) m_expectedCodec = CODEC_AAC;
577577
if(endsWith(h_host.get(), ".wav" )) m_expectedCodec = CODEC_WAV;
@@ -600,9 +600,6 @@ bool Audio::connecttohost(const char* host, const char* user, const char* pwd) {
600600

601601
exit:
602602
xSemaphoreGiveRecursive(mutex_playAudioData);
603-
x_ps_free(&c_host);
604-
x_ps_free(&rqh);
605-
x_ps_free(&toEncode);
606603
return res;
607604
}
608605
//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
@@ -894,37 +891,35 @@ bool Audio::connecttospeech(const char* speech, const char* lang) {
894891
return false;
895892
}
896893

897-
char* req = (char*)x_ps_calloc(strlen(urlStr.get()) + 200, sizeof(char)); // request header
898-
strcat(req, "GET ");
899-
strcat(req, path);
900-
strcat(req, "?ie=UTF-8&tl=");
901-
strcat(req, lang);
902-
strcat(req, "&client=tw-ob&q=");
903-
strcat(req, urlStr.get());
904-
strcat(req, " HTTP/1.1\r\n");
905-
strcat(req, "Host: ");
906-
strcat(req, host);
907-
strcat(req, "\r\n");
908-
strcat(req, "User-Agent: Mozilla/5.0 \r\n");
909-
strcat(req, "Accept-Encoding: identity\r\n");
910-
strcat(req, "Accept: text/html\r\n");
911-
strcat(req, "Connection: close\r\n\r\n");
894+
auto req = audio_calloc<char>(strlen(urlStr.get()) + 200); // request header
895+
strcat(req.get(), "GET ");
896+
strcat(req.get(), path);
897+
strcat(req.get(), "?ie=UTF-8&tl=");
898+
strcat(req.get(), lang);
899+
strcat(req.get(), "&client=tw-ob&q=");
900+
strcat(req.get(), urlStr.get());
901+
strcat(req.get(), " HTTP/1.1\r\n");
902+
strcat(req.get(), "Host: ");
903+
strcat(req.get(), host);
904+
strcat(req.get(), "\r\n");
905+
strcat(req.get(), "User-Agent: Mozilla/5.0 \r\n");
906+
strcat(req.get(), "Accept-Encoding: identity\r\n");
907+
strcat(req.get(), "Accept: text/html\r\n");
908+
strcat(req.get(), "Connection: close\r\n\r\n");
912909

913910
_client = static_cast<WiFiClient*>(&client);
914911
log_info("connect to \"%s\"", host);
915912
if(!_client->connect(host, 80)) {
916913
log_e("Connection failed");
917-
x_ps_free(&req);
918914
xSemaphoreGiveRecursive(mutex_playAudioData);
919915
return false;
920916
}
921-
_client->print(req);
917+
_client->print(req.get());
922918

923919
m_f_running = true;
924920
m_f_ssl = false;
925921
m_f_tts = true;
926922
m_dataMode = HTTP_RESPONSE_HEADER;
927-
x_ps_free(&req);
928923
m_lastHost = audio_strdup(host);
929924
xSemaphoreGiveRecursive(mutex_playAudioData);
930925
return true;
@@ -2871,20 +2866,20 @@ ps_ptr<char> Audio::parsePlaylist_M3U8() {
28712866
if(startsWith(m_playlistContent[i], "#")) i++; // #MY-USER-CHUNK-DATA-1:ON-TEXT-DATA="20....
28722867
if(i == lines) continue; // and exit for()
28732868

2874-
char* tmp = nullptr;
2869+
ps_ptr<char> tmp = nullptr;
28752870
if(!startsWith(m_playlistContent[i], "http")) {
28762871

28772872
// playlist: http://station.com/aaa/bbb/xxx.m3u8
28782873
// chunklist: http://station.com/aaa/bbb/ddd.aac
28792874
// result: http://station.com/aaa/bbb/ddd.aac
28802875

28812876
if(m_lastM3U8host) {
2882-
tmp = (char*)x_ps_calloc(strlen(m_lastM3U8host) + strlen(m_playlistContent[i]) + 1, sizeof(char));
2883-
strcpy(tmp, m_lastM3U8host);
2877+
tmp = audio_calloc<char>(strlen(m_lastM3U8host) + strlen(m_playlistContent[i]) + 1);
2878+
strcpy(tmp.get(), m_lastM3U8host);
28842879
}
28852880
else {
2886-
tmp = (char*)x_ps_calloc(strlen(m_lastHost.get()) + strlen(m_playlistContent[i]) + 1, sizeof(char));
2887-
strcpy(tmp, m_lastHost.get());
2881+
tmp = audio_calloc<char>(strlen(m_lastHost.get()) + strlen(m_playlistContent[i]) + 1);
2882+
strcpy(tmp.get(), m_lastHost.get());
28882883
}
28892884

28902885
if(m_playlistContent[i][0] != '/'){
@@ -2893,62 +2888,59 @@ ps_ptr<char> Audio::parsePlaylist_M3U8() {
28932888
// chunklist: ddd.aac // m_playlistContent[i]
28942889
// result: http://station.com/aaa/bbb/ddd.aac // m_playlistContent[i]
28952890

2896-
int idx = lastIndexOf(tmp, "/");
2891+
int idx = lastIndexOf(tmp.get(), "/");
28972892
tmp[idx + 1] = '\0';
2898-
strcat(tmp, m_playlistContent[i]);
2893+
strcat(tmp.get(), m_playlistContent[i]);
28992894
}
29002895
else{
29012896

29022897
// playlist: http://station.com/aaa/bbb/xxx.m3u8
29032898
// chunklist: /aaa/bbb/ddd.aac
29042899
// result: http://station.com/aaa/bbb/ddd.aac
29052900

2906-
int idx = indexOf(tmp, "/", 8);
2901+
int idx = indexOf(tmp.get(), "/", 8);
29072902
tmp[idx] = '\0';
2908-
strcat(tmp, m_playlistContent[i]);
2903+
strcat(tmp.get(), m_playlistContent[i]);
29092904
}
29102905
}
2911-
else { tmp = strdup(m_playlistContent[i]); }
2906+
else { tmp = audio_strdup(m_playlistContent[i]); }
29122907

29132908
if(f_mediaSeq_found) {
29142909
lltoa(xMedSeq, llasc, 10);
2915-
if(indexOf(tmp, llasc) > 0) {
2916-
m_playlistURL.insert(m_playlistURL.begin(), strdup(tmp));
2910+
if(indexOf(tmp.get(), llasc) > 0) {
2911+
m_playlistURL.insert(m_playlistURL.begin(), strdup(tmp.get()));
29172912
xMedSeq++;
29182913
}
29192914
else{
29202915
lltoa(xMedSeq + 1, llasc, 10);
2921-
if(indexOf(tmp, llasc) > 0) {
2922-
m_playlistURL.insert(m_playlistURL.begin(), strdup(tmp));
2916+
if(indexOf(tmp.get(), llasc) > 0) {
2917+
m_playlistURL.insert(m_playlistURL.begin(), strdup(tmp.get()));
29232918
log_w("mediaseq %llu skipped", xMedSeq);
29242919
xMedSeq+= 2;
29252920
}
29262921
}
29272922
}
29282923
else { // without mediaSeqNr, with hash
2929-
uint32_t hash = simpleHash(tmp);
2924+
uint32_t hash = simpleHash(tmp.get());
29302925
if(m_hashQueue.size() == 0) {
29312926
m_hashQueue.insert(m_hashQueue.begin(), hash);
2932-
m_playlistURL.insert(m_playlistURL.begin(), strdup(tmp));
2927+
m_playlistURL.insert(m_playlistURL.begin(), strdup(tmp.get()));
29332928
}
29342929
else {
29352930
bool known = false;
29362931
for(int i = 0; i < m_hashQueue.size(); i++) {
29372932
if(hash == m_hashQueue[i]) {
2938-
if(m_f_Log) log_i("file already known %s", tmp);
2933+
// log_i("file already known %s", tmp);
29392934
known = true;
29402935
}
29412936
}
29422937
if(!known) {
29432938
m_hashQueue.insert(m_hashQueue.begin(), hash);
2944-
m_playlistURL.insert(m_playlistURL.begin(), strdup(tmp));
2939+
m_playlistURL.insert(m_playlistURL.begin(), strdup(tmp.get()));
29452940
}
29462941
}
29472942
if(m_hashQueue.size() > 20) m_hashQueue.pop_back();
29482943
}
2949-
2950-
x_ps_free(&tmp);
2951-
29522944
continue;
29532945
}
29542946
}

0 commit comments

Comments
 (0)