Skip to content

Commit

Permalink
-Re-added loading for items prior to current index in MediaSourceMana…
Browse files Browse the repository at this point in the history
…ger to allow faster access time.

-Added some null checks annotation.
  • Loading branch information
karyogamy committed Mar 1, 2018
1 parent a1220c7 commit 9ea08c8
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -326,8 +326,10 @@ private void loadImmediate() {
maybeLoadItem(currentItem);

// The rest are just for seamless playback
final int leftBound = currentIndex + 1;
final int rightLimit = leftBound + WINDOW_SIZE;
// Although timeline is not updated prior to the current index, these sources are still
// loaded into the cache for faster retrieval at a potentially later time.
final int leftBound = Math.max(0, currentIndex - WINDOW_SIZE);
final int rightLimit = currentIndex + WINDOW_SIZE + 1;
final int rightBound = Math.min(playQueue.size(), rightLimit);
final List<PlayQueueItem> items = new ArrayList<>(
playQueue.getStreams().subList(leftBound,rightBound));
Expand All @@ -343,10 +345,9 @@ private void loadImmediate() {
}
}

private void maybeLoadItem(@Nullable final PlayQueueItem item) {
private void maybeLoadItem(@NonNull final PlayQueueItem item) {
if (DEBUG) Log.d(TAG, "maybeLoadItem() called.");

if (sources == null || item == null) return;
if (sources == null) return;

final int index = playQueue.indexOf(item);
if (index > sources.getSize() - 1) return;
Expand All @@ -355,7 +356,11 @@ private void maybeLoadItem(@Nullable final PlayQueueItem item) {
if (DEBUG) Log.d(TAG, " Loaded: [" + item.getTitle() +
"] with url: " + item.getUrl());

if (isCorrectionNeeded(item)) update(playQueue.indexOf(item), mediaSource);
final int itemIndex = playQueue.indexOf(item);
// Only update the playlist timeline for items at the current index or after.
if (itemIndex >= playQueue.getIndex() && isCorrectionNeeded(item)) {
update(itemIndex, mediaSource);
}

loadingItems.remove(item);
tryUnblock();
Expand Down Expand Up @@ -449,7 +454,7 @@ private void populateSources() {
* with position * in respect to the play queue only if no {@link MediaSource}
* already exists at the given index.
* */
private void emplace(final int index, final MediaSource source) {
private void emplace(final int index, @NonNull final MediaSource source) {
if (sources == null) return;
if (index < 0 || index < sources.getSize()) return;

Expand Down Expand Up @@ -489,7 +494,7 @@ private void move(final int source, final int target) {
* this will modify the playback timeline prior to the index and cause desynchronization
* on the playing item between {@link PlayQueue} and {@link DynamicConcatenatingMediaSource}.
* */
private synchronized void update(final int index, final MediaSource source) {
private synchronized void update(final int index, @NonNull final MediaSource source) {
if (sources == null) return;
if (index < 0 || index >= sources.getSize()) return;

Expand Down
10 changes: 5 additions & 5 deletions app/src/main/java/org/schabi/newpipe/playlist/PlayQueue.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public abstract class PlayQueue implements Serializable {

private ArrayList<PlayQueueItem> backup;
private ArrayList<PlayQueueItem> streams;
private final AtomicInteger queueIndex;
@NonNull private final AtomicInteger queueIndex;

private transient BehaviorSubject<PlayQueueEvent> eventBroadcast;
private transient Flowable<PlayQueueEvent> broadcastReceiver;
Expand Down Expand Up @@ -133,7 +133,7 @@ public PlayQueueItem getItem(int index) {
* Returns the index of the given item using referential equality.
* May be null despite play queue contains identical item.
* */
public int indexOf(final PlayQueueItem item) {
public int indexOf(@NonNull final PlayQueueItem item) {
// referential equality, can't think of a better way to do this
// todo: better than this
return streams.indexOf(item);
Expand Down Expand Up @@ -213,7 +213,7 @@ public synchronized void offsetIndex(final int offset) {
*
* @see #append(List items)
* */
public synchronized void append(final PlayQueueItem... items) {
public synchronized void append(@NonNull final PlayQueueItem... items) {
append(Arrays.asList(items));
}

Expand All @@ -225,7 +225,7 @@ public synchronized void append(final PlayQueueItem... items) {
*
* Will emit a {@link AppendEvent} on any given context.
* */
public synchronized void append(final List<PlayQueueItem> items) {
public synchronized void append(@NonNull final List<PlayQueueItem> items) {
List<PlayQueueItem> itemList = new ArrayList<>(items);

if (isShuffled()) {
Expand Down Expand Up @@ -393,7 +393,7 @@ public synchronized void unshuffle() {
// Rx Broadcast
//////////////////////////////////////////////////////////////////////////*/

private void broadcast(final PlayQueueEvent event) {
private void broadcast(@NonNull final PlayQueueEvent event) {
if (eventBroadcast != null) {
eventBroadcast.onNext(event);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public class NavigationHelper {
// Players
//////////////////////////////////////////////////////////////////////////*/

@NonNull
public static Intent getPlayerIntent(@NonNull final Context context,
@NonNull final Class targetClazz,
@NonNull final PlayQueue playQueue,
Expand All @@ -74,12 +75,14 @@ public static Intent getPlayerIntent(@NonNull final Context context,
return intent;
}

@NonNull
public static Intent getPlayerIntent(@NonNull final Context context,
@NonNull final Class targetClazz,
@NonNull final PlayQueue playQueue) {
return getPlayerIntent(context, targetClazz, playQueue, null);
}

@NonNull
public static Intent getPlayerEnqueueIntent(@NonNull final Context context,
@NonNull final Class targetClazz,
@NonNull final PlayQueue playQueue,
Expand All @@ -89,6 +92,7 @@ public static Intent getPlayerEnqueueIntent(@NonNull final Context context,
.putExtra(BasePlayer.SELECT_ON_APPEND, selectOnAppend);
}

@NonNull
public static Intent getPlayerIntent(@NonNull final Context context,
@NonNull final Class targetClazz,
@NonNull final PlayQueue playQueue,
Expand Down

0 comments on commit 9ea08c8

Please sign in to comment.