Feature: Honor remote desktop tuning commands on Android hosts
The Android client dropped CMD_QUALITY_LEVEL, CMD_FPS, CMD_QUALITY_PROFILES and CMD_SCREEN_TUNING as log lines, and hardcoded ScreenSettings.QualityLevel to Good. Because the server initializes adaptive quality from the client's reported level, that hardcoded value pinned every Android session to a fixed profile: adaptive quality could never start. ScreenTuning.kt now owns the state in SharedPreferences (level, max fps and the master's six-row profile table), and C++ only parses the wire format. The level is persisted when the master sets it explicitly, including -1 adaptive and -2 disabled, and is reported back in TOKEN_BITMAPINFO on the next screen session. A concrete level switch applies the profile bitrate at runtime via MediaCodec.setParameters(PARAMETER_KEY_VIDEO_BITRATE), which is smooth and needs no encoder rebuild. Frame rate is stored as-is (255 = unlimited, matching Windows) and only reaches KEY_FRAME_RATE at the next capture start, since surface encoders have no runtime fps cap. Ultra and High default to a zero bitrate because their Windows algorithms are DIFF/RGB565, so Android falls back to the previous 4000 kbps. CMD_SCREEN_TUNING stays log-only: all four flags belong to the Windows capture/x264 path and Android does not claim SCREEN_CAP_TUNING_FLAGS. CMD_SCREEN_SIZE is ignored; the server's pending resolution-change mark self-heals after one second. Verified on the emulator: a seeded Level 5 starts capture at 800 kbps/8 fps and the server title shows Minimal; a master-side switch to Good changes the live bitrate to 3000, and a restart picks up fps 30 from CMD_FPS. Co-Authored-By: deepseek-flash
This commit is contained in:
1 parent
a801a2999a
commit
3ab5154db1
6 files changed
+444
-12
No files matched your search
@@ -121,6 +121,27 @@ class CaptureService : Service() {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 服务端 CMD_QUALITY_LEVEL 的落点:按档位配置表换码率。setParameters 是运行时
|
||||
* 平滑生效,不需要重建编码器(重建会丢一个 IDR,画面会闪一下)。
|
||||
* 调用方是屏幕子连接接收线程,编码器只能在 idrHandler 的线程上碰。
|
||||
*/
|
||||
@JvmStatic
|
||||
fun applyBitRate(kbps: Int) {
|
||||
val svc = instance ?: return
|
||||
svc.idrHandler.post {
|
||||
if (kbps <= 0 || kbps == svc.currentBitRateKbps) return@post
|
||||
val codec = svc.mediaCodec ?: return@post // 还没开抓:开抓时会读到新档位
|
||||
runCatching {
|
||||
val p = Bundle()
|
||||
p.putInt(MediaCodec.PARAMETER_KEY_VIDEO_BITRATE, kbps * 1000)
|
||||
codec.setParameters(p)
|
||||
svc.currentBitRateKbps = kbps
|
||||
Log.i(TAG, "bitrate -> $kbps kbps")
|
||||
}.onFailure { Log.e(TAG, "bitrate change to $kbps failed: $it") }
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 静止屏幕黑屏修复:SurfaceFlinger 在无脏区时不向 VirtualDisplay 推帧,
|
||||
* 导致编码器无输入,REQUEST_SYNC_FRAME 悬空排队,浏览器等 10-60 秒才见画面。
|
||||
@@ -150,6 +171,8 @@ class CaptureService : Service() {
|
||||
private var mediaProjection: MediaProjection? = null
|
||||
private var virtualDisplay: VirtualDisplay? = null
|
||||
private var mediaCodec: MediaCodec? = null
|
||||
// 当前编码码率(kbps):CMD_QUALITY_LEVEL 切换档位时只在真的变化时才 setParameters
|
||||
private var currentBitRateKbps = 0
|
||||
|
||||
private val idrHandler = Handler(Looper.getMainLooper())
|
||||
// CS-04 fix: runCatching 包住 setParameters,防止 codec 已停止时抛异常
|
||||
@@ -269,10 +292,14 @@ class CaptureService : Service() {
|
||||
val (encW, encH) = scaleDown(physW, physH, MAX_LONG_SIDE)
|
||||
val dpi = resources.displayMetrics.densityDpi
|
||||
|
||||
Log.i(TAG, "Capture: phys=${physW}x${physH} enc=${encW}x${encH} dpi=$dpi")
|
||||
// 主控端上次保存的档位决定开抓码率;自适应档从 Good 起步(与服务端假设一致)
|
||||
val (bitRateKbps, fps) = ScreenTuning.startupRate()
|
||||
Log.i(TAG, "Capture: phys=${physW}x${physH} enc=${encW}x${encH} dpi=$dpi " +
|
||||
"bitrate=${bitRateKbps}kbps fps=$fps level=${ScreenTuning.getQualityLevel()}")
|
||||
|
||||
val codec = MediaCodec.createEncoderByType(MediaFormat.MIMETYPE_VIDEO_AVC)
|
||||
val surface = configureEncoder(codec, encW, encH)
|
||||
val surface = configureEncoder(codec, encW, encH, bitRateKbps, fps)
|
||||
currentBitRateKbps = bitRateKbps
|
||||
|
||||
var configData: ByteArray? = null
|
||||
|
||||
@@ -351,15 +378,16 @@ class CaptureService : Service() {
|
||||
Log.i(TAG, "MediaCodec + VirtualDisplay started ${encW}x${encH}")
|
||||
}
|
||||
|
||||
private fun buildFmt(w: Int, h: Int, lowLatency: Boolean, baseline: Boolean) =
|
||||
private fun buildFmt(w: Int, h: Int, lowLatency: Boolean, baseline: Boolean,
|
||||
bitRateKbps: Int, fps: Int) =
|
||||
MediaFormat.createVideoFormat(MediaFormat.MIMETYPE_VIDEO_AVC, w, h).apply {
|
||||
setInteger(MediaFormat.KEY_COLOR_FORMAT,
|
||||
MediaCodecInfo.CodecCapabilities.COLOR_FormatSurface)
|
||||
setInteger(MediaFormat.KEY_BIT_RATE, 4_000_000)
|
||||
setInteger(MediaFormat.KEY_FRAME_RATE, 30)
|
||||
setInteger(MediaFormat.KEY_BIT_RATE, bitRateKbps * 1000)
|
||||
setInteger(MediaFormat.KEY_FRAME_RATE, fps)
|
||||
setInteger(MediaFormat.KEY_I_FRAME_INTERVAL, 2)
|
||||
setInteger(MediaFormat.KEY_PRIORITY, 0)
|
||||
setInteger(MediaFormat.KEY_OPERATING_RATE, 30)
|
||||
setInteger(MediaFormat.KEY_OPERATING_RATE, fps)
|
||||
if (lowLatency) {
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
|
||||
setInteger(MediaFormat.KEY_LATENCY, 0)
|
||||
@@ -372,7 +400,8 @@ class CaptureService : Service() {
|
||||
}
|
||||
|
||||
// 逐级降级:低延迟+Baseline → 低延迟 → 默认,确保真机兼容
|
||||
private fun configureEncoder(codec: MediaCodec, w: Int, h: Int): android.view.Surface {
|
||||
private fun configureEncoder(codec: MediaCodec, w: Int, h: Int,
|
||||
bitRateKbps: Int, fps: Int): android.view.Surface {
|
||||
val configs = listOf(
|
||||
true to true,
|
||||
true to false,
|
||||
@@ -380,7 +409,7 @@ class CaptureService : Service() {
|
||||
)
|
||||
for ((ll, bl) in configs) {
|
||||
try {
|
||||
val fmt = buildFmt(w, h, ll, bl)
|
||||
val fmt = buildFmt(w, h, ll, bl, bitRateKbps, fps)
|
||||
codec.configure(fmt, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE)
|
||||
Log.i(TAG, "Encoder configured: lowLatency=$ll baseline=$bl")
|
||||
return codec.createInputSurface()
|
||||
|
||||
Reference in new issue
Block a user