This commit is contained in:
cxh 2025-10-16 13:44:05 +08:00
parent f2d864af0b
commit d802f80213

View File

@ -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<int>(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();
if (was_running)
{
res.result = 0;
res.reason = was_running ? "Stopped manually" : "Already stopped";
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 里的 ctxstop_camera 已经负责转移并 join
}