fix:调整缓冲区大小和配置项

This commit is contained in:
liyi 2025-06-20 16:35:25 +08:00
parent c8a4e5d28e
commit c97f719ccb

View File

@ -49,7 +49,7 @@ class VideoDecoder(
companion object {
private const val TAG = "VideoDecoder"
private const val TIMEOUT_US = 10000L
private const val INPUT_BUFFER_QUEUE_CAPACITY = 100 // 增大输入缓冲区容量
private const val BUFFER_QUEUE_CAPACITY = 100 // 增大输入缓冲区容量
}
// region 成员变量定义
@ -60,12 +60,12 @@ class VideoDecoder(
private var mediaCodec: MediaCodec? = null
// 输入帧队列,支持并发,容量较大以防止丢帧
private val inputFrameQueue = LinkedBlockingQueue<FrameData>(INPUT_BUFFER_QUEUE_CAPACITY)
private val inputFrameQueue = LinkedBlockingQueue<FrameData>(BUFFER_QUEUE_CAPACITY)
private var running = true // 解码器运行状态
private val frameSeqSet = Collections.newSetFromMap(ConcurrentHashMap<Int, Boolean>()) // 防止重复帧入队
// 解码输出缓冲区,增大容量
private val outputFrameQueue = LinkedBlockingQueue<DecodedFrame>(100)
private val outputFrameQueue = LinkedBlockingQueue<DecodedFrame>(BUFFER_QUEUE_CAPACITY)
// 渲染线程控制
private var scheduler = Executors.newSingleThreadScheduledExecutor()
@ -80,7 +80,7 @@ class VideoDecoder(
// 渲染帧率fps可由外部控制默认30
@Volatile
var renderFps: Int = 20
var renderFps: Int = 25
// 兜底记录最近一次I帧的frameSeqP/B帧依赖校验
@Volatile
@ -99,7 +99,7 @@ class VideoDecoder(
// 1. 新增成员变量
@Volatile
private var latestRenderedTimestampMs: Long? = null
private val MAX_ALLOWED_DELAY_MS = 650 // 最大允许延迟,单位毫秒
private val MAX_ALLOWED_DELAY_MS = 750 // 最大允许延迟,单位毫秒
@Volatile
private var timestampBaseMs: Long? = null
@Volatile
@ -109,7 +109,7 @@ class VideoDecoder(
private val reorderBuffer = mutableMapOf<Int, FrameData>() // key: frameSeq
private val receivedIFrames = mutableSetOf<Int>() // 已收到的I帧frameSeq
private val reorderLock = ReentrantLock() // 线程安全
private val MAX_REORDER_BUFFER_SIZE = 50
private val MAX_REORDER_BUFFER_SIZE = BUFFER_QUEUE_CAPACITY
// 输入帧结构体
private data class FrameData(
@ -152,7 +152,7 @@ class VideoDecoder(
format.setInteger(MediaFormat.KEY_LOW_LATENCY, 1)
format.setInteger(MediaFormat.KEY_PRIORITY, 0)
format.setInteger(MediaFormat.KEY_MAX_B_FRAMES, 0)
format.setInteger(MediaFormat.KEY_FRAME_RATE, renderFps);
// 高通解码器特定配置
format.setInteger("vendor.qti-ext-dec-low-latency.enable", 1)
format.setInteger("vendor.qti-ext-dec-picture-order.enable", 0)
@ -247,9 +247,10 @@ class VideoDecoder(
mainHandler.post { onFrameRendered() }
hasNotifiedFlutter = true
}
Log.d(TAG, "[Render] 渲染: bufferIdx=${frame.bufferIndex}, pts=${frame.timestampUs}, 当前outputFrameQueue=${outputFrameQueue.size}")
// Log.d(TAG, "[Render] 渲染: bufferIdx=${frame.bufferIndex}, pts=${frame.timestampUs}, " +
// "当前outputFrameQueue=${outputFrameQueue.size}")
} else {
Log.w(TAG, "[Render] 渲染空转无帧可渲染当前outputFrameQueue=${outputFrameQueue.size}")
// Log.w(TAG, "[Render] 渲染空转无帧可渲染当前outputFrameQueue=${outputFrameQueue.size}")
}
} catch (e: Exception) {
Log.e(TAG, "[RenderTask] Exception", e)