Skip to content

Commit e8216b2

Browse files
Apply code review suggestions.
1 parent e3062d7 commit e8216b2

2 files changed

Lines changed: 35 additions & 29 deletions

File tree

app/src/main/java/org/schabi/newpipe/player/Player.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2056,6 +2056,7 @@ public void useVideoSource(final boolean videoEnabled) {
20562056

20572057
setRecovery();
20582058
}, () -> {
2059+
// This is executed when the current stream info is not available.
20592060
reloadPlayQueueManager();
20602061
setRecovery();
20612062
});

app/src/main/java/org/schabi/newpipe/player/playback/MediaSourceManager.java

Lines changed: 34 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import org.schabi.newpipe.player.playqueue.events.PlayQueueEvent;
2222
import org.schabi.newpipe.player.playqueue.events.RemoveEvent;
2323
import org.schabi.newpipe.player.playqueue.events.ReorderEvent;
24-
import org.schabi.newpipe.util.ServiceHelper;
2524

2625
import java.util.Collection;
2726
import java.util.Collections;
@@ -42,6 +41,7 @@
4241
import static org.schabi.newpipe.player.mediasource.FailedMediaSource.MediaSourceResolutionException;
4342
import static org.schabi.newpipe.player.mediasource.FailedMediaSource.StreamInfoLoadException;
4443
import static org.schabi.newpipe.player.playqueue.PlayQueue.DEBUG;
44+
import static org.schabi.newpipe.util.ServiceHelper.getCacheExpirationMillis;
4545

4646
public class MediaSourceManager {
4747
@NonNull
@@ -420,34 +420,39 @@ private void maybeLoadItem(@NonNull final PlayQueueItem item) {
420420
}
421421

422422
private Single<ManagedMediaSource> getLoadedMediaSource(@NonNull final PlayQueueItem stream) {
423-
return stream.getStream().map(streamInfo -> {
424-
final var source = playbackListener.sourceOf(stream, streamInfo);
425-
426-
return Optional.ofNullable(source)
427-
.flatMap(source1 -> MediaItemTag.from(source1.getMediaItem()))
428-
.<ManagedMediaSource>map(tag -> {
429-
final long expiration = System.currentTimeMillis()
430-
+ ServiceHelper.getCacheExpirationMillis(streamInfo.getServiceId());
431-
return new LoadedMediaSource(source, tag, stream, expiration);
432-
})
433-
.orElseGet(() -> {
434-
final String message = "Unable to resolve source from stream info. "
435-
+ "URL: " + stream.getUrl() + ", "
436-
+ "audio count: " + streamInfo.getAudioStreams().size() + ", "
437-
+ "video count: " + streamInfo.getVideoOnlyStreams().size() + ", "
438-
+ streamInfo.getVideoStreams().size();
439-
return FailedMediaSource.of(stream, new MediaSourceResolutionException(
440-
message));
441-
});
442-
}).onErrorReturn(throwable -> {
443-
if (throwable instanceof ExtractionException) {
444-
return FailedMediaSource.of(stream, new StreamInfoLoadException(throwable));
445-
}
446-
// Non-source related error expected here (e.g. network),
447-
// should allow retry shortly after the error.
448-
return FailedMediaSource.of(stream, new Exception(throwable),
449-
/*allowRetryIn=*/TimeUnit.MILLISECONDS.convert(3, TimeUnit.SECONDS));
450-
});
423+
return stream.getStream()
424+
.map(streamInfo -> Optional
425+
.ofNullable(playbackListener.sourceOf(stream, streamInfo))
426+
.<ManagedMediaSource>flatMap(source ->
427+
MediaItemTag.from(source.getMediaItem())
428+
.map(tag -> {
429+
final int serviceId = streamInfo.getServiceId();
430+
final long expiration = System.currentTimeMillis()
431+
+ getCacheExpirationMillis(serviceId);
432+
return new LoadedMediaSource(source, tag, stream,
433+
expiration);
434+
})
435+
)
436+
.orElseGet(() -> {
437+
final String message = "Unable to resolve source from stream info. "
438+
+ "URL: " + stream.getUrl()
439+
+ ", audio count: " + streamInfo.getAudioStreams().size()
440+
+ ", video count: " + streamInfo.getVideoOnlyStreams().size()
441+
+ ", " + streamInfo.getVideoStreams().size();
442+
return FailedMediaSource.of(stream,
443+
new MediaSourceResolutionException(message));
444+
})
445+
)
446+
.onErrorReturn(throwable -> {
447+
if (throwable instanceof ExtractionException) {
448+
return FailedMediaSource.of(stream, new StreamInfoLoadException(throwable));
449+
}
450+
// Non-source related error expected here (e.g. network),
451+
// should allow retry shortly after the error.
452+
final long allowRetryIn = TimeUnit.MILLISECONDS.convert(3,
453+
TimeUnit.SECONDS);
454+
return FailedMediaSource.of(stream, new Exception(throwable), allowRetryIn);
455+
});
451456
}
452457

453458
private void onMediaSourceReceived(@NonNull final PlayQueueItem item,

0 commit comments

Comments
 (0)