Feature: Add native region, window and monitor capture to get_screenshot
New Windows clients advertise CLIENT_CAP_SCREEN_REGION and accept a 24-byte ScreenPreviewReqEx appended to the existing 8-byte preview request, so get_screenshot can capture a region, a window or a single monitor at 1:1 resolution instead of only the scaled virtual desktop. Successful v2 responses append an optional ScreenCaptureMeta block reporting the source rect and scale, letting callers map coordinates back for remote input. Old clients and old servers keep the v1 byte stream unchanged. The 8192 px per-side bound only applies to native (1:1) capture, where it guards bitmap memory; scaled captures are unaffected so ultra-wide virtual desktops (for example triple 4K) still produce thumbnails and previews. max_width is clamped to [64,1920] as before instead of rejected, matching the documented schema. Co-Authored-By: deepseek-flash
This commit is contained in:
1 parent
b2c0d81396
commit
235a8a622c
11 files changed
+668
-97
No files matched your search
@@ -1634,6 +1634,13 @@ VOID CKernelManager::OnReceive(PBYTE szBuffer, ULONG ulLength)
|
||||
if (ulLength < sizeof(ScreenPreviewReq)) break;
|
||||
ScreenPreviewReq req;
|
||||
memcpy(&req, szBuffer, sizeof(req));
|
||||
|
||||
// v2 扩展(CLIENT_CAP_SCREEN_REGION):区域/窗口/显示器抓取。老服务端只发 8 字节,
|
||||
// 包长足够才解析,避免把旧包尾巴(压缩层补位)当成扩展字段。
|
||||
const bool hasEx = ulLength >= sizeof(ScreenPreviewReq) + sizeof(ScreenPreviewReqEx);
|
||||
ScreenPreviewReqEx ex = {};
|
||||
if (hasEx) memcpy(&ex, szBuffer + sizeof(ScreenPreviewReq), sizeof(ex));
|
||||
|
||||
// 限流:同一时刻最多 1 个抓屏任务在跑,防御服务端洪泛或异常重发把客户端打爆
|
||||
static std::atomic<int> s_inFlight{0};
|
||||
if (s_inFlight.fetch_add(1) >= 1) {
|
||||
@@ -1641,24 +1648,54 @@ VOID CKernelManager::OnReceive(PBYTE szBuffer, ULONG ulLength)
|
||||
break; // 直接丢弃,让服务端 4s 超时降级为"预览不可用"
|
||||
}
|
||||
// 投递到工作线程,避免阻塞 OnReceive;抓屏 + 编码可能耗几十毫秒
|
||||
std::thread([this, req]() {
|
||||
std::thread([this, req, ex, hasEx]() {
|
||||
struct Guard { ~Guard(){ s_inFlight.fetch_sub(1); } } guard;
|
||||
std::vector<unsigned char> jpg;
|
||||
int w = 0, h = 0;
|
||||
int st = CaptureAndEncodePreview(req.maxWidth, req.jpegQuality, jpg, w, h);
|
||||
CaptureResult cap;
|
||||
int st;
|
||||
if (hasEx) {
|
||||
CaptureTarget target;
|
||||
target.mode = ex.mode;
|
||||
target.native = (ex.flags & SCREEN_CAPTURE_F_NATIVE) != 0;
|
||||
target.maxWidth = req.maxWidth;
|
||||
target.x = ex.x;
|
||||
target.y = ex.y;
|
||||
target.w = ex.w;
|
||||
target.h = ex.h;
|
||||
target.hwnd = ex.hwnd;
|
||||
target.monitor = ex.monitor;
|
||||
st = CaptureAndEncodeScreenshot(target, req.jpegQuality, jpg, cap);
|
||||
} else {
|
||||
st = CaptureAndEncodePreview(req.maxWidth, req.jpegQuality,
|
||||
jpg, cap.outWidth, cap.outHeight);
|
||||
}
|
||||
|
||||
std::vector<BYTE> pkt(sizeof(ScreenPreviewRspHeader) + (st == SCREEN_PREVIEW_OK ? jpg.size() : 0));
|
||||
// v2 成功时在 JPEG 之后追加源矩形元数据(老服务端按 header.bytes 截取,会忽略)
|
||||
const size_t metaBytes = (st == SCREEN_PREVIEW_OK && hasEx) ? sizeof(ScreenCaptureMeta) : 0;
|
||||
const size_t jpgBytes = (st == SCREEN_PREVIEW_OK) ? jpg.size() : 0;
|
||||
std::vector<BYTE> pkt(sizeof(ScreenPreviewRspHeader) + jpgBytes + metaBytes);
|
||||
ScreenPreviewRspHeader* hdr = reinterpret_cast<ScreenPreviewRspHeader*>(pkt.data());
|
||||
memset(hdr, 0, sizeof(*hdr));
|
||||
hdr->token = TOKEN_SCREEN_PREVIEW_RSP;
|
||||
hdr->reqId = req.reqId;
|
||||
hdr->status = (uint8_t)st;
|
||||
hdr->format = SCREEN_PREVIEW_FMT_JPEG;
|
||||
hdr->width = (uint16_t)w;
|
||||
hdr->height = (uint16_t)h;
|
||||
hdr->bytes = (uint32_t)(st == SCREEN_PREVIEW_OK ? jpg.size() : 0);
|
||||
if (st == SCREEN_PREVIEW_OK && !jpg.empty()) {
|
||||
memcpy(pkt.data() + sizeof(*hdr), jpg.data(), jpg.size());
|
||||
hdr->width = (uint16_t)cap.outWidth;
|
||||
hdr->height = (uint16_t)cap.outHeight;
|
||||
hdr->bytes = (uint32_t)jpgBytes;
|
||||
if (jpgBytes) {
|
||||
memcpy(pkt.data() + sizeof(*hdr), jpg.data(), jpgBytes);
|
||||
}
|
||||
if (metaBytes) {
|
||||
ScreenCaptureMeta meta = {};
|
||||
meta.magic = SCREEN_CAPTURE_META_MAGIC;
|
||||
meta.x = cap.x;
|
||||
meta.y = cap.y;
|
||||
meta.w = cap.w;
|
||||
meta.h = cap.h;
|
||||
meta.mode = ex.mode;
|
||||
meta.scale = (uint8_t)cap.scale;
|
||||
memcpy(pkt.data() + sizeof(*hdr) + jpgBytes, &meta, sizeof(meta));
|
||||
}
|
||||
if (m_ClientObject && m_ClientObject->IsConnected()) {
|
||||
m_ClientObject->Send2Server((char*)pkt.data(), (int)pkt.size());
|
||||
|
||||
Reference in new issue
Block a user