Commit 128d8ff0 by kimvde Committed by tonihei

Handle multiple sequences in a Composition

This lays the groundwork for full multi-asset, and more particularly for
adding looping background audio.

PiperOrigin-RevId: 512887888
parent 4cf7d3c7
...@@ -73,6 +73,7 @@ import java.util.concurrent.atomic.AtomicInteger; ...@@ -73,6 +73,7 @@ import java.util.concurrent.atomic.AtomicInteger;
private final AtomicInteger nonEndedTracks; private final AtomicInteger nonEndedTracks;
private AssetLoader currentAssetLoader; private AssetLoader currentAssetLoader;
private boolean trackCountReported;
private int processedInputsSize; private int processedInputsSize;
private volatile long currentDurationUs; private volatile long currentDurationUs;
...@@ -197,8 +198,11 @@ import java.util.concurrent.atomic.AtomicInteger; ...@@ -197,8 +198,11 @@ import java.util.concurrent.atomic.AtomicInteger;
if (currentMediaItemIndex.get() == 0) { if (currentMediaItemIndex.get() == 0) {
boolean addForcedAudioTrack = boolean addForcedAudioTrack =
forceAudioTrack && nonEndedTracks.get() == 1 && trackType == C.TRACK_TYPE_VIDEO; forceAudioTrack && nonEndedTracks.get() == 1 && trackType == C.TRACK_TYPE_VIDEO;
int trackCount = nonEndedTracks.get() + (addForcedAudioTrack ? 1 : 0); if (!trackCountReported) {
compositeAssetLoaderListener.onTrackCount(trackCount); int trackCount = nonEndedTracks.get() + (addForcedAudioTrack ? 1 : 0);
compositeAssetLoaderListener.onTrackCount(trackCount);
trackCountReported = true;
}
sampleConsumer = sampleConsumer =
new SampleConsumerWrapper( new SampleConsumerWrapper(
compositeAssetLoaderListener.onTrackAdded( compositeAssetLoaderListener.onTrackAdded(
......
...@@ -23,6 +23,7 @@ import com.google.android.exoplayer2.C; ...@@ -23,6 +23,7 @@ import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.util.HandlerWrapper; import com.google.android.exoplayer2.util.HandlerWrapper;
import com.google.android.exoplayer2.util.ListenerSet; import com.google.android.exoplayer2.util.ListenerSet;
import com.google.android.exoplayer2.util.Util; import com.google.android.exoplayer2.util.Util;
import java.util.concurrent.atomic.AtomicInteger;
/** /**
* Listener for fallback {@link TransformationRequest TransformationRequests} from the audio and * Listener for fallback {@link TransformationRequest TransformationRequests} from the audio and
...@@ -31,12 +32,12 @@ import com.google.android.exoplayer2.util.Util; ...@@ -31,12 +32,12 @@ import com.google.android.exoplayer2.util.Util;
/* package */ final class FallbackListener { /* package */ final class FallbackListener {
private final Composition composition; private final Composition composition;
private final TransformationRequest originalTransformationRequest;
private final ListenerSet<Transformer.Listener> transformerListeners; private final ListenerSet<Transformer.Listener> transformerListeners;
private final HandlerWrapper transformerListenerHandler; private final HandlerWrapper transformerListenerHandler;
private final TransformationRequest originalTransformationRequest;
private final AtomicInteger trackCount;
private TransformationRequest fallbackTransformationRequest; private TransformationRequest fallbackTransformationRequest;
private int trackCount;
/** /**
* Creates a new instance. * Creates a new instance.
...@@ -58,6 +59,7 @@ import com.google.android.exoplayer2.util.Util; ...@@ -58,6 +59,7 @@ import com.google.android.exoplayer2.util.Util;
this.transformerListenerHandler = transformerListenerHandler; this.transformerListenerHandler = transformerListenerHandler;
this.originalTransformationRequest = originalTransformationRequest; this.originalTransformationRequest = originalTransformationRequest;
this.fallbackTransformationRequest = originalTransformationRequest; this.fallbackTransformationRequest = originalTransformationRequest;
trackCount = new AtomicInteger();
} }
/** /**
...@@ -65,9 +67,11 @@ import com.google.android.exoplayer2.util.Util; ...@@ -65,9 +67,11 @@ import com.google.android.exoplayer2.util.Util;
* *
* <p>The track count must be set 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}.
*
* <p>Can be called from any thread.
*/ */
public void setTrackCount(@IntRange(from = 1) int trackCount) { public void setTrackCount(@IntRange(from = 1) int trackCount) {
this.trackCount = trackCount; this.trackCount.set(trackCount);
} }
/** /**
...@@ -86,7 +90,7 @@ import com.google.android.exoplayer2.util.Util; ...@@ -86,7 +90,7 @@ import com.google.android.exoplayer2.util.Util;
* #setTrackCount(int)}. * #setTrackCount(int)}.
*/ */
public void onTransformationRequestFinalized(TransformationRequest transformationRequest) { public void onTransformationRequestFinalized(TransformationRequest transformationRequest) {
checkState(trackCount-- > 0); checkState(trackCount.getAndDecrement() > 0);
TransformationRequest.Builder fallbackRequestBuilder = TransformationRequest.Builder fallbackRequestBuilder =
fallbackTransformationRequest.buildUpon(); fallbackTransformationRequest.buildUpon();
...@@ -107,7 +111,8 @@ import com.google.android.exoplayer2.util.Util; ...@@ -107,7 +111,8 @@ import com.google.android.exoplayer2.util.Util;
TransformationRequest newFallbackTransformationRequest = fallbackRequestBuilder.build(); TransformationRequest newFallbackTransformationRequest = fallbackRequestBuilder.build();
fallbackTransformationRequest = newFallbackTransformationRequest; fallbackTransformationRequest = newFallbackTransformationRequest;
if (trackCount == 0 && !originalTransformationRequest.equals(fallbackTransformationRequest)) { if (trackCount.get() == 0
&& !originalTransformationRequest.equals(fallbackTransformationRequest)) {
transformerListenerHandler.post( transformerListenerHandler.post(
() -> () ->
transformerListeners.sendEvent( transformerListeners.sendEvent(
......
...@@ -69,7 +69,6 @@ import org.checkerframework.checker.nullness.qual.RequiresNonNull; ...@@ -69,7 +69,6 @@ import org.checkerframework.checker.nullness.qual.RequiresNonNull;
private final SparseArray<TrackInfo> trackTypeToInfo; private final SparseArray<TrackInfo> trackTypeToInfo;
private final ScheduledExecutorService abortScheduledExecutorService; private final ScheduledExecutorService abortScheduledExecutorService;
private int trackCount;
private boolean isReady; private boolean isReady;
private boolean isEnded; private boolean isEnded;
private @C.TrackType int previousTrackType; private @C.TrackType int previousTrackType;
...@@ -79,6 +78,8 @@ import org.checkerframework.checker.nullness.qual.RequiresNonNull; ...@@ -79,6 +78,8 @@ import org.checkerframework.checker.nullness.qual.RequiresNonNull;
private boolean isAborted; private boolean isAborted;
private @MonotonicNonNull Muxer muxer; private @MonotonicNonNull Muxer muxer;
private volatile int trackCount;
public MuxerWrapper(String outputPath, Muxer.Factory muxerFactory, Listener listener) { public MuxerWrapper(String outputPath, Muxer.Factory muxerFactory, Listener listener) {
this.outputPath = outputPath; this.outputPath = outputPath;
this.muxerFactory = muxerFactory; this.muxerFactory = muxerFactory;
...@@ -95,6 +96,8 @@ import org.checkerframework.checker.nullness.qual.RequiresNonNull; ...@@ -95,6 +96,8 @@ import org.checkerframework.checker.nullness.qual.RequiresNonNull;
* <p>The track count must be set before any track format is {@linkplain #addTrackFormat(Format) * <p>The track count must be set before any track format is {@linkplain #addTrackFormat(Format)
* added}. * added}.
* *
* <p>Can be called from any thread.
*
* @throws IllegalStateException If a track format was {@linkplain #addTrackFormat(Format) added} * @throws IllegalStateException If a track format was {@linkplain #addTrackFormat(Format) added}
* before calling this method. * before calling this method.
*/ */
...@@ -135,6 +138,7 @@ import org.checkerframework.checker.nullness.qual.RequiresNonNull; ...@@ -135,6 +138,7 @@ import org.checkerframework.checker.nullness.qual.RequiresNonNull;
* the track. * the track.
*/ */
public void addTrackFormat(Format format) throws Muxer.MuxerException { public void addTrackFormat(Format format) throws Muxer.MuxerException {
int trackCount = this.trackCount;
checkState(trackCount > 0, "The track count should be set before the formats are added."); checkState(trackCount > 0, "The track count should be set before the formats are added.");
checkState(trackTypeToInfo.size() < trackCount, "All track formats have already been added."); checkState(trackTypeToInfo.size() < trackCount, "All track formats have already been added.");
@Nullable String sampleMimeType = format.sampleMimeType; @Nullable String sampleMimeType = format.sampleMimeType;
......
...@@ -39,8 +39,9 @@ import java.util.List; ...@@ -39,8 +39,9 @@ import java.util.List;
* *
* <p>The {@link SampleConsumer} and {@link OnMediaItemChangedListener} methods must be called from * <p>The {@link SampleConsumer} and {@link OnMediaItemChangedListener} methods must be called from
* the same thread. This thread can change when the {@link * the same thread. This thread can change when the {@link
* OnMediaItemChangedListener#onMediaItemChanged(EditedMediaItem, Format, long) MediaItem} changes, * OnMediaItemChangedListener#onMediaItemChanged(EditedMediaItem, long, Format, boolean) MediaItem}
* and can be different from the thread used to call the other {@code SamplePipeline} methods. * changes, and can be different from the thread used to call the other {@code SamplePipeline}
* methods.
*/ */
/* package */ abstract class SamplePipeline implements SampleConsumer, OnMediaItemChangedListener { /* package */ abstract class SamplePipeline implements SampleConsumer, OnMediaItemChangedListener {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment