Simplify AssetLoader.Listener interface

Replace onTrackRegistered and onAllTracksRegistered with onTrackCount.

PiperOrigin-RevId: 496853037
This commit is contained in:
kimvde 2022-12-21 08:43:33 +00:00 committed by Tianyi Feng
parent 06ccb6e8ed
commit a4c3038b16
4 changed files with 31 additions and 34 deletions

View File

@ -119,8 +119,7 @@ public interface AssetLoader {
*
* <ul>
* <li>{@linkplain #onDurationUs(long)} Report} the duration of the input media.
* <li>{@linkplain #onTrackRegistered() Register} each output track.
* <li>{@linkplain #onAllTracksRegistered() Signal} that all the tracks have been registered.
* <li>{@linkplain #onTrackCount(int) Report} the number of output tracks.
* <li>{@linkplain #onTrackAdded(Format, long, long) Add} the information for each track.
* </ul>
*
@ -131,25 +130,16 @@ public interface AssetLoader {
/** Called when the duration of the input media is known. */
void onDurationUs(long durationUs);
/**
* Called to register a single output track of sample data.
*
* <p>Must be called for each track that will be output.
*
* <p>Must be called on the same thread as {@link #onTrackAdded(Format, long, long)}.
*/
void onTrackRegistered();
/** Called when all the tracks have been {@linkplain #onTrackRegistered() registered}. */
void onAllTracksRegistered();
/** Called when the number of tracks output by the asset loader is known. */
void onTrackCount(int trackCount);
/**
* Called when the information on a {@linkplain #onTrackRegistered() registered} track is known.
* Called when the information on a track is known.
*
* <p>Must be called after the duration has been {@linkplain #onDurationUs(long) reported} and
* all the tracks have been {@linkplain #onAllTracksRegistered registered}.
* <p>Must be called after the {@linkplain #onDurationUs(long) duration} and the {@linkplain
* #onTrackCount(int) track count} have been reported.
*
* <p>Must be called on the same thread as {@link #onTrackRegistered()}.
* <p>Must be called once per {@linkplain #onTrackCount(int) declared} track.
*
* @param format The {@link Format} of the input media (prior to video slow motion flattening or
* to decoding).

View File

@ -343,7 +343,14 @@ public final class ExoPlayerAssetLoader implements AssetLoader {
@Override
public void onTracksChanged(Tracks tracks) {
assetLoaderListener.onAllTracksRegistered();
int trackCount = 0;
if (tracks.isTypeSelected(C.TRACK_TYPE_AUDIO)) {
trackCount++;
}
if (tracks.isTypeSelected(C.TRACK_TYPE_VIDEO)) {
trackCount++;
}
assetLoaderListener.onTrackCount(trackCount);
}
@Override

View File

@ -129,7 +129,6 @@ import org.checkerframework.checker.nullness.qual.RequiresNonNull;
@Override
protected void onEnabled(boolean joining, boolean mayRenderStartOfStream) {
assetLoaderListener.onTrackRegistered();
mediaClock.updateTimeForTrackType(getTrackType(), 0L);
}

View File

@ -49,6 +49,7 @@ import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
/* package */ final class TransformerInternal {
@ -378,15 +379,16 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
private final MediaItem mediaItem;
private final FallbackListener fallbackListener;
private final AtomicInteger trackCount;
private int tracksAddedCount;
private volatile long durationUs;
private volatile boolean trackRegistered;
public ComponentListener(MediaItem mediaItem, FallbackListener fallbackListener) {
this.mediaItem = mediaItem;
this.fallbackListener = fallbackListener;
trackCount = new AtomicInteger();
durationUs = C.TIME_UNSET;
}
@ -398,28 +400,27 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
}
@Override
public void onTrackRegistered() {
trackRegistered = true;
muxerWrapper.registerTrack();
fallbackListener.registerTrack();
if (forceSilentAudio) {
muxerWrapper.registerTrack();
fallbackListener.registerTrack();
}
}
@Override
public void onAllTracksRegistered() {
if (!trackRegistered) {
public void onTrackCount(int trackCount) {
if (trackCount == 0) {
onError(new IllegalStateException("The output does not contain any tracks."));
}
this.trackCount.set(trackCount);
}
@Override
public SamplePipeline.Input onTrackAdded(
Format format, long streamStartPositionUs, long streamOffsetUs)
throws TransformationException {
if (tracksAddedCount == 0) {
if (forceSilentAudio) {
trackCount.incrementAndGet();
}
for (int i = 0; i < trackCount.get(); i++) {
muxerWrapper.registerTrack();
fallbackListener.registerTrack();
}
}
SamplePipeline samplePipeline =
getSamplePipeline(format, streamStartPositionUs, streamOffsetUs);
internalHandler.obtainMessage(MSG_REGISTER_SAMPLE_PIPELINE, samplePipeline).sendToTarget();