Improved `ListHelper#getSortedStreamVideosList
`
This commit is contained in:
parent
cc96ac173c
commit
8ed87e7fbb
1 changed files with 35 additions and 55 deletions
|
@ -20,7 +20,11 @@ import java.util.Arrays;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.Comparator;
|
import java.util.Comparator;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
import java.util.HashSet;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Objects;
|
||||||
|
import java.util.Set;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
public final class ListHelper {
|
public final class ListHelper {
|
||||||
// Video format in order of quality. 0=lowest quality, n=highest quality
|
// Video format in order of quality. 0=lowest quality, n=highest quality
|
||||||
|
@ -34,8 +38,9 @@ public final class ListHelper {
|
||||||
private static final List<MediaFormat> AUDIO_FORMAT_EFFICIENCY_RANKING =
|
private static final List<MediaFormat> AUDIO_FORMAT_EFFICIENCY_RANKING =
|
||||||
Arrays.asList(MediaFormat.WEBMA, MediaFormat.M4A, MediaFormat.MP3);
|
Arrays.asList(MediaFormat.WEBMA, MediaFormat.M4A, MediaFormat.MP3);
|
||||||
|
|
||||||
private static final List<String> HIGH_RESOLUTION_LIST
|
private static final Set<String> HIGH_RESOLUTION_LIST
|
||||||
= Arrays.asList("1440p", "2160p", "1440p60", "2160p60");
|
// Uses a HashSet for better performance
|
||||||
|
= new HashSet<>(Arrays.asList("1440p", "2160p", "1440p60", "2160p60"));
|
||||||
|
|
||||||
private ListHelper() { }
|
private ListHelper() { }
|
||||||
|
|
||||||
|
@ -213,66 +218,39 @@ public final class ListHelper {
|
||||||
@Nullable final List<VideoStream> videoStreams,
|
@Nullable final List<VideoStream> videoStreams,
|
||||||
@Nullable final List<VideoStream> videoOnlyStreams,
|
@Nullable final List<VideoStream> videoOnlyStreams,
|
||||||
final boolean ascendingOrder,
|
final boolean ascendingOrder,
|
||||||
final boolean preferVideoOnlyStreams) {
|
final boolean preferVideoOnlyStreams
|
||||||
final ArrayList<VideoStream> retList = new ArrayList<>();
|
) {
|
||||||
|
// Determine order of streams
|
||||||
|
// The last added list is preferred
|
||||||
|
final List<List<VideoStream>> videoStreamsOrdered =
|
||||||
|
preferVideoOnlyStreams
|
||||||
|
? Arrays.asList(videoStreams, videoOnlyStreams)
|
||||||
|
: Arrays.asList(videoOnlyStreams, videoStreams);
|
||||||
|
|
||||||
|
final List<VideoStream> allInitialStreams = videoStreamsOrdered.stream()
|
||||||
|
// Ignore lists that are null
|
||||||
|
.filter(Objects::nonNull)
|
||||||
|
.flatMap(List::stream)
|
||||||
|
// Filter out higher resolutions (or not if high resolutions should always be shown)
|
||||||
|
.filter(stream -> showHigherResolutions
|
||||||
|
|| !HIGH_RESOLUTION_LIST.contains(stream.getResolution()))
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
|
||||||
final HashMap<String, VideoStream> hashMap = new HashMap<>();
|
final HashMap<String, VideoStream> hashMap = new HashMap<>();
|
||||||
|
|
||||||
if (preferVideoOnlyStreams) {
|
|
||||||
if (videoStreams != null) {
|
|
||||||
for (final VideoStream stream : videoStreams) {
|
|
||||||
if (!showHigherResolutions && HIGH_RESOLUTION_LIST.contains(
|
|
||||||
stream.getResolution())) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
retList.add(stream);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (videoOnlyStreams != null) {
|
|
||||||
for (final VideoStream stream : videoOnlyStreams) {
|
|
||||||
if (!showHigherResolutions && HIGH_RESOLUTION_LIST.contains(
|
|
||||||
stream.getResolution())) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
retList.add(stream);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if (videoOnlyStreams != null) {
|
|
||||||
for (final VideoStream stream : videoOnlyStreams) {
|
|
||||||
if (!showHigherResolutions && HIGH_RESOLUTION_LIST.contains(
|
|
||||||
stream.getResolution())) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
retList.add(stream);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (videoStreams != null) {
|
|
||||||
for (final VideoStream stream : videoStreams) {
|
|
||||||
if (!showHigherResolutions && HIGH_RESOLUTION_LIST.contains(
|
|
||||||
stream.getResolution())) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
retList.add(stream);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Add all to the hashmap
|
// Add all to the hashmap
|
||||||
for (final VideoStream videoStream : retList) {
|
for (final VideoStream videoStream : allInitialStreams) {
|
||||||
hashMap.put(videoStream.getResolution(), videoStream);
|
hashMap.put(videoStream.getResolution(), videoStream);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Override the values when the key == resolution, with the defaultFormat
|
// Override the values when the key == resolution, with the defaultFormat
|
||||||
for (final VideoStream videoStream : retList) {
|
for (final VideoStream videoStream : allInitialStreams) {
|
||||||
if (videoStream.getFormat() == defaultFormat) {
|
if (videoStream.getFormat() == defaultFormat) {
|
||||||
hashMap.put(videoStream.getResolution(), videoStream);
|
hashMap.put(videoStream.getResolution(), videoStream);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
retList.clear();
|
// Return the sorted list
|
||||||
retList.addAll(hashMap.values());
|
return sortStreamList(new ArrayList<>(hashMap.values()), ascendingOrder);
|
||||||
sortStreamList(retList, ascendingOrder);
|
|
||||||
return retList;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -293,11 +271,13 @@ public final class ListHelper {
|
||||||
*
|
*
|
||||||
* @param videoStreams list that the sorting will be applied
|
* @param videoStreams list that the sorting will be applied
|
||||||
* @param ascendingOrder true -> smallest to greatest | false -> greatest to smallest
|
* @param ascendingOrder true -> smallest to greatest | false -> greatest to smallest
|
||||||
|
* @return The sorted list (same reference as parameter videoStreams)
|
||||||
*/
|
*/
|
||||||
private static void sortStreamList(final List<VideoStream> videoStreams,
|
private static List<VideoStream> sortStreamList(final List<VideoStream> videoStreams,
|
||||||
final boolean ascendingOrder) {
|
final boolean ascendingOrder) {
|
||||||
final Comparator<VideoStream> comparator = ListHelper::compareVideoStreamResolution;
|
final Comparator<VideoStream> comparator = ListHelper::compareVideoStreamResolution;
|
||||||
Collections.sort(videoStreams, ascendingOrder ? comparator : comparator.reversed());
|
Collections.sort(videoStreams, ascendingOrder ? comparator : comparator.reversed());
|
||||||
|
return videoStreams;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in a new issue