Skip to content

Commit

Permalink
-Refactored synchronization checks out from MediaSourceManager to Man…
Browse files Browse the repository at this point in the history
…agedMediaSource.

-Fixed null input causing potential NPE on PlayQueueItem.
  • Loading branch information
karyogamy committed Mar 17, 2018
1 parent f818b0b commit d7ccd15
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,13 @@ public void releasePeriod(MediaPeriod mediaPeriod) {}
public void releaseSource() {}

@Override
public boolean canReplace(@NonNull final PlayQueueItem newIdentity) {
public boolean shouldBeReplacedWith(@NonNull final PlayQueueItem newIdentity,
final boolean isInterruptable) {
return newIdentity != playQueueItem || canRetry();
}

@Override
public boolean isStreamEqual(@NonNull PlayQueueItem stream) {
return playQueueItem == stream;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,13 @@ public void releaseSource() {
}

@Override
public boolean canReplace(@NonNull final PlayQueueItem newIdentity) {
return newIdentity != stream || isExpired();
public boolean shouldBeReplacedWith(@NonNull PlayQueueItem newIdentity,
final boolean isInterruptable) {
return newIdentity != stream || (isInterruptable && isExpired());
}

@Override
public boolean isStreamEqual(@NonNull PlayQueueItem stream) {
return this.stream == stream;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,21 @@
import org.schabi.newpipe.playlist.PlayQueueItem;

public interface ManagedMediaSource extends MediaSource {
boolean canReplace(@NonNull final PlayQueueItem newIdentity);
/**
* Determines whether or not this {@link ManagedMediaSource} can be replaced.
*
* @param newIdentity a stream the {@link ManagedMediaSource} should encapsulate over, if
* it is different from the existing stream in the
* {@link ManagedMediaSource}, then it should be replaced.
* @param isInterruptable specifies if this {@link ManagedMediaSource} potentially
* being played.
* */
boolean shouldBeReplacedWith(@NonNull final PlayQueueItem newIdentity,
final boolean isInterruptable);

/**
* Determines if the {@link PlayQueueItem} is the one the
* {@link ManagedMediaSource} encapsulates over.
* */
boolean isStreamEqual(@NonNull final PlayQueueItem stream);
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,13 @@ public class PlaceholderMediaSource implements ManagedMediaSource {
@Override public void releaseSource() {}

@Override
public boolean canReplace(@NonNull final PlayQueueItem newIdentity) {
public boolean shouldBeReplacedWith(@NonNull PlayQueueItem newIdentity,
final boolean isInterruptable) {
return true;
}

@Override
public boolean isStreamEqual(@NonNull PlayQueueItem stream) {
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -268,15 +268,10 @@ private boolean isPlayQueueReady() {
private boolean isPlaybackReady() {
if (sources.getSize() != playQueue.size()) return false;

final MediaSource mediaSource = sources.getMediaSource(playQueue.getIndex());
final ManagedMediaSource mediaSource =
(ManagedMediaSource) sources.getMediaSource(playQueue.getIndex());
final PlayQueueItem playQueueItem = playQueue.getItem();

if (mediaSource instanceof LoadedMediaSource) {
return playQueueItem == ((LoadedMediaSource) mediaSource).getStream();
} else if (mediaSource instanceof FailedMediaSource) {
return playQueueItem == ((FailedMediaSource) mediaSource).getStream();
}
return false;
return mediaSource.isStreamEqual(playQueueItem);
}

private void maybeBlock() {
Expand Down Expand Up @@ -453,12 +448,8 @@ private boolean isCorrectionNeeded(@NonNull final PlayQueueItem item) {
if (index == -1 || index >= sources.getSize()) return false;

final ManagedMediaSource mediaSource = (ManagedMediaSource) sources.getMediaSource(index);

if (index == playQueue.getIndex() && mediaSource instanceof LoadedMediaSource) {
return item != ((LoadedMediaSource) mediaSource).getStream();
} else {
return mediaSource.canReplace(item);
}
return mediaSource.shouldBeReplacedWith(item,
/*mightBeInProgress=*/index != playQueue.getIndex());
}

/**
Expand All @@ -479,7 +470,7 @@ private void maybeRenewCurrentIndex() {
final ManagedMediaSource currentSource =
(ManagedMediaSource) sources.getMediaSource(currentIndex);
final PlayQueueItem currentItem = playQueue.getItem();
if (!currentSource.canReplace(currentItem)) {
if (!currentSource.shouldBeReplacedWith(currentItem, /*canInterruptOnRenew=*/true)) {
maybeSynchronizePlayer();
return;
}
Expand Down
31 changes: 16 additions & 15 deletions app/src/main/java/org/schabi/newpipe/playlist/PlayQueueItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,19 @@
import java.io.Serializable;

import io.reactivex.Single;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

public class PlayQueueItem implements Serializable {
final public static long RECOVERY_UNSET = Long.MIN_VALUE;
public final static long RECOVERY_UNSET = Long.MIN_VALUE;
private final static String EMPTY_STRING = "";

final private String title;
final private String url;
@NonNull final private String title;
@NonNull final private String url;
final private int serviceId;
final private long duration;
final private String thumbnailUrl;
final private String uploader;
final private StreamType streamType;
@NonNull final private String thumbnailUrl;
@NonNull final private String uploader;
@NonNull final private StreamType streamType;

private long recoveryPosition;
private Throwable error;
Expand All @@ -42,15 +41,16 @@ public class PlayQueueItem implements Serializable {
item.getThumbnailUrl(), item.getUploaderName(), item.getStreamType());
}

private PlayQueueItem(final String name, final String url, final int serviceId,
final long duration, final String thumbnailUrl, final String uploader,
final StreamType streamType) {
this.title = name;
this.url = url;
private PlayQueueItem(@Nullable final String name, @Nullable final String url,
final int serviceId, final long duration,
@Nullable final String thumbnailUrl, @Nullable final String uploader,
@NonNull final StreamType streamType) {
this.title = name != null ? name : EMPTY_STRING;
this.url = url != null ? url : EMPTY_STRING;
this.serviceId = serviceId;
this.duration = duration;
this.thumbnailUrl = thumbnailUrl;
this.uploader = uploader;
this.thumbnailUrl = thumbnailUrl != null ? thumbnailUrl : EMPTY_STRING;
this.uploader = uploader != null ? uploader : EMPTY_STRING;
this.streamType = streamType;

this.recoveryPosition = RECOVERY_UNSET;
Expand Down Expand Up @@ -84,6 +84,7 @@ public String getUploader() {
return uploader;
}

@NonNull
public StreamType getStreamType() {
return streamType;
}
Expand Down

0 comments on commit d7ccd15

Please sign in to comment.