1
1
#pragma once
2
- #include " AudioToolsConfig.h"
3
2
#include " AudioTools/AudioCodecs/AudioEncoded.h"
4
3
#include " AudioTools/CoreAudio/AudioBasic/Str.h"
5
4
#include " AudioTools/CoreAudio/AudioHttp/URLStream.h"
6
5
#include " AudioTools/CoreAudio/StreamCopy.h"
6
+ #include " AudioToolsConfig.h"
7
7
8
8
#define MAX_HLS_LINE 512
9
9
#define START_URLS_LIMIT 4
@@ -24,6 +24,7 @@ namespace audio_tools_hls {
24
24
* @copyright GPLv3
25
25
*/
26
26
27
+ template <typename URLStream>
27
28
class URLLoaderHLS {
28
29
public:
29
30
URLLoaderHLS () = default ;
@@ -81,15 +82,13 @@ class URLLoaderHLS {
81
82
return url_stream.httpRequest ().reply ().get (CONTENT_TYPE);
82
83
}
83
84
84
- int contentLength () {
85
- return url_stream.contentLength ();
86
- }
85
+ int contentLength () { return url_stream.contentLength (); }
87
86
88
87
void setBufferSize (int size, int count) {
89
88
buffer_size = size;
90
89
buffer_count = count;
91
90
// support call after begin()!
92
- if (buffer.size ()!= 0 ) {
91
+ if (buffer.size () != 0 ) {
93
92
buffer.resize (buffer_size * buffer_count);
94
93
}
95
94
}
@@ -176,7 +175,7 @@ class URLLoaderHLS {
176
175
class URLHistory {
177
176
public:
178
177
bool add (const char *url) {
179
- if (url== nullptr ) return true ;
178
+ if (url == nullptr ) return true ;
180
179
bool found = false ;
181
180
StrView url_str (url);
182
181
for (int j = 0 ; j < history.size (); j++) {
@@ -206,10 +205,11 @@ class URLHistory {
206
205
};
207
206
208
207
/* *
209
- * @brief Simple Parser for HLS data.
208
+ * @brief Simple Parser for HLS data.
210
209
* @author Phil Schatzmann
211
210
* @copyright GPLv3
212
211
*/
212
+ template <typename URLStream>
213
213
class HLSParser {
214
214
public:
215
215
// loads the index url
@@ -222,6 +222,7 @@ class HLSParser {
222
222
TRACEI ();
223
223
segments_url_str = " " ;
224
224
bandwidth = 0 ;
225
+ total_read = 0 ;
225
226
226
227
if (!parseIndex ()) {
227
228
TRACEE ();
@@ -262,14 +263,13 @@ class HLSParser {
262
263
reloadSegments ();
263
264
264
265
if (active) result = url_loader.readBytes (data, len);
266
+ total_read += result;
265
267
return result;
266
268
}
267
269
268
270
const char *indexUrl () { return index_url_str; }
269
271
270
- const char *segmentsUrl () {
271
- return segments_url_str.c_str ();
272
- }
272
+ const char *segmentsUrl () { return segments_url_str.c_str (); }
273
273
274
274
// / Provides the codec
275
275
const char *getCodec () { return codec.c_str (); }
@@ -310,20 +310,26 @@ class HLSParser {
310
310
const char *reqURL)) {
311
311
resolve_url = cb;
312
312
}
313
+ // / Provides the hls url as string
314
+ const char *urlStr () { return url_str.c_str (); }
313
315
314
- protected:
316
+ // / Povides the number of bytes read
317
+ size_t totalRead () { return total_read; };
318
+
319
+ protected:
315
320
enum class URLType { Undefined, Index, Segment };
316
321
URLType next_url_type = URLType::Undefined;
317
322
int bandwidth = 0 ;
318
323
int url_count = 5 ;
324
+ size_t total_read = 0 ;
319
325
bool url_active = false ;
320
326
bool is_extm3u = false ;
321
327
Str codec;
322
328
Str segments_url_str;
323
329
Str url_str;
324
330
const char *index_url_str = nullptr ;
325
331
URLStream url_stream;
326
- URLLoaderHLS url_loader;
332
+ URLLoaderHLS<URLStream> url_loader;
327
333
URLHistory url_history;
328
334
bool active = false ;
329
335
bool parse_segments_active = false ;
@@ -531,7 +537,8 @@ class HLSParser {
531
537
memset (tmp, 0 , MAX_HLS_LINE);
532
538
while (true ) {
533
539
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);
535
542
if (len == 0 && url_stream.available () == 0 ) break ;
536
543
StrView str (tmp);
537
544
@@ -633,13 +640,14 @@ namespace audio_tools {
633
640
* @ingroup http *@copyright GPLv3
634
641
*/
635
642
636
- class HLSStream : public AbstractURLStream {
643
+ template <typename URLStream>
644
+ class HLSStreamT : public AbstractURLStream {
637
645
public:
638
646
// / Empty constructor
639
- HLSStream () = default ;
647
+ HLSStreamT () = default ;
640
648
641
649
// / Convenience constructor which logs in to the WiFi
642
- HLSStream (const char *ssid, const char *password) {
650
+ HLSStreamT (const char *ssid, const char *password) {
643
651
setSSID (ssid);
644
652
setPassword (password);
645
653
}
@@ -719,8 +727,16 @@ class HLSStream : public AbstractURLStream {
719
727
parser.setURLResolver (cb);
720
728
}
721
729
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
+
722
738
protected:
723
- audio_tools_hls::HLSParser parser;
739
+ audio_tools_hls::HLSParser<URLStream> parser;
724
740
const char *ssid = nullptr ;
725
741
const char *password = nullptr ;
726
742
@@ -759,4 +775,6 @@ class HLSStream : public AbstractURLStream {
759
775
void addRequestHeader (const char *header, const char *value) {}
760
776
};
761
777
778
+ using HLSStream = HLSStreamT<URLStream>;
779
+
762
780
} // namespace audio_tools
0 commit comments