ExoTrackSelection


@UnstableApi
public interface ExoTrackSelection extends TrackSelection

Known direct subclasses
BaseTrackSelection

An abstract base class suitable for most ExoTrackSelection implementations.

FakeTrackSelection

A fake ExoTrackSelection that only returns 1 fixed track, and allows querying the number of calls to its methods.

Known indirect subclasses
AdaptiveTrackSelection

A bandwidth based adaptive ExoTrackSelection, whose selected track is updated to be the one of highest quality given the current network conditions and the state of the buffer.

FixedTrackSelection

A TrackSelection consisting of a single track.

RandomTrackSelection

An ExoTrackSelection whose selected track is updated randomly.


A TrackSelection that can change the individually selected track as a result of calling updateSelectedTrack or evaluateQueueSize. This only happens between calls to enable and disable.

Summary

Nested types

public final class ExoTrackSelection.Definition

Contains of a subset of selected tracks belonging to a TrackGroup.

public interface ExoTrackSelection.Factory

Factory for ExoTrackSelection instances.

Public methods

abstract void

Disables this track selection.

abstract void

Enables the track selection.

abstract int
evaluateQueueSize(long playbackPositionUs, List<MediaChunk> queue)

Returns the number of chunks that should be retained in the queue.

abstract boolean
excludeTrack(int index, long exclusionDurationMs)

Attempts to exclude the track at the specified index in the selection, making it ineligible for selection by calls to updateSelectedTrack for the specified period of time.

default long

Returns the most recent bitrate estimate utilised for track selection.

abstract Format

Returns the Format of the individual selected track.

abstract int

Returns the index of the selected track.

abstract int

Returns the index in the track group of the individual selected track.

abstract @Nullable Object

Returns optional data associated with the current track selection.

abstract int

Returns the reason for the current track selection.

abstract boolean
isTrackExcluded(int index, long nowMs)

Returns whether the track at the specified index in the selection is excluded.

default void

Called to notify the selection of a position discontinuity.

default void
onPlayWhenReadyChanged(boolean playWhenReady)

Called to notify when the playback is paused or resumed.

abstract void
onPlaybackSpeed(float playbackSpeed)

Called to notify the selection of the current playback speed.

default void

Called to notify when a rebuffer occurred.

default boolean
shouldCancelChunkLoad(
    long playbackPositionUs,
    Chunk loadingChunk,
    List<MediaChunk> queue
)

Returns whether an ongoing load of a chunk should be canceled.

abstract void
updateSelectedTrack(
    long playbackPositionUs,
    long bufferedDurationUs,
    long availableDurationUs,
    List<MediaChunk> queue,
    MediaChunkIterator[] mediaChunkIterators
)

Updates the selected track for sources that load media in discrete MediaChunks.

Inherited Constants

From androidx.media3.exoplayer.trackselection.TrackSelection
default static final int

The first value that can be used for application specific track selection types.

default static final int

An unspecified track selection type.

Inherited methods

From androidx.media3.exoplayer.trackselection.TrackSelection
abstract Format
getFormat(int index)

Returns the format of the track at a given index in the selection.

abstract int

Returns the index in the track group of the track at a given index in the selection.

abstract TrackGroup

Returns the TrackGroup to which the selected tracks belong.

abstract int

Returns an integer specifying the type of the selection, or TYPE_UNSET if not specified.

abstract int
indexOf(Format format)

Returns the index in the selection of the track with the specified format.

abstract int
indexOf(int indexInTrackGroup)

Returns the index in the selection of the track with the specified index in the track group.

abstract int

Returns the number of tracks in the selection.

Public methods

disable

abstract void disable()

Disables this track selection. No further dynamic changes via updateSelectedTrack, evaluateQueueSize or shouldCancelChunkLoad will happen after this call.

This method may only be called when the track selection is already enabled.

enable

abstract void enable()

Enables the track selection. Dynamic changes via updateSelectedTrack, evaluateQueueSize or shouldCancelChunkLoad will only happen after this call.

This method may not be called when the track selection is already enabled.

evaluateQueueSize

abstract int evaluateQueueSize(long playbackPositionUs, List<MediaChunk> queue)

Returns the number of chunks that should be retained in the queue.

May be called by sources that load media in discrete MediaChunks and support discarding of buffered chunks.

To avoid excessive re-buffering, implementations should normally return the size of the queue. An example of a case where a smaller value may be returned is if network conditions have improved dramatically, allowing chunks to be discarded and re-buffered in a track of significantly higher quality. Discarding chunks may allow faster switching to a higher quality track in this case.

Note that even if the source supports discarding of buffered chunks, the actual number of discarded chunks is not guaranteed. The source will call updateSelectedTrack with the updated queue of chunks before loading a new chunk to allow switching to another quality.

This method will only be called when the selection is enabled and none of the MediaChunks in the queue are currently loading.

Parameters
long playbackPositionUs

The current playback position in microseconds. If playback of the period to which this track selection belongs has not yet started, the value will be the starting position in the period minus the duration of any media in previous periods still to be played.

List<MediaChunk> queue

The queue of buffered MediaChunks. Must not be modified.

Returns
int

The number of chunks to retain in the queue.

excludeTrack

abstract boolean excludeTrack(int index, long exclusionDurationMs)

Attempts to exclude the track at the specified index in the selection, making it ineligible for selection by calls to updateSelectedTrack for the specified period of time.

Exclusion will fail if all other tracks are currently excluded. If excluding the currently selected track, note that it will remain selected until the next call to updateSelectedTrack.

This method will only be called when the selection is enabled.

Parameters
int index

The index of the track in the selection.

long exclusionDurationMs

The duration of time for which the track should be excluded, in milliseconds.

Returns
boolean

Whether exclusion was successful.

getLatestBitrateEstimate

default long getLatestBitrateEstimate()

Returns the most recent bitrate estimate utilised for track selection.

The default behavior is to return RATE_UNSET_INT, indicating that the bitrate estimate was not computed for the track selection decision.

getSelectedFormat

abstract Format getSelectedFormat()

Returns the Format of the individual selected track.

getSelectedIndex

abstract int getSelectedIndex()

Returns the index of the selected track.

getSelectedIndexInTrackGroup

abstract int getSelectedIndexInTrackGroup()

Returns the index in the track group of the individual selected track.

getSelectionData

abstract @Nullable Object getSelectionData()

Returns optional data associated with the current track selection.

getSelectionReason

@C.SelectionReason
abstract int getSelectionReason()

Returns the reason for the current track selection.

isTrackExcluded

abstract boolean isTrackExcluded(int index, long nowMs)

Returns whether the track at the specified index in the selection is excluded.

Parameters
int index

The index of the track in the selection.

long nowMs

The current time in the timebase of elapsedRealtime.

onDiscontinuity

default void onDiscontinuity()

Called to notify the selection of a position discontinuity.

This happens when the playback position jumps, e.g., as a result of a seek being performed.

onPlayWhenReadyChanged

default void onPlayWhenReadyChanged(boolean playWhenReady)

Called to notify when the playback is paused or resumed.

Parameters
boolean playWhenReady

Whether playback will proceed when ready.

onPlaybackSpeed

abstract void onPlaybackSpeed(float playbackSpeed)

Called to notify the selection of the current playback speed. The playback speed may affect adaptive track selection.

Parameters
float playbackSpeed

The factor by which playback is sped up.

onRebuffer

default void onRebuffer()

Called to notify when a rebuffer occurred.

A rebuffer is defined to be caused by buffer depletion rather than a user action. Hence this method is not called during initial buffering or when buffering as a result of a seek operation.

shouldCancelChunkLoad

default boolean shouldCancelChunkLoad(
    long playbackPositionUs,
    Chunk loadingChunk,
    List<MediaChunk> queue
)

Returns whether an ongoing load of a chunk should be canceled.

May be called by sources that load media in discrete MediaChunks and support canceling the ongoing chunk load. The ongoing chunk load is either the last in the queue or another type of Chunk, for example, if the source loads initialization or encryption data.

To avoid excessive re-buffering, implementations should normally return false. An example where true might be returned is if a load of a high quality chunk gets stuck and canceling this load in favor of a lower quality alternative may avoid a rebuffer.

The source will call evaluateQueueSize after the cancelation finishes to allow discarding of chunks, and updateSelectedTrack before loading a new chunk to allow switching to another quality.

This method will only be called when the selection is enabled.

Parameters
long playbackPositionUs

The current playback position in microseconds. If playback of the period to which this track selection belongs has not yet started, the value will be the starting position in the period minus the duration of any media in previous periods still to be played.

Chunk loadingChunk

The currently loading Chunk that will be canceled if this method returns true.

List<MediaChunk> queue

The queue of buffered MediaChunks, including the loadingChunk if it's a MediaChunk. Must not be modified.

Returns
boolean

Whether the ongoing load of loadingChunk should be canceled.

updateSelectedTrack

abstract void updateSelectedTrack(
    long playbackPositionUs,
    long bufferedDurationUs,
    long availableDurationUs,
    List<MediaChunk> queue,
    MediaChunkIterator[] mediaChunkIterators
)

Updates the selected track for sources that load media in discrete MediaChunks.

This method will only be called when the selection is enabled.

Parameters
long playbackPositionUs

The current playback position in microseconds. If playback of the period to which this track selection belongs has not yet started, the value will be the starting position in the period minus the duration of any media in previous periods still to be played.

long bufferedDurationUs

The duration of media currently buffered from the current playback position, in microseconds. Note that the next load position can be calculated as (playbackPositionUs + bufferedDurationUs).

long availableDurationUs

The duration of media available for buffering from the current playback position, in microseconds, or TIME_UNSET if media can be buffered to the end of the current period. Note that if not set to TIME_UNSET, the position up to which media is available for buffering can be calculated as (playbackPositionUs + availableDurationUs).

List<MediaChunk> queue

The queue of already buffered MediaChunks. Must not be modified.

MediaChunkIterator[] mediaChunkIterators

An array of MediaChunkIterators providing information about the sequence of upcoming media chunks for each track in the selection. All iterators start from the media chunk which will be loaded next if the respective track is selected. Note that this information may not be available for all tracks, and so some iterators may be empty.