diff --git a/libraries/effect/src/main/java/androidx/media3/effect/ChainingGlShaderProgramListener.java b/libraries/effect/src/main/java/androidx/media3/effect/ChainingGlShaderProgramListener.java index fd5175665c..32682aea84 100644 --- a/libraries/effect/src/main/java/androidx/media3/effect/ChainingGlShaderProgramListener.java +++ b/libraries/effect/src/main/java/androidx/media3/effect/ChainingGlShaderProgramListener.java @@ -15,15 +15,9 @@ */ package androidx.media3.effect; -import android.util.Pair; -import androidx.annotation.GuardedBy; -import androidx.annotation.Nullable; -import androidx.media3.common.C; import androidx.media3.common.GlTextureInfo; import androidx.media3.effect.GlShaderProgram.InputListener; import androidx.media3.effect.GlShaderProgram.OutputListener; -import java.util.ArrayDeque; -import java.util.Queue; /** * Connects a producing and a consuming {@link GlShaderProgram} instance. @@ -35,15 +29,9 @@ import java.util.Queue; implements GlShaderProgram.InputListener, GlShaderProgram.OutputListener { private final GlShaderProgram producingGlShaderProgram; - private final GlShaderProgram consumingGlShaderProgram; + private final FrameConsumptionManager frameConsumptionManager; private final VideoFrameProcessingTaskExecutor videoFrameProcessingTaskExecutor; - @GuardedBy("this") - private final Queue> availableFrames; - - @GuardedBy("this") - private int consumingGlShaderProgramInputCapacity; - /** * Creates a new instance. * @@ -61,29 +49,14 @@ import java.util.Queue; GlShaderProgram consumingGlShaderProgram, VideoFrameProcessingTaskExecutor videoFrameProcessingTaskExecutor) { this.producingGlShaderProgram = producingGlShaderProgram; - this.consumingGlShaderProgram = consumingGlShaderProgram; + frameConsumptionManager = + new FrameConsumptionManager(consumingGlShaderProgram, videoFrameProcessingTaskExecutor); this.videoFrameProcessingTaskExecutor = videoFrameProcessingTaskExecutor; - availableFrames = new ArrayDeque<>(); } @Override public synchronized void onReadyToAcceptInputFrame() { - @Nullable Pair pendingFrame = availableFrames.poll(); - if (pendingFrame == null) { - consumingGlShaderProgramInputCapacity++; - return; - } - - long presentationTimeUs = pendingFrame.second; - if (presentationTimeUs == C.TIME_END_OF_SOURCE) { - videoFrameProcessingTaskExecutor.submit( - consumingGlShaderProgram::signalEndOfCurrentInputStream); - } else { - videoFrameProcessingTaskExecutor.submit( - () -> - consumingGlShaderProgram.queueInputFrame( - /* inputTexture= */ pendingFrame.first, presentationTimeUs)); - } + frameConsumptionManager.onReadyToAcceptInputFrame(); } @Override @@ -94,32 +67,18 @@ import java.util.Queue; @Override public synchronized void onFlush() { - consumingGlShaderProgramInputCapacity = 0; - availableFrames.clear(); + frameConsumptionManager.onFlush(); videoFrameProcessingTaskExecutor.submit(producingGlShaderProgram::flush); } @Override public synchronized void onOutputFrameAvailable( GlTextureInfo outputTexture, long presentationTimeUs) { - if (consumingGlShaderProgramInputCapacity > 0) { - videoFrameProcessingTaskExecutor.submit( - () -> - consumingGlShaderProgram.queueInputFrame( - /* inputTexture= */ outputTexture, presentationTimeUs)); - consumingGlShaderProgramInputCapacity--; - } else { - availableFrames.add(new Pair<>(outputTexture, presentationTimeUs)); - } + frameConsumptionManager.queueInputFrame(outputTexture, presentationTimeUs); } @Override public synchronized void onCurrentOutputStreamEnded() { - if (!availableFrames.isEmpty()) { - availableFrames.add(new Pair<>(GlTextureInfo.UNSET, C.TIME_END_OF_SOURCE)); - } else { - videoFrameProcessingTaskExecutor.submit( - consumingGlShaderProgram::signalEndOfCurrentInputStream); - } + frameConsumptionManager.signalEndOfCurrentStream(); } } diff --git a/libraries/effect/src/main/java/androidx/media3/effect/FrameConsumptionManager.java b/libraries/effect/src/main/java/androidx/media3/effect/FrameConsumptionManager.java index 7a150ab897..14b0d834ca 100644 --- a/libraries/effect/src/main/java/androidx/media3/effect/FrameConsumptionManager.java +++ b/libraries/effect/src/main/java/androidx/media3/effect/FrameConsumptionManager.java @@ -26,12 +26,11 @@ import java.util.Queue; /** * Manages queueing frames and sending them to a given {@link GlShaderProgram - * consumingGLShaderProgram} at a consumable pace. + * consumingGlShaderProgram} at a consumable pace. * *

Frames are stored as a {@link GlTextureInfo} with a {@code presentationTimeUs}. */ -// TODO(b/261820382): Converge ChainingGlShaderProgramListener with this class. /* package */ final class FrameConsumptionManager implements GlShaderProgram.InputListener { private final GlShaderProgram consumingGlShaderProgram; private final VideoFrameProcessingTaskExecutor videoFrameProcessingTaskExecutor; @@ -45,8 +44,7 @@ import java.util.Queue; /** * Creates a new instance. * - * @param consumingGlShaderProgram The {@link GlShaderProgram} for which this {@code - * texIdTextureManager} will be set as the {@link GlShaderProgram.InputListener}. + * @param consumingGlShaderProgram The {@link GlShaderProgram} that frames are queued to. * @param videoFrameProcessingTaskExecutor The {@link VideoFrameProcessingTaskExecutor}. */ public FrameConsumptionManager(