Sfoglia il codice sorgente

bug: 解决Strict Mode Crash的问题 close #255

hexleo 3 anni fa
parent
commit
2f988d69cf

+ 2 - 19
Android/PlayerProj/animplayer/src/main/java/com/tencent/qgame/animplayer/AnimView.kt

@@ -135,13 +135,14 @@ open class AnimView @JvmOverloads constructor(context: Context, attrs: Attribute
 
     override fun onSurfaceTextureDestroyed(surface: SurfaceTexture): Boolean {
         ALog.i(TAG, "onSurfaceTextureDestroyed")
+        this.surface = null
         player.onSurfaceTextureDestroyed()
         uiHandler.post {
             innerTextureView?.surfaceTextureListener = null
             innerTextureView = null
             removeAllViews()
         }
-        return !belowKitKat()
+        return true
     }
 
     override fun onSurfaceTextureAvailable(surface: SurfaceTexture, width: Int, height: Int) {
@@ -177,9 +178,6 @@ open class AnimView @JvmOverloads constructor(context: Context, attrs: Attribute
     override fun onDetachedFromWindow() {
         ALog.i(TAG, "onDetachedFromWindow")
         super.onDetachedFromWindow()
-        if (belowKitKat()) {
-            release()
-        }
         player.isDetachedFromWindow = true
         player.onSurfaceTextureDestroyed()
     }
@@ -310,19 +308,4 @@ open class AnimView @JvmOverloads constructor(context: Context, attrs: Attribute
         if (Looper.myLooper() == Looper.getMainLooper()) f() else uiHandler.post { f() }
     }
 
-    /**
-     * fix Error detachFromGLContext crash
-     */
-    private fun belowKitKat(): Boolean {
-        return Build.VERSION.SDK_INT <= 19
-    }
-
-    private fun release() {
-        try {
-            surface?.release()
-        } catch (error: Throwable) {
-            ALog.e(TAG, "failed to release mSurfaceTexture= $surface: ${error.message}", error)
-        }
-        surface = null
-    }
 }

+ 5 - 1
Android/PlayerProj/animplayer/src/main/java/com/tencent/qgame/animplayer/EGLUtil.kt

@@ -32,6 +32,7 @@ class EGLUtil {
     private var eglSurface: EGLSurface? = null
     private var eglContext: EGLContext? = null
     private var eglConfig: EGLConfig? = null
+    private var surface: Surface? = null
 
     init {
         eglDisplay = EGL10.EGL_NO_DISPLAY
@@ -46,7 +47,8 @@ class EGLUtil {
             val version = IntArray(2)
             egl?.eglInitialize(eglDisplay, version)
             eglConfig = chooseConfig()
-            eglSurface = egl?.eglCreateWindowSurface(eglDisplay, eglConfig, Surface(surfaceTexture), null)
+            surface = Surface(surfaceTexture)
+            eglSurface = egl?.eglCreateWindowSurface(eglDisplay, eglConfig, surface, null)
             eglContext = createContext(egl, eglDisplay, eglConfig)
             if (eglSurface == null || eglSurface == EGL10.EGL_NO_SURFACE) {
                 ALog.e(TAG, "error:${Integer.toHexString(egl?.eglGetError() ?: 0)}")
@@ -105,6 +107,8 @@ class EGLUtil {
             eglDestroySurface(eglDisplay, eglSurface)
             eglDestroyContext(eglDisplay, eglContext)
             eglTerminate(eglDisplay)
+            surface?.release()
+            surface = null
         }
     }
 

+ 6 - 2
Android/PlayerProj/animplayer/src/main/java/com/tencent/qgame/animplayer/HardDecoder.kt

@@ -33,6 +33,7 @@ class HardDecoder(player: AnimPlayer) : Decoder(player), SurfaceTexture.OnFrameA
         private const val TAG = "${Constant.TAG}.HardDecoder"
     }
 
+    private var surface: Surface? = null
     private var glTexture: SurfaceTexture? = null
     private val bufferInfo by lazy { MediaCodec.BufferInfo() }
     private var needDestroy = false
@@ -64,7 +65,7 @@ class HardDecoder(player: AnimPlayer) : Decoder(player), SurfaceTexture.OnFrameA
         renderData()
     }
 
-    fun renderData() {
+    private fun renderData() {
         renderThread.handler?.post {
             try {
                 glTexture?.apply {
@@ -160,7 +161,8 @@ class HardDecoder(player: AnimPlayer) : Decoder(player), SurfaceTexture.OnFrameA
                     )
                     configure(format, null, null, 0)
                 } else {
-                    configure(format, Surface(glTexture), null, 0)
+                    surface = Surface(glTexture)
+                    configure(format, surface, null, 0)
                 }
 
                 start()
@@ -360,6 +362,8 @@ class HardDecoder(player: AnimPlayer) : Decoder(player), SurfaceTexture.OnFrameA
                 speedControlUtil.reset()
                 player.pluginManager.onRelease()
                 render?.releaseTexture()
+                surface?.release()
+                surface = null
             } catch (e: Throwable) {
                 ALog.e(TAG, "release e=$e", e)
             }