Change FallbackListener registerTrack to setTrackCount

AssetLoader declares the tracks with a setTrackCount() method. Setting
the track count on the FallbackListener is easier than calling
registerTrack() as many times as the number of tracks.

PiperOrigin-RevId: 496919969
This commit is contained in:
kimvde 2022-12-21 15:49:06 +00:00 committed by Marc Baechinger
parent 59166cce9c
commit 7c3cffdebe
4 changed files with 16 additions and 16 deletions

View File

@ -18,6 +18,7 @@ package androidx.media3.transformer;
import static androidx.media3.common.util.Assertions.checkState; import static androidx.media3.common.util.Assertions.checkState;
import androidx.annotation.IntRange;
import androidx.media3.common.C; import androidx.media3.common.C;
import androidx.media3.common.MediaItem; import androidx.media3.common.MediaItem;
import androidx.media3.common.util.HandlerWrapper; import androidx.media3.common.util.HandlerWrapper;
@ -61,13 +62,13 @@ import androidx.media3.common.util.Util;
} }
/** /**
* Registers an output track. * Sets the number of output tracks.
* *
* <p>All tracks must be registered before a transformation request is {@linkplain * <p>The track count must be set before a transformation request is {@linkplain
* #onTransformationRequestFinalized(TransformationRequest) finalized}. * #onTransformationRequestFinalized(TransformationRequest) finalized}.
*/ */
public void registerTrack() { public void setTrackCount(@IntRange(from = 1) int trackCount) {
trackCount++; this.trackCount = trackCount;
} }
/** /**
@ -82,8 +83,8 @@ import androidx.media3.common.util.Util;
* TransformationRequest)} once this method has been called for each track. * TransformationRequest)} once this method has been called for each track.
* *
* @param transformationRequest The final {@link TransformationRequest} for a track. * @param transformationRequest The final {@link TransformationRequest} for a track.
* @throws IllegalStateException If called for more tracks than registered using {@link * @throws IllegalStateException If called for more tracks than declared in {@link
* #registerTrack()}. * #setTrackCount(int)}.
*/ */
public void onTransformationRequestFinalized(TransformationRequest transformationRequest) { public void onTransformationRequestFinalized(TransformationRequest transformationRequest) {
checkState(trackCount-- > 0); checkState(trackCount-- > 0);

View File

@ -417,8 +417,8 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
} }
for (int i = 0; i < trackCount.get(); i++) { for (int i = 0; i < trackCount.get(); i++) {
muxerWrapper.registerTrack(); muxerWrapper.registerTrack();
fallbackListener.registerTrack();
} }
fallbackListener.setTrackCount(trackCount.get());
} }
SamplePipeline samplePipeline = SamplePipeline samplePipeline =

View File

@ -40,7 +40,7 @@ public class FallbackListenerTest {
private static final MediaItem PLACEHOLDER_MEDIA_ITEM = MediaItem.fromUri(Uri.EMPTY); private static final MediaItem PLACEHOLDER_MEDIA_ITEM = MediaItem.fromUri(Uri.EMPTY);
@Test @Test
public void onTransformationRequestFinalized_withoutTrackRegistration_throwsException() { public void onTransformationRequestFinalized_withoutTrackCountSet_throwsException() {
TransformationRequest transformationRequest = new TransformationRequest.Builder().build(); TransformationRequest transformationRequest = new TransformationRequest.Builder().build();
FallbackListener fallbackListener = FallbackListener fallbackListener =
new FallbackListener( new FallbackListener(
@ -52,13 +52,13 @@ public class FallbackListenerTest {
} }
@Test @Test
public void onTransformationRequestFinalized_afterTrackRegistration_completesSuccessfully() { public void onTransformationRequestFinalized_afterTrackCountSet_completesSuccessfully() {
TransformationRequest transformationRequest = new TransformationRequest.Builder().build(); TransformationRequest transformationRequest = new TransformationRequest.Builder().build();
FallbackListener fallbackListener = FallbackListener fallbackListener =
new FallbackListener( new FallbackListener(
PLACEHOLDER_MEDIA_ITEM, createListenerSet(), createHandler(), transformationRequest); PLACEHOLDER_MEDIA_ITEM, createListenerSet(), createHandler(), transformationRequest);
fallbackListener.registerTrack(); fallbackListener.setTrackCount(1);
fallbackListener.onTransformationRequestFinalized(transformationRequest); fallbackListener.onTransformationRequestFinalized(transformationRequest);
ShadowLooper.idleMainLooper(); ShadowLooper.idleMainLooper();
} }
@ -76,7 +76,7 @@ public class FallbackListenerTest {
createHandler(), createHandler(),
originalRequest); originalRequest);
fallbackListener.registerTrack(); fallbackListener.setTrackCount(1);
fallbackListener.onTransformationRequestFinalized(unchangedRequest); fallbackListener.onTransformationRequestFinalized(unchangedRequest);
ShadowLooper.idleMainLooper(); ShadowLooper.idleMainLooper();
@ -97,7 +97,7 @@ public class FallbackListenerTest {
createHandler(), createHandler(),
originalRequest); originalRequest);
fallbackListener.registerTrack(); fallbackListener.setTrackCount(1);
fallbackListener.onTransformationRequestFinalized(audioFallbackRequest); fallbackListener.onTransformationRequestFinalized(audioFallbackRequest);
ShadowLooper.idleMainLooper(); ShadowLooper.idleMainLooper();
@ -127,8 +127,7 @@ public class FallbackListenerTest {
createHandler(), createHandler(),
originalRequest); originalRequest);
fallbackListener.registerTrack(); fallbackListener.setTrackCount(2);
fallbackListener.registerTrack();
fallbackListener.onTransformationRequestFinalized(audioFallbackRequest); fallbackListener.onTransformationRequestFinalized(audioFallbackRequest);
fallbackListener.onTransformationRequestFinalized(videoFallbackRequest); fallbackListener.onTransformationRequestFinalized(videoFallbackRequest);
ShadowLooper.idleMainLooper(); ShadowLooper.idleMainLooper();

View File

@ -61,8 +61,8 @@ public final class VideoEncoderWrapperTest {
fallbackListener); fallbackListener);
@Before @Before
public void registerTrack() { public void setUp() {
fallbackListener.registerTrack(); fallbackListener.setTrackCount(1);
createShadowH264Encoder(); createShadowH264Encoder();
} }