From 185d1dd952eb8507e5416ab37ac98adc86713e57 Mon Sep 17 00:00:00 2001 From: cxh Date: Wed, 15 Oct 2025 10:52:29 +0800 Subject: [PATCH] temp --- 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 96bc7bb..ded8ab5 100644 --- a/src/rtmp_manager.cpp +++ b/src/rtmp_manager.cpp @@ -82,11 +82,12 @@ void RTMPManager::stream_loop(Camera cam, StreamType type) std::string key = make_stream_key(cam.name, type); LOG_INFO("[RTMP] Stream loop started for " + key); - const int MAX_RETRIES = 5; + const int MAX_RETRIES = 1; int retry_count = 0; while (true) { + // 检查线程是否被标记为停止 { std::lock_guard lock(streams_mutex); auto it = streams.find(key); @@ -110,15 +111,14 @@ void RTMPManager::stream_loop(Camera cam, StreamType type) LOG_INFO("[RTMP] " + key + " is streaming."); bool stop_flag = false; - GstMessage *msg = nullptr; while (!stop_flag) { - // 等待消息,但设置 100ms 超时 - msg = gst_bus_timed_pop_filtered( + GstMessage *msg = gst_bus_timed_pop_filtered( bus, 100 * GST_MSECOND, static_cast(GST_MESSAGE_ERROR | GST_MESSAGE_EOS)); + // 每次循环检查 stop 标志 { std::lock_guard lock(streams_mutex); auto it = streams.find(key); @@ -144,12 +144,14 @@ void RTMPManager::stream_loop(Camera cam, StreamType type) g_error_free(err); if (debug) g_free(debug); + gst_message_unref(msg); stop_flag = true; } else if (GST_MESSAGE_TYPE(msg) == GST_MESSAGE_EOS) { LOG_WARN("[RTMP] EOS on " + key); update_status(key, {false, StreamResult::EOS_RECEIVED, "EOS"}); + gst_message_unref(msg); stop_flag = true; } @@ -161,6 +163,14 @@ void RTMPManager::stream_loop(Camera cam, StreamType type) gst_object_unref(bus); gst_object_unref(pipeline); + // 如果线程被 stop_camera 停掉了,不再重试 + { + std::lock_guard lock(streams_mutex); + auto it = streams.find(key); + if (it == streams.end() || !it->second->running.load()) + break; + } + if (!stop_flag) break; @@ -170,8 +180,6 @@ void RTMPManager::stream_loop(Camera cam, StreamType type) break; } - if (!ctx->running.load()) - break; LOG_WARN("[RTMP] Reconnecting " + key + " in 1s..."); std::this_thread::sleep_for(std::chrono::seconds(1)); }