From 06d1630375bad3cda68a40b2b8fc271e0884b5a3 Mon Sep 17 00:00:00 2001 From: 0xboobface <0xboobface@gmail.com> Date: Wed, 11 Jul 2018 21:25:50 +0200 Subject: [PATCH] Use okhttp for all http connections --- .../ctbrec/recorder/download/HlsDownload.java | 33 ++++++++++++------- src/main/resources/logback.xml | 2 +- 2 files changed, 23 insertions(+), 12 deletions(-) diff --git a/src/main/java/ctbrec/recorder/download/HlsDownload.java b/src/main/java/ctbrec/recorder/download/HlsDownload.java index 23b7366c..339973ca 100644 --- a/src/main/java/ctbrec/recorder/download/HlsDownload.java +++ b/src/main/java/ctbrec/recorder/download/HlsDownload.java @@ -38,6 +38,8 @@ import ctbrec.HttpClient; import ctbrec.Model; import ctbrec.recorder.Chaturbate; import ctbrec.recorder.StreamInfo; +import okhttp3.Request; +import okhttp3.Response; public class HlsDownload implements Download { @@ -82,7 +84,7 @@ public class HlsDownload implements Download { for (int i = nextSegment; i < lsp.seq; i++) { URL segmentUrl = new URL(first.replaceAll(Integer.toString(seq), Integer.toString(i))); LOG.debug("Reloading segment {} for model {}", i, model.getName()); - threadPool.submit(new SegmentDownload(segmentUrl, downloadDir)); + threadPool.submit(new SegmentDownload(segmentUrl, downloadDir, client)); } // TODO switch to a lower bitrate/resolution ?!? } @@ -92,7 +94,7 @@ public class HlsDownload implements Download { skip--; } else { URL segmentUrl = new URL(segment); - threadPool.submit(new SegmentDownload(segmentUrl, downloadDir)); + threadPool.submit(new SegmentDownload(segmentUrl, downloadDir, client)); //new SegmentDownload(segment, downloadDir).call(); } } @@ -142,7 +144,9 @@ public class HlsDownload implements Download { private LiveStreamingPlaylist parseSegments(String segments) throws IOException, ParseException, PlaylistException { URL segmentsUrl = new URL(segments); - InputStream inputStream = segmentsUrl.openStream(); + Request request = new Request.Builder().url(segmentsUrl).addHeader("connection", "keep-alive").build(); + Response response = client.execute(request); + InputStream inputStream = response.body().byteStream(); PlaylistParser parser = new PlaylistParser(inputStream, Format.EXT_M3U, Encoding.UTF_8); Playlist playlist = parser.parse(); if(playlist.hasMediaPlaylist()) { @@ -168,8 +172,10 @@ public class HlsDownload implements Download { } private String parseMaster(String url, int streamUrlIndex) throws IOException, ParseException, PlaylistException { - URL masterUrl = new URL(url); - InputStream inputStream = masterUrl.openStream(); + Request request = new Request.Builder().url(url).addHeader("connection", "keep-alive").build(); + Response response = client.execute(request); + InputStream inputStream = response.body().byteStream(); + PlaylistParser parser = new PlaylistParser(inputStream, Format.EXT_M3U, Encoding.UTF_8); Playlist playlist = parser.parse(); if(playlist.hasMasterPlaylist()) { @@ -182,9 +188,9 @@ public class HlsDownload implements Download { } String uri = bestQuality.getUri(); if(!uri.startsWith("http")) { - String _masterUrl = masterUrl.toString(); - _masterUrl = _masterUrl.substring(0, _masterUrl.lastIndexOf('/') + 1); - String segmentUri = _masterUrl + uri; + String masterUrl = url; + String baseUri = masterUrl.substring(0, masterUrl.lastIndexOf('/') + 1); + String segmentUri = baseUri + uri; return segmentUri; } } @@ -202,9 +208,11 @@ public class HlsDownload implements Download { private static class SegmentDownload implements Callable { private URL url; private Path file; + private HttpClient client; - public SegmentDownload(URL url, Path dir) { + public SegmentDownload(URL url, Path dir, HttpClient client) { this.url = url; + this.client = client; File path = new File(url.getPath()); file = FileSystems.getDefault().getPath(dir.toString(), path.getName()); } @@ -213,8 +221,11 @@ public class HlsDownload implements Download { public Boolean call() throws Exception { LOG.trace("Downloading segment to " + file); for (int i = 0; i < 3; i++) { - try( FileOutputStream fos = new FileOutputStream(file.toFile()); - InputStream in = url.openStream()) + Request request = new Request.Builder().url(url).addHeader("connection", "keep-alive").build(); + Response response = client.execute(request); + try ( + FileOutputStream fos = new FileOutputStream(file.toFile()); + InputStream in = response.body().byteStream()) { byte[] b = new byte[1024 * 100]; int length = -1; diff --git a/src/main/resources/logback.xml b/src/main/resources/logback.xml index 3ecd8ee2..5838b223 100644 --- a/src/main/resources/logback.xml +++ b/src/main/resources/logback.xml @@ -29,7 +29,7 @@ --> - +