2828import com .yanzhenjie .permission .runtime .Permission ;
2929
3030import java .io .IOException ;
31+ import java .util .concurrent .Callable ;
3132
3233import io .agora .api .example .MainApplication ;
3334import io .agora .api .example .R ;
3839import io .agora .api .example .common .gles .core .GlUtil ;
3940import io .agora .api .example .utils .CommonUtil ;
4041import io .agora .base .TextureBuffer ;
42+ import io .agora .base .TextureBufferHelper ;
4143import io .agora .base .VideoFrame ;
44+ import io .agora .base .internal .video .EglBase ;
45+ import io .agora .base .internal .video .EglBase14 ;
4246import io .agora .base .internal .video .RendererCommon ;
4347import io .agora .base .internal .video .YuvConverter ;
4448import io .agora .rtc2 .ChannelMediaOptions ;
@@ -90,6 +94,18 @@ public class PushExternalVideo extends BaseFragment implements View.OnClickListe
9094 private int mSurfaceWidth ;
9195 private int mSurfaceHeight ;
9296 private boolean mTextureDestroyed ;
97+ private volatile static boolean glPrepared ;
98+ private volatile TextureBufferHelper textureBufferHelper ;
99+
100+ private boolean prepareGl (EglBase .Context eglContext , final int width , final int height ) {
101+ Log .d (TAG , "prepareGl" );
102+ textureBufferHelper = TextureBufferHelper .create ("STProcess" , eglContext );
103+ if (textureBufferHelper == null ) {
104+ return false ;
105+ }
106+ Log .d (TAG , "prepareGl completed" );
107+ return true ;
108+ }
93109
94110 @ Nullable
95111 @ Override
@@ -171,6 +187,11 @@ public void onDestroy() {
171187 * triggers the removeInjectStreamUrl method.*/
172188 engine .leaveChannel ();
173189 engine .stopPreview ();
190+ if (textureBufferHelper != null )
191+ {
192+ textureBufferHelper .dispose ();
193+ textureBufferHelper = null ;
194+ }
174195 }
175196 handler .post (RtcEngine ::destroy );
176197 engine = null ;
@@ -325,18 +346,16 @@ public void onFrameAvailable(SurfaceTexture surfaceTexture) {
325346 mEglCore .swapBuffers (mDrawSurface );
326347
327348 if (joined ) {
328- /**about AgoraVideoFrame, see https://docs.agora.io/en/Video/API%20Reference/java/classio_1_1agora_1_1rtc_1_1video_1_1_agora_video_frame.html*/
329- VideoFrame .TextureBuffer buffer = new TextureBuffer (
330- mEglCore .getEGLContext (),
331- DEFAULT_CAPTURE_HEIGHT /* For simplicity, swap frame width and height */ ,
332- DEFAULT_CAPTURE_WIDTH /* For simplicity, swap frame width and height */ ,
333- VideoFrame .TextureBuffer .Type .OES ,
334- mPreviewTexture ,
335- RendererCommon .convertMatrixToAndroidGraphicsMatrix (mTransform ),
336- mHandler ,
337- mYuvConverter ,
338- null /* for simplicity just pass null, if you want to avoid texture in use case, you can use this callback*/ );
339- VideoFrame frame = new VideoFrame (buffer , 0 , timestampNs );
349+ VideoFrame .Buffer buffer = textureBufferHelper .invoke (new Callable <VideoFrame .Buffer >() {
350+ @ Override
351+ public VideoFrame .Buffer call () throws Exception
352+ {
353+ return textureBufferHelper .wrapTextureBuffer ( DEFAULT_CAPTURE_HEIGHT ,
354+ DEFAULT_CAPTURE_WIDTH , VideoFrame .TextureBuffer .Type .OES , mPreviewTexture ,
355+ RendererCommon .convertMatrixToAndroidGraphicsMatrix (mTransform ));
356+ }
357+ });
358+ VideoFrame frame = new VideoFrame (buffer , 0 , System .nanoTime ());
340359 /**Pushes the video frame using the AgoraVideoFrame class and passes the video frame to the Agora SDK.
341360 * Call the setExternalVideoSource method and set pushMode as true before calling this
342361 * method. Otherwise, a failure returns after calling this method.
@@ -347,7 +366,7 @@ public void onFrameAvailable(SurfaceTexture surfaceTexture) {
347366 * PS:
348367 * In the Communication profile, the SDK does not support textured video frames.*/
349368 boolean a = engine .pushExternalVideoFrame (frame );
350- Log .e (TAG , "pushExternalVideoFrame:" + a );
369+ Log .d (TAG , "pushExternalVideoFrame:" + a );
351370 }
352371 }
353372
@@ -363,6 +382,11 @@ public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int hei
363382 * */
364383 mHandler = new Handler (Looper .myLooper ());
365384 mEglCore = new EglCore ();
385+ if (!glPrepared ){
386+ // setup egl context
387+ EglBase .Context eglContext = new EglBase14 .Context (mEglCore .getEGLContext ());
388+ glPrepared = prepareGl (eglContext , width , height );
389+ }
366390 mDummySurface = mEglCore .createOffscreenSurface (1 , 1 );
367391 mEglCore .makeCurrent (mDummySurface );
368392 mPreviewTexture = GlUtil .createTextureObject (GLES11Ext .GL_TEXTURE_EXTERNAL_OES );
0 commit comments