Skip to content

Commit

Permalink
[v1-legacy] Fix Target host is not specified with HLS relative urls…
Browse files Browse the repository at this point in the history
… in m3u8 playlists (#49)

* Backporting relative URL to absolute URL conversion.

* Fix bad logic
  • Loading branch information
devoxin authored Dec 26, 2023
1 parent 254e34d commit d9bae45
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public class HlsStreamSegmentUrlProvider extends M3uStreamSegmentUrlProvider {
private volatile String segmentPlaylistUrl;

public HlsStreamSegmentUrlProvider(String streamListUrl, String segmentPlaylistUrl) {
super(streamListUrl);
this.streamListUrl = streamListUrl;
this.segmentPlaylistUrl = segmentPlaylistUrl;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;

Expand All @@ -29,8 +30,25 @@ public abstract class M3uStreamSegmentUrlProvider {
private static final long SEGMENT_WAIT_STEP_MS = 200;
private static final RequestConfig streamingRequestConfig = RequestConfig.custom().setSocketTimeout(5000).setConnectionRequestTimeout(5000).setConnectTimeout(5000).build();

protected String baseUrl;
protected SegmentInfo lastSegment;

protected M3uStreamSegmentUrlProvider() {
this(null);
}

protected M3uStreamSegmentUrlProvider(String originUrl) {
if (originUrl != null) {
if (originUrl.endsWith("/")) {
originUrl = originUrl.substring(0, originUrl.length() - 1);
}

this.baseUrl = originUrl.substring(0, originUrl.lastIndexOf("/"));
} else {
this.baseUrl = null;
}
}

protected static String createSegmentUrl(String playlistUrl, String segmentName) {
return URI.create(playlistUrl).resolve(segmentName).toString();
}
Expand Down Expand Up @@ -118,6 +136,20 @@ public InputStream getNextSegmentStream(HttpInterface httpInterface) {

protected abstract HttpUriRequest createSegmentGetRequest(String url);

protected boolean isAbsoluteUrl(String url) {
try {
// A URL is considered absolute if we don't have a baseUrl (so cannot convert a relative URL)
// or if URI#isAbsolute returns true.
return this.baseUrl == null || new URI(url).isAbsolute();
} catch (URISyntaxException e) {
return false;
}
}

protected String getAbsoluteUrl(String url) {
return baseUrl + ((url.startsWith("/")) ? url : "/" + url);
}

protected List<ChannelStreamInfo> loadChannelStreamsList(String[] lines) {
ExtendedM3uParser.Line streamInfoLine = null;

Expand All @@ -129,7 +161,8 @@ protected List<ChannelStreamInfo> loadChannelStreamsList(String[] lines) {
if (line.isData() && streamInfoLine != null) {
String quality = getQualityFromM3uDirective(streamInfoLine);
if (quality != null) {
streams.add(new ChannelStreamInfo(quality, line.lineData));
String lineData = line.lineData;
streams.add(new ChannelStreamInfo(quality, isAbsoluteUrl(lineData) ? lineData : getAbsoluteUrl(lineData)));
}

streamInfoLine = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public class TwitchStreamSegmentUrlProvider extends M3uStreamSegmentUrlProvider
* @param manager Twitch source manager.
*/
public TwitchStreamSegmentUrlProvider(String channelName, TwitchStreamAudioSourceManager manager) {
super(null);
this.channelName = channelName;
this.manager = manager;
this.tokenExpirationTime = -1;
Expand Down

0 comments on commit d9bae45

Please sign in to comment.