// rtsp_manager.cpp #include "rtsp_manager.hpp" #include "logger.hpp" #include #include #include #include #include #include #include // 静态变量定义 GMainLoop *RTSPManager::loop = nullptr; GMainContext *RTSPManager::main_context = nullptr; GstRTSPServer *RTSPManager::server = nullptr; std::unordered_map RTSPManager::streaming_status; std::unordered_map RTSPManager::mounted_factories; std::mutex RTSPManager::mounted_factories_mutex; std::unordered_map> RTSPManager::media_map; std::mutex RTSPManager::media_map_mutex; bool set_v4l2_format(const std::string &dev, int width, int height) { int fd = open(dev.c_str(), O_RDWR); if (fd < 0) { LOG_ERROR("Failed to open " + dev); return false; } struct v4l2_format fmt; memset(&fmt, 0, sizeof(fmt)); fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; // 先读当前格式,避免每次都硬 S_FMT if (ioctl(fd, VIDIOC_G_FMT, &fmt) == 0) { bool match = true; if (fmt.fmt.pix_mp.width != (unsigned int)width || fmt.fmt.pix_mp.height != (unsigned int)height || fmt.fmt.pix_mp.pixelformat != V4L2_PIX_FMT_NV12) { match = false; } if (match) { close(fd); LOG_INFO("[RTSP] V4L2 format already NV12 " + std::to_string(width) + "x" + std::to_string(height) + " for " + dev); return true; } } memset(&fmt, 0, sizeof(fmt)); fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; fmt.fmt.pix_mp.width = width; fmt.fmt.pix_mp.height = height; fmt.fmt.pix_mp.pixelformat = V4L2_PIX_FMT_NV12; fmt.fmt.pix_mp.num_planes = 1; if (ioctl(fd, VIDIOC_S_FMT, &fmt) < 0) { LOG_ERROR("VIDIOC_S_FMT failed for " + dev); close(fd); return false; } LOG_INFO("[RTSP] Set V4L2 format to NV12 " + std::to_string(width) + "x" + std::to_string(height) + " for " + dev); close(fd); return true; } void RTSPManager::init() { gst_init(nullptr, nullptr); LOG_INFO("[RTSP] GStreamer initialized."); } GstRTSPMediaFactory *RTSPManager::create_media_factory(const Camera &cam) { // 启动前把 v4l2 格式设成我们想要的 set_v4l2_format(cam.device, cam.width, cam.height); int w = cam.width; int h = cam.height; std::string caps = "video/x-raw,format=NV12," "width=" + std::to_string(w) + ",height=" + std::to_string(h) + ",framerate=" + std::to_string(cam.fps) + "/1"; // 注意几点: // 1) 给 mpph264enc 起一个名字 name=enc,方便后面在 on_media_created 里拿到 // 2) option-force-idr / option-idr-interval 依然在这里设置一遍,保证周期 IDR // 3) config-interval=1 一定要写在 rtph264pay 上 std::string launch_str = "( v4l2src device=" + cam.device + " io-mode=2 is-live=true do-timestamp=true" " ! " + caps + " ! queue leaky=downstream max-size-time=0 max-size-bytes=0 max-size-buffers=0" " ! mpph264enc name=enc" " rc-mode=cbr" " bps=" + std::to_string(cam.bitrate) + " gop=" + std::to_string(cam.fps) + " option-force-idr=true" " option-idr-interval=" + std::to_string(cam.fps) + " header-mode=1" " ! h264parse" " ! rtph264pay name=pay0 pt=96 config-interval=1 )"; LOG_INFO("[RTSP] Launch for " + cam.name + ": " + launch_str); GstRTSPMediaFactory *factory = gst_rtsp_media_factory_new(); gst_rtsp_media_factory_set_launch(factory, launch_str.c_str()); // 先保持 shared=TRUE,节省资源 gst_rtsp_media_factory_set_shared(factory, TRUE); // 客户端断开时不重置 pipeline,防止反复 s_stream(0/1) gst_rtsp_media_factory_set_suspend_mode(factory, GST_RTSP_SUSPEND_MODE_NONE); g_signal_connect_data(factory, "media-configure", G_CALLBACK(on_media_created), g_strdup(cam.name.c_str()), (GClosureNotify)g_free, (GConnectFlags)0); return factory; } void RTSPManager::start(const std::vector &cams) { server = gst_rtsp_server_new(); gst_rtsp_server_set_service(server, "8554"); 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://0.0.0.0:8554"); g_main_loop_run(loop); if (server) { g_object_unref(server); server = nullptr; } if (loop) { g_main_loop_unref(loop); loop = nullptr; } LOG_INFO("[RTSP] Server stopped."); } void RTSPManager::on_media_created(GstRTSPMediaFactory *, GstRTSPMedia *media, gpointer user_data) { const char *cam_name = static_cast(user_data); LOG_INFO(std::string("[RTSP] media-configure for camera: ") + cam_name); // 把 media 保存起来,方便后面 unmount g_object_ref(media); { std::lock_guard lock(media_map_mutex); media_map[cam_name].push_back(media); } g_signal_connect_data( media, "unprepared", G_CALLBACK(on_media_unprepared), g_strdup(cam_name), (GClosureNotify)g_free, (GConnectFlags)0); // 获取底层 pipeline GstElement *pipeline = gst_rtsp_media_get_element(media); if (!pipeline) { LOG_ERROR(std::string("[RTSP] Pipeline is NULL for camera: ") + cam_name); return; } // ★★★ 关键:拿到 encoder 元素,强制打一发 IDR,保证 VLC 第一次连接就能拿到关键帧 GstElement *enc = gst_bin_get_by_name(GST_BIN(pipeline), "enc"); if (enc) { LOG_INFO(std::string("[RTSP] Forcing IDR for camera: ") + cam_name); // 这里用和 launch 里同名的属性,确保 Rockchip mpph264enc 能识别 g_object_set(enc, "option-force-idr", TRUE, NULL); // 如果你想更狠一点,也可以顺便再 set 一次 idr-interval // g_object_set(enc, "option-idr-interval", cam.fps, NULL); // 这里需要你把 fps 传进来才行 gst_object_unref(enc); } else { LOG_WARN(std::string("[RTSP] Encoder 'enc' not found in pipeline for camera: ") + cam_name); } // 强制 pipeline 进入 PLAYING,避免停在 PAUSED/preroll GstStateChangeReturn ret = gst_element_set_state(pipeline, GST_STATE_PLAYING); if (ret == GST_STATE_CHANGE_FAILURE) { LOG_ERROR(std::string("[RTSP] Failed to set pipeline PLAYING for camera: ") + cam_name); } else { LOG_INFO(std::string("[RTSP] Force pipeline PLAYING for camera: ") + cam_name); } // gst_rtsp_media_get_element() 返回的是加过 ref 的对象,需要 unref 一次 gst_object_unref(pipeline); } void RTSPManager::on_media_unprepared(GstRTSPMedia *media, gpointer user_data) { const char *cam_name = static_cast(user_data); LOG_INFO(std::string("[RTSP] media-unprepared: ") + cam_name); { std::lock_guard lock(media_map_mutex); auto it = media_map.find(cam_name); if (it != media_map.end()) { auto &vec = it->second; vec.erase(std::remove(vec.begin(), vec.end(), media), vec.end()); if (vec.empty()) media_map.erase(it); } else { LOG_WARN(std::string("[RTSP] media-unprepared but no entry in media_map for camera: ") + cam_name); } } g_object_unref(media); } gboolean RTSPManager::mount_camera_in_main(gpointer data) { Camera *cam = static_cast(data); if (!cam || !server) { delete cam; return G_SOURCE_REMOVE; } GstRTSPMountPoints *mounts = gst_rtsp_server_get_mount_points(server); std::string mount_point = "/" + cam->name; GstRTSPMediaFactory *factory = create_media_factory(*cam); gst_rtsp_mount_points_add_factory(mounts, mount_point.c_str(), factory); g_object_unref(mounts); { 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://localhost:8554" + mount_point); delete cam; return G_SOURCE_REMOVE; } gboolean RTSPManager::unmount_camera_in_main(gpointer data) { Camera *cam = static_cast(data); if (!cam || !server) { delete cam; return G_SOURCE_REMOVE; } std::string cam_name = cam->name; std::string mount_point = "/" + cam_name; // 停掉 media { std::lock_guard lock(media_map_mutex); auto it = media_map.find(cam_name); if (it != media_map.end()) { for (GstRTSPMedia *media : it->second) { GstElement *pipeline = gst_rtsp_media_get_element(media); if (pipeline) { gst_element_set_state(pipeline, GST_STATE_NULL); gst_object_unref(pipeline); } gst_rtsp_media_unprepare(media); } it->second.clear(); media_map.erase(it); } } // 移除 factory GstRTSPMountPoints *mounts = gst_rtsp_server_get_mount_points(server); if (mounts) { gst_rtsp_mount_points_remove_factory(mounts, mount_point.c_str()); g_object_unref(mounts); } { std::lock_guard lock(mounted_factories_mutex); auto it = mounted_factories.find(cam_name); if (it != mounted_factories.end()) { if (it->second) g_object_unref(it->second); mounted_factories.erase(it); } streaming_status[cam_name] = false; } LOG_INFO("[RTSP] Camera '" + cam_name + "' unmounted."); delete cam; return G_SOURCE_REMOVE; } void RTSPManager::mount_camera(const Camera &cam) { Camera *camCopy = new Camera(cam); g_main_context_invoke(main_context, [](gpointer data) -> gboolean { return RTSPManager::mount_camera_in_main(data); }, camCopy); } void RTSPManager::unmount_camera(const Camera &cam) { Camera *camCopy = new Camera(cam); g_main_context_invoke(main_context, [](gpointer data) -> gboolean { return RTSPManager::unmount_camera_in_main(data); }, camCopy); } bool RTSPManager::is_streaming(const std::string &cam_name) { std::lock_guard lock(mounted_factories_mutex); auto it = streaming_status.find(cam_name); return it != streaming_status.end() ? it->second : false; } bool RTSPManager::is_any_streaming() { std::lock_guard lock(mounted_factories_mutex); for (auto &kv : streaming_status) if (kv.second) return true; return false; } void RTSPManager::stop() { if (loop) { g_main_context_invoke( main_context, [](gpointer data) -> gboolean { g_main_loop_quit(static_cast(data)); return G_SOURCE_REMOVE; }, loop); } }