-Improved play queue message bus

-Hooking play queue engines with video players (to be removed)
-Proof of concept for previous and next controls
This commit is contained in:
John Zhen M 2017-09-02 11:06:36 -07:00 committed by John Zhen Mo
parent b859823011
commit 74b58cae59
17 changed files with 345 additions and 95 deletions

View file

@ -259,9 +259,10 @@ public abstract class BasePlayer implements Player.EventListener,
isPrepared = false;
if (simpleExoPlayer.getPlaybackState() != Player.STATE_IDLE) simpleExoPlayer.stop();
if (simpleExoPlayer.getPlaybackState() != Player.STATE_IDLE) simpleExoPlayer.setPlayWhenReady(false);//simpleExoPlayer.stop();
if (videoStartPos > 0) simpleExoPlayer.seekTo(videoStartPos);
simpleExoPlayer.prepare(mediaSource);
if (!playbackManager.prepared) simpleExoPlayer.prepare(mediaSource);
playbackManager.prepared = true;
simpleExoPlayer.setPlayWhenReady(autoPlay);
}
@ -557,7 +558,8 @@ public abstract class BasePlayer implements Player.EventListener,
@Override
public void block() {
if (currentState != STATE_LOADING) changeState(STATE_LOADING);
if (currentState != STATE_BUFFERING) changeState(STATE_BUFFERING);
simpleExoPlayer.stop();
}
@Override
@ -565,6 +567,11 @@ public abstract class BasePlayer implements Player.EventListener,
if (currentState != STATE_PLAYING) changeState(STATE_PLAYING);
}
@Override
public void resync() {
simpleExoPlayer.seekTo(0, 0L);
}
@Override
public void sync(final StreamInfo info) {
videoTitle = info.title;

View file

@ -40,6 +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.util.AnimationUtils;
import org.schabi.newpipe.util.NavigationHelper;
import org.schabi.newpipe.util.PermissionHelper;
@ -227,6 +228,13 @@ public class MainVideoPlayer extends Activity {
channelTextView.setText(getUploaderName());
}
@Override
public void sync(final StreamInfo info) {
super.sync(info);
titleTextView.setText(getVideoTitle());
channelTextView.setText(getChannelName());
}
@Override
public void playUrl(String url, String format, boolean autoPlay) {
super.playUrl(url, format, autoPlay);

View file

@ -0,0 +1,95 @@
package org.schabi.newpipe.player;
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.events.PlayQueueMessage;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import io.reactivex.annotations.NonNull;
public class MediaSourceManager {
private DynamicConcatenatingMediaSource sources;
// indices maps media source index to play queue index
// Invariant 1: all indices occur once only in this list
private List<Integer> indices;
private PlaybackListener playbackListener;
private PlayQueue playQueue;
private Subscription playQueueReactor;
interface PlaybackListener {
void block();
void unblock();
void resync();
void sync(final StreamInfo info);
MediaSource sourceOf(final StreamInfo info);
}
public MediaSourceManager(@NonNull final MediaSourceManager.PlaybackListener listener,
@NonNull final PlayQueue playQueue) {
this.sources = new DynamicConcatenatingMediaSource();
this.indices = Collections.synchronizedList(new ArrayList<Integer>());
this.playbackListener = listener;
this.playQueue = playQueue;
playQueue.getEventBroadcast().subscribe(getReactor());
}
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) {
switch (event.type()) {
case INIT:
break;
case APPEND:
break;
case SELECT:
break;
case REMOVE:
case SWAP:
break;
case NEXT:
default:
break;
}
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

@ -1,5 +1,7 @@
package org.schabi.newpipe.player;
import android.util.Log;
import com.google.android.exoplayer2.source.DynamicConcatenatingMediaSource;
import com.google.android.exoplayer2.source.MediaSource;
@ -7,24 +9,25 @@ 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.PlayQueueEvent;
import org.schabi.newpipe.playlist.events.PlayQueueEvent;
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.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.schedulers.Schedulers;
public class PlaybackManager {
private final String TAG = "PlaybackManager@" + Integer.toHexString(hashCode());
private static final int WINDOW_SIZE = 5;
private static final int WINDOW_SIZE = 3;
private DynamicConcatenatingMediaSource mediaSource;
private List<PlayQueueItem> queueSource;
private List<StreamInfo> syncInfos;
private int sourceIndex;
private PlaybackListener listener;
@ -32,10 +35,13 @@ public class PlaybackManager {
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);
}
@ -43,13 +49,13 @@ public class PlaybackManager {
public PlaybackManager(@NonNull final PlaybackListener listener,
@NonNull final PlayQueue playQueue) {
this.mediaSource = new DynamicConcatenatingMediaSource();
this.queueSource = Collections.synchronizedList(new ArrayList<PlayQueueItem>(10));
this.syncInfos = Collections.synchronizedList(new ArrayList<StreamInfo>());
this.sourceIndex = 0;
this.listener = listener;
this.playQueue = playQueue;
playQueue.getPlayQueueFlowable().subscribe(getReactor());
playQueue.getEventBroadcast().subscribe(getReactor());
}
@NonNull
@ -63,10 +69,8 @@ public class PlaybackManager {
}
public void changeSource(final MediaSource newSource) {
listener.block();
this.mediaSource.removeMediaSource(0);
this.mediaSource.addMediaSource(0, newSource);
listener.unblock();
}
public void refreshMedia(final int newMediaIndex) {
@ -75,43 +79,42 @@ public class PlaybackManager {
if (newMediaIndex == sourceIndex + 1) {
playQueue.incrementIndex();
mediaSource.removeMediaSource(0);
queueSource.remove(0);
syncInfos.remove(0);
} else {
//something went wrong
Log.e(TAG, "Refresh media failed, reloading.");
reload();
}
}
private void removeCurrent() {
listener.block();
mediaSource.removeMediaSource(0);
queueSource.remove(0);
listener.unblock();
syncInfos.remove(0);
}
private Subscription loaderReactor;
private void load() {
if (mediaSource.getSize() < WINDOW_SIZE && queueSource.size() < WINDOW_SIZE)
load(mediaSource.getSize());
if (mediaSource.getSize() < WINDOW_SIZE) load(mediaSource.getSize());
}
private void load(final int from) {
clear(from);
if (loaderReactor != null) loaderReactor.cancel();
// 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 int index = playQueue.getIndex() + i;
final PlayQueueItem item = playQueue.get(index);
if (queueSource.size() > i) queueSource.set(i, item);
else queueSource.add(item);
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());
}
@ -127,13 +130,14 @@ public class PlaybackManager {
@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(queueSource.size());
playQueue.remove(playQueue.getIndex());
}
@Override
@ -145,7 +149,7 @@ public class PlaybackManager {
}
private void tryUnblock() {
if (mediaSource.getSize() > 0 && queueSource.size() > 0) listener.unblock();
if (mediaSource.getSize() > 0) listener.unblock();
}
private void init() {
@ -155,19 +159,13 @@ public class PlaybackManager {
private void clear(int from) {
while (mediaSource.getSize() > from) {
queueSource.remove(from);
mediaSource.removeMediaSource(from);
syncInfos.remove(from);
}
}
private void clear() {
listener.block();
clear(0);
listener.unblock();
}
private Subscriber<PlayQueueEvent> getReactor() {
return new Subscriber<PlayQueueEvent>() {
private Subscriber<PlayQueueMessage> getReactor() {
return new Subscriber<PlayQueueMessage>() {
@Override
public void onSubscribe(@NonNull Subscription d) {
if (playQueueReactor != null) playQueueReactor.cancel();
@ -176,23 +174,19 @@ public class PlaybackManager {
}
@Override
public void onNext(@NonNull PlayQueueEvent event) {
public void onNext(@NonNull PlayQueueMessage event) {
if (playQueue.getStreams().size() - playQueue.getIndex() < WINDOW_SIZE && !playQueue.isComplete()) {
listener.block();
playQueue.fetch();
}
switch (event) {
switch (event.type()) {
case INIT:
init();
break;
case APPEND:
load();
break;
case REMOVE_CURRENT:
removeCurrent();
load();
break;
case SELECT:
reload();
break;
@ -200,15 +194,13 @@ public class PlaybackManager {
case SWAP:
load(1);
break;
case CLEAR:
clear();
break;
case NEXT:
default:
break;
}
tryUnblock();
if (!syncInfos.isEmpty()) listener.sync(syncInfos.get(0));
if (playQueueReactor != null) playQueueReactor.request(1);
}

View file

@ -229,10 +229,16 @@ public abstract class VideoPlayer extends BasePlayer implements SimpleExoPlayer.
return buildMediaSource(getSelectedVideoStream().url, MediaFormat.getSuffixById(getSelectedVideoStream().format));
}
@Override
public void block() {
if (currentState != STATE_BUFFERING) changeState(STATE_BUFFERING);
simpleExoPlayer.stop();
}
@Override
public void unblock() {
play(true);
super.unblock();
if (currentState != STATE_PLAYING) changeState(STATE_PLAYING);
if (!isPlaying()) play(true);
}
public void handleIntent(Intent intent) {