@@ -343,7 +343,10 @@ static List<VideoStream> getSortedStreamVideosList(
343343 */
344344 private static List <VideoStream > sortStreamList (final List <VideoStream > videoStreams ,
345345 final boolean ascendingOrder ) {
346- final Comparator <VideoStream > comparator = ListHelper ::compareVideoStreamResolution ;
346+ // Compares the quality of two video streams.
347+ final Comparator <VideoStream > comparator = Comparator .nullsLast (Comparator
348+ .comparing (VideoStream ::getResolution , ListHelper ::compareVideoStreamResolution )
349+ .thenComparingInt (s -> VIDEO_FORMAT_QUALITY_RANKING .indexOf (s .getFormat ())));
347350 Collections .sort (videoStreams , ascendingOrder ? comparator : comparator .reversed ());
348351 return videoStreams ;
349352 }
@@ -360,8 +363,7 @@ static int getHighestQualityAudioIndex(@Nullable final MediaFormat format,
360363 @ Nullable final List <AudioStream > audioStreams ) {
361364 return getAudioIndexByHighestRank (format , audioStreams ,
362365 // Compares descending (last = highest rank)
363- (s1 , s2 ) -> compareAudioStreamBitrate (s1 , s2 , AUDIO_FORMAT_QUALITY_RANKING )
364- );
366+ getAudioStreamComparator (AUDIO_FORMAT_QUALITY_RANKING ));
365367 }
366368
367369 /**
@@ -374,11 +376,15 @@ static int getHighestQualityAudioIndex(@Nullable final MediaFormat format,
374376 */
375377 static int getMostCompactAudioIndex (@ Nullable final MediaFormat format ,
376378 @ Nullable final List <AudioStream > audioStreams ) {
377-
378379 return getAudioIndexByHighestRank (format , audioStreams ,
379- // The "-" is important -> Compares ascending (first = highest rank)
380- (s1 , s2 ) -> -compareAudioStreamBitrate (s1 , s2 , AUDIO_FORMAT_EFFICIENCY_RANKING )
381- );
380+ // The "reversed()" is important -> Compares ascending (first = highest rank)
381+ getAudioStreamComparator (AUDIO_FORMAT_EFFICIENCY_RANKING ).reversed ());
382+ }
383+
384+ private static Comparator <AudioStream > getAudioStreamComparator (
385+ final List <MediaFormat > formatRanking ) {
386+ return Comparator .nullsLast (Comparator .comparingInt (AudioStream ::getAverageBitrate ))
387+ .thenComparingInt (stream -> formatRanking .indexOf (stream .getFormat ()));
382388 }
383389
384390 /**
@@ -544,28 +550,6 @@ private static MediaFormat getMediaFormatFromKey(@NonNull final Context context,
544550 return format ;
545551 }
546552
547- // Compares the quality of two audio streams
548- private static int compareAudioStreamBitrate (final AudioStream streamA ,
549- final AudioStream streamB ,
550- final List <MediaFormat > formatRanking ) {
551- if (streamA == null ) {
552- return -1 ;
553- }
554- if (streamB == null ) {
555- return 1 ;
556- }
557- if (streamA .getAverageBitrate () < streamB .getAverageBitrate ()) {
558- return -1 ;
559- }
560- if (streamA .getAverageBitrate () > streamB .getAverageBitrate ()) {
561- return 1 ;
562- }
563-
564- // Same bitrate and format
565- return formatRanking .indexOf (streamA .getFormat ())
566- - formatRanking .indexOf (streamB .getFormat ());
567- }
568-
569553 private static int compareVideoStreamResolution (@ NonNull final String r1 ,
570554 @ NonNull final String r2 ) {
571555 try {
@@ -582,28 +566,6 @@ private static int compareVideoStreamResolution(@NonNull final String r1,
582566 }
583567 }
584568
585- // Compares the quality of two video streams.
586- private static int compareVideoStreamResolution (final VideoStream streamA ,
587- final VideoStream streamB ) {
588- if (streamA == null ) {
589- return -1 ;
590- }
591- if (streamB == null ) {
592- return 1 ;
593- }
594-
595- final int resComp = compareVideoStreamResolution (streamA .getResolution (),
596- streamB .getResolution ());
597- if (resComp != 0 ) {
598- return resComp ;
599- }
600-
601- // Same bitrate and format
602- return ListHelper .VIDEO_FORMAT_QUALITY_RANKING .indexOf (streamA .getFormat ())
603- - ListHelper .VIDEO_FORMAT_QUALITY_RANKING .indexOf (streamB .getFormat ());
604- }
605-
606-
607569 private static boolean isLimitingDataUsage (final Context context ) {
608570 return getResolutionLimit (context ) != null ;
609571 }
0 commit comments