perf(live-stream): 低延迟参数+RTSP通道编码修正+4K缩放转码+快照兜底恢复

This commit is contained in:
liufumingren 2026-08-26 23:31:57 +08:00
parent 9ebe3d0dc4
commit 93198a8828
2 changed files with 89 additions and 41 deletions

View File

@ -10,6 +10,7 @@ import EventEmitter from 'events';
import { getResourcePath, getProjectRoot, extractPortCandidates, writeRuntimeState, isRecordingMode, getRunMode } from '../utils/runtime.js';
import db from '../utils/db.js';
import http from 'http';
import net from 'net';
function hasProcessArg(flag) {
return process.argv.includes(flag);
@ -800,9 +801,9 @@ class Main extends EventEmitter {
const rawStreamType = parseInt(params.get('streamType'));
const streamType = Number.isNaN(rawStreamType) ? 1 : rawStreamType;
const mode = params.get('mode') || 'auto'; // auto=RTSP优先→失败降级HTTP
// 输出帧率限制:无限制时按源帧率(~25fps)推 MJPEG,浏览器持续解码大图导致内存暴涨,默认 5fps
// 输出帧率:默认 25fps 与源帧率一致(不抽帧,保持流畅);背压丢帧保护负责防内存泄漏,无需靠限帧
const rawFps = parseInt(params.get('fps'));
const fps = Number.isNaN(rawFps) ? 5 : Math.min(15, Math.max(1, rawFps));
const fps = Number.isNaN(rawFps) ? 25 : Math.min(30, Math.max(1, rawFps));
const channelCode = 100 * channel + streamType;
if (!ip || !user || !pass) {
@ -835,10 +836,29 @@ class Main extends EventEmitter {
return this._handleHikvisionLiveStreamHttp(res, { ip, user, pass, channel, streamType, channelCode, hikvisionService, params });
}
// auto 模式:快速预检 RTSP 端口,不通直接降级(避免 ffmpeg 长等待造成“连不上”假象)
if (mode === 'auto' && ffmpegPath) {
const rtspPort = parseInt(params.get('rtspPort')) || 554;
const rtspReachable = await new Promise((resolve) => {
const s = net.createConnection({ host: ip, port: rtspPort });
s.setTimeout(1500);
s.once('connect', () => { s.destroy(); resolve(true); });
s.once('timeout', () => { s.destroy(); resolve(false); });
s.once('error', () => { resolve(false); });
});
if (!rtspReachable) {
console.log('[Hikvision] RTSP 端口 %d 预检不通,直接降级 HTTP 快照', rtspPort);
return this._handleHikvisionLiveStreamHttp(res, { ip, user, pass, channel, streamType, channelCode, hikvisionService, params });
}
}
// RTSP 模式(默认)
const rtspPort = parseInt(params.get('rtspPort')) || 554;
const rtspUrl = `rtsp://${encodeURIComponent(user)}:${encodeURIComponent(pass)}@${ip}:${rtspPort}/Streaming/Channels/${channelCode}`;
console.log('[Hikvision] live-stream RTSP channel=%d code=%d url=%s', channel, channelCode,
// RTSP 通道编码:海康 RTSP 1=主码流、2=子码流;streamType 0/1 均按主码流(601),2 按子码流(602)
const rtspStreamType = streamType === 2 ? 2 : 1;
const rtspChannelCode = 100 * channel + rtspStreamType;
const rtspUrl = `rtsp://${encodeURIComponent(user)}:${encodeURIComponent(pass)}@${ip}:${rtspPort}/Streaming/Channels/${rtspChannelCode}`;
console.log('[Hikvision] live-stream RTSP channel=%d code=%d url=%s', channel, rtspChannelCode,
rtspUrl.replace(/:[^:@]+@/, ':****@'));
let ffmpeg = null;
@ -854,7 +874,7 @@ class Main extends EventEmitter {
if (!res.writableEnded) try { res.end(); } catch {}
};
// RTSP 失败时降级到 HTTP 快照模式
// RTSP 失败时降级到 HTTP 快照模式(WS 模式加密协商未实现,仅 mode=ws 显式使用)
const fallbackToHttp = () => {
if (closed) return;
closed = true;
@ -883,13 +903,19 @@ class Main extends EventEmitter {
// 先不发送响应头——等首帧到或 ffmpeg 失败后再决定
// 如果连不上 RTSP,ffmpeg 会快速退出,此时可返回 JSON 错误
const rtspTransport = params.get('transport') === 'udp' ? 'udp' : 'tcp';
// 主码流可能是 4K:缩放到 1280 宽降低编码数据量与浏览器解码压力;fps < 源帧率时才限帧
const vf = fps >= 25 ? 'scale=1280:-2' : `scale=1280:-2,fps=${fps}`;
ffmpeg = spawn(ffmpegPath, [
'-rtsp_transport', 'tcp',
'-rtsp_transport', rtspTransport,
'-timeout', '3000000', // RTSP 连接超时 3s(微秒)
'-fflags', 'nobuffer', // 低延迟:不缓冲输入
'-flags', 'low_delay',
'-max_delay', '200000', // demuxer 缓冲上限 200ms
'-i', rtspUrl,
'-vf', 'fps=' + fps,
'-vf', vf,
'-f', 'mjpeg',
'-q:v', '5',
'-q:v', '6',
'-an',
'-nostdin',
'pipe:1'
@ -1043,12 +1069,16 @@ class Main extends EventEmitter {
const { spawn } = await import('child_process');
// Step 2: 启动 ffmpeg(从 stdin 读取 PS 流 → MJPEG 输出)
const vfArgsWs = fps >= 25 ? [] : ['-vf', 'fps=' + fps];
ffmpeg = spawn(ffmpegPath, [
'-f', 'mpegts',
'-f', 'mpeg', // 海康 ISAPI WS 推送的是 MPEG-PS 流(非 TS)
'-fflags', 'nobuffer', // 低延迟:不缓冲输入
'-flags', 'low_delay',
'-max_delay', '200000', // demuxer 缓冲上限 200ms
'-i', 'pipe:0',
'-vf', 'fps=' + fps,
...vfArgsWs,
'-f', 'mjpeg',
'-q:v', '5',
'-q:v', '6',
'-an',
'-nostdin',
'pipe:1'
@ -1174,17 +1204,20 @@ class Main extends EventEmitter {
*/
async _handleHikvisionLiveStreamHttp(res, { ip, user, pass, channel, streamType, channelCode, hikvisionService, params }) {
const httpPort = parseInt(params.get('httpPort')) || parseInt(params.get('port')) || 80;
const interval = Math.max(100, parseInt(params.get('interval')) || 250); // 轮询间隔 ms
// 最小拉取间隔:默认 200ms(5fps 均匀节奏,与旧版一致的兜底体验);可用 &interval= 覆盖(0=连续)
const interval = Number.isFinite(parseInt(params.get('interval'))) ? Math.max(0, parseInt(params.get('interval'))) : 200;
let closed = false;
let frameCount = 0;
const startTime = Date.now();
let cookieJar = {};
let sessionTag = '';
let agent = null;
const doCleanup = () => {
if (closed) return;
closed = true;
if (agent) { try { agent.destroy(); } catch {} }
if (!res.writableEnded) try { res.end(); } catch {}
};
@ -1198,6 +1231,9 @@ class Main extends EventEmitter {
sessionTag = loginResult.sessionTag;
console.log('[Hikvision] live-stream HTTP 登录成功 ch=%d, 开始轮询', channel);
// 连接复用:避免每帧 TCP 握手开销
agent = new http.Agent({ keepAlive: true, maxSockets: 2 });
// Step 2: 发送 MJPEG 响应头
res.writeHead(200, {
'Content-Type': 'multipart/x-mixed-replace; boundary=snapshot',
@ -1211,39 +1247,43 @@ class Main extends EventEmitter {
const snapshotUrl = `http://${ip}:${httpPort}/ISAPI/Streaming/channels/${channelCode}/picture`;
const headers = sessionTag ? { 'SessionTag': sessionTag } : {};
// Step 3: 轮询循环
while (!closed) {
const loopStart = Date.now();
// Step 3: 单路轮询(均匀节奏,避免并发乱序导致的画面跳帧)
const fetchFrame = async () => {
try {
const result = await hikvisionService._req('GET', snapshotUrl, null, headers, cookieJar, true);
const hdrs = sessionTag ? { 'SessionTag': sessionTag } : {};
const result = await hikvisionService._req('GET', snapshotUrl, null, hdrs, cookieJar, true, agent);
if (result.statusCode === 200 && result.body && result.body.length > 500) {
const jpeg = result.body; // Buffer (binary=true)
if (!res.destroyed) {
// 背压保护:客户端消费慢(如浏览器后台标签页)时丢帧,
// 避免 res 缓冲无限堆积导致内存持续增长
if (res.writableLength < 512 * 1024) {
res.write(`--snapshot\r\nContent-Type: image/jpeg\r\nContent-Length: ${jpeg.length}\r\n\r\n`);
res.write(jpeg);
res.write('\r\n');
frameCount++;
}
}
} else if (result.statusCode === 401) {
// Session 过期,重新登录
console.log('[Hikvision] live-stream HTTP session 过期,重新登录');
return result.body;
}
if (result.statusCode === 401) {
// Session 过期,重新登录(login 内部有锁)
const relogin = await hikvisionService.login(ip, httpPort, user, pass);
cookieJar = relogin.cookieJar;
sessionTag = relogin.sessionTag;
headers['SessionTag'] = sessionTag || '';
}
} catch (e) {
// 单次快照失败不中断,继续轮询
}
} catch (e) { /* 单次失败忽略 */ }
return null;
};
// 控制轮询频率
const elapsed = Date.now() - loopStart;
const delay = Math.max(0, interval - elapsed);
if (delay > 0) await new Promise(r => setTimeout(r, delay));
const writeFrame = (jpeg) => {
if (!jpeg || closed || res.destroyed) return;
// 背压保护:客户端消费慢(如浏览器后台标签页)时丢帧,避免内存持续增长
if (res.writableLength < 512 * 1024) {
res.write(`--snapshot\r\nContent-Type: image/jpeg\r\nContent-Length: ${jpeg.length}\r\n\r\n`);
res.write(jpeg);
res.write('\r\n');
frameCount++;
}
};
while (!closed) {
const loopStart = Date.now();
const jpeg = await fetchFrame();
writeFrame(jpeg);
if (interval > 0) {
const delay = Math.max(0, interval - (Date.now() - loopStart));
if (delay > 0) await new Promise(r => setTimeout(r, delay));
}
}
} catch (err) {
console.error('[Main] live-stream HTTP 启动失败:', err.message);

View File

@ -821,7 +821,13 @@ class HikvisionService extends BaseService {
if (op === 0x09) { wsSend(payload, 0x0A); }
else if (op === 0x02) { onData?.(payload); }
else if (op === 0x01) {
try { const obj = JSON.parse(payload.toString('utf-8')); if (obj.statusCode && obj.statusCode !== 0 && obj.statusCode !== 1) break; } catch {}
try {
const obj = JSON.parse(payload.toString('utf-8'));
if (obj.statusCode && obj.statusCode !== 0 && obj.statusCode !== 1) {
console.log('[Hikvision] liveview cmd 响应错误:', payload.toString('utf-8'));
break;
}
} catch {}
}
}
} catch (e) { onError?.(e); }
@ -829,7 +835,7 @@ class HikvisionService extends BaseService {
})();
});
}
async _req(method, url, body, headers, cookieJar, binary = false) {
async _req(method, url, body, headers, cookieJar, binary = false, agent = null) {
const u = new URL(url);
const bodyBuf = body ? Buffer.from(typeof body === 'string' ? body : body, 'utf-8') : null;
const cookies = Object.entries(cookieJar || {}).map(([k, v]) => `${k}=${v}`).join('; ');
@ -845,7 +851,9 @@ class HikvisionService extends BaseService {
return new Promise((resolve, reject) => {
const mod = u.protocol === 'https:' ? https : http;
const req = mod.request({ hostname: u.hostname, port: u.port || 80, path: u.pathname + u.search, method, headers: hdrs, timeout: 10000 }, (res) => {
const reqOpts = { hostname: u.hostname, port: u.port || 80, path: u.pathname + u.search, method, headers: hdrs, timeout: 10000 };
if (agent) reqOpts.agent = agent;
const req = mod.request(reqOpts, (res) => {
const ck = [];
res.on('data', c => ck.push(c));
res.on('end', () => {