perf: 降低串流延迟(软编降档、编码器实时参数、NV12 转换与绘制优化)
- VideoEncoder 按实时流配置编码器:实时优先、不攒帧(KEY_LATENCY)、 禁 B 帧、CBR;个别 ROM 拒绝这些参数时自动退回默认配置 - 新增 VideoEncoder.isSoftwareEncoder:全部候选编码器都是 c2.android./OMX.google. 时判定为软编,ReceiverSession 把长边上限从 1920 降到 1280 (模拟器实测 864x1920 → 576x1280,滑动时帧率明显回升) - ConvertNv12 改为每 2 个像素复用一次 U/V 计算,裁剪改成无分支写法 - 串流窗口画面铺满客户区时跳过整屏刷黑,省一次全屏填充 - README 3.4 补充编码器实时配置与软编降档说明
This commit is contained in:
1 parent
04f2b800b2
commit
cbbc2a642d
5 files changed
+135
-30
No files matched your search
@@ -50,6 +50,9 @@ class ReceiverSession(
|
||||
|
||||
/** 编码长边上限,兼顾清晰度与局域网带宽。 */
|
||||
private const val MAX_LONG_SIDE = 1920
|
||||
|
||||
/** 软件编码器(模拟器 / 无硬编设备)的上限:1080p30 根本编不动,会一直掉帧。 */
|
||||
private const val MAX_LONG_SIDE_SOFTWARE = 1280
|
||||
}
|
||||
|
||||
private val executor = Executors.newFixedThreadPool(3)
|
||||
@@ -294,6 +297,16 @@ class ReceiverSession(
|
||||
}
|
||||
}
|
||||
|
||||
/** 长边上限:软编(模拟器/无硬编)降一档,否则编码器跟不上、画面一直卡。 */
|
||||
private val maxLongSide: Int by lazy {
|
||||
if (VideoEncoder.isSoftwareEncoder(preferH265)) {
|
||||
Log.i(TAG, "只有软件编码器:长边限制 $MAX_LONG_SIDE_SOFTWARE(否则编不动会掉帧)")
|
||||
MAX_LONG_SIDE_SOFTWARE
|
||||
} else {
|
||||
MAX_LONG_SIDE
|
||||
}
|
||||
}
|
||||
|
||||
fun stop() {
|
||||
running = false
|
||||
displayManager?.unregisterDisplayListener(displayListener)
|
||||
@@ -310,7 +323,7 @@ class ReceiverSession(
|
||||
executor.shutdownNow()
|
||||
}
|
||||
|
||||
/** 本机真实屏幕尺寸(跟随旋转),长边限制在 [MAX_LONG_SIDE] 内,宽高取偶数。 */
|
||||
/** 本机真实屏幕尺寸(跟随旋转),长边限制在 [maxLongSide] 内,宽高取偶数。 */
|
||||
private fun currentScreenSize(): IntArray {
|
||||
val metrics = DisplayMetrics()
|
||||
val display = displayManager?.getDisplay(Display.DEFAULT_DISPLAY)
|
||||
@@ -352,8 +365,9 @@ class ReceiverSession(
|
||||
var w = width.coerceAtLeast(16)
|
||||
var h = height.coerceAtLeast(16)
|
||||
val longSide = maxOf(w, h)
|
||||
if (longSide > MAX_LONG_SIDE) {
|
||||
val scale = MAX_LONG_SIDE.toDouble() / longSide
|
||||
val limit = maxLongSide
|
||||
if (longSide > limit) {
|
||||
val scale = limit.toDouble() / longSide
|
||||
w = (w * scale).toInt()
|
||||
h = (h * scale).toInt()
|
||||
}
|
||||
|
||||
+84
-7
@@ -2,12 +2,45 @@ package com.ttstd.lancast.receiver.encode
|
||||
|
||||
import android.media.MediaCodec
|
||||
import android.media.MediaCodecInfo
|
||||
import android.media.MediaCodecList
|
||||
import android.media.MediaFormat
|
||||
import android.os.Build
|
||||
import android.os.Bundle
|
||||
import android.util.Log
|
||||
import android.view.Surface
|
||||
import java.nio.ByteBuffer
|
||||
|
||||
class VideoEncoder(private val onPacket: (ByteBuffer, MediaCodec.BufferInfo) -> Unit) {
|
||||
companion object {
|
||||
private const val TAG = "VideoEncoder"
|
||||
|
||||
/**
|
||||
* 当前 MIME 会不会落到软件编码器上(模拟器、无硬编的廉价机)。
|
||||
*
|
||||
* 软件编码 1080p30 跑不动,会不断丢帧,表现出来就是串流很卡,
|
||||
* 因此调用方要据此把分辨率降一档。
|
||||
*/
|
||||
fun isSoftwareEncoder(preferH265: Boolean): Boolean {
|
||||
val mime =
|
||||
if (preferH265) MediaFormat.MIMETYPE_VIDEO_HEVC else MediaFormat.MIMETYPE_VIDEO_AVC
|
||||
return runCatching {
|
||||
val candidates = MediaCodecList(MediaCodecList.REGULAR_CODECS).codecInfos
|
||||
.filter { info ->
|
||||
info.isEncoder &&
|
||||
info.supportedTypes.any { it.equals(mime, ignoreCase = true) }
|
||||
}
|
||||
// 只要有一个硬件实现就不算软编(真机上硬编通常排在前面)。
|
||||
candidates.isNotEmpty() && candidates.none { !isSoftwareCodecName(it.name) }
|
||||
}.getOrDefault(false)
|
||||
}
|
||||
|
||||
/** Google/Android 自带的软件编解码器名字前缀。 */
|
||||
private fun isSoftwareCodecName(name: String): Boolean =
|
||||
name.startsWith("c2.android.") ||
|
||||
name.startsWith("c2.google.") ||
|
||||
name.startsWith("OMX.google.")
|
||||
}
|
||||
|
||||
private var codec: MediaCodec? = null
|
||||
var inputSurface: Surface? = null
|
||||
private set
|
||||
@@ -21,19 +54,63 @@ class VideoEncoder(private val onPacket: (ByteBuffer, MediaCodec.BufferInfo) ->
|
||||
|
||||
fun start(width: Int, height: Int, fps: Int, bitrateBps: Int, preferH265: Boolean) {
|
||||
val mime = if (preferH265) MediaFormat.MIMETYPE_VIDEO_HEVC else MediaFormat.MIMETYPE_VIDEO_AVC
|
||||
val format = MediaFormat.createVideoFormat(mime, width, height).apply {
|
||||
setInteger(MediaFormat.KEY_COLOR_FORMAT, MediaCodecInfo.CodecCapabilities.COLOR_FormatSurface)
|
||||
setInteger(MediaFormat.KEY_BIT_RATE, bitrateBps)
|
||||
setInteger(MediaFormat.KEY_FRAME_RATE, fps)
|
||||
setInteger(MediaFormat.KEY_I_FRAME_INTERVAL, 2)
|
||||
var encoder = MediaCodec.createEncoderByType(mime)
|
||||
try {
|
||||
encoder.configure(
|
||||
buildFormat(mime, width, height, fps, bitrateBps, lowLatency = true),
|
||||
null,
|
||||
null,
|
||||
MediaCodec.CONFIGURE_FLAG_ENCODE,
|
||||
)
|
||||
} catch (error: Exception) {
|
||||
// 个别 ROM 的编码器不认识低延迟参数会直接抛,退回最保守的配置保证能开流。
|
||||
Log.w(TAG, "低延迟编码参数不被支持,退回默认配置", error)
|
||||
runCatching { encoder.release() }
|
||||
encoder = MediaCodec.createEncoderByType(mime)
|
||||
encoder.configure(
|
||||
buildFormat(mime, width, height, fps, bitrateBps, lowLatency = false),
|
||||
null,
|
||||
null,
|
||||
MediaCodec.CONFIGURE_FLAG_ENCODE,
|
||||
)
|
||||
}
|
||||
val encoder = MediaCodec.createEncoderByType(mime)
|
||||
encoder.configure(format, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE)
|
||||
inputSurface = encoder.createInputSurface()
|
||||
encoder.start()
|
||||
codec = encoder
|
||||
}
|
||||
|
||||
/**
|
||||
* [lowLatency] 为 true 时要求编码器按实时流工作:
|
||||
* 实时优先、不额外缓冲帧、禁止 B 帧、恒定码率。
|
||||
* 默认配置下编码器会攒几帧再吐,交互延迟能到上百毫秒。
|
||||
*/
|
||||
private fun buildFormat(
|
||||
mime: String,
|
||||
width: Int,
|
||||
height: Int,
|
||||
fps: Int,
|
||||
bitrateBps: Int,
|
||||
lowLatency: Boolean,
|
||||
): MediaFormat = MediaFormat.createVideoFormat(mime, width, height).apply {
|
||||
setInteger(MediaFormat.KEY_COLOR_FORMAT, MediaCodecInfo.CodecCapabilities.COLOR_FormatSurface)
|
||||
setInteger(MediaFormat.KEY_BIT_RATE, bitrateBps)
|
||||
setInteger(MediaFormat.KEY_FRAME_RATE, fps)
|
||||
setInteger(MediaFormat.KEY_I_FRAME_INTERVAL, 2)
|
||||
if (!lowLatency) return@apply
|
||||
setInteger(MediaFormat.KEY_PRIORITY, 0)
|
||||
setInteger(MediaFormat.KEY_OPERATING_RATE, fps)
|
||||
setInteger(
|
||||
MediaFormat.KEY_BITRATE_MODE,
|
||||
MediaCodecInfo.EncoderCapabilities.BITRATE_MODE_CBR,
|
||||
)
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
|
||||
setInteger(MediaFormat.KEY_MAX_B_FRAMES, 0)
|
||||
}
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
|
||||
setInteger(MediaFormat.KEY_LATENCY, 1)
|
||||
}
|
||||
}
|
||||
|
||||
fun drain() {
|
||||
val encoder = codec ?: return
|
||||
val info = MediaCodec.BufferInfo()
|
||||
|
||||
Reference in new issue
Block a user