MediaCodec decodes to GLSurfaceView
private< /span> void createCodecFromGL(){ mRtmpView = (RTMPDisplayView2)findViewById(R.id.view_preview); mRtmpView.init(); new Thread(){ @Override public void run() {super .run(); synchronized (mRtmpView.sSync){ try {mRtmpView.sSync. wait();} catch (InterruptedException e) {e.printStackTrace(); }} Surface mSurface = mRtmpView.getSurface(); mRtmpView.mSurfaceTexture.setOnFrameAvailableListener(new SurfaceTexture.OnFrameAvailableListener() {// TODO move to decode chread. @Override public void onFrameAvailable(SurfaceTexture surfaceTexture) {Log.i(TAG, "onFrameAvailable") ; mRtmpView.requestRender();} }); mVideoDecoder.init(mSurface); mVideoDecoder.start();} }.start();} ```Encoder initialization: ```java mDecoder = MediaCodec.createDecoderByType(VIDEO_TYPE ); MediaFormat format = MediaFormat.createVideoFormat(VIDEO_TYPE, 1540, 1280); format.setInteger(MediaFormat.KEY_MAX_INPUT_SIZE, 1540 * 1280); format.setInteger("durationUs", 63446722); mDecoder.start();"se-preview-section-delimiter">
The decoding thread is no longer posted. Initialization of GLSurfaceView, pay attention to the dirty mode:
setEGLContextClientVersion(2); setPreserveEGLContextOnPause(true); render = new ViewRenderer(); setRenderer(render); setRenderMode(RENDERMODE_WHEN_DIRTY); "se-preview-section-delimiter">
Create SurfaceTexture and Surface in GLThread initialization:
“`java
@Override
public void onSurfaceCreated(GL10 gl10, EGLConfig eglConfig) {
Log.i(TAG,”onSurfaceCreated”);
mProgram = createProgram(VERTEX_SHADER, FRAGMENT_SHADER);
if (mProgram == 0) {
throw new RuntimeException(“failed creating program”);
}
maPositionHandle = GLES20.glGetAttribLocation(mProgram, “aPosition”);
maTextureHandle = GLES20.glGetAttribLocation(mProgram, “aTextureCoord”) ;
muMVPMatrixHandle = GLES20.glGetUniformLocation(mProgram, “uMVPMatrix”);
muSTMatrixHandle = GLES20.glGetUniformLocation(mProgram, “uSTMatrix”);
int[] textures = new int[1];
GLES20.glGenTextures(1, textures, 0);
mTextureID = textures[0] ;
GLES20.glBindTexture(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, mTextureID);
checkGlError(“glBindTexture mTextureID”);
GLES20.glTexParameterf(GLES11Ext.GL_TEXTURE_EXT_ERNAL_TEX, GLESERN_OIL/>, .GL_NEAREST);
GLES20.glTexParameterf(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, GLES20.GL_TEXTURE_MAG_FILTER,
GLES20.GL_LINEAR);
GLES20.glTexParameteri(GLES11Ext.GL_OLESTURE,
GLES11Ext. .GL_CLAMP_TO_EDGE);
GLES20.glTexParameteri(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, GLES20.GL_TEXTURE_WRAP_T,
GLES20.GL_CLAMP_TO_EDGE);
checkGlError("glTexParameter = newTextureTexture Surface />");
mSurf ace = new Surface(mSurfaceTexture);
synchronized (sSync){
sSync.notifyAll();
}
}
@Override
public void onDrawFrame (GL10 gl10) {
mSurfaceTexture.getTransformMatrix(mSTMatrix);
mSurfaceTexture.updateTexImage();
//draw more...
}
```< /pre>The thread lock is because the GL thread will not be created until the end of the Activity OnResume life cycle. The monitoring of SurfaceTexture needs to be independent of the GL thread. So create a thread at the beginning.
Finally, post an example Shader:"`java
private static final String VERTEX_SHADER =
"uniform mat4 uMVPMatrix; " +
"uniform mat4 uSTMatrix; ”+
“attribute vec4 aPosition; ” +
“attribute vec4 aTextureCoord; ” +
“varying vec2 vTextureCoord; ” +
“void main() { " +
”gl_Position = uMVPMatrix * aPosition; ” +
” vTextureCoord = (uSTMatrix * aTextureCoord).xy; ” +
“} ”;private static final String FRAGMENT_SHADER =
"#extension GL_OES_EGL_image_external: require " +
"precision mediump float; " + // highp here doesn't seem to matter
"varying vec2 vTextureCoord ; " +
"uniform samplerExternalOES sTexture; " +
"void main() { " +
"gl_FragColor = texture2D(sTexture, vTextureCoord); " +
"} ";
```
MediaCodec decodes to GLSurfaceView
private void < span class="hljs-title">createCodecFromGL(){ mRtmpView = (RTMPDisplayView2)findViewById(R.id.view_preview); mRtmpView.init(); new span> Thread(){ @Override public void span> run() {super.run(); synchronized (mRtmpView.sSync){ try {mRtmpView.sSync.wait();} catch (InterruptedException e) {e.printStackTrace();}} Surface mSurface = mR tmpView.getSurface(); mRtmpView.mSurfaceTexture.setOnFrameAvailableListener(new SurfaceTexture.OnFrameAvailableListener() {//TODO move to decode chread . @Override public void onFrameAvailable(SurfaceTexture surfaceTexture) {Log.i(TAG, "onFrameAvailable"); mRtmpView.requestRender( );} }); mVideoDecoder.init(mSurface); mVideoDecoder.start();} }.start();} ```Encoder initialization: ```java mDecoder = MediaCodec.createDecoderByType(VIDEO_TYPE); MediaFormat format = MediaFormat.createVideoFormat(VIDEO_TYPE, 1540, 1280); format.setInteger(MediaFormat.KEY_MAX_INPUT_SIZE, 1540 * 1280); format.setInteger("durationUs", 63446722); mDecoder.start();"se-preview-section-delimiter">
The decoding thread is not posted anymore. Initialization of GLSurfaceView, pay attention to the dirty mode:
setEGLContextClientVersion(2); setPreserveEGLContextOnPause(true); render = new ViewRenderer(); setRenderer(render); setRenderMode(RENDERMODE_WHEN_DIRTY); "se-preview-section-delimiter">
Create SurfaceTexture and Surface in GLThread initialization:
"`java
@Override
public void onSurfaceCreated(GL10 gl10, EGLConfig eglConfig) {
Log.i(TAG,”onSurfaceCreated”);
mProgram = createProgram(VERTEX_SHADER, FRAGMENT_SHADER);
if (mProgram == 0) {
throw new RuntimeException(“failed creating program”);
}
maPositionHandle = GLES20.glGetAttribLocation(mProgram, “aPosition”);
maTextureHandle = GLES20.glGetAttribLocation(mProgram, “aTextureCoord”) ;
muMVPMatrixHandle = GLES20.glGetUniformLocation(mProgram, “uMVPMatrix”);
mu STMatrixHandle = GLES20.glGetUniformLocation(mProgram, “uSTMatrix”);
int[] textures = new int[1];
GLES20.glGenTextures(1, textures, 0);
mTextureID = textures[0] ;
GLES20.glBindTexture(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, mTextureID);
checkGlError(“glBindTexture mTextureID”);
GLES20.glTexParameterf(GLES11Ext.GL_TEXTURE_EXT_ERNAL_TEX, GLESERN_OIL/>, .GL_NEAREST);
GLES20.glTexParameterf(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, GLES20.GL_TEXTURE_MAG_FILTER,
GLES20.GL_LINEAR);
GLES20.glTexParameteri(GLES11Ext.GL_OLESTURE,
GLES11Ext. .GL_CLAMP_TO_EDGE);
GLES20.glTexParameteri(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, GLES20.GL_TEXTURE_WRAP_T,
GLES20.GL_CLAMP_TO_EDGE);
checkGlError("glTexParameter = newTextureTexture Surface />");
mSurface = new Surface(mSurfaceTexture);
synchronized (sSync){
sSync.notifyAll();
}
}
@Override
public void onDrawFrame( GL10 gl10) {
mSurfaceTexture.getTransformMatrix(mSTMatrix);
mSurfaceTexture.updateTexImage();
//draw more...
}
``` pre>The thread lock is because the GL thread will not be created until the end of the Activity OnResume life cycle. The monitoring of SurfaceTexture needs to be independent of the GL thread. So create a thread at the beginning.
Finally, post an example Shader:"`java
private static final String VERTEX_SHADER =
"uniform mat4 uMVPMatrix; " +
"uniform mat4 uSTMatrix; ”+
“attribute vec4 aPosition; ” +
“attribute vec4 aTextureCoord; ” +
“varying vec2 vTextureCoord; ” +
“void main() { " +
”gl_Position = uMVPMatrix * aPosition; ” +
” vTextureCoord = (uSTMatrix * aTextureCoord).xy; ” +
“} ”;private static final String FRAGMENT_SHADER =
"#extension GL_OES_EGL_image_external: require " +
"precision mediump float; " + // highp here doesn't seem to matter
"varying vec2 vTextureCoord ; " +
"uniform samplerExternalOES sTexture; " +
"void main() { " +
"gl_FragColor = texture2D(sTexture, vTextureCoord); " +
"} ";
```