feat: 每NVR 3并发 + ffmpeg纯复制修复 + onvif依赖 + OSD日志
- 调度器: maxPerNvr 1→3, running追踪从Set改Map计数 - ffmpeg: 移除libx264重编码兜底, 修正-f mpegps→自动检测 - 新增 onvif 依赖用于摄像头OSD - setting.setCameraOSD 增加入口/参数/结果日志 - wsClient 心跳更新
This commit is contained in:
parent
1c8f945773
commit
f56ca33a8d
30
core/main.js
30
core/main.js
@ -586,15 +586,16 @@ class Main extends EventEmitter {
|
|||||||
try { const p = JSON.parse(row.task_params || '{}'); const fp = this._resolvePath(p.output); if (fp && fs.existsSync(fp)) fs.unlinkSync(fp); } catch {}
|
try { const p = JSON.parse(row.task_params || '{}'); const fp = this._resolvePath(p.output); if (fp && fs.existsSync(fp)) fs.unlinkSync(fp); } catch {}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ④ 每个录像机(IP)独占一个 worker,避免不同 NVR 互相阻塞
|
// ④ 每个录像机(IP)最多 maxPerNvr 个并发 worker
|
||||||
const maxPerNvr = 1; // 每个 NVR 最多 1 个并发
|
const maxPerNvr = 3; // 每个 NVR 最多 3 个并发
|
||||||
const maxTotal = 4; // 总并发上限
|
const maxTotal = 4; // 总并发上限
|
||||||
|
|
||||||
// 获取正在运行的 NVR IP
|
// 获取正在运行的 IP 及其并发数
|
||||||
const runningRows = this.db.all(`SELECT DISTINCT json_extract(task_params, '$.ip') as ip FROM hik_tasks WHERE status = 'running'`);
|
const runningRows = this.db.all(`SELECT json_extract(task_params, '$.ip') as ip, COUNT(*) as cnt FROM hik_tasks WHERE status = 'running' GROUP BY ip`);
|
||||||
const runningIps = new Set(runningRows.map(r => r.ip).filter(Boolean));
|
const runningCount = new Map();
|
||||||
|
for (const r of runningRows) { if (r.ip) runningCount.set(r.ip, r.cnt); }
|
||||||
|
|
||||||
// 获取待处理任务,按 IP 分组取最早的一条
|
// 获取待处理任务,按 IP 分组取最早的多条
|
||||||
const pendingRows = this.db.all(`SELECT * FROM hik_tasks WHERE status = 'pending' ORDER BY id ASC`);
|
const pendingRows = this.db.all(`SELECT * FROM hik_tasks WHERE status = 'pending' ORDER BY id ASC`);
|
||||||
const pendingByIp = new Map();
|
const pendingByIp = new Map();
|
||||||
for (const pt of pendingRows) {
|
for (const pt of pendingRows) {
|
||||||
@ -604,15 +605,19 @@ class Main extends EventEmitter {
|
|||||||
this.db.run(`UPDATE hik_tasks SET status='failed', error_message='参数不完整' WHERE id = ?`, pt.id);
|
this.db.run(`UPDATE hik_tasks SET status='failed', error_message='参数不完整' WHERE id = ?`, pt.id);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (!pendingByIp.has(ip)) pendingByIp.set(ip, pt);
|
if (!pendingByIp.has(ip)) pendingByIp.set(ip, []);
|
||||||
|
if (pendingByIp.get(ip).length < maxPerNvr) pendingByIp.get(ip).push(pt);
|
||||||
}
|
}
|
||||||
|
|
||||||
let started = 0;
|
let started = 0;
|
||||||
for (const [ip, pt] of pendingByIp) {
|
for (const [ip, tasks] of pendingByIp) {
|
||||||
if (runningIps.has(ip)) continue; // 该 NVR 已有下载中
|
const current = runningCount.get(ip) || 0;
|
||||||
|
if (current >= maxPerNvr) continue; // 该 NVR 已达并发上限
|
||||||
if (started >= maxTotal) break;
|
if (started >= maxTotal) break;
|
||||||
if (runningIps.size + started >= maxTotal) break;
|
// 最多启动 maxPerNvr - current 个新任务
|
||||||
|
const slots = Math.min(tasks.length, maxPerNvr - current, maxTotal - started);
|
||||||
|
for (let i = 0; i < slots; i++) {
|
||||||
|
const pt = tasks[i];
|
||||||
const downloadCfg = { ...JSON.parse(pt.task_params || '{}'), output: this._resolvePath(JSON.parse(pt.task_params || '{}').output) };
|
const downloadCfg = { ...JSON.parse(pt.task_params || '{}'), output: this._resolvePath(JSON.parse(pt.task_params || '{}').output) };
|
||||||
const wid = ip; // 用 IP 作为 worker_id
|
const wid = ip; // 用 IP 作为 worker_id
|
||||||
this.db.run(`UPDATE hik_tasks SET status='running', worker_id=?, start_time=datetime('now') WHERE id=?`, wid, pt.id);
|
this.db.run(`UPDATE hik_tasks SET status='running', worker_id=?, start_time=datetime('now') WHERE id=?`, wid, pt.id);
|
||||||
@ -667,9 +672,10 @@ class Main extends EventEmitter {
|
|||||||
};
|
};
|
||||||
this._hikTasks.set(taskId, task);
|
this._hikTasks.set(taskId, task);
|
||||||
hikvisionService.startDownload(downloadCfg, taskId, task);
|
hikvisionService.startDownload(downloadCfg, taskId, task);
|
||||||
runningIps.add(ip);
|
runningCount.set(ip, (runningCount.get(ip) || 0) + 1);
|
||||||
started++;
|
started++;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
// 空闲拉取
|
// 空闲拉取
|
||||||
const pendingCount = pendingByIp.size;
|
const pendingCount = pendingByIp.size;
|
||||||
if (pendingCount === 0) {
|
if (pendingCount === 0) {
|
||||||
|
|||||||
@ -110,7 +110,7 @@ class WSClient {
|
|||||||
const { token, refreshToken, tokenExpireTime } = this.app.config;
|
const { token, refreshToken, tokenExpireTime } = this.app.config;
|
||||||
if (!this.app.info && token && refreshToken && tokenExpireTime) {
|
if (!this.app.info && token && refreshToken && tokenExpireTime) {
|
||||||
if (tokenExpireTime < Date.now()) {
|
if (tokenExpireTime < Date.now()) {
|
||||||
//console.log('[WSClient] 心跳检测: token 已过期,尝试刷新');
|
console.log('[WSClient] 心跳检测: token 已过期,尝试刷新');
|
||||||
this.send({ method: 'auth.refreshToken', data: { token: refreshToken } });
|
this.send({ method: 'auth.refreshToken', data: { token: refreshToken } });
|
||||||
} else {
|
} else {
|
||||||
//拉取信息
|
//拉取信息
|
||||||
@ -122,7 +122,7 @@ class WSClient {
|
|||||||
}
|
}
|
||||||
async _handleMessage(msg) {
|
async _handleMessage(msg) {
|
||||||
if(msg === 'pong') {
|
if(msg === 'pong') {
|
||||||
//console.log('[WSClient] 收到心跳响应');
|
console.log('[WSClient] 收到心跳响应');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
//console.log("[WSClient]原始消息", msg)
|
//console.log("[WSClient]原始消息", msg)
|
||||||
|
|||||||
@ -25,6 +25,7 @@
|
|||||||
"@xmldom/xmldom": "^0.9.10",
|
"@xmldom/xmldom": "^0.9.10",
|
||||||
"better-sqlite3": "^12.11.1",
|
"better-sqlite3": "^12.11.1",
|
||||||
"iconv-lite": "^0.7.2",
|
"iconv-lite": "^0.7.2",
|
||||||
|
"onvif": "^0.8.1",
|
||||||
"ws": "^8.19.0"
|
"ws": "^8.19.0"
|
||||||
},
|
},
|
||||||
"optionalDependencies": {
|
"optionalDependencies": {
|
||||||
|
|||||||
@ -508,9 +508,9 @@ class HikvisionService extends BaseService {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
transcoded = await ffRun(`-f mpegts -i "${workFile}" -c:v copy -bsf:v h264_mp4toannexb -an "${mp4Path}"`, 60000);
|
// PS → MP4 纯复制(视频流 copy,丢弃音频),不指定 -f 让 ffmpeg 自动检测
|
||||||
if (!transcoded) transcoded = await ffRun(`-f mpegps -i "${workFile}" -c:v copy -bsf:v h264_mp4toannexb -an "${mp4Path}"`, 60000);
|
transcoded = await ffRun(`-i "${workFile}" -c:v copy -bsf:v h264_mp4toannexb -an "${mp4Path}"`, 60000);
|
||||||
if (!transcoded) transcoded = await ffRun(`-i "${workFile}" -c:v libx264 -preset fast -crf 23 -an "${mp4Path}"`, 120000);
|
// copy 失败直接保留 PS 原文件,不再使用 libx264 重编码(多 worker 时 CPU 不可接受)
|
||||||
if (transcoded) {
|
if (transcoded) {
|
||||||
const mp4Size = fs.statSync(mp4Path).size;
|
const mp4Size = fs.statSync(mp4Path).size;
|
||||||
try { fs.unlinkSync(workFile); } catch { /* */ }
|
try { fs.unlinkSync(workFile); } catch { /* */ }
|
||||||
|
|||||||
@ -316,10 +316,16 @@ class SettingService extends service {
|
|||||||
* data: { rtspUrl, text, x?, y? }
|
* data: { rtspUrl, text, x?, y? }
|
||||||
*/
|
*/
|
||||||
async setCameraOSD(data) {
|
async setCameraOSD(data) {
|
||||||
if (!data || !data.rtspUrl || !data.text) return;
|
console.log('[CameraOSD] set 收到请求:', JSON.stringify(data));
|
||||||
|
if (!data || !data.rtspUrl || !data.text) {
|
||||||
|
console.log('[CameraOSD] set 参数不完整,跳过:', { hasRtsp: !!data?.rtspUrl, hasText: !!data?.text });
|
||||||
|
return;
|
||||||
|
}
|
||||||
try {
|
try {
|
||||||
|
console.log('[CameraOSD] set 开始执行 rtspUrl=%s text=%s x=%d y=%d', data.rtspUrl, data.text, data.x, data.y);
|
||||||
const { setCameraOSD } = await import('../utils/cameraOsd.js');
|
const { setCameraOSD } = await import('../utils/cameraOsd.js');
|
||||||
await setCameraOSD(data.rtspUrl, data.text, data.x, data.y);
|
await setCameraOSD(data.rtspUrl, data.text, data.x, data.y);
|
||||||
|
console.log('[CameraOSD] set 执行成功');
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error('[CameraOSD] set 执行失败:', e.message);
|
console.error('[CameraOSD] set 执行失败:', e.message);
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user