webrtc_m130/sdk/android/api/org/webrtc/TextureBufferImpl.java

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

203 lines
6.8 KiB
Java
Raw Normal View History

/*
* Copyright 2017 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
package org.webrtc;
import android.graphics.Matrix;
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}
2018-04-16 16:28:29 +02:00
import android.os.Handler;
import androidx.annotation.Nullable;
/**
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}
2018-04-16 16:28:29 +02:00
* 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.
*/
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}
2018-04-16 16:28:29 +02:00
public class TextureBufferImpl implements VideoFrame.TextureBuffer {
interface RefCountMonitor {
void onRetain(TextureBufferImpl textureBuffer);
void onRelease(TextureBufferImpl textureBuffer);
void onDestroy(TextureBufferImpl textureBuffer);
}
// This is the full resolution the texture has in memory after applying the transformation matrix
// that might include cropping. This resolution is useful to know when sampling the texture to
// avoid downscaling artifacts.
private final int unscaledWidth;
private final int unscaledHeight;
// This is the resolution that has been applied after cropAndScale().
private final int width;
private final int height;
private final Type type;
private final int id;
private final Matrix transformMatrix;
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}
2018-04-16 16:28:29 +02:00
private final Handler toI420Handler;
private final YuvConverter yuvConverter;
private final RefCountDelegate refCountDelegate;
private final RefCountMonitor refCountMonitor;
public TextureBufferImpl(int width, int height, Type type, int id, Matrix transformMatrix,
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}
2018-04-16 16:28:29 +02:00
Handler toI420Handler, YuvConverter yuvConverter, @Nullable Runnable releaseCallback) {
this(width, height, width, height, type, id, transformMatrix, toI420Handler, yuvConverter,
new RefCountMonitor() {
@Override
public void onRetain(TextureBufferImpl textureBuffer) {}
@Override
public void onRelease(TextureBufferImpl textureBuffer) {}
@Override
public void onDestroy(TextureBufferImpl textureBuffer) {
if (releaseCallback != null) {
releaseCallback.run();
}
}
});
}
TextureBufferImpl(int width, int height, Type type, int id, Matrix transformMatrix,
Handler toI420Handler, YuvConverter yuvConverter, RefCountMonitor refCountMonitor) {
this(width, height, width, height, type, id, transformMatrix, toI420Handler, yuvConverter,
refCountMonitor);
}
private TextureBufferImpl(int unscaledWidth, int unscaledHeight, int width, int height, Type type,
int id, Matrix transformMatrix, Handler toI420Handler, YuvConverter yuvConverter,
RefCountMonitor refCountMonitor) {
this.unscaledWidth = unscaledWidth;
this.unscaledHeight = unscaledHeight;
this.width = width;
this.height = height;
this.type = type;
this.id = id;
this.transformMatrix = transformMatrix;
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}
2018-04-16 16:28:29 +02:00
this.toI420Handler = toI420Handler;
this.yuvConverter = yuvConverter;
this.refCountDelegate = new RefCountDelegate(() -> refCountMonitor.onDestroy(this));
this.refCountMonitor = refCountMonitor;
}
@Override
public VideoFrame.TextureBuffer.Type getType() {
return type;
}
@Override
public int getTextureId() {
return id;
}
@Override
public Matrix getTransformMatrix() {
return transformMatrix;
}
@Override
public int getWidth() {
return width;
}
@Override
public int getHeight() {
return height;
}
@Override
public VideoFrame.I420Buffer toI420() {
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}
2018-04-16 16:28:29 +02:00
return ThreadUtils.invokeAtFrontUninterruptibly(
toI420Handler, () -> yuvConverter.convert(this));
}
@Override
public void retain() {
refCountMonitor.onRetain(this);
refCountDelegate.retain();
}
@Override
public void release() {
refCountMonitor.onRelease(this);
refCountDelegate.release();
}
@Override
public VideoFrame.Buffer cropAndScale(
int cropX, int cropY, int cropWidth, int cropHeight, int scaleWidth, int scaleHeight) {
final Matrix cropAndScaleMatrix = new Matrix();
// In WebRTC, Y=0 is the top row, while in OpenGL Y=0 is the bottom row. This means that the Y
// direction is effectively reversed.
final int cropYFromBottom = height - (cropY + cropHeight);
cropAndScaleMatrix.preTranslate(cropX / (float) width, cropYFromBottom / (float) height);
cropAndScaleMatrix.preScale(cropWidth / (float) width, cropHeight / (float) height);
return applyTransformMatrix(cropAndScaleMatrix,
(int) Math.round(unscaledWidth * cropWidth / (float) width),
(int) Math.round(unscaledHeight * cropHeight / (float) height), scaleWidth, scaleHeight);
}
/**
* Returns the width of the texture in memory. This should only be used for downscaling, and you
* should still respect the width from getWidth().
*/
public int getUnscaledWidth() {
return unscaledWidth;
}
/**
* Returns the height of the texture in memory. This should only be used for downscaling, and you
* should still respect the height from getHeight().
*/
public int getUnscaledHeight() {
return unscaledHeight;
}
public Handler getToI420Handler() {
return toI420Handler;
}
public YuvConverter getYuvConverter() {
return yuvConverter;
}
/**
* Create a new TextureBufferImpl with an applied transform matrix and a new size. The
* existing buffer is unchanged. The given transform matrix is applied first when texture
* coordinates are still in the unmodified [0, 1] range.
*/
public TextureBufferImpl applyTransformMatrix(
Matrix transformMatrix, int newWidth, int newHeight) {
return applyTransformMatrix(transformMatrix, /* unscaledWidth= */ newWidth,
/* unscaledHeight= */ newHeight, /* scaledWidth= */ newWidth,
/* scaledHeight= */ newHeight);
}
private TextureBufferImpl applyTransformMatrix(Matrix transformMatrix, int unscaledWidth,
int unscaledHeight, int scaledWidth, int scaledHeight) {
final Matrix newMatrix = new Matrix(this.transformMatrix);
newMatrix.preConcat(transformMatrix);
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}
2018-04-16 16:28:29 +02:00
retain();
return new TextureBufferImpl(unscaledWidth, unscaledHeight, scaledWidth, scaledHeight, type, id,
newMatrix, toI420Handler, yuvConverter, new RefCountMonitor() {
@Override
public void onRetain(TextureBufferImpl textureBuffer) {
refCountMonitor.onRetain(TextureBufferImpl.this);
}
@Override
public void onRelease(TextureBufferImpl textureBuffer) {
refCountMonitor.onRelease(TextureBufferImpl.this);
}
@Override
public void onDestroy(TextureBufferImpl textureBuffer) {
release();
}
});
}
}