2025-09-09 13:28:05 +08:00
|
|
|
// rtsp_manager.cpp
|
2025-09-08 14:55:07 +08:00
|
|
|
#include "rtsp_manager.hpp"
|
|
|
|
|
#include "logger.hpp"
|
|
|
|
|
#include <iostream>
|
|
|
|
|
|
2025-09-09 09:57:24 +08:00
|
|
|
// 静态变量定义
|
2025-09-08 14:55:07 +08:00
|
|
|
GMainLoop *RTSPManager::loop = nullptr;
|
2025-09-09 11:02:41 +08:00
|
|
|
GMainContext *RTSPManager::main_context = nullptr;
|
2025-09-08 14:55:07 +08:00
|
|
|
GstRTSPServer *RTSPManager::server = nullptr;
|
2025-09-09 11:02:41 +08:00
|
|
|
std::unordered_map<std::string, bool> RTSPManager::streaming_status;
|
|
|
|
|
std::unordered_map<std::string, GstRTSPMediaFactory *> RTSPManager::mounted_factories;
|
|
|
|
|
std::mutex RTSPManager::mounted_factories_mutex;
|
2025-09-09 13:28:05 +08:00
|
|
|
std::unordered_map<std::string, std::vector<GstRTSPMedia *>> RTSPManager::media_map;
|
|
|
|
|
std::mutex RTSPManager::media_map_mutex;
|
2025-09-09 10:30:50 +08:00
|
|
|
|
2025-09-08 14:55:07 +08:00
|
|
|
void RTSPManager::init()
|
|
|
|
|
{
|
|
|
|
|
gst_init(nullptr, nullptr);
|
|
|
|
|
LOG_INFO("[RTSP] GStreamer initialized.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
GstRTSPMediaFactory *RTSPManager::create_media_factory(const Camera &cam)
|
|
|
|
|
{
|
|
|
|
|
std::string launch_str =
|
|
|
|
|
"( v4l2src device=" + cam.device +
|
|
|
|
|
" ! video/x-raw,format=NV12,width=" + std::to_string(cam.width) +
|
|
|
|
|
",height=" + std::to_string(cam.height) +
|
|
|
|
|
",framerate=" + std::to_string(cam.fps) + "/1"
|
|
|
|
|
" ! videoconvert ! queue ! mpph264enc ! rtph264pay name=pay0 pt=96 )";
|
|
|
|
|
|
|
|
|
|
GstRTSPMediaFactory *factory = gst_rtsp_media_factory_new();
|
|
|
|
|
gst_rtsp_media_factory_set_launch(factory, launch_str.c_str());
|
|
|
|
|
gst_rtsp_media_factory_set_shared(factory, TRUE);
|
2025-09-09 13:28:05 +08:00
|
|
|
|
|
|
|
|
// 连接 media-created 信号,跟踪媒体对象
|
|
|
|
|
g_signal_connect_data(factory, "media-created", G_CALLBACK(on_media_created), g_strdup(cam.name.c_str()),
|
|
|
|
|
(GClosureNotify)g_free, 0);
|
|
|
|
|
|
2025-09-08 14:55:07 +08:00
|
|
|
return factory;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void RTSPManager::start(const std::vector<Camera> &cameras)
|
|
|
|
|
{
|
|
|
|
|
server = gst_rtsp_server_new();
|
|
|
|
|
gst_rtsp_server_set_service(server, "8554");
|
|
|
|
|
|
|
|
|
|
loop = g_main_loop_new(nullptr, FALSE);
|
2025-09-09 11:02:41 +08:00
|
|
|
main_context = g_main_loop_get_context(loop);
|
2025-09-09 11:00:57 +08:00
|
|
|
|
2025-09-08 14:55:07 +08:00
|
|
|
gst_rtsp_server_attach(server, nullptr);
|
|
|
|
|
|
|
|
|
|
LOG_INFO("[RTSP] Server running on rtsp://localhost:8554");
|
|
|
|
|
g_main_loop_run(loop);
|
2025-09-08 15:51:10 +08:00
|
|
|
|
|
|
|
|
if (server)
|
|
|
|
|
{
|
|
|
|
|
g_object_unref(server);
|
|
|
|
|
server = nullptr;
|
|
|
|
|
}
|
|
|
|
|
if (loop)
|
|
|
|
|
{
|
|
|
|
|
g_main_loop_unref(loop);
|
|
|
|
|
loop = nullptr;
|
|
|
|
|
}
|
|
|
|
|
LOG_INFO("[RTSP] Server stopped.");
|
2025-09-08 14:55:07 +08:00
|
|
|
}
|
|
|
|
|
|
2025-09-09 13:28:05 +08:00
|
|
|
// 媒体创建信号处理函数
|
|
|
|
|
void RTSPManager::on_media_created(GstRTSPMediaFactory *factory, GstRTSPMedia *media, gpointer user_data)
|
|
|
|
|
{
|
|
|
|
|
const char *cam_name = static_cast<const char *>(user_data);
|
|
|
|
|
std::lock_guard<std::mutex> lock(media_map_mutex);
|
|
|
|
|
media_map[cam_name].push_back(media);
|
|
|
|
|
g_object_ref(media); // 增加引用计数,防止意外销毁
|
|
|
|
|
|
|
|
|
|
// 连接 unprepared 信号,以便在媒体销毁时从列表中移除
|
|
|
|
|
g_signal_connect_data(media, "unprepared", G_CALLBACK(on_media_unprepared), g_strdup(cam_name), (GClosureNotify)g_free, 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 媒体 unprepared 信号处理函数
|
|
|
|
|
void RTSPManager::on_media_unprepared(GstRTSPMedia *media, gpointer user_data)
|
|
|
|
|
{
|
|
|
|
|
const char *cam_name = static_cast<const char *>(user_data);
|
|
|
|
|
std::lock_guard<std::mutex> lock(media_map_mutex);
|
|
|
|
|
auto it = media_map.find(cam_name);
|
|
|
|
|
if (it != media_map.end())
|
|
|
|
|
{
|
|
|
|
|
auto &media_list = it->second;
|
|
|
|
|
media_list.erase(std::remove(media_list.begin(), media_list.end(), media), media_list.end());
|
|
|
|
|
if (media_list.empty())
|
|
|
|
|
{
|
|
|
|
|
media_map.erase(it);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
g_object_unref(media); // 释放我们在 on_media_created 中增加的引用
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-09 11:34:17 +08:00
|
|
|
// 挂载摄像头
|
2025-09-09 11:02:41 +08:00
|
|
|
gboolean RTSPManager::mount_camera_in_main(gpointer data)
|
2025-09-09 09:57:24 +08:00
|
|
|
{
|
2025-09-09 10:30:50 +08:00
|
|
|
Camera *cam = static_cast<Camera *>(data);
|
2025-09-09 11:02:41 +08:00
|
|
|
if (!cam || !server)
|
2025-09-09 10:30:50 +08:00
|
|
|
{
|
|
|
|
|
delete cam;
|
|
|
|
|
return G_SOURCE_REMOVE;
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-09 11:02:41 +08:00
|
|
|
GstRTSPMountPoints *mounts = gst_rtsp_server_get_mount_points(server);
|
2025-09-09 10:30:50 +08:00
|
|
|
if (!mounts)
|
|
|
|
|
{
|
|
|
|
|
delete cam;
|
|
|
|
|
return G_SOURCE_REMOVE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::string mount_point = "/" + cam->name;
|
2025-09-09 11:02:41 +08:00
|
|
|
GstRTSPMediaFactory *factory = create_media_factory(*cam);
|
2025-09-09 10:30:50 +08:00
|
|
|
if (!factory)
|
|
|
|
|
{
|
|
|
|
|
g_object_unref(mounts);
|
|
|
|
|
delete cam;
|
|
|
|
|
return G_SOURCE_REMOVE;
|
|
|
|
|
}
|
2025-09-09 09:57:24 +08:00
|
|
|
|
|
|
|
|
gst_rtsp_mount_points_add_factory(mounts, mount_point.c_str(), factory);
|
|
|
|
|
g_object_unref(mounts);
|
|
|
|
|
|
2025-09-09 10:30:50 +08:00
|
|
|
{
|
|
|
|
|
std::lock_guard<std::mutex> lock(mounted_factories_mutex);
|
|
|
|
|
mounted_factories[cam->name] = factory;
|
2025-09-09 11:02:41 +08:00
|
|
|
streaming_status[cam->name] = true;
|
2025-09-09 10:30:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
LOG_INFO("[RTSP] Camera '" + cam->name + "' mounted at rtsp://localhost:8554" + mount_point);
|
|
|
|
|
delete cam;
|
|
|
|
|
return G_SOURCE_REMOVE;
|
2025-09-09 09:57:24 +08:00
|
|
|
}
|
|
|
|
|
|
2025-09-09 13:28:05 +08:00
|
|
|
// 卸载摄像头(停止所有媒体会话)
|
2025-09-09 11:02:41 +08:00
|
|
|
gboolean RTSPManager::unmount_camera_in_main(gpointer data)
|
2025-09-09 09:57:24 +08:00
|
|
|
{
|
2025-09-09 10:30:50 +08:00
|
|
|
Camera *cam = static_cast<Camera *>(data);
|
2025-09-09 11:02:41 +08:00
|
|
|
if (!cam || !server)
|
2025-09-09 10:30:50 +08:00
|
|
|
{
|
|
|
|
|
delete cam;
|
|
|
|
|
return G_SOURCE_REMOVE;
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-09 13:28:05 +08:00
|
|
|
std::string cam_name = cam->name;
|
|
|
|
|
std::string mount_point = "/" + cam_name;
|
|
|
|
|
|
|
|
|
|
// 停止所有相关的媒体会话
|
|
|
|
|
{
|
|
|
|
|
std::lock_guard<std::mutex> lock(media_map_mutex);
|
|
|
|
|
auto it = media_map.find(cam_name);
|
|
|
|
|
if (it != media_map.end())
|
|
|
|
|
{
|
|
|
|
|
for (GstRTSPMedia *media : it->second)
|
|
|
|
|
{
|
|
|
|
|
gst_rtsp_media_stop(media);
|
|
|
|
|
}
|
|
|
|
|
it->second.clear();
|
|
|
|
|
media_map.erase(it);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-09 11:02:41 +08:00
|
|
|
GstRTSPMountPoints *mounts = gst_rtsp_server_get_mount_points(server);
|
2025-09-09 11:34:17 +08:00
|
|
|
if (mounts)
|
2025-09-09 10:30:50 +08:00
|
|
|
{
|
2025-09-09 11:34:17 +08:00
|
|
|
gst_rtsp_mount_points_remove_factory(mounts, mount_point.c_str());
|
|
|
|
|
g_object_unref(mounts);
|
2025-09-09 10:30:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
std::lock_guard<std::mutex> lock(mounted_factories_mutex);
|
2025-09-09 13:28:05 +08:00
|
|
|
auto it = mounted_factories.find(cam_name);
|
2025-09-09 10:30:50 +08:00
|
|
|
if (it != mounted_factories.end())
|
|
|
|
|
{
|
2025-09-09 13:10:20 +08:00
|
|
|
if (it->second && G_IS_OBJECT(it->second))
|
|
|
|
|
g_object_unref(it->second); // 安全 unref
|
2025-09-09 11:34:17 +08:00
|
|
|
mounted_factories.erase(it);
|
2025-09-09 10:30:50 +08:00
|
|
|
}
|
2025-09-09 13:28:05 +08:00
|
|
|
streaming_status[cam_name] = false;
|
2025-09-09 10:30:50 +08:00
|
|
|
}
|
|
|
|
|
|
2025-09-09 13:28:05 +08:00
|
|
|
LOG_INFO("[RTSP] Camera '" + cam_name + "' unmounted.");
|
2025-09-09 10:30:50 +08:00
|
|
|
delete cam;
|
|
|
|
|
return G_SOURCE_REMOVE;
|
|
|
|
|
}
|
2025-09-09 11:00:57 +08:00
|
|
|
|
|
|
|
|
void RTSPManager::mount_camera(const Camera &cam)
|
|
|
|
|
{
|
|
|
|
|
Camera *camCopy = new Camera(cam);
|
|
|
|
|
g_main_context_invoke(main_context, [](gpointer data) -> gboolean
|
2025-09-09 11:02:41 +08:00
|
|
|
{ return RTSPManager::mount_camera_in_main(data); }, camCopy);
|
2025-09-09 11:00:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void RTSPManager::unmount_camera(const Camera &cam)
|
|
|
|
|
{
|
|
|
|
|
Camera *camCopy = new Camera(cam);
|
|
|
|
|
g_main_context_invoke(main_context, [](gpointer data) -> gboolean
|
2025-09-09 11:02:41 +08:00
|
|
|
{ return RTSPManager::unmount_camera_in_main(data); }, camCopy);
|
2025-09-09 11:00:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool RTSPManager::is_streaming(const std::string &cam_name)
|
|
|
|
|
{
|
|
|
|
|
std::lock_guard<std::mutex> lock(mounted_factories_mutex);
|
|
|
|
|
auto it = streaming_status.find(cam_name);
|
|
|
|
|
return it != streaming_status.end() ? it->second : false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void RTSPManager::stop()
|
|
|
|
|
{
|
|
|
|
|
if (loop)
|
|
|
|
|
{
|
|
|
|
|
g_main_context_invoke(main_context, [](gpointer data) -> gboolean
|
|
|
|
|
{
|
2025-09-09 11:02:41 +08:00
|
|
|
g_main_loop_quit(static_cast<GMainLoop *>(data));
|
2025-09-09 11:00:57 +08:00
|
|
|
return G_SOURCE_REMOVE; }, loop);
|
|
|
|
|
}
|
|
|
|
|
}
|