11#pragma once
2- #include " AudioToolsConfig.h"
32#include " AudioTools/AudioCodecs/AudioEncoded.h"
43#include " AudioTools/CoreAudio/AudioBasic/Str.h"
54#include " AudioTools/CoreAudio/AudioHttp/URLStream.h"
65#include " AudioTools/CoreAudio/StreamCopy.h"
6+ #include " AudioToolsConfig.h"
77
88#define MAX_HLS_LINE 512
99#define START_URLS_LIMIT 4
@@ -24,6 +24,7 @@ namespace audio_tools_hls {
2424 * @copyright GPLv3
2525 */
2626
27+ template <typename URLStream>
2728class URLLoaderHLS {
2829 public:
2930 URLLoaderHLS () = default ;
@@ -81,15 +82,13 @@ class URLLoaderHLS {
8182 return url_stream.httpRequest ().reply ().get (CONTENT_TYPE);
8283 }
8384
84- int contentLength () {
85- return url_stream.contentLength ();
86- }
85+ int contentLength () { return url_stream.contentLength (); }
8786
8887 void setBufferSize (int size, int count) {
8988 buffer_size = size;
9089 buffer_count = count;
9190 // support call after begin()!
92- if (buffer.size ()!= 0 ) {
91+ if (buffer.size () != 0 ) {
9392 buffer.resize (buffer_size * buffer_count);
9493 }
9594 }
@@ -176,7 +175,7 @@ class URLLoaderHLS {
176175class URLHistory {
177176 public:
178177 bool add (const char *url) {
179- if (url== nullptr ) return true ;
178+ if (url == nullptr ) return true ;
180179 bool found = false ;
181180 StrView url_str (url);
182181 for (int j = 0 ; j < history.size (); j++) {
@@ -206,10 +205,11 @@ class URLHistory {
206205};
207206
208207/* *
209- * @brief Simple Parser for HLS data.
208+ * @brief Simple Parser for HLS data.
210209 * @author Phil Schatzmann
211210 * @copyright GPLv3
212211 */
212+ template <typename URLStream>
213213class HLSParser {
214214 public:
215215 // loads the index url
@@ -222,6 +222,7 @@ class HLSParser {
222222 TRACEI ();
223223 segments_url_str = " " ;
224224 bandwidth = 0 ;
225+ total_read = 0 ;
225226
226227 if (!parseIndex ()) {
227228 TRACEE ();
@@ -262,14 +263,13 @@ class HLSParser {
262263 reloadSegments ();
263264
264265 if (active) result = url_loader.readBytes (data, len);
266+ total_read += result;
265267 return result;
266268 }
267269
268270 const char *indexUrl () { return index_url_str; }
269271
270- const char *segmentsUrl () {
271- return segments_url_str.c_str ();
272- }
272+ const char *segmentsUrl () { return segments_url_str.c_str (); }
273273
274274 // / Provides the codec
275275 const char *getCodec () { return codec.c_str (); }
@@ -310,20 +310,26 @@ class HLSParser {
310310 const char *reqURL)) {
311311 resolve_url = cb;
312312 }
313+ // / Provides the hls url as string
314+ const char *urlStr () { return url_str.c_str (); }
313315
314- protected:
316+ // / Povides the number of bytes read
317+ size_t totalRead () { return total_read; };
318+
319+ protected:
315320 enum class URLType { Undefined, Index, Segment };
316321 URLType next_url_type = URLType::Undefined;
317322 int bandwidth = 0 ;
318323 int url_count = 5 ;
324+ size_t total_read = 0 ;
319325 bool url_active = false ;
320326 bool is_extm3u = false ;
321327 Str codec;
322328 Str segments_url_str;
323329 Str url_str;
324330 const char *index_url_str = nullptr ;
325331 URLStream url_stream;
326- URLLoaderHLS url_loader;
332+ URLLoaderHLS<URLStream> url_loader;
327333 URLHistory url_history;
328334 bool active = false ;
329335 bool parse_segments_active = false ;
@@ -531,7 +537,8 @@ class HLSParser {
531537 memset (tmp, 0 , MAX_HLS_LINE);
532538 while (true ) {
533539 memset (tmp, 0 , MAX_HLS_LINE);
534- size_t len = url_stream.httpRequest ().readBytesUntil (' \n ' , tmp, MAX_HLS_LINE);
540+ size_t len =
541+ url_stream.httpRequest ().readBytesUntil (' \n ' , tmp, MAX_HLS_LINE);
535542 if (len == 0 && url_stream.available () == 0 ) break ;
536543 StrView str (tmp);
537544
@@ -633,13 +640,14 @@ namespace audio_tools {
633640 * @ingroup http *@copyright GPLv3
634641 */
635642
636- class HLSStream : public AbstractURLStream {
643+ template <typename URLStream>
644+ class HLSStreamT : public AbstractURLStream {
637645 public:
638646 // / Empty constructor
639- HLSStream () = default ;
647+ HLSStreamT () = default ;
640648
641649 // / Convenience constructor which logs in to the WiFi
642- HLSStream (const char *ssid, const char *password) {
650+ HLSStreamT (const char *ssid, const char *password) {
643651 setSSID (ssid);
644652 setPassword (password);
645653 }
@@ -719,8 +727,16 @@ class HLSStream : public AbstractURLStream {
719727 parser.setURLResolver (cb);
720728 }
721729
730+ const char *urlStr () override { return parser.urlStr (); }
731+
732+ size_t totalRead () override { return parser.totalRead (); };
733+ // / not implemented
734+ void setConnectionClose (bool flag) override {};
735+ // / not implemented
736+ bool waitForData (int timeout) override { return false ; }
737+
722738 protected:
723- audio_tools_hls::HLSParser parser;
739+ audio_tools_hls::HLSParser<URLStream> parser;
724740 const char *ssid = nullptr ;
725741 const char *password = nullptr ;
726742
@@ -759,4 +775,6 @@ class HLSStream : public AbstractURLStream {
759775 void addRequestHeader (const char *header, const char *value) {}
760776};
761777
778+ using HLSStream = HLSStreamT<URLStream>;
779+
762780} // namespace audio_tools
0 commit comments