Add TrackSelector.release()

The TrackSelector is released when the player is released. The
TrackSelector can be reused if TrackSelector.init() is called again.

PiperOrigin-RevId: 446439717
This commit is contained in:
christosts 2022-05-04 14:33:50 +01:00 committed by Ian Baker
parent e6518126ac
commit c8c7ec61f0
5 changed files with 52 additions and 11 deletions

View File

@ -980,6 +980,7 @@ import java.util.concurrent.TimeoutException;
playbackInfo.bufferedPositionUs = playbackInfo.positionUs; playbackInfo.bufferedPositionUs = playbackInfo.positionUs;
playbackInfo.totalBufferedDurationUs = 0; playbackInfo.totalBufferedDurationUs = 0;
analyticsCollector.release(); analyticsCollector.release();
trackSelector.release();
removeSurfaceCallbacks(); removeSurfaceCallbacks();
if (ownedSurface != null) { if (ownedSurface != null) {
ownedSurface.release(); ownedSurface.release();

View File

@ -516,6 +516,7 @@ public final class DownloadHelper {
if (mediaPreparer != null) { if (mediaPreparer != null) {
mediaPreparer.release(); mediaPreparer.release();
} }
trackSelector.release();
} }
/** /**

View File

@ -15,10 +15,12 @@
*/ */
package androidx.media3.exoplayer.trackselection; package androidx.media3.exoplayer.trackselection;
import static androidx.media3.common.util.Assertions.checkStateNotNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.media3.common.Player;
import androidx.media3.common.Timeline; import androidx.media3.common.Timeline;
import androidx.media3.common.TrackSelectionParameters; import androidx.media3.common.TrackSelectionParameters;
import androidx.media3.common.util.Assertions;
import androidx.media3.common.util.UnstableApi; import androidx.media3.common.util.UnstableApi;
import androidx.media3.exoplayer.ExoPlaybackException; import androidx.media3.exoplayer.ExoPlaybackException;
import androidx.media3.exoplayer.ExoPlayer; import androidx.media3.exoplayer.ExoPlayer;
@ -65,6 +67,8 @@ import androidx.media3.exoplayer.upstream.BandwidthMeter;
* track selection for the currently playing period differs from the one that was invalidated. * track selection for the currently playing period differs from the one that was invalidated.
* Implementing subclasses can trigger invalidation by calling {@link #invalidate()}, which * Implementing subclasses can trigger invalidation by calling {@link #invalidate()}, which
* will call {@link InvalidationListener#onTrackSelectionsInvalidated()}. * will call {@link InvalidationListener#onTrackSelectionsInvalidated()}.
* <li>When the player is {@linkplain Player#release() released}, it will release the track
* selector by calling {@link #release()}.
* </ul> * </ul>
* *
* <h2>Renderer configuration</h2> * <h2>Renderer configuration</h2>
@ -113,6 +117,15 @@ public abstract class TrackSelector {
this.bandwidthMeter = bandwidthMeter; this.bandwidthMeter = bandwidthMeter;
} }
/**
* Called by the player to release the selector. The selector cannot be used until {@link
* #init(InvalidationListener, BandwidthMeter)} is called again.
*/
public final void release() {
this.listener = null;
this.bandwidthMeter = null;
}
/** /**
* Called by the player to perform a track selection. * Called by the player to perform a track selection.
* *
@ -177,9 +190,10 @@ public abstract class TrackSelector {
/** /**
* Returns a bandwidth meter which can be used by track selections to select tracks. Must only be * Returns a bandwidth meter which can be used by track selections to select tracks. Must only be
* called after {@link #init(InvalidationListener, BandwidthMeter)} has been called. * called when the track selector is {@linkplain #init(InvalidationListener, BandwidthMeter)
* initialized}.
*/ */
protected final BandwidthMeter getBandwidthMeter() { protected final BandwidthMeter getBandwidthMeter() {
return Assertions.checkNotNull(bandwidthMeter); return checkStateNotNull(bandwidthMeter);
} }
} }

View File

@ -62,6 +62,7 @@ import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSet;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import org.junit.After;
import org.junit.Before; import org.junit.Before;
import org.junit.BeforeClass; import org.junit.BeforeClass;
import org.junit.Test; import org.junit.Test;
@ -146,6 +147,11 @@ public final class DefaultTrackSelectorTest {
trackSelector.init(invalidationListener, bandwidthMeter); trackSelector.init(invalidationListener, bandwidthMeter);
} }
@After
public void tearDown() {
trackSelector.release();
}
@Test @Test
public void parameters_buildUponThenBuild_isEqual() { public void parameters_buildUponThenBuild_isEqual() {
Parameters parameters = buildParametersForEqualsTest(); Parameters parameters = buildParametersForEqualsTest();

View File

@ -16,7 +16,7 @@
package androidx.media3.exoplayer.trackselection; package androidx.media3.exoplayer.trackselection;
import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.fail; import static org.junit.Assert.assertThrows;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.media3.common.Timeline; import androidx.media3.common.Timeline;
@ -59,12 +59,7 @@ public class TrackSelectorTest {
@Test @Test
public void getBandwidthMeter_beforeInitialization_throwsException() { public void getBandwidthMeter_beforeInitialization_throwsException() {
try { assertThrows(IllegalStateException.class, () -> trackSelector.getBandwidthMeter());
trackSelector.getBandwidthMeter();
fail();
} catch (Exception e) {
// Expected.
}
} }
@Test @Test
@ -73,6 +68,30 @@ public class TrackSelectorTest {
BandwidthMeter bandwidthMeter = Mockito.mock(BandwidthMeter.class); BandwidthMeter bandwidthMeter = Mockito.mock(BandwidthMeter.class);
trackSelector.init(invalidationListener, bandwidthMeter); trackSelector.init(invalidationListener, bandwidthMeter);
assertThat(trackSelector.getBandwidthMeter()).isEqualTo(bandwidthMeter); assertThat(trackSelector.getBandwidthMeter()).isSameInstanceAs(bandwidthMeter);
}
@Test
public void getBandwidthMeter_afterRelease_throwsException() {
InvalidationListener invalidationListener = Mockito.mock(InvalidationListener.class);
BandwidthMeter bandwidthMeter = Mockito.mock(BandwidthMeter.class);
trackSelector.init(invalidationListener, bandwidthMeter);
trackSelector.release();
assertThrows(IllegalStateException.class, () -> trackSelector.getBandwidthMeter());
}
@Test
public void initialize_afterRelease() {
InvalidationListener invalidationListener = Mockito.mock(InvalidationListener.class);
BandwidthMeter bandwidthMeter = Mockito.mock(BandwidthMeter.class);
trackSelector.init(invalidationListener, bandwidthMeter);
trackSelector.release();
BandwidthMeter anotherBandwidthMeter = Mockito.mock(BandwidthMeter.class);
trackSelector.init(invalidationListener, anotherBandwidthMeter);
assertThat(trackSelector.getBandwidthMeter()).isSameInstanceAs(anotherBandwidthMeter);
} }
} }