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
This commit is contained in:
1 parent
5eb44307ee
commit
c7e23f750d
5 files changed
+432
-53
No files matched your search
@@ -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<std::mutex> 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<std::mutex> 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<std::mutex> 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<std::mutex> 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<std::mutex> 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<std::mutex> 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<std::mutex> 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<std::mutex> 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<std::mutex> 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<std::mutex> 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<std::mutex> 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<std::mutex> lock(m_ScreenContextsMutex);
|
||||
m_ScreenContexts[device_id] = ctx;
|
||||
Mprintf("[WebService] Registered screen context for device %llu\n", device_id);
|
||||
|
||||
Reference in new issue
Block a user