Skip to content

Commit 9091213

Browse files
committed
more uique pointers
1 parent 749bf87 commit 9091213

File tree

2 files changed

+24
-25
lines changed

2 files changed

+24
-25
lines changed

src/Audio.cpp

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -214,8 +214,6 @@ Audio::~Audio() {
214214

215215
i2s_channel_disable(m_i2s_tx_handle);
216216
i2s_del_channel(m_i2s_tx_handle);
217-
218-
x_ps_free(&m_playlistBuff);
219217
x_ps_free(&m_lastM3U8host);
220218

221219
stopAudioTask();
@@ -263,7 +261,6 @@ void Audio::setDefaults() {
263261
VORBISDecoder_FreeBuffers();
264262
memset(m_outBuff.get(), 0, m_outbuffSize * sizeof(int16_t)); // Clear OutputBuffer
265263
memset(m_samplesBuff48K.get(), 0, m_samplesBuff48KSize * sizeof(int16_t)); // Clear samplesBuff48K
266-
x_ps_free(&m_playlistBuff);
267264
vector_clear_and_shrink(m_playlistURL);
268265
vector_clear_and_shrink(m_playlistContent);
269266
m_hashQueue.clear();
@@ -2535,7 +2532,7 @@ void Audio::loop() {
25352532
}
25362533
}
25372534
else { // m3u8 datastream only
2538-
const char* host = NULL;
2535+
ps_ptr<char> host = NULL;
25392536
static uint8_t no_host_cnt = 0;
25402537
static uint32_t no_host_timer = millis();
25412538
if(no_host_timer > millis()) {return;}
@@ -2556,7 +2553,7 @@ void Audio::loop() {
25562553
if(!host) no_host_cnt++; else {no_host_cnt = 0; no_host_timer = millis();}
25572554
if(no_host_cnt == 2){no_host_timer = millis() + 2000;} // no new url? wait 2 seconds
25582555
if(host) { // host contains the next playlist URL
2559-
httpPrint(host);
2556+
httpPrint(host.get());
25602557
m_dataMode = HTTP_RESPONSE_HEADER;
25612558
}
25622559
else { // host == NULL means connect to m3u8 URL
@@ -2809,7 +2806,7 @@ const char* Audio::parsePlaylist_ASX() { // Advanced Stream Redirector
28092806
return host;
28102807
}
28112808
//-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
2812-
const char* Audio::parsePlaylist_M3U8() {
2809+
ps_ptr<char> Audio::parsePlaylist_M3U8() {
28132810

28142811
// example: audio chunks
28152812
// #EXTM3U
@@ -2967,17 +2964,16 @@ const char* Audio::parsePlaylist_M3U8() {
29672964
}
29682965

29692966
if(m_playlistURL.size() > 0) {
2970-
x_ps_free(&m_playlistBuff);
2971-
2967+
ps_ptr<char>m_playlistBuff = nullptr;
29722968
if(m_playlistURL[m_playlistURL.size() - 1]) {
2973-
m_playlistBuff = strdup(m_playlistURL[m_playlistURL.size() - 1]);
2969+
m_playlistBuff = audio_strdup(m_playlistURL[m_playlistURL.size() - 1]);
29742970
x_ps_free(&m_playlistURL[m_playlistURL.size() - 1]);
29752971
m_playlistURL.pop_back();
29762972
m_playlistURL.shrink_to_fit();
29772973
}
29782974
if(m_f_Log) log_i("now playing %s", m_playlistBuff);
2979-
if(endsWith(m_playlistBuff, "ts")) m_f_ts = true;
2980-
if(indexOf(m_playlistBuff, ".ts?") > 0) m_f_ts = true;
2975+
if(endsWith(m_playlistBuff.get(), "ts")) m_f_ts = true;
2976+
if(indexOf(m_playlistBuff.get(), ".ts?") > 0) m_f_ts = true;
29812977
return m_playlistBuff;
29822978
}
29832979
else {
@@ -6186,26 +6182,21 @@ void Audio::seek_m4a_ilst() { // ilist - item list atom, contains the metadat
61866182
if(len > 1024) len = 1024;
61876183
log_w("found at pos %i, len %i", seekpos, len);
61886184

6189-
uint8_t* data = (uint8_t*)x_ps_calloc(len, sizeof(uint8_t));
6190-
if(!data) {
6191-
log_e("out od memory");
6192-
audiofile.seek(0);
6193-
return;
6194-
}
6185+
auto data = audio_calloc<uint8_t>(len);
61956186
len -= 4;
61966187
audiofile.seek(seekpos);
6197-
audiofile.read(data, len);
6188+
audiofile.read(data.get(), len);
61986189

61996190
int offset = 0;
62006191
for(int i = 0; i < 12; i++) {
6201-
offset = specialIndexOf(data, info[i], len, true); // seek info[] with '\0'
6192+
offset = specialIndexOf(data.get(), info[i], len, true); // seek info[] with '\0'
62026193
if(offset > 0) {
62036194
offset += 19;
6204-
if(*(data + offset) == 0) offset++;
6195+
if(*(data.get() + offset) == 0) offset++;
62056196
char value[256] = {0};
6206-
size_t temp = strlen((const char*)data + offset);
6197+
size_t temp = strlen((const char*)data.get() + offset);
62076198
if(temp > 254) temp = 254;
6208-
memcpy(value, (data + offset), temp);
6199+
memcpy(value, (data.get() + offset), temp);
62096200
value[temp] = '\0';
62106201
m_chbuf[0] = '\0';
62116202
if(i == 0) sprintf(m_chbuf.get(), "Title: %s", value);
@@ -6226,7 +6217,6 @@ void Audio::seek_m4a_ilst() { // ilist - item list atom, contains the metadat
62266217
}
62276218
}
62286219
m_f_m4aID3dataAreRead = true;
6229-
x_ps_free(&data);
62306220
audiofile.seek(0);
62316221
return;
62326222
}

src/Audio.h

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ class Audio : private AudioBuffer{
208208
const char* parsePlaylist_M3U();
209209
const char* parsePlaylist_PLS();
210210
const char* parsePlaylist_ASX();
211-
const char* parsePlaylist_M3U8();
211+
ps_ptr<char> parsePlaylist_M3U8();
212212
ps_ptr<char> m3u8redirection(uint8_t* codec);
213213
uint64_t m3u8_findMediaSeqInURL();
214214
bool STfromEXTINF(char* str);
@@ -497,6 +497,16 @@ class Audio : private AudioBuffer{
497497
return std::unique_ptr<T[], PsramDeleter>(raw);
498498
}
499499

500+
// Request memory for an array of T
501+
template <typename T>
502+
std::unique_ptr<T[], PsramDeleter> audio_calloc(std::size_t count) {
503+
T* raw = static_cast<T*>(ps_calloc(count, sizeof(T)));
504+
if (!raw) {
505+
log_e("audio_malloc_array: OOM, no space for %zu bytes", sizeof(T) * count);
506+
}
507+
return std::unique_ptr<T[], PsramDeleter>(raw);
508+
}
509+
500510
// Copies raw data into a Unique_PTR goal (e.g. like memcpy)
501511
template <typename T>
502512
void audio_memcpy(std::unique_ptr<T[]>& dest, const T* src, std::size_t count) {
@@ -506,7 +516,6 @@ class Audio : private AudioBuffer{
506516
}
507517
}
508518

509-
510519
std::unique_ptr<char[], PsramDeleter> audio_strdup(const char* str) {
511520
if (!str) {
512521
log_e("audio_strdup: input str is NULL");

0 commit comments

Comments
 (0)