Skip to content

Commit

Permalink
Merge pull request #65 from Tencent/feature/android_filecontainer
Browse files Browse the repository at this point in the history
feat: 抽象IFileContainer接口
  • Loading branch information
hexleo authored Mar 22, 2021
2 parents 8a79f85 + 978775f commit 39cda4b
Show file tree
Hide file tree
Showing 12 changed files with 184 additions and 114 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package com.tencent.qgame.animplayer

import android.os.SystemClock
import com.tencent.qgame.animplayer.file.IFileContainer
import com.tencent.qgame.animplayer.util.ALog
import org.json.JSONObject
import java.nio.charset.Charset
Expand All @@ -36,7 +37,7 @@ class AnimConfigManager(val player: AnimPlayer) {
* 解析配置
* @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 {
isParsingConfig = true
// 解析vapc
Expand Down Expand Up @@ -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()
this.config = config
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package com.tencent.qgame.animplayer

import com.tencent.qgame.animplayer.file.IFileContainer
import com.tencent.qgame.animplayer.inter.IAnimListener
import com.tencent.qgame.animplayer.mask.MaskConfig
import com.tencent.qgame.animplayer.plugin.AnimPluginManager
Expand Down Expand Up @@ -71,7 +72,7 @@ class AnimPlayer(val animView: IAnimView) {
decoder?.onSurfaceSizeChanged(width, height)
}

fun startPlay(fileContainer: FileContainer) {
fun startPlay(fileContainer: IFileContainer) {
isStartRunning = true
prepareDecoder()
if (decoder?.prepareThread() == false) {
Expand All @@ -98,7 +99,7 @@ class AnimPlayer(val animView: IAnimView) {
}
}

private fun innerStartPlay(fileContainer: FileContainer) {
private fun innerStartPlay(fileContainer: IFileContainer) {
synchronized(AnimPlayer::class.java) {
if (isSurfaceAvailable) {
isStartRunning = false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@ import android.os.Build
import android.os.Handler
import android.os.Looper
import android.util.AttributeSet
import android.view.MotionEvent
import android.view.TextureView
import android.view.View
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.IFetchResource
import com.tencent.qgame.animplayer.inter.OnResourceClickListener
Expand All @@ -51,7 +53,7 @@ open class AnimView @JvmOverloads constructor(context: Context, attrs: Attribute
private var surface: SurfaceTexture? = null
private var animListener: IAnimListener? = null
private var innerTextureView: InnerTextureView? = null
private var lastFile: FileContainer? = null
private var lastFile: IFileContainer? = null
private val scaleTypeUtil = ScaleTypeUtil()

// 代理监听
Expand Down Expand Up @@ -235,15 +237,15 @@ open class AnimView @JvmOverloads constructor(context: Context, attrs: Attribute

override fun startPlay(assetManager: AssetManager, assetsPath: String) {
try {
val fileContainer = FileContainer(assetManager, assetsPath)
val fileContainer = AssetsFileContainer(assetManager, assetsPath)
startPlay(fileContainer)
} catch (e: Throwable) {
animProxyListener.onFailed(Constant.REPORT_ERROR_TYPE_FILE_ERROR, Constant.ERROR_MSG_FILE_ERROR)
}
}


override fun startPlay(fileContainer: FileContainer) {
override fun startPlay(fileContainer: IFileContainer) {
ui {
if (visibility != View.VISIBLE) {
ALog.e(TAG, "AnimView is GONE, can't play")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package com.tencent.qgame.animplayer

import android.media.*
import com.tencent.qgame.animplayer.file.IFileContainer
import com.tencent.qgame.animplayer.util.ALog
import com.tencent.qgame.animplayer.util.MediaUtil
import java.lang.RuntimeException
Expand All @@ -41,7 +42,7 @@ class AudioPlayer(val player: AnimPlayer) {
return Decoder.createThread(decodeThread, "anim_audio_thread")
}

fun start(fileContainer: FileContainer) {
fun start(fileContainer: IFileContainer) {
isStopReq = false
needDestroy = false
if (!prepareThread()) return
Expand All @@ -63,7 +64,7 @@ class AudioPlayer(val player: AnimPlayer) {
isStopReq = true
}

private fun startPlay(fileContainer: FileContainer) {
private fun startPlay(fileContainer: IFileContainer) {
val extractor = MediaUtil.getExtractor(fileContainer)
this.extractor = extractor
val audioIndex = MediaUtil.selectAudioTrack(extractor)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package com.tencent.qgame.animplayer
import android.os.Build
import android.os.HandlerThread
import android.os.Handler
import com.tencent.qgame.animplayer.file.IFileContainer
import com.tencent.qgame.animplayer.inter.IAnimListener
import com.tencent.qgame.animplayer.util.ALog
import com.tencent.qgame.animplayer.util.SpeedControlUtil
Expand Down Expand Up @@ -70,7 +71,7 @@ abstract class Decoder(val player: AnimPlayer) : IAnimListener {
var isStopReq = false // 是否需要停止
val speedControlUtil by lazy { SpeedControlUtil() }

abstract fun start(fileContainer: FileContainer)
abstract fun start(fileContainer: IFileContainer)

fun stop() {
isStopReq = true
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import android.media.MediaExtractor
import android.media.MediaFormat
import android.os.Build
import android.view.Surface
import com.tencent.qgame.animplayer.file.IFileContainer
import com.tencent.qgame.animplayer.util.ALog
import com.tencent.qgame.animplayer.util.MediaUtil

Expand All @@ -35,7 +36,7 @@ class HardDecoder(player: AnimPlayer) : Decoder(player), SurfaceTexture.OnFrameA
private val bufferInfo by lazy { MediaCodec.BufferInfo() }
private var needDestroy = false

override fun start(fileContainer: FileContainer) {
override fun start(fileContainer: IFileContainer) {
isStopReq = false
needDestroy = false
isRunning = true
Expand All @@ -62,7 +63,7 @@ class HardDecoder(player: AnimPlayer) : Decoder(player), SurfaceTexture.OnFrameA
}
}

private fun startPlay(fileContainer: FileContainer) {
private fun startPlay(fileContainer: IFileContainer) {
try {
if (!prepareRender()) {
throw RuntimeException("render create fail")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package com.tencent.qgame.animplayer

import android.content.res.AssetManager
import android.graphics.SurfaceTexture
import com.tencent.qgame.animplayer.file.IFileContainer
import com.tencent.qgame.animplayer.inter.IAnimListener
import com.tencent.qgame.animplayer.inter.IFetchResource
import com.tencent.qgame.animplayer.inter.OnResourceClickListener
Expand Down Expand Up @@ -53,7 +54,7 @@ interface IAnimView {

fun startPlay(assetManager: AssetManager, assetsPath: String)

fun startPlay(fileContainer: FileContainer)
fun startPlay(fileContainer: IFileContainer)

fun stopPlay()

Expand Down
Original file line number Diff line number Diff line change
@@ -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()
}
}
Loading

0 comments on commit 39cda4b

Please sign in to comment.