2025-12-17 17:21:11 +08:00
|
|
|
|
// rtmp_manager.hpp
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
|
|
#include <gst/gst.h>
|
|
|
|
|
|
|
|
|
|
|
|
#include <atomic>
|
|
|
|
|
|
#include <mutex>
|
|
|
|
|
|
#include <string>
|
|
|
|
|
|
#include <thread>
|
|
|
|
|
|
#include <unordered_map>
|
|
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
|
|
|
|
#include "app_config.hpp"
|
|
|
|
|
|
#include "logger.hpp"
|
|
|
|
|
|
|
|
|
|
|
|
std::string get_ip_address(const std::string& ifname);
|
|
|
|
|
|
|
|
|
|
|
|
class RTMPManager
|
|
|
|
|
|
{
|
|
|
|
|
|
public:
|
|
|
|
|
|
struct StreamStatus
|
|
|
|
|
|
{
|
|
|
|
|
|
bool running{false};
|
|
|
|
|
|
std::string last_error;
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// 初始化与控制接口
|
|
|
|
|
|
static void init();
|
|
|
|
|
|
static void start_all();
|
|
|
|
|
|
static void stop_all();
|
|
|
|
|
|
static bool is_streaming(const std::string& cam_name);
|
|
|
|
|
|
static std::string get_stream_url(const std::string& cam_name);
|
|
|
|
|
|
|
|
|
|
|
|
// 新增:单通道状态结构体(用于心跳)
|
|
|
|
|
|
struct ChannelInfo
|
|
|
|
|
|
{
|
|
|
|
|
|
int loc; // 摄像头位置索引(0~7)
|
|
|
|
|
|
std::string url; // 正常时的推流地址
|
|
|
|
|
|
bool running; // 是否正常运行
|
|
|
|
|
|
std::string reason; // 错误原因(仅在异常时填)
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// 新增:获取所有通道详细状态
|
|
|
|
|
|
static std::vector<ChannelInfo> get_all_channels_status();
|
|
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
struct StreamContext
|
|
|
|
|
|
{
|
|
|
|
|
|
std::atomic<bool> thread_running{false};
|
|
|
|
|
|
std::thread thread;
|
|
|
|
|
|
|
|
|
|
|
|
StreamStatus status;
|
|
|
|
|
|
std::mutex status_mutex;
|
2025-12-19 09:01:23 +08:00
|
|
|
|
|
|
|
|
|
|
// 新增:记录最后收到帧的时间(毫秒)
|
|
|
|
|
|
std::atomic<int64_t> last_frame_ms{0};
|
2025-12-17 17:21:11 +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);
|
|
|
|
|
|
|
|
|
|
|
|
static std::unordered_map<std::string, std::unique_ptr<StreamContext>> streams;
|
|
|
|
|
|
static std::mutex streams_mutex;
|
|
|
|
|
|
|
|
|
|
|
|
static constexpr int RETRY_BASE_DELAY_MS = 3000;
|
|
|
|
|
|
};
|