优化循环逻辑

This commit is contained in:
cxh 2025-11-12 17:57:20 +08:00
parent fe68f9af7a
commit b125c8db42

View File

@ -112,9 +112,10 @@ GstElement* RTMPManager::create_pipeline(const Camera& cam)
void RTMPManager::stream_loop(Camera cam, StreamContext* ctx) void RTMPManager::stream_loop(Camera cam, StreamContext* ctx)
{ {
const std::string key = make_key(cam.name); const std::string key = make_key(cam.name);
while (ctx->running) while (ctx->running)
{ {
// 设备是否存在 // 1. 检查设备节点是否存在
struct stat st{}; struct stat st{};
if (stat(cam.device.c_str(), &st) != 0) if (stat(cam.device.c_str(), &st) != 0)
{ {
@ -125,7 +126,7 @@ void RTMPManager::stream_loop(Camera cam, StreamContext* ctx)
continue; continue;
} }
// 创建管线 // 2. 创建 GStreamer 管线
GstElement* pipeline = create_pipeline(cam); GstElement* pipeline = create_pipeline(cam);
if (!pipeline) if (!pipeline)
{ {
@ -137,13 +138,13 @@ void RTMPManager::stream_loop(Camera cam, StreamContext* ctx)
gst_element_set_name(pipeline, key.c_str()); gst_element_set_name(pipeline, key.c_str());
GstBus* bus = gst_element_get_bus(pipeline); GstBus* bus = gst_element_get_bus(pipeline);
// 在 videoconvert 的 src pad 上挂 probe 判断是否有帧流过 // 3. 在 v4l2src 的 src pad 上挂探测 probe
bool got_frame = false; bool got_frame = false;
{ {
GstElement* vc = gst_bin_get_by_name(GST_BIN(pipeline), "vc"); GstElement* src = gst_bin_get_by_name(GST_BIN(pipeline), "src");
if (vc) if (src)
{ {
GstPad* pad = gst_element_get_static_pad(vc, "src"); GstPad* pad = gst_element_get_static_pad(src, "src");
if (pad) if (pad)
{ {
gst_pad_add_probe( gst_pad_add_probe(
@ -158,39 +159,36 @@ void RTMPManager::stream_loop(Camera cam, StreamContext* ctx)
} }
else else
{ {
LOG_WARN("[RTMP] " + key + " - vc has no src pad?"); LOG_WARN("[RTMP] " + key + " - src has no 'src' pad?");
} }
gst_object_unref(vc); gst_object_unref(src);
} }
else else
{ {
LOG_WARN("[RTMP] " + key + " - cannot find element 'vc' for pad-probe"); LOG_WARN("[RTMP] " + key + " - cannot find element 'src' for pad-probe");
} }
} }
// 启动 // 4. 启动播放
LOG_INFO("[RTMP] Starting stream: " + key); LOG_INFO("[RTMP] Starting stream: " + key);
gst_element_set_state(pipeline, GST_STATE_PLAYING); gst_element_set_state(pipeline, GST_STATE_PLAYING);
// 等待进入 PLAYING最长 5s // 等待进入 PLAYING 状态(最长 5s
GstState state = GST_STATE_NULL, pending = GST_STATE_NULL;
bool confirmed_running = false; bool confirmed_running = false;
if (gst_element_get_state(pipeline, &state, &pending, 5 * GST_SECOND) == GST_STATE_CHANGE_SUCCESS &&
state == GST_STATE_PLAYING)
{ {
GstState state = GST_STATE_NULL, pending = GST_STATE_NULL; confirmed_running = true;
// 注意第三个参数单位是纳秒5s=5*GST_SECOND ctx->status.running = true;
if (gst_element_get_state(pipeline, &state, &pending, 5 * GST_SECOND) == GST_STATE_CHANGE_SUCCESS && ctx->status.last_error.clear();
state == GST_STATE_PLAYING) LOG_INFO("[RTMP] " + key + " confirmed PLAYING");
{ }
confirmed_running = true; else
ctx->status.running = true; {
ctx->status.last_error.clear(); ctx->status.running = false;
LOG_INFO("[RTMP] " + key + " confirmed PLAYING"); ctx->status.last_error = "Pipeline failed to confirm PLAYING";
} LOG_WARN("[RTMP] " + key + " - " + ctx->status.last_error);
else
{
ctx->status.running = false;
ctx->status.last_error = "Pipeline failed to confirm PLAYING";
LOG_WARN("[RTMP] " + key + " - " + ctx->status.last_error);
}
} }
if (!confirmed_running) if (!confirmed_running)
@ -202,13 +200,13 @@ void RTMPManager::stream_loop(Camera cam, StreamContext* ctx)
continue; continue;
} }
// 运行中5s 内必须看到帧,否则认定无信号重启 // 5. 运行阶段:监测帧和错误
const auto start_t = std::chrono::steady_clock::now(); const auto start_t = std::chrono::steady_clock::now();
bool need_restart = false; bool need_restart = false;
while (ctx->running) while (ctx->running)
{ {
// 检查帧 // 检查是否收到(前 5s 内)
if (!got_frame) if (!got_frame)
{ {
auto elapsed = auto elapsed =
@ -225,12 +223,11 @@ void RTMPManager::stream_loop(Camera cam, StreamContext* ctx)
} }
else else
{ {
// 一旦有帧,保持 running=true
ctx->status.running = true; ctx->status.running = true;
ctx->status.last_error.clear(); ctx->status.last_error.clear();
} }
// 监听错误/EOS // 等待错误或 EOS 消息
GstMessage* msg = gst_bus_timed_pop_filtered(bus, 200 * GST_MSECOND, GstMessage* msg = gst_bus_timed_pop_filtered(bus, 200 * GST_MSECOND,
(GstMessageType)(GST_MESSAGE_ERROR | GST_MESSAGE_EOS)); (GstMessageType)(GST_MESSAGE_ERROR | GST_MESSAGE_EOS));
@ -263,11 +260,12 @@ void RTMPManager::stream_loop(Camera cam, StreamContext* ctx)
if (need_restart) break; if (need_restart) break;
} }
// 收尾 // 6. 收尾清理
gst_element_set_state(pipeline, GST_STATE_NULL); gst_element_set_state(pipeline, GST_STATE_NULL);
if (bus) gst_object_unref(bus); if (bus) gst_object_unref(bus);
gst_object_unref(pipeline); gst_object_unref(pipeline);
// 7. 若仍在运行状态,准备重启
if (ctx->running) if (ctx->running)
{ {
LOG_WARN("[RTMP] Restarting " + key + " in 3s..."); LOG_WARN("[RTMP] Restarting " + key + " in 3s...");