Browse Source

feat: 抽象IFileContainer接口

hexleo 5 years ago
parent
commit
978775f4a7

+ 3 - 2
Android/PlayerProj/animplayer/src/main/java/com/tencent/qgame/animplayer/AnimConfigManager.kt

@@ -16,6 +16,7 @@
 package com.tencent.qgame.animplayer
 package com.tencent.qgame.animplayer
 
 
 import android.os.SystemClock
 import android.os.SystemClock
+import com.tencent.qgame.animplayer.file.IFileContainer
 import com.tencent.qgame.animplayer.util.ALog
 import com.tencent.qgame.animplayer.util.ALog
 import org.json.JSONObject
 import org.json.JSONObject
 import java.nio.charset.Charset
 import java.nio.charset.Charset
@@ -36,7 +37,7 @@ class AnimConfigManager(val player: AnimPlayer) {
      * 解析配置
      * 解析配置
      * @return true 解析成功 false 解析失败
      * @return true 解析成功 false 解析失败
      */
      */
-    fun parseConfig(fileContainer: FileContainer, enableVersion1: Boolean, defaultVideoMode: Int, defaultFps: Int): Int {
+    fun parseConfig(fileContainer: IFileContainer, enableVersion1: Boolean, defaultVideoMode: Int, defaultFps: Int): Int {
         try {
         try {
             isParsingConfig = true
             isParsingConfig = true
             // 解析vapc
             // 解析vapc
@@ -87,7 +88,7 @@ class AnimConfigManager(val player: AnimPlayer) {
     }
     }
 
 
 
 
-    private fun parse(fileContainer: FileContainer, defaultVideoMode: Int, defaultFps: Int): Boolean {
+    private fun parse(fileContainer: IFileContainer, defaultVideoMode: Int, defaultFps: Int): Boolean {
 
 
         val config = AnimConfig()
         val config = AnimConfig()
         this.config = config
         this.config = config

+ 3 - 2
Android/PlayerProj/animplayer/src/main/java/com/tencent/qgame/animplayer/AnimPlayer.kt

@@ -15,6 +15,7 @@
  */
  */
 package com.tencent.qgame.animplayer
 package com.tencent.qgame.animplayer
 
 
+import com.tencent.qgame.animplayer.file.IFileContainer
 import com.tencent.qgame.animplayer.inter.IAnimListener
 import com.tencent.qgame.animplayer.inter.IAnimListener
 import com.tencent.qgame.animplayer.mask.MaskConfig
 import com.tencent.qgame.animplayer.mask.MaskConfig
 import com.tencent.qgame.animplayer.plugin.AnimPluginManager
 import com.tencent.qgame.animplayer.plugin.AnimPluginManager
@@ -71,7 +72,7 @@ class AnimPlayer(val animView: IAnimView) {
         decoder?.onSurfaceSizeChanged(width, height)
         decoder?.onSurfaceSizeChanged(width, height)
     }
     }
 
 
-    fun startPlay(fileContainer: FileContainer) {
+    fun startPlay(fileContainer: IFileContainer) {
         isStartRunning = true
         isStartRunning = true
         prepareDecoder()
         prepareDecoder()
         if (decoder?.prepareThread() == false) {
         if (decoder?.prepareThread() == false) {
@@ -98,7 +99,7 @@ class AnimPlayer(val animView: IAnimView) {
         }
         }
     }
     }
 
 
-    private fun innerStartPlay(fileContainer: FileContainer) {
+    private fun innerStartPlay(fileContainer: IFileContainer) {
         synchronized(AnimPlayer::class.java) {
         synchronized(AnimPlayer::class.java) {
             if (isSurfaceAvailable) {
             if (isSurfaceAvailable) {
                 isStartRunning = false
                 isStartRunning = false

+ 6 - 4
Android/PlayerProj/animplayer/src/main/java/com/tencent/qgame/animplayer/AnimView.kt

@@ -22,10 +22,12 @@ import android.os.Build
 import android.os.Handler
 import android.os.Handler
 import android.os.Looper
 import android.os.Looper
 import android.util.AttributeSet
 import android.util.AttributeSet
-import android.view.MotionEvent
 import android.view.TextureView
 import android.view.TextureView
 import android.view.View
 import android.view.View
 import android.widget.FrameLayout
 import android.widget.FrameLayout
+import com.tencent.qgame.animplayer.file.AssetsFileContainer
+import com.tencent.qgame.animplayer.file.FileContainer
+import com.tencent.qgame.animplayer.file.IFileContainer
 import com.tencent.qgame.animplayer.inter.IAnimListener
 import com.tencent.qgame.animplayer.inter.IAnimListener
 import com.tencent.qgame.animplayer.inter.IFetchResource
 import com.tencent.qgame.animplayer.inter.IFetchResource
 import com.tencent.qgame.animplayer.inter.OnResourceClickListener
 import com.tencent.qgame.animplayer.inter.OnResourceClickListener
@@ -51,7 +53,7 @@ open class AnimView @JvmOverloads constructor(context: Context, attrs: Attribute
     private var surface: SurfaceTexture? = null
     private var surface: SurfaceTexture? = null
     private var animListener: IAnimListener? = null
     private var animListener: IAnimListener? = null
     private var innerTextureView: InnerTextureView? = null
     private var innerTextureView: InnerTextureView? = null
-    private var lastFile: FileContainer? = null
+    private var lastFile: IFileContainer? = null
     private val scaleTypeUtil = ScaleTypeUtil()
     private val scaleTypeUtil = ScaleTypeUtil()
 
 
     // 代理监听
     // 代理监听
@@ -235,7 +237,7 @@ open class AnimView @JvmOverloads constructor(context: Context, attrs: Attribute
 
 
     override fun startPlay(assetManager: AssetManager, assetsPath: String) {
     override fun startPlay(assetManager: AssetManager, assetsPath: String) {
         try {
         try {
-            val fileContainer = FileContainer(assetManager, assetsPath)
+            val fileContainer = AssetsFileContainer(assetManager, assetsPath)
             startPlay(fileContainer)
             startPlay(fileContainer)
         } catch (e: Throwable) {
         } catch (e: Throwable) {
             animProxyListener.onFailed(Constant.REPORT_ERROR_TYPE_FILE_ERROR, Constant.ERROR_MSG_FILE_ERROR)
             animProxyListener.onFailed(Constant.REPORT_ERROR_TYPE_FILE_ERROR, Constant.ERROR_MSG_FILE_ERROR)
@@ -243,7 +245,7 @@ open class AnimView @JvmOverloads constructor(context: Context, attrs: Attribute
     }
     }
 
 
 
 
-    override fun startPlay(fileContainer: FileContainer) {
+    override fun startPlay(fileContainer: IFileContainer) {
         ui {
         ui {
             if (visibility != View.VISIBLE) {
             if (visibility != View.VISIBLE) {
                 ALog.e(TAG, "AnimView is GONE, can't play")
                 ALog.e(TAG, "AnimView is GONE, can't play")

+ 3 - 2
Android/PlayerProj/animplayer/src/main/java/com/tencent/qgame/animplayer/AudioPlayer.kt

@@ -16,6 +16,7 @@
 package com.tencent.qgame.animplayer
 package com.tencent.qgame.animplayer
 
 
 import android.media.*
 import android.media.*
+import com.tencent.qgame.animplayer.file.IFileContainer
 import com.tencent.qgame.animplayer.util.ALog
 import com.tencent.qgame.animplayer.util.ALog
 import com.tencent.qgame.animplayer.util.MediaUtil
 import com.tencent.qgame.animplayer.util.MediaUtil
 import java.lang.RuntimeException
 import java.lang.RuntimeException
@@ -41,7 +42,7 @@ class AudioPlayer(val player: AnimPlayer) {
         return Decoder.createThread(decodeThread, "anim_audio_thread")
         return Decoder.createThread(decodeThread, "anim_audio_thread")
     }
     }
 
 
-    fun start(fileContainer: FileContainer) {
+    fun start(fileContainer: IFileContainer) {
         isStopReq = false
         isStopReq = false
         needDestroy = false
         needDestroy = false
         if (!prepareThread()) return
         if (!prepareThread()) return
@@ -63,7 +64,7 @@ class AudioPlayer(val player: AnimPlayer) {
         isStopReq = true
         isStopReq = true
     }
     }
 
 
-    private fun startPlay(fileContainer: FileContainer) {
+    private fun startPlay(fileContainer: IFileContainer) {
         val extractor = MediaUtil.getExtractor(fileContainer)
         val extractor = MediaUtil.getExtractor(fileContainer)
         this.extractor = extractor
         this.extractor = extractor
         val audioIndex = MediaUtil.selectAudioTrack(extractor)
         val audioIndex = MediaUtil.selectAudioTrack(extractor)

+ 2 - 1
Android/PlayerProj/animplayer/src/main/java/com/tencent/qgame/animplayer/Decoder.kt

@@ -18,6 +18,7 @@ package com.tencent.qgame.animplayer
 import android.os.Build
 import android.os.Build
 import android.os.HandlerThread
 import android.os.HandlerThread
 import android.os.Handler
 import android.os.Handler
+import com.tencent.qgame.animplayer.file.IFileContainer
 import com.tencent.qgame.animplayer.inter.IAnimListener
 import com.tencent.qgame.animplayer.inter.IAnimListener
 import com.tencent.qgame.animplayer.util.ALog
 import com.tencent.qgame.animplayer.util.ALog
 import com.tencent.qgame.animplayer.util.SpeedControlUtil
 import com.tencent.qgame.animplayer.util.SpeedControlUtil
@@ -70,7 +71,7 @@ abstract class Decoder(val player: AnimPlayer) : IAnimListener {
     var isStopReq = false // 是否需要停止
     var isStopReq = false // 是否需要停止
     val speedControlUtil by lazy { SpeedControlUtil() }
     val speedControlUtil by lazy { SpeedControlUtil() }
 
 
-    abstract fun start(fileContainer: FileContainer)
+    abstract fun start(fileContainer: IFileContainer)
 
 
     fun stop() {
     fun stop() {
         isStopReq = true
         isStopReq = true

+ 0 - 98
Android/PlayerProj/animplayer/src/main/java/com/tencent/qgame/animplayer/FileContainer.kt

@@ -1,98 +0,0 @@
-/*
- * Tencent is pleased to support the open source community by making vap available.
- *
- * Copyright (C) 2020 THL A29 Limited, a Tencent company.  All rights reserved.
- *
- * Licensed under the MIT License (the "License"); you may not use this file except in
- * compliance with the License. You may obtain a copy of the License at
- *
- * http://opensource.org/licenses/MIT
- *
- * Unless required by applicable law or agreed to in writing, software distributed under the License is
- * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
- * either express or implied. See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package com.tencent.qgame.animplayer
-
-import android.content.res.AssetFileDescriptor
-import android.content.res.AssetManager
-import android.media.MediaExtractor
-import java.io.File
-import java.io.FileNotFoundException
-import java.io.RandomAccessFile
-
-open class FileContainer {
-
-    private var isAssets = false
-
-    private var file: File? = null
-    private var randomAccessFile: RandomAccessFile? = null
-    private var assetFd: AssetFileDescriptor? = null
-    private var assetsInputStream: AssetManager.AssetInputStream? = null
-
-    constructor(file: File) {
-        isAssets = false
-        this.file = file
-        if (!(file.exists() && file.isFile && file.canRead())) throw FileNotFoundException("Unable to read $file")
-    }
-
-    constructor(assetManager: AssetManager, assetsPath: String) {
-        isAssets = true
-        assetFd = assetManager.openFd(assetsPath)
-        assetsInputStream = assetManager.open(assetsPath, AssetManager.ACCESS_STREAMING) as AssetManager.AssetInputStream
-    }
-
-
-    fun setDataSource(extractor: MediaExtractor) {
-        if (isAssets) {
-            val assetFd = this.assetFd ?: return
-            if (assetFd.declaredLength < 0) {
-                extractor.setDataSource(assetFd.fileDescriptor)
-            } else {
-                extractor.setDataSource(assetFd.fileDescriptor, assetFd.startOffset, assetFd.declaredLength)
-            }
-        } else {
-            val file = this.file ?: return
-            extractor.setDataSource(file.toString())
-        }
-    }
-
-    fun startRandomRead() {
-        if (isAssets) return
-        val file = this.file ?: return
-        randomAccessFile = RandomAccessFile(file, "r")
-    }
-
-    fun read(b: ByteArray, off: Int, len: Int):Int {
-        return if (isAssets) {
-            assetsInputStream?.read(b, off, len) ?: -1
-        } else {
-            randomAccessFile?.read(b, off, len) ?: -1
-        }
-    }
-
-    fun skip(pos: Long) {
-        if (isAssets) {
-            assetsInputStream?.skip(pos)
-        } else {
-            randomAccessFile?.skipBytes(pos.toInt())
-        }
-    }
-
-    fun closeRandomRead() {
-        if (isAssets) {
-            assetsInputStream?.close()
-        } else {
-            randomAccessFile?.close()
-        }
-    }
-
-    fun close() {
-        if (isAssets) {
-            assetFd?.close()
-            assetsInputStream?.close()
-        }
-    }
-
-}

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

@@ -21,6 +21,7 @@ import android.media.MediaExtractor
 import android.media.MediaFormat
 import android.media.MediaFormat
 import android.os.Build
 import android.os.Build
 import android.view.Surface
 import android.view.Surface
+import com.tencent.qgame.animplayer.file.IFileContainer
 import com.tencent.qgame.animplayer.util.ALog
 import com.tencent.qgame.animplayer.util.ALog
 import com.tencent.qgame.animplayer.util.MediaUtil
 import com.tencent.qgame.animplayer.util.MediaUtil
 
 
@@ -35,7 +36,7 @@ class HardDecoder(player: AnimPlayer) : Decoder(player), SurfaceTexture.OnFrameA
     private val bufferInfo by lazy { MediaCodec.BufferInfo() }
     private val bufferInfo by lazy { MediaCodec.BufferInfo() }
     private var needDestroy = false
     private var needDestroy = false
 
 
-    override fun start(fileContainer: FileContainer) {
+    override fun start(fileContainer: IFileContainer) {
         isStopReq = false
         isStopReq = false
         needDestroy = false
         needDestroy = false
         isRunning = true
         isRunning = true
@@ -62,7 +63,7 @@ class HardDecoder(player: AnimPlayer) : Decoder(player), SurfaceTexture.OnFrameA
         }
         }
     }
     }
 
 
-    private fun startPlay(fileContainer: FileContainer) {
+    private fun startPlay(fileContainer: IFileContainer) {
         try {
         try {
             if (!prepareRender()) {
             if (!prepareRender()) {
                 throw RuntimeException("render create fail")
                 throw RuntimeException("render create fail")

+ 2 - 1
Android/PlayerProj/animplayer/src/main/java/com/tencent/qgame/animplayer/IAnimView.kt

@@ -17,6 +17,7 @@ package com.tencent.qgame.animplayer
 
 
 import android.content.res.AssetManager
 import android.content.res.AssetManager
 import android.graphics.SurfaceTexture
 import android.graphics.SurfaceTexture
+import com.tencent.qgame.animplayer.file.IFileContainer
 import com.tencent.qgame.animplayer.inter.IAnimListener
 import com.tencent.qgame.animplayer.inter.IAnimListener
 import com.tencent.qgame.animplayer.inter.IFetchResource
 import com.tencent.qgame.animplayer.inter.IFetchResource
 import com.tencent.qgame.animplayer.inter.OnResourceClickListener
 import com.tencent.qgame.animplayer.inter.OnResourceClickListener
@@ -53,7 +54,7 @@ interface IAnimView {
 
 
     fun startPlay(assetManager: AssetManager, assetsPath: String)
     fun startPlay(assetManager: AssetManager, assetsPath: String)
 
 
-    fun startPlay(fileContainer: FileContainer)
+    fun startPlay(fileContainer: IFileContainer)
 
 
     fun stopPlay()
     fun stopPlay()
 
 

+ 65 - 0
Android/PlayerProj/animplayer/src/main/java/com/tencent/qgame/animplayer/file/AssetsFileContainer.kt

@@ -0,0 +1,65 @@
+/*
+ * Tencent is pleased to support the open source community by making vap available.
+ *
+ * Copyright (C) 2020 THL A29 Limited, a Tencent company.  All rights reserved.
+ *
+ * Licensed under the MIT License (the "License"); you may not use this file except in
+ * compliance with the License. You may obtain a copy of the License at
+ *
+ * http://opensource.org/licenses/MIT
+ *
+ * Unless required by applicable law or agreed to in writing, software distributed under the License is
+ * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
+ * either express or implied. See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.tencent.qgame.animplayer.file
+
+import android.content.res.AssetFileDescriptor
+import android.content.res.AssetManager
+import android.media.MediaExtractor
+import com.tencent.qgame.animplayer.Constant
+import com.tencent.qgame.animplayer.util.ALog
+
+class AssetsFileContainer(assetManager: AssetManager, assetsPath: String): IFileContainer {
+
+    companion object {
+        private const val TAG = "${Constant.TAG}.FileContainer"
+    }
+
+    private val assetFd: AssetFileDescriptor = assetManager.openFd(assetsPath)
+    private val assetsInputStream: AssetManager.AssetInputStream =
+        assetManager.open(assetsPath, AssetManager.ACCESS_STREAMING) as AssetManager.AssetInputStream
+
+    init {
+        ALog.i(TAG, "AssetsFileContainer init")
+    }
+
+    override fun setDataSource(extractor: MediaExtractor) {
+        if (assetFd.declaredLength < 0) {
+            extractor.setDataSource(assetFd.fileDescriptor)
+        } else {
+            extractor.setDataSource(assetFd.fileDescriptor, assetFd.startOffset, assetFd.declaredLength)
+        }
+    }
+
+    override fun startRandomRead() {
+    }
+
+    override fun read(b: ByteArray, off: Int, len: Int): Int {
+        return assetsInputStream.read(b, off, len)
+    }
+
+    override fun skip(pos: Long) {
+        assetsInputStream.skip(pos)
+    }
+
+    override fun closeRandomRead() {
+        assetsInputStream.close()
+    }
+
+    override fun close() {
+        assetFd.close()
+        assetsInputStream.close()
+    }
+}

+ 61 - 0
Android/PlayerProj/animplayer/src/main/java/com/tencent/qgame/animplayer/file/FileContainer.kt

@@ -0,0 +1,61 @@
+/*
+ * Tencent is pleased to support the open source community by making vap available.
+ *
+ * Copyright (C) 2020 THL A29 Limited, a Tencent company.  All rights reserved.
+ *
+ * Licensed under the MIT License (the "License"); you may not use this file except in
+ * compliance with the License. You may obtain a copy of the License at
+ *
+ * http://opensource.org/licenses/MIT
+ *
+ * Unless required by applicable law or agreed to in writing, software distributed under the License is
+ * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
+ * either express or implied. See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.tencent.qgame.animplayer.file
+
+import android.media.MediaExtractor
+import com.tencent.qgame.animplayer.Constant
+import com.tencent.qgame.animplayer.util.ALog
+import java.io.File
+import java.io.FileNotFoundException
+import java.io.RandomAccessFile
+
+class FileContainer(private val file: File) : IFileContainer {
+
+    companion object {
+        private const val TAG = "${Constant.TAG}.FileContainer"
+    }
+
+    private var randomAccessFile: RandomAccessFile? = null
+
+    init {
+        ALog.i(TAG, "FileContainer init")
+        if (!(file.exists() && file.isFile && file.canRead())) throw FileNotFoundException("Unable to read $file")
+    }
+
+    override fun setDataSource(extractor: MediaExtractor) {
+        extractor.setDataSource(file.toString())
+
+    }
+
+    override fun startRandomRead() {
+        randomAccessFile = RandomAccessFile(file, "r")
+    }
+
+    override fun read(b: ByteArray, off: Int, len: Int): Int {
+        return randomAccessFile?.read(b, off, len) ?: -1
+    }
+
+    override fun skip(pos: Long) {
+        randomAccessFile?.skipBytes(pos.toInt())
+    }
+
+    override fun closeRandomRead() {
+        randomAccessFile?.close()
+    }
+
+    override fun close() {
+    }
+}

+ 34 - 0
Android/PlayerProj/animplayer/src/main/java/com/tencent/qgame/animplayer/file/IFileContainer.kt

@@ -0,0 +1,34 @@
+/*
+ * Tencent is pleased to support the open source community by making vap available.
+ *
+ * Copyright (C) 2020 THL A29 Limited, a Tencent company.  All rights reserved.
+ *
+ * Licensed under the MIT License (the "License"); you may not use this file except in
+ * compliance with the License. You may obtain a copy of the License at
+ *
+ * http://opensource.org/licenses/MIT
+ *
+ * Unless required by applicable law or agreed to in writing, software distributed under the License is
+ * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
+ * either express or implied. See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.tencent.qgame.animplayer.file
+
+import android.media.MediaExtractor
+
+interface IFileContainer {
+
+    fun setDataSource(extractor: MediaExtractor)
+
+    fun startRandomRead()
+
+    fun read(b: ByteArray, off: Int, len: Int): Int
+
+    fun skip(pos: Long)
+
+    fun closeRandomRead()
+
+    fun close()
+
+}

+ 2 - 2
Android/PlayerProj/animplayer/src/main/java/com/tencent/qgame/animplayer/util/MediaUtil.kt

@@ -19,7 +19,7 @@ import android.media.MediaCodecList
 import android.media.MediaExtractor
 import android.media.MediaExtractor
 import android.media.MediaFormat
 import android.media.MediaFormat
 import com.tencent.qgame.animplayer.Constant
 import com.tencent.qgame.animplayer.Constant
-import com.tencent.qgame.animplayer.FileContainer
+import com.tencent.qgame.animplayer.file.IFileContainer
 import kotlin.collections.HashMap
 import kotlin.collections.HashMap
 
 
 
 
@@ -35,7 +35,7 @@ object MediaUtil {
     }
     }
 
 
 
 
-    fun getExtractor(file: FileContainer): MediaExtractor {
+    fun getExtractor(file: IFileContainer): MediaExtractor {
         val extractor = MediaExtractor()
         val extractor = MediaExtractor()
         file.setDataSource(extractor)
         file.setDataSource(extractor)
         return extractor
         return extractor