From 6ffef38cadb4cd36d6d7fde426503d8bd4a2be3c Mon Sep 17 00:00:00 2001 From: kimvde Date: Wed, 21 Dec 2022 16:57:05 +0000 Subject: [PATCH] Change MuxerWrapper registerTrack to setTrackCount AssetLoader declares the tracks with a setTrackCount() method. Setting the track count on the MuxerWrapper is easier than calling registerTrack() as many times as the number of tracks. PiperOrigin-RevId: 496933501 --- .../exoplayer2/transformer/MuxerWrapper.java | 24 ++++++++++--------- .../transformer/TransformerInternal.java | 12 +++++----- 2 files changed, 19 insertions(+), 17 deletions(-) diff --git a/library/transformer/src/main/java/com/google/android/exoplayer2/transformer/MuxerWrapper.java b/library/transformer/src/main/java/com/google/android/exoplayer2/transformer/MuxerWrapper.java index 0c124cdf00..b0dfe8e014 100644 --- a/library/transformer/src/main/java/com/google/android/exoplayer2/transformer/MuxerWrapper.java +++ b/library/transformer/src/main/java/com/google/android/exoplayer2/transformer/MuxerWrapper.java @@ -25,6 +25,7 @@ import static java.util.concurrent.TimeUnit.MILLISECONDS; import android.os.ParcelFileDescriptor; import android.util.SparseIntArray; import android.util.SparseLongArray; +import androidx.annotation.IntRange; import androidx.annotation.Nullable; import com.google.android.exoplayer2.C; import com.google.android.exoplayer2.Format; @@ -99,18 +100,19 @@ import org.checkerframework.checker.nullness.qual.RequiresNonNull; } /** - * Registers an output track. + * Sets the number of output tracks. * - *

All tracks must be registered before any track format is {@linkplain #addTrackFormat(Format) + *

The track count must be set before any track format is {@linkplain #addTrackFormat(Format) * added}. * * @throws IllegalStateException If a track format was {@linkplain #addTrackFormat(Format) added} * before calling this method. */ - public void registerTrack() { + public void setTrackCount(@IntRange(from = 1) int trackCount) { checkState( - trackFormatCount == 0, "Tracks cannot be registered after track formats have been added."); - trackCount++; + trackFormatCount == 0, + "The track count cannot be set after track formats have been added."); + this.trackCount = trackCount; } /** Returns whether the sample {@linkplain MimeTypes MIME type} is supported. */ @@ -130,7 +132,7 @@ import org.checkerframework.checker.nullness.qual.RequiresNonNull; /** * Adds a track format to the muxer. * - *

The tracks must all be {@linkplain #registerTrack() registered} before any format is added + *

The number of tracks must be {@linkplain #setTrackCount(int) set} before any format is added * and all the formats must be added before samples are {@linkplain #writeSample(int, ByteBuffer, * boolean, long) written}. * @@ -141,7 +143,7 @@ import org.checkerframework.checker.nullness.qual.RequiresNonNull; * track. */ public void addTrackFormat(Format format) throws Muxer.MuxerException { - checkState(trackCount > 0, "All tracks should be registered before the formats are added."); + checkState(trackCount > 0, "The track count should be set before the formats are added."); checkState(trackFormatCount < trackCount, "All track formats have already been added."); @Nullable String sampleMimeType = format.sampleMimeType; boolean isAudio = MimeTypes.isAudio(sampleMimeType); @@ -175,8 +177,8 @@ import org.checkerframework.checker.nullness.qual.RequiresNonNull; * @param presentationTimeUs The presentation time of the sample in microseconds. * @return Whether the sample was successfully written. This is {@code false} if the muxer hasn't * {@linkplain #addTrackFormat(Format) received a format} for every {@linkplain - * #registerTrack() registered track}, or if it should write samples of other track types - * first to ensure a good interleaving. + * #setTrackCount(int) track}, or if it should write samples of other track types first to + * ensure a good interleaving. * @throws IllegalStateException If the muxer doesn't have any {@linkplain #endTrack(int) * non-ended} track of the given track type. * @throws Muxer.MuxerException If the underlying muxer fails to write the sample. @@ -302,8 +304,8 @@ import org.checkerframework.checker.nullness.qual.RequiresNonNull; * @param trackType The track type, defined by the {@code TRACK_TYPE_*} constants in {@link C}. * @return Whether the muxer can write a sample of the given track type. This is {@code false} if * the muxer hasn't {@link #addTrackFormat(Format) received a format} for every {@link - * #registerTrack() registered track}, or if it should write samples of other track types - * first to ensure a good interleaving. + * #setTrackCount(int) track}, or if it should write samples of other track types first to + * ensure a good interleaving. * @throws IllegalStateException If the muxer doesn't have any {@link #endTrack(int) non-ended} * track of the given track type. */ diff --git a/library/transformer/src/main/java/com/google/android/exoplayer2/transformer/TransformerInternal.java b/library/transformer/src/main/java/com/google/android/exoplayer2/transformer/TransformerInternal.java index cf61c1fa45..e75f141bb3 100644 --- a/library/transformer/src/main/java/com/google/android/exoplayer2/transformer/TransformerInternal.java +++ b/library/transformer/src/main/java/com/google/android/exoplayer2/transformer/TransformerInternal.java @@ -405,6 +405,9 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull; onError(new IllegalStateException("The output does not contain any tracks.")); } this.trackCount.set(trackCount); + if (forceSilentAudio) { + this.trackCount.incrementAndGet(); + } } @Override @@ -412,12 +415,9 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull; 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(); - } + // Call setTrackCount() methods here so that they are called from the same thread as the + // MuxerWrapper and FallbackListener methods called when building the sample pipelines. + muxerWrapper.setTrackCount(trackCount.get()); fallbackListener.setTrackCount(trackCount.get()); }