Skip to content

Commit

Permalink
Requiring offload disables any tracks that are non-compatible
Browse files Browse the repository at this point in the history
With `AudioOffloadModePreference` `AUDIO_OFFLOAD_MODE_PREFERENCE_REQUIRED`, the `DefaultTrackSelector` will select a single audio track that it finds is offload compatible. If not any audio track is supported in offload, then no track will be selected.

PiperOrigin-RevId: 537877183
  • Loading branch information
microkatz authored and tof-tof committed Jun 6, 2023
1 parent 3d8a2f8 commit 68a7a11
Show file tree
Hide file tree
Showing 3 changed files with 239 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,18 +75,26 @@
public class TrackSelectionParameters implements Bundleable {

/**
* The preference level for enabling audio offload on the audio sink. Either {@link
* #AUDIO_OFFLOAD_MODE_PREFERENCE_ENABLED} or {@link #AUDIO_OFFLOAD_MODE_PREFERENCE_DISABLED}.
* The preference level for enabling audio offload on the audio sink. One of {@link
* #AUDIO_OFFLOAD_MODE_PREFERENCE_REQUIRED}, {@link #AUDIO_OFFLOAD_MODE_PREFERENCE_ENABLED}, or
* {@link #AUDIO_OFFLOAD_MODE_PREFERENCE_DISABLED}.
*/
@Documented
@Retention(RetentionPolicy.SOURCE)
@Target(TYPE_USE)
@IntDef({
AUDIO_OFFLOAD_MODE_PREFERENCE_REQUIRED,
AUDIO_OFFLOAD_MODE_PREFERENCE_ENABLED,
AUDIO_OFFLOAD_MODE_PREFERENCE_DISABLED,
})
public @interface AudioOffloadModePreference {}

/**
* The track selector will only select tracks that with the renderer capabilities provide an audio
* offload compatible playback scenario. If it is impossible to create an offload-compatible track
* selection, then no tracks will be selected.
*/
public static final int AUDIO_OFFLOAD_MODE_PREFERENCE_REQUIRED = 2;
/**
* The track selector will enable audio offload if the selected tracks and renderer capabilities
* are compatible.
Expand Down Expand Up @@ -982,9 +990,8 @@ public static TrackSelectionParameters getDefaults(Context context) {
public final ImmutableList<String> preferredAudioMimeTypes;

/**
* The preferred offload mode setting for audio playback. Either {@link
* #AUDIO_OFFLOAD_MODE_PREFERENCE_ENABLED} or {@link #AUDIO_OFFLOAD_MODE_PREFERENCE_DISABLED}. The
* default is {@code AUDIO_OFFLOAD_MODE_PREFERENCE_DISABLED}.
* The preferred offload mode setting for audio playback. The default is {@link
* #AUDIO_OFFLOAD_MODE_PREFERENCE_DISABLED}.
*/
public final @AudioOffloadModePreference int audioOffloadModePreference;
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import static com.google.android.exoplayer2.RendererCapabilities.AUDIO_OFFLOAD_NOT_SUPPORTED;
import static com.google.android.exoplayer2.RendererCapabilities.AUDIO_OFFLOAD_SPEED_CHANGE_SUPPORTED;
import static com.google.android.exoplayer2.trackselection.TrackSelectionParameters.AUDIO_OFFLOAD_MODE_PREFERENCE_DISABLED;
import static com.google.android.exoplayer2.trackselection.TrackSelectionParameters.AUDIO_OFFLOAD_MODE_PREFERENCE_REQUIRED;
import static com.google.android.exoplayer2.util.Assertions.checkStateNotNull;
import static com.google.android.exoplayer2.util.Util.castNonNull;
import static java.lang.annotation.ElementType.TYPE_USE;
Expand Down Expand Up @@ -2507,6 +2508,10 @@ public void onRendererCapabilitiesChanged(Renderer renderer) {
* <p>The implementation should not account for overrides and disabled flags. Track selections
* generated by this method will be overridden to account for these properties.
*
* <p>If selection parameters include {@link Parameters#audioOffloadModePreference} with {@link
* TrackSelectionParameters#AUDIO_OFFLOAD_MODE_PREFERENCE_REQUIRED}, then only audio tracks will
* be selected. If no audio track is supported in offload, then no track will be selected.
*
* @param mappedTrackInfo Mapped track information.
* @param rendererFormatSupports The {@link Capabilities} for each mapped track, indexed by
* renderer, track group and track (in that order).
Expand Down Expand Up @@ -2571,7 +2576,6 @@ public void onRendererCapabilitiesChanged(Renderer renderer) {
trackType, mappedTrackInfo.getTrackGroups(i), rendererFormatSupports[i], params);
}
}

return definitions;
}

Expand All @@ -2598,6 +2602,9 @@ protected Pair<ExoTrackSelection.Definition, Integer> selectVideoTrack(
@AdaptiveSupport int[] mixedMimeTypeSupports,
Parameters params)
throws ExoPlaybackException {
if (params.audioOffloadModePreference == AUDIO_OFFLOAD_MODE_PREFERENCE_REQUIRED) {
return null;
}
return selectTracksForType(
C.TRACK_TYPE_VIDEO,
mappedTrackInfo,
Expand Down Expand Up @@ -2713,6 +2720,9 @@ protected Pair<ExoTrackSelection.Definition, Integer> selectTextTrack(
Parameters params,
@Nullable String selectedAudioLanguage)
throws ExoPlaybackException {
if (params.audioOffloadModePreference == AUDIO_OFFLOAD_MODE_PREFERENCE_REQUIRED) {
return null;
}
return selectTracksForType(
C.TRACK_TYPE_TEXT,
mappedTrackInfo,
Expand Down Expand Up @@ -2741,6 +2751,9 @@ protected Pair<ExoTrackSelection.Definition, Integer> selectTextTrack(
protected ExoTrackSelection.Definition selectOtherTrack(
int trackType, TrackGroupArray groups, @Capabilities int[][] formatSupport, Parameters params)
throws ExoPlaybackException {
if (params.audioOffloadModePreference == AUDIO_OFFLOAD_MODE_PREFERENCE_REQUIRED) {
return null;
}
@Nullable TrackGroup selectedGroup = null;
int selectedTrackIndex = 0;
@Nullable OtherTrackScore selectedTrackScore = null;
Expand Down Expand Up @@ -3062,7 +3075,8 @@ private static void maybeConfigureRendererForOffload(
@Capabilities
int trackFormatSupport =
rendererFormatSupports[i][trackGroupIndex][trackSelection.getIndexInTrackGroup(0)];
if (rendererSupportsOffload(parameters, trackFormatSupport, trackSelection)) {
if (rendererSupportsOffload(
parameters, trackFormatSupport, trackSelection.getSelectedFormat())) {
audioRendererIndex = i;
audioRenderersSupportingOffload++;
}
Expand All @@ -3085,11 +3099,11 @@ private static void maybeConfigureRendererForOffload(
*
* @param parameters The selection parameters with audio offload mode preferences.
* @param formatSupport The {@link Capabilities} for the selected track.
* @param selection The track selection.
* @param format The format of the track selection.
* @return Whether the renderer supports tunneling for the {@link ExoTrackSelection}.
*/
private static boolean rendererSupportsOffload(
Parameters parameters, @Capabilities int formatSupport, ExoTrackSelection selection) {
Parameters parameters, @Capabilities int formatSupport, Format format) {
if (RendererCapabilities.getAudioOffloadSupport(formatSupport) == AUDIO_OFFLOAD_NOT_SUPPORTED) {
return false;
}
Expand All @@ -3101,9 +3115,7 @@ private static boolean rendererSupportsOffload(
}
// TODO(b/235883373): Add check for OPUS where gapless info is in initialization data
if (parameters.isGaplessSupportRequired) {
boolean isGapless =
selection.getSelectedFormat().encoderDelay != 0
|| selection.getSelectedFormat().encoderPadding != 0;
boolean isGapless = format.encoderDelay != 0 || format.encoderPadding != 0;
boolean isGaplessSupported =
(RendererCapabilities.getAudioOffloadSupport(formatSupport)
& AUDIO_OFFLOAD_GAPLESS_SUPPORTED)
Expand Down Expand Up @@ -3636,7 +3648,8 @@ public AudioTrackInfo(
usesHardwareAcceleration =
RendererCapabilities.getHardwareAccelerationSupport(formatSupport)
== RendererCapabilities.HARDWARE_ACCELERATION_SUPPORTED;
selectionEligibility = evaluateSelectionEligibility(formatSupport, hasMappedVideoTracks);
selectionEligibility =
evaluateSelectionEligibility(parameters, formatSupport, hasMappedVideoTracks);
}

@Override
Expand Down Expand Up @@ -3710,19 +3723,24 @@ public int compareTo(AudioTrackInfo other) {
}

private @SelectionEligibility int evaluateSelectionEligibility(
@Capabilities int rendererSupport, boolean hasMappedVideoTracks) {
Parameters params, @Capabilities int rendererSupport, boolean hasMappedVideoTracks) {
if (!isSupported(rendererSupport, parameters.exceedRendererCapabilitiesIfNecessary)) {
return SELECTION_ELIGIBILITY_NO;
}
if (!isWithinConstraints && !parameters.exceedAudioConstraintsIfNecessary) {
return SELECTION_ELIGIBILITY_NO;
}
if (params.audioOffloadModePreference == AUDIO_OFFLOAD_MODE_PREFERENCE_REQUIRED
&& !rendererSupportsOffload(params, rendererSupport, format)) {
return SELECTION_ELIGIBILITY_NO;
}
return isSupported(rendererSupport, /* allowExceedsCapabilities= */ false)
&& isWithinConstraints
&& format.bitrate != Format.NO_VALUE
&& !parameters.forceHighestSupportedBitrate
&& !parameters.forceLowestBitrate
&& (parameters.allowMultipleAdaptiveSelections || !hasMappedVideoTracks)
&& params.audioOffloadModePreference != AUDIO_OFFLOAD_MODE_PREFERENCE_REQUIRED
? SELECTION_ELIGIBILITY_ADAPTIVE
: SELECTION_ELIGIBILITY_FIXED;
}
Expand Down
Loading

0 comments on commit 68a7a11

Please sign in to comment.