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.
296 lines
12 KiB
C++
296 lines
12 KiB
C++
// ScreenSpy.cpp: implementation of the CScreenSpy class.
|
||
//
|
||
//////////////////////////////////////////////////////////////////////
|
||
|
||
#include "stdafx.h"
|
||
#include "ScreenSpy.h"
|
||
#include "Common.h"
|
||
#include <stdio.h>
|
||
#include <common/iniFile.h>
|
||
|
||
//////////////////////////////////////////////////////////////////////
|
||
// Construction/Destruction
|
||
//////////////////////////////////////////////////////////////////////
|
||
|
||
CScreenSpy::CScreenSpy(ULONG ulbiBitCount, BYTE algo, BOOL vDesk, int gop, BOOL all, int level,
|
||
RECT rc, BOOL switchScreen, HWND hwnd, bool dynamicFg) :
|
||
ScreenCapture(ulbiBitCount, algo, all, level, rc, switchScreen)
|
||
{
|
||
m_hTargetWnd = hwnd;
|
||
m_bDynamicForeground = dynamicFg;
|
||
m_GOP = gop;
|
||
|
||
// 窗口捕获模式:用 DWM 真实边界覆盖基类的全屏尺寸,并缓存阴影偏移量
|
||
if (hwnd) {
|
||
RECT wndRc = {}, frameRc = {};
|
||
GetWindowRect(hwnd, &wndRc);
|
||
if (SUCCEEDED(DwmGetWindowAttribute(hwnd, DWMWA_EXTENDED_FRAME_BOUNDS, &frameRc, sizeof(frameRc)))) {
|
||
m_ShadowLeft = frameRc.left - wndRc.left;
|
||
m_ShadowTop = frameRc.top - wndRc.top;
|
||
} else {
|
||
frameRc = wndRc;
|
||
}
|
||
m_ulFullWidth = frameRc.right - frameRc.left;
|
||
m_ulFullHeight = frameRc.bottom - frameRc.top;
|
||
m_CachedWndW = wndRc.right - wndRc.left;
|
||
m_CachedWndH = wndRc.bottom - wndRc.top;
|
||
m_iScreenX = frameRc.left; // 窗口左上角在屏幕上的绝对坐标,供 PointConversion 使用
|
||
m_iScreenY = frameRc.top;
|
||
m_bZoomed = false;
|
||
Mprintf("CScreenSpy: 窗口捕获 HWND=%p 尺寸=%dx%d shadow=(%d,%d)\n",
|
||
hwnd, m_ulFullWidth, m_ulFullHeight, m_ShadowLeft, m_ShadowTop);
|
||
}
|
||
|
||
m_BitmapInfor_Full = ConstructBitmapInfo(ulbiBitCount, m_ulFullWidth, m_ulFullHeight);
|
||
|
||
iniFile cfg(CLIENT_PATH);
|
||
int strategy = cfg.GetInt("settings", "ScreenStrategy", 0);
|
||
int maxWidth = cfg.GetInt("settings", "ScreenWidth", 0);
|
||
m_BitmapInfor_Send = new BITMAPINFO(*m_BitmapInfor_Full);
|
||
m_nInstructionSet = cfg.GetInt("settings", "CpuSpeedup", 0);
|
||
|
||
Mprintf("CScreenSpy: strategy=%d, maxWidth=%d, fullWidth=%d, algo=%d\n",
|
||
strategy, maxWidth, m_BitmapInfor_Send->bmiHeader.biWidth, m_bAlgorithm);
|
||
|
||
if (strategy == 1) {
|
||
// strategy=1: 用户明确选择原始分辨率
|
||
Mprintf("CScreenSpy: 使用原始分辨率\n");
|
||
} else if (maxWidth > 0 && maxWidth < m_BitmapInfor_Send->bmiHeader.biWidth) {
|
||
// maxWidth>0: 自定义 maxWidth,等比缩放(自适应质量使用)
|
||
float ratio = (float)maxWidth / m_BitmapInfor_Send->bmiHeader.biWidth;
|
||
m_BitmapInfor_Send->bmiHeader.biWidth = maxWidth;
|
||
m_BitmapInfor_Send->bmiHeader.biHeight = (LONG)(m_BitmapInfor_Send->bmiHeader.biHeight * ratio);
|
||
m_BitmapInfor_Send->bmiHeader.biSizeImage =
|
||
((m_BitmapInfor_Send->bmiHeader.biWidth * m_BitmapInfor_Send->bmiHeader.biBitCount + 31) / 32) *
|
||
4 * m_BitmapInfor_Send->bmiHeader.biHeight;
|
||
Mprintf("CScreenSpy: 自定义分辨率 %dx%d\n",
|
||
m_BitmapInfor_Send->bmiHeader.biWidth, m_BitmapInfor_Send->bmiHeader.biHeight);
|
||
} else {
|
||
// strategy=0 或 maxWidth=0: 默认 1080p 限制(等比,避免竖屏被压扁)
|
||
FitDefaultSendSize(m_BitmapInfor_Send->bmiHeader.biWidth, m_BitmapInfor_Send->bmiHeader.biHeight);
|
||
m_BitmapInfor_Send->bmiHeader.biSizeImage =
|
||
((m_BitmapInfor_Send->bmiHeader.biWidth * m_BitmapInfor_Send->bmiHeader.biBitCount + 31) / 32) *
|
||
4 * m_BitmapInfor_Send->bmiHeader.biHeight;
|
||
Mprintf("CScreenSpy: 1080p 限制 %dx%d\n",
|
||
m_BitmapInfor_Send->bmiHeader.biWidth, m_BitmapInfor_Send->bmiHeader.biHeight);
|
||
}
|
||
|
||
m_hDeskTopDC = GetDC(NULL);
|
||
|
||
m_BitmapData_Full = NULL;
|
||
m_hFullMemDC = CreateCompatibleDC(m_hDeskTopDC);
|
||
m_BitmapHandle = ::CreateDIBSection(m_hDeskTopDC, m_BitmapInfor_Full, DIB_RGB_COLORS, &m_BitmapData_Full, NULL, NULL);
|
||
::SelectObject(m_hFullMemDC, m_BitmapHandle);
|
||
m_FirstBuffer = (LPBYTE)m_BitmapData_Full;
|
||
|
||
m_DiffBitmapData_Full = NULL;
|
||
m_hDiffMemDC = CreateCompatibleDC(m_hDeskTopDC);
|
||
m_DiffBitmapHandle = ::CreateDIBSection(m_hDeskTopDC, m_BitmapInfor_Full, DIB_RGB_COLORS, &m_DiffBitmapData_Full, NULL, NULL);
|
||
::SelectObject(m_hDiffMemDC, m_DiffBitmapHandle);
|
||
|
||
m_RectBuffer = new BYTE[m_BitmapInfor_Full->bmiHeader.biSizeImage * 2 + 12];
|
||
m_BmpZoomBuffer = new BYTE[m_BitmapInfor_Send->bmiHeader.biSizeImage * 2 + 12];
|
||
m_BmpZoomFirst = new BYTE[m_BitmapInfor_Send->bmiHeader.biSizeImage * 2 + 12];
|
||
m_FirstBuffer = scaleBitmap(m_BmpZoomFirst, m_FirstBuffer);
|
||
|
||
m_bVirtualPaint = vDesk;
|
||
m_data.Create(m_hDeskTopDC, m_iScreenX, m_iScreenY, m_ulFullWidth, m_ulFullHeight);
|
||
// ROI
|
||
int w = m_ROI.right - m_ROI.left, h = m_ROI.bottom - m_ROI.top;
|
||
if (w > 0 && h > 0) {
|
||
m_nScaleSendWidth = m_BitmapInfor_Send->bmiHeader.biWidth;
|
||
m_nScaleSendHeight = m_BitmapInfor_Send->bmiHeader.biHeight;
|
||
m_BitmapInfor_Send->bmiHeader.biWidth = w;
|
||
m_BitmapInfor_Send->bmiHeader.biHeight = h;
|
||
m_BitmapInfor_Send->bmiHeader.biSizeImage = w * h * 4;
|
||
}
|
||
}
|
||
|
||
|
||
CScreenSpy::~CScreenSpy()
|
||
{
|
||
if (m_BitmapInfor_Full != NULL) {
|
||
delete m_BitmapInfor_Full;
|
||
m_BitmapInfor_Full = NULL;
|
||
}
|
||
|
||
ReleaseDC(NULL, m_hDeskTopDC);
|
||
|
||
if (m_hFullMemDC!=NULL) {
|
||
DeleteDC(m_hFullMemDC);
|
||
|
||
::DeleteObject(m_BitmapHandle);
|
||
if (m_BitmapData_Full!=NULL) {
|
||
m_BitmapData_Full = NULL;
|
||
}
|
||
|
||
m_hFullMemDC = NULL;
|
||
}
|
||
|
||
if (m_hDiffMemDC!=NULL) {
|
||
DeleteDC(m_hDiffMemDC);
|
||
|
||
::DeleteObject(m_DiffBitmapHandle);
|
||
if (m_DiffBitmapData_Full!=NULL) {
|
||
m_DiffBitmapData_Full = NULL;
|
||
}
|
||
}
|
||
|
||
if (m_RectBuffer) {
|
||
delete[] m_RectBuffer;
|
||
m_RectBuffer = NULL;
|
||
}
|
||
}
|
||
|
||
LPBYTE CScreenSpy::GetFirstScreenData(ULONG* ulFirstScreenLength)
|
||
{
|
||
if (m_hTargetWnd && IsIconic(m_hTargetWnd)) {
|
||
*ulFirstScreenLength = 0;
|
||
return nullptr;
|
||
}
|
||
ScanScreen(m_hFullMemDC, m_hDeskTopDC, m_ulFullWidth, m_ulFullHeight);
|
||
m_RectBuffer[0] = TOKEN_FIRSTSCREEN;
|
||
LPBYTE bmp = scaleBitmap(m_BmpZoomBuffer, (LPBYTE)m_BitmapData_Full);
|
||
memcpy(m_FirstBuffer, bmp, m_BitmapInfor_Send->bmiHeader.biSizeImage);
|
||
// H264/AV1:不发原始位图,IDR 到达后服务端自行解锁;节省每次切窗口的流量峰值
|
||
if (m_bAlgorithm == ALGORITHM_H264) {
|
||
*ulFirstScreenLength = 0;
|
||
return nullptr;
|
||
}
|
||
memcpy(1 + m_RectBuffer, bmp, m_BitmapInfor_Send->bmiHeader.biSizeImage);
|
||
if (m_bAlgorithm == ALGORITHM_GRAY) {
|
||
ToGray(1 + m_RectBuffer, 1 + m_RectBuffer, m_BitmapInfor_Send->bmiHeader.biSizeImage);
|
||
}
|
||
*ulFirstScreenLength = m_BitmapInfor_Send->bmiHeader.biSizeImage;
|
||
|
||
return m_RectBuffer; //内存
|
||
}
|
||
|
||
|
||
VOID CScreenSpy::ScanScreen(HDC hdcDest, HDC hdcSour, ULONG ulWidth, ULONG ulHeight)
|
||
{
|
||
if (m_hTargetWnd || m_bDynamicForeground) {
|
||
if (m_bDynamicForeground && !UpdateDynamicForeground()) return;
|
||
if (IsIconic(m_hTargetWnd)) return;
|
||
if (!CheckWindowResize()) return;
|
||
CaptureWindowContent(hdcDest, hdcSour, ulWidth, ulHeight);
|
||
return;
|
||
}
|
||
if (m_bVirtualPaint) {
|
||
RECT rcFill = { 0, 0, (LONG)ulWidth, (LONG)ulHeight };
|
||
HBRUSH hBrush = CreateSolidBrush(RGB(30, 30, 30));
|
||
FillRect(hdcDest, &rcFill, hBrush);
|
||
DeleteObject(hBrush);
|
||
int n = 0;
|
||
if (n = EnumWindowsTopToDown(NULL, EnumHwndsPrint, (LPARAM)&m_data.SetScreenDC(hdcDest))) {
|
||
Mprintf("EnumWindowsTopToDown failed: %d!!! GetLastError: %d\n", n, GetLastError());
|
||
Sleep(50);
|
||
}
|
||
return;
|
||
}
|
||
AUTO_TICK(70, "");
|
||
#if COPY_ALL
|
||
BitBlt(hdcDest, 0, 0, ulWidth, ulHeight, hdcSour, m_iScreenX, m_iScreenY, SRCCOPY);
|
||
#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);
|
||
if (!bNoSleep) Sleep(ulJumpSleep);
|
||
}
|
||
#endif
|
||
}
|
||
|
||
// 每帧跟踪前景窗口;切换时尺寸相同直接复用,尺寸不同触发重建
|
||
// 返回 false 表示本帧跳过(冻结)
|
||
bool CScreenSpy::UpdateDynamicForeground()
|
||
{
|
||
HWND fg = GetForegroundWindow();
|
||
if (fg && fg != m_hTargetWnd) {
|
||
RECT wndRc = {}, frameRc = {};
|
||
GetWindowRect(fg, &wndRc);
|
||
frameRc = wndRc;
|
||
DwmGetWindowAttribute(fg, DWMWA_EXTENDED_FRAME_BOUNDS, &frameRc, sizeof(frameRc));
|
||
ULONG newW = (ULONG)(frameRc.right - frameRc.left);
|
||
ULONG newH = (ULONG)(frameRc.bottom - frameRc.top);
|
||
if (newW != m_ulFullWidth || newH != m_ulFullHeight) {
|
||
// 尺寸不同:让 WorkThread 重建,m_NextTargetWnd 传递新 HWND
|
||
m_NextTargetWnd = fg;
|
||
m_bNeedRestart = true;
|
||
return false;
|
||
}
|
||
// 尺寸相同:直接切换,更新阴影偏移缓存,无需重建
|
||
m_ShadowLeft = frameRc.left - wndRc.left;
|
||
m_ShadowTop = frameRc.top - wndRc.top;
|
||
m_CachedWndW = wndRc.right - wndRc.left;
|
||
m_CachedWndH = wndRc.bottom - wndRc.top;
|
||
m_PendingWndW = m_PendingWndH = 0;
|
||
m_hTargetWnd = fg;
|
||
Mprintf("CScreenSpy: 前景切换(同尺寸) -> HWND=%p\n", fg);
|
||
}
|
||
return m_hTargetWnd != NULL; // NULL=无前景窗口,冻结上一帧
|
||
}
|
||
|
||
// 检测窗口 resize:GetWindowRect 每帧调用;DWM 查询仅在尺寸稳定 300ms 后触发
|
||
// 同时更新 m_iScreenX/Y(窗口移动时坐标同步),返回 false 表示本帧跳过
|
||
bool CScreenSpy::CheckWindowResize()
|
||
{
|
||
static const DWORD RESIZE_DEBOUNCE_MS = 300;
|
||
RECT fullRc = {};
|
||
GetWindowRect(m_hTargetWnd, &fullRc);
|
||
m_iScreenX = fullRc.left + m_ShadowLeft;
|
||
m_iScreenY = fullRc.top + m_ShadowTop;
|
||
int w = fullRc.right - fullRc.left;
|
||
int h = fullRc.bottom - fullRc.top;
|
||
if (w == m_CachedWndW && h == m_CachedWndH)
|
||
return true;
|
||
// 尺寸有变化:更新防抖记录
|
||
if (w != m_PendingWndW || h != m_PendingWndH) {
|
||
m_PendingWndW = w;
|
||
m_PendingWndH = h;
|
||
m_SizeChangeTick = GetTickCount();
|
||
}
|
||
if (GetTickCount() - m_SizeChangeTick < RESIZE_DEBOUNCE_MS)
|
||
return false; // 尚未稳定,冻结等待
|
||
// 稳定 300ms:查询 DWM 真实帧边界,决定是否重建
|
||
RECT frameRc = fullRc;
|
||
if (SUCCEEDED(DwmGetWindowAttribute(m_hTargetWnd, DWMWA_EXTENDED_FRAME_BOUNDS, &frameRc, sizeof(frameRc)))) {
|
||
m_ShadowLeft = frameRc.left - fullRc.left;
|
||
m_ShadowTop = frameRc.top - fullRc.top;
|
||
} else {
|
||
m_ShadowLeft = m_ShadowTop = 0;
|
||
}
|
||
if ((ULONG)(frameRc.right - frameRc.left) != m_ulFullWidth ||
|
||
(ULONG)(frameRc.bottom - frameRc.top) != m_ulFullHeight) {
|
||
Mprintf("CScreenSpy: 窗口尺寸变化 %dx%d -> %dx%d,触发重建\n",
|
||
m_ulFullWidth, m_ulFullHeight,
|
||
frameRc.right - frameRc.left, frameRc.bottom - frameRc.top);
|
||
m_bNeedRestart = true;
|
||
return false;
|
||
}
|
||
m_CachedWndW = w;
|
||
m_CachedWndH = h;
|
||
return true;
|
||
}
|
||
|
||
// 前景模式直接 BitBlt(零闪烁);指定窗口模式用 PrintWindow(处理遮挡)
|
||
void CScreenSpy::CaptureWindowContent(HDC hdcDest, HDC hdcSour, ULONG ulWidth, ULONG ulHeight)
|
||
{
|
||
if (m_bDynamicForeground) {
|
||
BitBlt(hdcDest, 0, 0, ulWidth, ulHeight, hdcSour, m_iScreenX, m_iScreenY, SRCCOPY);
|
||
} else {
|
||
HDC hTmp = m_data.GetWindowDC();
|
||
HBITMAP hOld = (HBITMAP)SelectObject(hTmp, m_data.GetWindowBmp());
|
||
if (PrintWindow(m_hTargetWnd, hTmp, PW_RENDERFULLCONTENT))
|
||
BitBlt(hdcDest, 0, 0, ulWidth, ulHeight, hTmp, m_ShadowLeft, m_ShadowTop, SRCCOPY);
|
||
SelectObject(hTmp, hOld);
|
||
}
|
||
}
|