forked from j62/ctbrec
1
0
Fork 0

Make sure to not download segments multiple times

For some reason streamate playlists can go back in time. This change
makes sure, that we don't download segments multiple times as a
consequence of the time travelling ;)
This commit is contained in:
0xboobface 2018-12-17 19:04:57 +01:00
parent 75fedfcddd
commit ad71f0cf11
3 changed files with 14 additions and 6 deletions

View File

@ -9,6 +9,7 @@
than this value, get deleted automatically. than this value, get deleted automatically.
* Double-click in Recording tab starts the player * Double-click in Recording tab starts the player
* Fix: BongaCams friends tab not working * Fix: BongaCams friends tab not working
* Fix: BongaCams search fails with JSON exception
* Fix: In some cases MFC models got confused * Fix: In some cases MFC models got confused
1.15.0 1.15.0

View File

@ -52,6 +52,9 @@ public abstract class AbstractHlsDownload implements Download {
Request request = new Request.Builder().url(segmentsUrl).addHeader("connection", "keep-alive").build(); Request request = new Request.Builder().url(segmentsUrl).addHeader("connection", "keep-alive").build();
try(Response response = client.execute(request)) { try(Response response = client.execute(request)) {
if(response.isSuccessful()) { if(response.isSuccessful()) {
// String body = response.body().string();
// InputStream inputStream = new ByteArrayInputStream(body.getBytes("utf-8"));
// LOG.debug("Segments {}", body);
InputStream inputStream = response.body().byteStream(); InputStream inputStream = response.body().byteStream();
PlaylistParser parser = new PlaylistParser(inputStream, Format.EXT_M3U, Encoding.UTF_8, ParsingMode.LENIENT); PlaylistParser parser = new PlaylistParser(inputStream, Format.EXT_M3U, Encoding.UTF_8, ParsingMode.LENIENT);
Playlist playlist = parser.parse(); Playlist playlist = parser.parse();

View File

@ -76,13 +76,13 @@ public class HlsDownload extends AbstractHlsDownload {
} }
int lastSegment = 0; int lastSegment = 0;
int nextSegment = 0; int nextSegment = 0;
boolean sleep = true; // this enables sleeping between playlist requests. once we miss a segment, this is set to false, so that no sleeping happens anymore int waitFactor = 1;
while(running) { while(running) {
SegmentPlaylist lsp = getNextSegments(segments); SegmentPlaylist lsp = getNextSegments(segments);
if(nextSegment > 0 && lsp.seq > nextSegment) { if(nextSegment > 0 && lsp.seq > nextSegment) {
// TODO switch to a lower bitrate/resolution ?!? // TODO switch to a lower bitrate/resolution ?!?
LOG.warn("Missed segments {} < {} in download for {}", nextSegment, lsp.seq, model); waitFactor *= 2;
sleep = false; LOG.warn("Missed segments {} < {} in download for {} - setting wait factor to 1/{}", nextSegment, lsp.seq, model, waitFactor);
} }
int skip = nextSegment - lsp.seq; int skip = nextSegment - lsp.seq;
for (String segment : lsp.segments) { for (String segment : lsp.segments) {
@ -97,9 +97,9 @@ public class HlsDownload extends AbstractHlsDownload {
} }
long wait = 0; long wait = 0;
if(sleep && lastSegment == lsp.seq) { if(lastSegment == lsp.seq) {
// playlist didn't change -> wait for at least half the target duration // playlist didn't change -> wait for at least half the target duration
wait = (long) lsp.targetDuration * 1000 / 2; wait = (long) lsp.targetDuration * 1000 / waitFactor;
LOG.trace("Playlist didn't change... waiting for {}ms", wait); LOG.trace("Playlist didn't change... waiting for {}ms", wait);
} else { } else {
// playlist did change -> wait for at least last segment duration // playlist did change -> wait for at least last segment duration
@ -115,8 +115,12 @@ public class HlsDownload extends AbstractHlsDownload {
} }
} }
// this if check makes sure, that we don't decrease nextSegment. for some reason
// streamate playlists sometimes jump back. e.g. max sequence = 79 -> 80 -> 79
lastSegment = lsp.seq; lastSegment = lsp.seq;
nextSegment = lastSegment + lsp.segments.size(); if(lastSegment + lsp.segments.size() > nextSegment) {
nextSegment = lastSegment + lsp.segments.size();
}
} }
} else { } else {
throw new IOException("Couldn't determine segments uri"); throw new IOException("Couldn't determine segments uri");