2017-07-28 07:12:23 -07:00
|
|
|
/*
|
|
|
|
|
* 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;
|
|
|
|
|
import java.nio.ByteBuffer;
|
2018-04-09 17:51:19 +02:00
|
|
|
import java.util.concurrent.atomic.AtomicInteger;
|
|
|
|
|
import javax.annotation.Nullable;
|
2017-07-28 07:12:23 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Android texture buffer backed by a SurfaceTextureHelper's texture. The buffer calls
|
|
|
|
|
* |releaseCallback| when it is released.
|
|
|
|
|
*/
|
|
|
|
|
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;
|
2018-04-09 17:51:19 +02:00
|
|
|
private final RefCountDelegate refCountDelegate;
|
2017-07-28 07:12:23 -07:00
|
|
|
|
|
|
|
|
public TextureBufferImpl(int width, int height, Type type, int id, Matrix transformMatrix,
|
2018-04-09 17:51:19 +02:00
|
|
|
SurfaceTextureHelper surfaceTextureHelper, @Nullable Runnable releaseCallback) {
|
2017-07-28 07:12:23 -07:00
|
|
|
this.width = width;
|
|
|
|
|
this.height = height;
|
|
|
|
|
this.type = type;
|
|
|
|
|
this.id = id;
|
|
|
|
|
this.transformMatrix = transformMatrix;
|
|
|
|
|
this.surfaceTextureHelper = surfaceTextureHelper;
|
2018-04-09 17:51:19 +02:00
|
|
|
this.refCountDelegate = new RefCountDelegate(releaseCallback);
|
2017-07-28 07:12:23 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@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() {
|
2017-10-16 11:20:26 +02:00
|
|
|
return surfaceTextureHelper.textureToYuv(this);
|
2017-07-28 07:12:23 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void retain() {
|
2018-04-09 17:51:19 +02:00
|
|
|
refCountDelegate.retain();
|
2017-07-28 07:12:23 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void release() {
|
2018-04-09 17:51:19 +02:00
|
|
|
refCountDelegate.release();
|
2017-07-28 07:12:23 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@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);
|
|
|
|
|
|
|
|
|
|
return new TextureBufferImpl(
|
|
|
|
|
scaleWidth, scaleHeight, type, id, newMatrix, surfaceTextureHelper, new Runnable() {
|
|
|
|
|
@Override
|
|
|
|
|
public void run() {
|
|
|
|
|
release();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|