diff --git a/common/src/main/java/ctbrec/sites/jasmin/LiveJasminWebSocketDownload.java b/common/src/main/java/ctbrec/sites/jasmin/LiveJasminWebSocketDownload.java index 47327987..f28c7252 100644 --- a/common/src/main/java/ctbrec/sites/jasmin/LiveJasminWebSocketDownload.java +++ b/common/src/main/java/ctbrec/sites/jasmin/LiveJasminWebSocketDownload.java @@ -13,8 +13,13 @@ import org.json.JSONObject; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import com.google.common.eventbus.Subscribe; + import ctbrec.Config; import ctbrec.Model; +import ctbrec.event.Event; +import ctbrec.event.EventBusHolder; +import ctbrec.event.ModelStateChangedEvent; import ctbrec.io.HttpClient; import ctbrec.recorder.download.Download; import okhttp3.Request; @@ -67,6 +72,8 @@ public class LiveJasminWebSocketDownload implements Download { LOG.debug("stream host: {}",streamHost); LOG.debug("clientinstanceid {}",clientInstanceId); + EventBusHolder.BUS.register(this); + Request request = new Request.Builder() .url("https://" + relayHost + "/") .header("Origin", "https://www.livejasmin.com") @@ -98,13 +105,26 @@ public class LiveJasminWebSocketDownload implements Download { sendToRelay("{\"event\":\"connectSharedObject\",\"name\":\"data/chat_so\"}"); }).start(); } else if (event.optString("event").equals("updateSharedObject")) { - // TODO JSONArray list = event.getJSONArray("list"); for (int i = 0; i < list.length(); i++) { JSONObject obj = list.getJSONObject(i); if (obj.optString("name").equals("streamList")) { - LOG.debug(obj.toString(2)); + //LOG.debug(obj.toString(2)); streamPath = getStreamPath(obj.getJSONObject("newValue")); + } else if(obj.optString("name").equals("isPrivate") + || obj.optString("name").equals("onPrivate") + || obj.optString("name").equals("onPrivateAll") + || obj.optString("name").equals("onPrivateLJ")) + { + if(obj.optBoolean("newValue")) { + // model went private, stop recording + LOG.debug("Model {} state changed to private -> stopping download", model.getName()); + stop(); + } + } else if(obj.optString("name").equals("recommendedBandwidth") || obj.optString("name").equals("realQualityData")) { + // stream quality related -> do nothing + } else { + LOG.debug("{} -{}", model.getName(), obj.toString()); } } @@ -120,13 +140,22 @@ public class LiveJasminWebSocketDownload implements Download { } }).start(); } - }else if(event.optString("event").equals("call")) { + } else if(event.optString("event").equals("call")) { String func = event.optString("funcName"); - if(func.equals("closeConnection")) { + if (func.equals("closeConnection")) { connectionClosed = true; - //System.out.println(event.get("data")); + // System.out.println(event.get("data")); stop(); + } else if (func.equals("addLine")) { + // chat message -> ignore + } else if (func.equals("receiveInvitation")) { + // invitation to private show -> ignore + } else { + LOG.debug("{} -{}", model.getName(), event.toString()); } + } else { + if(!event.optString("event").equals("pong")) + LOG.debug("{} -{}", model.getName(), event.toString()); } } @@ -156,12 +185,14 @@ public class LiveJasminWebSocketDownload implements Download { @Override public void onClosed(WebSocket webSocket, int code, String reason) { LOG.trace("relay closed {} {} {}", code, reason, model.getName()); + stop(); } @Override public void onFailure(WebSocket webSocket, Throwable t, Response response) { if(!connectionClosed) { LOG.trace("relay failure {}", model.getName(), t); + stop(); if (response != null) { response.close(); } @@ -170,6 +201,17 @@ public class LiveJasminWebSocketDownload implements Download { }); } + @Subscribe + public void handleEvent(Event evt) { + if(evt.getType() == Event.Type.MODEL_STATUS_CHANGED) { + ModelStateChangedEvent me = (ModelStateChangedEvent) evt; + if(me.getModel().equals(model) && me.getOldState() == Model.State.ONLINE) { + LOG.debug("Model {} state changed to {} -> stopping download", me.getNewState(), model.getName()); + stop(); + } + } + } + private void sendToRelay(String msg) { LOG.trace("relay --> {} {}", model.getName(), msg); relay.send(msg); @@ -264,12 +306,14 @@ public class LiveJasminWebSocketDownload implements Download { @Override public void onClosed(WebSocket webSocket, int code, String reason) { LOG.trace("stream closed {} {} {}", code, reason, model.getName()); + stop(); } @Override public void onFailure(WebSocket webSocket, Throwable t, Response response) { if(!connectionClosed) { LOG.trace("stream failure {}", model.getName(), t); + stop(); if (response != null) { response.close(); } @@ -281,9 +325,14 @@ public class LiveJasminWebSocketDownload implements Download { @Override public void stop() { connectionClosed = true; - stream.close(1000, ""); - relay.close(1000, ""); + EventBusHolder.BUS.unregister(this); isAlive = false; + if (stream != null) { + stream.close(1000, ""); + } + if (relay != null) { + relay.close(1000, ""); + } } @Override