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:
yuanyuanxianganddeepseek-flash committed 2026-11-10 11:34:07 +01:00
1 parent 5eb44307ee
commit c7e23f750d
5 files changed
+432 -53

No files matched your search

+99 -8
View File
@@ -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)