NewPipe/app/src/main/java/org/schabi/newpipe/player/playback/CustomTrackSelector.java

93 lines
3.8 KiB
Java
Raw Normal View History

2019-04-28 01:45:19 +02:00
package org.schabi.newpipe.player.playback;
2020-06-06 15:29:52 +02:00
import android.content.Context;
2019-04-28 01:45:19 +02:00
import android.text.TextUtils;
import android.util.Pair;
2019-10-07 07:54:31 +02:00
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
2019-04-28 01:45:19 +02:00
import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.Format;
2020-05-01 11:58:24 +02:00
import com.google.android.exoplayer2.RendererCapabilities.Capabilities;
2019-04-28 01:45:19 +02:00
import com.google.android.exoplayer2.source.TrackGroup;
import com.google.android.exoplayer2.source.TrackGroupArray;
import com.google.android.exoplayer2.trackselection.DefaultTrackSelector;
2021-08-28 19:41:58 +02:00
import com.google.android.exoplayer2.trackselection.ExoTrackSelection;
2019-04-28 01:45:19 +02:00
import com.google.android.exoplayer2.util.Assertions;
/**
* This class allows irregular text language labels for use when selecting text captions and
* is mostly a copy-paste from {@link DefaultTrackSelector}.
* <p>
2019-04-28 01:45:19 +02:00
* This is a hack and should be removed once ExoPlayer fixes language normalization to accept
* a broader set of languages.
* </p>
*/
2019-04-28 01:45:19 +02:00
public class CustomTrackSelector extends DefaultTrackSelector {
private String preferredTextLanguage;
2020-06-06 15:29:52 +02:00
public CustomTrackSelector(final Context context,
2021-08-28 19:41:58 +02:00
final ExoTrackSelection.Factory adaptiveTrackSelectionFactory) {
2020-06-06 15:29:52 +02:00
super(context, adaptiveTrackSelectionFactory);
2019-04-28 01:45:19 +02:00
}
private static boolean formatHasLanguage(final Format format, final String language) {
return language != null && TextUtils.equals(language, format.language);
}
2019-04-28 01:45:19 +02:00
public String getPreferredTextLanguage() {
return preferredTextLanguage;
}
public void setPreferredTextLanguage(@NonNull final String label) {
Assertions.checkNotNull(label);
if (!label.equals(preferredTextLanguage)) {
preferredTextLanguage = label;
invalidate();
}
}
@Override
2019-10-07 07:54:31 +02:00
@Nullable
2021-08-28 19:41:58 +02:00
protected Pair<ExoTrackSelection.Definition, TextTrackScore> selectTextTrack(
2020-05-01 11:58:24 +02:00
final TrackGroupArray groups,
@NonNull final int[][] formatSupport,
@NonNull final Parameters params,
@Nullable final String selectedAudioLanguage) {
2019-04-28 01:45:19 +02:00
TrackGroup selectedGroup = null;
2019-10-07 07:54:31 +02:00
int selectedTrackIndex = C.INDEX_UNSET;
TextTrackScore selectedTrackScore = null;
2020-05-01 11:58:24 +02:00
2019-04-28 01:45:19 +02:00
for (int groupIndex = 0; groupIndex < groups.length; groupIndex++) {
2020-08-16 10:24:58 +02:00
final TrackGroup trackGroup = groups.get(groupIndex);
@Capabilities final int[] trackFormatSupport = formatSupport[groupIndex];
2020-05-01 11:58:24 +02:00
2019-04-28 01:45:19 +02:00
for (int trackIndex = 0; trackIndex < trackGroup.length; trackIndex++) {
if (isSupported(trackFormatSupport[trackIndex],
params.exceedRendererCapabilitiesIfNecessary)) {
2020-08-16 10:24:58 +02:00
final Format format = trackGroup.getFormat(trackIndex);
final TextTrackScore trackScore = new TextTrackScore(format, params,
trackFormatSupport[trackIndex], selectedAudioLanguage);
2020-05-01 11:58:24 +02:00
if (formatHasLanguage(format, preferredTextLanguage)) {
2019-10-07 07:54:31 +02:00
selectedGroup = trackGroup;
selectedTrackIndex = trackIndex;
selectedTrackScore = trackScore;
2020-05-01 11:58:24 +02:00
break; // found user selected match (perfect!)
} else if (trackScore.isWithinConstraints && (selectedTrackScore == null
|| trackScore.compareTo(selectedTrackScore) > 0)) {
2019-04-28 01:45:19 +02:00
selectedGroup = trackGroup;
selectedTrackIndex = trackIndex;
selectedTrackScore = trackScore;
}
}
}
}
return selectedGroup == null ? null
2021-08-28 19:41:58 +02:00
: Pair.create(new ExoTrackSelection.Definition(selectedGroup, selectedTrackIndex),
Assertions.checkNotNull(selectedTrackScore));
2019-04-28 01:45:19 +02:00
}
}