1
This commit is contained in:
parent
259efde6ca
commit
9017939660
@ -1,17 +1,17 @@
|
|||||||
// mqtt_client_wrapper.cpp
|
// mqtt_client_wrapper.cpp
|
||||||
#include "mqtt_client_wrapper.hpp"
|
#include "mqtt_client_wrapper.hpp"
|
||||||
#include <thread>
|
|
||||||
|
#include <algorithm>
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
#include <nlohmann/json.hpp>
|
#include <nlohmann/json.hpp>
|
||||||
#include <algorithm>
|
#include <thread>
|
||||||
|
|
||||||
std::shared_ptr<MQTTClient> mqtt_client;
|
std::shared_ptr<MQTTClient> mqtt_client;
|
||||||
extern std::atomic<bool> g_running;
|
extern std::atomic<bool> g_running;
|
||||||
|
|
||||||
static void send_heartbeat()
|
static void send_heartbeat()
|
||||||
{
|
{
|
||||||
if (!mqtt_client || !mqtt_client->isConnected())
|
if (!mqtt_client || !mqtt_client->isConnected()) return;
|
||||||
return;
|
|
||||||
|
|
||||||
nlohmann::json hb_data;
|
nlohmann::json hb_data;
|
||||||
hb_data["time"] = Logger::get_current_time_utc8();
|
hb_data["time"] = Logger::get_current_time_utc8();
|
||||||
@ -40,10 +40,7 @@ static void on_mqtt_connected()
|
|||||||
}
|
}
|
||||||
|
|
||||||
// MQTT 连接断开
|
// MQTT 连接断开
|
||||||
static void on_mqtt_disconnected()
|
static void on_mqtt_disconnected() { LOG_WARN("[MQTT] Disconnected from broker: " + g_app_config.mqtt.server_ip); }
|
||||||
{
|
|
||||||
LOG_WARN("[MQTT] Disconnected from broker: " + g_app_config.mqtt.server_ip);
|
|
||||||
}
|
|
||||||
|
|
||||||
// 处理消息
|
// 处理消息
|
||||||
static void on_mqtt_message_received(const std::string& topic, const std::string& message)
|
static void on_mqtt_message_received(const std::string& topic, const std::string& message)
|
||||||
@ -119,7 +116,7 @@ void mqtt_client_thread_func()
|
|||||||
|
|
||||||
while (g_running)
|
while (g_running)
|
||||||
{
|
{
|
||||||
mqtt_client = std::make_unique<MQTTClient>(cfg);
|
mqtt_client = std::make_shared<MQTTClient>(cfg);
|
||||||
mqtt_client->setConnectCallback(on_mqtt_connected);
|
mqtt_client->setConnectCallback(on_mqtt_connected);
|
||||||
mqtt_client->setDisconnectCallback(on_mqtt_disconnected);
|
mqtt_client->setDisconnectCallback(on_mqtt_disconnected);
|
||||||
mqtt_client->setMessageCallback(on_mqtt_message_received);
|
mqtt_client->setMessageCallback(on_mqtt_message_received);
|
||||||
@ -154,8 +151,7 @@ void mqtt_client_thread_func()
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 重连间隔
|
// 重连间隔
|
||||||
for (int i = 0; i < 5 && g_running; i++)
|
for (int i = 0; i < 5 && g_running; i++) std::this_thread::sleep_for(std::chrono::milliseconds(200));
|
||||||
std::this_thread::sleep_for(std::chrono::milliseconds(200));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
LOG_INFO("[MQTT] Client thread exiting.");
|
LOG_INFO("[MQTT] Client thread exiting.");
|
||||||
|
|||||||
@ -1,14 +1,16 @@
|
|||||||
// rtsp_manager.cpp
|
// rtsp_manager.cpp
|
||||||
#include "rtsp_manager.hpp"
|
#include "rtsp_manager.hpp"
|
||||||
#include "logger.hpp"
|
|
||||||
|
|
||||||
#include <iostream>
|
|
||||||
#include <algorithm>
|
|
||||||
#include <thread>
|
|
||||||
#include <chrono>
|
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <sys/ioctl.h>
|
|
||||||
#include <linux/videodev2.h>
|
#include <linux/videodev2.h>
|
||||||
|
#include <sys/ioctl.h>
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
|
#include <chrono>
|
||||||
|
#include <iostream>
|
||||||
|
#include <thread>
|
||||||
|
|
||||||
|
#include "logger.hpp"
|
||||||
|
|
||||||
// 静态变量定义
|
// 静态变量定义
|
||||||
GMainLoop* RTSPManager::loop = nullptr;
|
GMainLoop* RTSPManager::loop = nullptr;
|
||||||
@ -39,8 +41,7 @@ bool set_v4l2_format(const std::string &dev, int width, int height)
|
|||||||
if (ioctl(fd, VIDIOC_G_FMT, &fmt) == 0)
|
if (ioctl(fd, VIDIOC_G_FMT, &fmt) == 0)
|
||||||
{
|
{
|
||||||
bool match = true;
|
bool match = true;
|
||||||
if (fmt.fmt.pix_mp.width != (unsigned int)width ||
|
if (fmt.fmt.pix_mp.width != (unsigned int)width || fmt.fmt.pix_mp.height != (unsigned int)height ||
|
||||||
fmt.fmt.pix_mp.height != (unsigned int)height ||
|
|
||||||
fmt.fmt.pix_mp.pixelformat != V4L2_PIX_FMT_NV12)
|
fmt.fmt.pix_mp.pixelformat != V4L2_PIX_FMT_NV12)
|
||||||
{
|
{
|
||||||
match = false;
|
match = false;
|
||||||
@ -49,7 +50,8 @@ bool set_v4l2_format(const std::string &dev, int width, int height)
|
|||||||
if (match)
|
if (match)
|
||||||
{
|
{
|
||||||
close(fd);
|
close(fd);
|
||||||
LOG_INFO("[RTSP] V4L2 format already NV12 " + std::to_string(width) + "x" + std::to_string(height) + " for " + dev);
|
LOG_INFO("[RTSP] V4L2 format already NV12 " + std::to_string(width) + "x" + std::to_string(height) +
|
||||||
|
" for " + dev);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -90,9 +92,7 @@ GstRTSPMediaFactory *RTSPManager::create_media_factory(const Camera &cam)
|
|||||||
std::string caps =
|
std::string caps =
|
||||||
"video/x-raw,format=NV12,"
|
"video/x-raw,format=NV12,"
|
||||||
"width=" +
|
"width=" +
|
||||||
std::to_string(w) +
|
std::to_string(w) + ",height=" + std::to_string(h) + ",framerate=" + std::to_string(cam.fps) + "/1";
|
||||||
",height=" + std::to_string(h) +
|
|
||||||
",framerate=" + std::to_string(cam.fps) + "/1";
|
|
||||||
|
|
||||||
// 关键:tee 一路给 RTSP,一路给 fakesink,保持 v4l2src / mpph264enc 永远在跑
|
// 关键:tee 一路给 RTSP,一路给 fakesink,保持 v4l2src / mpph264enc 永远在跑
|
||||||
//
|
//
|
||||||
@ -100,16 +100,14 @@ GstRTSPMediaFactory *RTSPManager::create_media_factory(const Camera &cam)
|
|||||||
// v4l2src -> caps -> tee name=t
|
// v4l2src -> caps -> tee name=t
|
||||||
// tee src0: t. -> queue -> mpph264enc -> h264parse -> rtph264pay (给 RTSP)
|
// tee src0: t. -> queue -> mpph264enc -> h264parse -> rtph264pay (给 RTSP)
|
||||||
// tee src1: t. -> queue -> fakesink (假消费者,让 DMA 持续 dequeue)
|
// tee src1: t. -> queue -> fakesink (假消费者,让 DMA 持续 dequeue)
|
||||||
std::string launch_str =
|
std::string launch_str = "( v4l2src device=" + cam.device +
|
||||||
"( v4l2src device=" + cam.device +
|
|
||||||
" io-mode=2 is-live=true do-timestamp=true"
|
" io-mode=2 is-live=true do-timestamp=true"
|
||||||
" ! " +
|
" ! " +
|
||||||
caps +
|
caps +
|
||||||
" ! tee name=t "
|
" ! tee name=t "
|
||||||
" ! queue leaky=downstream max-size-time=0 max-size-bytes=0 max-size-buffers=0"
|
" ! queue leaky=downstream max-size-time=0 max-size-bytes=0 max-size-buffers=0"
|
||||||
" ! mpph264enc name=enc rc-mode=cbr bps=" +
|
" ! mpph264enc name=enc rc-mode=cbr bps=" +
|
||||||
std::to_string(cam.bitrate) +
|
std::to_string(cam.bitrate) + " gop=" + std::to_string(cam.fps) +
|
||||||
" gop=" + std::to_string(cam.fps) +
|
|
||||||
" header-mode=1"
|
" header-mode=1"
|
||||||
" ! h264parse"
|
" ! h264parse"
|
||||||
" ! rtph264pay name=pay0 pt=96 config-interval=1 "
|
" ! rtph264pay name=pay0 pt=96 config-interval=1 "
|
||||||
@ -127,12 +125,8 @@ GstRTSPMediaFactory *RTSPManager::create_media_factory(const Camera &cam)
|
|||||||
// 客户端断开时不要乱 reset,交给我们自己处理 / 或干脆不动
|
// 客户端断开时不要乱 reset,交给我们自己处理 / 或干脆不动
|
||||||
gst_rtsp_media_factory_set_suspend_mode(factory, GST_RTSP_SUSPEND_MODE_NONE);
|
gst_rtsp_media_factory_set_suspend_mode(factory, GST_RTSP_SUSPEND_MODE_NONE);
|
||||||
|
|
||||||
g_signal_connect_data(factory,
|
g_signal_connect_data(factory, "media-configure", G_CALLBACK(on_media_created), g_strdup(cam.name.c_str()),
|
||||||
"media-configure",
|
(GClosureNotify)g_free, (GConnectFlags)0);
|
||||||
G_CALLBACK(on_media_created),
|
|
||||||
g_strdup(cam.name.c_str()),
|
|
||||||
(GClosureNotify)g_free,
|
|
||||||
(GConnectFlags)0);
|
|
||||||
|
|
||||||
return factory;
|
return factory;
|
||||||
}
|
}
|
||||||
@ -151,8 +145,7 @@ void RTSPManager::start(const std::vector<Camera> &cams)
|
|||||||
// 在这里统一挂载所有 enabled 摄像头
|
// 在这里统一挂载所有 enabled 摄像头
|
||||||
for (const auto& cam : cams)
|
for (const auto& cam : cams)
|
||||||
{
|
{
|
||||||
if (!cam.enabled)
|
if (!cam.enabled) continue;
|
||||||
continue;
|
|
||||||
|
|
||||||
GstRTSPMediaFactory* factory = create_media_factory(cam);
|
GstRTSPMediaFactory* factory = create_media_factory(cam);
|
||||||
std::string mount_point = "/" + cam.name;
|
std::string mount_point = "/" + cam.name;
|
||||||
@ -165,8 +158,7 @@ void RTSPManager::start(const std::vector<Camera> &cams)
|
|||||||
streaming_status[cam.name] = true;
|
streaming_status[cam.name] = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
LOG_INFO("[RTSP] Camera '" + cam.name +
|
LOG_INFO("[RTSP] Camera '" + cam.name + "' mounted at rtsp://0.0.0.0:8554" + mount_point);
|
||||||
"' mounted at rtsp://0.0.0.0:8554" + mount_point);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
g_object_unref(mounts);
|
g_object_unref(mounts);
|
||||||
@ -193,51 +185,37 @@ void RTSPManager::start(const std::vector<Camera> &cams)
|
|||||||
void RTSPManager::on_media_created(GstRTSPMediaFactory*, GstRTSPMedia* media, gpointer user_data)
|
void RTSPManager::on_media_created(GstRTSPMediaFactory*, GstRTSPMedia* media, gpointer user_data)
|
||||||
{
|
{
|
||||||
const char* cam_name = static_cast<const char*>(user_data);
|
const char* cam_name = static_cast<const char*>(user_data);
|
||||||
|
|
||||||
LOG_INFO(std::string("[RTSP] media-configure for camera: ") + cam_name);
|
LOG_INFO(std::string("[RTSP] media-configure for camera: ") + cam_name);
|
||||||
|
|
||||||
// 保存 media,用于之后 unmount 时统一管理
|
// ✅ 只记录指针,不 g_object_ref(生命周期归 gst-rtsp-server)
|
||||||
g_object_ref(media);
|
|
||||||
{
|
{
|
||||||
std::lock_guard<std::mutex> lock(media_map_mutex);
|
std::lock_guard<std::mutex> lock(media_map_mutex);
|
||||||
media_map[cam_name].push_back(media);
|
media_map[cam_name].push_back(media);
|
||||||
}
|
}
|
||||||
|
|
||||||
g_signal_connect_data(
|
g_signal_connect_data(media, "unprepared", G_CALLBACK(on_media_unprepared), g_strdup(cam_name),
|
||||||
media,
|
(GClosureNotify)g_free, (GConnectFlags)0);
|
||||||
"unprepared",
|
|
||||||
G_CALLBACK(on_media_unprepared),
|
|
||||||
g_strdup(cam_name),
|
|
||||||
(GClosureNotify)g_free,
|
|
||||||
(GConnectFlags)0);
|
|
||||||
|
|
||||||
// 不再手动改 pipeline 状态,交给 gst-rtsp-server 自己管理
|
|
||||||
// 如果后面发现确实有卡在 PAUSED 的情况,再在这里加逻辑
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void RTSPManager::on_media_unprepared(GstRTSPMedia* media, gpointer user_data)
|
void RTSPManager::on_media_unprepared(GstRTSPMedia* media, gpointer user_data)
|
||||||
{
|
{
|
||||||
const char* cam_name = static_cast<const char*>(user_data);
|
const char* cam_name = static_cast<const char*>(user_data);
|
||||||
|
|
||||||
LOG_INFO(std::string("[RTSP] media-unprepared: ") + cam_name);
|
LOG_INFO(std::string("[RTSP] media-unprepared: ") + cam_name);
|
||||||
|
|
||||||
{
|
|
||||||
std::lock_guard<std::mutex> lock(media_map_mutex);
|
std::lock_guard<std::mutex> lock(media_map_mutex);
|
||||||
auto it = media_map.find(cam_name);
|
auto it = media_map.find(cam_name);
|
||||||
if (it != media_map.end())
|
if (it != media_map.end())
|
||||||
{
|
{
|
||||||
auto& vec = it->second;
|
auto& vec = it->second;
|
||||||
vec.erase(std::remove(vec.begin(), vec.end(), media), vec.end());
|
vec.erase(std::remove(vec.begin(), vec.end(), media), vec.end());
|
||||||
if (vec.empty())
|
if (vec.empty()) media_map.erase(it);
|
||||||
media_map.erase(it);
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
LOG_WARN(std::string("[RTSP] media-unprepared but no entry in media_map for camera: ") + cam_name);
|
LOG_WARN(std::string("[RTSP] media-unprepared but no entry in media_map for camera: ") + cam_name);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
g_object_unref(media);
|
// ✅ 不要 g_object_unref(media) —— gst-rtsp-server 会处理
|
||||||
}
|
}
|
||||||
|
|
||||||
gboolean RTSPManager::mount_camera_in_main(gpointer data)
|
gboolean RTSPManager::mount_camera_in_main(gpointer data)
|
||||||
@ -281,27 +259,11 @@ gboolean RTSPManager::unmount_camera_in_main(gpointer data)
|
|||||||
std::string cam_name = cam->name;
|
std::string cam_name = cam->name;
|
||||||
std::string mount_point = "/" + cam_name;
|
std::string mount_point = "/" + cam_name;
|
||||||
|
|
||||||
// 停掉 media
|
// ✅ 不要手动 gst_rtsp_media_unprepare / set_state(NULL)
|
||||||
|
// 只移除我们自己的记录。media 的 teardown 交给 gst-rtsp-server。
|
||||||
{
|
{
|
||||||
std::lock_guard<std::mutex> lock(media_map_mutex);
|
std::lock_guard<std::mutex> lock(media_map_mutex);
|
||||||
auto it = media_map.find(cam_name);
|
media_map.erase(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
|
// 移除 factory
|
||||||
@ -317,8 +279,11 @@ gboolean RTSPManager::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())
|
||||||
{
|
{
|
||||||
if (it->second)
|
// ⚠️ 这里是否 unref factory 取决于 mount_points 是否持有引用。
|
||||||
g_object_unref(it->second);
|
// 一般 remove_factory 后会释放其引用;你再 unref 可能导致过度 unref。
|
||||||
|
// 为了稳,建议:先别手动 unref,避免 double-unref。
|
||||||
|
// if (it->second) g_object_unref(it->second);
|
||||||
|
|
||||||
mounted_factories.erase(it);
|
mounted_factories.erase(it);
|
||||||
}
|
}
|
||||||
streaming_status[cam_name] = false;
|
streaming_status[cam_name] = false;
|
||||||
@ -332,15 +297,15 @@ gboolean RTSPManager::unmount_camera_in_main(gpointer data)
|
|||||||
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(
|
||||||
{ return RTSPManager::mount_camera_in_main(data); }, camCopy);
|
main_context, [](gpointer data) -> gboolean { 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(
|
||||||
{ return RTSPManager::unmount_camera_in_main(data); }, camCopy);
|
main_context, [](gpointer data) -> gboolean { 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)
|
||||||
@ -354,8 +319,7 @@ bool RTSPManager::is_any_streaming()
|
|||||||
{
|
{
|
||||||
std::lock_guard<std::mutex> lock(mounted_factories_mutex);
|
std::lock_guard<std::mutex> lock(mounted_factories_mutex);
|
||||||
for (auto& kv : streaming_status)
|
for (auto& kv : streaming_status)
|
||||||
if (kv.second)
|
if (kv.second) return true;
|
||||||
return true;
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user