From b0bb6ec8a5b5ba0e4645244fa517830264f66b80 Mon Sep 17 00:00:00 2001 From: tonihei Date: Mon, 25 Mar 2019 11:35:44 +0000 Subject: [PATCH] Allow to set selection reason and data when specifying an override. It's currently very difficult to actually set a reason for track selection triggered by a SelectionOverride. It would require creating a new custom TrackSelection.Factory where the reason gets injected somehow before the override is specified on the player. Simplify this whole procedure by allowing to set the reason directly and forward this reason to all fixed selections created based on this override. PiperOrigin-RevId: 240114942 --- RELEASENOTES.md | 1 + .../AdaptiveTrackSelection.java | 4 ++- .../trackselection/DefaultTrackSelector.java | 32 +++++++++++++++++-- .../trackselection/TrackSelection.java | 16 ++++++++++ .../trackselection/TrackSelectionUtil.java | 4 ++- 5 files changed, 52 insertions(+), 5 deletions(-) diff --git a/RELEASENOTES.md b/RELEASENOTES.md index 6158ea9e07..989d67a49a 100644 --- a/RELEASENOTES.md +++ b/RELEASENOTES.md @@ -22,6 +22,7 @@ ([#3314](https://github.com/google/ExoPlayer/issues/3314)). * Update `TrackSelection.Factory` interface to support creating all track selections together. + * Allow to specify a selection reason for a `SelectionOverride`. * Do not retry failed loads whose error is `FileNotFoundException`. * Support Dolby Vision extraction in MP4 and fMP4. * Offline: diff --git a/library/core/src/main/java/com/google/android/exoplayer2/trackselection/AdaptiveTrackSelection.java b/library/core/src/main/java/com/google/android/exoplayer2/trackselection/AdaptiveTrackSelection.java index 32e6a499cd..ba4dd6e8a9 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/trackselection/AdaptiveTrackSelection.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/trackselection/AdaptiveTrackSelection.java @@ -242,7 +242,9 @@ public class AdaptiveTrackSelection extends BaseTrackSelection { createAdaptiveTrackSelection(definition.group, bandwidthMeter, definition.tracks); selections[i] = adaptiveSelection; } else { - selections[i] = new FixedTrackSelection(definition.group, definition.tracks[0]); + selections[i] = + new FixedTrackSelection( + definition.group, definition.tracks[0], definition.reason, definition.data); int trackBitrate = definition.group.getFormat(definition.tracks[0]).bitrate; if (trackBitrate != Format.NO_VALUE) { totalFixedBandwidth += trackBitrate; diff --git a/library/core/src/main/java/com/google/android/exoplayer2/trackselection/DefaultTrackSelector.java b/library/core/src/main/java/com/google/android/exoplayer2/trackselection/DefaultTrackSelector.java index 0b510ea496..9852736e4d 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/trackselection/DefaultTrackSelector.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/trackselection/DefaultTrackSelector.java @@ -1173,15 +1173,29 @@ public class DefaultTrackSelector extends MappingTrackSelector { public final int groupIndex; public final int[] tracks; public final int length; + public final int reason; + public final int data; /** * @param groupIndex The overriding track group index. * @param tracks The overriding track indices within the track group. */ public SelectionOverride(int groupIndex, int... tracks) { + this(groupIndex, tracks, C.SELECTION_REASON_MANUAL, /* data= */ 0); + } + + /** + * @param groupIndex The overriding track group index. + * @param tracks The overriding track indices within the track group. + * @param reason The reason for the override. One of the {@link C} SELECTION_REASON_ constants. + * @param data Optional data associated with this override. + */ + public SelectionOverride(int groupIndex, int[] tracks, int reason, int data) { this.groupIndex = groupIndex; this.tracks = Arrays.copyOf(tracks, tracks.length); this.length = tracks.length; + this.reason = reason; + this.data = data; Arrays.sort(this.tracks); } @@ -1190,6 +1204,8 @@ public class DefaultTrackSelector extends MappingTrackSelector { length = in.readByte(); tracks = new int[length]; in.readIntArray(tracks); + reason = in.readInt(); + data = in.readInt(); } /** Returns whether this override contains the specified track index. */ @@ -1204,7 +1220,9 @@ public class DefaultTrackSelector extends MappingTrackSelector { @Override public int hashCode() { - return 31 * groupIndex + Arrays.hashCode(tracks); + int hash = 31 * groupIndex + Arrays.hashCode(tracks); + hash = 31 * hash + reason; + return 31 * hash + data; } @Override @@ -1216,7 +1234,10 @@ public class DefaultTrackSelector extends MappingTrackSelector { return false; } SelectionOverride other = (SelectionOverride) obj; - return groupIndex == other.groupIndex && Arrays.equals(tracks, other.tracks); + return groupIndex == other.groupIndex + && Arrays.equals(tracks, other.tracks) + && reason == other.reason + && data == other.data; } // Parcelable implementation. @@ -1231,6 +1252,8 @@ public class DefaultTrackSelector extends MappingTrackSelector { dest.writeInt(groupIndex); dest.writeInt(tracks.length); dest.writeIntArray(tracks); + dest.writeInt(reason); + dest.writeInt(data); } public static final Parcelable.Creator CREATOR = @@ -1405,7 +1428,10 @@ public class DefaultTrackSelector extends MappingTrackSelector { override == null ? null : new TrackSelection.Definition( - rendererTrackGroups.get(override.groupIndex), override.tracks); + rendererTrackGroups.get(override.groupIndex), + override.tracks, + override.reason, + override.data); } } diff --git a/library/core/src/main/java/com/google/android/exoplayer2/trackselection/TrackSelection.java b/library/core/src/main/java/com/google/android/exoplayer2/trackselection/TrackSelection.java index e244bf4a8c..bd99403b07 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/trackselection/TrackSelection.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/trackselection/TrackSelection.java @@ -43,6 +43,10 @@ public interface TrackSelection { public final TrackGroup group; /** The indices of the selected tracks in {@link #group}. */ public final int[] tracks; + /** The track selection reason. One of the {@link C} SELECTION_REASON_ constants. */ + public final int reason; + /** Optional data associated with this selection of tracks. */ + @Nullable public final Object data; /** * @param group The {@link TrackGroup}. Must not be null. @@ -50,8 +54,20 @@ public interface TrackSelection { * null or empty. May be in any order. */ public Definition(TrackGroup group, int... tracks) { + this(group, tracks, C.SELECTION_REASON_UNKNOWN, /* data= */ null); + } + + /** + * @param group The {@link TrackGroup}. Must not be null. + * @param tracks The indices of the selected tracks within the {@link TrackGroup}. Must not be + * @param reason The track selection reason. One of the {@link C} SELECTION_REASON_ constants. + * @param data Optional data associated with this selection of tracks. + */ + public Definition(TrackGroup group, int[] tracks, int reason, @Nullable Object data) { this.group = group; this.tracks = tracks; + this.reason = reason; + this.data = data; } } diff --git a/library/core/src/main/java/com/google/android/exoplayer2/trackselection/TrackSelectionUtil.java b/library/core/src/main/java/com/google/android/exoplayer2/trackselection/TrackSelectionUtil.java index 3fd763157e..71afd87b0f 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/trackselection/TrackSelectionUtil.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/trackselection/TrackSelectionUtil.java @@ -71,7 +71,9 @@ public final class TrackSelectionUtil { createdAdaptiveTrackSelection = true; selections[i] = adaptiveTrackSelectionFactory.createAdaptiveTrackSelection(definition); } else { - selections[i] = new FixedTrackSelection(definition.group, definition.tracks[0]); + selections[i] = + new FixedTrackSelection( + definition.group, definition.tracks[0], definition.reason, definition.data); } } return selections;