2017-09-06 08:49:00 +02:00
|
|
|
package org.schabi.newpipe.player.playback;
|
2017-09-02 20:06:36 +02:00
|
|
|
|
2017-09-05 00:38:58 +02:00
|
|
|
import android.support.annotation.Nullable;
|
2017-09-03 04:30:34 +02:00
|
|
|
|
2017-09-02 20:06:36 +02:00
|
|
|
import com.google.android.exoplayer2.source.DynamicConcatenatingMediaSource;
|
|
|
|
import com.google.android.exoplayer2.source.MediaSource;
|
|
|
|
|
|
|
|
import org.reactivestreams.Subscriber;
|
|
|
|
import org.reactivestreams.Subscription;
|
2017-09-04 19:23:56 +02:00
|
|
|
import org.schabi.newpipe.extractor.stream.StreamInfo;
|
2017-09-02 20:06:36 +02:00
|
|
|
import org.schabi.newpipe.playlist.PlayQueue;
|
2017-09-03 04:30:34 +02:00
|
|
|
import org.schabi.newpipe.playlist.PlayQueueItem;
|
2017-09-02 20:06:36 +02:00
|
|
|
import org.schabi.newpipe.playlist.events.PlayQueueMessage;
|
2017-09-03 04:30:34 +02:00
|
|
|
import org.schabi.newpipe.playlist.events.RemoveEvent;
|
2017-09-07 22:01:02 +02:00
|
|
|
import org.schabi.newpipe.playlist.events.MoveEvent;
|
2017-09-02 20:06:36 +02:00
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.List;
|
|
|
|
|
2017-09-04 19:23:56 +02:00
|
|
|
import io.reactivex.SingleObserver;
|
2017-09-03 04:30:34 +02:00
|
|
|
import io.reactivex.android.schedulers.AndroidSchedulers;
|
2017-09-02 20:06:36 +02:00
|
|
|
import io.reactivex.annotations.NonNull;
|
2017-09-03 04:30:34 +02:00
|
|
|
import io.reactivex.disposables.CompositeDisposable;
|
|
|
|
import io.reactivex.disposables.Disposable;
|
|
|
|
import io.reactivex.functions.Consumer;
|
|
|
|
|
2017-09-06 08:49:00 +02:00
|
|
|
public class PlaybackManager {
|
|
|
|
private final String TAG = "PlaybackManager@" + Integer.toHexString(hashCode());
|
2017-09-04 04:15:11 +02:00
|
|
|
// One-side rolling window size for default loading
|
|
|
|
// Effectively loads WINDOW_SIZE * 2 streams
|
2017-09-06 08:49:00 +02:00
|
|
|
private static final int WINDOW_SIZE = 2;
|
2017-09-02 20:06:36 +02:00
|
|
|
|
2017-09-04 19:23:56 +02:00
|
|
|
private final PlaybackListener playbackListener;
|
|
|
|
private final PlayQueue playQueue;
|
|
|
|
|
|
|
|
private DynamicConcatenatingMediaSource sources;
|
2017-09-03 04:30:34 +02:00
|
|
|
// 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
|
2017-09-04 19:23:56 +02:00
|
|
|
private List<Integer> sourceToQueueIndex;
|
2017-09-02 20:06:36 +02:00
|
|
|
|
|
|
|
private Subscription playQueueReactor;
|
2017-09-06 08:49:00 +02:00
|
|
|
private Disposable syncReactor;
|
2017-09-03 04:30:34 +02:00
|
|
|
private CompositeDisposable disposables;
|
2017-09-02 20:06:36 +02:00
|
|
|
|
2017-09-04 04:15:11 +02:00
|
|
|
private boolean isBlocked;
|
|
|
|
|
2017-09-06 08:49:00 +02:00
|
|
|
public PlaybackManager(@NonNull final PlaybackListener listener,
|
|
|
|
@NonNull final PlayQueue playQueue) {
|
2017-09-02 20:06:36 +02:00
|
|
|
this.playbackListener = listener;
|
|
|
|
this.playQueue = playQueue;
|
|
|
|
|
2017-09-04 19:23:56 +02:00
|
|
|
this.disposables = new CompositeDisposable();
|
|
|
|
|
|
|
|
this.sources = new DynamicConcatenatingMediaSource();
|
|
|
|
this.sourceToQueueIndex = Collections.synchronizedList(new ArrayList<Integer>());
|
2017-09-03 04:30:34 +02:00
|
|
|
|
|
|
|
playQueue.getBroadcastReceiver()
|
|
|
|
.observeOn(AndroidSchedulers.mainThread())
|
|
|
|
.subscribe(getReactor());
|
|
|
|
}
|
|
|
|
|
2017-09-04 04:15:11 +02:00
|
|
|
/*//////////////////////////////////////////////////////////////////////////
|
|
|
|
// Exposed Methods
|
|
|
|
//////////////////////////////////////////////////////////////////////////*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Returns the media source index of the currently playing stream.
|
|
|
|
* */
|
2017-09-06 08:49:00 +02:00
|
|
|
public int getCurrentSourceIndex() {
|
2017-09-03 04:30:34 +02:00
|
|
|
return sourceToQueueIndex.indexOf(playQueue.getIndex());
|
|
|
|
}
|
|
|
|
|
|
|
|
@NonNull
|
2017-09-06 08:49:00 +02:00
|
|
|
public DynamicConcatenatingMediaSource getMediaSource() {
|
2017-09-03 04:30:34 +02:00
|
|
|
return sources;
|
|
|
|
}
|
|
|
|
|
2017-09-04 04:15:11 +02:00
|
|
|
/*
|
2017-09-04 19:23:56 +02:00
|
|
|
* Called when the player has transitioned to another stream.
|
2017-09-04 04:15:11 +02:00
|
|
|
* */
|
2017-09-06 08:49:00 +02:00
|
|
|
public void refresh(final int newSourceIndex) {
|
|
|
|
if (sourceToQueueIndex.indexOf(newSourceIndex) != -1 && newSourceIndex == getCurrentSourceIndex() + 1) {
|
|
|
|
playQueue.offsetIndex(+1);
|
2017-09-03 04:30:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-06 08:49:00 +02:00
|
|
|
public void report(final Exception error) {
|
2017-09-04 14:42:34 +02:00
|
|
|
// ignore error checking for now, just remove the current index
|
2017-09-07 22:01:02 +02:00
|
|
|
if (error == null) return;
|
|
|
|
|
|
|
|
tryBlock();
|
2017-09-04 14:42:34 +02:00
|
|
|
|
|
|
|
final int index = playQueue.getIndex();
|
|
|
|
playQueue.remove(index);
|
2017-09-04 19:23:56 +02:00
|
|
|
|
|
|
|
resetSources();
|
2017-09-05 00:38:58 +02:00
|
|
|
load();
|
2017-09-04 14:42:34 +02:00
|
|
|
}
|
|
|
|
|
2017-09-06 08:49:00 +02:00
|
|
|
public void updateCurrent(final int newSortedStreamsIndex) {
|
2017-09-07 22:01:02 +02:00
|
|
|
tryBlock();
|
2017-09-05 21:27:12 +02:00
|
|
|
|
|
|
|
PlayQueueItem item = playQueue.getCurrent();
|
|
|
|
item.setSortedQualityIndex(newSortedStreamsIndex);
|
2017-09-06 08:49:00 +02:00
|
|
|
|
2017-09-05 21:27:12 +02:00
|
|
|
resetSources();
|
|
|
|
load();
|
|
|
|
}
|
|
|
|
|
2017-09-06 08:49:00 +02:00
|
|
|
public void dispose() {
|
2017-09-04 04:15:11 +02:00
|
|
|
if (playQueueReactor != null) playQueueReactor.cancel();
|
|
|
|
if (disposables != null) disposables.dispose();
|
2017-09-06 08:49:00 +02:00
|
|
|
if (syncReactor != null) syncReactor.dispose();
|
|
|
|
if (sources != null) sources.releaseSource();
|
|
|
|
if (sourceToQueueIndex != null) sourceToQueueIndex.clear();
|
2017-09-04 04:15:11 +02:00
|
|
|
|
|
|
|
playQueueReactor = null;
|
|
|
|
disposables = null;
|
2017-09-06 08:49:00 +02:00
|
|
|
syncReactor = null;
|
|
|
|
sources = null;
|
|
|
|
sourceToQueueIndex = null;
|
2017-09-04 04:15:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*//////////////////////////////////////////////////////////////////////////
|
|
|
|
// Event Reactor
|
|
|
|
//////////////////////////////////////////////////////////////////////////*/
|
|
|
|
|
|
|
|
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) {
|
|
|
|
// why no pattern matching in Java =(
|
|
|
|
switch (event.type()) {
|
|
|
|
case INIT:
|
2017-09-05 00:38:58 +02:00
|
|
|
isBlocked = true;
|
2017-09-07 22:01:02 +02:00
|
|
|
break;
|
2017-09-04 04:15:11 +02:00
|
|
|
case APPEND:
|
|
|
|
break;
|
|
|
|
case SELECT:
|
|
|
|
onSelect();
|
|
|
|
break;
|
|
|
|
case REMOVE:
|
|
|
|
final RemoveEvent removeEvent = (RemoveEvent) event;
|
|
|
|
remove(removeEvent.index());
|
|
|
|
break;
|
2017-09-07 22:01:02 +02:00
|
|
|
case MOVE:
|
2017-09-08 06:47:43 +02:00
|
|
|
throw new UnsupportedOperationException("Move not yet supported");
|
2017-09-04 04:15:11 +02:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-09-04 19:23:56 +02:00
|
|
|
if (!isPlayQueueReady()) {
|
|
|
|
tryBlock();
|
2017-09-04 04:15:11 +02:00
|
|
|
playQueue.fetch();
|
2017-09-05 21:27:12 +02:00
|
|
|
} else if (playQueue.isEmpty()) {
|
|
|
|
playbackListener.shutdown();
|
2017-09-07 22:01:02 +02:00
|
|
|
} else {
|
|
|
|
load(); // All event warrants a load
|
2017-09-04 04:15:11 +02:00
|
|
|
}
|
2017-09-05 21:27:12 +02:00
|
|
|
|
2017-09-04 04:15:11 +02:00
|
|
|
if (playQueueReactor != null) playQueueReactor.request(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onError(@NonNull Throwable e) {}
|
|
|
|
|
|
|
|
@Override
|
2017-09-07 22:01:02 +02:00
|
|
|
public void onComplete() {}
|
2017-09-04 04:15:11 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/*//////////////////////////////////////////////////////////////////////////
|
|
|
|
// Internal Helpers
|
|
|
|
//////////////////////////////////////////////////////////////////////////*/
|
|
|
|
|
|
|
|
private boolean isPlayQueueReady() {
|
|
|
|
return playQueue.isComplete() || playQueue.size() - playQueue.getIndex() > WINDOW_SIZE;
|
|
|
|
}
|
|
|
|
|
|
|
|
private boolean isCurrentIndexLoaded() {
|
|
|
|
return getCurrentSourceIndex() != -1;
|
|
|
|
}
|
|
|
|
|
2017-09-05 00:38:58 +02:00
|
|
|
private boolean tryBlock() {
|
2017-09-04 19:23:56 +02:00
|
|
|
if (!isBlocked) {
|
|
|
|
playbackListener.block();
|
|
|
|
isBlocked = true;
|
2017-09-05 00:38:58 +02:00
|
|
|
return true;
|
2017-09-04 19:23:56 +02:00
|
|
|
}
|
2017-09-05 00:38:58 +02:00
|
|
|
return false;
|
2017-09-04 14:42:34 +02:00
|
|
|
}
|
|
|
|
|
2017-09-05 00:38:58 +02:00
|
|
|
private boolean tryUnblock() {
|
2017-09-04 04:15:11 +02:00
|
|
|
if (isPlayQueueReady() && isCurrentIndexLoaded() && isBlocked) {
|
|
|
|
isBlocked = false;
|
|
|
|
playbackListener.unblock();
|
2017-09-05 00:38:58 +02:00
|
|
|
return true;
|
2017-09-04 04:15:11 +02:00
|
|
|
}
|
2017-09-05 00:38:58 +02:00
|
|
|
return false;
|
2017-09-04 04:15:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Responds to a SELECT event.
|
2017-09-07 22:01:02 +02:00
|
|
|
*
|
|
|
|
* If the player is being blocked, then nothing should happen.
|
|
|
|
*
|
|
|
|
* Otherwise:
|
|
|
|
*
|
|
|
|
* When the selected item is already loaded, then we simply synchronize and
|
2017-09-06 08:49:00 +02:00
|
|
|
* start loading some more items.
|
|
|
|
*
|
2017-09-07 22:01:02 +02:00
|
|
|
* When the current item has not been fully loaded, then the player will be
|
2017-09-06 08:49:00 +02:00
|
|
|
* blocked. The sources will be reset and reloaded, to conserve memory.
|
2017-09-04 04:15:11 +02:00
|
|
|
* */
|
|
|
|
private void onSelect() {
|
2017-09-07 22:01:02 +02:00
|
|
|
if (isBlocked) return;
|
|
|
|
|
|
|
|
if (isCurrentIndexLoaded()) {
|
2017-09-03 04:30:34 +02:00
|
|
|
sync();
|
2017-09-04 19:23:56 +02:00
|
|
|
} else {
|
|
|
|
tryBlock();
|
2017-09-06 08:49:00 +02:00
|
|
|
resetSources();
|
2017-09-03 04:30:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void sync() {
|
2017-09-05 21:27:12 +02:00
|
|
|
final PlayQueueItem currentItem = playQueue.getCurrent();
|
|
|
|
|
2017-09-06 08:49:00 +02:00
|
|
|
final Consumer<StreamInfo> syncPlayback = new Consumer<StreamInfo>() {
|
2017-09-03 04:30:34 +02:00
|
|
|
@Override
|
|
|
|
public void accept(StreamInfo streamInfo) throws Exception {
|
2017-09-05 21:27:12 +02:00
|
|
|
playbackListener.sync(streamInfo, currentItem.getSortedQualityIndex());
|
2017-09-03 04:30:34 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-09-06 08:49:00 +02:00
|
|
|
currentItem.getStream().subscribe(syncPlayback);
|
2017-09-03 04:30:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private void load() {
|
2017-09-05 00:38:58 +02:00
|
|
|
// The current item has higher priority
|
2017-09-03 04:30:34 +02:00
|
|
|
final int currentIndex = playQueue.getIndex();
|
2017-09-05 00:38:58 +02:00
|
|
|
final PlayQueueItem currentItem = playQueue.get(currentIndex);
|
|
|
|
if (currentItem != null) load(currentItem);
|
2017-09-07 22:01:02 +02:00
|
|
|
else return;
|
2017-09-03 04:30:34 +02:00
|
|
|
|
2017-09-05 00:38:58 +02:00
|
|
|
// The rest are just for seamless playback
|
2017-09-03 04:30:34 +02:00
|
|
|
final int leftBound = Math.max(0, currentIndex - WINDOW_SIZE);
|
|
|
|
final int rightBound = Math.min(playQueue.size(), currentIndex + WINDOW_SIZE);
|
|
|
|
final List<PlayQueueItem> items = playQueue.getStreams().subList(leftBound, rightBound);
|
|
|
|
for (final PlayQueueItem item: items) {
|
|
|
|
load(item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-05 00:38:58 +02:00
|
|
|
private void load(@Nullable final PlayQueueItem item) {
|
|
|
|
if (item == null) return;
|
2017-09-04 04:15:11 +02:00
|
|
|
|
2017-09-04 19:23:56 +02:00
|
|
|
item.getStream().subscribe(new SingleObserver<StreamInfo>() {
|
2017-09-03 04:30:34 +02:00
|
|
|
@Override
|
|
|
|
public void onSubscribe(@NonNull Disposable d) {
|
2017-09-06 08:49:00 +02:00
|
|
|
if (disposables == null) {
|
2017-09-03 04:30:34 +02:00
|
|
|
d.dispose();
|
2017-09-06 08:49:00 +02:00
|
|
|
return;
|
2017-09-03 04:30:34 +02:00
|
|
|
}
|
2017-09-06 08:49:00 +02:00
|
|
|
|
|
|
|
disposables.add(d);
|
2017-09-03 04:30:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onSuccess(@NonNull StreamInfo streamInfo) {
|
2017-09-05 21:27:12 +02:00
|
|
|
final MediaSource source = playbackListener.sourceOf(streamInfo, item.getSortedQualityIndex());
|
2017-09-03 04:30:34 +02:00
|
|
|
insert(playQueue.indexOf(item), source);
|
2017-09-05 00:38:58 +02:00
|
|
|
if (tryUnblock()) sync();
|
2017-09-03 04:30:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onError(@NonNull Throwable e) {
|
|
|
|
playQueue.remove(playQueue.indexOf(item));
|
2017-09-04 04:15:11 +02:00
|
|
|
load();
|
2017-09-03 04:30:34 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-09-04 19:23:56 +02:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2017-09-04 04:15:11 +02:00
|
|
|
/*//////////////////////////////////////////////////////////////////////////
|
|
|
|
// Media Source List Manipulation
|
|
|
|
//////////////////////////////////////////////////////////////////////////*/
|
|
|
|
|
2017-09-03 04:30:34 +02:00
|
|
|
// Insert source into playlist with position in respect to the play queue
|
|
|
|
// If the play queue index already exists, then the insert is ignored
|
|
|
|
private void insert(final int queueIndex, final MediaSource source) {
|
|
|
|
if (queueIndex < 0) return;
|
|
|
|
|
|
|
|
int pos = Collections.binarySearch(sourceToQueueIndex, queueIndex);
|
|
|
|
if (pos < 0) {
|
|
|
|
final int sourceIndex = -pos-1;
|
|
|
|
sourceToQueueIndex.add(sourceIndex, queueIndex);
|
|
|
|
sources.addMediaSource(sourceIndex, source);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void remove(final int queueIndex) {
|
|
|
|
if (queueIndex < 0) return;
|
|
|
|
|
|
|
|
final int sourceIndex = sourceToQueueIndex.indexOf(queueIndex);
|
|
|
|
if (sourceIndex != -1) {
|
|
|
|
sourceToQueueIndex.remove(sourceIndex);
|
|
|
|
sources.removeMediaSource(sourceIndex);
|
|
|
|
// Will be slow on really large arrays, fast enough for typical use case
|
|
|
|
for (int i = sourceIndex; i < sourceToQueueIndex.size(); i++) {
|
|
|
|
sourceToQueueIndex.set(i, sourceToQueueIndex.get(i) - 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-09-02 20:06:36 +02:00
|
|
|
}
|