Start websocket connection in call instead of init
This commit is contained in:
parent
abffa14f8d
commit
8137444389
|
@ -1,20 +1,5 @@
|
|||
package ctbrec.sites.showup;
|
||||
|
||||
import static ctbrec.io.HttpConstants.*;
|
||||
|
||||
import java.io.EOFException;
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.time.Duration;
|
||||
import java.time.Instant;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import ctbrec.Config;
|
||||
import ctbrec.Model;
|
||||
import ctbrec.Recording;
|
||||
|
@ -26,18 +11,34 @@ import okhttp3.Response;
|
|||
import okhttp3.WebSocket;
|
||||
import okhttp3.WebSocketListener;
|
||||
import okio.ByteString;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.EOFException;
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.time.Duration;
|
||||
import java.time.Instant;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import static ctbrec.io.HttpConstants.*;
|
||||
|
||||
public class ShowupWebrtcDownload extends AbstractDownload {
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(ShowupWebrtcDownload.class);
|
||||
private static final int MAX_SECONDS_WITHOUT_TRANSFER = 20;
|
||||
|
||||
private transient WebSocket ws;
|
||||
private transient HttpClient httpClient;
|
||||
private transient FileOutputStream fout;
|
||||
private transient Instant timeOfLastTransfer = Instant.MAX;
|
||||
private final HttpClient httpClient;
|
||||
private WebSocket ws;
|
||||
private FileOutputStream fout;
|
||||
private Instant timeOfLastTransfer = Instant.MAX;
|
||||
|
||||
private volatile boolean running;
|
||||
private volatile boolean started;
|
||||
|
||||
|
||||
private File targetFile;
|
||||
|
||||
|
@ -54,83 +55,6 @@ public class ShowupWebrtcDownload extends AbstractDownload {
|
|||
splittingStrategy = initSplittingStrategy(config.getSettings());
|
||||
targetFile = config.getFileForRecording(model, "mp4", startTime);
|
||||
timeOfLastTransfer = Instant.now();
|
||||
|
||||
ShowupModel showupModel = (ShowupModel) model;
|
||||
Request request = new Request.Builder()
|
||||
.url(showupModel.getWebRtcUrl())
|
||||
.header(USER_AGENT, Config.getInstance().getSettings().httpUserAgent)
|
||||
.header(ACCEPT, "*/*")
|
||||
.header(ACCEPT_LANGUAGE, "pl")
|
||||
.header(ORIGIN, Showup.BASE_URL)
|
||||
.build();
|
||||
|
||||
running = true;
|
||||
LOG.debug("Opening webrtc connection {}", request.url());
|
||||
ws = httpClient.newWebSocket(request, new WebSocketListener() {
|
||||
@Override
|
||||
public void onOpen(WebSocket webSocket, Response response) {
|
||||
super.onOpen(webSocket, response);
|
||||
LOG.trace("onOpen {} {}", webSocket, response);
|
||||
response.close();
|
||||
try {
|
||||
LOG.debug("Recording video stream to {}", targetFile);
|
||||
Files.createDirectories(targetFile.getParentFile().toPath());
|
||||
fout = new FileOutputStream(targetFile);
|
||||
} catch (Exception e) {
|
||||
LOG.error("Couldn't open file {} to save the video stream", targetFile, e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onMessage(WebSocket webSocket, ByteString bytes) {
|
||||
super.onMessage(webSocket, bytes);
|
||||
timeOfLastTransfer = Instant.now();
|
||||
if (bytes == null) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
fout.write(bytes.toByteArray());
|
||||
} catch (IOException e) {
|
||||
if (running) {
|
||||
LOG.error("Couldn't write video stream to file", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onMessage(WebSocket webSocket, String text) {
|
||||
super.onMessage(webSocket, text);
|
||||
LOG.trace("onMessageT {} {}", webSocket, text);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFailure(WebSocket webSocket, Throwable t, Response response) {
|
||||
super.onFailure(webSocket, t, response);
|
||||
stop();
|
||||
if (t instanceof EOFException) {
|
||||
LOG.info("End of stream detected for model {}", model);
|
||||
} else {
|
||||
LOG.error("Websocket failure for model {} {} {}", model, response, t);
|
||||
}
|
||||
if (response != null) {
|
||||
response.close();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onClosing(WebSocket webSocket, int code, String reason) {
|
||||
super.onClosing(webSocket, code, reason);
|
||||
LOG.trace("Websocket closing for model {} {} {}", model, code, reason);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onClosed(WebSocket webSocket, int code, String reason) {
|
||||
super.onClosed(webSocket, code, reason);
|
||||
LOG.debug("Websocket closed for model {} {} {}", model, code, reason);
|
||||
stop();
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -189,6 +113,11 @@ public class ShowupWebrtcDownload extends AbstractDownload {
|
|||
|
||||
@Override
|
||||
public Download call() throws Exception {
|
||||
if (!started) {
|
||||
started = true;
|
||||
startDownload();
|
||||
}
|
||||
|
||||
if (splittingStrategy.splitNecessary(this)) {
|
||||
stop();
|
||||
rescheduleTime = Instant.now();
|
||||
|
@ -205,4 +134,80 @@ public class ShowupWebrtcDownload extends AbstractDownload {
|
|||
return this;
|
||||
}
|
||||
|
||||
private void startDownload() throws IOException {
|
||||
ShowupModel showupModel = (ShowupModel) model;
|
||||
Request request = new Request.Builder()
|
||||
.url(showupModel.getWebRtcUrl())
|
||||
.header(USER_AGENT, Config.getInstance().getSettings().httpUserAgent)
|
||||
.header(ACCEPT, "*/*")
|
||||
.header(ACCEPT_LANGUAGE, "pl")
|
||||
.header(ORIGIN, Showup.BASE_URL)
|
||||
.build();
|
||||
|
||||
running = true;
|
||||
LOG.debug("Opening webrtc connection {}", request.url());
|
||||
ws = httpClient.newWebSocket(request, new WebSocketListener() {
|
||||
@Override
|
||||
public void onOpen(WebSocket webSocket, Response response) {
|
||||
super.onOpen(webSocket, response);
|
||||
LOG.trace("onOpen {} {}", webSocket, response);
|
||||
response.close();
|
||||
try {
|
||||
LOG.debug("Recording video stream to {}", targetFile);
|
||||
Files.createDirectories(targetFile.getParentFile().toPath());
|
||||
fout = new FileOutputStream(targetFile);
|
||||
} catch (Exception e) {
|
||||
LOG.error("Couldn't open file {} to save the video stream", targetFile, e);
|
||||
stop();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onMessage(WebSocket webSocket, ByteString bytes) {
|
||||
super.onMessage(webSocket, bytes);
|
||||
timeOfLastTransfer = Instant.now();
|
||||
try {
|
||||
fout.write(bytes.toByteArray());
|
||||
} catch (IOException e) {
|
||||
if (running) {
|
||||
LOG.error("Couldn't write video stream to file", e);
|
||||
stop();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onMessage(WebSocket webSocket, String text) {
|
||||
super.onMessage(webSocket, text);
|
||||
LOG.trace("onMessageT {} {}", webSocket, text);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFailure(WebSocket webSocket, Throwable t, Response response) {
|
||||
super.onFailure(webSocket, t, response);
|
||||
stop();
|
||||
if (t instanceof EOFException) {
|
||||
LOG.info("End of stream detected for model {}", model);
|
||||
} else {
|
||||
LOG.error("Websocket failure for model {} {} {}", model, response, t);
|
||||
}
|
||||
if (response != null) {
|
||||
response.close();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onClosing(WebSocket webSocket, int code, String reason) {
|
||||
super.onClosing(webSocket, code, reason);
|
||||
LOG.trace("Websocket closing for model {} {} {}", model, code, reason);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onClosed(WebSocket webSocket, int code, String reason) {
|
||||
super.onClosed(webSocket, code, reason);
|
||||
LOG.debug("Websocket closed for model {} {} {}", model, code, reason);
|
||||
stop();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue