From 389de9d456adb128e8027946abae19864631a622 Mon Sep 17 00:00:00 2001 From: yuanyuanxiang <962914132@qq.com> Date: Fri, 30 Oct 2026 21:57:39 +0100 Subject: [PATCH] fix(client): fall back to GDI capture when DXGI duplication yields no frames Some environments never deliver a frame from IDXGIOutputDuplication::AcquireNextFrame: headless or virtual displays, Session 0 desktops, and drivers that do not push duplication notifications. It returns DXGI_ERROR_WAIT_TIMEOUT forever, even while the desktop is changing. Consequences: - The first screen could not be captured, so the server kept showing "Please wait..." because m_bIsFirst was never cleared. - Adding a GDI fallback for the first screen alone was not enough: the picture appeared but then froze, because ScanNextScreen still polled DXGI and always got nothing back, leaving the encoder without input. Fixes: - Add CaptureByGDI(), which grabs a frame with GetDC(NULL) + CreateDIBSection + BitBlt. GDI does not depend on desktop changes, which is why the plain GDI capture path always worked in these environments. - When the first screen cannot be obtained from DXGI, capture it with GDI and mark the whole session as GDI (m_bGDIFallback = true), releasing the duplication object. All later frames then go through GDI so the picture keeps updating. - Normal machines are unaffected: if DXGI delivers the probe frame or the first screen, the flag stays false and the GDI path is never entered. This also avoids mis-detecting a momentarily static desktop as a broken duplication, which a plain timeout counter would do. - Create the GDI offscreen DIB lazily and reuse it (m_hGdiMemDC, m_hGdiBmp, m_pGdiBits). The fallback is a per-frame path, so allocating and freeing about 15MB every frame would cause visible jitter. - Reuse m_BitmapInfor_Full for CreateDIBSection instead of building a second BITMAPINFO: it is already 32bpp BI_RGB with a positive height (bottom-up) and matches the surface size. - Guard the GDI buffer readiness check. If CreateCompatibleDC or SelectObject fails, drop the bitmap so the next call does not mistake a half-initialized state for a usable one; otherwise BitBlt would silently write nowhere and memcpy would read uninitialized memory. - Restore the previous bitmap before DeleteDC and check for HGDI_ERROR, otherwise DeleteObject on the still-selected bitmap fails and leaks it. The fallback matches the plain GDI capture path (full-size BitBlt plus ScaleBitmap), so a downgraded session performs the same as GDI mode. The only added cost versus DXGI is one BitBlt per frame. --- client/ScreenCapturerDXGI.h | 113 +++++++++++++++++++++++++++++++++--- 1 file changed, 105 insertions(+), 8 deletions(-) diff --git a/client/ScreenCapturerDXGI.h b/client/ScreenCapturerDXGI.h index d963e96..37aa509 100644 --- a/client/ScreenCapturerDXGI.h +++ b/client/ScreenCapturerDXGI.h @@ -44,6 +44,19 @@ private: // 因此探测完先留在手上(不 ReleaseFrame),由 ConsumeProbeFrame() 消费。 IDXGIResource* m_probeRes = nullptr; + // DXGI 在部分环境下完全拿不到帧(无头/虚拟显示、Session 0 桌面、驱动不推送 + // duplication 通知等),连首屏都要靠 GDI 兜底。这种情况下本会话直接降级为 + // GDI 抓屏,否则 ScanNextScreen 永远返回 nullptr,表现为「首屏有画面, + // 但之后切换窗口画面不再更新」。 + bool m_bGDIFallback = false; + + // GDI 兜底用的离屏缓冲。降级后是每帧都要走的持续路径,必须复用, + // 否则每帧 CreateDIBSection/DeleteObject 会反复分配十几 MB 造成抖动。 + HDC m_hGdiMemDC = nullptr; + HBITMAP m_hGdiBmp = nullptr; + HGDIOBJ m_hGdiOldBmp = nullptr; + void* m_pGdiBits = nullptr; + public: ScreenCapturerDXGI(BYTE algo, int gop = DEFAULT_GOP, BOOL all = FALSE, int level = LEVEL_H264_SOFT, RECT rc = {0}, BOOL switchScreen = TRUE) @@ -257,6 +270,21 @@ public: void CleanupDXGI() { + // GDI 兜底缓冲:先把旧位图选回 DC,再删 DC,最后删位图 + if (m_hGdiMemDC) { + // SelectObject 失败会返回 HGDI_ERROR,不能拿它去恢复,否则位图仍被 + // DC 选入,后面的 DeleteObject 会失败 + if (m_hGdiOldBmp && m_hGdiOldBmp != HGDI_ERROR) + SelectObject(m_hGdiMemDC, m_hGdiOldBmp); + DeleteDC(m_hGdiMemDC); + m_hGdiMemDC = nullptr; + m_hGdiOldBmp = nullptr; + } + if (m_hGdiBmp) { + DeleteObject(m_hGdiBmp); + m_hGdiBmp = nullptr; + m_pGdiBits = nullptr; + } // 探测帧若还挂着(例如 InitDXGI 中途失败),必须归还,否则 duplication 无法释放 if (m_probeRes) { if (deskDupl) deskDupl->ReleaseFrame(); @@ -306,17 +334,24 @@ public: Mprintf("[DXGI] first screen slow: %ums attempts=%d ok=%d\n", elapsed, attempts, ret == 0); } - if (ret == kNoNewFrame) { - // 静态桌面:DXGI 无新帧,m_FirstBuffer 里仍是上一帧,直接复用 - if (!m_bFirstValid) { + if (ret != 0) { + // DXGI 抓帧失败(无新帧 / 尺寸异常 / Map 失败等)。首屏不能放弃:一旦 + // 返回 nullptr,服务端 m_bIsFirst 就再无机会解锁(ScreenManager 的 + // m_SendFirst 已置 TRUE 不会重试),只能等桌面变化,表现为一直"请等待"。 + if (CaptureByGDI(m_FirstBuffer, 1)) { + // duplication 连首帧都产不出来,本会话后续帧也必然是空的。 + // 直接降级为 GDI 抓屏,保证画面能持续更新(否则只有首屏是活的)。 + m_bGDIFallback = true; + if (deskDupl) { + deskDupl->Release(); + deskDupl = nullptr; + } + Mprintf("[DXGI] no frame from duplication (ret=%d), session falls back to GDI\n", ret); + ret = 0; + } else { *ulFirstScreenLength = 0; return nullptr; } - ret = 0; - } - if (ret) { - *ulFirstScreenLength = 0; - return nullptr; } // reservedBytes=1,实际位图从 m_FirstBuffer+1 开始; // 服务端也是从偏移 1 开始拷贝,写回时必须保持同样的偏移 @@ -400,6 +435,59 @@ private: return SUCCEEDED(hr) && deskDupl; } + // 惰性创建 GDI 离屏 DIB(尺寸与 DXGI 表面一致,32bpp bottom-up,与发送格式相同) + bool EnsureGDIBuffer() + { + if (m_hGdiMemDC && m_hGdiBmp && m_pGdiBits) return true; + // m_BitmapInfor_Full 已是 32bpp BI_RGB、biHeight 为正(bottom-up)、 + // 尺寸等于 m_ulFullWidth/Height,直接复用,不必另建一份 BITMAPINFO + if (!m_BitmapInfor_Full || m_ulFullWidth == 0 || m_ulFullHeight == 0) return false; + + HDC hScreen = GetDC(NULL); + if (!hScreen) return false; + + if (!m_hGdiMemDC) m_hGdiMemDC = CreateCompatibleDC(hScreen); + if (!m_hGdiBmp) { + m_hGdiBmp = CreateDIBSection(hScreen, m_BitmapInfor_Full, DIB_RGB_COLORS, + &m_pGdiBits, NULL, 0); + } + ReleaseDC(NULL, hScreen); + + if (!m_hGdiMemDC || !m_hGdiBmp || !m_pGdiBits) return false; + + HGDIOBJ old = SelectObject(m_hGdiMemDC, m_hGdiBmp); + if (!old || old == HGDI_ERROR) { + // 选入失败:销毁位图,否则下次会被上面的「已就绪」判断误认为可用, + // 而实际 BitBlt 写不到 m_pGdiBits,抓出来是未初始化的内存 + DeleteObject(m_hGdiBmp); + m_hGdiBmp = nullptr; + m_pGdiBits = nullptr; + return false; + } + m_hGdiOldBmp = old; + return true; + } + + // DXGI 的 AcquireNextFrame 只在桌面有新内容呈现时才返回。在无头/虚拟显示环境, + // 或驱动在 DuplicateOutput 后不推送初始帧时,它可能永远超时。此时首屏发不出去, + // 服务端 m_bIsFirst 永不解锁,表现为远程桌面一直停在"请等待......"。 + // GDI BitBlt 不依赖桌面变化,这里用它兜底抓一帧。 + bool CaptureByGDI(LPBYTE buffer, int reservedBytes) + { + if (!EnsureGDIBuffer()) return false; + + HDC hScreen = GetDC(NULL); + if (!hScreen) return false; + BOOL blt = BitBlt(m_hGdiMemDC, 0, 0, (int)m_ulFullWidth, (int)m_ulFullHeight, + hScreen, m_iScreenX, m_iScreenY, SRCCOPY); + ReleaseDC(NULL, hScreen); + if (!blt) return false; + + memcpy(buffer + reservedBytes, m_pGdiBits, + (size_t)m_ulFullWidth * m_ulFullHeight * 4); + return true; + } + // 把 DXGI 表面(top-down,srcW×srcH,BGRA)按 rotation 摆正后写入目标 // (bottom-up DIB,m_ulFullWidth×m_ulFullHeight)。分块处理保证缓存友好。 void BlitRotated(BYTE* dst, const BYTE* src, int srcPitch, @@ -484,6 +572,15 @@ private: int CaptureFrame(LPBYTE buffer, ULONG* frameSize, int reservedBytes, bool* valid) { + // 本会话已降级为 GDI(首屏就是靠 GDI 兜底才成功的,说明 duplication 在这个 + // 环境产不出帧)。继续轮询 DXGI 只会一直返回「无新帧」,画面就停住不动。 + if (m_bGDIFallback) { + if (!CaptureByGDI(buffer, reservedBytes)) return -1; + *frameSize = m_ulFullWidth * m_ulFullHeight * 4; + if (valid) *valid = true; + return 0; + } + if (!deskDupl) { if (!ReinitDuplication()) return -10; }