Skip to content

Commit

Permalink
Fix ExternalTextureManager: repeated queueing input frame in preview
Browse files Browse the repository at this point in the history
TL;DR: we should check if there are new frames available to queue to the
ExternalTextureProcessor before actually queueing a frame.

The overall flow on the external texture processor:

- `SurfaceTexture.onFrameAvailable` is called on `ExtTexMgr`, and
  - it calls `updateTexImage()`, and sets `frame`
  - it calls `maybeQueueFrameToExtTexProc()`
    - the frame is queued to `ExtTexProc` if `frame` is set

  - From `ExtTexProc.queueInputFrame()`:
    - notifies the `frameProcessorListener` of available frame
    - notifies the `inputListener` of `onReadyToAcceptInputFrame`
      - (`ExtTexMgr` is the listener), it calls `maybeQueueFrameToExtTexProc()`
       again

-- Parallelly --
- `ExtTexProc` calls `inputListener.onInputFrameProcessed`, when the frame is
released
  - (`ExtTexMgr` is the listener), sets `frame` to `null`

*Problem*

This logic relies on `frame` to be cleared at the right time.

In transformer, it's OK b/c `ExtTexProc` release the frame immediately in
`queueInputFrame()` and calls `onInputFrameProcessed` which also reset `frame`

But in previewing, the frame is not released for a while, up to 10 ms.
In this case, `frame` will not reset in this 10 ms, and
`maybeQueueFrameToExtTexProc()` is repeatedly queueing the same input frame.

PiperOrigin-RevId: 470211620
  • Loading branch information
claincly authored and marcbaechinger committed Oct 19, 2022
1 parent a395b23 commit a8c54dd
Showing 1 changed file with 21 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package androidx.media3.effect;

import static com.google.android.exoplayer2.util.Assertions.checkNotNull;

import android.graphics.SurfaceTexture;
import androidx.annotation.Nullable;
import androidx.annotation.WorkerThread;
Expand All @@ -41,15 +43,17 @@
private final float[] textureTransformMatrix;
private final Queue<FrameInfo> pendingFrames;

// Incremented on any thread, decremented on the GL thread only.
// Incremented on any thread when a frame becomes available on the surfaceTexture, decremented on
// the GL thread only.
private final AtomicInteger availableFrameCount;
// Incremented on any thread, decremented on the GL thread only.
private final AtomicInteger externalTextureProcessorInputCapacity;

// Set to true on any thread. Read on the GL thread only.
private volatile boolean inputStreamEnded;
// The frame that is sent downstream and is not done processing yet.
// Set to null on any thread. Read and set to non-null on the GL thread only.
@Nullable private volatile FrameInfo frame;
@Nullable private volatile FrameInfo currentFrame;

private long previousStreamOffsetUs;

Expand Down Expand Up @@ -84,12 +88,7 @@ public SurfaceTexture getSurfaceTexture() {
surfaceTexture.setOnFrameAvailableListener(
unused -> {
availableFrameCount.getAndIncrement();
frameProcessingTaskExecutor.submit(
() -> {
if (maybeUpdateFrame()) {
maybeQueueFrameToExternalTextureProcessor();
}
});
frameProcessingTaskExecutor.submit(this::maybeQueueFrameToExternalTextureProcessor);
});
return surfaceTexture;
}
Expand All @@ -102,20 +101,15 @@ public void onReadyToAcceptInputFrame() {

@Override
public void onInputFrameProcessed(TextureInfo inputTexture) {
frame = null;
frameProcessingTaskExecutor.submit(
() -> {
if (maybeUpdateFrame()) {
maybeQueueFrameToExternalTextureProcessor();
}
});
currentFrame = null;
frameProcessingTaskExecutor.submit(this::maybeQueueFrameToExternalTextureProcessor);
}

/**
* Notifies the {@code ExternalTextureManager} that a frame with the given {@link FrameInfo} will
* become available via the {@link SurfaceTexture} eventually.
*
* <p>Can be called on any thread, but the caller must ensure that frames are registered in the
* <p>Can be called on any thread. The caller must ensure that frames are registered in the
* correct order.
*/
public void registerInputFrame(FrameInfo frame) {
Expand All @@ -140,7 +134,7 @@ public int getPendingFrameCount() {
@WorkerThread
public void signalEndOfInput() {
inputStreamEnded = true;
if (pendingFrames.isEmpty() && frame == null) {
if (pendingFrames.isEmpty() && currentFrame == null) {
externalTextureProcessor.signalEndOfCurrentInputStream();
}
}
Expand All @@ -150,29 +144,23 @@ public void release() {
}

@WorkerThread
private boolean maybeUpdateFrame() {
if (frame != null || availableFrameCount.get() == 0) {
return false;
private void maybeQueueFrameToExternalTextureProcessor() {
if (externalTextureProcessorInputCapacity.get() == 0
|| availableFrameCount.get() == 0
|| currentFrame != null) {
return;
}

availableFrameCount.getAndDecrement();
surfaceTexture.updateTexImage();
frame = pendingFrames.remove();
return true;
}

@WorkerThread
private void maybeQueueFrameToExternalTextureProcessor() {
if (externalTextureProcessorInputCapacity.get() == 0 || frame == null) {
return;
}
this.currentFrame = pendingFrames.remove();

FrameInfo frame = this.frame;
FrameInfo currentFrame = checkNotNull(this.currentFrame);
externalTextureProcessorInputCapacity.getAndDecrement();
surfaceTexture.getTransformMatrix(textureTransformMatrix);
externalTextureProcessor.setTextureTransformMatrix(textureTransformMatrix);
long frameTimeNs = surfaceTexture.getTimestamp();
long streamOffsetUs = frame.streamOffsetUs;
long streamOffsetUs = currentFrame.streamOffsetUs;
if (streamOffsetUs != previousStreamOffsetUs) {
if (previousStreamOffsetUs != C.TIME_UNSET) {
externalTextureProcessor.signalEndOfCurrentInputStream();
Expand All @@ -182,7 +170,8 @@ private void maybeQueueFrameToExternalTextureProcessor() {
// Correct for the stream offset so processors see original media presentation timestamps.
long presentationTimeUs = (frameTimeNs / 1000) - streamOffsetUs;
externalTextureProcessor.queueInputFrame(
new TextureInfo(externalTexId, /* fboId= */ C.INDEX_UNSET, frame.width, frame.height),
new TextureInfo(
externalTexId, /* fboId= */ C.INDEX_UNSET, currentFrame.width, currentFrame.height),
presentationTimeUs);

if (inputStreamEnded && pendingFrames.isEmpty()) {
Expand Down

0 comments on commit a8c54dd

Please sign in to comment.