first commit
This commit is contained in:
parent
9efe5a4ed7
commit
1c42d3d243
@ -4,9 +4,10 @@
|
|||||||
#include <gst/gst.h>
|
#include <gst/gst.h>
|
||||||
#include <gst/rtsp-server/rtsp-server.h>
|
#include <gst/rtsp-server/rtsp-server.h>
|
||||||
#include "app_config.hpp"
|
#include "app_config.hpp"
|
||||||
|
#include <unordered_map>
|
||||||
|
#include <string>
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
|
|
||||||
// RTSP 管理器,负责启动/关闭 RTSP 服务器
|
|
||||||
class RTSPManager
|
class RTSPManager
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
@ -14,21 +15,24 @@ public:
|
|||||||
static void start(const std::vector<Camera> &cameras);
|
static void start(const std::vector<Camera> &cameras);
|
||||||
static void stop();
|
static void stop();
|
||||||
|
|
||||||
// === 按需挂载/卸载摄像头 ===
|
|
||||||
static void mount_camera(const Camera &cam);
|
static void mount_camera(const Camera &cam);
|
||||||
static void unmount_camera(const Camera &cam);
|
static void unmount_camera(const Camera &cam);
|
||||||
|
|
||||||
// === 查询播放状态 ===
|
|
||||||
static bool is_streaming(const std::string &cam_name);
|
static bool is_streaming(const std::string &cam_name);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
static GMainLoop *loop;
|
static GMainLoop *loop;
|
||||||
static GMainContext *main_context; // 新增:保存 loop 的 context
|
static GMainContext *main_context;
|
||||||
static GstRTSPServer *server;
|
static GstRTSPServer *server;
|
||||||
|
|
||||||
// 播放状态表
|
|
||||||
static std::unordered_map<std::string, bool> streaming_status;
|
static std::unordered_map<std::string, bool> streaming_status;
|
||||||
|
|
||||||
// 创建 MediaFactory
|
// 工厂创建函数
|
||||||
static GstRTSPMediaFactory *create_media_factory(const Camera &cam);
|
static GstRTSPMediaFactory *create_media_factory(const Camera &cam);
|
||||||
|
|
||||||
|
// --- 把挂载/卸载函数变成私有静态成员 ---
|
||||||
|
static gboolean mount_camera_in_main(gpointer data);
|
||||||
|
static gboolean unmount_camera_in_main(gpointer data);
|
||||||
|
|
||||||
|
// --- 静态 mutex 和工厂表 ---
|
||||||
|
static std::unordered_map<std::string, GstRTSPMediaFactory *> mounted_factories;
|
||||||
|
static std::mutex mounted_factories_mutex;
|
||||||
};
|
};
|
||||||
|
|||||||
@ -1,17 +1,14 @@
|
|||||||
#include "rtsp_manager.hpp"
|
#include "rtsp_manager.hpp"
|
||||||
#include "logger.hpp"
|
#include "logger.hpp"
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <mutex>
|
|
||||||
|
|
||||||
// 静态变量定义
|
// 静态变量定义
|
||||||
GMainLoop *RTSPManager::loop = nullptr;
|
GMainLoop *RTSPManager::loop = nullptr;
|
||||||
GMainContext *RTSPManager::main_context = nullptr; // 新增
|
GMainContext *RTSPManager::main_context = nullptr;
|
||||||
GstRTSPServer *RTSPManager::server = nullptr;
|
GstRTSPServer *RTSPManager::server = nullptr;
|
||||||
std::unordered_map<std::string, bool> RTSPManager::streaming_status; // 播放状态表
|
std::unordered_map<std::string, bool> RTSPManager::streaming_status;
|
||||||
|
std::unordered_map<std::string, GstRTSPMediaFactory *> RTSPManager::mounted_factories;
|
||||||
// 保存已挂载的 factory,方便卸载时找到并释放
|
std::mutex RTSPManager::mounted_factories_mutex;
|
||||||
static std::unordered_map<std::string, GstRTSPMediaFactory *> mounted_factories;
|
|
||||||
static std::mutex mounted_factories_mutex;
|
|
||||||
|
|
||||||
void RTSPManager::init()
|
void RTSPManager::init()
|
||||||
{
|
{
|
||||||
@ -31,7 +28,6 @@ GstRTSPMediaFactory *RTSPManager::create_media_factory(const Camera &cam)
|
|||||||
GstRTSPMediaFactory *factory = gst_rtsp_media_factory_new();
|
GstRTSPMediaFactory *factory = gst_rtsp_media_factory_new();
|
||||||
gst_rtsp_media_factory_set_launch(factory, launch_str.c_str());
|
gst_rtsp_media_factory_set_launch(factory, launch_str.c_str());
|
||||||
gst_rtsp_media_factory_set_shared(factory, TRUE);
|
gst_rtsp_media_factory_set_shared(factory, TRUE);
|
||||||
|
|
||||||
return factory;
|
return factory;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -40,9 +36,8 @@ void RTSPManager::start(const std::vector<Camera> &cameras)
|
|||||||
server = gst_rtsp_server_new();
|
server = gst_rtsp_server_new();
|
||||||
gst_rtsp_server_set_service(server, "8554");
|
gst_rtsp_server_set_service(server, "8554");
|
||||||
|
|
||||||
// 不在启动时挂载摄像头,由调度指令控制推流
|
|
||||||
loop = g_main_loop_new(nullptr, FALSE);
|
loop = g_main_loop_new(nullptr, FALSE);
|
||||||
main_context = g_main_loop_get_context(loop); // 保存 context
|
main_context = g_main_loop_get_context(loop);
|
||||||
|
|
||||||
gst_rtsp_server_attach(server, nullptr);
|
gst_rtsp_server_attach(server, nullptr);
|
||||||
|
|
||||||
@ -62,19 +57,17 @@ void RTSPManager::start(const std::vector<Camera> &cameras)
|
|||||||
LOG_INFO("[RTSP] Server stopped.");
|
LOG_INFO("[RTSP] Server stopped.");
|
||||||
}
|
}
|
||||||
|
|
||||||
// --- 主线程挂载函数 ---
|
// --- 静态成员实现 ---
|
||||||
static gboolean mount_camera_in_main(gpointer data)
|
gboolean RTSPManager::mount_camera_in_main(gpointer data)
|
||||||
{
|
{
|
||||||
Camera *cam = static_cast<Camera *>(data);
|
Camera *cam = static_cast<Camera *>(data);
|
||||||
if (!cam)
|
if (!cam || !server)
|
||||||
return G_SOURCE_REMOVE;
|
|
||||||
if (!RTSPManager::server)
|
|
||||||
{
|
{
|
||||||
delete cam;
|
delete cam;
|
||||||
return G_SOURCE_REMOVE;
|
return G_SOURCE_REMOVE;
|
||||||
}
|
}
|
||||||
|
|
||||||
GstRTSPMountPoints *mounts = gst_rtsp_server_get_mount_points(RTSPManager::server);
|
GstRTSPMountPoints *mounts = gst_rtsp_server_get_mount_points(server);
|
||||||
if (!mounts)
|
if (!mounts)
|
||||||
{
|
{
|
||||||
delete cam;
|
delete cam;
|
||||||
@ -82,7 +75,7 @@ static gboolean mount_camera_in_main(gpointer data)
|
|||||||
}
|
}
|
||||||
|
|
||||||
std::string mount_point = "/" + cam->name;
|
std::string mount_point = "/" + cam->name;
|
||||||
GstRTSPMediaFactory *factory = RTSPManager::create_media_factory(*cam);
|
GstRTSPMediaFactory *factory = create_media_factory(*cam);
|
||||||
if (!factory)
|
if (!factory)
|
||||||
{
|
{
|
||||||
g_object_unref(mounts);
|
g_object_unref(mounts);
|
||||||
@ -96,7 +89,7 @@ static gboolean mount_camera_in_main(gpointer data)
|
|||||||
{
|
{
|
||||||
std::lock_guard<std::mutex> lock(mounted_factories_mutex);
|
std::lock_guard<std::mutex> lock(mounted_factories_mutex);
|
||||||
mounted_factories[cam->name] = factory;
|
mounted_factories[cam->name] = factory;
|
||||||
RTSPManager::streaming_status[cam->name] = true;
|
streaming_status[cam->name] = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
LOG_INFO("[RTSP] Camera '" + cam->name + "' mounted at rtsp://localhost:8554" + mount_point);
|
LOG_INFO("[RTSP] Camera '" + cam->name + "' mounted at rtsp://localhost:8554" + mount_point);
|
||||||
@ -104,19 +97,16 @@ static gboolean mount_camera_in_main(gpointer data)
|
|||||||
return G_SOURCE_REMOVE;
|
return G_SOURCE_REMOVE;
|
||||||
}
|
}
|
||||||
|
|
||||||
// --- 主线程卸载函数 ---
|
gboolean RTSPManager::unmount_camera_in_main(gpointer data)
|
||||||
static gboolean unmount_camera_in_main(gpointer data)
|
|
||||||
{
|
{
|
||||||
Camera *cam = static_cast<Camera *>(data);
|
Camera *cam = static_cast<Camera *>(data);
|
||||||
if (!cam)
|
if (!cam || !server)
|
||||||
return G_SOURCE_REMOVE;
|
|
||||||
if (!RTSPManager::server)
|
|
||||||
{
|
{
|
||||||
delete cam;
|
delete cam;
|
||||||
return G_SOURCE_REMOVE;
|
return G_SOURCE_REMOVE;
|
||||||
}
|
}
|
||||||
|
|
||||||
GstRTSPMountPoints *mounts = gst_rtsp_server_get_mount_points(RTSPManager::server);
|
GstRTSPMountPoints *mounts = gst_rtsp_server_get_mount_points(server);
|
||||||
if (!mounts)
|
if (!mounts)
|
||||||
{
|
{
|
||||||
delete cam;
|
delete cam;
|
||||||
@ -132,12 +122,11 @@ static gboolean unmount_camera_in_main(gpointer data)
|
|||||||
auto it = mounted_factories.find(cam->name);
|
auto it = mounted_factories.find(cam->name);
|
||||||
if (it != mounted_factories.end())
|
if (it != mounted_factories.end())
|
||||||
{
|
{
|
||||||
GstRTSPMediaFactory *factory = it->second;
|
if (it->second)
|
||||||
if (factory)
|
g_object_unref(it->second);
|
||||||
g_object_unref(factory);
|
|
||||||
mounted_factories.erase(it);
|
mounted_factories.erase(it);
|
||||||
}
|
}
|
||||||
RTSPManager::streaming_status[cam->name] = false;
|
streaming_status[cam->name] = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
LOG_INFO("[RTSP] Camera '" + cam->name + "' unmounted.");
|
LOG_INFO("[RTSP] Camera '" + cam->name + "' unmounted.");
|
||||||
@ -145,23 +134,20 @@ static gboolean unmount_camera_in_main(gpointer data)
|
|||||||
return G_SOURCE_REMOVE;
|
return G_SOURCE_REMOVE;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 公共接口:挂载摄像头(线程安全)
|
|
||||||
void RTSPManager::mount_camera(const Camera &cam)
|
void RTSPManager::mount_camera(const Camera &cam)
|
||||||
{
|
{
|
||||||
Camera *camCopy = new Camera(cam);
|
Camera *camCopy = new Camera(cam);
|
||||||
g_main_context_invoke(main_context, [](gpointer data) -> gboolean
|
g_main_context_invoke(main_context, [](gpointer data) -> gboolean
|
||||||
{ return mount_camera_in_main(data); }, camCopy);
|
{ return RTSPManager::mount_camera_in_main(data); }, camCopy);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 公共接口:卸载摄像头(线程安全)
|
|
||||||
void RTSPManager::unmount_camera(const Camera &cam)
|
void RTSPManager::unmount_camera(const Camera &cam)
|
||||||
{
|
{
|
||||||
Camera *camCopy = new Camera(cam);
|
Camera *camCopy = new Camera(cam);
|
||||||
g_main_context_invoke(main_context, [](gpointer data) -> gboolean
|
g_main_context_invoke(main_context, [](gpointer data) -> gboolean
|
||||||
{ return unmount_camera_in_main(data); }, camCopy);
|
{ return RTSPManager::unmount_camera_in_main(data); }, camCopy);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 查询播放状态
|
|
||||||
bool RTSPManager::is_streaming(const std::string &cam_name)
|
bool RTSPManager::is_streaming(const std::string &cam_name)
|
||||||
{
|
{
|
||||||
std::lock_guard<std::mutex> lock(mounted_factories_mutex);
|
std::lock_guard<std::mutex> lock(mounted_factories_mutex);
|
||||||
@ -175,8 +161,7 @@ void RTSPManager::stop()
|
|||||||
{
|
{
|
||||||
g_main_context_invoke(main_context, [](gpointer data) -> gboolean
|
g_main_context_invoke(main_context, [](gpointer data) -> gboolean
|
||||||
{
|
{
|
||||||
GMainLoop *loop = static_cast<GMainLoop *>(data);
|
g_main_loop_quit(static_cast<GMainLoop *>(data));
|
||||||
g_main_loop_quit(loop);
|
|
||||||
return G_SOURCE_REMOVE; }, loop);
|
return G_SOURCE_REMOVE; }, loop);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user