From d802f80213c4749079dbc06b567588c40eceba8b Mon Sep 17 00:00:00 2001 From: cxh Date: Thu, 16 Oct 2025 13:44:05 +0800 Subject: [PATCH] 1 --- src/rtmp_manager.cpp | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/src/rtmp_manager.cpp b/src/rtmp_manager.cpp index 62d3221..60f2126 100644 --- a/src/rtmp_manager.cpp +++ b/src/rtmp_manager.cpp @@ -178,7 +178,6 @@ 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; @@ -194,24 +193,31 @@ RTMPManager::StreamResultInfo RTMPManager::stop_camera(const std::string &cam_na auto it = streams.find(key); if (it == streams.end()) { - // 没有这个流(可能从未推过或启动失败)→ 视为成功,因为状态已经是“未推” res.result = 0; res.reason = "Already stopped (no active stream)"; return res; } - // 找到了正在推的流 → 停止 ctx = std::move(it->second); streams.erase(it); } - bool was_running = ctx->running.load(); + bool was_running = ctx->status.running; // ✅ 新增:判断是否真的在推流 ctx->running.store(false); if (ctx->thread.joinable()) ctx->thread.join(); - res.result = 0; - res.reason = was_running ? "Stopped manually" : "Already stopped"; + if (was_running) + { + res.result = 0; + res.reason = "Stopped manually"; + } + else + { + res.result = 1; + res.reason = "Not running (stream never started)"; + } + return res; } @@ -293,6 +299,7 @@ void RTMPManager::stream_loop(Camera cam, StreamType type, StreamContext *ctx) { first_frame = true; status.running = true; + ctx->status.running = true; status.last_result = StreamResult::OK; status.last_error.clear(); try_set_start(status); // 首帧成功:通知 start_camera @@ -335,6 +342,7 @@ void RTMPManager::stream_loop(Camera cam, StreamType type, StreamContext *ctx) if (bus) gst_object_unref(bus); gst_object_unref(pipeline); + ctx->status.running = false; // 线程收尾:这里不要删 map 里的 ctx,stop_camera 已经负责转移并 join }