Skip to content

Commit 1265146

Browse files
committed
HLSStream: URLStream w/o pointers
1 parent 80bb903 commit 1265146

File tree

1 file changed

+16
-21
lines changed

1 file changed

+16
-21
lines changed

src/AudioTools/AudioLibs/HLSStream.h

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ namespace audio_tools_hls {
2626

2727
class URLLoaderHLS {
2828
public:
29-
// URLLoaderHLS(URLStream &stream) { p_stream = &stream; };
3029
URLLoaderHLS() = default;
3130

3231
~URLLoaderHLS() { end(); }
@@ -41,8 +40,7 @@ class URLLoaderHLS {
4140

4241
void end() {
4342
TRACED();
44-
if (p_stream != nullptr) p_stream->end();
45-
p_stream = nullptr;
43+
url_stream.end();
4644
buffer.clear();
4745
active = false;
4846
}
@@ -80,13 +78,11 @@ class URLLoaderHLS {
8078
}
8179

8280
const char *contentType() {
83-
if (p_stream == nullptr) return nullptr;
84-
return p_stream->httpRequest().reply().get(CONTENT_TYPE);
81+
return url_stream.httpRequest().reply().get(CONTENT_TYPE);
8582
}
8683

8784
int contentLength() {
88-
if (p_stream == nullptr) return 0;
89-
return p_stream->contentLength();
85+
return url_stream.contentLength();
9086
}
9187

9288
void setBufferSize(int size, int count) {
@@ -98,16 +94,15 @@ class URLLoaderHLS {
9894
}
9995
}
10096

101-
void setCACert(const char *cert) { p_stream->setCACert(cert); }
97+
void setCACert(const char *cert) { url_stream.setCACert(cert); }
10298

10399
protected:
104100
Vector<const char *> urls{10};
105101
RingBuffer<uint8_t> buffer{0};
106102
bool active = false;
107103
int buffer_size = DEFAULT_BUFFER_SIZE;
108104
int buffer_count = HLS_BUFFER_COUNT;
109-
URLStream default_stream;
110-
URLStream *p_stream = &default_stream;
105+
URLStream url_stream;
111106
const char *url_to_play = nullptr;
112107

113108
/// try to keep the buffer filled
@@ -126,22 +121,22 @@ class URLLoaderHLS {
126121
}
127122

128123
// switch current stream if we have no more data
129-
if (!*p_stream && !urls.empty()) {
124+
if (!url_stream && !urls.empty()) {
130125
LOGD("Refilling");
131126
if (url_to_play != nullptr) {
132127
delete url_to_play;
133128
}
134129
url_to_play = urls[0];
135130
LOGI("playing %s", url_to_play);
136-
p_stream->end();
137-
p_stream->setConnectionClose(true);
138-
p_stream->setTimeout(HLS_TIMEOUT);
139-
p_stream->begin(url_to_play);
140-
p_stream->waitForData(HLS_TIMEOUT);
131+
url_stream.end();
132+
url_stream.setConnectionClose(true);
133+
url_stream.setTimeout(HLS_TIMEOUT);
134+
url_stream.begin(url_to_play);
135+
url_stream.waitForData(HLS_TIMEOUT);
141136
urls.pop_front();
142137
// assert(urls[0]!=url);
143138

144-
LOGI("Playing %s of %d", p_stream->urlStr(), (int)urls.size());
139+
LOGI("Playing %s of %d", url_stream.urlStr(), (int)urls.size());
145140
}
146141

147142
int total = 0;
@@ -150,7 +145,7 @@ class URLLoaderHLS {
150145
// try to keep the buffer filled
151146
while (to_write > 0) {
152147
uint8_t tmp[to_write] = {0};
153-
int read = p_stream->readBytes(tmp, to_write);
148+
int read = url_stream.readBytes(tmp, to_write);
154149
total += read;
155150
if (read > 0) {
156151
failed = 0;
@@ -160,10 +155,10 @@ class URLLoaderHLS {
160155
to_write = min(buffer.availableForWrite(), DEFAULT_BUFFER_SIZE);
161156
}
162157
// After we processed all data we close the stream to get a new url
163-
if (p_stream->totalRead() == p_stream->contentLength()) {
158+
if (url_stream.totalRead() == url_stream.contentLength()) {
164159
LOGI("Closing stream because all bytes were processed: available: %d",
165-
p_stream->available());
166-
p_stream->end();
160+
url_stream.available());
161+
url_stream.end();
167162
break;
168163
}
169164
LOGD("Refilled with %d now %d available to write", total,

0 commit comments

Comments
 (0)