From 22d32bcc64018e83219e1715efd11ef6622ec32c Mon Sep 17 00:00:00 2001 From: 0xboobface <0xboobface@gmail.com> Date: Tue, 4 Sep 2018 16:50:38 +0200 Subject: [PATCH] Add possibility to switch the stream resolution for a recording Add the possibility to switch the stream resolution for a model, which already is recorded by the recorder. This makes it more convenient to switch the resolution, if you already have a bunch of models in the recorder list. --- .../java/ctbrec/recorder/LocalRecorder.java | 9 ++++ src/main/java/ctbrec/recorder/Recorder.java | 2 + .../java/ctbrec/recorder/RemoteRecorder.java | 5 +++ .../recorder/server/RecorderServlet.java | 5 +++ .../java/ctbrec/ui/RecordedModelsTab.java | 42 ++++++++++++++++++- 5 files changed, 62 insertions(+), 1 deletion(-) diff --git a/src/main/java/ctbrec/recorder/LocalRecorder.java b/src/main/java/ctbrec/recorder/LocalRecorder.java index 543498ae..158a2af1 100644 --- a/src/main/java/ctbrec/recorder/LocalRecorder.java +++ b/src/main/java/ctbrec/recorder/LocalRecorder.java @@ -9,6 +9,8 @@ import java.io.File; import java.io.IOException; import java.nio.file.Files; import java.nio.file.StandardCopyOption; +import java.security.InvalidKeyException; +import java.security.NoSuchAlgorithmException; import java.text.SimpleDateFormat; import java.time.Instant; import java.util.ArrayList; @@ -614,4 +616,11 @@ public class LocalRecorder implements Recorder { } return mergedFile; } + + @Override + public void switchStreamSource(Model model) throws IOException, InvalidKeyException, NoSuchAlgorithmException, IllegalStateException { + LOG.debug("Switching stream source to index {} for model {}", model.getStreamUrlIndex(), model.getName()); + stopRecordingProcess(model); + tryRestartRecording(model); + } } diff --git a/src/main/java/ctbrec/recorder/Recorder.java b/src/main/java/ctbrec/recorder/Recorder.java index e4b0a461..e94eba58 100644 --- a/src/main/java/ctbrec/recorder/Recorder.java +++ b/src/main/java/ctbrec/recorder/Recorder.java @@ -14,6 +14,8 @@ public interface Recorder { public void stopRecording(Model model) throws IOException, InvalidKeyException, NoSuchAlgorithmException, IllegalStateException; + public void switchStreamSource(Model model) throws IOException, InvalidKeyException, NoSuchAlgorithmException, IllegalStateException; + /** * Returns, if a model is in the list of models to record. This does not reflect, if there currently is a recording running. The model might be offline * aswell. diff --git a/src/main/java/ctbrec/recorder/RemoteRecorder.java b/src/main/java/ctbrec/recorder/RemoteRecorder.java index b0a9892a..03cb8921 100644 --- a/src/main/java/ctbrec/recorder/RemoteRecorder.java +++ b/src/main/java/ctbrec/recorder/RemoteRecorder.java @@ -264,4 +264,9 @@ public class RemoteRecorder implements Recorder { this.model = model; } } + + @Override + public void switchStreamSource(Model model) throws IOException, InvalidKeyException, NoSuchAlgorithmException, IllegalStateException { + sendRequest("switch", model); + } } diff --git a/src/main/java/ctbrec/recorder/server/RecorderServlet.java b/src/main/java/ctbrec/recorder/server/RecorderServlet.java index 976c5b68..88eadb68 100644 --- a/src/main/java/ctbrec/recorder/server/RecorderServlet.java +++ b/src/main/java/ctbrec/recorder/server/RecorderServlet.java @@ -104,6 +104,11 @@ public class RecorderServlet extends AbstractCtbrecServlet { resp.getWriter().write(recAdapter.toJson(rec)); resp.getWriter().write("]}"); break; + case "switch": + recorder.switchStreamSource(request.model); + response = "{\"status\": \"success\", \"msg\": \"Resolution switched\"}"; + resp.getWriter().write(response); + break; default: resp.setStatus(SC_BAD_REQUEST); response = "{\"status\": \"error\", \"msg\": \"Unknown action\"}"; diff --git a/src/main/java/ctbrec/ui/RecordedModelsTab.java b/src/main/java/ctbrec/ui/RecordedModelsTab.java index d41b5f03..aa3d0415 100644 --- a/src/main/java/ctbrec/ui/RecordedModelsTab.java +++ b/src/main/java/ctbrec/ui/RecordedModelsTab.java @@ -9,10 +9,12 @@ import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.ThreadFactory; import java.util.concurrent.TimeUnit; +import java.util.function.Function; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import ctbrec.HttpClient; import ctbrec.Model; import ctbrec.recorder.Recorder; import javafx.application.Platform; @@ -217,8 +219,46 @@ public class RecordedModelsTab extends Tab implements TabSelectionListener { openInBrowser.setOnAction((e) -> Launcher.open(table.getSelectionModel().getSelectedItem().getUrl())); MenuItem openInPlayer = new MenuItem("Open in Player"); openInPlayer.setOnAction((e) -> Player.play(table.getSelectionModel().getSelectedItem().getUrl())); + MenuItem switchStreamSource = new MenuItem("Switch resolution"); + switchStreamSource.setOnAction((e) -> switchStreamSource(table.getSelectionModel().getSelectedItem())); - return new ContextMenu(stop, copyUrl, openInBrowser, openInPlayer); + return new ContextMenu(stop, copyUrl, openInBrowser, switchStreamSource); + } + + private void switchStreamSource(JavaFxModel fxModel) { + if(!fxModel.isOnline()) { + Alert alert = new AutosizeAlert(Alert.AlertType.INFORMATION); + alert.setTitle("Switch resolution"); + alert.setHeaderText("Couldn't switch stream resolution"); + alert.setContentText("The resolution can only be changed, when the model is online"); + alert.showAndWait(); + return; + } + + HttpClient client = HttpClient.getInstance(); + Function onSuccess = (m) -> { + try { + recorder.switchStreamSource(m); + } catch (InvalidKeyException | NoSuchAlgorithmException | IllegalStateException | IOException e) { + LOG.error("Error while switching stream resolution", e); + showStreamSwitchErrorDialog(e); + } + return null; + }; + Function onFail = (t) -> { + LOG.error("Error while switching stream resolution", t); + showStreamSwitchErrorDialog(t); + return null; + }; + StreamSourceSelectionDialog.show(fxModel.getDelegate(), client, onSuccess, onFail); + } + + private void showStreamSwitchErrorDialog(Throwable throwable) { + Alert alert = new AutosizeAlert(Alert.AlertType.ERROR); + alert.setTitle("Error"); + alert.setHeaderText("Couldn't switch stream resolution"); + alert.setContentText("Error while switching stream resolution: " + throwable.getLocalizedMessage()); + alert.showAndWait(); } private void stopAction() {