fix(nvr): 스트림 시작을 500ms 간격으로 — 분할 화면 5채널 동시 요청이 첫 프레임 타임아웃에 걸리던 문제

분할 화면이 채널 5개를 한꺼번에 열면 NVR(동시 RTSP 세션 ≈4)이 첫 키프레임을 늦게 줘서
6초 첫프레임 타임아웃에 걸렸고, ffmpeg가 SIGKILL("exited null before first frame")된 뒤
재시도로 겨우 복구됐다(2026-11-03 채널 0~4 서브 스트림 전부 1차 시도 실패).
NVR 채널의 최초 launch()를 슬롯 예약으로 500ms씩 벌려 순차 시작한다. 독립 카메라(1000+)는 제외.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
kimandClaude Sonnet 5.5 committed 2026-11-03 13:50:56 +09:00
1 parent 84bf2e7016
commit 5033d2e35f
1 file changed
+21 -1
+21 -1
View File
@@ -587,6 +587,19 @@ export function registerNvrRoutes(
// 스트림이 시작된 시각 — /stop이 방금 시작한 새 스트림을 죽이지 않게 하려고(아래 /stop 참고).
const activeStreamStartedAt = new Map<string, number>();
const STOP_GRACE_MS = 3000;
// NVR 스트림 시작 간격 — 분할 화면이 채널 5개를 한꺼번에 열면 NVR(동시 RTSP 세션 ≈4)이 첫
// 키프레임을 늦게 줘서 6초 첫프레임 타임아웃에 걸리고, ffmpeg가 SIGKILL("exited null before first
// frame")된 뒤 재시도로 겨우 복구됐다(2026-11-03 채널 0~4 서브 스트림 전부 1차 시도 실패).
// 시작 시각만 NVR_START_GAP_MS씩 벌려 순차로 연다. 첫프레임 타이머는 launch() 안에서 시작하므로
// 대기 시간은 타임아웃에 포함되지 않는다. 독립 카메라(1000+ id)는 NVR 세션이 아니라 대상 아님.
const NVR_START_GAP_MS = 500;
let nvrNextStartAt = 0;
const reserveNvrStartSlot = (): number => {
const now = Date.now();
const at = Math.max(now, nvrNextStartAt);
nvrNextStartAt = at + NVR_START_GAP_MS;
return at - now;
};
app.get('/api/nvr/stream/:channelId', (req: Request, res: Response) => {
const user = (req as any).user;
if (!user) return res.status(401).json({ error: 'Unauthorized' });
@@ -811,7 +824,14 @@ export function registerNvrRoutes(
respawnTimer = setTimeout(() => { respawnTimer = null; launch(); }, 800);
};
launch();
// 첫 시작은 슬롯 예약으로 간격을 둔다. 대기 중 클라이언트가 떠나면 abort()가 respawnTimer를
// 지워 launch()가 호출되지 않는다.
const startDelay = extraCam ? 0 : reserveNvrStartSlot();
if (startDelay > 0) {
respawnTimer = setTimeout(() => { respawnTimer = null; launch(); }, startDelay);
} else {
launch();
}
// 클라이언트 연결 종료(탭 닫기, img.src 변경, 페이지 이동) → ffmpeg 즉시 정리.
res.on('close', abort);