2026-01-22 10:19:45 +08:00
|
|
|
|
// rtmp_manager.cpp (reliable integrated version)
|
|
|
|
|
|
// - record:启动时由配置决定(静态开关)
|
|
|
|
|
|
// - live:MQTT 动态开关(运行时可变)
|
|
|
|
|
|
// - 关键增强:PLAYING 成功后再“放行 valve”(避免 cold-start mux/sink 饿死)
|
|
|
|
|
|
// - 关键增强:rtmpsink 强制 IPv4(protocol=0),减少偶发握手卡住
|
|
|
|
|
|
// - 关键增强:更严谨的 cleanup + 保存/释放 valve 引用,避免泄漏/悬挂引用
|
|
|
|
|
|
|
2025-10-14 17:13:08 +08:00
|
|
|
|
#include "rtmp_manager.hpp"
|
2025-10-15 14:14:00 +08:00
|
|
|
|
|
2025-10-16 13:11:36 +08:00
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
|
|
#include <ifaddrs.h>
|
|
|
|
|
|
#include <netinet/in.h>
|
2025-10-16 14:37:47 +08:00
|
|
|
|
#include <sys/stat.h>
|
2025-10-16 13:11:36 +08:00
|
|
|
|
|
2025-10-16 10:25:51 +08:00
|
|
|
|
#include <chrono>
|
2025-10-16 13:11:36 +08:00
|
|
|
|
#include <cstring>
|
2026-01-20 13:47:30 +08:00
|
|
|
|
#include <memory>
|
2025-10-16 10:25:51 +08:00
|
|
|
|
#include <thread>
|
2025-10-15 10:18:02 +08:00
|
|
|
|
|
2026-01-20 13:47:30 +08:00
|
|
|
|
std::atomic<bool> RTMPManager::g_live_enabled{false};
|
2026-01-22 10:19:45 +08:00
|
|
|
|
// record 是“启动时决定一次”的静态开关:由 config.json 决定
|
2026-01-22 09:29:10 +08:00
|
|
|
|
bool RTMPManager::g_record_enabled = true;
|
2026-01-20 13:47:30 +08:00
|
|
|
|
|
2025-12-29 14:24:40 +08:00
|
|
|
|
// =======================================================
|
|
|
|
|
|
// 工具函数
|
|
|
|
|
|
// =======================================================
|
2025-11-12 17:47:56 +08:00
|
|
|
|
static bool device_exists(const std::string& path)
|
2025-10-17 15:52:47 +08:00
|
|
|
|
{
|
|
|
|
|
|
struct stat st;
|
2025-12-29 14:24:40 +08:00
|
|
|
|
return stat(path.c_str(), &st) == 0;
|
2025-10-17 15:52:47 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-29 14:24:40 +08:00
|
|
|
|
// 获取指定网卡 IPv4
|
2026-01-22 10:21:04 +08:00
|
|
|
|
std::string get_ip_address(const std::string& ifname)
|
2025-10-16 13:11:36 +08:00
|
|
|
|
{
|
2025-12-29 14:24:40 +08:00
|
|
|
|
struct ifaddrs* ifaddr = nullptr;
|
|
|
|
|
|
if (getifaddrs(&ifaddr) != 0) return "";
|
2025-10-16 13:11:36 +08:00
|
|
|
|
|
2025-12-29 14:24:40 +08:00
|
|
|
|
char ip[INET_ADDRSTRLEN] = {0};
|
|
|
|
|
|
for (auto* ifa = ifaddr; ifa; ifa = ifa->ifa_next)
|
2025-10-16 13:11:36 +08:00
|
|
|
|
{
|
2025-12-29 14:24:40 +08:00
|
|
|
|
if (!ifa->ifa_addr) continue;
|
2025-10-16 13:11:36 +08:00
|
|
|
|
if (ifa->ifa_addr->sa_family == AF_INET && ifname == ifa->ifa_name)
|
|
|
|
|
|
{
|
2025-12-29 14:24:40 +08:00
|
|
|
|
auto* addr = &reinterpret_cast<sockaddr_in*>(ifa->ifa_addr)->sin_addr;
|
|
|
|
|
|
inet_ntop(AF_INET, addr, ip, sizeof(ip));
|
|
|
|
|
|
break;
|
2025-10-16 13:11:36 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
freeifaddrs(ifaddr);
|
2025-12-29 14:24:40 +08:00
|
|
|
|
return ip;
|
2025-10-16 13:11:36 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-22 10:19:45 +08:00
|
|
|
|
// 安全释放 GstObject*
|
|
|
|
|
|
static void safe_unref(GstObject*& obj)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (obj)
|
|
|
|
|
|
{
|
|
|
|
|
|
gst_object_unref(obj);
|
|
|
|
|
|
obj = nullptr;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-22 10:38:47 +08:00
|
|
|
|
// 安全设置 GStreamer 属性:有则设,无则跳过
|
|
|
|
|
|
static void try_set_property(GstElement* elem, const char* prop, int value)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!elem) return;
|
|
|
|
|
|
|
|
|
|
|
|
GObjectClass* klass = G_OBJECT_GET_CLASS(elem);
|
|
|
|
|
|
if (klass && g_object_class_find_property(klass, prop))
|
|
|
|
|
|
{
|
|
|
|
|
|
g_object_set(G_OBJECT(elem), prop, value, nullptr);
|
|
|
|
|
|
LOG_INFO(std::string("[RTMP] Set ") + GST_ELEMENT_NAME(elem) + "." + prop + "=" + std::to_string(value));
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
LOG_WARN(std::string("[RTMP] ") + GST_ELEMENT_NAME(elem) + " has NO property '" + prop + "' (skip)");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-29 14:24:40 +08:00
|
|
|
|
// =======================================================
|
|
|
|
|
|
// 静态成员
|
|
|
|
|
|
// =======================================================
|
2025-10-15 08:57:40 +08:00
|
|
|
|
std::unordered_map<std::string, std::unique_ptr<RTMPManager::StreamContext>> RTMPManager::streams;
|
2025-10-14 17:13:08 +08:00
|
|
|
|
std::mutex RTMPManager::streams_mutex;
|
|
|
|
|
|
|
2025-12-29 14:24:40 +08:00
|
|
|
|
// =======================================================
|
|
|
|
|
|
// 初始化
|
|
|
|
|
|
// =======================================================
|
2025-10-14 17:13:08 +08:00
|
|
|
|
void RTMPManager::init()
|
|
|
|
|
|
{
|
|
|
|
|
|
gst_init(nullptr, nullptr);
|
2025-12-29 14:24:40 +08:00
|
|
|
|
LOG_INFO("[RTMP] GStreamer initialized");
|
2025-10-14 17:13:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-11-12 17:47:56 +08:00
|
|
|
|
std::string RTMPManager::make_key(const std::string& name) { return name + "_main"; }
|
2025-10-15 08:50:01 +08:00
|
|
|
|
|
2025-12-29 14:24:40 +08:00
|
|
|
|
// =======================================================
|
|
|
|
|
|
// 创建推流管线(稳定版)
|
|
|
|
|
|
// =======================================================
|
2025-11-12 17:47:56 +08:00
|
|
|
|
GstElement* RTMPManager::create_pipeline(const Camera& cam)
|
2025-10-16 10:25:51 +08:00
|
|
|
|
{
|
2025-12-29 14:24:40 +08:00
|
|
|
|
const std::string stream = cam.name + "_main";
|
2025-10-17 17:31:06 +08:00
|
|
|
|
const std::string app = "camera";
|
2025-11-12 18:10:16 +08:00
|
|
|
|
|
2026-01-22 10:19:45 +08:00
|
|
|
|
// 维持你当前 SRS 的 vhost query 方式,避免改动联动太多
|
2025-12-29 14:24:40 +08:00
|
|
|
|
const std::string live_rtmp = "rtmp://36.153.162.171:19435/" + app + "/" + stream + "?vhost=live";
|
2026-01-20 13:47:30 +08:00
|
|
|
|
const std::string record_rtmp = "rtmp://127.0.0.1:2935/" + app + "/" + stream + "?vhost=record";
|
|
|
|
|
|
|
2026-01-22 10:19:45 +08:00
|
|
|
|
// 关键:rtmpsink protocol=0 强制 IPv4(减少握手卡住/IPv6 干扰)
|
|
|
|
|
|
// 关键:valve 默认 drop=true,真正放行在 PLAYING 成功后再做(避免 cold-start 饿死)
|
2026-01-20 14:17:53 +08:00
|
|
|
|
std::string pipeline_str = "v4l2src device=" + cam.device +
|
2025-12-29 14:13:37 +08:00
|
|
|
|
" io-mode=dmabuf "
|
2026-01-20 14:14:50 +08:00
|
|
|
|
"! video/x-raw,format=NV12,width=1920,height=1080,framerate=" +
|
2025-12-29 15:09:37 +08:00
|
|
|
|
std::to_string(cam.fps) +
|
|
|
|
|
|
"/1 "
|
|
|
|
|
|
"! videoscale "
|
2026-01-20 13:47:30 +08:00
|
|
|
|
"! video/x-raw,width=" +
|
2025-12-29 14:36:48 +08:00
|
|
|
|
std::to_string(cam.width) + ",height=" + std::to_string(cam.height) +
|
2026-01-20 14:17:53 +08:00
|
|
|
|
" "
|
|
|
|
|
|
"! queue max-size-buffers=2 leaky=downstream "
|
2026-01-20 14:14:50 +08:00
|
|
|
|
"! mpph264enc rc-mode=cbr bps=" +
|
|
|
|
|
|
std::to_string(cam.bitrate) + " gop=" + std::to_string(cam.fps) +
|
|
|
|
|
|
" header-mode=each-idr profile=baseline "
|
2025-12-29 14:24:40 +08:00
|
|
|
|
"! h264parse config-interval=1 "
|
2026-01-20 13:47:30 +08:00
|
|
|
|
"! tee name=t "
|
|
|
|
|
|
|
2026-01-22 10:19:45 +08:00
|
|
|
|
// ===== record branch =====
|
2026-01-20 14:14:50 +08:00
|
|
|
|
"t. ! queue max-size-buffers=8 leaky=downstream "
|
2026-01-22 10:19:45 +08:00
|
|
|
|
"! valve name=record_valve drop=true "
|
2026-01-22 09:29:10 +08:00
|
|
|
|
"! queue max-size-buffers=8 leaky=downstream "
|
2026-01-20 14:14:50 +08:00
|
|
|
|
"! flvmux name=rec_mux streamable=true "
|
|
|
|
|
|
"! rtmpsink name=rec_sink location=\"" +
|
2026-01-20 13:47:30 +08:00
|
|
|
|
record_rtmp +
|
2026-01-22 10:38:47 +08:00
|
|
|
|
"\" sync=false async=false "
|
2026-01-20 13:47:30 +08:00
|
|
|
|
|
2026-01-22 10:19:45 +08:00
|
|
|
|
// ===== live branch =====
|
2026-01-20 14:14:50 +08:00
|
|
|
|
"t. ! queue max-size-buffers=8 leaky=downstream "
|
2026-01-20 13:47:30 +08:00
|
|
|
|
"! valve name=live_valve drop=true "
|
2026-01-20 14:14:50 +08:00
|
|
|
|
"! queue max-size-buffers=8 leaky=downstream "
|
|
|
|
|
|
"! flvmux name=live_mux streamable=true "
|
|
|
|
|
|
"! rtmpsink name=live_sink location=\"" +
|
2026-01-22 10:38:47 +08:00
|
|
|
|
live_rtmp + "\" sync=false async=false ";
|
2026-01-20 14:14:50 +08:00
|
|
|
|
|
|
|
|
|
|
GError* err = nullptr;
|
|
|
|
|
|
GstElement* pipeline = gst_parse_launch(pipeline_str.c_str(), &err);
|
|
|
|
|
|
if (err)
|
2025-10-16 10:25:51 +08:00
|
|
|
|
{
|
2026-01-20 14:14:50 +08:00
|
|
|
|
LOG_ERROR("[RTMP] Pipeline creation failed: " + std::string(err->message));
|
|
|
|
|
|
g_error_free(err);
|
2025-10-16 10:25:51 +08:00
|
|
|
|
return nullptr;
|
2025-10-15 17:01:43 +08:00
|
|
|
|
}
|
2025-12-29 14:24:40 +08:00
|
|
|
|
|
2025-10-16 10:25:51 +08:00
|
|
|
|
return pipeline;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-29 14:24:40 +08:00
|
|
|
|
// =======================================================
|
|
|
|
|
|
// 主推流线程
|
|
|
|
|
|
// =======================================================
|
2025-11-12 17:47:56 +08:00
|
|
|
|
void RTMPManager::stream_loop(Camera cam, StreamContext* ctx)
|
2025-10-16 10:25:51 +08:00
|
|
|
|
{
|
2025-10-17 16:27:04 +08:00
|
|
|
|
const std::string key = make_key(cam.name);
|
2025-11-12 17:57:20 +08:00
|
|
|
|
|
2026-01-22 10:19:45 +08:00
|
|
|
|
auto mark_error = [&](const std::string& err)
|
|
|
|
|
|
{
|
|
|
|
|
|
std::lock_guard<std::mutex> lk(ctx->status_mutex);
|
|
|
|
|
|
ctx->status.running = false;
|
|
|
|
|
|
ctx->status.last_error = err;
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2025-11-24 10:07:27 +08:00
|
|
|
|
while (ctx->thread_running)
|
2025-10-17 13:36:22 +08:00
|
|
|
|
{
|
2026-01-22 10:19:45 +08:00
|
|
|
|
// 1) 设备存在性
|
2025-12-29 14:24:40 +08:00
|
|
|
|
if (!device_exists(cam.device))
|
2025-10-17 13:36:22 +08:00
|
|
|
|
{
|
2026-01-22 10:19:45 +08:00
|
|
|
|
mark_error("Device not found: " + cam.device);
|
2025-12-29 14:24:40 +08:00
|
|
|
|
LOG_WARN("[RTMP] " + key + " - device not found");
|
2025-10-17 15:26:08 +08:00
|
|
|
|
std::this_thread::sleep_for(std::chrono::seconds(3));
|
|
|
|
|
|
continue;
|
2025-10-17 13:36:22 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-22 10:19:45 +08:00
|
|
|
|
// 2) 创建 pipeline
|
2025-11-12 17:47:56 +08:00
|
|
|
|
GstElement* pipeline = create_pipeline(cam);
|
2025-10-17 15:26:08 +08:00
|
|
|
|
if (!pipeline)
|
2025-10-17 13:36:22 +08:00
|
|
|
|
{
|
2026-01-22 10:19:45 +08:00
|
|
|
|
mark_error("Pipeline creation failed");
|
2025-10-17 15:26:08 +08:00
|
|
|
|
std::this_thread::sleep_for(std::chrono::seconds(3));
|
|
|
|
|
|
continue;
|
2025-10-17 13:36:22 +08:00
|
|
|
|
}
|
2025-10-17 16:14:20 +08:00
|
|
|
|
|
2026-01-22 10:19:45 +08:00
|
|
|
|
// 2.1) 获取 valve(保存到 ctx,供 MQTT 动态控制 live,record 启动静态决定)
|
2026-01-20 13:47:30 +08:00
|
|
|
|
GstElement* live_valve = gst_bin_get_by_name(GST_BIN(pipeline), "live_valve");
|
2026-01-22 10:19:45 +08:00
|
|
|
|
GstElement* record_valve = gst_bin_get_by_name(GST_BIN(pipeline), "record_valve");
|
2026-01-20 13:47:30 +08:00
|
|
|
|
|
2026-01-22 13:37:25 +08:00
|
|
|
|
// ===== 获取 rec_sink / live_sink =====
|
|
|
|
|
|
GstElement* rec_sink = gst_bin_get_by_name(GST_BIN(pipeline), "rec_sink");
|
|
|
|
|
|
GstElement* live_sink = gst_bin_get_by_name(GST_BIN(pipeline), "live_sink");
|
|
|
|
|
|
|
|
|
|
|
|
// ===== 尝试设置 protocol=0(兼容:有就设,无就跳过)=====
|
2026-01-22 10:38:47 +08:00
|
|
|
|
try_set_property(rec_sink, "protocol", 0);
|
|
|
|
|
|
try_set_property(live_sink, "protocol", 0);
|
|
|
|
|
|
|
2026-01-22 13:37:25 +08:00
|
|
|
|
// ===== 引用使用完就释放(不交给 ctx 管理)=====
|
2026-01-22 10:38:47 +08:00
|
|
|
|
if (rec_sink) gst_object_unref(rec_sink);
|
|
|
|
|
|
if (live_sink) gst_object_unref(live_sink);
|
|
|
|
|
|
|
2026-01-22 10:19:45 +08:00
|
|
|
|
if (!live_valve || !record_valve)
|
2026-01-20 13:47:30 +08:00
|
|
|
|
{
|
2026-01-22 10:19:45 +08:00
|
|
|
|
if (!live_valve) LOG_ERROR("[RTMP] " + key + " - live_valve not found");
|
|
|
|
|
|
if (!record_valve) LOG_ERROR("[RTMP] " + key + " - record_valve not found");
|
|
|
|
|
|
mark_error("valve not found");
|
2026-01-20 13:47:30 +08:00
|
|
|
|
|
2026-01-22 10:19:45 +08:00
|
|
|
|
if (live_valve) gst_object_unref(live_valve);
|
|
|
|
|
|
if (record_valve) gst_object_unref(record_valve);
|
2026-01-22 09:29:10 +08:00
|
|
|
|
gst_object_unref(pipeline);
|
|
|
|
|
|
std::this_thread::sleep_for(std::chrono::seconds(3));
|
|
|
|
|
|
continue;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
std::lock_guard<std::mutex> lk(ctx->status_mutex);
|
2026-01-22 10:19:45 +08:00
|
|
|
|
// 替换旧引用(避免泄漏)
|
|
|
|
|
|
if (ctx->live_valve) gst_object_unref(ctx->live_valve);
|
2026-01-22 09:29:10 +08:00
|
|
|
|
if (ctx->record_valve) gst_object_unref(ctx->record_valve);
|
|
|
|
|
|
|
2026-01-22 10:19:45 +08:00
|
|
|
|
ctx->live_valve = live_valve; // 引用交给 ctx 管理
|
|
|
|
|
|
ctx->record_valve = record_valve; // 引用交给 ctx 管理
|
|
|
|
|
|
}
|
2026-01-22 09:29:10 +08:00
|
|
|
|
|
2026-01-22 10:19:45 +08:00
|
|
|
|
// 2.2) 启动前:统一先 drop=true(不放行)
|
|
|
|
|
|
// 真正放行在 PLAYING 确认后进行(关键稳定性增强)
|
|
|
|
|
|
g_object_set(G_OBJECT(live_valve), "drop", TRUE, nullptr);
|
|
|
|
|
|
g_object_set(G_OBJECT(record_valve), "drop", TRUE, nullptr);
|
2026-01-20 13:47:30 +08:00
|
|
|
|
|
2025-12-29 14:24:40 +08:00
|
|
|
|
GstBus* bus = gst_element_get_bus(pipeline);
|
2025-10-17 16:14:20 +08:00
|
|
|
|
|
2025-10-17 15:58:54 +08:00
|
|
|
|
LOG_INFO("[RTMP] Starting stream: " + key);
|
2025-10-17 16:27:04 +08:00
|
|
|
|
gst_element_set_state(pipeline, GST_STATE_PLAYING);
|
2025-10-17 15:38:19 +08:00
|
|
|
|
|
2026-01-22 10:19:45 +08:00
|
|
|
|
// 3) 等待进入 PLAYING
|
2025-12-29 14:24:40 +08:00
|
|
|
|
GstState state = GST_STATE_NULL;
|
2026-01-22 10:19:45 +08:00
|
|
|
|
auto ret = gst_element_get_state(pipeline, &state, nullptr, 5 * GST_SECOND);
|
|
|
|
|
|
if (!(ret == GST_STATE_CHANGE_SUCCESS && state == GST_STATE_PLAYING))
|
2025-10-17 16:10:02 +08:00
|
|
|
|
{
|
2026-01-22 10:19:45 +08:00
|
|
|
|
mark_error("Failed to reach PLAYING");
|
|
|
|
|
|
LOG_ERROR("[RTMP] " + key + " failed to PLAY");
|
|
|
|
|
|
goto cleanup;
|
2025-11-12 17:57:20 +08:00
|
|
|
|
}
|
2026-01-22 10:19:45 +08:00
|
|
|
|
|
|
|
|
|
|
// 4) PLAYING 成功后,稍微延迟再按最终策略放行 valve(关键稳定性增强)
|
|
|
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(150));
|
|
|
|
|
|
|
|
|
|
|
|
// record:启动时决定一次(静态开关)
|
|
|
|
|
|
g_object_set(G_OBJECT(record_valve), "drop", g_record_enabled ? FALSE : TRUE, nullptr);
|
|
|
|
|
|
|
|
|
|
|
|
// live:运行期由 MQTT 控制(动态开关),此处按当前全局状态设置初值
|
|
|
|
|
|
g_object_set(G_OBJECT(live_valve), "drop", g_live_enabled.load() ? FALSE : TRUE, nullptr);
|
|
|
|
|
|
|
2025-11-12 17:57:20 +08:00
|
|
|
|
{
|
2025-11-24 10:07:27 +08:00
|
|
|
|
std::lock_guard<std::mutex> lk(ctx->status_mutex);
|
2026-01-22 10:19:45 +08:00
|
|
|
|
ctx->status.running = true;
|
|
|
|
|
|
ctx->status.last_error.clear();
|
2025-10-17 16:10:02 +08:00
|
|
|
|
}
|
2026-01-22 10:19:45 +08:00
|
|
|
|
LOG_INFO("[RTMP] " + key + " confirmed PLAYING");
|
2025-10-17 16:10:02 +08:00
|
|
|
|
|
2026-01-22 10:19:45 +08:00
|
|
|
|
// 5) 正常运行:监听 ERROR / EOS
|
2025-11-24 10:07:27 +08:00
|
|
|
|
while (ctx->thread_running)
|
2025-10-17 15:52:47 +08:00
|
|
|
|
{
|
2025-12-29 14:24:40 +08:00
|
|
|
|
GstMessage* msg = gst_bus_timed_pop_filtered(
|
|
|
|
|
|
bus, 300 * GST_MSECOND, static_cast<GstMessageType>(GST_MESSAGE_ERROR | GST_MESSAGE_EOS));
|
|
|
|
|
|
|
|
|
|
|
|
if (!msg)
|
2025-10-17 16:27:04 +08:00
|
|
|
|
{
|
2026-01-22 10:19:45 +08:00
|
|
|
|
// 心跳:能跑就认为 running
|
2025-11-24 10:07:27 +08:00
|
|
|
|
std::lock_guard<std::mutex> lk(ctx->status_mutex);
|
2025-10-17 16:27:04 +08:00
|
|
|
|
ctx->status.running = true;
|
2025-12-29 14:24:40 +08:00
|
|
|
|
continue;
|
2025-10-17 15:52:47 +08:00
|
|
|
|
}
|
2025-10-16 10:52:51 +08:00
|
|
|
|
|
2025-12-29 14:24:40 +08:00
|
|
|
|
if (GST_MESSAGE_TYPE(msg) == GST_MESSAGE_ERROR)
|
2025-10-17 08:45:21 +08:00
|
|
|
|
{
|
2025-12-29 14:24:40 +08:00
|
|
|
|
GError* err = nullptr;
|
|
|
|
|
|
gst_message_parse_error(msg, &err, nullptr);
|
|
|
|
|
|
|
2026-01-22 10:19:45 +08:00
|
|
|
|
mark_error(err ? err->message : "Unknown GStreamer error");
|
2025-12-29 14:24:40 +08:00
|
|
|
|
LOG_ERROR("[RTMP] " + key + " ERROR: " + (err ? err->message : "unknown"));
|
2026-01-22 10:19:45 +08:00
|
|
|
|
|
2025-12-29 14:24:40 +08:00
|
|
|
|
if (err) g_error_free(err);
|
|
|
|
|
|
gst_message_unref(msg);
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (GST_MESSAGE_TYPE(msg) == GST_MESSAGE_EOS)
|
|
|
|
|
|
{
|
2026-01-22 10:19:45 +08:00
|
|
|
|
mark_error("EOS");
|
2025-12-29 14:24:40 +08:00
|
|
|
|
LOG_WARN("[RTMP] " + key + " EOS");
|
|
|
|
|
|
gst_message_unref(msg);
|
|
|
|
|
|
break;
|
2025-10-17 08:45:21 +08:00
|
|
|
|
}
|
2025-10-17 16:27:04 +08:00
|
|
|
|
|
2025-12-29 14:24:40 +08:00
|
|
|
|
gst_message_unref(msg);
|
2025-10-16 10:52:51 +08:00
|
|
|
|
}
|
2025-10-16 11:21:18 +08:00
|
|
|
|
|
2025-12-29 14:24:40 +08:00
|
|
|
|
cleanup:
|
2026-01-22 10:19:45 +08:00
|
|
|
|
// 6) 清理:先释放 ctx 保存的 valve 引用(避免下一轮使用旧引用)
|
2026-01-22 09:29:10 +08:00
|
|
|
|
{
|
2026-01-22 10:19:45 +08:00
|
|
|
|
std::lock_guard<std::mutex> lk(ctx->status_mutex);
|
|
|
|
|
|
if (ctx->live_valve)
|
|
|
|
|
|
{
|
|
|
|
|
|
gst_object_unref(ctx->live_valve);
|
|
|
|
|
|
ctx->live_valve = nullptr;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (ctx->record_valve)
|
|
|
|
|
|
{
|
|
|
|
|
|
gst_object_unref(ctx->record_valve);
|
|
|
|
|
|
ctx->record_valve = nullptr;
|
|
|
|
|
|
}
|
2026-01-22 09:29:10 +08:00
|
|
|
|
}
|
2026-01-20 13:47:30 +08:00
|
|
|
|
|
2026-01-22 10:19:45 +08:00
|
|
|
|
// 7) pipeline 清理
|
2025-10-17 15:26:08 +08:00
|
|
|
|
gst_element_set_state(pipeline, GST_STATE_NULL);
|
2025-10-17 15:58:54 +08:00
|
|
|
|
if (bus) gst_object_unref(bus);
|
2025-10-17 15:26:08 +08:00
|
|
|
|
gst_object_unref(pipeline);
|
2025-10-16 12:53:41 +08:00
|
|
|
|
|
2025-11-24 10:07:27 +08:00
|
|
|
|
if (ctx->thread_running)
|
2025-10-17 08:45:21 +08:00
|
|
|
|
{
|
2025-10-17 16:27:04 +08:00
|
|
|
|
LOG_WARN("[RTMP] Restarting " + key + " in 3s...");
|
2025-12-29 14:24:40 +08:00
|
|
|
|
std::this_thread::sleep_for(std::chrono::seconds(3));
|
2025-10-17 08:45:21 +08:00
|
|
|
|
}
|
2025-10-16 10:52:51 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-11-24 10:07:27 +08:00
|
|
|
|
{
|
|
|
|
|
|
std::lock_guard<std::mutex> lk(ctx->status_mutex);
|
|
|
|
|
|
ctx->status.running = false;
|
|
|
|
|
|
}
|
2025-10-17 15:58:54 +08:00
|
|
|
|
LOG_INFO("[RTMP] Stream thread exited for " + key);
|
2025-10-16 10:52:51 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-29 14:24:40 +08:00
|
|
|
|
// =======================================================
|
|
|
|
|
|
// 启停管理
|
|
|
|
|
|
// =======================================================
|
2025-10-17 15:58:54 +08:00
|
|
|
|
void RTMPManager::start_all()
|
2025-10-16 10:25:51 +08:00
|
|
|
|
{
|
2025-12-29 14:24:40 +08:00
|
|
|
|
LOG_INFO("[RTMP] Starting enabled streams...");
|
2025-10-16 10:25:51 +08:00
|
|
|
|
std::lock_guard<std::mutex> lock(streams_mutex);
|
|
|
|
|
|
|
2025-10-17 15:58:54 +08:00
|
|
|
|
int delay_ms = 0;
|
2025-12-23 08:59:08 +08:00
|
|
|
|
for (const auto& cam : g_app_config.cameras)
|
2025-10-17 15:58:54 +08:00
|
|
|
|
{
|
2025-12-23 08:59:08 +08:00
|
|
|
|
if (!cam.enabled)
|
|
|
|
|
|
{
|
|
|
|
|
|
LOG_INFO("[RTMP] Skip disabled camera: " + cam.name);
|
|
|
|
|
|
continue;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-29 14:24:40 +08:00
|
|
|
|
const auto key = make_key(cam.name);
|
|
|
|
|
|
if (streams.count(key)) continue;
|
2025-10-17 15:58:54 +08:00
|
|
|
|
|
|
|
|
|
|
auto ctx = std::make_unique<StreamContext>();
|
2025-11-24 10:07:27 +08:00
|
|
|
|
ctx->thread_running.store(true);
|
2025-10-17 15:58:54 +08:00
|
|
|
|
|
|
|
|
|
|
ctx->thread = std::thread(
|
|
|
|
|
|
[cam, ptr = ctx.get(), delay_ms]()
|
|
|
|
|
|
{
|
|
|
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(delay_ms));
|
|
|
|
|
|
stream_loop(cam, ptr);
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
streams.emplace(key, std::move(ctx));
|
2025-12-23 08:59:08 +08:00
|
|
|
|
delay_ms += 200;
|
2025-10-17 15:58:54 +08:00
|
|
|
|
}
|
2025-10-16 10:25:51 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-17 16:00:35 +08:00
|
|
|
|
void RTMPManager::stop_all()
|
|
|
|
|
|
{
|
|
|
|
|
|
std::lock_guard<std::mutex> lock(streams_mutex);
|
2026-01-20 13:47:30 +08:00
|
|
|
|
|
|
|
|
|
|
for (auto& kv : streams)
|
|
|
|
|
|
{
|
|
|
|
|
|
kv.second->thread_running.store(false);
|
|
|
|
|
|
|
|
|
|
|
|
std::lock_guard<std::mutex> lk(kv.second->status_mutex);
|
2026-01-22 09:29:10 +08:00
|
|
|
|
|
2026-01-20 13:47:30 +08:00
|
|
|
|
if (kv.second->live_valve)
|
|
|
|
|
|
{
|
|
|
|
|
|
gst_object_unref(kv.second->live_valve);
|
|
|
|
|
|
kv.second->live_valve = nullptr;
|
|
|
|
|
|
}
|
2026-01-22 09:29:10 +08:00
|
|
|
|
if (kv.second->record_valve)
|
|
|
|
|
|
{
|
|
|
|
|
|
gst_object_unref(kv.second->record_valve);
|
|
|
|
|
|
kv.second->record_valve = nullptr;
|
|
|
|
|
|
}
|
2026-01-20 13:47:30 +08:00
|
|
|
|
}
|
2025-12-29 14:24:40 +08:00
|
|
|
|
|
2025-11-12 17:47:56 +08:00
|
|
|
|
for (auto& kv : streams)
|
2025-10-17 16:00:35 +08:00
|
|
|
|
if (kv.second->thread.joinable()) kv.second->thread.join();
|
2025-12-29 14:24:40 +08:00
|
|
|
|
|
2025-10-17 16:00:35 +08:00
|
|
|
|
streams.clear();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-29 14:24:40 +08:00
|
|
|
|
// =======================================================
|
|
|
|
|
|
// 状态 & URL
|
|
|
|
|
|
// =======================================================
|
2025-11-12 17:47:56 +08:00
|
|
|
|
bool RTMPManager::is_streaming(const std::string& cam_name)
|
2025-10-17 16:00:35 +08:00
|
|
|
|
{
|
|
|
|
|
|
std::lock_guard<std::mutex> lock(streams_mutex);
|
|
|
|
|
|
auto it = streams.find(make_key(cam_name));
|
2025-11-24 10:07:27 +08:00
|
|
|
|
if (it == streams.end()) return false;
|
|
|
|
|
|
|
2025-12-29 14:24:40 +08:00
|
|
|
|
std::lock_guard<std::mutex> lk(it->second->status_mutex);
|
|
|
|
|
|
return it->second->status.running;
|
2025-10-17 16:00:35 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-11-12 17:47:56 +08:00
|
|
|
|
std::string RTMPManager::get_stream_url(const std::string& cam_name)
|
2025-10-16 10:25:51 +08:00
|
|
|
|
{
|
2025-10-16 13:11:36 +08:00
|
|
|
|
std::string ip = get_ip_address("enP2p33s0");
|
2026-01-07 17:00:02 +08:00
|
|
|
|
if (ip.empty()) ip = "192.168.4.194";
|
2025-12-29 14:24:40 +08:00
|
|
|
|
|
|
|
|
|
|
return "http://" + ip + ":11985/rtc/v1/whep/?app=camera&stream=" + cam_name + "_main&vhost=live";
|
2025-10-16 10:25:51 +08:00
|
|
|
|
}
|
2025-12-29 14:24:40 +08:00
|
|
|
|
|
|
|
|
|
|
// =======================================================
|
|
|
|
|
|
// 汇总状态
|
|
|
|
|
|
// =======================================================
|
2025-10-17 15:52:47 +08:00
|
|
|
|
std::vector<RTMPManager::ChannelInfo> RTMPManager::get_all_channels_status()
|
2025-10-17 13:36:22 +08:00
|
|
|
|
{
|
2025-10-17 15:52:47 +08:00
|
|
|
|
std::vector<ChannelInfo> result;
|
2025-10-17 15:26:08 +08:00
|
|
|
|
std::lock_guard<std::mutex> lock(streams_mutex);
|
2025-10-17 15:52:47 +08:00
|
|
|
|
|
|
|
|
|
|
for (size_t i = 0; i < g_app_config.cameras.size(); ++i)
|
|
|
|
|
|
{
|
2025-11-12 17:47:56 +08:00
|
|
|
|
const auto& cam = g_app_config.cameras[i];
|
2025-12-29 14:24:40 +08:00
|
|
|
|
ChannelInfo ch{};
|
2025-10-17 15:52:47 +08:00
|
|
|
|
ch.loc = static_cast<int>(i);
|
2025-12-23 08:59:08 +08:00
|
|
|
|
|
|
|
|
|
|
if (!cam.enabled)
|
|
|
|
|
|
{
|
2025-12-29 14:24:40 +08:00
|
|
|
|
ch.running = false;
|
2025-12-23 08:59:08 +08:00
|
|
|
|
ch.reason = "Disabled by config";
|
|
|
|
|
|
result.push_back(ch);
|
|
|
|
|
|
continue;
|
|
|
|
|
|
}
|
2025-10-17 15:52:47 +08:00
|
|
|
|
|
2025-12-29 14:24:40 +08:00
|
|
|
|
auto it = streams.find(make_key(cam.name));
|
2025-10-17 15:52:47 +08:00
|
|
|
|
if (it != streams.end())
|
|
|
|
|
|
{
|
2025-12-29 14:24:40 +08:00
|
|
|
|
std::lock_guard<std::mutex> lk(it->second->status_mutex);
|
|
|
|
|
|
ch.running = it->second->status.running;
|
|
|
|
|
|
ch.reason = it->second->status.last_error;
|
|
|
|
|
|
if (ch.running) ch.url = get_stream_url(cam.name);
|
2025-10-17 15:52:47 +08:00
|
|
|
|
}
|
2025-12-23 08:59:08 +08:00
|
|
|
|
else
|
|
|
|
|
|
{
|
2025-12-29 14:24:40 +08:00
|
|
|
|
ch.running = false;
|
|
|
|
|
|
ch.reason = "Not started";
|
2025-12-23 08:59:08 +08:00
|
|
|
|
}
|
2025-10-17 15:52:47 +08:00
|
|
|
|
|
|
|
|
|
|
result.push_back(ch);
|
|
|
|
|
|
}
|
2025-12-23 08:59:08 +08:00
|
|
|
|
|
2025-10-17 15:26:08 +08:00
|
|
|
|
return result;
|
2025-10-17 13:36:22 +08:00
|
|
|
|
}
|
2026-01-20 13:47:30 +08:00
|
|
|
|
|
2026-01-22 10:19:45 +08:00
|
|
|
|
// =======================================================
|
|
|
|
|
|
// live:MQTT 动态开关
|
|
|
|
|
|
// =======================================================
|
2026-01-20 13:47:30 +08:00
|
|
|
|
void RTMPManager::set_live_enabled_all(bool enable)
|
|
|
|
|
|
{
|
|
|
|
|
|
std::lock_guard<std::mutex> lock(streams_mutex);
|
|
|
|
|
|
|
|
|
|
|
|
g_live_enabled.store(enable);
|
|
|
|
|
|
|
|
|
|
|
|
for (auto& kv : streams)
|
|
|
|
|
|
{
|
|
|
|
|
|
auto* ctx = kv.second.get();
|
|
|
|
|
|
std::lock_guard<std::mutex> lk(ctx->status_mutex);
|
|
|
|
|
|
if (!ctx->live_valve) continue;
|
|
|
|
|
|
|
|
|
|
|
|
// enable=true → drop=false(放行 live)
|
|
|
|
|
|
g_object_set(G_OBJECT(ctx->live_valve), "drop", enable ? FALSE : TRUE, nullptr);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
LOG_INFO(std::string("[RTMP] Live ") + (enable ? "ENABLED" : "DISABLED") + " for all streams");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void RTMPManager::set_live_enabled(const std::string& cam_name, bool enable)
|
|
|
|
|
|
{
|
|
|
|
|
|
std::lock_guard<std::mutex> lock(streams_mutex);
|
|
|
|
|
|
auto it = streams.find(make_key(cam_name));
|
|
|
|
|
|
if (it == streams.end()) return;
|
|
|
|
|
|
|
|
|
|
|
|
auto* ctx = it->second.get();
|
|
|
|
|
|
std::lock_guard<std::mutex> lk(ctx->status_mutex);
|
|
|
|
|
|
if (!ctx->live_valve) return;
|
|
|
|
|
|
|
|
|
|
|
|
g_object_set(G_OBJECT(ctx->live_valve), "drop", enable ? FALSE : TRUE, nullptr);
|
|
|
|
|
|
LOG_INFO("[RTMP] Live " + std::string(enable ? "ENABLED" : "DISABLED") + " for " + cam_name);
|
|
|
|
|
|
}
|