Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Target host is not specified with HLS relative urls in m3u8 playlists #48

Merged
merged 5 commits into from
Dec 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -28,6 +28,7 @@ public class BeamSegmentUrlProvider extends M3uStreamSegmentUrlProvider {
* @param channelId Channel ID number.
*/
public BeamSegmentUrlProvider(String channelId) {
super(null);
this.channelId = channelId;
}

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 @@ -122,6 +140,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 @@ -133,7 +165,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