Improve code readability and fix an issue with text tracks that should not be selected

This commit is contained in:
Yannick RUI 2019-07-19 08:38:47 +02:00
parent 1909987dc8
commit 8b554dc30a

View File

@ -2076,7 +2076,8 @@ public class DefaultTrackSelector extends MappingTrackSelector {
params.exceedRendererCapabilitiesIfNecessary)) { params.exceedRendererCapabilitiesIfNecessary)) {
Format format = trackGroup.getFormat(trackIndex); Format format = trackGroup.getFormat(trackIndex);
TextTrackScore trackScore = new TextTrackScore(format, params, trackFormatSupport[trackIndex], selectedAudioLanguage); TextTrackScore trackScore = new TextTrackScore(format, params, trackFormatSupport[trackIndex], selectedAudioLanguage);
if ((selectedTrackScore == null) || trackScore.compareTo(selectedTrackScore) > 0) { if (trackScore.isWithinConstraints
&& ((selectedTrackScore == null) || trackScore.compareTo(selectedTrackScore) > 0)) {
selectedGroup = trackGroup; selectedGroup = trackGroup;
selectedTrackIndex = trackIndex; selectedTrackIndex = trackIndex;
selectedTrackScore = trackScore; selectedTrackScore = trackScore;
@ -2514,8 +2515,9 @@ public class DefaultTrackSelector extends MappingTrackSelector {
private final int preferredLanguageScore; private final int preferredLanguageScore;
private final int selectedAudioLanguageScore; private final int selectedAudioLanguageScore;
private final boolean trackHasNoLanguage; private final boolean trackHasNoLanguage;
private final boolean selectUndeterminedTextLanguage; private final boolean hasLanguageMatch;
private final boolean stringDefinesNoLang; private final boolean hasSelectedAudioLanguageMatch;
private final boolean isWithinConstraints;
public TextTrackScore( public TextTrackScore(
Format format, Format format,
@ -2525,13 +2527,17 @@ public class DefaultTrackSelector extends MappingTrackSelector {
isWithinRendererCapabilities = isSupported(trackFormatSupport, false); isWithinRendererCapabilities = isSupported(trackFormatSupport, false);
int maskedSelectionFlags = int maskedSelectionFlags =
format.selectionFlags & ~parameters.disabledTextTrackSelectionFlags; format.selectionFlags & ~parameters.disabledTextTrackSelectionFlags;
isDefault = (format.selectionFlags & C.SELECTION_FLAG_DEFAULT) != 0; isDefault = (maskedSelectionFlags & C.SELECTION_FLAG_DEFAULT) != 0;
isForced = (maskedSelectionFlags & C.SELECTION_FLAG_FORCED) != 0; isForced = (maskedSelectionFlags & C.SELECTION_FLAG_FORCED) != 0;
preferredLanguageScore = getFormatLanguageScore(format, parameters.preferredTextLanguage); preferredLanguageScore = getFormatLanguageScore(format, parameters.preferredTextLanguage);
selectedAudioLanguageScore = getFormatLanguageScore(format, selectedAudioLanguage); selectedAudioLanguageScore = getFormatLanguageScore(format, selectedAudioLanguage);
trackHasNoLanguage = formatHasNoLanguage(format); trackHasNoLanguage = formatHasNoLanguage(format);
selectUndeterminedTextLanguage = parameters.selectUndeterminedTextLanguage; hasLanguageMatch = preferredLanguageScore > 0
stringDefinesNoLang = stringDefinesNoLanguage(selectedAudioLanguage); || (parameters.selectUndeterminedTextLanguage && trackHasNoLanguage);
hasSelectedAudioLanguageMatch = (selectedAudioLanguageScore > 0)
|| (trackHasNoLanguage && stringDefinesNoLanguage(selectedAudioLanguage));
isWithinConstraints =
(hasLanguageMatch || isDefault || (isForced && hasSelectedAudioLanguageMatch));
} }
/** /**
@ -2546,34 +2552,26 @@ public class DefaultTrackSelector extends MappingTrackSelector {
if (this.isWithinRendererCapabilities != other.isWithinRendererCapabilities) { if (this.isWithinRendererCapabilities != other.isWithinRendererCapabilities) {
return this.isWithinRendererCapabilities ? 1 : -1; return this.isWithinRendererCapabilities ? 1 : -1;
} }
if ((this.preferredLanguageScore > 0 || (this.selectUndeterminedTextLanguage && this.trackHasNoLanguage)) == if (this.hasLanguageMatch != other.hasLanguageMatch) {
(other.preferredLanguageScore > 0 || (other.selectUndeterminedTextLanguage && other.trackHasNoLanguage))) { return this.hasLanguageMatch ? 1 : -1;
if (this.preferredLanguageScore > 0 || (this.selectUndeterminedTextLanguage }
&& this.trackHasNoLanguage)) {
if (this.isDefault != other.isDefault) { if (this.isDefault != other.isDefault) {
return this.isDefault ? 1 : -1; return this.isDefault ? 1 : -1;
} }
if (this.hasLanguageMatch) {
if (this.isForced != other.isForced) { if (this.isForced != other.isForced) {
// Prefer non-forced to forced if a preferred text language has been specified. Where // Prefer non-forced to forced if a preferred text language has been specified. Where
// both are provided the non-forced track will usually contain the forced subtitles as // both are provided the non-forced track will usually contain the forced subtitles as
// a subset. // a subset.
return !this.isForced ? 1 : -1; return !this.isForced ? 1 : -1;
} }
return (this.preferredLanguageScore > other.preferredLanguageScore) ? 1 : -1; return this.preferredLanguageScore - other.preferredLanguageScore;
} else { } else {
if (this.isDefault != other.isDefault) { if ((this.isForced && this.hasSelectedAudioLanguageMatch) !=
return this.isDefault ? 1 : -1; (other.isForced && other.hasSelectedAudioLanguageMatch)) {
return (this.isForced && this.hasSelectedAudioLanguageMatch) ? 1 : -1;
} }
if ((this.isForced && (this.selectedAudioLanguageScore > 0 || (this.trackHasNoLanguage && this.stringDefinesNoLang))) != return 0;
(other.isForced && (other.selectedAudioLanguageScore > 0 || (other.trackHasNoLanguage && other.stringDefinesNoLang)))) {
return (this.isForced && (this.selectedAudioLanguageScore > 0
|| (this.trackHasNoLanguage && this.stringDefinesNoLang))) ? 1 : -1;
}
// Track should not be selected.
return -1;
}
} else {
return (this.preferredLanguageScore > 0 || (this.selectUndeterminedTextLanguage && this.trackHasNoLanguage)) ? 1 : -1;
} }
} }
} }