From c850b2d2eec73c07af6b4472f61efcb959f7e0c6 Mon Sep 17 00:00:00 2001 From: cxh Date: Mon, 24 Nov 2025 15:01:56 +0800 Subject: [PATCH] 1 --- src/main.cpp | 10 ---------- src/rtsp_manager.cpp | 30 ++++++++++++++++++++++++++++-- 2 files changed, 28 insertions(+), 12 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index ab21729..5b1b636 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -49,16 +49,6 @@ int main() // 初始化 GStreamer RTSPManager::init(); - // ⭐ 程序启动 → 强制 mount 所有摄像头(RTSP 永远在线) - for (const auto &cam : g_app_config.cameras) - { - if (cam.enabled) - { - RTSPManager::mount_camera(cam); - LOG_INFO("[INIT] Mounted camera: " + cam.name); - } - } - std::thread rtsp_thread; std::thread mqtt_thread; diff --git a/src/rtsp_manager.cpp b/src/rtsp_manager.cpp index 1a53dd4..4fcfe84 100644 --- a/src/rtsp_manager.cpp +++ b/src/rtsp_manager.cpp @@ -58,7 +58,7 @@ GstRTSPMediaFactory *RTSPManager::create_media_factory(const Camera &cam) return factory; } -void RTSPManager::start(const std::vector &) +void RTSPManager::start(const std::vector &cams) { server = gst_rtsp_server_new(); gst_rtsp_server_set_service(server, "8554"); @@ -66,9 +66,35 @@ void RTSPManager::start(const std::vector &) loop = g_main_loop_new(nullptr, FALSE); main_context = g_main_loop_get_context(loop); + // 先获取 mountpoints + GstRTSPMountPoints *mounts = gst_rtsp_server_get_mount_points(server); + + // 在这里统一挂载所有 enabled 摄像头 + for (const auto &cam : cams) + { + if (!cam.enabled) + continue; + + GstRTSPMediaFactory *factory = create_media_factory(cam); + std::string mount_point = "/" + cam.name; + + gst_rtsp_mount_points_add_factory(mounts, mount_point.c_str(), factory); + + { + std::lock_guard lock(mounted_factories_mutex); + mounted_factories[cam.name] = factory; + streaming_status[cam.name] = true; + } + + LOG_INFO("[RTSP] Camera '" + cam.name + + "' mounted at rtsp://0.0.0.0:8554" + mount_point); + } + + g_object_unref(mounts); + gst_rtsp_server_attach(server, nullptr); - LOG_INFO("[RTSP] Server running on rtsp://localhost:8554"); + LOG_INFO("[RTSP] Server running on rtsp://0.0.0.0:8554"); g_main_loop_run(loop); if (server)