Reland "Android: Generalize and make TextureBufferImpl public"
This reverts commit 64051d4975b5cee06ab36584f272ff97e35de357. Reason for revert: Fix applied. Original change's description: > Revert "Android: Generalize and make TextureBufferImpl public" > > This reverts commit 28111d7fa0b94e37a5eeba616eb806c65b12560e. > > Reason for revert: Crashes video_quality_loopback_test. > > Original change's description: > > Android: Generalize and make TextureBufferImpl public > > > > This CL generalizes TextureBufferImpl so it's useful from other contexts than > > from a SurfaceTextureHelper, and fixes a bug in cropAndScale(). It also exposes > > the class in the api so that clients don't have to duplicate the logic. > > > > Bug: None > > Change-Id: Ib82aa8bee025ec14de74a7be9d91fd4e5298a248 > > Reviewed-on: https://webrtc-review.googlesource.com/69819 > > Reviewed-by: Sami Kalliomäki <sakal@webrtc.org> > > Commit-Queue: Magnus Jedvert <magjed@webrtc.org> > > Cr-Commit-Position: refs/heads/master@{#22875} > > TBR=magjed@webrtc.org,sakal@webrtc.org > > Change-Id: Ica7fc181fec70b8b79f39f0e114eef81a03aa116 > No-Presubmit: true > No-Tree-Checks: true > No-Try: true > Bug: None > Reviewed-on: https://webrtc-review.googlesource.com/70240 > Reviewed-by: Sami Kalliomäki <sakal@webrtc.org> > Commit-Queue: Sami Kalliomäki <sakal@webrtc.org> > Cr-Commit-Position: refs/heads/master@{#22878} TBR=magjed@webrtc.org,sakal@webrtc.org Change-Id: I173d1ccfe0baa80460f796ebaedc51731233108f No-Presubmit: true No-Tree-Checks: true No-Try: true Bug: None Reviewed-on: https://webrtc-review.googlesource.com/70183 Commit-Queue: Magnus Jedvert <magjed@webrtc.org> Reviewed-by: Magnus Jedvert <magjed@webrtc.org> Cr-Commit-Position: refs/heads/master@{#22883}
This commit is contained in:
committed by
Commit Bot
parent
324865a206
commit
1d270f8193
@ -766,11 +766,11 @@ rtc_android_library("video_api_java") {
|
||||
"api/org/webrtc/JavaI420Buffer.java",
|
||||
"api/org/webrtc/RendererCommon.java",
|
||||
"api/org/webrtc/SurfaceTextureHelper.java",
|
||||
"api/org/webrtc/TextureBufferImpl.java",
|
||||
"api/org/webrtc/YuvConverter.java",
|
||||
"api/org/webrtc/YuvHelper.java",
|
||||
"src/java/org/webrtc/EglBase10.java",
|
||||
"src/java/org/webrtc/EglBase14.java",
|
||||
"src/java/org/webrtc/TextureBufferImpl.java",
|
||||
]
|
||||
|
||||
deps = [
|
||||
|
||||
@ -80,7 +80,7 @@ public class SurfaceTextureHelper {
|
||||
private final EglBase eglBase;
|
||||
private final SurfaceTexture surfaceTexture;
|
||||
private final int oesTextureId;
|
||||
private YuvConverter yuvConverter;
|
||||
private final YuvConverter yuvConverter = new YuvConverter();
|
||||
|
||||
// These variables are only accessed from the |handler| thread.
|
||||
@Nullable private OnTextureFrameAvailableListener listener;
|
||||
@ -243,29 +243,16 @@ public class SurfaceTextureHelper {
|
||||
throw new IllegalStateException("textureToByteBuffer called with unexpected textureId");
|
||||
}
|
||||
|
||||
ThreadUtils.invokeAtFrontUninterruptibly(handler, new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
if (yuvConverter == null) {
|
||||
yuvConverter = new YuvConverter();
|
||||
}
|
||||
yuvConverter.convert(buf, width, height, stride, textureId, transformMatrix);
|
||||
}
|
||||
});
|
||||
ThreadUtils.invokeAtFrontUninterruptibly(handler,
|
||||
() -> yuvConverter.convert(buf, width, height, stride, textureId, transformMatrix));
|
||||
}
|
||||
|
||||
/**
|
||||
* Posts to the correct thread to convert |textureBuffer| to I420.
|
||||
*/
|
||||
public VideoFrame.I420Buffer textureToYuv(final TextureBuffer textureBuffer) {
|
||||
final VideoFrame.I420Buffer[] result = new VideoFrame.I420Buffer[1];
|
||||
ThreadUtils.invokeAtFrontUninterruptibly(handler, () -> {
|
||||
if (yuvConverter == null) {
|
||||
yuvConverter = new YuvConverter();
|
||||
}
|
||||
result[0] = yuvConverter.convert(textureBuffer);
|
||||
});
|
||||
return result[0];
|
||||
return ThreadUtils.invokeAtFrontUninterruptibly(
|
||||
handler, () -> yuvConverter.convert(textureBuffer));
|
||||
}
|
||||
|
||||
private void updateTexImage() {
|
||||
@ -302,9 +289,7 @@ public class SurfaceTextureHelper {
|
||||
if (isTextureInUse || !isQuitting) {
|
||||
throw new IllegalStateException("Unexpected release.");
|
||||
}
|
||||
if (yuvConverter != null) {
|
||||
yuvConverter.release();
|
||||
}
|
||||
GLES20.glDeleteTextures(1, new int[] {oesTextureId}, 0);
|
||||
surfaceTexture.release();
|
||||
eglBase.release();
|
||||
@ -320,12 +305,7 @@ public class SurfaceTextureHelper {
|
||||
* buffer calls returnTextureFrame() when it is released.
|
||||
*/
|
||||
public TextureBuffer createTextureBuffer(int width, int height, Matrix transformMatrix) {
|
||||
return new TextureBufferImpl(
|
||||
width, height, TextureBuffer.Type.OES, oesTextureId, transformMatrix, this, new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
returnTextureFrame();
|
||||
}
|
||||
});
|
||||
return new TextureBufferImpl(width, height, TextureBuffer.Type.OES, oesTextureId,
|
||||
transformMatrix, handler, yuvConverter, this ::returnTextureFrame);
|
||||
}
|
||||
}
|
||||
|
||||
@ -11,31 +11,32 @@
|
||||
package org.webrtc;
|
||||
|
||||
import android.graphics.Matrix;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import javax.annotation.Nullable;
|
||||
import android.os.Handler;
|
||||
|
||||
/**
|
||||
* Android texture buffer backed by a SurfaceTextureHelper's texture. The buffer calls
|
||||
* |releaseCallback| when it is released.
|
||||
* Android texture buffer that glues together the necessary information together with a generic
|
||||
* release callback. ToI420() is implemented by providing a Handler and a YuvConverter.
|
||||
*/
|
||||
class TextureBufferImpl implements VideoFrame.TextureBuffer {
|
||||
public class TextureBufferImpl implements VideoFrame.TextureBuffer {
|
||||
private final int width;
|
||||
private final int height;
|
||||
private final Type type;
|
||||
private final int id;
|
||||
private final Matrix transformMatrix;
|
||||
private final SurfaceTextureHelper surfaceTextureHelper;
|
||||
private final Handler toI420Handler;
|
||||
private final YuvConverter yuvConverter;
|
||||
private final RefCountDelegate refCountDelegate;
|
||||
|
||||
public TextureBufferImpl(int width, int height, Type type, int id, Matrix transformMatrix,
|
||||
SurfaceTextureHelper surfaceTextureHelper, @Nullable Runnable releaseCallback) {
|
||||
Handler toI420Handler, YuvConverter yuvConverter, @Nullable Runnable releaseCallback) {
|
||||
this.width = width;
|
||||
this.height = height;
|
||||
this.type = type;
|
||||
this.id = id;
|
||||
this.transformMatrix = transformMatrix;
|
||||
this.surfaceTextureHelper = surfaceTextureHelper;
|
||||
this.toI420Handler = toI420Handler;
|
||||
this.yuvConverter = yuvConverter;
|
||||
this.refCountDelegate = new RefCountDelegate(releaseCallback);
|
||||
}
|
||||
|
||||
@ -66,7 +67,8 @@ class TextureBufferImpl implements VideoFrame.TextureBuffer {
|
||||
|
||||
@Override
|
||||
public VideoFrame.I420Buffer toI420() {
|
||||
return surfaceTextureHelper.textureToYuv(this);
|
||||
return ThreadUtils.invokeAtFrontUninterruptibly(
|
||||
toI420Handler, () -> yuvConverter.convert(this));
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -82,17 +84,12 @@ class TextureBufferImpl implements VideoFrame.TextureBuffer {
|
||||
@Override
|
||||
public VideoFrame.Buffer cropAndScale(
|
||||
int cropX, int cropY, int cropWidth, int cropHeight, int scaleWidth, int scaleHeight) {
|
||||
retain();
|
||||
Matrix newMatrix = new Matrix(transformMatrix);
|
||||
newMatrix.postScale(cropWidth / (float) width, cropHeight / (float) height);
|
||||
newMatrix.postTranslate(cropX / (float) width, cropY / (float) height);
|
||||
final Matrix newMatrix = new Matrix(transformMatrix);
|
||||
newMatrix.preTranslate(cropX / (float) width, cropY / (float) height);
|
||||
newMatrix.preScale(cropWidth / (float) width, cropHeight / (float) height);
|
||||
|
||||
retain();
|
||||
return new TextureBufferImpl(
|
||||
scaleWidth, scaleHeight, type, id, newMatrix, surfaceTextureHelper, new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
release();
|
||||
}
|
||||
});
|
||||
scaleWidth, scaleHeight, type, id, newMatrix, toI420Handler, yuvConverter, this ::release);
|
||||
}
|
||||
}
|
||||
@ -171,10 +171,10 @@ public class VideoRenderer {
|
||||
yuvStrides[1], yuvPlanes[2], yuvStrides[2],
|
||||
() -> { VideoRenderer.renderFrameDone(this); });
|
||||
} else {
|
||||
// Note: surfaceTextureHelper being null means calling toI420 will crash.
|
||||
// Note: No Handler or YuvConverter means calling toI420 will crash.
|
||||
buffer = new TextureBufferImpl(width, height, VideoFrame.TextureBuffer.Type.OES, textureId,
|
||||
RendererCommon.convertMatrixToAndroidGraphicsMatrix(samplingMatrix),
|
||||
null /* surfaceTextureHelper */, () -> { VideoRenderer.renderFrameDone(this); });
|
||||
RendererCommon.convertMatrixToAndroidGraphicsMatrix(samplingMatrix), null /* handler */,
|
||||
null /* yuvConverter */, () -> VideoRenderer.renderFrameDone(this));
|
||||
}
|
||||
return new VideoFrame(buffer, rotationDegree, 0 /* timestampNs */);
|
||||
}
|
||||
|
||||
@ -18,8 +18,8 @@ import org.webrtc.VideoFrame.I420Buffer;
|
||||
import org.webrtc.VideoFrame.TextureBuffer;
|
||||
|
||||
/**
|
||||
* Class for converting OES textures to a YUV ByteBuffer. It should be constructed on a thread with
|
||||
* an active EGL context, and only be used from that thread.
|
||||
* Class for converting OES textures to a YUV ByteBuffer. It can be constructed on any thread, but
|
||||
* should only be operated from a single thread with an active EGL context.
|
||||
*/
|
||||
public class YuvConverter {
|
||||
// Vertex coordinates in Normalized Device Coordinates, i.e.
|
||||
@ -111,7 +111,7 @@ public class YuvConverter {
|
||||
// clang-format on
|
||||
|
||||
private final ThreadUtils.ThreadChecker threadChecker = new ThreadUtils.ThreadChecker();
|
||||
private final GlTextureFrameBuffer textureFrameBuffer;
|
||||
private final GlTextureFrameBuffer textureFrameBuffer = new GlTextureFrameBuffer(GLES20.GL_RGBA);
|
||||
private TextureBuffer.Type shaderTextureType;
|
||||
private GlShader shader;
|
||||
private int texMatrixLoc;
|
||||
@ -123,8 +123,7 @@ public class YuvConverter {
|
||||
* This class should be constructed on a thread that has an active EGL context.
|
||||
*/
|
||||
public YuvConverter() {
|
||||
threadChecker.checkIsOnValidThread();
|
||||
textureFrameBuffer = new GlTextureFrameBuffer(GLES20.GL_RGBA);
|
||||
threadChecker.detachThread();
|
||||
}
|
||||
|
||||
/** Converts the texture buffer to I420. */
|
||||
|
||||
Reference in New Issue
Block a user