Add stream source "Origin"

This commit is contained in:
0xboobface 2020-04-23 19:28:12 +02:00
parent a4d517aa25
commit 3c86310edf
3 changed files with 39 additions and 18 deletions

View File

@ -0,0 +1,13 @@
package ctbrec;
public class NotImplementedExcetion extends RuntimeException {
public NotImplementedExcetion() {
super();
}
public NotImplementedExcetion(String mesg) {
super(mesg);
}
}

View File

@ -3,6 +3,7 @@ package ctbrec.recorder.download;
import java.text.DecimalFormat; import java.text.DecimalFormat;
public class StreamSource implements Comparable<StreamSource> { public class StreamSource implements Comparable<StreamSource> {
public static final int ORIGIN = Integer.MAX_VALUE - 1;
public int bandwidth; public int bandwidth;
public int width; public int width;
public int height; public int height;
@ -44,8 +45,10 @@ public class StreamSource implements Comparable<StreamSource> {
public String toString() { public String toString() {
DecimalFormat df = new DecimalFormat("0.00"); DecimalFormat df = new DecimalFormat("0.00");
float mbit = bandwidth / 1024.0f / 1024.0f; float mbit = bandwidth / 1024.0f / 1024.0f;
if(height == Integer.MAX_VALUE) { if (height == Integer.MAX_VALUE) {
return "unknown resolution (" + df.format(mbit) + " Mbit/s)"; return "unknown resolution (" + df.format(mbit) + " Mbit/s)";
} else if (height == ORIGIN) {
return "Origin";
} else { } else {
return height + "p (" + df.format(mbit) + " Mbit/s)"; return height + "p (" + df.format(mbit) + " Mbit/s)";
} }

View File

@ -22,6 +22,7 @@ import com.squareup.moshi.JsonWriter;
import ctbrec.AbstractModel; import ctbrec.AbstractModel;
import ctbrec.Config; import ctbrec.Config;
import ctbrec.NotImplementedExcetion;
import ctbrec.io.HttpException; import ctbrec.io.HttpException;
import ctbrec.recorder.download.StreamSource; import ctbrec.recorder.download.StreamSource;
import okhttp3.MediaType; import okhttp3.MediaType;
@ -31,6 +32,8 @@ import okhttp3.Response;
public class StreamateModel extends AbstractModel { public class StreamateModel extends AbstractModel {
private static final String ORIGIN = "origin";
private static final Logger LOG = LoggerFactory.getLogger(StreamateModel.class); private static final Logger LOG = LoggerFactory.getLogger(StreamateModel.class);
private boolean online = false; private boolean online = false;
@ -40,7 +43,7 @@ public class StreamateModel extends AbstractModel {
@Override @Override
public boolean isOnline(boolean ignoreCache) throws IOException, ExecutionException, InterruptedException { public boolean isOnline(boolean ignoreCache) throws IOException, ExecutionException, InterruptedException {
if(ignoreCache) { if (ignoreCache) {
String url = "https://sea1c-ls.naiadsystems.com/sea1c-edge-ls/80/live/s:" + getName() + ".json"; String url = "https://sea1c-ls.naiadsystems.com/sea1c-edge-ls/80/live/s:" + getName() + ".json";
Request req = new Request.Builder().url(url) Request req = new Request.Builder().url(url)
.addHeader(USER_AGENT, Config.getInstance().getSettings().httpUserAgent) .addHeader(USER_AGENT, Config.getInstance().getSettings().httpUserAgent)
@ -49,7 +52,7 @@ public class StreamateModel extends AbstractModel {
.addHeader(REFERER, Streamate.BASE_URL + '/' + getName()) .addHeader(REFERER, Streamate.BASE_URL + '/' + getName())
.addHeader(X_REQUESTED_WITH, XML_HTTP_REQUEST) .addHeader(X_REQUESTED_WITH, XML_HTTP_REQUEST)
.build(); .build();
try(Response response = site.getHttpClient().execute(req)) { try (Response response = site.getHttpClient().execute(req)) {
online = response.isSuccessful(); online = response.isSuccessful();
} }
} }
@ -87,8 +90,8 @@ public class StreamateModel extends AbstractModel {
.addHeader(REFERER, Streamate.BASE_URL + '/' + getName()) .addHeader(REFERER, Streamate.BASE_URL + '/' + getName())
.addHeader(X_REQUESTED_WITH, XML_HTTP_REQUEST) .addHeader(X_REQUESTED_WITH, XML_HTTP_REQUEST)
.build(); .build();
try(Response response = site.getHttpClient().execute(req)) { try (Response response = site.getHttpClient().execute(req)) {
if(response.isSuccessful()) { if (response.isSuccessful()) {
JSONObject json = new JSONObject(response.body().string()); JSONObject json = new JSONObject(response.body().string());
JSONObject formats = json.getJSONObject("formats"); JSONObject formats = json.getJSONObject("formats");
JSONObject hls = formats.getJSONObject("mp4-hls"); JSONObject hls = formats.getJSONObject("mp4-hls");
@ -107,17 +110,18 @@ public class StreamateModel extends AbstractModel {
} }
// add raw source stream // add raw source stream
if(formats.has("mp4-ws")) { if (hls.has(ORIGIN)) {
JSONObject ws = formats.getJSONObject("mp4-ws"); JSONObject origin = hls.getJSONObject(ORIGIN);
JSONObject origin = hls.getJSONObject("origin");
StreamSource src = new StreamSource(); StreamSource src = new StreamSource();
src.mediaPlaylistUrl = origin.getString("location"); src.mediaPlaylistUrl = origin.getString("location");
origin = ws.getJSONObject("origin"); // switch to web socket origin, because it has width, height and bitrates origin = hls.getJSONObject(ORIGIN);
src.width = origin.optInt("videoWidth"); src.width = origin.optInt("videoWidth");
src.height = origin.optInt("videoHeight"); src.height = origin.optInt("videoHeight");
src.bandwidth = (origin.optInt("videoKbps") + origin.optInt("audioKbps")) * 1024; src.bandwidth = (origin.optInt("videoKbps") + origin.optInt("audioKbps")) * 1024;
src.height = StreamSource.ORIGIN;
streamSources.add(src); streamSources.add(src);
} }
} else { } else {
throw new HttpException(response.code(), response.message()); throw new HttpException(response.code(), response.message());
} }
@ -159,10 +163,13 @@ public class StreamateModel extends AbstractModel {
if(!isOnline()) { if(!isOnline()) {
return new int[2]; return new int[2];
} }
List<StreamSource> streamSources = getStreamSources(); List<StreamSource> sources = getStreamSources();
Collections.sort(streamSources); Collections.sort(sources);
StreamSource best = streamSources.get(streamSources.size()-1); StreamSource best = sources.get(sources.size() - 1);
resolution = new int[] {best.width, best.height}; if (best.height == StreamSource.ORIGIN) {
best = sources.get(sources.size() - 2);
}
resolution = new int[] { best.width, best.height };
} catch (InterruptedException e) { } catch (InterruptedException e) {
LOG.warn("Couldn't determine stream resolution for {} - {}", getName(), e.getMessage()); LOG.warn("Couldn't determine stream resolution for {} - {}", getName(), e.getMessage());
Thread.currentThread().interrupt(); Thread.currentThread().interrupt();
@ -208,7 +215,7 @@ public class StreamateModel extends AbstractModel {
.addHeader(REFERER, getSite().getBaseUrl()) .addHeader(REFERER, getSite().getBaseUrl())
.post(body) .post(body)
.build(); .build();
try(Response response = getSite().getHttpClient().execute(request)) { try (Response response = getSite().getHttpClient().execute(request)) {
String content = response.body().string(); String content = response.body().string();
if (response.isSuccessful()) { if (response.isSuccessful()) {
JSONObject json = new JSONObject(content); JSONObject json = new JSONObject(content);
@ -229,14 +236,13 @@ public class StreamateModel extends AbstractModel {
@Override @Override
public void readSiteSpecificData(JsonReader reader) throws IOException { public void readSiteSpecificData(JsonReader reader) throws IOException {
reader.nextName(); reader.nextName();
id = reader.nextLong(); id = reader.nextLong();
} }
@Override @Override
public void writeSiteSpecificData(JsonWriter writer) throws IOException { public void writeSiteSpecificData(JsonWriter writer) throws IOException {
if(id == null) { if (id == null) {
try { try {
loadModelId(); loadModelId();
} catch (IOException e) { } catch (IOException e) {
@ -248,7 +254,6 @@ public class StreamateModel extends AbstractModel {
@Override @Override
public void receiveTip(Double tokens) throws IOException { public void receiveTip(Double tokens) throws IOException {
// TODO Auto-generated method stub throw new NotImplementedExcetion("Tipping is not implemented for Streamate");
} }
} }