// rtsp_manager.hpp
#pragma once

#include <gst/gst.h>
#include <gst/rtsp-server/rtsp-server.h>
#include "app_config.hpp"
#include <unordered_map>
#include <string>
#include <mutex>
#include <vector>

class RTSPManager
{
public:
    static void init();
    static void start(const std::vector<Camera> &cameras);
    static void stop();

    static void mount_camera(const Camera &cam);
    static void unmount_camera(const Camera &cam);
    static bool is_streaming(const std::string &cam_name);
    static bool is_any_streaming();

private:
    static GMainLoop *loop;
    static GMainContext *main_context;
    static GstRTSPServer *server;
    static std::unordered_map<std::string, bool> streaming_status;

    // 工厂创建函数
    static GstRTSPMediaFactory *create_media_factory(const Camera &cam);

    // 挂载/卸载函数
    static gboolean mount_camera_in_main(gpointer data);
    static gboolean unmount_camera_in_main(gpointer data);

    // 静态 mutex 和工厂表
    static std::unordered_map<std::string, GstRTSPMediaFactory *> mounted_factories;
    static std::mutex mounted_factories_mutex;

    // 媒体对象跟踪
    static std::unordered_map<std::string, std::vector<GstRTSPMedia *>> media_map;
    static std::mutex media_map_mutex;

    // 信号处理函数
    static void on_media_created(GstRTSPMediaFactory *factory, GstRTSPMedia *media, gpointer user_data);
    static void on_media_unprepared(GstRTSPMedia *media, gpointer user_data);
};