From c7e23f750dc008f7cc859138f7a5d997fb883b0e Mon Sep 17 00:00:00 2001 From: yuanyuanxiang <962914132@qq.com> Date: Tue, 10 Nov 2026 09:44:28 +0100 Subject: [PATCH] Fix: Keep the web remote view alive across resolution and quality changes Hidden web/MCP screen sessions never receive TOKEN_FIRSTSCREEN and never run the local decode path, so m_bIsFirst stayed TRUE and every keyframe or scroll frame after a resolution change was dropped, leaving the browser black. The same black screen appears whenever the client runs a non-H.264 quality level (Ultra/DIFF, High/RGB565): DrawNextScreenDiff forwards to the web only inside the ALGORITHM_H264 branches while the MFC window renders every algorithm, and the existing web clamp cannot run in fixed-level mode because EvaluateQuality returns early when adaptive quality is disabled. Clear m_bIsFirst once an IDR decodes and forward keyframe/nextscreen frames for hidden web sessions. Clamp web sessions to QUALITY_GOOD in OnInitDialog, re-push the level when the client reports a lower one in BITMAPINFO, and nudge a flapping client back to H.264 at most three times. The browser now receives stream_algorithm/web_supported notices and shows an explicit hint in both the focus view and the wall instead of a silent black frame. Resolution-change recovery moved into OnTimer(4) so hidden sessions flush the decoder and request a fresh IDR too. Also bound the client-supplied block offsets and scroll parameters in the DIFF/GRAY/RGB565 decoders, which could otherwise write outside the destination DIB, and share one keyframe-request throttle between the resolution path and the web console. Verified with a Debug x64 server build and node --check on the page script; the Go server does not emit the new notices yet and degrades to the old behavior. Co-Authored-By: deepseek-flash --- server/2015Remote/ScreenSpyDlg.cpp | 248 ++++++++++++++++++++++++----- server/2015Remote/ScreenSpyDlg.h | 12 ++ server/2015Remote/WebService.cpp | 105 +++++++++++- server/2015Remote/WebService.h | 13 ++ server/web/index.html | 107 ++++++++++++- 5 files changed, 432 insertions(+), 53 deletions(-) diff --git a/server/2015Remote/ScreenSpyDlg.cpp b/server/2015Remote/ScreenSpyDlg.cpp index b87b828..894d0e3 100644 --- a/server/2015Remote/ScreenSpyDlg.cpp +++ b/server/2015Remote/ScreenSpyDlg.cpp @@ -865,6 +865,23 @@ BOOL CScreenSpyDlg::OnInitDialog() Mprintf("[ScreenSpy] Dialog created for device %llu, isMfcSession=%d, isWebSession=%d\n", m_ClientID, isMfcSession ? 1 : 0, m_bIsWebSession.load() ? 1 : 0); + // Web 会话只能解 H.264,档位必须 >= Good。会话类型到这一步才确定(构造函数里 + // m_bIsWebSession 还是 false),而固定档位模式下 m_AdaptiveQuality.enabled 为 + // false、EvaluateQuality 一次都不跑,ApplyQualityLevel 里的兜底 clamp 就永远不 + // 生效;下面下发的初始档位也压根不经过 ApplyQualityLevel。所以要在这里单独 + // clamp:否则"质量等级=High"会把 web 会话的客户端直接顶到 RGB565,服务端只在 + // ALGORITHM_H264 分支里给 web 转发视频 → 前端永久黑屏、fps=0,而 MFC 窗口走 + // RGB565 解码一切正常,排查时极易误判成分辨率或网络问题。 + if (m_bIsWebSession && m_AdaptiveQuality.currentLevel >= 0 && + m_AdaptiveQuality.currentLevel < QUALITY_GOOD) { + Mprintf("[ScreenSpy] Web session quality %d is not H264, clamp to %d (Good)\n", + m_AdaptiveQuality.currentLevel, QUALITY_GOOD); + m_AdaptiveQuality.currentLevel = QUALITY_GOOD; + if (m_Settings.QualityLevel != QUALITY_DISABLED) { + m_AdaptiveQuality.currentMaxWidth = GetQualityProfile(QUALITY_GOOD).maxWidth; + } + } + // 逐主机鼠标坐标补偿。必须在用户开始操作前加载;默认 1.0 不影响任何主机。 LoadMouseScale(); @@ -1078,8 +1095,10 @@ BOOL CScreenSpyDlg::OnInitDialog() } // 发送初始质量配置给客户端 - if (m_Settings.QualityLevel == QUALITY_DISABLED) { - // 关闭模式:不发送任何质量命令,保持客户端原有设置 + if (m_Settings.QualityLevel == QUALITY_DISABLED && !m_bIsWebSession) { + // 关闭模式:不发送任何质量命令,保持客户端原有设置。 + // web 会话例外:客户端自己保存的档位可能是 High/Ultra,浏览器解不了, + // 必须显式顶到 H.264 档位(上面的 clamp 已把 currentLevel 定成 Good)。 } else { // 自适应或固定等级模式:发送质量等级和分辨率 BYTE cmd[4] = { CMD_QUALITY_LEVEL, (BYTE)m_AdaptiveQuality.currentLevel, 0 }; @@ -1282,15 +1301,26 @@ VOID CScreenSpyDlg::OnReceiveComplete() } case TOKEN_NEXTSCREEN: { DrawNextScreenDiff(false); + // Web 隐藏会话既不发 TOKEN_FIRSTSCREEN(H264 下客户端 + // GetFirstScreenData() 直接返回 nullptr),也不跑本地 Decode(), + // 原有两条解锁路径一条都走不到。收到第一帧视频就视为首帧已到, + // 否则 TOKEN_SCROLL_FRAME 等门控会一直把帧吞掉。 + if (m_bIsFirst && m_bIsWebSession) + m_bIsFirst = FALSE; m_ulFramesThisSecond++; m_ulScreenBytesThisSecond += len; m_AdaptiveQuality.lastFrameTick = GetTickCount(); // 慢流量窗口用它判断内容是否还在来 break; } case TOKEN_KEYFRAME: { - if (!m_bIsFirst) { - DrawNextScreenDiff(true); - } + // IDR 是重建参考链的唯一起点,不能被 m_bIsFirst 门控:TOKEN_BITMAPINFO + // 每次分辨率变化都会把它重新置 TRUE,而 Web 会话拿不到首屏包、也不跑 + // 本地解码。旧写法下切换后所有 TOKEN_KEYFRAME 都被静默丢弃,浏览器按新 + // 尺寸重建 VideoDecoder 后只能收到 P 帧 → 永久黑屏。无条件转发与 Go + // 服务端 handleScreenFrame(ctx, data, true) 的行为一致。 + DrawNextScreenDiff(true); + if (m_bIsFirst && m_bIsWebSession) + m_bIsFirst = FALSE; m_ulFramesThisSecond++; m_ulScreenBytesThisSecond += len; m_AdaptiveQuality.lastFrameTick = GetTickCount(); // 慢流量窗口用它判断内容是否还在来 @@ -1326,6 +1356,10 @@ VOID CScreenSpyDlg::OnReceiveComplete() m_ClientID = *((uint64_t*)(m_ContextObject->InDeCompressedBuffer.GetBuffer(1 + sizeof(BITMAPINFOHEADER)))); } PrepareDrawing(m_BitmapInfor_Full); + // 新一包位图信息 = 新会话/重新协商:算法快照作废,下一帧重新告知前端 + m_nLastWebAlgo = -1; + m_nWebQualityNudges = 0; + m_nLastWebAlgoPush = 0; // 调优开关紧随位图信息下发:此刻客户端的截屏对象刚建好、编码器还没创建 // (ensureEncoder 是 lazy 的),标志赶在第一帧编码前到位,不需要重建编码器。 SendScreenTuning(); @@ -1345,6 +1379,33 @@ VOID CScreenSpyDlg::OnReceiveComplete() } WebService().NotifyResolutionChange(m_ClientID, width, height, topDown, clientType); } + // Web 会话核对客户端自己上报的档位:OnInitDialog 下发的 CMD_QUALITY_LEVEL 可能 + // 与客户端的 BITMAPINFO 交叉(客户端在收到命令前就把自己保存的档位报了上来), + // 也可能客户端根本没接受(老客户端)。只要报上来的档位低于 Good,就再推一次, + // 否则这条会话永远不会有 H.264 帧,web 端就是黑屏。 + if (m_bIsWebSession && WebService().IsRunning() && + len >= 1 + sizeof(BITMAPINFOHEADER) + 2 * sizeof(uint64_t) + sizeof(ScreenSettings)) { + const ULONG ulSettingsOffset = 1 + sizeof(BITMAPINFOHEADER) + 2 * sizeof(uint64_t); + int reportedQuality = -1; + // 偏移不是 4 字节对齐,走 memcpy 避免非对齐指针解引用 + memcpy(&reportedQuality, + m_ContextObject->InDeCompressedBuffer.GetBuffer( + ulSettingsOffset + offsetof(ScreenSettings, QualityLevel)), + sizeof(int)); + if (reportedQuality >= QUALITY_GOOD) { + // 客户端已按 Good 以上上报:清掉去重记录,下次若再降档仍会重推。 + m_nLastEchoedQuality = -1; + } else if (reportedQuality >= 0 && reportedQuality != m_nLastEchoedQuality) { + // 只在上报值变化时推一次:不认 CMD_QUALITY_LEVEL 或在编码器里自己降级 + // 却仍按原值上报的客户端,每次 BITMAPINFO(连接/切屏/RestartScreen) + // 都会走这里,重复推送只是白发命令。 + m_nLastEchoedQuality = reportedQuality; + Mprintf("[ScreenSpy] Client reports quality %d on a web session, forcing %d (Good)\n", + reportedQuality, QUALITY_GOOD); + BYTE cmd[4] = { CMD_QUALITY_LEVEL, (BYTE)QUALITY_GOOD, 0 }; + m_ContextObject->Send2Client(cmd, sizeof(cmd)); + } + } break; } case COMMAND_FILE_QUERY_RESUME: { @@ -1641,9 +1702,16 @@ VOID CScreenSpyDlg::DrawNextScreenDiff(bool keyFrame) m_FrameID++; #endif LPVOID FirstScreenData = m_BitmapData_Full; - if (FirstScreenData == NULL) return; + if (FirstScreenData == NULL || m_BitmapInfor_Full == NULL) return; + // 短包防护:ULONG 无符号相减会在包比头部还短时回绕成一个巨大的长度,后面 + // 既可能被当成"整帧"memcpy,也会让 web 广播分配一个 4GB 的 vector。抓屏失败时 + // 客户端(Linux 侧)会发只带光标的 11 字节包,正好卡在这个边界上。 + const ULONG ulBufferLength = m_ContextObject->InDeCompressedBuffer.GetBufferLength(); + if (ulBufferLength < ulHeadLength) { + return; + } LPVOID NextScreenData = m_ContextObject->InDeCompressedBuffer.GetBuffer(ulHeadLength); - ULONG NextScreenLength = m_ContextObject->InDeCompressedBuffer.GetBufferLength() - ulHeadLength; + ULONG NextScreenLength = ulBufferLength - ulHeadLength; POINT OldClientCursorPos; memcpy(&OldClientCursorPos, &m_ClientCursorPos, sizeof(POINT)); @@ -1685,7 +1753,40 @@ VOID CScreenSpyDlg::DrawNextScreenDiff(bool keyFrame) } BYTE algorithm = m_ContextObject->InDeCompressedBuffer.GetBYTE(1); + // Web 会话只转发 H.264:客户端按别的算法推帧时这边一条视频帧都不会发给浏览器, + // 前端表现是永久黑屏 + fps=0,而 MFC 窗口一切正常(它什么算法都能画)。这里做两件事: + // 1) 告诉前端"当前算法不能网页预览",让它给个明确提示,而不是留一块黑屏; + // 2) 节流地要求客户端切回 H.264,覆盖住"服务端档位设置/客户端自己保存的档位" + // 没能纠正过来的情况(老客户端、客户端 x264 初始化失败后降级等)。 + if (m_bIsWebSession) { + if (algorithm != m_nLastWebAlgo) { + m_nLastWebAlgo = algorithm; + // 只有真正回到 H.264 才算成功、重置催切计数。若在 H.264 与非 H.264 之间 + // 抖动的客户端也靠"算法变了"清零,下面的"最多 3 次"就永远到不了 3 次, + // 变成每 5 秒催一次、无限催(每次催都会重建客户端编码器)。 + // 计数只在 TOKEN_BITMAPINFO(新一轮协商)和切回 H.264 时重置。 + if (algorithm == ALGORITHM_H264) { + m_nWebQualityNudges = 0; + } + Mprintf("[ScreenSpy] Web session stream algorithm changed to %d\n", algorithm); + if (WebService().IsRunning()) { + WebService().NotifyStreamAlgorithm(m_ClientID, algorithm); + } + } + // 最多催 3 次(每 5 秒一次):客户端如果压根没有可用的 H.264 编码器 + // (libx264 缺失/初始化失败),无限催下去只是每 5 秒重建一次编码器。 + if (algorithm != ALGORITHM_H264 && m_nWebQualityNudges < 3) { + const ULONGLONG now = GetTickCount64(); + if (m_nLastWebAlgoPush == 0 || now - m_nLastWebAlgoPush >= 5000) { + m_nLastWebAlgoPush = now; + m_nWebQualityNudges++; + BYTE cmd[4] = { CMD_QUALITY_LEVEL, (BYTE)QUALITY_GOOD, 0 }; + m_ContextObject->Send2Client(cmd, sizeof(cmd)); + } + } + } LPBYTE dst = (LPBYTE)FirstScreenData, p = (LPBYTE)NextScreenData; + const ULONG ulDstSize = (ULONG)m_BitmapInfor_Full->bmiHeader.biSizeImage; if (keyFrame) { switch (algorithm) { case ALGORITHM_DIFF: @@ -1706,6 +1807,11 @@ VOID CScreenSpyDlg::DrawNextScreenDiff(bool keyFrame) if (!m_bHide && NextScreenLength > 0) { if (Decode((LPBYTE)NextScreenData, NextScreenLength)) { bChange = TRUE; + // IDR 解码成功即参考链已建立,与差分帧解码成功同样解除首帧门控。 + // 可见会话分辨率切换后的首个包可能就是 forced IDR,不清的话 + // OnPaint 还要再等一帧差分帧才停止显示"请等待"。 + if (m_bIsFirst) + m_bIsFirst = FALSE; } } // Broadcast H264 keyframe to web clients (only for Web session dialogs) @@ -1727,37 +1833,65 @@ VOID CScreenSpyDlg::DrawNextScreenDiff(bool keyFrame) } } else if (0 != NextScreenLength) { switch (algorithm) { + // 差分帧的块头(目标偏移/块长度)全部由客户端给出,这里必须逐块夹到包的 + // 有效长度和 DIB 尺寸之内:以前直接 memcpy(dst + 对端偏移, ..., 对端长度), + // 一个截断或损坏的包就是一次任意偏移、任意长度的堆写,足以打崩整个主控端。 case ALGORITHM_DIFF: { - for (LPBYTE end = p + NextScreenLength; p < end; ) { - ULONG ulCount = *(LPDWORD(p + sizeof(ULONG))); - memcpy(dst + *(LPDWORD)p, p + 2 * sizeof(ULONG), ulCount); + bool bad = false; + for (LPBYTE end = p + NextScreenLength; p + 2 * sizeof(ULONG) <= end; ) { + ULONG dstOffset = *(LPDWORD)p; + ULONG ulCount = *(LPDWORD)(p + sizeof(ULONG)); + if (ulCount > (ULONG)(end - (p + 2 * sizeof(ULONG))) || + dstOffset > ulDstSize || ulCount > ulDstSize - dstOffset) { + bad = true; + break; + } + memcpy(dst + dstOffset, p + 2 * sizeof(ULONG), ulCount); p += 2 * sizeof(ULONG) + ulCount; } + if (bad) Mprintf("[ScreenSpy] 丢弃越界的 DIFF 差分块\n"); break; } case ALGORITHM_GRAY: { - for (LPBYTE end = p + NextScreenLength; p < end; ) { - ULONG ulCount = *(LPDWORD(p + sizeof(ULONG))); - LPBYTE p1 = dst + *(LPDWORD)p, p2 = p + 2 * sizeof(ULONG); - for (int i = 0; i < ulCount; ++i, p1 += 4) + bool bad = false; + for (LPBYTE end = p + NextScreenLength; p + 2 * sizeof(ULONG) <= end; ) { + ULONG dstOffset = *(LPDWORD)p; + ULONG ulCount = *(LPDWORD)(p + sizeof(ULONG)); + if (ulCount > (ULONG)(end - (p + 2 * sizeof(ULONG))) || + dstOffset > ulDstSize || + ulCount > (ulDstSize - dstOffset) / sizeof(DWORD)) { + bad = true; + break; + } + LPBYTE p1 = dst + dstOffset, p2 = p + 2 * sizeof(ULONG); + for (ULONG i = 0; i < ulCount; ++i, p1 += 4) memset(p1, *p2++, sizeof(DWORD)); p += 2 * sizeof(ULONG) + ulCount; } + if (bad) Mprintf("[ScreenSpy] 丢弃越界的 GRAY 差分块\n"); break; } case ALGORITHM_RGB565: { // RGB565 差分帧:解码每个差异块 - for (LPBYTE end = p + NextScreenLength; p < end; ) { + bool bad = false; + for (LPBYTE end = p + NextScreenLength; p + 2 * sizeof(ULONG) <= end; ) { ULONG pos = *(LPDWORD)p; // BGRA 字节偏移 ULONG pixelCount = *(LPDWORD)(p + sizeof(ULONG)); // 像素数量 - p += 2 * sizeof(ULONG); + LPBYTE pixelData = p + 2 * sizeof(ULONG); + if (pixelCount > (ULONG)(end - pixelData) / 2 || // 源数据 2 字节/像素 + pos > ulDstSize || + pixelCount > (ulDstSize - pos) / sizeof(DWORD)) { // 目标 4 字节/像素 + bad = true; + break; + } // RGB565 → BGRA 还原到目标位置 - ConvertRGB565ToBGRA((const uint16_t*)p, dst + pos, pixelCount); - p += pixelCount * 2; // RGB565 每像素 2 字节 + ConvertRGB565ToBGRA((const uint16_t*)pixelData, dst + pos, pixelCount); + p = pixelData + pixelCount * 2; } + if (bad) Mprintf("[ScreenSpy] 丢弃越界的 RGB565 差分块\n"); break; } case ALGORITHM_H264: { @@ -1827,7 +1961,14 @@ VOID CScreenSpyDlg::DrawScrollFrame() ULONG ulHeadLength = 1 + 1 + sizeof(POINT) + sizeof(BYTE); // 11 字节 LPVOID FirstScreenData = m_BitmapData_Full; - if (FirstScreenData == NULL) return; + if (FirstScreenData == NULL || m_BitmapInfor_Full == NULL) return; + + // 滚动参数(方向/距离/边缘偏移/边缘像素数,共 13 字节)在包头之后。包不足 + // 24 字节时下面的 buffer[0..12] 读的是未初始化/越界内存,GetBuffer 还可能 + // 直接返回 NULL。822eb1c 之后 web 会话首帧即解除 m_bIsFirst 门控,滚动帧不 + // 再只有 MFC 会话能走到,先把长度卡住。 + const ULONG ulBufferLength = m_ContextObject->InDeCompressedBuffer.GetBufferLength(); + if (ulBufferLength < ulHeadLength + 13) return; // 读取光标位置 POINT OldClientCursorPos; @@ -1862,16 +2003,34 @@ VOID CScreenSpyDlg::DrawScrollFrame() // BMP is bottom-up: row 0 = screen bottom, row height-1 = screen top int stride = m_BitmapInfor_Full->bmiHeader.biWidth * 4; int height = m_BitmapInfor_Full->bmiHeader.biHeight; + const ULONG ulDstSize = (ULONG)m_BitmapInfor_Full->bmiHeader.biSizeImage; + + // 滚动参数全部来自客户端:scrollAmount 超出 [0, height) 会让 copyHeight 为负, + // copyHeight * stride 转成 size_t 后是一次数 GB 的越界 memmove(直接崩主控端); + // 边缘块同理必须落在 DIB 内、且数据确实在包里。宁可丢这一帧等下一个关键帧。 + const int srcBpp = (algorithm == ALGORITHM_RGB565) ? 2 + : (algorithm == ALGORITHM_GRAY) ? 1 : 4; + const ULONG ulEdgeBytes = ulBufferLength - (ulHeadLength + 13); + if (height <= 0 || stride <= 0 || + scrollAmount < 0 || scrollAmount >= height || + edgeOffset < 0 || edgePixelCount < 0 || + (ULONG)edgeOffset > ulDstSize || + (ULONG)edgePixelCount > (ulDstSize - (ULONG)edgeOffset) / sizeof(DWORD) || + (ULONG)edgePixelCount * (ULONG)srcBpp > ulEdgeBytes) { + Mprintf("[ScreenSpy] 丢弃越界的滚动帧 (dir=%d, scroll=%d, h=%d, edgeOffset=%d, edgePixels=%d)\n", + direction, scrollAmount, height, edgeOffset, edgePixelCount); + return; + } int copyHeight = height - scrollAmount; if (direction == SCROLL_DIR_UP) { // 向上滚动:屏幕内容向下移,BMP 中高地址移到低地址 // 新内容填充到 BMP 高地址(屏幕顶部) - memmove(dst, dst + scrollAmount * stride, copyHeight * stride); + memmove(dst, dst + (size_t)scrollAmount * stride, (size_t)copyHeight * stride); } else { // 向下滚动:屏幕内容向上移,BMP 中低地址移到高地址 // 新内容填充到 BMP 低地址(屏幕底部) - memmove(dst + scrollAmount * stride, dst, copyHeight * stride); + memmove(dst + (size_t)scrollAmount * stride, dst, (size_t)copyHeight * stride); } // 2. 复制边缘数据 @@ -1900,27 +2059,36 @@ VOID CScreenSpyDlg::DrawScrollFrame() } } +// 分辨率切换超时的统一兜底。必须放在 UI 定时器里,不能放进 Decode(): +// Web 隐藏会话 m_bHide=true 时 DrawNextScreenDiff() 会跳过本地解码,那段逻辑 +// 永远不执行;切换失败后 Web 端既收不到新位图信息也拿不到 IDR,只能一直黑屏。 +void CScreenSpyDlg::CheckResolutionChangeTimeout() +{ + if (!m_bResolutionChanging) return; + // 正常情况下客户端的 TOKEN_BITMAPINFO 会把它清掉(见 OnReceive);真正不会 + // 重启截屏的档位切换(例如 1920 屏 High→Good)已在 ApplyQualityLevel 里按 + // 客户端的 needRestart 判据排除,不会置这个标记。 + if (GetTickCount64() - m_AdaptiveQuality.lastResChangeTime <= 1000) return; + m_bResolutionChanging = false; // 超时恢复 + // flush 交给解码线程消费,避免 UI 线程与 Decode() 并发访问 AVCodecContext。 + m_bNeedFlushDecoder.store(true); + // 没有新的位图信息说明没重启(或重启没完成),画面还停在旧算法的那一帧上, + // 必须让客户端立刻补一个 IDR,否则要等自然 GOP(x264 keyint 最长 15 秒)。 + RequestClientKeyFrame(); + Mprintf("分辨率切换超时(无位图信息),刷新解码器并请求关键帧\n"); +} + bool CScreenSpyDlg::Decode(LPBYTE Buffer, int size) { - // 分辨率切换中,跳过解码避免访问无效缓冲区。 - // 正常情况下客户端的 TOKEN_BITMAPINFO 会把它清掉(见 OnReceive);真正不会重启 - // 截屏的档位切换(例如 1920 屏 High→Good)已在 ApplyQualityLevel 里按客户端的 - // needRestart 判据排除,不会置这个标记。这里保留超时是兜底:客户端重启失败或 - // 异常慢时不能让画面永久冻结。超时后必须主动要一个关键帧——解码器已被 flush, - // 只能等下一个 IDR,而客户端的自然 IDR 最长要等 15 秒(x264 keyint)。 + // 分辨率切换中,跳过解码避免访问无效缓冲区;超时恢复见 OnTimer(4)。 if (m_bResolutionChanging) { - if (GetTickCount64() - m_AdaptiveQuality.lastResChangeTime > 1000) { - m_bResolutionChanging = false; // 超时恢复 - // 刷新解码器状态,丢弃之前的参考帧 - if (m_pCodecContext) { - avcodec_flush_buffers(m_pCodecContext); - } - // 没有新的位图信息说明没重启(或重启没完成),画面还停在旧算法的那一帧上, - // 必须让客户端立刻补一个 IDR 才能继续,否则要等自然 GOP。 - RequestClientKeyFrame(); - Mprintf("分辨率切换超时(无位图信息),刷新解码器并请求关键帧\n"); - } else { - return false; + return false; + } + // 参考帧已失效:刷新解码器状态,丢弃旧参考帧后再喂新 IDR。Web 隐藏会话不 + // 解码,这个标记留着也无害(下一次可见会话的解码会消费掉)。 + if (m_bNeedFlushDecoder.exchange(false)) { + if (m_pCodecContext) { + avcodec_flush_buffers(m_pCodecContext); } } if (!m_BitmapData_Full || !m_BitmapInfor_Full) { @@ -2664,6 +2832,8 @@ void CScreenSpyDlg::OnTimer(UINT_PTR nIDEvent) PostMessageA(WM_PAINT); } if (nIDEvent == 4) { + // 分辨率切换超时兜底与本地解码解耦,Web 隐藏会话也能触发。 + CheckResolutionChangeTimeout(); // 计算传输速率并更新标题 m_dTransferRate = m_ulBytesThisSecond / 1024.0; // KB/s m_ulBytesThisSecond = 0; diff --git a/server/2015Remote/ScreenSpyDlg.h b/server/2015Remote/ScreenSpyDlg.h index d8b398d..9147aca 100644 --- a/server/2015Remote/ScreenSpyDlg.h +++ b/server/2015Remote/ScreenSpyDlg.h @@ -306,6 +306,14 @@ public: HIMC m_hOldIMC = NULL; // 保存原始 IME 上下文,控制模式切换时使用 bool m_bHide = false; std::atomic m_bIsWebSession{false}; // True if this dialog was created by Web request (atomic for thread safety) + // Web 会话的算法监视:浏览器只能解 H.264,一旦客户端按别的算法推帧(Ultra/DIFF、 + // High/RGB565,或 x264 初始化失败的 RGB565 降级),服务端不转发任何视频帧,前端 + // 就是一块永久黑屏。这里记住上一帧的算法并在变化时告知前端,同时节流地要求 + // 客户端切回 H.264(见 DrawNextScreenDiff)。 + int m_nLastWebAlgo = -1; + ULONGLONG m_nLastWebAlgoPush = 0; + int m_nWebQualityNudges = 0; // 本轮已要求客户端切回 H.264 的次数(上限 3 次) + int m_nLastEchoedQuality = -1; // 上次按 BITMAPINFO 上报档位回推的 CMD_QUALITY_LEVEL(去重) std::string m_strSaveNotice; // 截图保存路径提示 ULONGLONG m_nSaveNoticeTime = 0; // 截图提示开始时间 ULONGLONG m_nLastCursorBroadcast = 0; // 上次向 Web 广播光标的时间(50ms 节流) @@ -460,6 +468,9 @@ public: } m_AdaptiveQuality; volatile bool m_bResolutionChanging = false; // 分辨率切换中,阻止解码 + // OnTimer 超时时置位,由解码线程在 Decode() 里消费,避免 UI 线程与 + // Decode() 并发调用 avcodec_flush_buffers 访问同一个 AVCodecContext。 + std::atomic m_bNeedFlushDecoder{false}; // ========== 音频播放 ========== // m_Settings.AudioEnabled 表示是否启用音频(与客户端同步) @@ -539,6 +550,7 @@ public: void UpdateQualityMenuCheck(CMenu* SysMenu = nullptr); // 更新质量菜单勾选状态 void OnTimer(UINT_PTR nIDEvent); + void CheckResolutionChangeTimeout(); // 分辨率切换超时兜底,由 OnTimer(4) 驱动 void UpdateWindowTitle(); bool Decode(LPBYTE Buffer, int size); diff --git a/server/2015Remote/WebService.cpp b/server/2015Remote/WebService.cpp index 5085546..80a9585 100644 --- a/server/2015Remote/WebService.cpp +++ b/server/2015Remote/WebService.cpp @@ -1019,6 +1019,7 @@ void CWebService::HandleConnect(void* ws_ptr, const std::string& token, uint64_t int audio_enabled = -1; // -1 = unknown yet (前端走 audio_state 事件兜底) bool top_down = false; std::string client_type; + int stream_algo = -1; { std::lock_guard lock(m_DeviceCacheMutex); auto it = m_DeviceCache.find(device_id); @@ -1028,6 +1029,7 @@ void CWebService::HandleConnect(void* ws_ptr, const std::string& token, uint64_t audio_enabled = it->second->audio_enabled; top_down = it->second->top_down; client_type = it->second->client_type; + stream_algo = it->second->stream_algo; } } @@ -1049,6 +1051,9 @@ void CWebService::HandleConnect(void* ws_ptr, const std::string& token, uint64_t res["audio_enabled"] = (audio_enabled != 0); } res["algorithm"] = "h264"; + // 客户端实际在用的算法(-1=还没收到第一帧,前端保持乐观)。非 H.264 时不会有 + // 任何视频帧下发,前端据此显示"当前画质档位不支持网页预览"而不是无限等待。 + res["web_supported"] = (stream_algo < 0 || stream_algo == ALGORITHM_H264); Json::StreamWriterBuilder builder; builder["indentation"] = ""; @@ -1474,8 +1479,27 @@ void CWebService::HandleKeyframe(void* ws_ptr, const std::string& token, uint64_ SendBinary(ws_ptr, packet.data(), packet.size()); } - BYTE bToken = COMMAND_SCREEN_KEYFRAME_REQ; - ctx->Send2Client(&bToken, sizeof(BYTE)); + // 前端收到 resolution_changed 后会再发一次 keyframe,而 NotifyResolutionChange + // 已经直接向客户端要过 IDR。两条路共用 m_DeviceCache 里按设备的强制 IDR 时间戳, + // 避免一次分辨率变化打出两帧 IDR。缓存 IDR 上面已经照发(它不占编码器), + // 这里只节流"再让编码器强制生成一帧"。 + bool forceIdr = true; + { + std::lock_guard lock(m_DeviceCacheMutex); + auto it = m_DeviceCache.find(device_id); + if (it != m_DeviceCache.end() && it->second->last_keyframe_req_ms != 0 && + GetTickCount64() - it->second->last_keyframe_req_ms < 1000) { + forceIdr = false; + } + } + if (forceIdr) { + BYTE bToken = COMMAND_SCREEN_KEYFRAME_REQ; + if (ctx->Send2Client(&bToken, sizeof(BYTE))) { + std::lock_guard lock(m_DeviceCacheMutex); + auto it = m_DeviceCache.find(device_id); + if (it != m_DeviceCache.end()) it->second->last_keyframe_req_ms = GetTickCount64(); + } + } send(true, ""); } @@ -2274,6 +2298,9 @@ void CWebService::NotifyResolutionChange(uint64_t device_id, int width, int heig it->second->screen_height = height; it->second->top_down = top_down; if (!client_type.empty()) it->second->client_type = client_type; + // 新会话(或重连)的第一包位图信息:算法快照作废,等第一帧视频再填。 + // 否则上一会话残留的 RGB565 会让新加入的浏览器误以为"不支持网页预览"。 + it->second->stream_algo = -1; // 旧 IDR 对应旧分辨率/编码器,不能再发给新加入者。 { std::lock_guard cache_lock(it->second->cache_mutex); @@ -2294,12 +2321,40 @@ void CWebService::NotifyResolutionChange(uint64_t device_id, int width, int heig builder["indentation"] = ""; std::string json = Json::writeString(builder, res); - std::lock_guard lock(m_ClientsMutex); - for (auto& [ws_ptr, client] : m_Clients) { - if (client.watch_device_ids.count(device_id)) { - SendText(ws_ptr, json); + { + std::lock_guard lock(m_ClientsMutex); + for (auto& [ws_ptr, client] : m_Clients) { + if (client.watch_device_ids.count(device_id)) { + SendText(ws_ptr, json); + } } } + + // 清掉旧 IDR 后必须立刻补一个新的,否则前端按新尺寸重建 VideoDecoder 后 + // 只能等下一个自然 GOP(x264 keyint 最长 15 秒),期间就是黑屏。先广播 + // resolution_changed 让前端重建解码器,再向客户端要 IDR:同一 WS 上有序, + // 新 IDR 一定晚于这条通知到达浏览器。 + // 会话尚未注册(构造函数阶段的第一包)/已关闭时拿不到 context,静默跳过; + // 前端的 keyframe 重试和客户端自然 GOP 仍是兜底。 + CONTEXT_OBJECT* ctx = GetScreenContext(device_id); + if (!ctx) return; + { + std::lock_guard lock(m_DeviceCacheMutex); + auto it = m_DeviceCache.find(device_id); + if (it == m_DeviceCache.end()) return; + // 快速重连/连续 BITMAPINFO 会在一秒内多次走到这里,按设备节流避免 + // 连续强制多帧 IDR。时间戳只在发送成功后写入,否则失败一次会把 + // 下一秒内的有效请求一起抑制掉。 + if (it->second->last_keyframe_req_ms != 0 && + GetTickCount64() - it->second->last_keyframe_req_ms < 1000) return; + } + BYTE bToken = COMMAND_SCREEN_KEYFRAME_REQ; + if (!ctx->Send2Client(&bToken, sizeof(BYTE))) return; + { + std::lock_guard lock(m_DeviceCacheMutex); + auto it = m_DeviceCache.find(device_id); + if (it != m_DeviceCache.end()) it->second->last_keyframe_req_ms = GetTickCount64(); + } } bool CWebService::GetScreenSize(uint64_t device_id, int& width, int& height) { @@ -2313,6 +2368,37 @@ bool CWebService::GetScreenSize(uint64_t device_id, int& width, int& height) { return width > 0 && height > 0; } +void CWebService::NotifyStreamAlgorithm(uint64_t device_id, int algo) { + if (m_bStopping) return; + + // 记进设备缓存:中途才加入观看的浏览器不会收到"变化"事件,靠 connect_result + // 里带出的 web_supported 拿到当前状态。 + { + std::lock_guard lock(m_DeviceCacheMutex); + auto it = m_DeviceCache.find(device_id); + if (it != m_DeviceCache.end()) it->second->stream_algo = algo; + } + + Json::Value res; + res["cmd"] = "stream_algorithm"; + res["id"] = std::to_string(device_id); + res["algo"] = algo; + // 浏览器只解 H.264(ALGORITHM_H264=2)。前端据此决定是否给"当前画质档位不支持 + // 网页预览"的提示;数值判断也放在服务端,前端不必知道算法枚举。 + res["web_supported"] = (algo == ALGORITHM_H264); + + Json::StreamWriterBuilder builder; + builder["indentation"] = ""; + std::string json = Json::writeString(builder, res); + + std::lock_guard lock(m_ClientsMutex); + for (auto& [ws_ptr, client] : m_Clients) { + if (client.watch_device_ids.count(device_id)) { + SendText(ws_ptr, json); + } + } +} + void CWebService::NotifyAudioState(uint64_t device_id, bool enabled) { if (m_bStopping) return; @@ -2777,6 +2863,13 @@ void CWebService::OnTerminalClosed(CONTEXT_OBJECT* ctx) { void CWebService::RegisterScreenContext(uint64_t device_id, CONTEXT_OBJECT* ctx) { if (!m_bRunning) return; + // 新会话首次为分辨率变化要 IDR 时必须放行:上一会话的时间戳可能还在 + // 1 秒节流窗口内。此处不与 m_ScreenContextsMutex 嵌套,避免锁序问题。 + { + std::lock_guard lock(m_DeviceCacheMutex); + auto it = m_DeviceCache.find(device_id); + if (it != m_DeviceCache.end()) it->second->last_keyframe_req_ms = 0; + } std::lock_guard lock(m_ScreenContextsMutex); m_ScreenContexts[device_id] = ctx; Mprintf("[WebService] Registered screen context for device %llu\n", device_id); diff --git a/server/2015Remote/WebService.h b/server/2015Remote/WebService.h index 29e411c..287e364 100644 --- a/server/2015Remote/WebService.h +++ b/server/2015Remote/WebService.h @@ -116,10 +116,19 @@ struct WebDeviceInfo { std::string client_type; // RES_CLIENT_TYPE: "APK"=Android, "EXE"=Windows, "LNX"=Linux, "MAC"=macOS // 当前会话的音频开关。-1=未知(客户端 BITMAPINFO 还没回来),0=关,1=开 int audio_enabled = -1; + // 客户端当前视频算法(ALGORITHM_*)。-1=未知(还没收到第一帧)。浏览器只解 H.264, + // 非 H.264 时服务端不转发视频帧,新加入的浏览器要靠 connect_result 里带出去的 + // web_supported 才知道该提示什么(见 NotifyStreamAlgorithm)。 + int stream_algo = -1; // Keyframe cache for new web clients std::vector keyframe_cache; std::mutex cache_mutex; + // 上次因分辨率变化向客户端请求新 IDR 的时间(GetTickCount64)。 + // 快速重连/连续 TOKEN_BITMAPINFO 会在一秒内多次触发通知,按设备节流 + // 避免连续强制多帧 IDR;RegisterScreenContext 会清零,新会话首包不受 + // 上一会话的时间戳影响。由 m_DeviceCacheMutex 保护。 + uint64_t last_keyframe_req_ms = 0; WebDeviceInfo() : id(0), screen_width(0), screen_height(0), online(false) {} }; @@ -159,6 +168,10 @@ public: // Resolution change notification void NotifyResolutionChange(uint64_t device_id, int width, int height, bool top_down = false, const std::string& client_type = ""); + // 当前视频算法(ALGORITHM_*)变化通知。浏览器只解 H.264:非 H.264 时服务端不转发 + // 任何视频帧,前端需要据此给出明确提示,而不是一直等一块黑屏。 + void NotifyStreamAlgorithm(uint64_t device_id, int algo); + // 读回 NotifyResolutionChange 缓存的物理捕获分辨率(来自 TOKEN_BITMAPINFO)。 // 尚未收到分辨率时返回 false 并置 w/h 为 0。MCP 远程控制(remote_open)用它映射归一化坐标。 bool GetScreenSize(uint64_t device_id, int& width, int& height); diff --git a/server/web/index.html b/server/web/index.html index 599b9ad..8f861e7 100644 --- a/server/web/index.html +++ b/server/web/index.html @@ -2908,6 +2908,8 @@ if (!isFocus) break; if (msg.ok) { + // 服务端缓存的当前视频算法:非 H.264 时不会有帧下发,直接提示。 + streamWebSupported = msg.web_supported !== false; // Resolution may not be available yet (first connection) if (msg.width && msg.height) { // 屏幕会话已确认在线(缓存会话),先补发断线期间遗留 @@ -2923,6 +2925,9 @@ // Wait for resolution_changed message updateScreenStatus('waiting', 'Waiting for video...'); } + if (!streamWebSupported) { + updateScreenStatus('waiting', WEB_UNSUPPORTED_HINT); + } // Audio state may or may not be cached yet on the server. // If not, the audio_state event below will populate it. if (typeof msg.audio_enabled === 'boolean') { @@ -3024,6 +3029,43 @@ if (isTouchDevice) updateUIForOrientation(); } initDecoder(msg.width, msg.height); + // 新解码器需要匹配新尺寸的 IDR。服务端清掉旧 IDR 缓存后会主动 + // 向客户端要一帧,这里再显式请求一次:老服务端、请求被 1 秒 + // 限流合并、或首个 IDR 在重建前到达时,看门狗仍能兜底恢复。 + requestFocusKeyframe(rcId || focusId); + break; + } + case 'stream_algorithm': { + // 服务端只在 H.264 时给 web 转发视频帧。客户端切到 DIFF/RGB565 + // (质量档位 High/Ultra,或客户端 x264 初始化失败后降级)时,画面 + // 会永久停在最后一帧/黑屏且 fps=0,以前没有任何提示,容易被误判成 + // 分辨率或网络问题。这里明确告诉操作者该去主控端把档位调回 Good。 + const saId = (msg.id !== undefined && msg.id !== null) ? String(msg.id) : null; + const saSupported = msg.web_supported !== false; + // 墙 tile 同样要拿到提示(墙和聚焦页是两套会话对象,必须都更新)。 + if (saId !== null) { + const wsess = wall.sessions.get(low32(saId)); + if (wsess && wsess.fullId === saId) { + wsess.webSupported = saSupported; + if (wsess.live) { + if (!saSupported) { + wallSetStatus(wsess, 'waiting', WEB_UNSUPPORTED_HINT); + } else if (wsess.status !== 'live') { + // 恢复 H.264:等下一帧到达时再标 Live,这里先显示恢复中。 + wallSetStatus(wsess, 'resuming'); + } + } + } + } + // 只有消息确实属于当前聚焦设备时才动聚焦页;墙模式下 currentDevice + // 为空,以前会误改隐藏的聚焦页状态。 + if (saId !== null && (!currentDevice || saId !== String(currentDevice.id))) break; + streamWebSupported = saSupported; + if (!streamWebSupported) { + updateScreenStatus('waiting', WEB_UNSUPPORTED_HINT); + } else if (decoder) { + updateScreenStatus('connected'); + } break; } case 'cursor': { @@ -3143,7 +3185,10 @@ function initDecoder(width, height) { decoderWidth = width; decoderHeight = height; - needKeyframe = false; + // 新建/重建解码器后必须先拿到 IDR 才能喂帧:configure 不带 avcC, + // SPS/PPS 由首个 keyframe 携带,delta 帧既没有参数集也没有参考帧。 + // 旧值 false 会把 P 帧直接送进新 decoder,解码失败后只剩一块黑画布。 + needKeyframe = true; decodeTimestamp = 0; // 远端分辨率:画布位图尺寸不再跟它走(画布位图 = 视口), @@ -3220,8 +3265,21 @@ } let decoderWidth = 0, decoderHeight = 0, needKeyframe = false; + // 当前视频算法是否能被浏览器解码(服务端 stream_algorithm / connect_result 维护)。 + // 非 H.264 时服务端不转发视频帧,界面上走提示而不是无限等待。 + let streamWebSupported = true; + const WEB_UNSUPPORTED_HINT = '当前画质档位不支持网页预览,请在主控端把质量等级调回 Good'; let decodeTimestamp = 0; // Monotonically increasing timestamp for decoder let currentCodec = null; // 'avc' | 'av1' | null(initDecoder 读取) + // 聚焦页关键帧恢复:needKeyframe 可能由 decoder error / codec 切换 / + // 分辨率变化 / 服务端吞帧等任一路径置位,统一由下面的看门狗兜底重请求, + // 不依赖每个置位点都记得发一次 keyframe。收到 IDR 后清零,之后若再出错 + // 可以重新重试。服务端对同一设备有 1 次/秒限流,这里间隔 1.5s、最多 5 次, + // 用完就交给客户端的自然 GOP,避免旧客户端不认该命令时无限刷。 + const FOCUS_KEYFRAME_RETRY_MS = 1500; + const FOCUS_KEYFRAME_MAX_RETRIES = 5; + let focusKeyframeRetries = 0; + let focusKeyframeReqAt = 0; // 首字节嗅探:H.264 Annex B 起始码必以 0x00 开头;AV1 OBU header // bit7=0 且 bits[3:6] = obu_type ∈ [1,15],首字节落在 [0x08,0x78] 区间且 @@ -3256,16 +3314,38 @@ // 单画面页请求一次新 IDR:晚加入 / 从墙放大的设备可立即重建参考链。 // 服务端按 1 次/秒/设备限流,失败时自然 GOP 兜底。 - function requestFocusKeyframe(deviceId) { - if (!serverCaps.keyframeRequest || !deviceId) return; - if (!ws || ws.readyState !== WebSocket.OPEN || !token) return; + function sendFocusKeyframe(deviceId) { + if (!serverCaps.keyframeRequest || !deviceId) return false; + if (!ws || ws.readyState !== WebSocket.OPEN || !token) return false; try { ws.send(JSON.stringify({ cmd: 'keyframe', id: String(deviceId), token, req: wallNextReq() })); - } catch (e) {} + focusKeyframeReqAt = performance.now(); + return true; + } catch (e) { return false; } } + // connect_result / resolution_changed 主动请求:新一轮恢复从头计数。 + function requestFocusKeyframe(deviceId) { + focusKeyframeRetries = 0; + sendFocusKeyframe(deviceId); + } + + // 看门狗:只要聚焦页还停在 needKeyframe 状态就按退避间隔继续要 IDR。 + // 只发不重置计数,避免与 requestFocusKeyframe 互相清零后无限重试。 + setInterval(() => { + if (!needKeyframe) return; + if (focusKeyframeRetries >= FOCUS_KEYFRAME_MAX_RETRIES) return; + if (!serverCaps.keyframeRequest || !currentDevice) return; + const screenPage = document.getElementById('screen-page'); + if (!screenPage || !screenPage.classList.contains('active')) return; + const now = performance.now(); + if (now - focusKeyframeReqAt < FOCUS_KEYFRAME_RETRY_MS) return; + // 只有真正发出去才消耗重试次数:WS 正在关闭时别把 5 次机会耗光。 + if (sendFocusKeyframe(String(currentDevice.id))) focusKeyframeRetries++; + }, 500); + function currentDeviceId() { return currentDevice ? String(currentDevice.id) : undefined; } @@ -4128,6 +4208,9 @@ if (changed && s.decoder) wallCloseDecoder(s); } if (typeof msg.top_down === 'boolean') s.topDown = msg.top_down === true; + // 服务端缓存的当前视频算法:非 H.264 时不会有任何帧下发,墙 tile 也 + // 要给明确提示,否则和聚焦页的黑屏一样会被当成连接/分辨率问题。 + if (typeof msg.web_supported === 'boolean') s.webSupported = msg.web_supported; if (s.width && s.height) wallPrepareCanvasDimensions(s); if (s.live) { @@ -4136,7 +4219,11 @@ s.needKeyframe = true; s.keyframeRetries = 0; if (s.keyframeTimer) { clearTimeout(s.keyframeTimer); s.keyframeTimer = null; } - wallSetStatus(s, 'resuming'); + if (s.webSupported === false) { + wallSetStatus(s, 'waiting', WEB_UNSUPPORTED_HINT); + } else { + wallSetStatus(s, 'resuming'); + } wallRequestKeyframe(s, true); } else { // 之前失败/离线的 session 现在连接成功:解除 Error/Offline, @@ -4493,7 +4580,8 @@ currentCodec = frameCodec; console.log('Reinitializing decoder on keyframe, codec=' + currentCodec); initDecoder(decoderWidth, decoderHeight); - needKeyframe = false; + // 不在这里清 needKeyframe:若 configure 尚未完成会在下面 + // 提前 return,保持 needKeyframe 让看门狗继续要 IDR。 } else { needKeyframe = true; return; @@ -4504,7 +4592,10 @@ // Skip delta frames if we need a keyframe if (needKeyframe && !isKeyframe) return; - if (isKeyframe) needKeyframe = false; + if (isKeyframe) { + needKeyframe = false; + focusKeyframeRetries = 0; // 恢复成功,之后再次 error 可重新重试 + } try { // Check decoder queue to avoid overwhelming it (but never skip keyframes)