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
@@ -11,6 +11,7 @@
|
||||
#include <vector>
|
||||
#include <thread>
|
||||
#include <cstring>
|
||||
#include <string>
|
||||
#include <cinttypes>
|
||||
#include <android/log.h>
|
||||
|
||||
@@ -26,6 +27,12 @@ extern void DispatchClipboardText(const uint8_t* data, size_t size);
|
||||
extern void DispatchLockScreen();
|
||||
// 隐私 overlay(COMMAND_SCREEN_BLANK / COMMAND_SCREEN_BLOCK_INPUT),定义在 main.cpp
|
||||
extern void DispatchPrivacyOverlay(bool black, bool block);
|
||||
// 画质/帧率协商:读写 ScreenTuning(SharedPreferences),定义在 main.cpp
|
||||
extern int GetClientQualityLevel(); // 上报用,读不到时回落 QUALITY_GOOD
|
||||
extern int GetClientMaxFps(); // 上报用,读不到时回落 20
|
||||
extern void DispatchQualityLevel(int level, bool persist);
|
||||
extern void DispatchMaxFps(int fps);
|
||||
extern void DispatchQualityProfilesCsv(const char* csv);
|
||||
|
||||
// Linux/macOS 共用的 BITMAPINFOHEADER 布局(与 Windows 完全一致)
|
||||
#pragma pack(push, 1)
|
||||
@@ -95,10 +102,13 @@ public:
|
||||
memcpy(&buf[off + 8], &zero, 8);
|
||||
|
||||
ScreenSettings ss = {};
|
||||
ss.MaxFPS = 10;
|
||||
ss.MaxFPS = GetClientMaxFps();
|
||||
ss.ScreenWidth = m_width;
|
||||
ss.ScreenHeight = m_height;
|
||||
ss.QualityLevel = QUALITY_GOOD; // 告知服务端使用 H264 解码器
|
||||
// 上报主控端上次为本机选择的档位(含 QUALITY_ADAPTIVE)。写死成 Good 会让服务端
|
||||
// 认为这是「固定档位会话」,m_AdaptiveQuality.enabled 永远为 false——自适应质量
|
||||
// 在 Android 主机上根本不会启动。具体编码参数由 ScreenTuning 在 Kotlin 侧应用。
|
||||
ss.QualityLevel = GetClientQualityLevel();
|
||||
ss.ScreenType = USING_VIRTUAL; // 虚拟显示
|
||||
memcpy(&buf[off + 16], &ss, sizeof(ss));
|
||||
|
||||
@@ -132,8 +142,58 @@ public:
|
||||
m_started = true;
|
||||
m_cond.notify_all();
|
||||
break;
|
||||
case CMD_QUALITY_LEVEL:
|
||||
if (size >= 2) LOGI_SH("QualityLevel=%d", (int)(int8_t)data[1]);
|
||||
case CMD_QUALITY_LEVEL: {
|
||||
// [cmd][level:int8][persist:1]。level 有符号(-2 关闭 / -1 自适应 / 0-5 具体档),
|
||||
// persist=1 表示这是主控端选定的档位、要落盘(老主控端的负载只有 4 字节)。
|
||||
if (size >= 2) {
|
||||
const int level = (int)(int8_t)data[1];
|
||||
const bool persist = size >= 3 && data[2] != 0;
|
||||
DispatchQualityLevel(level, persist);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case CMD_FPS:
|
||||
// [cmd][fps:1],255 = 不限帧。帧率只能写进 MediaCodec 的 KEY_FRAME_RATE
|
||||
// (码控提示),surface 编码器没有运行时限帧开关,所以下次开抓才生效。
|
||||
if (size >= 2) DispatchMaxFps((int)(uint8_t)data[1]);
|
||||
break;
|
||||
case CMD_QUALITY_PROFILES: {
|
||||
// [cmd][QualityProfile × QUALITY_COUNT](每档 16 字节)。存下来供开抓与
|
||||
// 档位切换时按主控端自定义的码率表取参数(主控端权威,见 ScreenSpyDlg)。
|
||||
const size_t need = 1 + sizeof(QualityProfile) * QUALITY_COUNT;
|
||||
if ((size_t)size < need) {
|
||||
LOGE_SH("QUALITY_PROFILES short: %u < %u", (unsigned)size, (unsigned)need);
|
||||
break;
|
||||
}
|
||||
std::string csv;
|
||||
for (int i = 0; i < QUALITY_COUNT; i++) {
|
||||
QualityProfile p;
|
||||
memcpy(&p, data + 1 + i * sizeof(QualityProfile), sizeof(p));
|
||||
if (i) csv += ';';
|
||||
csv += std::to_string(p.maxFPS) + ',' + std::to_string(p.maxWidth) + ',' +
|
||||
std::to_string(p.algorithm) + ',' + std::to_string(p.bitRate);
|
||||
}
|
||||
DispatchQualityProfilesCsv(csv.c_str());
|
||||
break;
|
||||
}
|
||||
case CMD_SCREEN_TUNING: {
|
||||
// [cmd][flags:4][version:1]。Android 抓屏是 MediaProjection + MediaCodec(H264),
|
||||
// 四个开关(x264 VBV / GDI 去延时 / 发送节流 / 自适应判据)都没有对应实现,
|
||||
// 因此只记日志、不落盘,也不声明 SCREEN_CAP_TUNING_FLAGS。
|
||||
uint32_t flags = 0;
|
||||
if (size >= 5) memcpy(&flags, data + 1, 4);
|
||||
const int ver = size >= 6 ? (int)data[5] : -1;
|
||||
LOGI_SH("SCREEN_TUNING flags=0x%X ver=%d (no-op on Android)", flags, ver);
|
||||
break;
|
||||
}
|
||||
case CMD_SCREEN_SIZE:
|
||||
// [cmd][strategy][maxWidth:int]。自适应质量要求缩放编码宽度;Android 源是
|
||||
// 整屏抓取、尺寸在开抓时固定(改尺寸需重建 MediaCodec),故忽略。
|
||||
// 服务端会按「客户端可能重启」先置分辨率切换标记,1 秒后超时自愈
|
||||
// (CheckResolutionChangeTimeout:刷新解码器 + 请求 IDR),表现为切档后
|
||||
// 画面停约 1 秒。回发 TOKEN_BITMAPINFO 能立刻清标记,但会连带 PrepareDrawing、
|
||||
// 通知 web 端重建解码器,比 1 秒停顿更扰动,因此不这么做。
|
||||
LOGI_SH("SCREEN_SIZE ignored on Android (%u bytes)", (unsigned)size);
|
||||
break;
|
||||
case COMMAND_SCREEN_SET_CLIPBOARD:
|
||||
// 服务端写剪贴板: [cmd:1][GBK text:N](与 Windows CF_TEXT 同编码;人类剪贴板
|
||||
|
||||
@@ -250,6 +250,13 @@ static jmethodID g_nativeExitMid = nullptr; // CaptureService.onNativeEx
|
||||
static jmethodID g_updateReceivedMid = nullptr; // CaptureService.onUpdateReceived(String) — 升级 APK 已落盘
|
||||
static jmethodID g_clipboardMid = nullptr; // CaptureService.onClipboardText([B)V — 远程剪贴板
|
||||
static jmethodID g_uninstallMid = nullptr; // CaptureService.onUninstallRequested()V — TOKEN_UNINSTALL
|
||||
// ScreenTuning(远程桌面画质/帧率协商:CMD_QUALITY_LEVEL / CMD_FPS / CMD_QUALITY_PROFILES)
|
||||
static jclass g_tuningClass = nullptr;
|
||||
static jmethodID g_getQualityLevelMid = nullptr; // ScreenTuning.getQualityLevel()I — 上报 ScreenSettings
|
||||
static jmethodID g_getMaxFpsMid = nullptr; // ScreenTuning.getMaxFps()I
|
||||
static jmethodID g_onQualityLevelMid = nullptr; // ScreenTuning.onQualityLevel(IZ)V
|
||||
static jmethodID g_onFpsMid = nullptr; // ScreenTuning.onFps(I)V
|
||||
static jmethodID g_onQualityProfilesMid = nullptr; // ScreenTuning.onQualityProfiles(String)V
|
||||
// ChatSession(远程交谈界面:帧入站文本、发送按钮)
|
||||
static jclass g_chatClass = nullptr;
|
||||
static jmethodID g_chatEventMid = nullptr; // ChatSession.onNativeEvent(I[B)V
|
||||
@@ -431,6 +438,110 @@ void DispatchPrivacyOverlay(bool black, bool block)
|
||||
if (attached) g_jvm->DetachCurrentThread();
|
||||
}
|
||||
|
||||
// ── 远程桌面画质/帧率协商 ────────────────────────────────────────────────────
|
||||
// 服务端在屏幕子连接里下发 CMD_QUALITY_LEVEL / CMD_FPS / CMD_QUALITY_PROFILES,
|
||||
// 客户端把档位/码率表/帧率存进 SharedPreferences,下次建立子连接时随 ScreenSettings
|
||||
// 上报(否则写死的 Good 会让服务端的自适应质量永远不启用)。C++ 只做协议解析,
|
||||
// 档位到编码器的落点在 Kotlin(MediaCodec 在那一侧)。
|
||||
|
||||
// attach 当前线程取 JNIEnv(子连接接收线程通常处于 detached 状态);失败返回 nullptr。
|
||||
static JNIEnv* AttachForJni(bool& attached)
|
||||
{
|
||||
attached = false;
|
||||
if (!g_jvm) return nullptr;
|
||||
JNIEnv* jenv = nullptr;
|
||||
jint st = g_jvm->GetEnv((void**)&jenv, JNI_VERSION_1_6);
|
||||
if (st == JNI_EDETACHED) {
|
||||
if (g_jvm->AttachCurrentThread(&jenv, nullptr) != JNI_OK || !jenv) return nullptr;
|
||||
attached = true;
|
||||
} else if (st != JNI_OK || !jenv) {
|
||||
return nullptr;
|
||||
}
|
||||
return jenv;
|
||||
}
|
||||
|
||||
// 屏幕子连接建立时填 ScreenSettings 用;读不到(服务未起)时按 Good/20 兜底。
|
||||
int GetClientQualityLevel()
|
||||
{
|
||||
if (!g_tuningClass || !g_getQualityLevelMid) return QUALITY_GOOD;
|
||||
bool attached = false;
|
||||
JNIEnv* jenv = AttachForJni(attached);
|
||||
if (!jenv) return QUALITY_GOOD;
|
||||
jint v = jenv->CallStaticIntMethod(g_tuningClass, g_getQualityLevelMid);
|
||||
if (jenv->ExceptionCheck()) { jenv->ExceptionClear(); v = QUALITY_GOOD; }
|
||||
if (attached) g_jvm->DetachCurrentThread();
|
||||
return (int)v;
|
||||
}
|
||||
|
||||
int GetClientMaxFps()
|
||||
{
|
||||
if (!g_tuningClass || !g_getMaxFpsMid) return 20;
|
||||
bool attached = false;
|
||||
JNIEnv* jenv = AttachForJni(attached);
|
||||
if (!jenv) return 20;
|
||||
jint v = jenv->CallStaticIntMethod(g_tuningClass, g_getMaxFpsMid);
|
||||
if (jenv->ExceptionCheck()) { jenv->ExceptionClear(); v = 20; }
|
||||
if (attached) g_jvm->DetachCurrentThread();
|
||||
return (int)v;
|
||||
}
|
||||
|
||||
// CMD_QUALITY_LEVEL:level 有符号(-2 关闭 / -1 自适应 / 0-5 具体档位),persist 表示落盘。
|
||||
void DispatchQualityLevel(int level, bool persist)
|
||||
{
|
||||
if (!g_tuningClass || !g_onQualityLevelMid) {
|
||||
LOGE("QUALITY: JNI not ready (cls=%p method=%p)", g_tuningClass, g_onQualityLevelMid);
|
||||
return;
|
||||
}
|
||||
bool attached = false;
|
||||
JNIEnv* jenv = AttachForJni(attached);
|
||||
if (!jenv) return;
|
||||
jenv->CallStaticVoidMethod(g_tuningClass, g_onQualityLevelMid,
|
||||
(jint)level, persist ? JNI_TRUE : JNI_FALSE);
|
||||
if (jenv->ExceptionCheck()) { jenv->ExceptionClear(); LOGE("QUALITY: onQualityLevel threw"); }
|
||||
LOGI("QUALITY: level=%d persist=%d", level, (int)persist);
|
||||
if (attached) g_jvm->DetachCurrentThread();
|
||||
}
|
||||
|
||||
// CMD_FPS:byte[1] = 最大帧率(255 = 不限)。帧率只在下次开抓时生效(见 ScreenTuning 注释)。
|
||||
void DispatchMaxFps(int fps)
|
||||
{
|
||||
if (!g_tuningClass || !g_onFpsMid) {
|
||||
LOGE("QUALITY: JNI not ready (cls=%p method=%p)", g_tuningClass, g_onFpsMid);
|
||||
return;
|
||||
}
|
||||
bool attached = false;
|
||||
JNIEnv* jenv = AttachForJni(attached);
|
||||
if (!jenv) return;
|
||||
jenv->CallStaticVoidMethod(g_tuningClass, g_onFpsMid, (jint)fps);
|
||||
if (jenv->ExceptionCheck()) { jenv->ExceptionClear(); LOGE("QUALITY: onFps threw"); }
|
||||
LOGI("QUALITY: maxFps=%d", fps);
|
||||
if (attached) g_jvm->DetachCurrentThread();
|
||||
}
|
||||
|
||||
// CMD_QUALITY_PROFILES:6 档 × 4 字段,C++ 侧拼成 "fps,width,algo,bitrate;..." 交 Kotlin 解析落盘。
|
||||
void DispatchQualityProfilesCsv(const char* csv)
|
||||
{
|
||||
if (!g_tuningClass || !g_onQualityProfilesMid) {
|
||||
LOGE("QUALITY: JNI not ready (cls=%p method=%p)", g_tuningClass, g_onQualityProfilesMid);
|
||||
return;
|
||||
}
|
||||
bool attached = false;
|
||||
JNIEnv* jenv = AttachForJni(attached);
|
||||
if (!jenv) return;
|
||||
jstring s = jenv->NewStringUTF(csv);
|
||||
if (!s) {
|
||||
if (jenv->ExceptionCheck()) jenv->ExceptionClear();
|
||||
LOGE("QUALITY: NewStringUTF failed");
|
||||
if (attached) g_jvm->DetachCurrentThread();
|
||||
return;
|
||||
}
|
||||
jenv->CallStaticVoidMethod(g_tuningClass, g_onQualityProfilesMid, s);
|
||||
if (jenv->ExceptionCheck()) { jenv->ExceptionClear(); LOGE("QUALITY: onQualityProfiles threw"); }
|
||||
jenv->DeleteLocalRef(s);
|
||||
LOGI("QUALITY: profiles %s", csv);
|
||||
if (attached) g_jvm->DetachCurrentThread();
|
||||
}
|
||||
|
||||
static std::string GetActiveWindowFromJava()
|
||||
{
|
||||
if (!g_jvm || !g_ctrlClass || !g_getActiveWindowMid) return "Android";
|
||||
@@ -1777,6 +1888,29 @@ Java_com_yama_client_YamaBridge_nativeInit(
|
||||
} else {
|
||||
LOGE("ChatSession class not found");
|
||||
}
|
||||
// 远程桌面画质/帧率协商:ScreenTuning 的读写入口(见本文件上方的说明)
|
||||
jclass tuningCls = env->FindClass("com/yama/client/ScreenTuning");
|
||||
if (env->ExceptionCheck()) { env->ExceptionClear(); tuningCls = nullptr; }
|
||||
if (tuningCls) {
|
||||
g_tuningClass = (jclass)env->NewGlobalRef(tuningCls);
|
||||
g_getQualityLevelMid = env->GetStaticMethodID(g_tuningClass, "getQualityLevel", "()I");
|
||||
if (env->ExceptionCheck()) { env->ExceptionClear(); g_getQualityLevelMid = nullptr; }
|
||||
if (!g_getQualityLevelMid) LOGE("ScreenTuning.getQualityLevel not found");
|
||||
g_getMaxFpsMid = env->GetStaticMethodID(g_tuningClass, "getMaxFps", "()I");
|
||||
if (env->ExceptionCheck()) { env->ExceptionClear(); g_getMaxFpsMid = nullptr; }
|
||||
if (!g_getMaxFpsMid) LOGE("ScreenTuning.getMaxFps not found");
|
||||
g_onQualityLevelMid = env->GetStaticMethodID(g_tuningClass, "onQualityLevel", "(IZ)V");
|
||||
if (env->ExceptionCheck()) { env->ExceptionClear(); g_onQualityLevelMid = nullptr; }
|
||||
if (!g_onQualityLevelMid) LOGE("ScreenTuning.onQualityLevel not found");
|
||||
g_onFpsMid = env->GetStaticMethodID(g_tuningClass, "onFps", "(I)V");
|
||||
if (env->ExceptionCheck()) { env->ExceptionClear(); g_onFpsMid = nullptr; }
|
||||
if (!g_onFpsMid) LOGE("ScreenTuning.onFps not found");
|
||||
g_onQualityProfilesMid = env->GetStaticMethodID(g_tuningClass, "onQualityProfiles", "(Ljava/lang/String;)V");
|
||||
if (env->ExceptionCheck()) { env->ExceptionClear(); g_onQualityProfilesMid = nullptr; }
|
||||
if (!g_onQualityProfilesMid) LOGE("ScreenTuning.onQualityProfiles not found");
|
||||
} else {
|
||||
LOGE("ScreenTuning class not found");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user