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:
1 parent
1e336113a5
commit
ca4e85826f
19 files changed
+239
-9
No files matched your search
@@ -60,8 +60,11 @@ std::unique_ptr<VideoEncoderBase> CreateEncoder(const EncoderRequest& req) {
|
||||
auto enc = std::make_unique<CX264Encoder>();
|
||||
p.rc = RateControl::CRF;
|
||||
p.crf = BitRateToCRF(req.bitrate_kbps);
|
||||
// capped CRF:由主控端「高级 → 远程桌面调优」控制,默认开、可取消勾选
|
||||
p.vbv_cap = req.vbv_cap;
|
||||
if (enc->open(p)) {
|
||||
Mprintf("=> encoder: %s (SW, crf=%d)\n", enc->backendName(), p.crf);
|
||||
Mprintf("=> encoder: %s (SW, crf=%d%s)\n", enc->backendName(), p.crf,
|
||||
p.vbv_cap ? ", vbv capped" : "");
|
||||
return enc;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,6 +12,9 @@ struct EncoderRequest {
|
||||
int fps = 30;
|
||||
int bitrate_kbps = 4000;
|
||||
int encodeLevel = LEVEL_H264_SOFT;
|
||||
// x264 软编加 VBV 峰值上限(capped CRF)。硬编路径本来就是 VBR + rc_max_rate,
|
||||
// 天然有上限,此标志对它们无影响。默认 false = 与历史行为一致。
|
||||
bool vbv_cap = false;
|
||||
};
|
||||
|
||||
// 按客户端能力 + 本机硬件能力创建一个 VideoEncoderBase。
|
||||
|
||||
@@ -171,6 +171,9 @@ public:
|
||||
int m_nScrollDetectInterval; // 滚动检测间隔(0=禁用, 1=每帧, 2=每2帧, ...)
|
||||
int m_nInstructionSet = 0;
|
||||
int m_nBitRate = 0; // H264 编码码率 (kbps), 0=自动
|
||||
int m_nTuningFlags = 0; // 远程桌面调优开关位(CMD_SCREEN_TUNING 下发)。
|
||||
// 本地初值恒为 0,由服务端在打开远程桌面时下发,
|
||||
// 主控端默认下发 0x7(三项全开);位定义见 TUNING_*
|
||||
|
||||
// 感兴趣区域 (ROI)
|
||||
RECT m_ROI = {0,0,0,0};
|
||||
@@ -895,6 +898,7 @@ public:
|
||||
req.fps = 20;
|
||||
req.bitrate_kbps = (m_nBitRate > 0) ? m_nBitRate : (width * height / 1266);
|
||||
req.encodeLevel = m_EncodeLevel;
|
||||
req.vbv_cap = (m_nTuningFlags & TUNING_X264_VBV) != 0;
|
||||
m_encoder = CreateEncoder(req);
|
||||
}
|
||||
|
||||
@@ -1087,6 +1091,31 @@ public:
|
||||
return m_nBitRate;
|
||||
}
|
||||
|
||||
// 远程桌面调优开关(CMD_SCREEN_TUNING)。返回 true 表示标志有变化。
|
||||
// 只有影响编码器参数的位(TUNING_ENCODER_BITS)变化才丢弃编码器;GDI 抓屏、发送节流
|
||||
// 这类位由抓屏/发送线程每次现读,改了立刻生效,不该为它们白丢一个 IDR 让画面闪一下。
|
||||
// 编码器重建后第一帧必然是 IDR,服务端能正确解码,不需要额外的关键帧握手。
|
||||
virtual bool SetTuningFlags(int flags)
|
||||
{
|
||||
if (m_nTuningFlags == flags) return false;
|
||||
const bool encoderChanged = ((m_nTuningFlags ^ flags) & TUNING_ENCODER_BITS) != 0;
|
||||
m_nTuningFlags = flags;
|
||||
if (encoderChanged && m_encoder) {
|
||||
Mprintf("=> TuningFlags: 0x%08X,重建编码器以应用新参数\n", flags);
|
||||
m_encoder.reset();
|
||||
m_bEncoderPrimed = false; // 重建后首帧必须真正 encode,不能走 skip
|
||||
} else {
|
||||
Mprintf("=> TuningFlags: 0x%08X(%s)\n", flags,
|
||||
m_encoder ? "不影响编码器,无需重建" : "编码器尚未创建,首帧时生效");
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
virtual int GetTuningFlags() const
|
||||
{
|
||||
return m_nTuningFlags;
|
||||
}
|
||||
|
||||
// 鼠标位置转换:将服务端坐标(基于发送分辨率)转换为客户端坐标(原始分辨率)
|
||||
virtual void PointConversion(POINT& pt) const
|
||||
{
|
||||
|
||||
@@ -603,6 +603,11 @@ void CScreenManager::InitScreenSpy()
|
||||
if (m_ScreenSpyObject && level >= 0 && level < QUALITY_COUNT) {
|
||||
m_ScreenSpyObject->SetBitRate(m_QualityProfiles[level].bitRate);
|
||||
}
|
||||
// 调优开关:服务端在打开远程桌面时下发,截屏对象因换窗口/改分辨率重建后要重新应用,
|
||||
// 否则重建出来的编码器又退回默认参数。
|
||||
if (m_ScreenSpyObject && m_nTuningFlags) {
|
||||
m_ScreenSpyObject->SetTuningFlags(m_nTuningFlags);
|
||||
}
|
||||
}
|
||||
|
||||
BOOL IsRunningAsSystem()
|
||||
@@ -735,6 +740,15 @@ DWORD WINAPI CScreenManager::WorkThreadProc(LPVOID lParam)
|
||||
if (szBuffer) {
|
||||
s0 = max(s0, 1000./This->m_ScreenSettings.MaxFPS); // 最快每秒20帧
|
||||
s0 = min(s0, 1000);
|
||||
// 帧率下限:网络慢时节流会把帧间隔放大到 4 倍(fps=8 → 500ms → ≈2fps),
|
||||
// 操作反馈变得很钝。主控端「高级 → 远程桌面调优 → 发送节流帧率下限」默认开,
|
||||
// 上限收到 2 倍(≈4fps)。代价是链路已饱和时排队更明显,弱网下若觉得画面
|
||||
// 更连贯但更滞后,应在高级菜单里关掉这一项。
|
||||
// 下限不能越过用户配置的 MaxFPS(MaxFPS 很低时间隔本来就更长),取两者较大值。
|
||||
if (This->m_nTuningFlags & TUNING_PACE_FPS_FLOOR) {
|
||||
const float floorS0 = max(sleep * 2.f, 1000.f / This->m_ScreenSettings.MaxFPS);
|
||||
if (s0 > floorS0) s0 = floorS0;
|
||||
}
|
||||
int span = s0-(clock() - last);
|
||||
Sleep(span > 0 ? span : 1);
|
||||
if (span < 0) { // 发送数据耗时较长,网络较差或数据较多
|
||||
@@ -1132,6 +1146,20 @@ VOID CScreenManager::OnReceive(PBYTE szBuffer, ULONG ulLength)
|
||||
}
|
||||
break;
|
||||
}
|
||||
case CMD_SCREEN_TUNING: {
|
||||
// 远程桌面调优开关:[cmd:1][flags:4 LE]。主控端「高级 → 远程桌面调优」三项,
|
||||
// 默认下发 0x7;取消勾选时对应位清零,等于回到加开关前的行为。
|
||||
// 老客户端不认识这条命令,switch 无 default 直接忽略。
|
||||
if (ulLength < 1 + sizeof(int)) break;
|
||||
int flags = 0;
|
||||
memcpy(&flags, szBuffer + 1, sizeof(int));
|
||||
Mprintf("[CScreenManager] TuningFlags: 0x%08X\n", flags);
|
||||
m_nTuningFlags = flags;
|
||||
if (m_ScreenSpyObject) {
|
||||
m_ScreenSpyObject->SetTuningFlags(flags);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case CMD_INSTRUCTION_SET: {
|
||||
int set = unsigned(szBuffer[1]);
|
||||
iniFile cfg(CLIENT_PATH);
|
||||
|
||||
@@ -42,6 +42,9 @@ public:
|
||||
HANDLE m_hWorkThread;
|
||||
ScreenSettings m_ScreenSettings = { 20 };
|
||||
QualityProfile m_QualityProfiles[QUALITY_COUNT]; // 本地质量配置(可被服务端覆盖)
|
||||
// 远程桌面调优开关位(CMD_SCREEN_TUNING 下发),本地初值 0,主控端默认下发 0x7。
|
||||
// 保存在这里而不是 ScreenSettings 里,避免改动服务端↔客户端的固定结构体布局。
|
||||
int m_nTuningFlags = 0;
|
||||
|
||||
virtual void InitScreenSpy();
|
||||
void LoadQualityProfiles(); // 从配置文件加载质量配置
|
||||
|
||||
@@ -194,11 +194,16 @@ VOID CScreenSpy::ScanScreen(HDC hdcDest, HDC hdcSour, ULONG ulWidth, ULONG ulHei
|
||||
#else
|
||||
const ULONG ulJumpLine = 50;
|
||||
const ULONG ulJumpSleep = ulJumpLine / 10;
|
||||
// 段间 Sleep 的用途是让出 CPU(避免长时间占满一个核)。代价很大:1080p 要切 22 段,
|
||||
// 每帧强制 110ms,而 20fps 的每帧预算只有 50ms —— 抓屏本身就超时,帧率被拖死。
|
||||
// 主控端「高级 → 远程桌面调优 → GDI 抓屏去分段延时」勾选后不再 Sleep;
|
||||
// 仍按行分段,避开超大尺寸单次 BitBlt 的驱动限制。
|
||||
const bool bNoSleep = (GetTuningFlags() & TUNING_GDI_NO_SLEEP) != 0;
|
||||
for (int i = 0, ulToJump = 0; i < (int)ulHeight; i += ulToJump) {
|
||||
ULONG ulv1 = ulHeight - i;
|
||||
ulToJump = (ulv1 > ulJumpLine) ? ulJumpLine : ulv1;
|
||||
BitBlt(hdcDest, 0, i, ulWidth, ulToJump, hdcSour, 0, i, SRCCOPY);
|
||||
Sleep(ulJumpSleep);
|
||||
if (!bNoSleep) Sleep(ulJumpSleep);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -23,6 +23,11 @@ struct EncoderParams {
|
||||
int crf = 23; // 当 rc == CRF
|
||||
int bitrate_kbps = 4000; // 当 rc == BITRATE
|
||||
int gop_seconds = 15; // 关键帧间隔(秒),与 x264 i_keyint_max=fps*15 对齐
|
||||
|
||||
// 给 CRF 编码加 VBV 峰值上限(capped CRF)。硬编路径本来就是 VBR + rc_max_rate,
|
||||
// 天然有上限;x264 走纯 CRF 时没有,复杂帧能冲到目标码率好几倍,跨洲际会堵死链路。
|
||||
// 默认 false = 不加 VBV,与历史行为一致,由主控端「远程桌面调优」开关打开。
|
||||
bool vbv_cap = false;
|
||||
};
|
||||
|
||||
class VideoEncoderBase {
|
||||
|
||||
+20
-4
@@ -1,4 +1,5 @@
|
||||
#include "X264Encoder.h"
|
||||
#include "common/logger.h"
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
|
||||
@@ -17,7 +18,7 @@
|
||||
// 合规构建的桩实现:头文件已不含 x264 类型,这里也不能碰 m_Param / x264_param_t。
|
||||
CX264Encoder::CX264Encoder() { m_forceIDR = false; }
|
||||
CX264Encoder::~CX264Encoder() {}
|
||||
bool CX264Encoder::open(int, int, int, int) { return false; }
|
||||
bool CX264Encoder::open(int, int, int, int, int) { return false; }
|
||||
bool CX264Encoder::open(const EncoderParams&) { return false; }
|
||||
void CX264Encoder::close() {}
|
||||
int CX264Encoder::encode(uint8_t*, uint8_t, uint32_t, uint32_t, uint32_t, uint8_t**, uint32_t*, int) { return -1; }
|
||||
@@ -44,7 +45,7 @@ CX264Encoder::~CX264Encoder()
|
||||
close();
|
||||
}
|
||||
|
||||
bool CX264Encoder::open(int width, int height, int fps, int crf)
|
||||
bool CX264Encoder::open(int width, int height, int fps, int crf, int vbv_kbps)
|
||||
{
|
||||
x264_param_t param = { 0 };
|
||||
x264_param_default_preset(¶m, "ultrafast", "zerolatency");
|
||||
@@ -63,10 +64,19 @@ bool CX264Encoder::open(int width, int height, int fps, int crf)
|
||||
param.i_csp = X264_CSP_I420;
|
||||
|
||||
// CRF: 静态画面自动降至极低码率,动态画面按质量需要分配
|
||||
// 不设 VBV —— 避免 IDR/复杂帧后的质量振荡
|
||||
param.rc.i_rc_method = X264_RC_CRF;
|
||||
param.rc.f_rf_constant = (float)crf;
|
||||
|
||||
// capped CRF:CRF 保证画质,VBV 保证峰值不失控。纯 CRF 下复杂帧(视频、切大窗口)
|
||||
// 单帧能冲到目标码率好几倍,跨洲际 2Mbps 链路上一个 500KB 的帧要传 2 秒,
|
||||
// 后续帧全排队 → 用户看到的是"卡住几秒然后画面猛跳"。默认不开(vbv_kbps=0),
|
||||
// 由主控端「高级 → 远程桌面调优」勾选后下发 TUNING_X264_VBV 打开。
|
||||
if (vbv_kbps > 0) {
|
||||
param.rc.i_vbv_max_bitrate = vbv_kbps; // 峰值 = 目标码率
|
||||
param.rc.i_vbv_buffer_size = vbv_kbps / 2; // 0.5 秒缓冲:够压住尖峰,又不至于抖动
|
||||
param.rc.f_vbv_buffer_init = 0.9f; // 从接近满缓冲起步,避免首帧质量塌
|
||||
}
|
||||
|
||||
//设置profile.
|
||||
if (x264_param_apply_profile(¶m, x264_profile_names[0])) {
|
||||
return false;
|
||||
@@ -106,7 +116,13 @@ bool CX264Encoder::open(const EncoderParams& params)
|
||||
{
|
||||
// x264 软编只支持 CRF;调用方走 BITRATE 时降级为 CRF=23(与 BitRateToCRF 默认一致)
|
||||
int crf = (params.rc == RateControl::CRF) ? params.crf : 23;
|
||||
return open(params.width, params.height, params.fps, crf);
|
||||
// vbv_cap 打开时用 bitrate_kbps 当峰值上限;bitrate 未知(<=0)则不加,退回纯 CRF
|
||||
int vbv = params.vbv_cap ? params.bitrate_kbps : 0;
|
||||
if (vbv > 0) {
|
||||
Mprintf("=> x264: capped CRF, crf=%d, vbv_max=%dkbps, buffer=%dkb\n",
|
||||
crf, vbv, vbv / 2);
|
||||
}
|
||||
return open(params.width, params.height, params.fps, crf, vbv);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -19,7 +19,7 @@ private:
|
||||
bool m_forceIDR; // 保留成员以对齐两套实现的行为,桩里不使用
|
||||
public:
|
||||
// 旧签名保留:与非合规构建保持同一份对外接口
|
||||
bool open(int width, int height, int fps, int crf);
|
||||
bool open(int width, int height, int fps, int crf, int vbv_kbps = 0);
|
||||
|
||||
// VideoEncoderBase
|
||||
bool open(const EncoderParams& params) override;
|
||||
@@ -59,7 +59,8 @@ private:
|
||||
bool m_forceIDR; // 下一次 encode 强制 IDR
|
||||
public:
|
||||
// 旧签名保留:被 ScreenCapture 临时直接调;新增 EncoderParams overload 走接口路径
|
||||
bool open(int width, int height, int fps, int crf);
|
||||
// vbv_kbps > 0 时给 CRF 加 VBV 峰值上限(capped CRF),0 = 不加(历史行为)
|
||||
bool open(int width, int height, int fps, int crf, int vbv_kbps = 0);
|
||||
bool open(x264_param_t * param);
|
||||
|
||||
// VideoEncoderBase
|
||||
|
||||
Reference in new issue
Block a user