sweeper_video/include/rtmp_manager.hpp

68 lines
1.9 KiB
C++
Raw Normal View History

// rtsp_manager.hpp
#pragma once
#include <gst/gst.h>
#include <unordered_map>
#include <string>
#include <mutex>
#include <thread>
#include <atomic>
2025-10-15 08:50:01 +08:00
#include <functional>
2025-10-15 08:57:40 +08:00
#include <memory>
2025-10-15 08:50:01 +08:00
#include "app_config.hpp"
class RTMPManager
{
public:
2025-10-15 08:50:01 +08:00
enum class StreamResult
{
OK,
PIPELINE_ERROR,
CONNECTION_FAIL,
EOS_RECEIVED,
UNKNOWN
};
struct StreamStatus
{
bool running;
StreamResult last_result;
std::string last_error;
};
using StreamCallback = std::function<void(const std::string &key, StreamStatus status)>;
static void init();
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);
static bool is_any_streaming();
static std::string get_stream_url(const std::string &cam_name, StreamType type);
2025-10-15 08:50:01 +08:00
static void set_status_callback(StreamCallback cb);
private:
struct StreamContext
{
2025-10-15 08:50:01 +08:00
std::atomic<bool> running{false};
std::thread thread;
2025-10-15 08:50:01 +08:00
StreamStatus status;
2025-10-15 08:57:40 +08:00
StreamContext() = default;
// non-copyable
StreamContext(const StreamContext &) = delete;
StreamContext &operator=(const StreamContext &) = delete;
// movable? we avoid moving by using unique_ptr
};
2025-10-15 08:57:40 +08:00
// store unique_ptr to avoid copy/move issues with atomic/thread
static std::unordered_map<std::string, std::unique_ptr<StreamContext>> streams;
static std::mutex streams_mutex;
2025-10-15 08:50:01 +08:00
static StreamCallback status_callback;
static void stream_loop(Camera cam, StreamType type);
static GstElement *create_pipeline(const Camera &cam, StreamType type);
static std::string make_stream_key(const std::string &cam_name, StreamType type);
2025-10-15 08:50:01 +08:00
static void update_status(const std::string &key, const StreamStatus &status);
};