2025-10-14 17:13:08 +08:00
|
|
|
|
// rtsp_manager.hpp
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
|
|
#include <gst/gst.h>
|
|
|
|
|
|
#include "app_config.hpp"
|
|
|
|
|
|
#include <unordered_map>
|
|
|
|
|
|
#include <string>
|
|
|
|
|
|
#include <mutex>
|
|
|
|
|
|
#include <thread>
|
|
|
|
|
|
#include <atomic>
|
|
|
|
|
|
|
|
|
|
|
|
class RTMPManager
|
|
|
|
|
|
{
|
|
|
|
|
|
public:
|
|
|
|
|
|
static void init();
|
|
|
|
|
|
// start/stop 增加 StreamType 参数
|
|
|
|
|
|
static void start_camera(const Camera &cam, StreamType type);
|
|
|
|
|
|
static void stop_camera(const std::string &cam_name, StreamType type);
|
|
|
|
|
|
static void stop_all();
|
2025-10-14 17:44:59 +08:00
|
|
|
|
static bool is_streaming(const std::string &cam_name, StreamType type);
|
2025-10-14 17:13:08 +08:00
|
|
|
|
static bool is_any_streaming();
|
|
|
|
|
|
|
|
|
|
|
|
// 获取推流 URL,用于应答
|
|
|
|
|
|
static std::string get_stream_url(const std::string &cam_name, StreamType type);
|
|
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
struct StreamContext
|
|
|
|
|
|
{
|
2025-10-14 17:44:59 +08:00
|
|
|
|
std::atomic<bool> running;
|
2025-10-14 17:13:08 +08:00
|
|
|
|
std::thread thread;
|
2025-10-14 17:44:59 +08:00
|
|
|
|
|
|
|
|
|
|
StreamContext() : running(false) {} // 确保 atomic 初始化
|
2025-10-14 17:13:08 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
static std::unordered_map<std::string, StreamContext> streams;
|
|
|
|
|
|
static std::mutex streams_mutex;
|
|
|
|
|
|
|
|
|
|
|
|
// stream loop 接收 StreamType 作为参数
|
|
|
|
|
|
static void stream_loop(Camera cam, StreamType type);
|
|
|
|
|
|
static GstElement *create_pipeline(const Camera &cam, StreamType type);
|
|
|
|
|
|
|
|
|
|
|
|
// 辅助:构建 map key
|
|
|
|
|
|
static std::string make_stream_key(const std::string &cam_name, StreamType type);
|
|
|
|
|
|
};
|