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