From cf6c86e0c6e7dec3481c3defe8a899f5006b8fec Mon Sep 17 00:00:00 2001 From: tom79 Date: Fri, 21 Feb 2020 09:27:22 +0100 Subject: [PATCH] Fix issue #403 --- .../android/activities/PeertubeActivity.java | 10 +++--- .../java/app/fedilab/android/client/API.java | 32 ++++++++++++------- .../android/client/Entities/Peertube.java | 30 ++++++++++++++--- .../fedilab/android/client/PeertubeAPI.java | 21 +++++++++--- 4 files changed, 69 insertions(+), 24 deletions(-) diff --git a/app/src/main/java/app/fedilab/android/activities/PeertubeActivity.java b/app/src/main/java/app/fedilab/android/activities/PeertubeActivity.java index 332f68767..ff73eeb6c 100644 --- a/app/src/main/java/app/fedilab/android/activities/PeertubeActivity.java +++ b/app/src/main/java/app/fedilab/android/activities/PeertubeActivity.java @@ -482,7 +482,7 @@ public class PeertubeActivity extends BaseActivity implements OnRetrievePeertube loader.setVisibility(View.GONE); return; } - if (apiResponse.getPeertubes() == null || apiResponse.getPeertubes().get(0) == null || apiResponse.getPeertubes().get(0).getFileUrl(null) == null) { + if (apiResponse.getPeertubes() == null || apiResponse.getPeertubes().get(0) == null || apiResponse.getPeertubes().get(0).getFileUrl(null, apiResponse.getPeertubes().get(0).isStreamService()) == null) { Toasty.error(getApplicationContext(), getString(R.string.toast_error), Toast.LENGTH_LONG).show(); loader.setVisibility(View.GONE); return; @@ -630,7 +630,7 @@ public class PeertubeActivity extends BaseActivity implements OnRetrievePeertube Util.getUserAgent(getApplicationContext(), "Mastalab"), null); ExtractorMediaSource videoSource = new ExtractorMediaSource.Factory(dataSourceFactory) - .createMediaSource(Uri.parse(apiResponse.getPeertubes().get(0).getFileUrl(null))); + .createMediaSource(Uri.parse(apiResponse.getPeertubes().get(0).getFileUrl(null, apiResponse.getPeertubes().get(0).isStreamService()))); player = ExoPlayerFactory.newSimpleInstance(PeertubeActivity.this); playerView.setPlayer(player); @@ -648,10 +648,10 @@ public class PeertubeActivity extends BaseActivity implements OnRetrievePeertube if (ContextCompat.checkSelfPermission(PeertubeActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED || ContextCompat.checkSelfPermission(PeertubeActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { ActivityCompat.requestPermissions(PeertubeActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, Helper.EXTERNAL_STORAGE_REQUEST_CODE); } else { - Helper.manageDownloads(PeertubeActivity.this, peertube.getFileDownloadUrl(null)); + Helper.manageDownloads(PeertubeActivity.this, peertube.getFileDownloadUrl(null, peertube.isStreamService())); } } else { - Helper.manageDownloads(PeertubeActivity.this, peertube.getFileDownloadUrl(null)); + Helper.manageDownloads(PeertubeActivity.this, peertube.getFileDownloadUrl(null, peertube.isStreamService())); } } }); @@ -846,7 +846,7 @@ public class PeertubeActivity extends BaseActivity implements OnRetrievePeertube Util.getUserAgent(getApplicationContext(), "Mastalab"), null); ExtractorMediaSource videoSource = new ExtractorMediaSource.Factory(dataSourceFactory) - .createMediaSource(Uri.parse(peertube.getFileUrl(res))); + .createMediaSource(Uri.parse(peertube.getFileUrl(res, peertube.isStreamService()))); player.prepare(videoSource); player.seekTo(0, position); player.setPlayWhenReady(true); diff --git a/app/src/main/java/app/fedilab/android/client/API.java b/app/src/main/java/app/fedilab/android/client/API.java index f3275db16..81a6e58cc 100644 --- a/app/src/main/java/app/fedilab/android/client/API.java +++ b/app/src/main/java/app/fedilab/android/client/API.java @@ -358,13 +358,27 @@ public class API { } catch (ParseException e) { e.printStackTrace(); } - JSONArray files = resobj.getJSONArray("files"); + ArrayList resolutions = new ArrayList<>(); - for (int j = 0; j < files.length(); j++) { - JSONObject attObj = files.getJSONObject(j); - resolutions.add(attObj.getJSONObject("resolution").get("id").toString()); + if( resobj.has("streamingPlaylists")){ + JSONArray files = resobj.getJSONArray("streamingPlaylists").getJSONObject(0).getJSONArray("files"); + + for (int j = 0; j < files.length(); j++) { + JSONObject attObj = files.getJSONObject(j); + resolutions.add(attObj.getJSONObject("resolution").get("id").toString()); + } + peertube.setResolution(resolutions); + peertube.setStreamService(true); + }else { + JSONArray files = resobj.getJSONArray("files"); + + for (int j = 0; j < files.length(); j++) { + JSONObject attObj = files.getJSONObject(j); + resolutions.add(attObj.getJSONObject("resolution").get("id").toString()); + } + peertube.setResolution(resolutions); + peertube.setStreamService(false); } - peertube.setResolution(resolutions); } catch (JSONException e) { e.printStackTrace(); } @@ -3356,17 +3370,13 @@ public class API { try { HttpsConnection httpsConnection = new HttpsConnection(context, this.instance); String response = httpsConnection.get(String.format("https://" + instance + "/api/v1/videos/%s", videoId), 10, null, null); + Helper.largeLog(response); JSONObject jsonObject = new JSONObject(response); peertube = parseSinglePeertube(context, instance, jsonObject); } catch (HttpsConnection.HttpsConnectionException e) { setError(e.getStatusCode(), e); - } catch (NoSuchAlgorithmException e) { e.printStackTrace(); - } catch (IOException e) { - e.printStackTrace(); - } catch (KeyManagementException e) { - e.printStackTrace(); - } catch (JSONException e) { + } catch (NoSuchAlgorithmException | IOException | KeyManagementException | JSONException e) { e.printStackTrace(); } List peertubes = new ArrayList<>(); diff --git a/app/src/main/java/app/fedilab/android/client/Entities/Peertube.java b/app/src/main/java/app/fedilab/android/client/Entities/Peertube.java index 452777327..1d81565ec 100644 --- a/app/src/main/java/app/fedilab/android/client/Entities/Peertube.java +++ b/app/src/main/java/app/fedilab/android/client/Entities/Peertube.java @@ -14,12 +14,15 @@ * see . */ package app.fedilab.android.client.Entities; + import org.json.JSONObject; import java.util.Date; import java.util.HashMap; import java.util.List; +import app.fedilab.android.helper.Helper; + /** * Created by Thomas on 29/09/2018. * Manage how to videos @@ -56,6 +59,7 @@ public class Peertube { private String headerType = null;//For overview timeline private String headerTypeValue = null;//For overview timeline private JSONObject cache; + private boolean streamService; public Peertube() { } @@ -174,12 +178,17 @@ public class Peertube { this.account = account; } - public String getFileUrl(String resolution) { + public String getFileUrl(String resolution, boolean streamService) { if (resolution == null) resolution = this.getResolution().get(0); if (resolution == null) return null; - return "https://" + this.host + "/static/webseed/" + getUuid() + "-" + resolution + ".mp4"; + if( streamService) { + return "https://" + this.host + "/static/streaming-playlists/hls/" + getUuid() + "/" + getUuid() + "-" + resolution + "-fragmented.mp4"; + }else{ + return "https://" + this.host + "/static/webseed/" + getUuid() + "-" + resolution + ".mp4"; + + } } @@ -201,12 +210,17 @@ public class Peertube { } - public String getFileDownloadUrl(String resolution) { + public String getFileDownloadUrl(String resolution, boolean streamService) { if (resolution == null) resolution = this.getResolution().get(0); if (resolution == null) return null; - return "https://" + this.host + "/download/videos/" + getUuid() + "-" + resolution + ".mp4"; + if( streamService) { + return "https://" + this.host + "/download/streaming-playlists/hls/videos/" + getUuid() + "/" + getUuid() + "-" + resolution + "-fragmented.mp4"; + }else{ + return "https://" + this.host + "/download/videos/" + getUuid() + "-" + resolution + ".mp4"; + } + } public List getResolution() { @@ -338,4 +352,12 @@ public class Peertube { public void setHeaderTypeValue(String headerTypeValue) { this.headerTypeValue = headerTypeValue; } + + public boolean isStreamService() { + return streamService; + } + + public void setStreamService(boolean streamService) { + this.streamService = streamService; + } } diff --git a/app/src/main/java/app/fedilab/android/client/PeertubeAPI.java b/app/src/main/java/app/fedilab/android/client/PeertubeAPI.java index 38e446ed9..9caa8a986 100644 --- a/app/src/main/java/app/fedilab/android/client/PeertubeAPI.java +++ b/app/src/main/java/app/fedilab/android/client/PeertubeAPI.java @@ -364,11 +364,24 @@ public class PeertubeAPI { } catch (ParseException e) { e.printStackTrace(); } - JSONArray files = resobj.getJSONArray("files"); ArrayList resolutions = new ArrayList<>(); - for (int j = 0; j < files.length(); j++) { - JSONObject attObj = files.getJSONObject(j); - resolutions.add(attObj.getJSONObject("resolution").get("id").toString()); + if( resobj.has("streamingPlaylists")){ + JSONArray files = resobj.getJSONArray("streamingPlaylists").getJSONObject(0).getJSONArray("files"); + + for (int j = 0; j < files.length(); j++) { + JSONObject attObj = files.getJSONObject(j); + resolutions.add(attObj.getJSONObject("resolution").get("id").toString()); + } + peertube.setResolution(resolutions); + peertube.setStreamService(true); + }else { + JSONArray files = resobj.getJSONArray("files"); + for (int j = 0; j < files.length(); j++) { + JSONObject attObj = files.getJSONObject(j); + resolutions.add(attObj.getJSONObject("resolution").get("id").toString()); + } + peertube.setResolution(resolutions); + peertube.setStreamService(false); } try { LinkedHashMap langue = new LinkedHashMap<>();