Commit 6f6f381b by olly Committed by Oliver Woodman

Fix audio and text track selection in the multi-renderer case

If we can select a track that has a strictly higher score than a
selection already made for a renderer of the same type, we should
prefer it.

Issue: #4711

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=212835895
parent 3e4115ff
...@@ -124,6 +124,9 @@ ...@@ -124,6 +124,9 @@
* Fix bugs reporting events for multi-period media sources * Fix bugs reporting events for multi-period media sources
([#4492](https://github.com/google/ExoPlayer/issues/4492) and ([#4492](https://github.com/google/ExoPlayer/issues/4492) and
[#4634](https://github.com/google/ExoPlayer/issues/4634)). [#4634](https://github.com/google/ExoPlayer/issues/4634)).
* Fix issue where the preferred audio or text track would not be selected if
mapped onto a secondary renderer of the corresponding type
([#4711](http://github.com/google/ExoPlayer/issues/4711)).
* Fix issue where errors of upcoming playlist items are thrown too early * Fix issue where errors of upcoming playlist items are thrown too early
([#4661](https://github.com/google/ExoPlayer/issues/4661)). ([#4661](https://github.com/google/ExoPlayer/issues/4661)).
* Allow edit lists which do not start with a sync sample. * Allow edit lists which do not start with a sync sample.
......
...@@ -1318,8 +1318,10 @@ public class DefaultTrackSelector extends MappingTrackSelector { ...@@ -1318,8 +1318,10 @@ public class DefaultTrackSelector extends MappingTrackSelector {
} }
} }
boolean selectedAudioTracks = false; AudioTrackScore selectedAudioTrackScore = null;
boolean selectedTextTracks = false; int selectedAudioRendererIndex = C.INDEX_UNSET;
int selectedTextTrackScore = Integer.MIN_VALUE;
int selectedTextRendererIndex = C.INDEX_UNSET;
for (int i = 0; i < rendererCount; i++) { for (int i = 0; i < rendererCount; i++) {
int trackType = mappedTrackInfo.getRendererType(i); int trackType = mappedTrackInfo.getRendererType(i);
switch (trackType) { switch (trackType) {
...@@ -1327,23 +1329,38 @@ public class DefaultTrackSelector extends MappingTrackSelector { ...@@ -1327,23 +1329,38 @@ public class DefaultTrackSelector extends MappingTrackSelector {
// Already done. Do nothing. // Already done. Do nothing.
break; break;
case C.TRACK_TYPE_AUDIO: case C.TRACK_TYPE_AUDIO:
if (!selectedAudioTracks) { Pair<TrackSelection, AudioTrackScore> audioSelection =
rendererTrackSelections[i] = selectAudioTrack(
selectAudioTrack( mappedTrackInfo.getTrackGroups(i),
mappedTrackInfo.getTrackGroups(i), rendererFormatSupports[i],
rendererFormatSupports[i], rendererMixedMimeTypeAdaptationSupports[i],
rendererMixedMimeTypeAdaptationSupports[i], params,
params, seenVideoRendererWithMappedTracks ? null : adaptiveTrackSelectionFactory);
seenVideoRendererWithMappedTracks ? null : adaptiveTrackSelectionFactory); if (audioSelection != null
selectedAudioTracks = rendererTrackSelections[i] != null; && (selectedAudioTrackScore == null
|| audioSelection.second.compareTo(selectedAudioTrackScore) > 0)) {
if (selectedAudioRendererIndex != C.INDEX_UNSET) {
// We've already made a selection for another audio renderer, but it had a lower
// score. Clear the selection for that renderer.
rendererTrackSelections[selectedAudioRendererIndex] = null;
}
rendererTrackSelections[i] = audioSelection.first;
selectedAudioTrackScore = audioSelection.second;
selectedAudioRendererIndex = i;
} }
break; break;
case C.TRACK_TYPE_TEXT: case C.TRACK_TYPE_TEXT:
if (!selectedTextTracks) { Pair<TrackSelection, Integer> textSelection =
rendererTrackSelections[i] = selectTextTrack(mappedTrackInfo.getTrackGroups(i), rendererFormatSupports[i], params);
selectTextTrack( if (textSelection != null && textSelection.second > selectedTextTrackScore) {
mappedTrackInfo.getTrackGroups(i), rendererFormatSupports[i], params); if (selectedTextRendererIndex != C.INDEX_UNSET) {
selectedTextTracks = rendererTrackSelections[i] != null; // We've already made a selection for another text renderer, but it had a lower score.
// Clear the selection for that renderer.
rendererTrackSelections[selectedTextRendererIndex] = null;
}
rendererTrackSelections[i] = textSelection.first;
selectedTextTrackScore = textSelection.second;
selectedTextRendererIndex = i;
} }
break; break;
default: default:
...@@ -1599,10 +1616,11 @@ public class DefaultTrackSelector extends MappingTrackSelector { ...@@ -1599,10 +1616,11 @@ public class DefaultTrackSelector extends MappingTrackSelector {
* @param params The selector's current constraint parameters. * @param params The selector's current constraint parameters.
* @param adaptiveTrackSelectionFactory A factory for generating adaptive track selections, or * @param adaptiveTrackSelectionFactory A factory for generating adaptive track selections, or
* null if a fixed track selection is required. * null if a fixed track selection is required.
* @return The {@link TrackSelection} for the renderer, or null if no selection was made. * @return The {@link TrackSelection} and corresponding {@link AudioTrackScore}, or null if no
* selection was made.
* @throws ExoPlaybackException If an error occurs while selecting the tracks. * @throws ExoPlaybackException If an error occurs while selecting the tracks.
*/ */
protected @Nullable TrackSelection selectAudioTrack( protected @Nullable Pair<TrackSelection, AudioTrackScore> selectAudioTrack(
TrackGroupArray groups, TrackGroupArray groups,
int[][] formatSupports, int[][] formatSupports,
int mixedMimeTypeAdaptationSupports, int mixedMimeTypeAdaptationSupports,
...@@ -1635,6 +1653,8 @@ public class DefaultTrackSelector extends MappingTrackSelector { ...@@ -1635,6 +1653,8 @@ public class DefaultTrackSelector extends MappingTrackSelector {
} }
TrackGroup selectedGroup = groups.get(selectedGroupIndex); TrackGroup selectedGroup = groups.get(selectedGroupIndex);
TrackSelection selection = null;
if (!params.forceHighestSupportedBitrate if (!params.forceHighestSupportedBitrate
&& !params.forceLowestBitrate && !params.forceLowestBitrate
&& adaptiveTrackSelectionFactory != null) { && adaptiveTrackSelectionFactory != null) {
...@@ -1643,11 +1663,17 @@ public class DefaultTrackSelector extends MappingTrackSelector { ...@@ -1643,11 +1663,17 @@ public class DefaultTrackSelector extends MappingTrackSelector {
getAdaptiveAudioTracks( getAdaptiveAudioTracks(
selectedGroup, formatSupports[selectedGroupIndex], params.allowMixedMimeAdaptiveness); selectedGroup, formatSupports[selectedGroupIndex], params.allowMixedMimeAdaptiveness);
if (adaptiveTracks.length > 0) { if (adaptiveTracks.length > 0) {
return adaptiveTrackSelectionFactory selection =
.createTrackSelection(selectedGroup, getBandwidthMeter(), adaptiveTracks); adaptiveTrackSelectionFactory.createTrackSelection(
selectedGroup, getBandwidthMeter(), adaptiveTracks);
} }
} }
return new FixedTrackSelection(selectedGroup, selectedTrackIndex); if (selection == null) {
// We didn't make an adaptive selection, so make a fixed one instead.
selection = new FixedTrackSelection(selectedGroup, selectedTrackIndex);
}
return Pair.create(selection, Assertions.checkNotNull(selectedTrackScore));
} }
private static int[] getAdaptiveAudioTracks(TrackGroup group, int[] formatSupport, private static int[] getAdaptiveAudioTracks(TrackGroup group, int[] formatSupport,
...@@ -1712,10 +1738,11 @@ public class DefaultTrackSelector extends MappingTrackSelector { ...@@ -1712,10 +1738,11 @@ public class DefaultTrackSelector extends MappingTrackSelector {
* @param formatSupport The result of {@link RendererCapabilities#supportsFormat} for each mapped * @param formatSupport The result of {@link RendererCapabilities#supportsFormat} for each mapped
* track, indexed by track group index and track index (in that order). * track, indexed by track group index and track index (in that order).
* @param params The selector's current constraint parameters. * @param params The selector's current constraint parameters.
* @return The {@link TrackSelection} for the renderer, or null if no selection was made. * @return The {@link TrackSelection} and corresponding track score, or null if no selection was
* made.
* @throws ExoPlaybackException If an error occurs while selecting the tracks. * @throws ExoPlaybackException If an error occurs while selecting the tracks.
*/ */
protected @Nullable TrackSelection selectTextTrack( protected @Nullable Pair<TrackSelection, Integer> selectTextTrack(
TrackGroupArray groups, int[][] formatSupport, Parameters params) TrackGroupArray groups, int[][] formatSupport, Parameters params)
throws ExoPlaybackException { throws ExoPlaybackException {
TrackGroup selectedGroup = null; TrackGroup selectedGroup = null;
...@@ -1770,8 +1797,10 @@ public class DefaultTrackSelector extends MappingTrackSelector { ...@@ -1770,8 +1797,10 @@ public class DefaultTrackSelector extends MappingTrackSelector {
} }
} }
} }
return selectedGroup == null ? null return selectedGroup == null
: new FixedTrackSelection(selectedGroup, selectedTrackIndex); ? null
: Pair.create(
new FixedTrackSelection(selectedGroup, selectedTrackIndex), selectedTrackScore);
} }
// General track selection methods. // General track selection methods.
...@@ -2032,12 +2061,9 @@ public class DefaultTrackSelector extends MappingTrackSelector { ...@@ -2032,12 +2061,9 @@ public class DefaultTrackSelector extends MappingTrackSelector {
} }
} }
/** /** Represents how well an audio track matches the selection {@link Parameters}. */
* A representation of how well a track fits with our track selection {@link Parameters}.
*
* <p>This is used to rank different audio tracks relatively with each other.
*/
private static final class AudioTrackScore implements Comparable<AudioTrackScore> { private static final class AudioTrackScore implements Comparable<AudioTrackScore> {
private final Parameters parameters; private final Parameters parameters;
private final int withinRendererCapabilitiesScore; private final int withinRendererCapabilitiesScore;
private final int matchLanguageScore; private final int matchLanguageScore;
...@@ -2057,7 +2083,7 @@ public class DefaultTrackSelector extends MappingTrackSelector { ...@@ -2057,7 +2083,7 @@ public class DefaultTrackSelector extends MappingTrackSelector {
} }
/** /**
* Compares the score of the current track format with another {@link AudioTrackScore}. * Compares this score with another.
* *
* @param other The other score to compare to. * @param other The other score to compare to.
* @return A positive integer if this score is better than the other. Zero if they are equal. A * @return A positive integer if this score is better than the other. Zero if they are equal. A
...@@ -2086,35 +2112,6 @@ public class DefaultTrackSelector extends MappingTrackSelector { ...@@ -2086,35 +2112,6 @@ public class DefaultTrackSelector extends MappingTrackSelector {
return resultSign * compareInts(this.bitrate, other.bitrate); return resultSign * compareInts(this.bitrate, other.bitrate);
} }
} }
@Override
public boolean equals(@Nullable Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
AudioTrackScore that = (AudioTrackScore) o;
return withinRendererCapabilitiesScore == that.withinRendererCapabilitiesScore
&& matchLanguageScore == that.matchLanguageScore
&& defaultSelectionFlagScore == that.defaultSelectionFlagScore
&& channelCount == that.channelCount && sampleRate == that.sampleRate
&& bitrate == that.bitrate;
}
@Override
public int hashCode() {
int result = withinRendererCapabilitiesScore;
result = 31 * result + matchLanguageScore;
result = 31 * result + defaultSelectionFlagScore;
result = 31 * result + channelCount;
result = 31 * result + sampleRate;
result = 31 * result + bitrate;
return result;
}
} }
/** /**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment