Add a getter method for texture manager

PiperOrigin-RevId: 533402277
This commit is contained in:
claincly 2023-05-19 10:55:07 +01:00 committed by Ian Baker
parent c44b3828ca
commit d7ad431cfc
2 changed files with 34 additions and 23 deletions

View File

@ -298,9 +298,6 @@ public final class DefaultVideoFrameProcessor implements VideoFrameProcessor {
// CountDownLatch to wait for the current input stream to finish processing. // CountDownLatch to wait for the current input stream to finish processing.
private volatile @MonotonicNonNull CountDownLatch latch; private volatile @MonotonicNonNull CountDownLatch latch;
// TODO(b/274109008) Use InputSwither to interact with texture manager.
// Owned and released by inputSwitcher.
private @MonotonicNonNull TextureManager textureManager;
private volatile @MonotonicNonNull FrameInfo nextInputFrameInfo; private volatile @MonotonicNonNull FrameInfo nextInputFrameInfo;
private volatile boolean inputStreamEnded; private volatile boolean inputStreamEnded;
private volatile boolean hasRefreshedNextInputFrameInfo; private volatile boolean hasRefreshedNextInputFrameInfo;
@ -363,7 +360,7 @@ public final class DefaultVideoFrameProcessor implements VideoFrameProcessor {
* @param height The default height for input buffers, in pixels. * @param height The default height for input buffers, in pixels.
*/ */
public void setInputDefaultBufferSize(int width, int height) { public void setInputDefaultBufferSize(int width, int height) {
checkNotNull(textureManager).setDefaultBufferSize(width, height); inputSwitcher.activeTextureManager().setDefaultBufferSize(width, height);
} }
@Override @Override
@ -371,7 +368,8 @@ public final class DefaultVideoFrameProcessor implements VideoFrameProcessor {
checkState( checkState(
hasRefreshedNextInputFrameInfo, hasRefreshedNextInputFrameInfo,
"setInputFrameInfo must be called before queueing another bitmap"); "setInputFrameInfo must be called before queueing another bitmap");
checkNotNull(textureManager) inputSwitcher
.activeTextureManager()
.queueInputBitmap( .queueInputBitmap(
inputBitmap, inputBitmap,
durationUs, durationUs,
@ -383,24 +381,24 @@ public final class DefaultVideoFrameProcessor implements VideoFrameProcessor {
@Override @Override
public void queueInputTexture(int textureId, long presentationTimeUs) { public void queueInputTexture(int textureId, long presentationTimeUs) {
checkNotNull(textureManager).queueInputTexture(textureId, presentationTimeUs); inputSwitcher.activeTextureManager().queueInputTexture(textureId, presentationTimeUs);
} }
@Override @Override
public void setOnInputFrameProcessedListener(OnInputFrameProcessedListener listener) { public void setOnInputFrameProcessedListener(OnInputFrameProcessedListener listener) {
checkNotNull(textureManager).setOnInputFrameProcessedListener(listener); inputSwitcher.activeTextureManager().setOnInputFrameProcessedListener(listener);
} }
@Override @Override
public Surface getInputSurface() { public Surface getInputSurface() {
return checkNotNull(textureManager).getInputSurface(); return inputSwitcher.activeTextureManager().getInputSurface();
} }
@Override @Override
public void registerInputStream(@InputType int inputType) { public void registerInputStream(@InputType int inputType) {
synchronized (lock) { synchronized (lock) {
if (unprocessedInputStreams.isEmpty()) { if (unprocessedInputStreams.isEmpty()) {
textureManager = inputSwitcher.switchToInput(inputType); inputSwitcher.switchToInput(inputType);
unprocessedInputStreams.add(inputType); unprocessedInputStreams.add(inputType);
return; return;
} }
@ -408,14 +406,14 @@ public final class DefaultVideoFrameProcessor implements VideoFrameProcessor {
// Wait until the current input stream is processed before continuing to the next input. // Wait until the current input stream is processed before continuing to the next input.
latch = new CountDownLatch(1); latch = new CountDownLatch(1);
checkNotNull(textureManager).signalEndOfCurrentInputStream(); inputSwitcher.activeTextureManager().signalEndOfCurrentInputStream();
try { try {
latch.await(); latch.await();
} catch (InterruptedException e) { } catch (InterruptedException e) {
Thread.currentThread().interrupt(); Thread.currentThread().interrupt();
listenerExecutor.execute(() -> listener.onError(VideoFrameProcessingException.from(e))); listenerExecutor.execute(() -> listener.onError(VideoFrameProcessingException.from(e)));
} }
textureManager = inputSwitcher.switchToInput(inputType); inputSwitcher.switchToInput(inputType);
synchronized (lock) { synchronized (lock) {
unprocessedInputStreams.add(inputType); unprocessedInputStreams.add(inputType);
} }
@ -424,7 +422,7 @@ public final class DefaultVideoFrameProcessor implements VideoFrameProcessor {
@Override @Override
public void setInputFrameInfo(FrameInfo inputFrameInfo) { public void setInputFrameInfo(FrameInfo inputFrameInfo) {
nextInputFrameInfo = adjustForPixelWidthHeightRatio(inputFrameInfo); nextInputFrameInfo = adjustForPixelWidthHeightRatio(inputFrameInfo);
checkNotNull(textureManager).setInputFrameInfo(nextInputFrameInfo); inputSwitcher.activeTextureManager().setInputFrameInfo(nextInputFrameInfo);
hasRefreshedNextInputFrameInfo = true; hasRefreshedNextInputFrameInfo = true;
} }
@ -434,13 +432,13 @@ public final class DefaultVideoFrameProcessor implements VideoFrameProcessor {
checkStateNotNull( checkStateNotNull(
nextInputFrameInfo, "setInputFrameInfo must be called before registering input frames"); nextInputFrameInfo, "setInputFrameInfo must be called before registering input frames");
checkNotNull(textureManager).registerInputFrame(nextInputFrameInfo); inputSwitcher.activeTextureManager().registerInputFrame(nextInputFrameInfo);
hasRefreshedNextInputFrameInfo = false; hasRefreshedNextInputFrameInfo = false;
} }
@Override @Override
public int getPendingInputFrameCount() { public int getPendingInputFrameCount() {
return checkNotNull(textureManager).getPendingFrameCount(); return inputSwitcher.activeTextureManager().getPendingFrameCount();
} }
/** /**
@ -481,7 +479,7 @@ public final class DefaultVideoFrameProcessor implements VideoFrameProcessor {
if (allInputStreamsProcessed) { if (allInputStreamsProcessed) {
inputSwitcher.signalEndOfInput(); inputSwitcher.signalEndOfInput();
} else { } else {
checkNotNull(textureManager).signalEndOfCurrentInputStream(); inputSwitcher.signalEndOfCurrentInputStream();
} }
} }
@ -490,10 +488,10 @@ public final class DefaultVideoFrameProcessor implements VideoFrameProcessor {
try { try {
videoFrameProcessingTaskExecutor.flush(); videoFrameProcessingTaskExecutor.flush();
CountDownLatch latch = new CountDownLatch(1); CountDownLatch latch = new CountDownLatch(1);
checkNotNull(textureManager).setOnFlushCompleteListener(latch::countDown); inputSwitcher.activeTextureManager().setOnFlushCompleteListener(latch::countDown);
videoFrameProcessingTaskExecutor.submit(finalShaderProgramWrapper::flush); videoFrameProcessingTaskExecutor.submit(finalShaderProgramWrapper::flush);
latch.await(); latch.await();
checkNotNull(textureManager).setOnFlushCompleteListener(null); inputSwitcher.activeTextureManager().setOnFlushCompleteListener(null);
} catch (InterruptedException e) { } catch (InterruptedException e) {
Thread.currentThread().interrupt(); Thread.currentThread().interrupt();
} }

View File

@ -23,7 +23,6 @@ import static androidx.media3.common.util.Assertions.checkStateNotNull;
import android.content.Context; import android.content.Context;
import android.util.SparseArray; import android.util.SparseArray;
import androidx.annotation.Nullable;
import androidx.media3.common.C; import androidx.media3.common.C;
import androidx.media3.common.ColorInfo; import androidx.media3.common.ColorInfo;
import androidx.media3.common.GlObjectsProvider; import androidx.media3.common.GlObjectsProvider;
@ -46,6 +45,7 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
private final boolean enableColorTransfers; private final boolean enableColorTransfers;
private @MonotonicNonNull GlShaderProgram downstreamShaderProgram; private @MonotonicNonNull GlShaderProgram downstreamShaderProgram;
private @MonotonicNonNull TextureManager activeTextureManager;
private boolean inputEnded; private boolean inputEnded;
private int activeInputType; private int activeInputType;
@ -159,17 +159,15 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
* registered}. * registered}.
* *
* @param newInputType The new {@link VideoFrameProcessor.InputType} to switch to. * @param newInputType The new {@link VideoFrameProcessor.InputType} to switch to.
* @return The {@link TextureManager} associated with the {@code newInputType}.
*/ */
public TextureManager switchToInput(@VideoFrameProcessor.InputType int newInputType) { public void switchToInput(@VideoFrameProcessor.InputType int newInputType) {
checkStateNotNull(downstreamShaderProgram); checkStateNotNull(downstreamShaderProgram);
checkState(inputs.indexOfKey(newInputType) >= 0, "Input type not registered: " + newInputType); checkState(inputs.indexOfKey(newInputType) >= 0, "Input type not registered: " + newInputType);
if (newInputType == activeInputType) { if (newInputType == activeInputType) {
return inputs.get(activeInputType).textureManager; activeTextureManager = inputs.get(activeInputType).textureManager;
} }
@Nullable TextureManager activeTextureManager = null;
for (int i = 0; i < inputs.size(); i++) { for (int i = 0; i < inputs.size(); i++) {
@VideoFrameProcessor.InputType int inputType = inputs.keyAt(i); @VideoFrameProcessor.InputType int inputType = inputs.keyAt(i);
Input input = inputs.get(inputType); Input input = inputs.get(inputType);
@ -181,11 +179,26 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
input.setActive(false); input.setActive(false);
} }
} }
activeInputType = newInputType; activeInputType = newInputType;
}
/**
* Returns the {@link TextureManager} that is currently being used.
*
* <p>Must call {@link #switchToInput} before calling this method.
*/
public TextureManager activeTextureManager() {
return checkNotNull(activeTextureManager); return checkNotNull(activeTextureManager);
} }
/**
* Invokes {@link TextureManager#signalEndOfCurrentInputStream} on the active {@link
* TextureManager}.
*/
public void signalEndOfCurrentInputStream() {
checkNotNull(activeTextureManager).signalEndOfCurrentInputStream();
}
/** Signals end of input to all {@linkplain #registerInput registered inputs}. */ /** Signals end of input to all {@linkplain #registerInput registered inputs}. */
public void signalEndOfInput() { public void signalEndOfInput() {
checkState(!inputEnded); checkState(!inputEnded);