From 5033d2e35f85976658ef7134acd0cc68ffe72ecd Mon Sep 17 00:00:00 2001 From: kim Date: Tue, 3 Nov 2026 13:50:56 +0900 Subject: [PATCH] =?UTF-8?q?fix(nvr):=20=EC=8A=A4=ED=8A=B8=EB=A6=BC=20?= =?UTF-8?q?=EC=8B=9C=EC=9E=91=EC=9D=84=20500ms=20=EA=B0=84=EA=B2=A9?= =?UTF-8?q?=EC=9C=BC=EB=A1=9C=20=E2=80=94=20=EB=B6=84=ED=95=A0=20=ED=99=94?= =?UTF-8?q?=EB=A9=B4=205=EC=B1=84=EB=84=90=20=EB=8F=99=EC=8B=9C=20?= =?UTF-8?q?=EC=9A=94=EC=B2=AD=EC=9D=B4=20=EC=B2=AB=20=ED=94=84=EB=A0=88?= =?UTF-8?q?=EC=9E=84=20=ED=83=80=EC=9E=84=EC=95=84=EC=9B=83=EC=97=90=20?= =?UTF-8?q?=EA=B1=B8=EB=A6=AC=EB=8D=98=20=EB=AC=B8=EC=A0=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 분할 화면이 채널 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 --- src/gateway/routes/nvr.ts | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/src/gateway/routes/nvr.ts b/src/gateway/routes/nvr.ts index e32698b..3b60fe1 100644 --- a/src/gateway/routes/nvr.ts +++ b/src/gateway/routes/nvr.ts @@ -587,6 +587,19 @@ export function registerNvrRoutes( // 스트림이 시작된 시각 — /stop이 방금 시작한 새 스트림을 죽이지 않게 하려고(아래 /stop 참고). const activeStreamStartedAt = new Map(); 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);