sweeper_video/include/rtmp_manager.hpp

76 lines
1.9 KiB
C++
Raw Normal View History

2025-10-15 15:36:48 +08:00
// rtmp_manager.hpp
2025-10-15 15:03:10 +08:00
#pragma once
2025-10-16 10:25:51 +08:00
#include <gst/gst.h>
#include <atomic>
2025-10-15 15:01:55 +08:00
#include <mutex>
#include <string>
#include <thread>
#include <unordered_map>
#include <vector>
2025-10-15 08:50:01 +08:00
#include "app_config.hpp"
2025-10-17 15:26:08 +08:00
#include "logger.hpp"
2025-11-14 17:05:24 +08:00
std::string get_ip_address(const std::string& ifname);
2025-10-15 15:03:10 +08:00
class RTMPManager
{
2025-10-15 15:01:55 +08:00
public:
2025-10-16 10:25:51 +08:00
struct StreamStatus
{
bool running{false};
std::string last_error;
};
2025-10-17 15:54:49 +08:00
// 初始化与控制接口
static void init();
2025-10-17 15:26:08 +08:00
static void start_all();
static void stop_all();
2025-11-14 17:05:24 +08:00
static bool is_streaming(const std::string& cam_name);
static std::string get_stream_url(const std::string& cam_name);
2025-10-17 15:54:49 +08:00
2025-11-24 10:07:27 +08:00
// 新增:单通道状态结构体(用于心跳)
2025-10-17 15:54:49 +08:00
struct ChannelInfo
{
int loc; // 摄像头位置索引0~7
std::string url; // 正常时的推流地址
bool running; // 是否正常运行
std::string reason; // 错误原因(仅在异常时填)
};
2025-11-24 10:07:27 +08:00
// 新增:获取所有通道详细状态
2025-10-17 15:54:49 +08:00
static std::vector<ChannelInfo> get_all_channels_status();
2026-01-20 13:47:30 +08:00
// 控制 live 分支开关MQTT 用)
static void set_live_enabled_all(bool enable);
static void set_live_enabled(const std::string& cam_name, bool enable);
2026-01-22 09:29:10 +08:00
static bool g_record_enabled;
2025-10-15 15:01:55 +08:00
private:
struct StreamContext
{
2025-11-24 10:07:27 +08:00
std::atomic<bool> thread_running{false};
std::thread thread;
2025-11-24 10:07:27 +08:00
2025-10-16 10:25:51 +08:00
StreamStatus status;
2025-11-24 10:07:27 +08:00
std::mutex status_mutex;
2026-01-20 13:47:30 +08:00
GstElement* live_valve{nullptr};
2026-01-22 09:29:10 +08:00
GstElement* record_valve = nullptr;
};
2025-11-14 17:05:24 +08:00
static void stream_loop(Camera cam, StreamContext* ctx);
static GstElement* create_pipeline(const Camera& cam);
static std::string make_key(const std::string& name);
2025-10-15 14:14:00 +08:00
static std::unordered_map<std::string, std::unique_ptr<StreamContext>> streams;
static std::mutex streams_mutex;
2026-01-22 09:29:10 +08:00
2026-01-20 13:49:41 +08:00
static std::atomic<bool> g_live_enabled;
2025-10-15 17:01:43 +08:00
2025-10-17 15:26:08 +08:00
static constexpr int RETRY_BASE_DELAY_MS = 3000;
};