package com.tgdz.gb28181.gl.playvideo_texuture;
import android.content.Context;
import android.graphics.SurfaceTexture;
import android.opengl.GLES30;
import android.opengl.GLUtils;
import android.util.Log;
import android.view.Surface;
import com.blankj.utilcode.util.LogUtils;
import com.km.myapplica.R;
import com.serenegiant.glutils.ShaderConst;
import com.tgdz.gb28181.constant.AreaConstant;
import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.nio.ShortBuffer;
/* loaded from: classes.dex */
public class VideoTextureSurfaceRenderer extends TextureSurfaceRenderer implements SurfaceTexture.OnFrameAvailableListener {
public static final String TAG = "VideoTextureSurfaceRenderer";
private static final float squareSize = 1.0f;
private long beginFrameTime;
private Context context;
private ShortBuffer drawListBuffer;
private int frameCount;
private SurfaceTexture inputTexture;
int positionHandle;
private int rotation;
private int shaderProgram;
private FloatBuffer textureBuffer;
int textureCoordinateHandle;
int textureParamHandle;
int textureTranformHandle;
private int[] textures;
private FloatBuffer vertexBuffer;
private float[] videoTextureTransform;
public WaterMark waterMark;
private static final float[] squareCoords180 = {1.0f, 1.0f, -1.0f, 1.0f, -1.0f, -1.0f, 1.0f, -1.0f};
private static final float[] squareCoords90 = {-1.0f, 1.0f, -1.0f, -1.0f, 1.0f, -1.0f, 1.0f, 1.0f};
/*private static final float[] squareCoords = {
-squareSize, squareSize, 0.0f, // top left
-squareSize, -squareSize, 0.0f, // bottom left
squareSize, -squareSize, 0.0f, // bottom right
squareSize, squareSize, 0.0f // top right
};*/
private static final float[] squareCoords = {-1.0f, -1.0f, 1.0f, -1.0f, 1.0f, 1.0f, -1.0f, 1.0f};
private static final short[] drawOrder = {0, 1, 2, 0, 2, 3};
private static final float[] textureCoords = {
0.0f, 1.0f, 0.0f, 1.0f, //左上
0.0f, 0.0f, 0.0f, 1.0f, //左下
1.0f, 0.0f, 0.0f, 1.0f, //右下
1.0f, 1.0f, 0.0f, 1.0f //右上
};
public VideoTextureSurfaceRenderer(Context context, Surface surface, int width, int heigght, int framerate) {
this(context, surface, width, heigght, framerate, -1);
}
public VideoTextureSurfaceRenderer(Context context, Surface surface, int i, int i2, int i3, int rotation) {
this(context, surface, i, i2, i3, rotation, null);
}
public VideoTextureSurfaceRenderer(Context context, Surface surface, int i, int i2, int i3, int i4, Surface surface2) {
super(surface, i, i2, i3, surface2);
this.textures = new int[1];
this.beginFrameTime = 0L;
this.frameCount = 0;
this.rotation = i4;
this.context = context;
this.videoTextureTransform = new float[16];
}
private void setupGraphics() {
this.shaderProgram = ShaderHelper.createAndLinkProgram(ShaderHelper.compileShader(35633, RawResourceReader.readTextFileFromRawResource(this.context, R.raw.vetext_sharder)), ShaderHelper.compileShader(35632, RawResourceReader.readTextFileFromRawResource(this.context, R.raw.fragment_sharder)), new String[]{"texture", "vPosition", "vTexCoordinate", "textureTransform"});
this.textureParamHandle = GLES30.glGetUniformLocation(this.shaderProgram, "texture");
this.textureCoordinateHandle = GLES30.glGetAttribLocation(this.shaderProgram, "vTexCoordinate");
this.positionHandle = GLES30.glGetAttribLocation(this.shaderProgram, "vPosition");
this.textureTranformHandle = GLES30.glGetUniformLocation(this.shaderProgram, "textureTransform");
}
/* JADX WARN: Can't fix incorrect switch cases order, some code will duplicate */
/* JADX WARN: Code restructure failed: missing block: B:173:0x00f9, code lost:
if (r3.equals("P290C") != false) goto L18;
*/
/*
Code decompiled incorrectly, please refer to instructions dump.
To view partially-correct code enable 'Show inconsistent code' option in preferences
*/
private void setupVertexBuffer() {
// 定义顶点数据 (例如:一个简单的矩形)
/* float[] vertexData = {
// x, y, z, u, v
-1.0f, -1.0f, 0.0f, 0f, 0f,
-1.0f, 1.0f, 0.0f, 0f, 1f,
1.0f, 1.0f, 0.0f, 1f, 1f,
1.0f, -1.0f, 0.0f, 1f, 0f
};
final float[] TEX_VERTEX = {
0.5f, 0.5f, //纹理坐标V0
1f, 1f, //纹理坐标V1
0f, 1f, //纹理坐标V2
0f, 0.0f, //纹理坐标V3
1f, 0.0f //纹理坐标V4
};
final short[] VERTEX_INDEX = {
2, 1, 0, 0, 3, 2
};
// 创建顶点缓冲区对象 (VBO)
int[] vbo = new int[1];
GLES30.glGenBuffers(1, vbo, 0);
if (vbo[0] == 0) {
throw new RuntimeException("无法创建顶点缓冲区对象");
}
// 绑定缓冲区对象
GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, vbo[0]);
// 将顶点数据传递到缓冲区
vertexBuffer = ByteBuffer
.allocateDirect(vertexData.length * 4) // 每个 float 占 4 字节
.order(ByteOrder.nativeOrder())
.asFloatBuffer();
vertexBuffer.put(vertexData).position(0);
drawListBuffer = ByteBuffer.allocateDirect(VERTEX_INDEX.length * 2)
.order(ByteOrder.nativeOrder())
.asShortBuffer()
.put(VERTEX_INDEX);
drawListBuffer.position(0);
GLES30.glBufferData(
GLES30.GL_ARRAY_BUFFER,
vertexData.length * 4,
vertexBuffer,
GLES30.GL_STATIC_DRAW
);
// 解绑缓冲区,防止意外修改
GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, 0);
ByteBuffer orderByteBuffer = ByteBuffer.allocateDirect(drawOrder. length * 2);
orderByteBuffer.order(ByteOrder.nativeOrder()); //Modifies this buffer's byte order
drawOrderBuffer = orderByteBuffer.asShortBuffer(); //创建此缓冲区的视图,作为一个short缓冲区.
drawOrderBuffer.put(drawOrder);
drawOrderBuffer.position(0); //下一个要被读或写的元素的索引,从0 开始*/
// Initialize the texture holder
if (rotation ==1){
ByteBuffer bb = ByteBuffer.allocateDirect(squareCoords90.length * 4);
bb.order(ByteOrder.nativeOrder());
vertexBuffer = bb.asFloatBuffer();
vertexBuffer.put(squareCoords90);
vertexBuffer.position(0);
}else {
ByteBuffer bb = ByteBuffer.allocateDirect(squareCoords.length * 4);
bb.order(ByteOrder.nativeOrder());
vertexBuffer = bb.asFloatBuffer();
vertexBuffer.put(squareCoords);
vertexBuffer.position(0);
}
drawListBuffer = ByteBuffer.allocateDirect(drawOrder.length * 2)
.order(ByteOrder.nativeOrder())
.asShortBuffer()
.put(drawOrder);
drawListBuffer.position(0);
}
private void setupTexture() {
ByteBuffer allocateDirect = ByteBuffer.allocateDirect(textureCoords.length * 4);
allocateDirect.order(ByteOrder.nativeOrder());
this.textureBuffer = allocateDirect.asFloatBuffer();
this.textureBuffer.put(textureCoords);
this.textureBuffer.position(0);
GLES30.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
GLES30.glActiveTexture(33984);
if (this.textures == null) {
this.textures = new int[1];
}
GLES30.glGenTextures(1, this.textures, 0);//// 生成纹理ID
checkGlError("Texture generate");
GLES30.glBindTexture(ShaderConst.GL_TEXTURE_EXTERNAL_OES, this.textures[0]);// 绑定纹理
checkGlError("Texture bind");
this.inputTexture = new SurfaceTexture(this.textures[0]);
this.inputTexture.setOnFrameAvailableListener(this, this.backgroundThread.handler);
}
@Override // com.tgdz.gb28181.gl.playvideo_texuture.TextureSurfaceRenderer
protected boolean draw(boolean z) {
GLES30.glClear(16384);
drawTexture();
if (z) {
this.waterMark.draw();
return true;
}
return true;
}
private void drawTexture() {
GLES30.glUseProgram(this.shaderProgram);
GLES30.glViewport(0, 0, this.width, this.height);
GLES30.glEnableVertexAttribArray(this.positionHandle);
GLES30.glVertexAttribPointer(this.positionHandle, 2, 5126, false, 0, (Buffer) this.vertexBuffer);
GLES30.glBindTexture(ShaderConst.GL_TEXTURE_EXTERNAL_OES, this.textures[0]);
GLES30.glActiveTexture(33984);
GLES30.glUniform1i(this.textureParamHandle, 0);
GLES30.glEnableVertexAttribArray(this.textureCoordinateHandle);
GLES30.glVertexAttribPointer(this.textureCoordinateHandle, 4, 5126, false, 0, (Buffer) this.textureBuffer);
GLES30.glUniformMatrix4fv(this.textureTranformHandle, 1, false, this.videoTextureTransform, 0);
GLES30.glDrawElements(5, drawOrder.length, 5123, this.drawListBuffer);
GLES30.glDisableVertexAttribArray(this.positionHandle);
GLES30.glDisableVertexAttribArray(this.textureCoordinateHandle);
}
@Override // com.tgdz.gb28181.gl.playvideo_texuture.TextureSurfaceRenderer
protected void initGLComponents() {
setupVertexBuffer();
setupTexture();
setupGraphics();
this.waterMark = new WaterMark(this.context, this.width, this.height);
}
@Override // com.tgdz.gb28181.gl.playvideo_texuture.TextureSurfaceRenderer
protected void deinitGLComponents() {
GLES30.glDeleteTextures(1, this.textures, 0);
GLES30.glDeleteProgram(this.shaderProgram);
this.waterMark.release();
}
public void checkGlError(String str) {
while (true) {
int glGetError = GLES30.glGetError();
if (glGetError == 0) {
return;
}
Log.e("SurfaceTest", str + ": glError " + GLUtils.getEGLErrorString(glGetError));
}
}
@Override // com.tgdz.gb28181.gl.playvideo_texuture.TextureSurfaceRenderer
public SurfaceTexture getInputTexture() {
return this.inputTexture;
}
@Override // android.graphics.SurfaceTexture.OnFrameAvailableListener
public void onFrameAvailable(SurfaceTexture surfaceTexture) {
LogUtils.i("onFrameAvailable可用");
long currentTimeMillis = System.currentTimeMillis();
if (this.beginFrameTime == 0) {
this.beginFrameTime = currentTimeMillis;
}
float f = ((this.frameCount * 1000.0f) / this.frameRate) - ((float) (currentTimeMillis - this.beginFrameTime));
if (f > 0.0f) {
Log.i(TAG, "onFrameAvailable: 延迟时间: "+f + "ms");
try {
Thread.sleep((long) f);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
this.frameCount++;
try {
this.inputTexture.updateTexImage();
this.inputTexture.getTransformMatrix(this.videoTextureTransform);
super.onFrameAvailable();
} catch (Exception e2) {
e2.printStackTrace();
}
}
@Override // com.tgdz.gb28181.gl.playvideo_texuture.TextureSurfaceRenderer
public void destroy() {
super.destroy();
}
}
为什么旋转输出的画面不是居中显示,而是靠左边呢?
我这个画面输出是向右的,能帮我修改成 旋转90,拉伸的话从中间放大截取
最新发布