From 01e031e7e79dcc9aa7c0b68b50d5b976c1b30e1c Mon Sep 17 00:00:00 2001 From: John Zhen Mo Date: Tue, 31 Oct 2017 12:42:56 -0700 Subject: [PATCH] -Modified recovery to not set if progress position is 0 or less. -Modified queue item synchronization to no longer trigger update when the sync is run on the identical item. --- .../org/schabi/newpipe/player/BasePlayer.java | 29 +++++++++++-------- .../player/playback/MediaSourceManager.java | 17 +++++------ 2 files changed, 25 insertions(+), 21 deletions(-) diff --git a/app/src/main/java/org/schabi/newpipe/player/BasePlayer.java b/app/src/main/java/org/schabi/newpipe/player/BasePlayer.java index 8508bb237..748ef00c5 100644 --- a/app/src/main/java/org/schabi/newpipe/player/BasePlayer.java +++ b/app/src/main/java/org/schabi/newpipe/player/BasePlayer.java @@ -26,6 +26,7 @@ import android.content.IntentFilter; import android.graphics.Bitmap; import android.media.AudioManager; import android.net.Uri; +import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.text.TextUtils; import android.util.Log; @@ -76,7 +77,6 @@ import java.util.concurrent.TimeUnit; import io.reactivex.Observable; import io.reactivex.android.schedulers.AndroidSchedulers; -import io.reactivex.annotations.NonNull; import io.reactivex.disposables.Disposable; import io.reactivex.functions.Consumer; import io.reactivex.functions.Predicate; @@ -193,7 +193,7 @@ public abstract class BasePlayer implements Player.EventListener, PlaybackListen .observeOn(AndroidSchedulers.mainThread()) .filter(new Predicate() { @Override - public boolean test(@NonNull Long aLong) throws Exception { + public boolean test(Long aLong) throws Exception { return isProgressLoopRunning(); } }) @@ -235,7 +235,7 @@ public abstract class BasePlayer implements Player.EventListener, PlaybackListen initPlayback(queue); } - protected void initPlayback(@NonNull final PlayQueue queue) { + protected void initPlayback(final PlayQueue queue) { playQueue = queue; playQueue.init(); playbackManager = new MediaSourceManager(this, playQueue); @@ -514,11 +514,10 @@ public abstract class BasePlayer implements Player.EventListener, PlaybackListen } break; case Player.STATE_READY: //3 - recover(); - if (!isPrepared) { isPrepared = true; onPrepared(playWhenReady); + recover(); break; } if (currentState == STATE_PAUSED_SEEK) break; @@ -631,17 +630,21 @@ public abstract class BasePlayer implements Player.EventListener, PlaybackListen } @Override - public void sync(@android.support.annotation.NonNull final PlayQueueItem item, + public void sync(@NonNull final PlayQueueItem item, @Nullable final StreamInfo info) { - if (simpleExoPlayer == null) return; - if (DEBUG) Log.d(TAG, "Syncing..."); - + if (currentItem == item && currentInfo == info) return; currentItem = item; currentInfo = info; + if (DEBUG) Log.d(TAG, "Syncing..."); + if (simpleExoPlayer == null) return; + // Check if on wrong window - final int currentSourceIndex = playQueue.getIndex(); - if (simpleExoPlayer.getCurrentWindowIndex() != currentSourceIndex) { + final int currentSourceIndex = playQueue.indexOf(item); + if (currentSourceIndex != playQueue.getIndex()) { + throw new IllegalStateException("Play Queue may be desynchronized: item index=[" + + currentSourceIndex + "], queue index=[" + playQueue.getIndex() + "]"); + } else if (simpleExoPlayer.getCurrentWindowIndex() != currentSourceIndex) { final long startPos = info != null ? info.start_position : 0; if (DEBUG) Log.d(TAG, "Rewinding to correct window: " + currentSourceIndex + " at: " + getTimeString((int)startPos)); simpleExoPlayer.seekTo(currentSourceIndex, startPos); @@ -900,7 +903,9 @@ public abstract class BasePlayer implements Player.EventListener, PlaybackListen final int queuePos = playQueue.getIndex(); final long windowPos = simpleExoPlayer.getCurrentPosition(); - setRecovery(queuePos, windowPos); + if (windowPos > 0) { + setRecovery(queuePos, windowPos); + } } public void setRecovery(final int queuePos, final long windowPos) { diff --git a/app/src/main/java/org/schabi/newpipe/player/playback/MediaSourceManager.java b/app/src/main/java/org/schabi/newpipe/player/playback/MediaSourceManager.java index f7d267c50..2e4e4af5c 100644 --- a/app/src/main/java/org/schabi/newpipe/player/playback/MediaSourceManager.java +++ b/app/src/main/java/org/schabi/newpipe/player/playback/MediaSourceManager.java @@ -52,7 +52,7 @@ public class MediaSourceManager { public MediaSourceManager(@NonNull final PlaybackListener listener, @NonNull final PlayQueue playQueue) { - this(listener, playQueue, 1, 1000L); + this(listener, playQueue, 1, 400L); } private MediaSourceManager(@NonNull final PlaybackListener listener, @@ -162,7 +162,7 @@ public class MediaSourceManager { return; } - // why no pattern matching in Java =( + // Event specific action switch (event.type()) { case INIT: case REORDER: @@ -172,31 +172,28 @@ public class MediaSourceManager { case APPEND: populateSources(); break; - case SELECT: - sync(); - break; case REMOVE: final RemoveEvent removeEvent = (RemoveEvent) event; remove(removeEvent.getRemoveIndex()); - // Sync only when the currently playing is removed - if (removeEvent.getQueueIndex() == removeEvent.getRemoveIndex()) sync(); break; case MOVE: final MoveEvent moveEvent = (MoveEvent) event; move(moveEvent.getFromIndex(), moveEvent.getToIndex()); break; + case SELECT: case RECOVERY: default: break; } + // Loading and Syncing switch (event.type()) { case INIT: case REORDER: case ERROR: - case APPEND: loadImmediate(); // low frequency, critical events break; + case APPEND: case REMOVE: case SELECT: case MOVE: @@ -294,7 +291,9 @@ public class MediaSourceManager { final DeferredMediaSource mediaSource = (DeferredMediaSource) sources.getMediaSource(playQueue.indexOf(item)); if (mediaSource.state() == DeferredMediaSource.STATE_PREPARED) mediaSource.load(); - if (tryUnblock()) sync(); + + tryUnblock(); + if (!isBlocked) sync(); } private void resetSources() {