From f2d864af0bef57f01f973bf609d6afbc09627dcf Mon Sep 17 00:00:00 2001 From: cxh Date: Thu, 16 Oct 2025 13:38:23 +0800 Subject: [PATCH] 1 --- src/rtmp_manager.cpp | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/src/rtmp_manager.cpp b/src/rtmp_manager.cpp index eb064ec..62d3221 100644 --- a/src/rtmp_manager.cpp +++ b/src/rtmp_manager.cpp @@ -122,6 +122,13 @@ RTMPManager::StreamResultInfo RTMPManager::start_camera(const Camera &cam, Strea res.loc = get_camera_index(cam.name); res.url = get_stream_url(cam.name, type); + if (res.loc < 0 || res.loc >= static_cast(g_app_config.cameras.size())) + { + res.result = 1; + res.reason = "Invalid channel index"; + return res; + } + const std::string key = make_stream_key(cam.name, type); std::unique_ptr ctx; @@ -171,27 +178,40 @@ RTMPManager::StreamResultInfo RTMPManager::stop_camera(const std::string &cam_na res.loc = get_camera_index(cam_name); res.url = get_stream_url(cam_name, type); + // 检查通道号是否合法 + if (res.loc < 0 || res.loc >= static_cast(g_app_config.cameras.size())) + { + res.result = 1; + res.reason = "Invalid channel index"; + return res; + } + std::unique_ptr ctx; std::string key = make_stream_key(cam_name, type); + { std::lock_guard lock(streams_mutex); auto it = streams.find(key); if (it == streams.end()) { + // 没有这个流(可能从未推过或启动失败)→ 视为成功,因为状态已经是“未推” res.result = 0; - res.reason = "Not streaming"; + res.reason = "Already stopped (no active stream)"; return res; } - it->second->running.store(false); + // 找到了正在推的流 → 停止 ctx = std::move(it->second); streams.erase(it); } + bool was_running = ctx->running.load(); + ctx->running.store(false); + if (ctx->thread.joinable()) ctx->thread.join(); res.result = 0; - res.reason = "Stopped manually"; + res.reason = was_running ? "Stopped manually" : "Already stopped"; return res; }