Feature: remote desktop tuning flags, on by default with per-item opt-out

Adds "Advanced -> Remote desktop tuning" with three independently
checkable items, all enabled by default:

  x264 bitrate cap (VBV)       TUNING_X264_VBV        0x1
  GDI capture: no slice delay  TUNING_GDI_NO_SLEEP    0x2
  Send pacing FPS floor        TUNING_PACE_FPS_FLOOR  0x4

Unticking an item clears its bit and restores the pre-commit behaviour
exactly, so the escape hatch is one click per measure -- no rebuild, no
config edit. Each bit is independent; a problem with one does not force
turning all three off.

Plumbing
--------
Menu state -> [settings] Tuning* -> CMy2015RemoteDlg::GetScreenTuningFlags()
  -> CScreenSpyDlg::SendScreenTuning()  (right after TOKEN_BITMAPINFO)
  -> CMD_SCREEN_TUNING = 178 [cmd:1][flags:4 LE]
  -> CScreenManager::OnReceive -> m_nTuningFlags -> ScreenCapture::SetTuningFlags

Why it is safe to send to old clients: CScreenManager::OnReceive has no
default branch in its switch, so an unknown command falls straight out
(verified). 178 was unassigned (177 = COMMAND_SET_PROTO, 199 =
TOKEN_DECRYPT), and the enum re-anchors explicitly so nothing auto-
increments onto it.

Flags are sent even when all-zero on purpose: the client's flag lives in
CScreenManager, which survives closing and reopening the remote desktop
window on the same connection. Without the zero send, unticking the menu
would leave the previous flags in effect.

Per-measure notes
-----------------
VBV: capped CRF (peak = target, 0.5s buffer, init 0.9). The hardware
paths already cap via rc_max_rate (1.5x, 1s buffer); this is tighter and
only affects x264. After an IDR the buffer drains and takes up to 0.5s
to refill, so a brief quality dip follows every keyframe -- if that shows
up, relaxing to 1.5x/1s would match NVENC/QSV.

GDI: keeps the 50-line BitBlt slicing (large single BitBlts hit driver
limits) and only drops the 5ms sleep between slices -- 1080p is 22 slices
= a forced 110ms per frame against a 50ms budget at 20fps.

Pacing: clamps s0 to 2x the base interval instead of 4x (2fps -> 4fps),
taken as max(2x base, 1000/MaxFPS) so it never exceeds the configured
MaxFPS. The growth/decay code itself is untouched. This one works against
the loop's own congestion signal, so it is the first to untick if a bad
link feels laggier rather than smoother.

SetTuningFlags only rebuilds the encoder when TUNING_ENCODER_BITS
(currently just VBV) changes -- the GDI and pacing bits are read live by
the capture and send threads, and rebuilding for them would drop an IDR
and visibly flash the picture.

Defaults: the on-by-default value lives in the third argument of
THIS_CFG.GetInt(), which only applies when the key is absent. A machine
that already has an explicit 0 keeps it, so the flip is not retroactive
and a test machine can end up with a mixed state.

Verified on the Debug build: with no Tuning* keys all three items start
checked; unticking VBV alone leaves the other two checked and survives a
restart; en_US/de_DE/zh_TW translations all resolve. Not yet measured end
to end on a real remote desktop session.
This commit is contained in:
yuanyuanxiang committed 2026-11-04 14:47:40 +01:00
1 parent 1e336113a5
commit ca4e85826f
19 files changed
+239 -9

No files matched your search

Binary file not shown.
+73
View File
@@ -1149,6 +1149,9 @@ BEGIN_MESSAGE_MAP(CMy2015RemoteDlg, CDialogEx)
ON_COMMAND(ID_TOOL_PREVIEW_LEVELS, &CMy2015RemoteDlg::OnPreviewLevels)
ON_COMMAND(ID_TOOL_FILE_SPEED, &CMy2015RemoteDlg::OnFileTransferSpeed)
ON_COMMAND(ID_TOOL_EMBED_TLS_ANCHOR, &CMy2015RemoteDlg::OnToolEmbedTlsAnchor)
ON_COMMAND(ID_TOOL_TUNING_X264_VBV, &CMy2015RemoteDlg::OnToolTuningX264Vbv)
ON_COMMAND(ID_TOOL_TUNING_GDI_SLEEP, &CMy2015RemoteDlg::OnToolTuningGdiSleep)
ON_COMMAND(ID_TOOL_TUNING_PACE_FPS_FLOOR, &CMy2015RemoteDlg::OnToolTuningPaceFpsFloor)
ON_COMMAND(ID_EXECUTE_TESTRUN, &CMy2015RemoteDlg::OnExecuteTestrun)
ON_COMMAND(ID_EXECUTE_GHOST, &CMy2015RemoteDlg::OnExecuteGhost)
ON_COMMAND(ID_MASTER_TRAIL, &CMy2015RemoteDlg::OnMasterTrail)
@@ -2800,10 +2803,16 @@ BOOL CMy2015RemoteDlg::OnInitDialog()
m_runNormal = THIS_CFG.GetInt("settings", "RunNormal", 0);
// 高级菜单:DLL内嵌TLS证书(勾选状态来自 [settings] EmbedTlsInDll,见上面的加载)
// 以及「远程桌面调优」子菜单里的三项,默认全开;取消勾选即回到加开关前的行为。
// 第三参数是默认值:只对从没写过该键的机器生效,已写过 0 的存量机器保持关。
m_bTuningX264Vbv = THIS_CFG.GetInt("settings", "TuningX264Vbv", 1) != 0;
m_bTuningGdiNoSleep = THIS_CFG.GetInt("settings", "TuningGdiNoSleep", 1) != 0;
m_bTuningPaceFpsFloor = THIS_CFG.GetInt("settings", "TuningPaceFpsFloor", 1) != 0;
if (CMenu* advMenu = m_MainMenu.GetSubMenu(5)) {
advMenu->CheckMenuItem(ID_TOOL_EMBED_TLS_ANCHOR,
m_bEmbedTlsInDll ? MF_CHECKED : MF_UNCHECKED);
}
SyncTuningMenuChecks();
CMenu* SubMenu = m_MainMenu.GetSubMenu(2);
if (SubMenu) {
@@ -10762,6 +10771,70 @@ void CMy2015RemoteDlg::OnToolEmbedTlsAnchor()
}
}
// 把「高级 → 远程桌面调优」里各项的勾选状态同步到菜单。
// 这些项挂在子菜单里,在顶级菜单上 CheckMenuItem 找不到,必须先定位子菜单。
void CMy2015RemoteDlg::SyncTuningMenuChecks()
{
CMenu* advMenu = m_MainMenu.GetSubMenu(5);
if (advMenu == nullptr) return;
CMenu* tuningMenu = FindSubMenuByCommand(advMenu, ID_TOOL_TUNING_X264_VBV);
if (tuningMenu == nullptr) return;
tuningMenu->CheckMenuItem(ID_TOOL_TUNING_X264_VBV,
m_bTuningX264Vbv ? MF_CHECKED : MF_UNCHECKED);
tuningMenu->CheckMenuItem(ID_TOOL_TUNING_GDI_SLEEP,
m_bTuningGdiNoSleep ? MF_CHECKED : MF_UNCHECKED);
tuningMenu->CheckMenuItem(ID_TOOL_TUNING_PACE_FPS_FLOOR,
m_bTuningPaceFpsFloor ? MF_CHECKED : MF_UNCHECKED);
}
// 高级→远程桌面调优→x264 码率封顶(VBV):默认开,取消勾选即关闭。
// 只影响此后打开的远程桌面窗口(打开时经 CMD_SCREEN_TUNING 下发),已打开的窗口不变,
// 因为改编码器参数要重建编码器,会在画面上留下一次可感知的断流。
void CMy2015RemoteDlg::OnToolTuningX264Vbv()
{
m_bTuningX264Vbv = !m_bTuningX264Vbv;
THIS_CFG.SetInt("settings", "TuningX264Vbv", m_bTuningX264Vbv ? 1 : 0);
SyncTuningMenuChecks();
Mprintf("[Tuning] TuningX264Vbv = %d(对之后打开的远程桌面生效)\n",
m_bTuningX264Vbv ? 1 : 0);
}
// 高级→远程桌面调优→GDI 抓屏去分段延时:默认开,取消勾选即关闭。
// 客户端 GDI 抓屏按 50 行一段 BitBlt,段间 Sleep(5ms) 让出 CPU;1080p 要切 22 段,
// 等于每帧强制 110ms,而 20fps 的预算只有 50ms —— 抓屏本身就把帧率拖死了。
// 勾选后不再 Sleep,仍按行分段(避开大尺寸单次 BitBlt 的驱动限制)。
void CMy2015RemoteDlg::OnToolTuningGdiSleep()
{
m_bTuningGdiNoSleep = !m_bTuningGdiNoSleep;
THIS_CFG.SetInt("settings", "TuningGdiNoSleep", m_bTuningGdiNoSleep ? 1 : 0);
SyncTuningMenuChecks();
Mprintf("[Tuning] TuningGdiNoSleep = %d(对之后打开的远程桌面生效)\n",
m_bTuningGdiNoSleep ? 1 : 0);
}
// 高级→远程桌面调优→发送节流帧率下限:默认开,取消勾选即关闭。
// 客户端发送节流在网络慢时把帧间隔放大到 4 倍(≈2fps),操作反馈变得很钝。
// 勾选后上限收到 2 倍(≈4fps)。代价是链路已饱和时排队更明显——三项里这一项
// 最可能在弱网下变成负优化,怀疑画面"更连贯但更滞后"时优先关掉它。
void CMy2015RemoteDlg::OnToolTuningPaceFpsFloor()
{
m_bTuningPaceFpsFloor = !m_bTuningPaceFpsFloor;
THIS_CFG.SetInt("settings", "TuningPaceFpsFloor", m_bTuningPaceFpsFloor ? 1 : 0);
SyncTuningMenuChecks();
Mprintf("[Tuning] TuningPaceFpsFloor = %d(对之后打开的远程桌面生效)\n",
m_bTuningPaceFpsFloor ? 1 : 0);
}
// 当前调优开关位,供 CScreenSpyDlg 打开远程桌面时下发 CMD_SCREEN_TUNING。
int CMy2015RemoteDlg::GetScreenTuningFlags() const
{
int flags = 0;
if (m_bTuningX264Vbv) flags |= TUNING_X264_VBV;
if (m_bTuningGdiNoSleep) flags |= TUNING_GDI_NO_SLEEP;
if (m_bTuningPaceFpsFloor) flags |= TUNING_PACE_FPS_FLOOR;
return flags;
}
// 按当前开关与证书重建六份内核载荷(ServerDll/ShellCode/TinyRun × x86/x64)。
// 换证书、切开关后都要走一遍:已加载的旧 Buffer 里是旧锚,继续下发会让新客户端钉住旧证书,
// 证书轮换后它们就再也连不上(客户端是 fail-closed)。重建后 md5 变化,客户端下次请求时会重下。
+13
View File
@@ -280,6 +280,19 @@ public:
void ReloadKernelPayloads();
// 高级→DLL内嵌TLS证书
afx_msg void OnToolEmbedTlsAnchor();
// 高级→远程桌面调优:三项默认全开,取消勾选即回到加开关前的行为。
// 勾选后打开远程桌面时经 CMD_SCREEN_TUNING 下发,某项出问题可以只关那一项。
// 持久化在 [settings] Tuning*;注意默认值只对没写过该键的机器生效。
bool m_bTuningX264Vbv = true; // TUNING_X264_VBV x264 软编加 VBV 码率封顶
bool m_bTuningGdiNoSleep = true; // TUNING_GDI_NO_SLEEP GDI 抓屏去分段延时
bool m_bTuningPaceFpsFloor = true; // TUNING_PACE_FPS_FLOOR 发送节流帧率下限
afx_msg void OnToolTuningX264Vbv();
afx_msg void OnToolTuningGdiSleep();
afx_msg void OnToolTuningPaceFpsFloor();
// 把上述各项的勾选状态同步到「远程桌面调优」子菜单
void SyncTuningMenuChecks();
// 当前调优开关位,供 CScreenSpyDlg 下发 CMD_SCREEN_TUNING 用
int GetScreenTuningFlags() const;
MasterSettings m_settings;
// 缓存上次检测到的上级 FRP 配置([settings] FrpConfig),由定时器检测外部模块写入的变更。
// 检出变更后会热切换 FRPC(首次=启动 / 覆盖=重启 / 撤销=停止),并在主对话框信息列表中给出友好提示。
+15
View File
@@ -266,6 +266,18 @@ CScreenSpyDlg::CScreenSpyDlg(CMy2015RemoteDlg* Parent, Server* IOCPServer, CONTE
}
// 下发主控端「高级 → 远程桌面调优」的开关位:[cmd:1][flags:4 LE]。主控端默认 0x7。
// 全 0 也照发,客户端据此把可能残留的旧标志清掉(同一客户端先后被不同主控端打开时)。
// 老客户端不认识这条命令,OnReceive 的 switch 无 default 分支,直接忽略。
VOID CScreenSpyDlg::SendScreenTuning(void)
{
int flags = (m_pParent != nullptr) ? m_pParent->GetScreenTuningFlags() : 0;
BYTE cmd[1 + sizeof(int)] = { CMD_SCREEN_TUNING };
memcpy(cmd + 1, &flags, sizeof(int));
m_ContextObject->Send2Client(cmd, sizeof(cmd));
Mprintf("[ScreenSpy] 下发调优开关: 0x%08X\n", flags);
}
VOID CScreenSpyDlg::SendNext(void)
{
BYTE bToken[32] = { COMMAND_NEXT };
@@ -1147,6 +1159,9 @@ VOID CScreenSpyDlg::OnReceiveComplete()
m_ClientID = *((uint64_t*)(m_ContextObject->InDeCompressedBuffer.GetBuffer(1 + sizeof(BITMAPINFOHEADER))));
}
PrepareDrawing(m_BitmapInfor_Full);
// 调优开关紧随位图信息下发:此刻客户端的截屏对象刚建好、编码器还没创建
// (ensureEncoder 是 lazy 的),标志赶在第一帧编码前到位,不需要重建编码器。
SendScreenTuning();
// 重连或切换显示器后重新校验鼠标补偿(客户端上报分辨率可能已变化)。
LoadMouseScale();
// 分辨率切换完成,允许解码
+2
View File
@@ -223,6 +223,8 @@ public:
void BroadcastCursorToWeb(bool force);
VOID SendNext(void);
// 下发主控端「高级 → 远程桌面调优」的开关位(CMD_SCREEN_TUNING),主控端默认 0x7
VOID SendScreenTuning(void);
VOID OnReceiveComplete();
HDC m_hFullDC;
HDC m_hFullMemDC;
+4
View File
@@ -2086,6 +2086,10 @@ MCPԶ
高级(&A)=&Erweitert
远程桌面质量等级(&Q)=Remote Desktop &Qualitaetsstufen
远程桌面质量等级=Remote Desktop Qualitaetsstufen
远程桌面调优(&T)=Remote-Desktop-Optimierung(&T)
x264 码率封顶 (VBV)=x264 Bitraten-Deckel (VBV)
GDI 抓屏去分段延时=GDI-Aufnahme ohne Segment-Pause
发送节流帧率下限=Mindest-Framerate beim Senden
等级=Stufe
帧率(FPS)=FPS
最大宽度=Max. Breite
+4
View File
@@ -2091,6 +2091,10 @@ HTTPS
高级(&A)=&Advanced
远程桌面质量等级(&Q)=Remote Desktop &Quality Levels
远程桌面质量等级=Remote Desktop Quality Levels
远程桌面调优(&T)=Remote desktop tuning(&T)
x264 码率封顶 (VBV)=x264 bitrate cap (VBV)
GDI 抓屏去分段延时=GDI capture: no per-slice delay
发送节流帧率下限=Send pacing FPS floor
等级=Level
帧率(FPS)=FPS
最大宽度=Max Width
+4
View File
@@ -2084,6 +2084,10 @@ HTTPS
高级(&A)=進階(&A)
远程桌面质量等级(&Q)=遠端桌面品質等級(&Q)
远程桌面质量等级=遠端桌面品質等級
远程桌面调优(&T)=遠端桌面調校(&T)
x264 码率封顶 (VBV)=x264 位元率上限 (VBV)
GDI 抓屏去分段延时=GDI 擷取去除分段延遲
发送节流帧率下限=傳送節流影格率下限
等级=等級
帧率(FPS)=幀率(FPS)
最大宽度=最大寬度
+5 -1
View File
@@ -1122,6 +1122,10 @@
#define ID_PROTO_SWITCH_WSS 33100
// 高级菜单开关:把服务端 TLS 证书内嵌进下发给客户端的 ServerDll
#define ID_TOOL_EMBED_TLS_ANCHOR 33101
// 高级 → 远程桌面调优:逐项勾选的开关,三项默认全开,取消勾选即回到旧行为
#define ID_TOOL_TUNING_X264_VBV 33102
#define ID_TOOL_TUNING_GDI_SLEEP 33103
#define ID_TOOL_TUNING_PACE_FPS_FLOOR 33104
#define ID_EXIT_FULLSCREEN 40001
// Next default values for new objects
@@ -1129,7 +1133,7 @@
#ifdef APSTUDIO_INVOKED
#ifndef APSTUDIO_READONLY_SYMBOLS
#define _APS_NEXT_RESOURCE_VALUE 417
#define _APS_NEXT_COMMAND_VALUE 33102
#define _APS_NEXT_COMMAND_VALUE 33105
#define _APS_NEXT_CONTROL_VALUE 2621
#define _APS_NEXT_SYMED_VALUE 105
#endif