2025-09-09 13:28:05 +08:00
|
|
|
|
// rtsp_manager.cpp
|
2025-09-08 14:55:07 +08:00
|
|
|
|
#include "rtsp_manager.hpp"
|
2025-11-21 15:32:56 +08:00
|
|
|
|
|
2025-11-24 16:37:53 +08:00
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
|
#include <linux/videodev2.h>
|
2025-12-17 14:13:52 +08:00
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
#include <chrono>
|
|
|
|
|
|
#include <iostream>
|
|
|
|
|
|
#include <thread>
|
|
|
|
|
|
|
|
|
|
|
|
#include "logger.hpp"
|
2025-09-08 14:55:07 +08:00
|
|
|
|
|
2025-09-09 09:57:24 +08:00
|
|
|
|
// 静态变量定义
|
2025-12-17 14:13:52 +08:00
|
|
|
|
GMainLoop* RTSPManager::loop = nullptr;
|
|
|
|
|
|
GMainContext* RTSPManager::main_context = nullptr;
|
|
|
|
|
|
GstRTSPServer* RTSPManager::server = nullptr;
|
2025-11-21 15:32:56 +08:00
|
|
|
|
|
2025-09-09 11:02:41 +08:00
|
|
|
|
std::unordered_map<std::string, bool> RTSPManager::streaming_status;
|
2025-12-17 14:13:52 +08:00
|
|
|
|
std::unordered_map<std::string, GstRTSPMediaFactory*> RTSPManager::mounted_factories;
|
2025-09-09 11:02:41 +08:00
|
|
|
|
std::mutex RTSPManager::mounted_factories_mutex;
|
2025-11-21 15:32:56 +08:00
|
|
|
|
|
2025-12-17 14:13:52 +08:00
|
|
|
|
std::unordered_map<std::string, std::vector<GstRTSPMedia*>> RTSPManager::media_map;
|
2025-09-09 13:28:05 +08:00
|
|
|
|
std::mutex RTSPManager::media_map_mutex;
|
2025-09-09 10:30:50 +08:00
|
|
|
|
|
2025-12-17 14:13:52 +08:00
|
|
|
|
bool set_v4l2_format(const std::string& dev, int width, int height)
|
2025-11-24 16:37:53 +08:00
|
|
|
|
{
|
|
|
|
|
|
int fd = open(dev.c_str(), O_RDWR);
|
|
|
|
|
|
if (fd < 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
LOG_ERROR("Failed to open " + dev);
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
struct v4l2_format fmt;
|
2025-11-25 08:54:26 +08:00
|
|
|
|
memset(&fmt, 0, sizeof(fmt));
|
|
|
|
|
|
fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
|
|
|
|
|
|
|
2025-11-25 09:21:08 +08:00
|
|
|
|
// 读格式,若相同则略过
|
2025-11-25 08:54:26 +08:00
|
|
|
|
if (ioctl(fd, VIDIOC_G_FMT, &fmt) == 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
bool match = true;
|
2025-12-17 14:13:52 +08:00
|
|
|
|
if (fmt.fmt.pix_mp.width != (unsigned int)width || fmt.fmt.pix_mp.height != (unsigned int)height ||
|
2025-11-25 08:54:26 +08:00
|
|
|
|
fmt.fmt.pix_mp.pixelformat != V4L2_PIX_FMT_NV12)
|
|
|
|
|
|
{
|
|
|
|
|
|
match = false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (match)
|
|
|
|
|
|
{
|
|
|
|
|
|
close(fd);
|
2025-12-17 14:13:52 +08:00
|
|
|
|
LOG_INFO("[RTSP] V4L2 format already NV12 " + std::to_string(width) + "x" + std::to_string(height) +
|
|
|
|
|
|
" for " + dev);
|
2025-11-25 08:54:26 +08:00
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-11-24 16:37:53 +08:00
|
|
|
|
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;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-11-25 08:54:26 +08:00
|
|
|
|
LOG_INFO("[RTSP] Set V4L2 format to NV12 " + std::to_string(width) + "x" + std::to_string(height) + " for " + dev);
|
2025-11-24 16:37:53 +08:00
|
|
|
|
close(fd);
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-08 14:55:07 +08:00
|
|
|
|
void RTSPManager::init()
|
|
|
|
|
|
{
|
|
|
|
|
|
gst_init(nullptr, nullptr);
|
|
|
|
|
|
LOG_INFO("[RTSP] GStreamer initialized.");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-17 14:13:52 +08:00
|
|
|
|
GstRTSPMediaFactory* RTSPManager::create_media_factory(const Camera& cam)
|
2025-09-08 14:55:07 +08:00
|
|
|
|
{
|
2025-11-25 09:13:52 +08:00
|
|
|
|
// 启动前把 v4l2 格式设成我们想要的
|
2025-11-25 09:02:06 +08:00
|
|
|
|
set_v4l2_format(cam.device, cam.width, cam.height);
|
2025-11-24 16:37:53 +08:00
|
|
|
|
|
2025-11-25 09:02:06 +08:00
|
|
|
|
int w = cam.width;
|
|
|
|
|
|
int h = cam.height;
|
2025-09-16 18:19:42 +08:00
|
|
|
|
|
2025-11-24 17:36:32 +08:00
|
|
|
|
std::string caps =
|
|
|
|
|
|
"video/x-raw,format=NV12,"
|
|
|
|
|
|
"width=" +
|
2025-12-17 14:13:52 +08:00
|
|
|
|
std::to_string(w) + ",height=" + std::to_string(h) + ",framerate=" + std::to_string(cam.fps) + "/1";
|
2025-11-24 17:36:32 +08:00
|
|
|
|
|
2025-11-25 09:27:20 +08:00
|
|
|
|
// 关键:tee 一路给 RTSP,一路给 fakesink,保持 v4l2src / mpph264enc 永远在跑
|
|
|
|
|
|
//
|
|
|
|
|
|
// 结构:
|
|
|
|
|
|
// v4l2src -> caps -> tee name=t
|
|
|
|
|
|
// tee src0: t. -> queue -> mpph264enc -> h264parse -> rtph264pay (给 RTSP)
|
|
|
|
|
|
// tee src1: t. -> queue -> fakesink (假消费者,让 DMA 持续 dequeue)
|
2025-12-17 14:13:52 +08:00
|
|
|
|
std::string launch_str = "( v4l2src device=" + cam.device +
|
|
|
|
|
|
" io-mode=2 is-live=true do-timestamp=true"
|
|
|
|
|
|
" ! " +
|
|
|
|
|
|
caps +
|
|
|
|
|
|
" ! tee name=t "
|
|
|
|
|
|
" ! 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) +
|
|
|
|
|
|
" header-mode=1"
|
|
|
|
|
|
" ! h264parse"
|
|
|
|
|
|
" ! rtph264pay name=pay0 pt=96 config-interval=1 "
|
|
|
|
|
|
" t. ! queue leaky=downstream max-size-buffers=3 max-size-bytes=0 max-size-time=0"
|
|
|
|
|
|
" ! fakesink sync=false async=false )";
|
2025-11-25 08:54:26 +08:00
|
|
|
|
|
2025-11-25 09:13:52 +08:00
|
|
|
|
LOG_INFO("[RTSP] Launch for " + cam.name + ": " + launch_str);
|
2025-09-08 14:55:07 +08:00
|
|
|
|
|
2025-12-17 14:13:52 +08:00
|
|
|
|
GstRTSPMediaFactory* factory = gst_rtsp_media_factory_new();
|
2025-09-08 14:55:07 +08:00
|
|
|
|
gst_rtsp_media_factory_set_launch(factory, launch_str.c_str());
|
2025-11-25 08:54:26 +08:00
|
|
|
|
|
2025-11-25 09:27:20 +08:00
|
|
|
|
// 先保持每个客户端独立 pipeline,逻辑简单可靠
|
2025-11-25 09:35:09 +08:00
|
|
|
|
gst_rtsp_media_factory_set_shared(factory, TRUE);
|
2025-11-24 17:36:32 +08:00
|
|
|
|
|
2025-11-25 09:27:20 +08:00
|
|
|
|
// 客户端断开时不要乱 reset,交给我们自己处理 / 或干脆不动
|
2025-11-24 17:36:32 +08:00
|
|
|
|
gst_rtsp_media_factory_set_suspend_mode(factory, GST_RTSP_SUSPEND_MODE_NONE);
|
2025-11-21 16:08:26 +08:00
|
|
|
|
|
2025-12-17 14:13:52 +08:00
|
|
|
|
g_signal_connect_data(factory, "media-configure", G_CALLBACK(on_media_created), g_strdup(cam.name.c_str()),
|
|
|
|
|
|
(GClosureNotify)g_free, (GConnectFlags)0);
|
2025-09-09 13:28:05 +08:00
|
|
|
|
|
2025-09-08 14:55:07 +08:00
|
|
|
|
return factory;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-17 14:13:52 +08:00
|
|
|
|
void RTSPManager::start(const std::vector<Camera>& cams)
|
2025-09-08 14:55:07 +08:00
|
|
|
|
{
|
|
|
|
|
|
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-11-24 15:01:56 +08:00
|
|
|
|
// 先获取 mountpoints
|
2025-12-17 14:13:52 +08:00
|
|
|
|
GstRTSPMountPoints* mounts = gst_rtsp_server_get_mount_points(server);
|
2025-11-24 15:01:56 +08:00
|
|
|
|
|
|
|
|
|
|
// 在这里统一挂载所有 enabled 摄像头
|
2025-12-17 14:13:52 +08:00
|
|
|
|
for (const auto& cam : cams)
|
2025-11-24 15:01:56 +08:00
|
|
|
|
{
|
2025-12-17 14:13:52 +08:00
|
|
|
|
if (!cam.enabled) continue;
|
2025-11-24 15:01:56 +08:00
|
|
|
|
|
2025-12-17 14:13:52 +08:00
|
|
|
|
GstRTSPMediaFactory* factory = create_media_factory(cam);
|
2025-11-24 15:01:56 +08:00
|
|
|
|
std::string mount_point = "/" + cam.name;
|
|
|
|
|
|
|
|
|
|
|
|
gst_rtsp_mount_points_add_factory(mounts, mount_point.c_str(), factory);
|
|
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
std::lock_guard<std::mutex> lock(mounted_factories_mutex);
|
|
|
|
|
|
mounted_factories[cam.name] = factory;
|
|
|
|
|
|
streaming_status[cam.name] = true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-17 14:13:52 +08:00
|
|
|
|
LOG_INFO("[RTSP] Camera '" + cam.name + "' mounted at rtsp://0.0.0.0:8554" + mount_point);
|
2025-11-24 15:01:56 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
g_object_unref(mounts);
|
|
|
|
|
|
|
2025-09-08 14:55:07 +08:00
|
|
|
|
gst_rtsp_server_attach(server, nullptr);
|
|
|
|
|
|
|
2025-11-24 15:01:56 +08:00
|
|
|
|
LOG_INFO("[RTSP] Server running on rtsp://0.0.0.0:8554");
|
2025-09-08 14:55:07 +08:00
|
|
|
|
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;
|
|
|
|
|
|
}
|
2025-09-09 13:38:23 +08:00
|
|
|
|
|
2025-09-08 15:51:10 +08:00
|
|
|
|
LOG_INFO("[RTSP] Server stopped.");
|
2025-09-08 14:55:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-17 14:13:52 +08:00
|
|
|
|
void RTSPManager::on_media_created(GstRTSPMediaFactory*, GstRTSPMedia* media, gpointer user_data)
|
2025-09-09 13:28:05 +08:00
|
|
|
|
{
|
2025-12-17 14:13:52 +08:00
|
|
|
|
const char* cam_name = static_cast<const char*>(user_data);
|
2025-11-21 15:32:56 +08:00
|
|
|
|
LOG_INFO(std::string("[RTSP] media-configure for camera: ") + cam_name);
|
|
|
|
|
|
|
2025-12-17 14:13:52 +08:00
|
|
|
|
// ✅ 只记录指针,不 g_object_ref(生命周期归 gst-rtsp-server)
|
2025-09-09 13:38:23 +08:00
|
|
|
|
{
|
|
|
|
|
|
std::lock_guard<std::mutex> lock(media_map_mutex);
|
|
|
|
|
|
media_map[cam_name].push_back(media);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-17 14:13:52 +08:00
|
|
|
|
g_signal_connect_data(media, "unprepared", G_CALLBACK(on_media_unprepared), g_strdup(cam_name),
|
|
|
|
|
|
(GClosureNotify)g_free, (GConnectFlags)0);
|
2025-09-09 13:28:05 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-17 14:13:52 +08:00
|
|
|
|
void RTSPManager::on_media_unprepared(GstRTSPMedia* media, gpointer user_data)
|
2025-09-09 13:28:05 +08:00
|
|
|
|
{
|
2025-12-17 14:13:52 +08:00
|
|
|
|
const char* cam_name = static_cast<const char*>(user_data);
|
2025-11-21 15:32:56 +08:00
|
|
|
|
LOG_INFO(std::string("[RTSP] media-unprepared: ") + cam_name);
|
|
|
|
|
|
|
2025-12-17 14:13:52 +08:00
|
|
|
|
std::lock_guard<std::mutex> lock(media_map_mutex);
|
|
|
|
|
|
auto it = media_map.find(cam_name);
|
|
|
|
|
|
if (it != media_map.end())
|
2025-09-09 13:28:05 +08:00
|
|
|
|
{
|
2025-12-17 14:13:52 +08:00
|
|
|
|
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);
|
2025-09-09 13:28:05 +08:00
|
|
|
|
}
|
2025-11-21 14:52:53 +08:00
|
|
|
|
|
2025-12-17 14:13:52 +08:00
|
|
|
|
// ✅ 不要 g_object_unref(media) —— gst-rtsp-server 会处理
|
2025-09-09 13:28:05 +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-12-17 14:13:52 +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-12-17 14:13:52 +08:00
|
|
|
|
GstRTSPMountPoints* mounts = gst_rtsp_server_get_mount_points(server);
|
2025-09-09 10:30:50 +08:00
|
|
|
|
|
|
|
|
|
|
std::string mount_point = "/" + cam->name;
|
2025-12-17 14:13:52 +08:00
|
|
|
|
GstRTSPMediaFactory* factory = create_media_factory(*cam);
|
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);
|
2025-11-21 15:32:56 +08:00
|
|
|
|
|
2025-09-09 10:30:50 +08:00
|
|
|
|
delete cam;
|
|
|
|
|
|
return G_SOURCE_REMOVE;
|
2025-09-09 09:57:24 +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-12-17 14:13:52 +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-11-21 15:32:56 +08:00
|
|
|
|
|
2025-09-09 13:28:05 +08:00
|
|
|
|
std::string cam_name = cam->name;
|
|
|
|
|
|
std::string mount_point = "/" + cam_name;
|
|
|
|
|
|
|
2025-12-17 14:13:52 +08:00
|
|
|
|
// ✅ 不要手动 gst_rtsp_media_unprepare / set_state(NULL)
|
|
|
|
|
|
// 只移除我们自己的记录。media 的 teardown 交给 gst-rtsp-server。
|
2025-09-09 13:38:23 +08:00
|
|
|
|
{
|
|
|
|
|
|
std::lock_guard<std::mutex> lock(media_map_mutex);
|
2025-12-17 14:13:52 +08:00
|
|
|
|
media_map.erase(cam_name);
|
2025-09-09 13:38:23 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-11-21 15:32:56 +08:00
|
|
|
|
// 移除 factory
|
2025-12-17 14:13:52 +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-12-17 14:13:52 +08:00
|
|
|
|
// ⚠️ 这里是否 unref factory 取决于 mount_points 是否持有引用。
|
|
|
|
|
|
// 一般 remove_factory 后会释放其引用;你再 unref 可能导致过度 unref。
|
|
|
|
|
|
// 为了稳,建议:先别手动 unref,避免 double-unref。
|
|
|
|
|
|
// if (it->second) g_object_unref(it->second);
|
|
|
|
|
|
|
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
|
|
|
|
|
2025-12-17 14:13:52 +08:00
|
|
|
|
void RTSPManager::mount_camera(const Camera& cam)
|
2025-09-09 11:00:57 +08:00
|
|
|
|
{
|
2025-12-17 14:13:52 +08:00
|
|
|
|
Camera* camCopy = new Camera(cam);
|
|
|
|
|
|
g_main_context_invoke(
|
|
|
|
|
|
main_context, [](gpointer data) -> gboolean { return RTSPManager::mount_camera_in_main(data); }, camCopy);
|
2025-09-09 11:00:57 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-17 14:13:52 +08:00
|
|
|
|
void RTSPManager::unmount_camera(const Camera& cam)
|
2025-09-09 11:00:57 +08:00
|
|
|
|
{
|
2025-12-17 14:13:52 +08:00
|
|
|
|
Camera* camCopy = new Camera(cam);
|
|
|
|
|
|
g_main_context_invoke(
|
|
|
|
|
|
main_context, [](gpointer data) -> gboolean { return RTSPManager::unmount_camera_in_main(data); }, camCopy);
|
2025-09-09 11:00:57 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-17 14:13:52 +08:00
|
|
|
|
bool RTSPManager::is_streaming(const std::string& cam_name)
|
2025-09-09 11:00:57 +08:00
|
|
|
|
{
|
|
|
|
|
|
std::lock_guard<std::mutex> lock(mounted_factories_mutex);
|
|
|
|
|
|
auto it = streaming_status.find(cam_name);
|
|
|
|
|
|
return it != streaming_status.end() ? it->second : false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-11-21 15:32:56 +08:00
|
|
|
|
bool RTSPManager::is_any_streaming()
|
|
|
|
|
|
{
|
|
|
|
|
|
std::lock_guard<std::mutex> lock(mounted_factories_mutex);
|
2025-12-17 14:13:52 +08:00
|
|
|
|
for (auto& kv : streaming_status)
|
|
|
|
|
|
if (kv.second) return true;
|
2025-11-21 15:32:56 +08:00
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-09 11:00:57 +08:00
|
|
|
|
void RTSPManager::stop()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (loop)
|
|
|
|
|
|
{
|
2025-11-20 11:06:02 +08:00
|
|
|
|
g_main_context_invoke(
|
|
|
|
|
|
main_context,
|
|
|
|
|
|
[](gpointer data) -> gboolean
|
|
|
|
|
|
{
|
2025-12-17 14:13:52 +08:00
|
|
|
|
g_main_loop_quit(static_cast<GMainLoop*>(data));
|
2025-11-20 11:06:02 +08:00
|
|
|
|
return G_SOURCE_REMOVE;
|
|
|
|
|
|
},
|
|
|
|
|
|
loop);
|
2025-09-09 11:00:57 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|