sweeper_video/src/rtsp_manager.cpp

73 lines
2.0 KiB
C++
Raw Normal View History

2025-09-08 14:55:07 +08:00
// rtsp_manager.cpp
#include "rtsp_manager.hpp"
#include "logger.hpp"
#include <iostream>
GMainLoop *RTSPManager::loop = nullptr;
GstRTSPServer *RTSPManager::server = nullptr;
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);
return factory;
}
void RTSPManager::start(const std::vector<Camera> &cameras)
{
server = gst_rtsp_server_new();
gst_rtsp_server_set_service(server, "8554");
GstRTSPMountPoints *mounts = gst_rtsp_server_get_mount_points(server);
for (auto &cam : cameras)
{
if (!cam.enabled)
continue;
auto factory = create_media_factory(cam);
std::string mount_point = "/" + cam.name;
gst_rtsp_mount_points_add_factory(mounts, mount_point.c_str(), factory);
LOG_INFO("[RTSP] Camera '" + cam.name + "' available at rtsp://localhost:8554" + mount_point);
}
g_object_unref(mounts);
loop = g_main_loop_new(nullptr, FALSE);
gst_rtsp_server_attach(server, nullptr);
LOG_INFO("[RTSP] Server running on rtsp://localhost:8554");
g_main_loop_run(loop);
}
void RTSPManager::stop()
{
if (server)
{
g_object_unref(server);
server = nullptr;
}
if (loop)
{
g_main_loop_unref(loop);
loop = nullptr;
}
LOG_INFO("[RTSP] Server stopped.");
}