Files
SimpleRemoter/android/app/src/main/java/com/yama/client/ScreenTuning.kt
T
yuanyuanxianganddeepseek-flash 3ab5154db1 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
2026-11-11 21:58:54 +01:00

161 lines
7.3 KiB
Kotlin
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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])
}
}