From c539db68ca4c03a3f3907f5d3086b1b0ea53a74c Mon Sep 17 00:00:00 2001 From: aquilescanta Date: Mon, 23 May 2016 04:27:15 -0700 Subject: [PATCH] Add first default track selection policies With this CL: * The first supported video track found is selected. * The first supported audio track with preferred language is selected. If none, we fallback to the first supported one. * For text, we only present a selection if one of the tracks has the preferred language and the track is flagged as forced. TODO list: * Add a selection policy for video (probably related with adaptiveness). * We should decide what to do with the default flag. * Perhaps, if no audio with the preferred language(assuming there is one) is found, we should fall back to a text track that has the preferred language. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=122985006 --- .../DefaultTrackSelectionPolicy.java | 82 +++++++++++++++++-- 1 file changed, 75 insertions(+), 7 deletions(-) diff --git a/library/src/main/java/com/google/android/exoplayer/DefaultTrackSelectionPolicy.java b/library/src/main/java/com/google/android/exoplayer/DefaultTrackSelectionPolicy.java index 91ccab89ee..0b95696d89 100644 --- a/library/src/main/java/com/google/android/exoplayer/DefaultTrackSelectionPolicy.java +++ b/library/src/main/java/com/google/android/exoplayer/DefaultTrackSelectionPolicy.java @@ -15,32 +15,78 @@ */ package com.google.android.exoplayer; +import com.google.android.exoplayer.util.Util; + /** * A {@link TrackSelectionPolicy} that allows configuration of common parameters. */ public class DefaultTrackSelectionPolicy extends TrackSelectionPolicy { + private String preferredLanguage; + + public void setPreferredLanguage(String preferredLanguage) { + if (!Util.areEqual(this.preferredLanguage, preferredLanguage)) { + this.preferredLanguage = preferredLanguage; + invalidate(); + } + } + @Override public TrackSelection[] selectTracks(TrackRenderer[] renderers, TrackGroupArray[] rendererTrackGroupArrays, int[][][] rendererFormatSupports) { // Make a track selection for each renderer. TrackSelection[] rendererTrackSelections = new TrackSelection[renderers.length]; for (int i = 0; i < renderers.length; i++) { - rendererTrackSelections[i] = selectTracksForRenderer(rendererTrackGroupArrays[i], - rendererFormatSupports[i]); + switch (renderers[i].getTrackType()) { + case C.TRACK_TYPE_AUDIO: + rendererTrackSelections[i] = selectTrackForAudioRenderer( + rendererTrackGroupArrays[i], rendererFormatSupports[i], preferredLanguage); + break; + case C.TRACK_TYPE_TEXT: + rendererTrackSelections[i] = selectTrackForTextRenderer(rendererTrackGroupArrays[i], + rendererFormatSupports[i], preferredLanguage); + break; + default: + rendererTrackSelections[i] = selectFirstSupportedTrack(rendererTrackGroupArrays[i], + rendererFormatSupports[i]); + break; + } } return rendererTrackSelections; } - private static TrackSelection selectTracksForRenderer(TrackGroupArray trackGroups, - int[][] formatSupport) { - // TODO: Allow more specific track selection parameters. + private TrackSelection selectTrackForTextRenderer(TrackGroupArray trackGroups, + int[][] formatSupport, String preferredLanguage) { + int firstForcedGroup = -1; + int firstForcedTrack = -1; for (int groupIndex = 0; groupIndex < trackGroups.length; groupIndex++) { TrackGroup trackGroup = trackGroups.get(groupIndex); int[] trackFormatSupport = formatSupport[groupIndex]; for (int trackIndex = 0; trackIndex < trackGroup.length; trackIndex++) { - if ((trackFormatSupport[trackIndex] & TrackRenderer.FORMAT_SUPPORT_MASK) - == TrackRenderer.FORMAT_HANDLED) { + if (isSupported(trackFormatSupport[trackIndex]) + && (trackGroup.getFormat(trackIndex).selectionFlags + & Format.SELECTION_FLAG_FORCED) != 0) { + if (firstForcedGroup == -1) { + firstForcedGroup = groupIndex; + firstForcedTrack = trackIndex; + } + if (preferredLanguage != null + && preferredLanguage.equals(trackGroup.getFormat(trackIndex).language)) { + return new TrackSelection(groupIndex, trackIndex); + } + } + } + } + return firstForcedGroup != -1 ? new TrackSelection(firstForcedGroup, firstForcedTrack) : null; + } + + private static TrackSelection selectFirstSupportedTrack(TrackGroupArray trackGroups, + int[][] formatSupport) { + for (int groupIndex = 0; groupIndex < trackGroups.length; groupIndex++) { + TrackGroup trackGroup = trackGroups.get(groupIndex); + int[] trackFormatSupport = formatSupport[groupIndex]; + for (int trackIndex = 0; trackIndex < trackGroup.length; trackIndex++) { + if (isSupported(trackFormatSupport[trackIndex])) { return new TrackSelection(groupIndex, trackIndex); } } @@ -48,5 +94,27 @@ public class DefaultTrackSelectionPolicy extends TrackSelectionPolicy { return null; } + private static TrackSelection selectTrackForAudioRenderer(TrackGroupArray trackGroups, + int[][] formatSupport, String preferredLanguage) { + if (preferredLanguage != null) { + for (int groupIndex = 0; groupIndex < trackGroups.length; groupIndex++) { + TrackGroup trackGroup = trackGroups.get(groupIndex); + int[] trackFormatSupport = formatSupport[groupIndex]; + for (int trackIndex = 0; trackIndex < trackGroup.length; trackIndex++) { + if (isSupported(trackFormatSupport[trackIndex]) + && preferredLanguage.equals(trackGroup.getFormat(trackIndex).language)) { + return new TrackSelection(groupIndex, trackIndex); + } + } + } + } + // No preferred language was selected or no audio track presented the preferred language. + return selectFirstSupportedTrack(trackGroups, formatSupport); + } + + private static boolean isSupported(int formatSupport) { + return (formatSupport & TrackRenderer.FORMAT_SUPPORT_MASK) == TrackRenderer.FORMAT_HANDLED; + } + }