From 3c86310edf5510a8e9a66777f7c2fb4ed396eb48 Mon Sep 17 00:00:00 2001 From: 0xboobface <0xboobface@gmail.com> Date: Thu, 23 Apr 2020 19:28:12 +0200 Subject: [PATCH] Add stream source "Origin" --- .../java/ctbrec/NotImplementedExcetion.java | 13 +++++++ .../recorder/download/StreamSource.java | 5 ++- .../sites/streamate/StreamateModel.java | 39 +++++++++++-------- 3 files changed, 39 insertions(+), 18 deletions(-) create mode 100644 common/src/main/java/ctbrec/NotImplementedExcetion.java diff --git a/common/src/main/java/ctbrec/NotImplementedExcetion.java b/common/src/main/java/ctbrec/NotImplementedExcetion.java new file mode 100644 index 00000000..4fb3d3c9 --- /dev/null +++ b/common/src/main/java/ctbrec/NotImplementedExcetion.java @@ -0,0 +1,13 @@ +package ctbrec; + +public class NotImplementedExcetion extends RuntimeException { + + public NotImplementedExcetion() { + super(); + } + + public NotImplementedExcetion(String mesg) { + super(mesg); + } + +} diff --git a/common/src/main/java/ctbrec/recorder/download/StreamSource.java b/common/src/main/java/ctbrec/recorder/download/StreamSource.java index 76ca84e6..5bd01a0c 100644 --- a/common/src/main/java/ctbrec/recorder/download/StreamSource.java +++ b/common/src/main/java/ctbrec/recorder/download/StreamSource.java @@ -3,6 +3,7 @@ package ctbrec.recorder.download; import java.text.DecimalFormat; public class StreamSource implements Comparable { + public static final int ORIGIN = Integer.MAX_VALUE - 1; public int bandwidth; public int width; public int height; @@ -44,8 +45,10 @@ public class StreamSource implements Comparable { public String toString() { DecimalFormat df = new DecimalFormat("0.00"); 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)"; + } else if (height == ORIGIN) { + return "Origin"; } else { return height + "p (" + df.format(mbit) + " Mbit/s)"; } diff --git a/common/src/main/java/ctbrec/sites/streamate/StreamateModel.java b/common/src/main/java/ctbrec/sites/streamate/StreamateModel.java index 90439d30..d35f81d3 100644 --- a/common/src/main/java/ctbrec/sites/streamate/StreamateModel.java +++ b/common/src/main/java/ctbrec/sites/streamate/StreamateModel.java @@ -22,6 +22,7 @@ import com.squareup.moshi.JsonWriter; import ctbrec.AbstractModel; import ctbrec.Config; +import ctbrec.NotImplementedExcetion; import ctbrec.io.HttpException; import ctbrec.recorder.download.StreamSource; import okhttp3.MediaType; @@ -31,6 +32,8 @@ import okhttp3.Response; public class StreamateModel extends AbstractModel { + private static final String ORIGIN = "origin"; + private static final Logger LOG = LoggerFactory.getLogger(StreamateModel.class); private boolean online = false; @@ -40,7 +43,7 @@ public class StreamateModel extends AbstractModel { @Override 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"; Request req = new Request.Builder().url(url) .addHeader(USER_AGENT, Config.getInstance().getSettings().httpUserAgent) @@ -49,7 +52,7 @@ public class StreamateModel extends AbstractModel { .addHeader(REFERER, Streamate.BASE_URL + '/' + getName()) .addHeader(X_REQUESTED_WITH, XML_HTTP_REQUEST) .build(); - try(Response response = site.getHttpClient().execute(req)) { + try (Response response = site.getHttpClient().execute(req)) { online = response.isSuccessful(); } } @@ -87,8 +90,8 @@ public class StreamateModel extends AbstractModel { .addHeader(REFERER, Streamate.BASE_URL + '/' + getName()) .addHeader(X_REQUESTED_WITH, XML_HTTP_REQUEST) .build(); - try(Response response = site.getHttpClient().execute(req)) { - if(response.isSuccessful()) { + try (Response response = site.getHttpClient().execute(req)) { + if (response.isSuccessful()) { JSONObject json = new JSONObject(response.body().string()); JSONObject formats = json.getJSONObject("formats"); JSONObject hls = formats.getJSONObject("mp4-hls"); @@ -107,17 +110,18 @@ public class StreamateModel extends AbstractModel { } // add raw source stream - if(formats.has("mp4-ws")) { - JSONObject ws = formats.getJSONObject("mp4-ws"); - JSONObject origin = hls.getJSONObject("origin"); + if (hls.has(ORIGIN)) { + JSONObject origin = hls.getJSONObject(ORIGIN); StreamSource src = new StreamSource(); 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.height = origin.optInt("videoHeight"); src.bandwidth = (origin.optInt("videoKbps") + origin.optInt("audioKbps")) * 1024; + src.height = StreamSource.ORIGIN; streamSources.add(src); } + } else { throw new HttpException(response.code(), response.message()); } @@ -159,10 +163,13 @@ public class StreamateModel extends AbstractModel { if(!isOnline()) { return new int[2]; } - List streamSources = getStreamSources(); - Collections.sort(streamSources); - StreamSource best = streamSources.get(streamSources.size()-1); - resolution = new int[] {best.width, best.height}; + List sources = getStreamSources(); + Collections.sort(sources); + StreamSource best = sources.get(sources.size() - 1); + if (best.height == StreamSource.ORIGIN) { + best = sources.get(sources.size() - 2); + } + resolution = new int[] { best.width, best.height }; } catch (InterruptedException e) { LOG.warn("Couldn't determine stream resolution for {} - {}", getName(), e.getMessage()); Thread.currentThread().interrupt(); @@ -208,7 +215,7 @@ public class StreamateModel extends AbstractModel { .addHeader(REFERER, getSite().getBaseUrl()) .post(body) .build(); - try(Response response = getSite().getHttpClient().execute(request)) { + try (Response response = getSite().getHttpClient().execute(request)) { String content = response.body().string(); if (response.isSuccessful()) { JSONObject json = new JSONObject(content); @@ -229,14 +236,13 @@ public class StreamateModel extends AbstractModel { @Override public void readSiteSpecificData(JsonReader reader) throws IOException { - reader.nextName(); id = reader.nextLong(); } @Override public void writeSiteSpecificData(JsonWriter writer) throws IOException { - if(id == null) { + if (id == null) { try { loadModelId(); } catch (IOException e) { @@ -248,7 +254,6 @@ public class StreamateModel extends AbstractModel { @Override public void receiveTip(Double tokens) throws IOException { - // TODO Auto-generated method stub - + throw new NotImplementedExcetion("Tipping is not implemented for Streamate"); } } \ No newline at end of file