-Modified play queues and items to use extraction helper.

-Fixed play queue item removal.
-Rebase changes.
This commit is contained in:
John Zhen M 2017-09-04 10:23:56 -07:00 committed by John Zhen Mo
parent 03f4a81b01
commit f58b0c6959
20 changed files with 212 additions and 1712 deletions

View file

@ -70,7 +70,7 @@ import com.nostra13.universalimageloader.core.listener.SimpleImageLoadingListene
import org.schabi.newpipe.Downloader;
import org.schabi.newpipe.R;
import org.schabi.newpipe.extractor.stream_info.StreamInfo;
import org.schabi.newpipe.extractor.stream.StreamInfo;
import org.schabi.newpipe.playlist.PlayQueue;
import java.io.File;
@ -594,10 +594,9 @@ public abstract class BasePlayer implements Player.EventListener,
public void sync(final int windowIndex, final long windowPos, final StreamInfo info) {
Log.d(TAG, "Syncing...");
videoUrl = info.webpage_url;
videoUrl = info.url;
videoThumbnailUrl = info.thumbnail_url;
videoTitle = info.title;
channelName = info.uploader;
videoTitle = info.name;
if (simpleExoPlayer.getCurrentWindowIndex() != windowIndex) {
Log.w(TAG, "Rewinding to correct window");
@ -615,7 +614,6 @@ public abstract class BasePlayer implements Player.EventListener,
simpleExoPlayer.prepare(playbackManager.getMediaSource());
simpleExoPlayer.seekToDefaultPosition();
simpleExoPlayer.setPlayWhenReady(true);
changeState(STATE_PLAYING);
}
@Override

View file

@ -40,7 +40,7 @@ import android.widget.TextView;
import android.widget.Toast;
import org.schabi.newpipe.R;
import org.schabi.newpipe.extractor.stream_info.StreamInfo;
import org.schabi.newpipe.extractor.stream.StreamInfo;
import org.schabi.newpipe.util.AnimationUtils;
import org.schabi.newpipe.util.NavigationHelper;
import org.schabi.newpipe.util.PermissionHelper;
@ -232,7 +232,7 @@ public class MainVideoPlayer extends Activity {
public void sync(final int windowIndex, final long windowPos, final StreamInfo info) {
super.sync(windowIndex, windowPos, info);
titleTextView.setText(getVideoTitle());
channelTextView.setText(getChannelName());
channelTextView.setText(getUploaderName());
playPauseButton.setImageResource(R.drawable.ic_pause_white);
}

View file

@ -7,7 +7,7 @@ import com.google.android.exoplayer2.source.MediaSource;
import org.reactivestreams.Subscriber;
import org.reactivestreams.Subscription;
import org.schabi.newpipe.extractor.stream_info.StreamInfo;
import org.schabi.newpipe.extractor.stream.StreamInfo;
import org.schabi.newpipe.playlist.PlayQueue;
import org.schabi.newpipe.playlist.PlayQueueItem;
import org.schabi.newpipe.playlist.events.PlayQueueMessage;
@ -19,6 +19,7 @@ import java.util.Collections;
import java.util.List;
import io.reactivex.MaybeObserver;
import io.reactivex.SingleObserver;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.CompositeDisposable;
@ -31,14 +32,14 @@ class MediaSourceManager {
// Effectively loads WINDOW_SIZE * 2 streams
private static final int WINDOW_SIZE = 3;
private final DynamicConcatenatingMediaSource sources;
private final PlaybackListener playbackListener;
private final PlayQueue playQueue;
private DynamicConcatenatingMediaSource sources;
// sourceToQueueIndex maps media source index to play queue index
// Invariant 1: this list is sorted in ascending order
// Invariant 2: this list contains no duplicates
private final List<Integer> sourceToQueueIndex;
private final PlaybackListener playbackListener;
private final PlayQueue playQueue;
private List<Integer> sourceToQueueIndex;
private Subscription playQueueReactor;
private Subscription loadingReactor;
@ -83,13 +84,13 @@ class MediaSourceManager {
MediaSourceManager(@NonNull final MediaSourceManager.PlaybackListener listener,
@NonNull final PlayQueue playQueue) {
this.sources = new DynamicConcatenatingMediaSource();
this.sourceToQueueIndex = Collections.synchronizedList(new ArrayList<Integer>());
this.playbackListener = listener;
this.playQueue = playQueue;
disposables = new CompositeDisposable();
this.disposables = new CompositeDisposable();
this.sources = new DynamicConcatenatingMediaSource();
this.sourceToQueueIndex = Collections.synchronizedList(new ArrayList<Integer>());
playQueue.getBroadcastReceiver()
.observeOn(AndroidSchedulers.mainThread())
@ -113,34 +114,25 @@ class MediaSourceManager {
}
/*
* Called when the player has seamlessly transitioned to another stream.
* Currently only expecting transitioning to the next stream and updates
* the play queue that a transition has occurred.
* Called when the player has transitioned to another stream.
* */
void refresh(final int newSourceIndex) {
if (newSourceIndex == getCurrentSourceIndex()) return;
if (newSourceIndex == getCurrentSourceIndex() + 1) {
playQueue.incrementIndex();
} else {
//something went wrong
Log.e(TAG, "Refresh media failed, reloading.");
if (sourceToQueueIndex.indexOf(newSourceIndex) != -1) {
playQueue.setIndex(sourceToQueueIndex.indexOf(newSourceIndex));
}
sync();
}
void report(final Exception error) {
// ignore error checking for now, just remove the current index
if (error != null && !isBlocked) {
doBlock();
if (error != null) {
tryBlock();
}
final int index = playQueue.getIndex();
remove(index);
playQueue.remove(index);
tryUnblock();
sync();
resetSources();
init();
}
void dispose() {
@ -192,8 +184,8 @@ class MediaSourceManager {
break;
}
if (!isPlayQueueReady() && !isBlocked) {
doBlock();
if (!isPlayQueueReady()) {
tryBlock();
playQueue.fetch();
}
if (playQueueReactor != null) playQueueReactor.request(1);
@ -221,9 +213,11 @@ class MediaSourceManager {
return getCurrentSourceIndex() != -1;
}
private void doBlock() {
playbackListener.block();
isBlocked = true;
private void tryBlock() {
if (!isBlocked) {
playbackListener.block();
isBlocked = true;
}
}
private void tryUnblock() {
@ -241,8 +235,8 @@ class MediaSourceManager {
private void onSelect() {
if (isCurrentIndexLoaded()) {
sync();
} else if (!isBlocked) {
doBlock();
} else {
tryBlock();
}
load();
@ -274,7 +268,7 @@ class MediaSourceManager {
private void init() {
final PlayQueueItem init = playQueue.getCurrent();
init.getStream().subscribe(new MaybeObserver<StreamInfo>() {
init.getStream().subscribe(new SingleObserver<StreamInfo>() {
@Override
public void onSubscribe(@NonNull Disposable d) {
if (disposables != null) {
@ -303,17 +297,11 @@ class MediaSourceManager {
playQueue.remove(playQueue.indexOf(init));
init();
}
@Override
public void onComplete() {
playQueue.remove(playQueue.indexOf(init));
init();
}
});
}
private void load(final PlayQueueItem item) {
item.getStream().subscribe(new MaybeObserver<StreamInfo>() {
item.getStream().subscribe(new SingleObserver<StreamInfo>() {
@Override
public void onSubscribe(@NonNull Disposable d) {
if (disposables != null) {
@ -335,15 +323,17 @@ class MediaSourceManager {
playQueue.remove(playQueue.indexOf(item));
load();
}
@Override
public void onComplete() {
playQueue.remove(playQueue.indexOf(item));
load();
}
});
}
private void resetSources() {
if (this.disposables != null) this.disposables.clear();
if (this.sources != null) this.sources.releaseSource();
if (this.sourceToQueueIndex != null) this.sourceToQueueIndex.clear();
this.sources = new DynamicConcatenatingMediaSource();
}
/*//////////////////////////////////////////////////////////////////////////
// Media Source List Manipulation
//////////////////////////////////////////////////////////////////////////*/

View file

@ -1,212 +0,0 @@
package org.schabi.newpipe.player;
import android.util.Log;
import com.google.android.exoplayer2.source.DynamicConcatenatingMediaSource;
import com.google.android.exoplayer2.source.MediaSource;
import org.reactivestreams.Subscriber;
import org.reactivestreams.Subscription;
import org.schabi.newpipe.extractor.stream_info.StreamInfo;
import org.schabi.newpipe.playlist.PlayQueue;
import org.schabi.newpipe.playlist.PlayQueueItem;
import org.schabi.newpipe.playlist.events.PlayQueueMessage;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import io.reactivex.Maybe;
import io.reactivex.annotations.NonNull;
public class PlaybackManager {
private final String TAG = "PlaybackManager@" + Integer.toHexString(hashCode());
private static final int WINDOW_SIZE = 3;
private DynamicConcatenatingMediaSource mediaSource;
private List<StreamInfo> syncInfos;
private int sourceIndex;
private PlaybackListener listener;
private PlayQueue playQueue;
private Subscription playQueueReactor;
public boolean prepared = false;
interface PlaybackListener {
void block();
void unblock();
void resync();
void sync(final StreamInfo info);
MediaSource sourceOf(final StreamInfo info);
}
public PlaybackManager(@NonNull final PlaybackListener listener,
@NonNull final PlayQueue playQueue) {
this.mediaSource = new DynamicConcatenatingMediaSource();
this.syncInfos = Collections.synchronizedList(new ArrayList<StreamInfo>());
this.sourceIndex = 0;
this.listener = listener;
this.playQueue = playQueue;
playQueue.getBroadcastReceiver().subscribe(getReactor());
}
@NonNull
public DynamicConcatenatingMediaSource getMediaSource() {
return mediaSource;
}
private void reload() {
listener.block();
mediaSource = new DynamicConcatenatingMediaSource();
syncInfos.clear();
load();
}
public void changeSource(final MediaSource newSource) {
this.mediaSource.removeMediaSource(0);
this.mediaSource.addMediaSource(0, newSource);
}
public void refreshMedia(final int newMediaIndex) {
if (newMediaIndex == sourceIndex) return;
if (newMediaIndex == sourceIndex + 1) {
playQueue.incrementIndex();
mediaSource.removeMediaSource(0);
syncInfos.remove(0);
} else {
//something went wrong
Log.e(TAG, "Refresh media failed, reloading.");
reload();
}
}
private Subscription loaderReactor;
private void load() {
if (mediaSource.getSize() < WINDOW_SIZE) load(mediaSource.getSize());
}
private void load(final int from) {
// Fetch queue items
//todo fix out of bound
final int index = playQueue.getIndex();
List<Maybe<StreamInfo>> maybes = new ArrayList<>();
for (int i = from; i < WINDOW_SIZE; i++) {
final PlayQueueItem item = playQueue.get(index + i);
maybes.add(item.getStream());
}
// Stop loading and clear pending media sources
if (loaderReactor != null) loaderReactor.cancel();
clear(from);
// Start sequential loading of media sources
Maybe.concat(maybes).subscribe(getSubscriber());
}
private Subscriber<StreamInfo> getSubscriber() {
return new Subscriber<StreamInfo>() {
@Override
public void onSubscribe(Subscription s) {
if (loaderReactor != null) loaderReactor.cancel();
loaderReactor = s;
s.request(1);
}
@Override
public void onNext(StreamInfo streamInfo) {
mediaSource.addMediaSource(listener.sourceOf(streamInfo));
syncInfos.add(streamInfo);
tryUnblock();
loaderReactor.request(1);
}
@Override
public void onError(Throwable t) {
playQueue.remove(playQueue.getIndex());
}
@Override
public void onComplete() {
if (loaderReactor != null) loaderReactor.cancel();
loaderReactor = null;
}
};
}
private void tryUnblock() {
if (mediaSource.getSize() > 0) listener.unblock();
}
private void clear(int from) {
while (mediaSource.getSize() > from) {
mediaSource.removeMediaSource(from);
syncInfos.remove(from);
}
}
private Subscriber<PlayQueueMessage> getReactor() {
return new Subscriber<PlayQueueMessage>() {
@Override
public void onSubscribe(@NonNull Subscription d) {
if (playQueueReactor != null) playQueueReactor.cancel();
playQueueReactor = d;
playQueueReactor.request(1);
}
@Override
public void onNext(@NonNull PlayQueueMessage event) {
if (playQueue.getStreams().size() - playQueue.getIndex() < WINDOW_SIZE && !playQueue.isComplete()) {
listener.block();
playQueue.fetch();
}
switch (event.type()) {
case SELECT:
case INIT:
reload();
break;
case APPEND:
load();
break;
case REMOVE:
case SWAP:
load(1);
break;
case NEXT:
default:
break;
}
tryUnblock();
if (!syncInfos.isEmpty()) listener.sync(syncInfos.get(0));
if (playQueueReactor != null) playQueueReactor.request(1);
}
@Override
public void onError(@NonNull Throwable e) {
}
@Override
public void onComplete() {
dispose();
}
};
}
public void dispose() {
if (playQueueReactor != null) playQueueReactor.cancel();
playQueueReactor = null;
}
}

View file

@ -53,14 +53,15 @@ import com.google.android.exoplayer2.source.MergingMediaSource;
import com.google.android.exoplayer2.ui.AspectRatioFrameLayout;
import org.schabi.newpipe.R;
import org.schabi.newpipe.extractor.InfoItem;
import org.schabi.newpipe.extractor.MediaFormat;
import org.schabi.newpipe.extractor.stream.AudioStream;
import org.schabi.newpipe.extractor.stream.StreamInfoItem;
import org.schabi.newpipe.extractor.stream.VideoStream;
import org.schabi.newpipe.extractor.stream.StreamInfo;
import org.schabi.newpipe.extractor.playlist.PlayListInfo;
import org.schabi.newpipe.playlist.ExternalPlayQueue;
import org.schabi.newpipe.util.AnimationUtils;
import org.schabi.newpipe.util.Utils;
import org.schabi.newpipe.util.ListHelper;
import java.io.Serializable;
import java.util.ArrayList;
@ -201,42 +202,50 @@ public abstract class VideoPlayer extends BasePlayer implements SimpleExoPlayer.
simpleExoPlayer.setVideoListener(this);
}
// @SuppressWarnings("unchecked")
// public void handleIntent2(Intent intent) {
// super.handleIntent(intent);
// if (DEBUG) Log.d(TAG, "handleIntent() called with: intent = [" + intent + "]");
// if (intent == null) return;
//
// selectedIndexStream = intent.getIntExtra(INDEX_SEL_VIDEO_STREAM, -1);
//
// Serializable serializable = intent.getSerializableExtra(VIDEO_STREAMS_LIST);
//
// if (serializable instanceof ArrayList) videoStreamsList = (ArrayList<VideoStream>) serializable;
// if (serializable instanceof Vector) videoStreamsList = new ArrayList<>((List<VideoStream>) serializable);
//
// Serializable audioStream = intent.getSerializableExtra(VIDEO_ONLY_AUDIO_STREAM);
// if (audioStream != null) videoOnlyAudioStream = (AudioStream) audioStream;
//
// startedFromNewPipe = intent.getBooleanExtra(STARTED_FROM_NEWPIPE, true);
// play(true);
// }
@SuppressWarnings("unchecked")
public void handleSingleStreamIntent(Intent intent) {
super.handleIntent(intent);
if (DEBUG) Log.d(TAG, "handleIntent() called with: intent = [" + intent + "]");
if (intent == null) return;
selectedIndexStream = intent.getIntExtra(INDEX_SEL_VIDEO_STREAM, -1);
Serializable serializable = intent.getSerializableExtra(VIDEO_STREAMS_LIST);
if (serializable instanceof ArrayList) videoStreamsList = (ArrayList<VideoStream>) serializable;
if (serializable instanceof Vector) videoStreamsList = new ArrayList<>((List<VideoStream>) serializable);
Serializable audioStream = intent.getSerializableExtra(VIDEO_ONLY_AUDIO_STREAM);
if (audioStream != null) videoOnlyAudioStream = (AudioStream) audioStream;
startedFromNewPipe = intent.getBooleanExtra(STARTED_FROM_NEWPIPE, true);
play(true);
}
@SuppressWarnings("unchecked")
public void handleIntent(Intent intent) {
if (intent == null) return;
handleExternalPlaylistIntent(intent);
}
@SuppressWarnings("unchecked")
public void handleExternalPlaylistIntent(Intent intent) {
selectedIndexStream = 0;
String url = intent.getStringExtra("url");
int nextPage = intent.getIntExtra("nextPage", 0);
int index = intent.getIntExtra("index", 0);
final int serviceId = intent.getIntExtra("serviceId", -1);
final int index = intent.getIntExtra("index", 0);
final Serializable serializable = intent.getSerializableExtra("streams");
final String nextPageUrl = intent.getStringExtra("nextPageUrl");
PlayListInfo info;
Serializable serializable = intent.getSerializableExtra("stream");
if (serializable instanceof PlayListInfo) info = (PlayListInfo) serializable;
else return;
List<InfoItem> info = new ArrayList<>();
if (serializable instanceof List) {
for (final Object o : (List) serializable) {
if (o instanceof InfoItem) info.add((StreamInfoItem) o);
}
}
playQueue = new ExternalPlayQueue(url, info, nextPage, index);
playQueue = new ExternalPlayQueue(serviceId, nextPageUrl, info, index);
playbackManager = new MediaSourceManager(this, playQueue);
}
@ -263,13 +272,13 @@ public abstract class VideoPlayer extends BasePlayer implements SimpleExoPlayer.
@Override
public MediaSource sourceOf(final StreamInfo info) {
final List<VideoStream> videos = Utils.getSortedStreamVideosList(context, info.video_streams, info.video_only_streams, false);
final VideoStream video = videos.get(Utils.getDefaultResolution(context, videos));
final List<VideoStream> videos = ListHelper.getSortedStreamVideosList(context, info.video_streams, info.video_only_streams, false);
final VideoStream video = videos.get(ListHelper.getDefaultResolutionIndex(context, videos));
final MediaSource mediaSource = super.buildMediaSource(video.url, MediaFormat.getSuffixById(video.format));
if (!video.isVideoOnly) return mediaSource;
final AudioStream audio = Utils.getHighestQualityAudio(info.audio_streams);
final AudioStream audio = ListHelper.getHighestQualityAudio(info.audio_streams);
final Uri audioUri = Uri.parse(audio.url);
return new MergingMediaSource(mediaSource, new ExtractorMediaSource(audioUri, cacheDataSourceFactory, extractorsFactory, null, null));
}