diff --git a/common/src/main/java/ctbrec/recorder/download/AbstractDownload.java b/common/src/main/java/ctbrec/recorder/download/AbstractDownload.java index 2dbe71e4..b90541ce 100644 --- a/common/src/main/java/ctbrec/recorder/download/AbstractDownload.java +++ b/common/src/main/java/ctbrec/recorder/download/AbstractDownload.java @@ -1,11 +1,36 @@ package ctbrec.recorder.download; +import java.io.IOException; import java.time.Instant; +import java.util.concurrent.ExecutorService; + +import ctbrec.Config; +import ctbrec.Model; +import ctbrec.Settings; +import ctbrec.UnknownModel; +import ctbrec.recorder.download.hls.CombinedSplittingStrategy; +import ctbrec.recorder.download.hls.NoopSplittingStrategy; +import ctbrec.recorder.download.hls.SizeSplittingStrategy; +import ctbrec.recorder.download.hls.TimeSplittingStrategy; public abstract class AbstractDownload implements Download { protected Instant startTime; protected Instant rescheduleTime = Instant.now(); + protected Model model = new UnknownModel(); + + protected transient Config config; + protected transient SplittingStrategy splittingStrategy; + protected transient ExecutorService downloadExecutor; + + @Override + public void init(Config config, Model model, Instant startTime, ExecutorService executorService) throws IOException { + this.config = config; + this.model = model; + this.startTime = startTime; + this.downloadExecutor = executorService; + splittingStrategy = initSplittingStrategy(config.getSettings()); + } @Override public Instant getStartTime() { @@ -16,4 +41,27 @@ public abstract class AbstractDownload implements Download { public Instant getRescheduleTime() { return rescheduleTime; } + + protected SplittingStrategy initSplittingStrategy(Settings settings) { + SplittingStrategy strategy; + switch (settings.splitStrategy) { + case TIME: + strategy = new TimeSplittingStrategy(); + break; + case SIZE: + strategy = new SizeSplittingStrategy(); + break; + case TIME_OR_SIZE: + SplittingStrategy timeSplittingStrategy = new TimeSplittingStrategy(); + SplittingStrategy sizeSplittingStrategy = new SizeSplittingStrategy(); + strategy = new CombinedSplittingStrategy(timeSplittingStrategy, sizeSplittingStrategy); + break; + case DONT: + default: + strategy = new NoopSplittingStrategy(); + break; + } + strategy.init(settings); + return strategy; + } } diff --git a/common/src/main/java/ctbrec/recorder/download/dash/DashDownload.java b/common/src/main/java/ctbrec/recorder/download/dash/DashDownload.java index e71708b3..9a8784e4 100644 --- a/common/src/main/java/ctbrec/recorder/download/dash/DashDownload.java +++ b/common/src/main/java/ctbrec/recorder/download/dash/DashDownload.java @@ -12,9 +12,7 @@ import java.math.BigInteger; import java.net.URL; import java.nio.file.Path; import java.text.DecimalFormat; -import java.time.Duration; import java.time.Instant; -import java.time.ZonedDateTime; import java.util.ArrayList; import java.util.List; import java.util.Locale; @@ -59,12 +57,9 @@ public class DashDownload extends AbstractDownload { private BigInteger lastVideoTimestamp = BigInteger.ZERO; private transient AtomicBoolean downloadFinished = new AtomicBoolean(false); private transient HttpClient httpClient; - private transient Config config; - private Model model; private transient Path downloadDir; private String manifestUrl; private boolean running = false; - private ZonedDateTime splitRecStartTime; private File targetFile; private File finalFile; @@ -244,7 +239,6 @@ public class DashDownload extends AbstractDownload { try { Thread.currentThread().setName("Download " + model.getName()); running = true; - splitRecStartTime = ZonedDateTime.now(); JAXBContext jc = JAXBContext.newInstance(MPDtype.class.getPackage().getName()); Unmarshaller u = jc.createUnmarshaller(); while (running && !Thread.currentThread().isInterrupted()) { @@ -279,15 +273,12 @@ public class DashDownload extends AbstractDownload { } private boolean splitRecording() { - if (config.getSettings().splitRecordings > 0) { - Duration recordingDuration = Duration.between(splitRecStartTime, ZonedDateTime.now()); - long seconds = recordingDuration.getSeconds(); - if (seconds >= config.getSettings().splitRecordings) { - internalStop(); - return true; - } + if (splittingStrategy.splitNecessary(this)) { + internalStop(); + return true; + } else { + return false; } - return false; } private void downloadManifestAndItsSegments(Unmarshaller u) throws IOException, JAXBException, ExecutionException, InterruptedException { diff --git a/common/src/main/java/ctbrec/recorder/download/hls/AbstractHlsDownload.java b/common/src/main/java/ctbrec/recorder/download/hls/AbstractHlsDownload.java index 3ccad859..78521347 100644 --- a/common/src/main/java/ctbrec/recorder/download/hls/AbstractHlsDownload.java +++ b/common/src/main/java/ctbrec/recorder/download/hls/AbstractHlsDownload.java @@ -25,7 +25,6 @@ import java.util.Map.Entry; import java.util.Optional; import java.util.concurrent.CompletableFuture; import java.util.concurrent.ExecutionException; -import java.util.concurrent.ExecutorService; import java.util.stream.Collectors; import javax.xml.bind.JAXBException; @@ -47,8 +46,6 @@ import com.iheartradio.m3u8.data.TrackData; import ctbrec.Config; import ctbrec.Model; import ctbrec.Model.State; -import ctbrec.Settings; -import ctbrec.UnknownModel; import ctbrec.io.BandwidthMeter; import ctbrec.io.HttpClient; import ctbrec.io.HttpConstants; @@ -57,7 +54,6 @@ import ctbrec.io.MissedSegmentsStatistics; import ctbrec.recorder.PlaylistGenerator.InvalidPlaylistException; import ctbrec.recorder.download.AbstractDownload; import ctbrec.recorder.download.HttpHeaderFactory; -import ctbrec.recorder.download.SplittingStrategy; import ctbrec.recorder.download.StreamSource; import ctbrec.sites.Site; import okhttp3.Request; @@ -72,11 +68,10 @@ public abstract class AbstractHlsDownload extends AbstractDownload { private transient NumberFormat nf = new DecimalFormat("000000"); private transient int playlistEmptyCount = 0; private transient int segmentCounter = 1; - protected transient Config config; + protected transient HttpClient client; - protected transient ExecutorService downloadExecutor; protected transient volatile boolean running = true; - protected transient SplittingStrategy splittingStrategy; + protected transient int lastSegmentNumber = 0; protected transient int nextSegmentNumber = 0; protected transient String segmentPlaylistUrl; @@ -89,8 +84,6 @@ public abstract class AbstractHlsDownload extends AbstractDownload { private transient int consecutivePlaylistTimeouts = 0; private transient int consecutivePlaylistErrors = 0; - protected Model model = new UnknownModel(); - protected AbstractHlsDownload(HttpClient client) { this.client = client; } @@ -99,15 +92,6 @@ public abstract class AbstractHlsDownload extends AbstractDownload { protected void segmentDownloadFinished(SegmentDownload segmentDownload) {} protected abstract void internalStop(); - @Override - public void init(Config config, Model model, Instant startTime, ExecutorService executorService) throws IOException { - this.config = config; - this.model = model; - this.startTime = startTime; - this.downloadExecutor = executorService; - splittingStrategy = initSplittingStrategy(config.getSettings()); - } - @Override public AbstractHlsDownload call() throws Exception { try { @@ -329,29 +313,6 @@ public abstract class AbstractHlsDownload extends AbstractDownload { } } - protected SplittingStrategy initSplittingStrategy(Settings settings) { - SplittingStrategy strategy; - switch (settings.splitStrategy) { - case TIME: - strategy = new TimeSplittingStrategy(); - break; - case SIZE: - strategy = new SizeSplittingStrategy(); - break; - case TIME_OR_SIZE: - SplittingStrategy timeSplittingStrategy = new TimeSplittingStrategy(); - SplittingStrategy sizeSplittingStrategy = new SizeSplittingStrategy(); - strategy = new CombinedSplittingStrategy(timeSplittingStrategy, sizeSplittingStrategy); - break; - case DONT: - default: - strategy = new NoopSplittingStrategy(); - break; - } - strategy.init(settings); - return strategy; - } - protected void enqueueNewSegments(SegmentPlaylist playlist, int nextSegmentNumber) throws IOException { int skip = nextSegmentNumber - playlist.seq; for (String segment : playlist.segments) { diff --git a/common/src/main/java/ctbrec/sites/showup/ShowupMergedDownload.java b/common/src/main/java/ctbrec/sites/showup/ShowupMergedDownload.java index d0e0ca15..205ee2bf 100644 --- a/common/src/main/java/ctbrec/sites/showup/ShowupMergedDownload.java +++ b/common/src/main/java/ctbrec/sites/showup/ShowupMergedDownload.java @@ -1,15 +1,10 @@ package ctbrec.sites.showup; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - import ctbrec.io.HttpClient; import ctbrec.recorder.download.hls.MergedFfmpegHlsDownload; public class ShowupMergedDownload extends MergedFfmpegHlsDownload { - private static final Logger LOG = LoggerFactory.getLogger(ShowupMergedDownload.class); - public ShowupMergedDownload(HttpClient client) { super(client); }