Add data transfer detection to ShowupWebrtcDownload

... so that downloads don't get stuck in recording state
This commit is contained in:
0xb00bface 2021-08-08 14:49:42 +02:00
parent 114acad34c
commit 19d4286f83
2 changed files with 18 additions and 10 deletions

View File

@ -11,6 +11,8 @@
* Fixed recordings not stopping, if playlist requests returned 403 or 404 * Fixed recordings not stopping, if playlist requests returned 403 or 404
* LiveJasmin recordings no first check the high res stream and fall back to * LiveJasmin recordings no first check the high res stream and fall back to
the low res stream, if it is not available the low res stream, if it is not available
* Add data transfer detection to ShowupWebrtcDownload, so that downloads don't
get stuck in recording state
4.4.3 4.4.3
======================== ========================

View File

@ -7,6 +7,7 @@ import java.io.File;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Files; import java.nio.file.Files;
import java.time.Duration;
import java.time.Instant; import java.time.Instant;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@ -29,10 +30,12 @@ import okio.ByteString;
public class ShowupWebrtcDownload extends AbstractDownload { public class ShowupWebrtcDownload extends AbstractDownload {
private static final Logger LOG = LoggerFactory.getLogger(ShowupWebrtcDownload.class); private static final Logger LOG = LoggerFactory.getLogger(ShowupWebrtcDownload.class);
private static final int MAX_SECONDS_WITHOUT_TRANSFER = 20;
private transient WebSocket ws; private transient WebSocket ws;
private transient HttpClient httpClient; private transient HttpClient httpClient;
private transient FileOutputStream fout; private transient FileOutputStream fout;
private transient Instant timeOfLastTransfer = Instant.MAX;
private volatile boolean running; private volatile boolean running;
@ -50,6 +53,7 @@ public class ShowupWebrtcDownload extends AbstractDownload {
this.downloadExecutor = executorService; this.downloadExecutor = executorService;
splittingStrategy = initSplittingStrategy(config.getSettings()); splittingStrategy = initSplittingStrategy(config.getSettings());
targetFile = config.getFileForRecording(model, "mp4", startTime); targetFile = config.getFileForRecording(model, "mp4", startTime);
timeOfLastTransfer = Instant.now();
ShowupModel showupModel = (ShowupModel) model; ShowupModel showupModel = (ShowupModel) model;
Request request = new Request.Builder() Request request = new Request.Builder()
@ -80,6 +84,7 @@ public class ShowupWebrtcDownload extends AbstractDownload {
@Override @Override
public void onMessage(WebSocket webSocket, ByteString bytes) { public void onMessage(WebSocket webSocket, ByteString bytes) {
super.onMessage(webSocket, bytes); super.onMessage(webSocket, bytes);
timeOfLastTransfer = Instant.now();
if (bytes == null) { if (bytes == null) {
return; return;
} }
@ -105,7 +110,7 @@ public class ShowupWebrtcDownload extends AbstractDownload {
if (t instanceof EOFException) { if (t instanceof EOFException) {
LOG.info("End of stream detected for model {}", model); LOG.info("End of stream detected for model {}", model);
} else { } else {
LOG.error("onFailure {} {}", webSocket, response, t); LOG.error("Websocket failure for model {} {} {}", model, response, t);
} }
if (response != null) { if (response != null) {
response.close(); response.close();
@ -115,13 +120,13 @@ public class ShowupWebrtcDownload extends AbstractDownload {
@Override @Override
public void onClosing(WebSocket webSocket, int code, String reason) { public void onClosing(WebSocket webSocket, int code, String reason) {
super.onClosing(webSocket, code, reason); super.onClosing(webSocket, code, reason);
LOG.trace("onClosing {} {} {}", webSocket, code, reason); LOG.trace("Websocket closing for model {} {} {}", model, code, reason);
} }
@Override @Override
public void onClosed(WebSocket webSocket, int code, String reason) { public void onClosed(WebSocket webSocket, int code, String reason) {
super.onClosed(webSocket, code, reason); super.onClosed(webSocket, code, reason);
LOG.debug("onClosed {} {} {}", webSocket, code, reason); LOG.debug("Websocket closed for model {} {} {}", model, code, reason);
stop(); stop();
} }
}); });
@ -132,12 +137,8 @@ public class ShowupWebrtcDownload extends AbstractDownload {
public void stop() { public void stop() {
running = false; running = false;
if (ws != null) { if (ws != null) {
boolean closed = ws.close(1000, ""); ws.close(1000, "");
if (closed) { ws = null;
ws = null;
} else {
LOG.error("websocket.close() returned false");
}
} }
} }
@ -188,15 +189,20 @@ public class ShowupWebrtcDownload extends AbstractDownload {
@Override @Override
public Download call() throws Exception { public Download call() throws Exception {
LOG.debug("ShowupWebrtcDownload.call()");
if (splittingStrategy.splitNecessary(this)) { if (splittingStrategy.splitNecessary(this)) {
stop(); stop();
rescheduleTime = Instant.now(); rescheduleTime = Instant.now();
} else { } else {
rescheduleTime = Instant.now().plusSeconds(Config.getInstance().getSettings().onlineCheckIntervalInSecs); rescheduleTime = Instant.now().plusSeconds(5);
} }
if (!model.isOnline(true)) { if (!model.isOnline(true)) {
stop(); stop();
} }
if (Duration.between(timeOfLastTransfer, Instant.now()).getSeconds() > MAX_SECONDS_WITHOUT_TRANSFER) {
LOG.info("No video data received for {} seconds. Stopping recording for model {}", MAX_SECONDS_WITHOUT_TRANSFER, model);
stop();
}
return this; return this;
} }