Basic playback of torrents

This commit is contained in:
Stefan Schueller 2018-03-11 02:24:06 +01:00
parent 231350f78f
commit 9378dba5a2
7 changed files with 222 additions and 55 deletions

View File

@ -38,12 +38,15 @@ dependencies {
implementation 'com.google.code.gson:gson:2.8.2' implementation 'com.google.code.gson:gson:2.8.2'
implementation 'org.webrtc:google-webrtc:1.0.+' // implementation 'org.webrtc:google-webrtc:1.0.+'
implementation 'com.android.support:design:27.1.0' implementation 'com.android.support:design:27.1.0'
implementation 'com.blackboardtheory:android-iconify-fontawesome:3.0.1-SNAPSHOT' implementation 'com.blackboardtheory:android-iconify-fontawesome:3.0.1-SNAPSHOT'
implementation 'com.github.TorrentStream:TorrentStream-Android:2.5.0' implementation 'com.github.TorrentStream:TorrentStream-Android:2.5.0'
implementation 'com.google.android.exoplayer:exoplayer:2.7.0'
// implementation "com.github.TorrentStream:TorrentStreamServer-Android:1.0.1"
// implementation 'com.devbrackets.android:exomedia:4.1.0'
implementation 'com.android.support:support-v4:27.1.0' implementation 'com.android.support:support-v4:27.1.0'
testImplementation 'junit:junit:4.12' testImplementation 'junit:junit:4.12'

View File

@ -8,6 +8,8 @@
<uses-permission android:name="android.permission.GET_ACCOUNTS" /> <uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.READ_PROFILE" /> <uses-permission android:name="android.permission.READ_PROFILE" />
<uses-permission android:name="android.permission.READ_CONTACTS" /> <uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application <application
android:allowBackup="true" android:allowBackup="true"

View File

@ -3,23 +3,40 @@ package net.schueller.peertube.activity;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle; import android.os.Bundle;
import android.os.Environment; import android.os.Environment;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity; import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log; import android.util.Log;
import android.view.View; import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast; import android.widget.Toast;
import com.github.se_bastiaan.torrentstream.StreamStatus;
import com.github.se_bastiaan.torrentstream.Torrent;
import com.github.se_bastiaan.torrentstream.TorrentOptions; import com.github.se_bastiaan.torrentstream.TorrentOptions;
import com.github.se_bastiaan.torrentstream.TorrentStream; import com.github.se_bastiaan.torrentstream.TorrentStream;
import com.github.se_bastiaan.torrentstream.listeners.TorrentListener;
import com.google.android.exoplayer2.ExoPlayerFactory;
import com.google.android.exoplayer2.SimpleExoPlayer;
import com.google.android.exoplayer2.source.ExtractorMediaSource;
import com.google.android.exoplayer2.source.MediaSource;
import com.google.android.exoplayer2.trackselection.AdaptiveTrackSelection;
import com.google.android.exoplayer2.trackselection.DefaultTrackSelector;
import com.google.android.exoplayer2.trackselection.TrackSelection;
import com.google.android.exoplayer2.trackselection.TrackSelector;
import com.google.android.exoplayer2.ui.PlayerView;
import com.google.android.exoplayer2.upstream.BandwidthMeter;
import com.google.android.exoplayer2.upstream.DataSource;
import com.google.android.exoplayer2.upstream.DefaultBandwidthMeter;
import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory;
import com.google.android.exoplayer2.util.Util;
import net.schueller.peertube.R; import net.schueller.peertube.R;
import net.schueller.peertube.model.Video; import net.schueller.peertube.model.Video;
import net.schueller.peertube.model.VideoList;
import net.schueller.peertube.network.GetVideoDataService; import net.schueller.peertube.network.GetVideoDataService;
import net.schueller.peertube.network.RetrofitInstance; import net.schueller.peertube.network.RetrofitInstance;
@ -29,27 +46,93 @@ import retrofit2.Response;
public class TorrentVideoPlayActivity extends AppCompatActivity { public class TorrentVideoPlayActivity extends AppCompatActivity {
private static final String TAG = "TorrentVideoPlayActivity";
private ProgressBar progressBar;
private PlayerView videoView;
private SimpleExoPlayer player;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.activity_torrent_video_play); setContentView(R.layout.activity_torrent_video_play);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
// get video ID // get video ID
Intent intent = getIntent(); Intent intent = getIntent();
String videoID = intent.getStringExtra(VideoListActivity.EXTRA_VIDEOID); String videoID = intent.getStringExtra(VideoListActivity.EXTRA_VIDEOID);
Log.v("TorrentVideoPlayActivity", "click: " + videoID); Log.v(TAG, "click: " + videoID);
progressBar = findViewById(R.id.progress);
progressBar.setMax(100);
videoView = findViewById(R.id.video_view);
// 1. Create a default TrackSelector
BandwidthMeter bandwidthMeter = new DefaultBandwidthMeter();
TrackSelection.Factory videoTrackSelectionFactory =
new AdaptiveTrackSelection.Factory(bandwidthMeter);
TrackSelector trackSelector =
new DefaultTrackSelector(videoTrackSelectionFactory);
// 2. Create the player
player = ExoPlayerFactory.newSimpleInstance(getApplicationContext(), trackSelector);
videoView.setPlayer(player);
TorrentOptions torrentOptions = new TorrentOptions.Builder()
.saveLocation(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS))
.removeFilesAfterStop(true)
.build();
TorrentStream torrentStream = TorrentStream.init(torrentOptions);
torrentStream.addListener(new TorrentListener() {
@Override
public void onStreamReady(Torrent torrent) {
Log.d(TAG, "Ready");
setupVideoView(torrent);
}
@Override
public void onStreamProgress(Torrent torrent, StreamStatus streamStatus) {
if(streamStatus.bufferProgress <= 100 && progressBar.getProgress() < 100 && progressBar.getProgress() != streamStatus.bufferProgress) {
//Log.d(TAG, "Progress: " + streamStatus.bufferProgress);
progressBar.setProgress(streamStatus.bufferProgress);
}
}
@Override
public void onStreamStopped() {
Log.d(TAG, "Stopped");
}
@Override
public void onStreamPrepared(Torrent torrent) {
Log.d(TAG, "Prepared");
if(!torrentStream.isStreaming()) {
torrent.startDownload();
}
// If you set TorrentOptions#autoDownload(false) then this is probably the place to call
// torrent.startDownload();
}
@Override
public void onStreamStarted(Torrent torrent) {
Log.d(TAG, "Started");
}
@Override
public void onStreamError(Torrent torrent, Exception e) {
Log.d(TAG, "Error: " + e.getMessage());
}
});
// get video details from api // get video details from api
SharedPreferences sharedPref = this.getPreferences(Context.MODE_PRIVATE); SharedPreferences sharedPref = this.getPreferences(Context.MODE_PRIVATE);
@ -63,27 +146,73 @@ public class TorrentVideoPlayActivity extends AppCompatActivity {
@Override @Override
public void onResponse(@NonNull Call<Video> call, @NonNull Response<Video> response) { public void onResponse(@NonNull Call<Video> call, @NonNull Response<Video> response) {
Toast.makeText(TorrentVideoPlayActivity.this, response.body().getDescription(), Toast.LENGTH_SHORT).show(); // Toast.makeText(TorrentVideoPlayActivity.this, response.body().getDescription(), Toast.LENGTH_SHORT).show();
// response.body().getFiles() String streamUrl = null;
// TorrentOptions torrentOptions = new TorrentOptions.Builder()
// .removeFilesAfterStop(true) TextView videoName = findViewById(R.id.name);
// .build(); TextView videoDescription = findViewById(R.id.description);
//
// TorrentStream torrentStream = TorrentStream.init(torrentOptions); try {
// torrentStream.startStream("https://butterpoject.org/test.torrent"); streamUrl = response.body().getFiles().get(0).getTorrentUrl();
videoName.setText(response.body().getName());
videoDescription.setText(response.body().getDescription());
} catch (NullPointerException e) {
e.getStackTrace();
}
Log.v(TAG, streamUrl);
torrentStream.startStream(streamUrl);
} }
@Override @Override
public void onFailure(@NonNull Call<Video> call, @NonNull Throwable t) { public void onFailure(@NonNull Call<Video> call, @NonNull Throwable t) {
Log.wtf("err", t.fillInStackTrace()); Log.wtf(TAG, t.fillInStackTrace());
Toast.makeText(TorrentVideoPlayActivity.this, "Something went wrong...Please try later!", Toast.LENGTH_SHORT).show(); Toast.makeText(TorrentVideoPlayActivity.this, "Something went wrong...Please try later!", Toast.LENGTH_SHORT).show();
} }
}); });
}
private void setupVideoView(Torrent torrent) {
Log.d(TAG, "Play Video");
// Produces DataSource instances through which media data is loaded.
DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory(getApplicationContext(),
Util.getUserAgent(getApplicationContext(), "yourApplicationName"), null);
// This is the MediaSource representing the media to be played.
MediaSource videoSource = new ExtractorMediaSource.Factory(dataSourceFactory)
.createMediaSource(Uri.fromFile(torrent.getVideoFile()));
player.setPlayWhenReady(true);
// Prepare the player with the source.
player.prepare(videoSource);
} }
@Override
protected void onDestroy() {
super.onDestroy();
player.release();
}
@Override
protected void onPause() {
super.onPause();
player.stop();
}
@Override
protected void onResume() {
super.onResume();
player.setPlayWhenReady(true);
}
} }

View File

@ -1,11 +1,14 @@
package net.schueller.peertube.activity; package net.schueller.peertube.activity;
import android.Manifest;
import android.app.Activity; import android.app.Activity;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView; import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.ActivityCompat;
import android.support.v4.widget.SwipeRefreshLayout; import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity; import android.support.v7.app.AppCompatActivity;
import android.os.Bundle; import android.os.Bundle;
@ -254,4 +257,13 @@ public class VideoListActivity extends AppCompatActivity {
Log.e("SecurityException", "Google Play Services not available."); Log.e("SecurityException", "Google Play Services not available.");
} }
} }
@Override
protected void onResume() {
super.onResume();
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 0);
}
}
} }

View File

@ -6,28 +6,59 @@
android:layout_height="match_parent" android:layout_height="match_parent"
tools:context="net.schueller.peertube.activity.TorrentVideoPlayActivity"> tools:context="net.schueller.peertube.activity.TorrentVideoPlayActivity">
<android.support.design.widget.AppBarLayout <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="match_parent">
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar" <com.google.android.exoplayer2.ui.PlayerView
android:id="@+id/video_view"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize" android:layout_height="250dp"
android:background="?attr/colorPrimary" android:background="@color/black"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout> app:layout_constraintDimensionRatio="H,3:1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
<include layout="@layout/content_torrent_video_play" /> >
<android.support.design.widget.FloatingActionButton </com.google.android.exoplayer2.ui.PlayerView>
android:id="@+id/fab"
android:layout_width="wrap_content" <ProgressBar
android:layout_height="wrap_content" android:id="@+id/progress"
android:layout_gravity="bottom|end" style="?android:attr/progressBarStyleHorizontal"
android:layout_margin="@dimen/fab_margin" android:layout_width="match_parent"
app:srcCompat="@android:drawable/ic_dialog_email" /> android:layout_height="wrap_content"
android:layout_below="@id/video_view"
android:background="@color/black"
android:indeterminate="false"
android:max="100" />
<android.support.v7.widget.LinearLayoutCompat
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/progress"
android:padding="16dp"
android:orientation="vertical">
<TextView
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="@style/Base.TextAppearance.AppCompat.Title" />
<TextView
android:id="@+id/description"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="@style/Base.TextAppearance.AppCompat.Body1" />
</android.support.v7.widget.LinearLayoutCompat>
</RelativeLayout>
</android.support.design.widget.CoordinatorLayout> </android.support.design.widget.CoordinatorLayout>

View File

@ -1,11 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="net.schueller.peertube.activity.TorrentVideoPlayActivity"
tools:showIn="@layout/activity_torrent_video_play">
</android.support.constraint.ConstraintLayout>

View File

@ -7,4 +7,5 @@
<color name="album_title">#4c4c4c</color> <color name="album_title">#4c4c4c</color>
<color name="black_overlay">#66000000</color> <color name="black_overlay">#66000000</color>
<color name="black">#000000</color>
</resources> </resources>