sweeper_video/include/rtmp_manager.hpp

114 lines
3.2 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-16 10:25:51 +08:00
#include <functional>
2025-10-16 10:54:11 +08:00
#include <future>
2025-10-15 08:57:40 +08:00
#include <memory>
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-16 10:25:51 +08:00
#include "data_manager.hpp"
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
enum class StreamResult
{
OK,
PIPELINE_ERROR,
CONNECTION_FAIL,
EOS_RECEIVED,
TIMEOUT,
UNKNOWN
};
struct StreamStatus
{
bool running{false};
StreamResult last_result{StreamResult::UNKNOWN};
std::string last_error;
};
2025-10-15 15:01:55 +08:00
struct StreamResultInfo
{
int loc{-1};
std::string url;
2025-10-16 10:25:51 +08:00
int result{1}; // 0 success, 1 fail
2025-10-15 15:01:55 +08:00
std::string reason;
};
2025-10-16 10:25:51 +08:00
using StreamCallback = std::function<void(const std::string &key, StreamStatus)>;
static void init();
2025-10-16 10:25:51 +08:00
static void set_status_callback(StreamCallback cb);
// 单路接口
static StreamResultInfo start_camera(const Camera &cam, StreamType type);
static StreamResultInfo stop_camera(const std::string &cam_name, StreamType type);
// 批量接口:处理 VideoPushRequest
static std::vector<StreamResultInfo> process_push_request(const VideoPushRequest &req);
static void stop_all();
2025-10-17 13:36:22 +08:00
// 自动启动所有录像流(上电调用一次)
static void start_all_record_streams();
2025-10-16 10:25:51 +08:00
static bool is_streaming(const std::string &cam_name, StreamType type);
static bool is_any_streaming();
static std::string get_stream_url(const std::string &cam_name, StreamType type);
2025-10-15 15:01:55 +08:00
private:
struct StreamContext
{
2025-10-15 08:50:01 +08:00
std::atomic<bool> running{false};
std::thread thread;
2025-10-16 12:53:41 +08:00
2025-10-17 13:36:22 +08:00
// 启动阶段的 promise用于首次启动结果反馈7s 内)
2025-10-16 12:53:41 +08:00
std::promise<StreamStatus> start_promise;
std::atomic<bool> start_promise_set{false};
2025-10-16 10:25:51 +08:00
StreamStatus status;
StreamContext() = default;
StreamContext(const StreamContext &) = delete;
StreamContext &operator=(const StreamContext &) = delete;
2025-10-17 09:00:12 +08:00
~StreamContext()
{
try
{
running.store(false);
if (thread.joinable())
{
thread.join(); // 防止 std::terminate()
}
}
catch (...)
{
// 安全兜底,不抛异常
}
}
};
2025-10-16 10:25:51 +08:00
static std::string make_stream_key(const std::string &cam_name, StreamType type);
static GstElement *create_pipeline(const Camera &cam, StreamType type);
2025-10-16 12:53:41 +08:00
static void stream_loop(Camera cam, StreamType type, StreamContext *ctx);
2025-10-17 13:36:22 +08:00
static bool do_stream_once(const Camera &cam, StreamType type, StreamContext *ctx);
2025-10-16 10:25:51 +08:00
static void update_status(const std::string &key, const StreamStatus &status);
2025-10-15 14:14:00 +08:00
static std::unordered_map<std::string, std::unique_ptr<StreamContext>> streams;
static std::mutex streams_mutex;
2025-10-16 10:25:51 +08:00
static StreamCallback status_callback;
2025-10-15 17:01:43 +08:00
2025-10-16 10:25:51 +08:00
// 自动重试参数
2025-10-17 13:36:22 +08:00
// MAINrecord为永久重试SUBlive使用限次重试
static constexpr int LIVE_MAX_RETRIES = 5;
static constexpr int RETRY_BASE_DELAY_MS = 2000; // 2s -> 可指数退避
2025-10-15 15:36:48 +08:00
};