Skip to content

Commit

Permalink
Remove non Player use of TrackSelectionArray, use TrackSelection[]
Browse files Browse the repository at this point in the history
This is necessary for the child cl that `TrackSelection`
in two distinct class. It avoids to split the array
version of such class too.

TrackSelectionArray exist to have an immutable array of TrackSelection.
Internal users are trusted to not mutate the array.

One drawback of this approach is that a `TrackSelectionArray`
has to be allocated on the boundary of the `Player` interface.
This should not be a performance issue as this only happens
on trackSelection changes, when the user calls
`Player.getCurrentTrackSelections` and on
`updateLoadControlTrackSelection`.

#player-to-common

PiperOrigin-RevId: 353582654
  • Loading branch information
krocard authored and icbaker committed Jan 25, 2021
1 parent c5a8154 commit bf3816b
Show file tree
Hide file tree
Showing 6 changed files with 144 additions and 143 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,15 @@ public ExoPlayerImpl(
Clock clock,
Looper applicationLooper,
@Nullable Player wrappingPlayer) {
Log.i(TAG, "Init " + Integer.toHexString(System.identityHashCode(this)) + " ["
+ ExoPlayerLibraryInfo.VERSION_SLASHY + "] [" + Util.DEVICE_DEBUG_INFO + "]");
Log.i(
TAG,
"Init "
+ Integer.toHexString(System.identityHashCode(this))
+ " ["
+ ExoPlayerLibraryInfo.VERSION_SLASHY
+ "] ["
+ Util.DEVICE_DEBUG_INFO
+ "]");
checkState(renderers.length > 0);
this.renderers = checkNotNull(renderers);
this.trackSelector = checkNotNull(trackSelector);
Expand Down Expand Up @@ -731,9 +738,17 @@ public void stop(boolean reset, @Nullable ExoPlaybackException error) {

@Override
public void release() {
Log.i(TAG, "Release " + Integer.toHexString(System.identityHashCode(this)) + " ["
+ ExoPlayerLibraryInfo.VERSION_SLASHY + "] [" + Util.DEVICE_DEBUG_INFO + "] ["
+ ExoPlayerLibraryInfo.registeredModules() + "]");
Log.i(
TAG,
"Release "
+ Integer.toHexString(System.identityHashCode(this))
+ " ["
+ ExoPlayerLibraryInfo.VERSION_SLASHY
+ "] ["
+ Util.DEVICE_DEBUG_INFO
+ "] ["
+ ExoPlayerLibraryInfo.registeredModules()
+ "]");
if (!internalPlayer.release()) {
// One of the renderers timed out releasing its resources.
listeners.sendEvent(
Expand Down Expand Up @@ -890,7 +905,7 @@ public TrackGroupArray getCurrentTrackGroups() {

@Override
public TrackSelectionArray getCurrentTrackSelections() {
return playbackInfo.trackSelectorResult.selections;
return new TrackSelectionArray(playbackInfo.trackSelectorResult.selections);
}

@Override
Expand Down Expand Up @@ -1013,11 +1028,11 @@ private void updatePlaybackInfo(
}
if (previousPlaybackInfo.trackSelectorResult != newPlaybackInfo.trackSelectorResult) {
trackSelector.onSelectionActivated(newPlaybackInfo.trackSelectorResult.info);
TrackSelectionArray newSelection =
new TrackSelectionArray(newPlaybackInfo.trackSelectorResult.selections);
listeners.queueEvent(
Player.EVENT_TRACKS_CHANGED,
listener ->
listener.onTracksChanged(
newPlaybackInfo.trackGroups, newPlaybackInfo.trackSelectorResult.selections));
listener -> listener.onTracksChanged(newPlaybackInfo.trackGroups, newSelection));
}
if (!previousPlaybackInfo.staticMetadata.equals(newPlaybackInfo.staticMetadata)) {
listeners.queueEvent(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -726,8 +726,7 @@ private void setShuffleOrderInternal(ShuffleOrder shuffleOrder) throws ExoPlayba
private void notifyTrackSelectionPlayWhenReadyChanged(boolean playWhenReady) {
MediaPeriodHolder periodHolder = queue.getPlayingPeriod();
while (periodHolder != null) {
TrackSelection[] trackSelections = periodHolder.getTrackSelectorResult().selections.getAll();
for (TrackSelection trackSelection : trackSelections) {
for (TrackSelection trackSelection : periodHolder.getTrackSelectorResult().selections) {
if (trackSelection != null) {
trackSelection.onPlayWhenReadyChanged(playWhenReady);
}
Expand Down Expand Up @@ -901,8 +900,7 @@ && isCurrentPeriodInMovingLiveWindow()
private void notifyTrackSelectionRebuffer() {
MediaPeriodHolder periodHolder = queue.getPlayingPeriod();
while (periodHolder != null) {
TrackSelection[] trackSelections = periodHolder.getTrackSelectorResult().selections.getAll();
for (TrackSelection trackSelection : trackSelections) {
for (TrackSelection trackSelection : periodHolder.getTrackSelectorResult().selections) {
if (trackSelection != null) {
trackSelection.onRebuffer();
}
Expand Down Expand Up @@ -1692,8 +1690,7 @@ private void reselectTracksInternal() throws ExoPlaybackException {
private void updateTrackSelectionPlaybackSpeed(float playbackSpeed) {
MediaPeriodHolder periodHolder = queue.getPlayingPeriod();
while (periodHolder != null) {
TrackSelection[] trackSelections = periodHolder.getTrackSelectorResult().selections.getAll();
for (TrackSelection trackSelection : trackSelections) {
for (TrackSelection trackSelection : periodHolder.getTrackSelectorResult().selections) {
if (trackSelection != null) {
trackSelection.onPlaybackSpeed(playbackSpeed);
}
Expand All @@ -1705,8 +1702,7 @@ private void updateTrackSelectionPlaybackSpeed(float playbackSpeed) {
private void notifyTrackSelectionDiscontinuity() {
MediaPeriodHolder periodHolder = queue.getPlayingPeriod();
while (periodHolder != null) {
TrackSelection[] trackSelections = periodHolder.getTrackSelectorResult().selections.getAll();
for (TrackSelection trackSelection : trackSelections) {
for (TrackSelection trackSelection : periodHolder.getTrackSelectorResult().selections) {
if (trackSelection != null) {
trackSelection.onDiscontinuity();
}
Expand Down Expand Up @@ -2018,7 +2014,7 @@ private boolean replaceStreamsOrDisableRendererForTransition() throws ExoPlaybac
}
if (!renderer.isCurrentStreamFinal()) {
// The renderer stream is not final, so we can replace the sample streams immediately.
Format[] formats = getFormats(newTrackSelectorResult.selections.get(i));
Format[] formats = getFormats(newTrackSelectorResult.selections[i]);
renderer.replaceStream(
formats,
readingPeriodHolder.sampleStreams[i],
Expand Down Expand Up @@ -2268,11 +2264,10 @@ private PlaybackInfo handlePositionDiscontinuity(
}

private ImmutableList<Metadata> extractMetadataFromTrackSelectionArray(
TrackSelectionArray trackSelectionArray) {
TrackSelection[] trackSelections) {
ImmutableList.Builder<Metadata> result = new ImmutableList.Builder<>();
boolean seenNonEmptyMetadata = false;
for (int i = 0; i < trackSelectionArray.length; i++) {
@Nullable TrackSelection trackSelection = trackSelectionArray.get(i);
for (TrackSelection trackSelection : trackSelections) {
if (trackSelection != null) {
Format format = trackSelection.getFormat(/* index= */ 0);
if (format.metadata == null) {
Expand Down Expand Up @@ -2320,7 +2315,7 @@ private void enableRenderer(int rendererIndex, boolean wasRendererEnabled)
TrackSelectorResult trackSelectorResult = periodHolder.getTrackSelectorResult();
RendererConfiguration rendererConfiguration =
trackSelectorResult.rendererConfigurations[rendererIndex];
TrackSelection newSelection = trackSelectorResult.selections.get(rendererIndex);
TrackSelection newSelection = trackSelectorResult.selections[rendererIndex];
Format[] formats = getFormats(newSelection);
// The renderer needs enabling with its new track selection.
boolean playing = shouldPlayWhenReady() && playbackInfo.playbackState == Player.STATE_READY;
Expand Down Expand Up @@ -2401,7 +2396,8 @@ private long getTotalBufferedDurationUs(long bufferedPositionInLoadingPeriodUs)

private void updateLoadControlTrackSelection(
TrackGroupArray trackGroups, TrackSelectorResult trackSelectorResult) {
loadControl.onTracksSelected(renderers, trackGroups, trackSelectorResult.selections);
TrackSelectionArray newSelection = new TrackSelectionArray(trackSelectorResult.selections);
loadControl.onTracksSelected(renderers, trackGroups, newSelection);
}

private boolean shouldPlayWhenReady() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import com.google.android.exoplayer2.source.SampleStream;
import com.google.android.exoplayer2.source.TrackGroupArray;
import com.google.android.exoplayer2.trackselection.TrackSelection;
import com.google.android.exoplayer2.trackselection.TrackSelectionArray;
import com.google.android.exoplayer2.trackselection.TrackSelector;
import com.google.android.exoplayer2.trackselection.TrackSelectorResult;
import com.google.android.exoplayer2.upstream.Allocator;
Expand Down Expand Up @@ -233,7 +232,7 @@ public TrackSelectorResult selectTracks(float playbackSpeed, Timeline timeline)
throws ExoPlaybackException {
TrackSelectorResult selectorResult =
trackSelector.selectTracks(rendererCapabilities, getTrackGroups(), info.id, timeline);
for (TrackSelection trackSelection : selectorResult.selections.getAll()) {
for (TrackSelection trackSelection : selectorResult.selections) {
if (trackSelection != null) {
trackSelection.onPlaybackSpeed(playbackSpeed);
}
Expand Down Expand Up @@ -289,10 +288,9 @@ public long applyTrackSelection(
trackSelectorResult = newTrackSelectorResult;
enableTrackSelectionsInResult();
// Disable streams on the period and get new streams for updated/newly-enabled tracks.
TrackSelectionArray trackSelections = newTrackSelectorResult.selections;
positionUs =
mediaPeriod.selectTracks(
trackSelections.getAll(),
newTrackSelectorResult.selections,
mayRetainStreamFlags,
sampleStreams,
streamResetFlags,
Expand All @@ -309,7 +307,7 @@ public long applyTrackSelection(
hasEnabledTracks = true;
}
} else {
Assertions.checkState(trackSelections.get(i) == null);
Assertions.checkState(newTrackSelectorResult.selections[i] == null);
}
}
return positionUs;
Expand Down Expand Up @@ -361,7 +359,7 @@ private void enableTrackSelectionsInResult() {
}
for (int i = 0; i < trackSelectorResult.length; i++) {
boolean rendererEnabled = trackSelectorResult.isRendererEnabled(i);
TrackSelection trackSelection = trackSelectorResult.selections.get(i);
TrackSelection trackSelection = trackSelectorResult.selections[i];
if (rendererEnabled && trackSelection != null) {
trackSelection.enable();
}
Expand All @@ -374,7 +372,7 @@ private void disableTrackSelectionsInResult() {
}
for (int i = 0; i < trackSelectorResult.length; i++) {
boolean rendererEnabled = trackSelectorResult.isRendererEnabled(i);
TrackSelection trackSelection = trackSelectorResult.selections.get(i);
TrackSelection trackSelection = trackSelectorResult.selections[i];
if (rendererEnabled && trackSelection != null) {
trackSelection.disable();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -847,7 +847,7 @@ private TrackSelectorResult runTrackSelection(int periodIndex) {
new MediaPeriodId(mediaPreparer.timeline.getUidOfPeriod(periodIndex)),
mediaPreparer.timeline);
for (int i = 0; i < trackSelectorResult.length; i++) {
@Nullable TrackSelection newSelection = trackSelectorResult.selections.get(i);
@Nullable TrackSelection newSelection = trackSelectorResult.selections[i];
if (newSelection == null) {
continue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@
import com.google.android.exoplayer2.util.Util;
import org.checkerframework.checker.nullness.compatqual.NullableType;

/**
* The result of a {@link TrackSelector} operation.
*/
/** The result of a {@link TrackSelector} operation. */
public final class TrackSelectorResult {

/** The number of selections in the result. Greater than or equal to zero. */
Expand All @@ -32,10 +30,8 @@ public final class TrackSelectorResult {
* renderer should be disabled.
*/
public final @NullableType RendererConfiguration[] rendererConfigurations;
/**
* A {@link TrackSelectionArray} containing the track selection for each renderer.
*/
public final TrackSelectionArray selections;
/** A {@link TrackSelection} array containing the track selection for each renderer. */
public final @NullableType TrackSelection[] selections;
/**
* An opaque object that will be returned to {@link TrackSelector#onSelectionActivated(Object)}
* should the selections be activated.
Expand All @@ -45,7 +41,7 @@ public final class TrackSelectorResult {
/**
* @param rendererConfigurations A {@link RendererConfiguration} for each renderer. A null entry
* indicates the corresponding renderer should be disabled.
* @param selections A {@link TrackSelectionArray} containing the selection for each renderer.
* @param selections A {@link TrackSelection} array containing the selection for each renderer.
* @param info An opaque object that will be returned to {@link
* TrackSelector#onSelectionActivated(Object)} should the selection be activated. May be
* {@code null}.
Expand All @@ -55,7 +51,7 @@ public TrackSelectorResult(
@NullableType TrackSelection[] selections,
@Nullable Object info) {
this.rendererConfigurations = rendererConfigurations;
this.selections = new TrackSelectionArray(selections);
this.selections = selections.clone();
this.info = info;
length = rendererConfigurations.length;
}
Expand Down Expand Up @@ -100,7 +96,6 @@ public boolean isEquivalent(@Nullable TrackSelectorResult other, int index) {
return false;
}
return Util.areEqual(rendererConfigurations[index], other.rendererConfigurations[index])
&& Util.areEqual(selections.get(index), other.selections.get(index));
&& Util.areEqual(selections[index], other.selections[index]);
}

}
Loading

0 comments on commit bf3816b

Please sign in to comment.