mirror of
https://github.com/TeamNewPipe/NewPipe
synced 2025-10-27 21:37:39 +00:00
Simplify selection of video stream
I was trying to understand the logic here, and noticed the indirection via a QualityResolver interfaces is pretty unnecessary. Just branching directly makes the logic a lot easier to follow. The `-999` sentinel value is a bit dumb, but java does not recognize that videoIndex is always initialized. Nice side-effect, the `Resolver` interface was completely unused and can be dropped.
This commit is contained in:
@@ -42,8 +42,6 @@ import static org.schabi.newpipe.player.notification.NotificationConstants.ACTIO
|
|||||||
import static org.schabi.newpipe.player.notification.NotificationConstants.ACTION_RECREATE_NOTIFICATION;
|
import static org.schabi.newpipe.player.notification.NotificationConstants.ACTION_RECREATE_NOTIFICATION;
|
||||||
import static org.schabi.newpipe.player.notification.NotificationConstants.ACTION_REPEAT;
|
import static org.schabi.newpipe.player.notification.NotificationConstants.ACTION_REPEAT;
|
||||||
import static org.schabi.newpipe.player.notification.NotificationConstants.ACTION_SHUFFLE;
|
import static org.schabi.newpipe.player.notification.NotificationConstants.ACTION_SHUFFLE;
|
||||||
import static org.schabi.newpipe.util.ListHelper.getPopupResolutionIndex;
|
|
||||||
import static org.schabi.newpipe.util.ListHelper.getResolutionIndex;
|
|
||||||
import static org.schabi.newpipe.util.Localization.assureCorrectAppLanguage;
|
import static org.schabi.newpipe.util.Localization.assureCorrectAppLanguage;
|
||||||
import static java.util.concurrent.TimeUnit.MILLISECONDS;
|
import static java.util.concurrent.TimeUnit.MILLISECONDS;
|
||||||
|
|
||||||
@@ -116,7 +114,6 @@ import org.schabi.newpipe.player.ui.PlayerUiList;
|
|||||||
import org.schabi.newpipe.player.ui.PopupPlayerUi;
|
import org.schabi.newpipe.player.ui.PopupPlayerUi;
|
||||||
import org.schabi.newpipe.player.ui.VideoPlayerUi;
|
import org.schabi.newpipe.player.ui.VideoPlayerUi;
|
||||||
import org.schabi.newpipe.util.DependentPreferenceHelper;
|
import org.schabi.newpipe.util.DependentPreferenceHelper;
|
||||||
import org.schabi.newpipe.util.ListHelper;
|
|
||||||
import org.schabi.newpipe.util.NavigationHelper;
|
import org.schabi.newpipe.util.NavigationHelper;
|
||||||
import org.schabi.newpipe.util.image.PicassoHelper;
|
import org.schabi.newpipe.util.image.PicassoHelper;
|
||||||
import org.schabi.newpipe.util.SerializedCache;
|
import org.schabi.newpipe.util.SerializedCache;
|
||||||
@@ -292,7 +289,7 @@ public final class Player implements PlaybackListener, Listener {
|
|||||||
context.getString(
|
context.getString(
|
||||||
R.string.use_exoplayer_decoder_fallback_key), false));
|
R.string.use_exoplayer_decoder_fallback_key), false));
|
||||||
|
|
||||||
videoResolver = new VideoPlaybackResolver(context, dataSource, getQualityResolver());
|
videoResolver = new VideoPlaybackResolver(context, dataSource);
|
||||||
audioResolver = new AudioPlaybackResolver(context, dataSource);
|
audioResolver = new AudioPlaybackResolver(context, dataSource);
|
||||||
|
|
||||||
currentThumbnailTarget = getCurrentThumbnailTarget();
|
currentThumbnailTarget = getCurrentThumbnailTarget();
|
||||||
@@ -306,25 +303,6 @@ public final class Player implements PlaybackListener, Listener {
|
|||||||
new NotificationPlayerUi(this)
|
new NotificationPlayerUi(this)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
private VideoPlaybackResolver.QualityResolver getQualityResolver() {
|
|
||||||
return new VideoPlaybackResolver.QualityResolver() {
|
|
||||||
@Override
|
|
||||||
public int getDefaultResolutionIndex(final List<VideoStream> sortedVideos) {
|
|
||||||
return videoPlayerSelected()
|
|
||||||
? ListHelper.getDefaultResolutionIndex(context, sortedVideos)
|
|
||||||
: ListHelper.getPopupDefaultResolutionIndex(context, sortedVideos);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public int getOverrideResolutionIndex(final List<VideoStream> sortedVideos,
|
|
||||||
final String playbackQuality) {
|
|
||||||
return videoPlayerSelected()
|
|
||||||
? getResolutionIndex(context, sortedVideos, playbackQuality)
|
|
||||||
: getPopupResolutionIndex(context, sortedVideos, playbackQuality);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
//endregion
|
//endregion
|
||||||
|
|
||||||
|
|
||||||
@@ -1908,7 +1886,10 @@ public final class Player implements PlaybackListener, Listener {
|
|||||||
// Note that the video is not fetched when the app is in background because the video
|
// Note that the video is not fetched when the app is in background because the video
|
||||||
// renderer is fully disabled (see useVideoSource method), except for HLS streams
|
// renderer is fully disabled (see useVideoSource method), except for HLS streams
|
||||||
// (see https://github.com/google/ExoPlayer/issues/9282).
|
// (see https://github.com/google/ExoPlayer/issues/9282).
|
||||||
return videoResolver.resolve(info);
|
return videoResolver.resolve(info, videoPlayerSelected()
|
||||||
|
? VideoPlaybackResolver.SelectedPlayer.MAIN
|
||||||
|
: VideoPlaybackResolver.SelectedPlayer.POPUP
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void disablePreloadingOfCurrentTrack() {
|
public void disablePreloadingOfCurrentTrack() {
|
||||||
|
|||||||
@@ -45,7 +45,6 @@ public class AudioPlaybackResolver implements PlaybackResolver {
|
|||||||
* @param info of the stream
|
* @param info of the stream
|
||||||
* @return the audio source to use or null if none could be found
|
* @return the audio source to use or null if none could be found
|
||||||
*/
|
*/
|
||||||
@Override
|
|
||||||
@Nullable
|
@Nullable
|
||||||
public MediaSource resolve(@NonNull final StreamInfo info) {
|
public MediaSource resolve(@NonNull final StreamInfo info) {
|
||||||
final MediaSource liveSource = PlaybackResolver.maybeBuildLiveMediaSource(dataSource, info);
|
final MediaSource liveSource = PlaybackResolver.maybeBuildLiveMediaSource(dataSource, info);
|
||||||
|
|||||||
@@ -46,11 +46,10 @@ import java.nio.charset.StandardCharsets;
|
|||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This interface is just a shorthand for {@link Resolver} with {@link StreamInfo} as source and
|
* This interface contains many static methods that can be used by classes
|
||||||
* {@link MediaSource} as product. It contains many static methods that can be used by classes
|
|
||||||
* implementing this interface, and nothing else.
|
* implementing this interface, and nothing else.
|
||||||
*/
|
*/
|
||||||
public interface PlaybackResolver extends Resolver<StreamInfo, MediaSource> {
|
public interface PlaybackResolver {
|
||||||
String TAG = PlaybackResolver.class.getSimpleName();
|
String TAG = PlaybackResolver.class.getSimpleName();
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,9 +0,0 @@
|
|||||||
package org.schabi.newpipe.player.resolver;
|
|
||||||
|
|
||||||
import androidx.annotation.NonNull;
|
|
||||||
import androidx.annotation.Nullable;
|
|
||||||
|
|
||||||
public interface Resolver<Source, Product> {
|
|
||||||
@Nullable
|
|
||||||
Product resolve(@NonNull Source source);
|
|
||||||
}
|
|
||||||
@@ -39,8 +39,6 @@ public class VideoPlaybackResolver implements PlaybackResolver {
|
|||||||
private final Context context;
|
private final Context context;
|
||||||
@NonNull
|
@NonNull
|
||||||
private final PlayerDataSource dataSource;
|
private final PlayerDataSource dataSource;
|
||||||
@NonNull
|
|
||||||
private final QualityResolver qualityResolver;
|
|
||||||
private SourceType streamSourceType;
|
private SourceType streamSourceType;
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
@@ -54,17 +52,23 @@ public class VideoPlaybackResolver implements PlaybackResolver {
|
|||||||
VIDEO_WITH_AUDIO_OR_AUDIO_ONLY
|
VIDEO_WITH_AUDIO_OR_AUDIO_ONLY
|
||||||
}
|
}
|
||||||
|
|
||||||
public VideoPlaybackResolver(@NonNull final Context context,
|
/**
|
||||||
@NonNull final PlayerDataSource dataSource,
|
* Depending on the player we select different video streams.
|
||||||
@NonNull final QualityResolver qualityResolver) {
|
*/
|
||||||
this.context = context;
|
public enum SelectedPlayer {
|
||||||
this.dataSource = dataSource;
|
MAIN,
|
||||||
this.qualityResolver = qualityResolver;
|
POPUP
|
||||||
|
}
|
||||||
|
|
||||||
|
public VideoPlaybackResolver(@NonNull final Context context,
|
||||||
|
@NonNull final PlayerDataSource dataSource) {
|
||||||
|
this.context = context;
|
||||||
|
this.dataSource = dataSource;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
@Nullable
|
@Nullable
|
||||||
public MediaSource resolve(@NonNull final StreamInfo info) {
|
public MediaSource resolve(@NonNull final StreamInfo info,
|
||||||
|
@NonNull final SelectedPlayer selectedPlayer) {
|
||||||
final MediaSource liveSource = PlaybackResolver.maybeBuildLiveMediaSource(dataSource, info);
|
final MediaSource liveSource = PlaybackResolver.maybeBuildLiveMediaSource(dataSource, info);
|
||||||
if (liveSource != null) {
|
if (liveSource != null) {
|
||||||
streamSourceType = SourceType.LIVE_STREAM;
|
streamSourceType = SourceType.LIVE_STREAM;
|
||||||
@@ -80,14 +84,42 @@ public class VideoPlaybackResolver implements PlaybackResolver {
|
|||||||
final List<AudioStream> audioStreamsList =
|
final List<AudioStream> audioStreamsList =
|
||||||
getFilteredAudioStreams(context, info.getAudioStreams());
|
getFilteredAudioStreams(context, info.getAudioStreams());
|
||||||
|
|
||||||
final int videoIndex;
|
int videoIndex = -999;
|
||||||
if (videoStreamsList.isEmpty()) {
|
if (videoStreamsList.isEmpty()) {
|
||||||
videoIndex = -1;
|
videoIndex = -1;
|
||||||
} else if (playbackQuality == null) {
|
} else if (playbackQuality == null) {
|
||||||
videoIndex = qualityResolver.getDefaultResolutionIndex(videoStreamsList);
|
switch (selectedPlayer) {
|
||||||
|
case MAIN -> {
|
||||||
|
videoIndex = ListHelper.getDefaultResolutionIndex(
|
||||||
|
context,
|
||||||
|
videoStreamsList
|
||||||
|
);
|
||||||
|
}
|
||||||
|
case POPUP -> {
|
||||||
|
videoIndex = ListHelper.getPopupDefaultResolutionIndex(
|
||||||
|
context,
|
||||||
|
videoStreamsList
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
videoIndex = qualityResolver.getOverrideResolutionIndex(videoStreamsList,
|
switch (selectedPlayer) {
|
||||||
getPlaybackQuality());
|
case MAIN -> {
|
||||||
|
videoIndex = ListHelper.getResolutionIndex(
|
||||||
|
context,
|
||||||
|
videoStreamsList,
|
||||||
|
getPlaybackQuality()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
case POPUP -> {
|
||||||
|
videoIndex = ListHelper.getPopupResolutionIndex(
|
||||||
|
context,
|
||||||
|
videoStreamsList,
|
||||||
|
getPlaybackQuality()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
final int audioIndex =
|
final int audioIndex =
|
||||||
@@ -195,10 +227,4 @@ public class VideoPlaybackResolver implements PlaybackResolver {
|
|||||||
public void setAudioTrack(@Nullable final String audioLanguage) {
|
public void setAudioTrack(@Nullable final String audioLanguage) {
|
||||||
this.audioTrack = audioLanguage;
|
this.audioTrack = audioLanguage;
|
||||||
}
|
}
|
||||||
|
|
||||||
public interface QualityResolver {
|
|
||||||
int getDefaultResolutionIndex(List<VideoStream> sortedVideos);
|
|
||||||
|
|
||||||
int getOverrideResolutionIndex(List<VideoStream> sortedVideos, String playbackQuality);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user