fix(client): use GDI capture when the session has no interactive desktop
A host whose DXGI initialisation hangs never opens the remote desktop window at all. WorkThreadProc runs InitScreenSpy() and SendBitMapInfo() back to back, so a capturer that never finishes constructing means TOKEN_BITMAPINFO is never sent, and the master - which only opens the window on that token - waits forever. On the affected Server 2022 box the screen thread stopped right after logging its type and algorithm, printing neither the DXGI init summary nor the existing "DXGI SPY init failed" fallback, so it was stuck inside the capturer constructor; a healthy machine on the same build reaches the summary in under a second. Six clicks left six stuck threads, each holding a D3D device. DXGI desktop duplication needs a display to duplicate. With no interactive desktop it can return no frames at best, and on some drivers it never returns from the initialisation calls at all. BitBlt has no such dependency and keeps working, which is why switching the screen type to GDI made the same host connect. Detect that up front and pick GDI instead of relying on a hang to be survivable: - IsWorkstationLocked() - the input desktop is Winlogon. Moved out of CKernelManager into Manager.cpp so the capturer can use it too; the manager keeps its private wrapper for the "Locked:"/"Inactive:" activity string. - IsCurrentSessionDisconnected() - the process's own session is WTSDisconnected. That is the state the reported host was actually in: its client runs in session 2, which was disconnected, and the desktop only came back after the master's "RDP session reset" ran tscon. Session 0 has no disconnected state and is left alone. Either condition means the same thing for capture, so both route to the plain GDI capturer. An active session keeps DXGI untouched, so hosts that work today are unaffected; the new log line names which condition fired. Co-Authored-By: deepseek-flash
This commit is contained in:
1 parent
6a3d536d38
commit
96f1bacd43
4 files changed
+55
-12
No files matched your search
+1
-11
@@ -125,17 +125,7 @@ private:
|
||||
|
||||
bool IsWorkstationLocked()
|
||||
{
|
||||
HDESK hInput = OpenInputDesktop(0, FALSE, GENERIC_READ);
|
||||
// 如果无法打开桌面,可能是因为桌面已经切换到 Winlogon
|
||||
if (!hInput) return true;
|
||||
char name[256] = {0};
|
||||
DWORD needed;
|
||||
bool isLocked = false;
|
||||
if (GetUserObjectInformationA(hInput, UOI_NAME, name, sizeof(name), &needed)) {
|
||||
isLocked = (_stricmp(name, "Winlogon") == 0);
|
||||
}
|
||||
CloseDesktop(hInput);
|
||||
return isLocked;
|
||||
return ::IsWorkstationLocked();
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -156,6 +156,22 @@ HDESK OpenActiveDesktop(ACCESS_MASK dwDesiredAccess)
|
||||
return hInputDesktop;
|
||||
}
|
||||
|
||||
bool IsWorkstationLocked()
|
||||
{
|
||||
HDESK hInput = OpenInputDesktop(0, FALSE, GENERIC_READ);
|
||||
// 打不开输入桌面,通常就是已经切到 Winlogon(锁屏)
|
||||
if (!hInput) return true;
|
||||
|
||||
char name[256] = { 0 };
|
||||
DWORD needed = 0;
|
||||
bool isLocked = false;
|
||||
if (GetUserObjectInformationA(hInput, UOI_NAME, name, sizeof(name), &needed)) {
|
||||
isLocked = (_stricmp(name, "Winlogon") == 0);
|
||||
}
|
||||
CloseDesktop(hInput);
|
||||
return isLocked;
|
||||
}
|
||||
|
||||
// 返回新桌面句柄,如果没有变化返回NULL
|
||||
HDESK IsDesktopChanged(HDESK currentDesk, DWORD accessRights)
|
||||
{
|
||||
|
||||
@@ -15,6 +15,10 @@
|
||||
|
||||
HDESK OpenActiveDesktop(ACCESS_MASK dwDesiredAccess = 0);
|
||||
|
||||
// 工作站是否已锁屏(输入桌面切到 Winlogon)。锁屏时 DXGI 桌面复制在部分环境会卡在
|
||||
// 初始化里不返回,抓屏初始化需要据此提前走 GDI。
|
||||
bool IsWorkstationLocked();
|
||||
|
||||
HDESK IsDesktopChanged(HDESK currentDesk, DWORD accessRights);
|
||||
|
||||
bool SwitchToDesktopIfChanged(HDESK& currentDesk, DWORD accessRights);
|
||||
|
||||
@@ -467,6 +467,26 @@ static BOOL RestoreConsoleSession()
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
// 进程所在会话是否已断开(RDP 断开后没有显示输出)。
|
||||
// 这种会话里 DXGI 桌面复制拿不到帧,部分驱动上连初始化都不返回,必须走 GDI。
|
||||
// Session 0 是服务会话,没有「断开」这个概念,直接返回 FALSE。
|
||||
static BOOL IsCurrentSessionDisconnected()
|
||||
{
|
||||
DWORD sessionId = 0;
|
||||
if (!ProcessIdToSessionId(GetCurrentProcessId(), &sessionId) || sessionId == 0)
|
||||
return FALSE;
|
||||
|
||||
WTS_CONNECTSTATE_CLASS* pState = NULL;
|
||||
DWORD bytes = 0;
|
||||
if (!WTSQuerySessionInformationA(WTS_CURRENT_SERVER_HANDLE, sessionId, WTSConnectState,
|
||||
(LPSTR*)&pState, &bytes) || !pState) {
|
||||
return FALSE;
|
||||
}
|
||||
const BOOL disconnected = (*pState == WTSDisconnected);
|
||||
WTSFreeMemory(pState);
|
||||
return disconnected;
|
||||
}
|
||||
|
||||
void CScreenManager::InitScreenSpy()
|
||||
{
|
||||
int DXGI = USING_GDI;
|
||||
@@ -551,7 +571,20 @@ void CScreenManager::InitScreenSpy()
|
||||
}
|
||||
}
|
||||
SAFE_DELETE(m_ScreenSpyObject);
|
||||
if ((USING_DXGI == DXGI && IsWindows8orHigher())) {
|
||||
// 锁屏(输入桌面切到 Winlogon)或会话已断开时,当前会话没有可用的交互桌面:
|
||||
// DXGI 桌面复制拿不到帧,部分环境(无 GPU 的 Server、断开/锁定的 RDP 会话)
|
||||
// 甚至会在初始化里卡死不返回 —— 那样 InitScreenSpy 走不完,WorkThreadProc 里
|
||||
// 紧随其后的 SendBitMapInfo() 永远发不出去,主控端一直等不到 TOKEN_BITMAPINFO,
|
||||
// 连远程桌面窗口都不会弹。GDI 的 BitBlt 不依赖桌面变化,这些情况下照样能抓,
|
||||
// 所以直接走 GDI。
|
||||
const BOOL locked = IsWorkstationLocked();
|
||||
const BOOL disconnected = IsCurrentSessionDisconnected();
|
||||
const bool noDesktop = (USING_DXGI == DXGI && (locked || disconnected));
|
||||
if (noDesktop) {
|
||||
Mprintf("CScreenManager: no interactive desktop (locked=%d, disconnected=%d), use GDI instead of DXGI\n",
|
||||
(int)locked, (int)disconnected);
|
||||
}
|
||||
if ((USING_DXGI == DXGI && IsWindows8orHigher() && !noDesktop)) {
|
||||
m_isGDI = FALSE;
|
||||
auto s = new ScreenCapturerDXGI(algo, DEFAULT_GOP, all, m_ScreenSettings.EncodeLevel, rect, switchScreen);
|
||||
if (s->IsInitSucceed()) {
|
||||
|
||||
Reference in new issue
Block a user