forked from j62/ctbrec
1
0
Fork 0

Implement splitting for hlsdl downloads

This commit is contained in:
0xb00bface 2021-01-09 22:24:51 +01:00
parent 8e22112603
commit 1baa216150
3 changed files with 52 additions and 28 deletions

View File

@ -2,12 +2,42 @@ package ctbrec.recorder.download;
import java.time.Instant;
import ctbrec.Settings;
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 transient SplittingStrategy splittingStrategy;
@Override
public Instant getStartTime() {
return startTime;
}
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;
}
}

View File

@ -44,7 +44,6 @@ import com.iheartradio.m3u8.data.TrackData;
import ctbrec.Config;
import ctbrec.Model;
import ctbrec.Recording.State;
import ctbrec.Settings;
import ctbrec.UnknownModel;
import ctbrec.io.BandwidthMeter;
import ctbrec.io.HttpClient;
@ -52,7 +51,6 @@ import ctbrec.io.HttpException;
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;
@ -69,7 +67,6 @@ public abstract class AbstractHlsDownload extends AbstractDownload {
protected Model model = new UnknownModel();
protected transient LinkedBlockingQueue<Runnable> downloadQueue = new LinkedBlockingQueue<>(50);
protected transient ExecutorService downloadThreadPool = new ThreadPoolExecutor(0, 5, 20, TimeUnit.SECONDS, downloadQueue, createThreadFactory());
protected transient SplittingStrategy splittingStrategy;
protected State state = State.UNKNOWN;
private int playlistEmptyCount = 0;
@ -238,27 +235,4 @@ public abstract class AbstractHlsDownload extends AbstractDownload {
this.url = url;
}
}
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;
}
}

View File

@ -12,6 +12,7 @@ import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
@ -48,15 +49,34 @@ public class HlsdlDownload extends AbstractDownload {
this.model = model;
String fileSuffix = config.getSettings().ffmpegFileSuffix;
targetFile = config.getFileForRecording(model, fileSuffix, startTime);
// TODO splittingStrategy = initSplittingStrategy(config.getSettings());
splittingStrategy = initSplittingStrategy(config.getSettings());
}
@Override
public void start() throws IOException {
try {
running = true;
Thread.currentThread().setName("Download " + model.getName());
String threadName = "Download " + model.getName();
Thread.currentThread().setName(threadName);
Files.createDirectories(targetFile.getParentFile().toPath());
Thread splittingMonitor = new Thread(() -> {
try {
while (running) {
if (splittingStrategy.splitNecessary(HlsdlDownload.this)) {
LOG.debug("splitting strategy {} triggered split", splittingStrategy.getClass().getSimpleName());
stop();
}
TimeUnit.SECONDS.sleep(1);
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
});
splittingMonitor.setName(threadName + " splitter");
splittingMonitor.setDaemon(true);
splittingMonitor.start();
Hlsdl hlsdl = new Hlsdl.Builder()
.logOutput(config.getSettings().loghlsdlOutput)
.onStarted(p -> hlsdlProcess = p)