config.h 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #ifndef JTJAI_MEDIA_CONFIG_H
  2. #define JTJAI_MEDIA_CONFIG_H
  3. #include <string>
  4. #include <vector>
  5. #include <boost/json.hpp>
  6. namespace jtjai_media {
  7. struct StreamConfig {
  8. std::string rtsp_url; // RTSP流地址
  9. int duration_seconds; // 访问时长(秒)
  10. double weight; // 访问权重
  11. std::string output_filename; // 输出文件名
  12. StreamConfig() = default;
  13. StreamConfig(const std::string& url, int duration, double w, const std::string& filename)
  14. : rtsp_url(url), duration_seconds(duration), weight(w), output_filename(filename) {}
  15. };
  16. struct GlobalConfig {
  17. int total_poll_duration_seconds; // 总轮询时长(秒)
  18. int max_concurrent_streams; // 最大并发流数
  19. std::string output_directory; // 输出目录
  20. std::string report_filename; // 报告文件名
  21. int connection_timeout_seconds; // 连接超时时间
  22. int read_timeout_seconds; // 读取超时时间
  23. int poll_cycles; // 轮询周期数(-1表示无限循环)
  24. int cycle_interval_seconds; // 轮询周期间隔(秒)
  25. GlobalConfig()
  26. : total_poll_duration_seconds(300)
  27. , max_concurrent_streams(10)
  28. , output_directory("./output")
  29. , report_filename("rtsp_report.json")
  30. , connection_timeout_seconds(10)
  31. , read_timeout_seconds(30)
  32. , poll_cycles(1)
  33. , cycle_interval_seconds(60) {}
  34. };
  35. class ConfigManager {
  36. public:
  37. ConfigManager() = default;
  38. // 从JSON文件加载配置
  39. bool load_from_file(const std::string& config_file);
  40. // 从JSON字符串加载配置
  41. bool load_from_json(const std::string& json_str);
  42. // 保存配置到文件
  43. bool save_to_file(const std::string& config_file) const;
  44. // 获取配置
  45. const GlobalConfig& get_global_config() const { return global_config_; }
  46. const std::vector<StreamConfig>& get_stream_configs() const { return stream_configs_; }
  47. // 设置配置
  48. void set_global_config(const GlobalConfig& config) { global_config_ = config; }
  49. void add_stream_config(const StreamConfig& config) { stream_configs_.push_back(config); }
  50. void clear_stream_configs() { stream_configs_.clear(); }
  51. // 验证配置有效性
  52. bool validate() const;
  53. // 获取配置信息字符串
  54. std::string to_string() const;
  55. private:
  56. GlobalConfig global_config_;
  57. std::vector<StreamConfig> stream_configs_;
  58. // JSON转换辅助函数
  59. boost::json::object stream_config_to_json(const StreamConfig& config) const;
  60. StreamConfig stream_config_from_json(const boost::json::object& json_obj) const;
  61. boost::json::object global_config_to_json(const GlobalConfig& config) const;
  62. GlobalConfig global_config_from_json(const boost::json::object& json_obj) const;
  63. };
  64. } // namespace jtjai_media
  65. #endif // JTJAI_MEDIA_CONFIG_H