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:
yuanyuanxianganddeepseek-flash committed 2026-11-11 21:58:54 +01:00
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()
@@ -0,0 +1,160 @@
package com.yama.client
import android.content.Context
import android.content.SharedPreferences
import android.util.Log
/**
* 远程桌面画质/帧率协商(CMD_QUALITY_LEVEL / CMD_FPS / CMD_QUALITY_PROFILES)。
*
* 之前这四个命令在 ScreenHandler 里只记一条日志:编码器永远 4Mbps/30fps,而且
* SendBitmapInfo 把 QualityLevel 写死成 Good——服务端用客户端上报的档位初始化
* 自适应状态,写死 Good 意味着「自适应质量」对 Android 主机永远不启用。
*
* 这里把主控端下发的东西存进 SharedPreferences:
* - 档位(persist=1 才落盘,与 Windows 客户端一致);
* - 质量配置表(主控端「高级 → 远程桌面质量等级」自定义的 6 档);
* - 最大帧率。
* 下次建立屏幕子连接时 C++ 侧通过 getQualityLevel()/getMaxFps() 读回并随
* ScreenSettings 上报,主控端据此恢复菜单勾选与自适应开关。
*
* 真正能落到 MediaCodec 的只有码率:档位切换时按配置表动态 setParameters
* (PARAMETER_KEY_VIDEO_BITRATE,平滑生效、不需要重建编码器)。帧率只能写进
* KEY_FRAME_RATE 的码控提示——Android 的 surface 编码器按画面内容出帧,没有
* 「限帧」的运行时开关,所以 maxFPS 要到下次开抓才带上。
*
* CMD_SCREEN_TUNING 的四个开关(GDI 去延时 / x264 VBV / 发送节流 / 自适应判据)
* 都是 Windows 抓屏与软编管线的,Android 没有对应实现,客户端不声明
* SCREEN_CAP_TUNING_FLAGS,服务端继续用主控端按设备保存的副本。
*/
object ScreenTuning {
const val TAG = "YAMA_TUNING"
// 与 common/commands.h 的 QualityLevel 常量一致
private const val QUALITY_DISABLED = -2
private const val QUALITY_ADAPTIVE = -1
private const val QUALITY_COUNT = 6
private const val QUALITY_GOOD = 2
// 关闭质量控制(-2)时使用「原有设置」:与本次改动前写死的编码参数一致
private const val LEGACY_BITRATE_KBPS = 4000
private const val LEGACY_FPS = 30
private const val PREFS = "screen_tuning"
private const val K_LEVEL = "quality_level"
private const val K_FPS = "max_fps"
private const val K_PROFILES = "profiles"
/** 默认档位表,必须与 common/commands.h GetQualityProfile 的内置默认一致(maxFPS/maxWidth/algorithm/bitRate) */
private val DEFAULT_PROFILES = intArrayOf(
25, 0, 1, 0, // Ultra
20, 0, 3, 0, // High
20, 1920, 2, 3000, // Good
15, 1600, 2, 2000, // Medium
12, 1280, 2, 1200, // Low
8, 1024, 2, 800, // Minimal
)
private fun prefs(): SharedPreferences? =
CaptureService.appContext?.getSharedPreferences(PREFS, Context.MODE_PRIVATE)
/** 建立屏幕子连接时上报给服务端(ScreenSettings.QualityLevel) */
@JvmStatic
fun getQualityLevel(): Int {
val v = prefs()?.getInt(K_LEVEL, QUALITY_GOOD) ?: QUALITY_GOOD
return if (v in 0 until QUALITY_COUNT || v == QUALITY_ADAPTIVE || v == QUALITY_DISABLED)
v else QUALITY_GOOD
}
/** 建立屏幕子连接时上报给服务端(ScreenSettings.MaxFPS) */
@JvmStatic
fun getMaxFps(): Int {
val stored = prefs()?.getInt(K_FPS, 0) ?: 0
if (stored > 0) return stored
val level = getQualityLevel()
if (level == QUALITY_DISABLED) return LEGACY_FPS // 与 startupRate 的编码参数保持一致
return profile(if (level in 0 until QUALITY_COUNT) level else QUALITY_GOOD)[0]
}
/**
* 开抓时的初始码率(kbps)与帧率。自适应从 Good 起步——服务端在
* m_Settings.QualityLevel == QUALITY_ADAPTIVE 时也是把 currentLevel 定成 Good。
*
* Android 一律编 H.264,而配置表里 Ultra/High 的默认算法是 DIFF/RGB565、bitRate=0
* (那是 Windows 的软编路径)。这两档没有码率可用,按改动前的 H.264 参数 4000kbps 起步;
* 主控端若在自定义表里给了码率则以它为准。
*/
fun startupRate(): Pair<Int, Int> {
val level = getQualityLevel()
val p = if (level in 0 until QUALITY_COUNT) profile(level) else profile(QUALITY_GOOD)
val kbps = if (level == QUALITY_DISABLED || p[3] <= 0) LEGACY_BITRATE_KBPS else p[3]
// CMD_FPS 的落点:主控端显式选过帧率就以它为准,否则按档位表的 maxFPS。
// 255=不限帧,编码器按 60 给码控提示(surface 编码器本来就按画面出帧)。
return Pair(kbps, getMaxFps().coerceIn(1, 60))
}
/** 服务端 CMD_QUALITY_LEVEL:level 有符号;persist=1 表示这是主控端选择的档位,要落盘 */
@JvmStatic
fun onQualityLevel(level: Int, persist: Boolean) {
if (persist) {
prefs()?.edit()?.putInt(K_LEVEL, level)?.apply()
Log.i(TAG, "quality level persisted: $level")
}
if (level in 0 until QUALITY_COUNT) {
val p = profile(level)
val kbps = if (p[3] > 0) p[3] else LEGACY_BITRATE_KBPS // Ultra/High 无 H.264 码率
Log.i(TAG, "quality level $level -> bitrate=${kbps}kbps fps=${p[0]}")
CaptureService.applyBitRate(kbps)
} else {
// -1 自适应:具体档位由服务端后续按网络状况下发;-2 关闭:不动原有设置
Log.i(TAG, "quality level mode: $level (rate unchanged)")
}
}
/** 服务端 CMD_FPS:255 = 不限帧;帧率只能在下次开抓时生效(见类注释)。原样保存,与 Windows 一致 */
@JvmStatic
fun onFps(fps: Int) {
val v = fps.coerceIn(1, 255)
prefs()?.edit()?.putInt(K_FPS, v)?.apply()
Log.i(TAG, "max fps -> $v (takes effect at next capture start)")
}
/** 服务端 CMD_QUALITY_PROFILES:6 档 × 4 字段,分号分隔档、逗号分隔字段 */
@JvmStatic
fun onQualityProfiles(csv: String) {
val rows = csv.split(';')
if (rows.size != QUALITY_COUNT) {
Log.e(TAG, "quality profiles: bad payload '${csv.take(64)}'")
return
}
val values = IntArray(QUALITY_COUNT * 4)
for ((i, row) in rows.withIndex()) {
val f = row.split(',')
if (f.size != 4) {
Log.e(TAG, "quality profiles: bad row $i")
return
}
for (j in 0 until 4) {
val n = f[j].toIntOrNull()
if (n == null || n < 0) {
Log.e(TAG, "quality profiles: bad number in row $i")
return
}
values[i * 4 + j] = n
}
}
prefs()?.edit()?.putString(K_PROFILES, values.joinToString(","))?.apply()
Log.i(TAG, "quality profiles stored: $csv")
}
/** 取某一档的 maxFPS/maxWidth/algorithm/bitRate(档位越界时按最近的合法档) */
private fun profile(level: Int): IntArray {
val raw = prefs()?.getString(K_PROFILES, null)
val table = if (raw != null) {
val v = raw.split(',').mapNotNull { it.toIntOrNull() }
if (v.size == QUALITY_COUNT * 4) v.toIntArray() else DEFAULT_PROFILES
} else DEFAULT_PROFILES
val base = level.coerceIn(0, QUALITY_COUNT - 1) * 4
return intArrayOf(table[base], table[base + 1], table[base + 2], table[base + 3])
}
}