From 5debf5a14a95016f955f47658d4bc6390a6c2381 Mon Sep 17 00:00:00 2001 From: olly Date: Mon, 23 Jan 2017 04:19:28 -0800 Subject: [PATCH] Use bitrate as fixed track selection tie breaker If we don't have resolutions (and therefore cannot determine pixel counts) then use bitrate as a tie breaker instead. Also use pixel count as a tie breaker if pixel counts are known but equal. Streams with known pixel counts will always be preferred over streams without. Issue: #2343 ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=145269968 --- .../trackselection/DefaultTrackSelector.java | 32 ++++++++++++------- 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/library/src/main/java/com/google/android/exoplayer2/trackselection/DefaultTrackSelector.java b/library/src/main/java/com/google/android/exoplayer2/trackselection/DefaultTrackSelector.java index 79979401f7f..f62d5d90753 100644 --- a/library/src/main/java/com/google/android/exoplayer2/trackselection/DefaultTrackSelector.java +++ b/library/src/main/java/com/google/android/exoplayer2/trackselection/DefaultTrackSelector.java @@ -560,6 +560,7 @@ private static TrackSelection selectFixedVideoTrack(TrackGroupArray groups, TrackGroup selectedGroup = null; int selectedTrackIndex = 0; int selectedTrackScore = 0; + int selectedBitrate = Format.NO_VALUE; int selectedPixelCount = Format.NO_VALUE; for (int groupIndex = 0; groupIndex < groups.length; groupIndex++) { TrackGroup trackGroup = groups.get(groupIndex); @@ -582,16 +583,24 @@ private static TrackSelection selectFixedVideoTrack(TrackGroupArray groups, } boolean selectTrack = trackScore > selectedTrackScore; if (trackScore == selectedTrackScore) { - // Use the pixel count as a tie breaker. If we're within constraints prefer a higher - // pixel count, else prefer a lower count. If still tied then prefer the first track - // (i.e. the one that's already selected). - int pixelComparison = comparePixelCounts(format.getPixelCount(), selectedPixelCount); - selectTrack = isWithinConstraints ? pixelComparison > 0 : pixelComparison < 0; + // Use the pixel count as a tie breaker (or bitrate if pixel counts are tied). If we're + // within constraints prefer a higher pixel count (or bitrate), else prefer a lower + // count (or bitrate). If still tied then prefer the first track (i.e. the one that's + // already selected). + int comparisonResult; + int formatPixelCount = format.getPixelCount(); + if (formatPixelCount != selectedPixelCount) { + comparisonResult = compareFormatValues(format.getPixelCount(), selectedPixelCount); + } else { + comparisonResult = compareFormatValues(format.bitrate, selectedBitrate); + } + selectTrack = isWithinConstraints ? comparisonResult > 0 : comparisonResult < 0; } if (selectTrack) { selectedGroup = trackGroup; selectedTrackIndex = trackIndex; selectedTrackScore = trackScore; + selectedBitrate = format.bitrate; selectedPixelCount = format.getPixelCount(); } } @@ -602,20 +611,19 @@ private static TrackSelection selectFixedVideoTrack(TrackGroupArray groups, } /** - * Compares two pixel counts for order. A known pixel count is considered greater than + * Compares two format values for order. A known value is considered greater than * {@link Format#NO_VALUE}. * - * @param first The first pixel count. - * @param second The second pixel count. - * @return A negative integer if the first pixel count is less than the second. Zero if they are - * equal. A positive integer if the first pixel count is greater than the second. + * @param first The first value. + * @param second The second value. + * @return A negative integer if the first value is less than the second. Zero if they are equal. + * A positive integer if the first value is greater than the second. */ - private static int comparePixelCounts(int first, int second) { + private static int compareFormatValues(int first, int second) { return first == Format.NO_VALUE ? (second == Format.NO_VALUE ? 0 : -1) : (second == Format.NO_VALUE ? 1 : (first - second)); } - // Audio track selection implementation. protected TrackSelection selectAudioTrack(TrackGroupArray groups, int[][] formatSupport,