13
13
import java .io .IOException ;
14
14
import java .io .InputStream ;
15
15
import java .net .URI ;
16
+ import java .net .URISyntaxException ;
16
17
import java .util .ArrayList ;
17
18
import java .util .List ;
18
19
@@ -29,8 +30,25 @@ public abstract class M3uStreamSegmentUrlProvider {
29
30
private static final long SEGMENT_WAIT_STEP_MS = 200 ;
30
31
private static final RequestConfig streamingRequestConfig = RequestConfig .custom ().setSocketTimeout (5000 ).setConnectionRequestTimeout (5000 ).setConnectTimeout (5000 ).build ();
31
32
33
+ protected String baseUrl ;
32
34
protected SegmentInfo lastSegment ;
33
35
36
+ protected M3uStreamSegmentUrlProvider () {
37
+ this (null );
38
+ }
39
+
40
+ protected M3uStreamSegmentUrlProvider (String originUrl ) {
41
+ if (originUrl != null ) {
42
+ if (originUrl .endsWith ("/" )) {
43
+ originUrl = originUrl .substring (0 , originUrl .length () - 1 );
44
+ }
45
+
46
+ this .baseUrl = originUrl .substring (0 , originUrl .lastIndexOf ("/" ));
47
+ } else {
48
+ this .baseUrl = null ;
49
+ }
50
+ }
51
+
34
52
protected static String createSegmentUrl (String playlistUrl , String segmentName ) {
35
53
return URI .create (playlistUrl ).resolve (segmentName ).toString ();
36
54
}
@@ -118,6 +136,20 @@ public InputStream getNextSegmentStream(HttpInterface httpInterface) {
118
136
119
137
protected abstract HttpUriRequest createSegmentGetRequest (String url );
120
138
139
+ protected boolean isAbsoluteUrl (String url ) {
140
+ try {
141
+ // A URL is considered absolute if we don't have a baseUrl (so cannot convert a relative URL)
142
+ // or if URI#isAbsolute returns true.
143
+ return this .baseUrl == null || new URI (url ).isAbsolute ();
144
+ } catch (URISyntaxException e ) {
145
+ return false ;
146
+ }
147
+ }
148
+
149
+ protected String getAbsoluteUrl (String url ) {
150
+ return baseUrl + ((url .startsWith ("/" )) ? url : "/" + url );
151
+ }
152
+
121
153
protected List <ChannelStreamInfo > loadChannelStreamsList (String [] lines ) {
122
154
ExtendedM3uParser .Line streamInfoLine = null ;
123
155
@@ -129,7 +161,8 @@ protected List<ChannelStreamInfo> loadChannelStreamsList(String[] lines) {
129
161
if (line .isData () && streamInfoLine != null ) {
130
162
String quality = getQualityFromM3uDirective (streamInfoLine );
131
163
if (quality != null ) {
132
- streams .add (new ChannelStreamInfo (quality , line .lineData ));
164
+ String lineData = line .lineData ;
165
+ streams .add (new ChannelStreamInfo (quality , isAbsoluteUrl (lineData ) ? lineData : getAbsoluteUrl (lineData )));
133
166
}
134
167
135
168
streamInfoLine = null ;
0 commit comments