This commit is contained in:
cxh 2025-10-15 10:52:29 +08:00
parent acfcbda026
commit 185d1dd952

View File

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