main.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. #include <iostream>
  2. #include <signal.h>
  3. #include <chrono>
  4. #include <thread>
  5. #include <iomanip>
  6. #include <sstream>
  7. #include <filesystem>
  8. #include "config.h"
  9. #include "scheduler.h"
  10. #include "reporter.h"
  11. #include "http_server.h"
  12. using namespace jtjai_media;
  13. // 全局变量用于信号处理
  14. static std::unique_ptr<StreamScheduler> g_scheduler;
  15. static std::unique_ptr<HttpServer> g_http_server;
  16. static std::atomic<bool> g_interrupted(false);
  17. // 信号处理函数
  18. void signal_handler(int signal) {
  19. std::cout << "\n接收到信号 " << signal << ",正在停止程序..." << std::endl;
  20. g_interrupted.store(true);
  21. if (g_scheduler) {
  22. g_scheduler->stop_execution();
  23. }
  24. if (g_http_server) {
  25. g_http_server->stop();
  26. }
  27. }
  28. // 进度回调函数
  29. void progress_callback(const StreamScheduler::SchedulerStats& stats) {
  30. std::cout << "\r进度: " << stats.completed_tasks << "/" << stats.total_tasks
  31. << " 完成, " << stats.failed_tasks << " 失败, "
  32. << "当前并发: " << stats.max_concurrent_used << " 流" << std::flush;
  33. }
  34. // 生成时间戳字符串
  35. std::string generate_timestamp() {
  36. auto now = std::chrono::system_clock::now();
  37. auto time_t = std::chrono::system_clock::to_time_t(now);
  38. auto tm = *std::localtime(&time_t);
  39. std::stringstream ss;
  40. ss << std::put_time(&tm, "%Y%m%d_%H%M%S");
  41. return ss.str();
  42. }
  43. // 报告生成回调函数
  44. void report_callback(const ConfigManager& config_mgr, int cycle_number,
  45. const std::string& cycle_output_dir,
  46. const std::vector<RTSPClientStats>& client_stats,
  47. const StreamScheduler::SchedulerStats& scheduler_stats) {
  48. std::cout << "生成第 " << cycle_number << " 轮执行报告..." << std::endl;
  49. // 为每个周期使用独立的时间戳目录
  50. ConfigManager cycle_config = config_mgr;
  51. auto cycle_global_config = cycle_config.get_global_config();
  52. // 使用传入的时间戳目录
  53. cycle_global_config.output_directory = cycle_output_dir;
  54. cycle_config.set_global_config(cycle_global_config);
  55. ResultReporter cycle_reporter(cycle_config);
  56. if (cycle_reporter.generate_report(client_stats, scheduler_stats)) {
  57. std::cout << "第 " << cycle_number << " 轮报告生成成功" << std::endl;
  58. std::cout << "输出目录: " << cycle_output_dir << std::endl;
  59. } else {
  60. std::cerr << "第 " << cycle_number << " 轮报告生成失败" << std::endl;
  61. }
  62. }
  63. int main(int argc, char* argv[]) {
  64. std::cout << "========================================" << std::endl;
  65. std::cout << "RTSP视频流并发拉取系统" << std::endl;
  66. std::cout << "========================================" << std::endl;
  67. // 注册信号处理器
  68. signal(SIGINT, signal_handler);
  69. signal(SIGTERM, signal_handler);
  70. // 解析命令行参数
  71. std::string config_file = "/Users/wenhongquan/CLionProjects/jtjai_media/config.json";
  72. if (argc > 1) {
  73. config_file = argv[1];
  74. }
  75. std::cout << "使用配置文件: " << config_file << std::endl;
  76. try {
  77. // 加载配置
  78. ConfigManager config_mgr;
  79. if (!config_mgr.load_from_file(config_file)) {
  80. std::cerr << "加载配置文件失败: " << config_file << std::endl;
  81. return 1;
  82. }
  83. std::cout << "配置加载成功" << std::endl;
  84. std::cout << config_mgr.to_string() << std::endl;
  85. const auto& global_config = config_mgr.get_global_config();
  86. // 启动HTTP服务器
  87. std::cout << "\n启动HTTP管理服务器..." << std::endl;
  88. g_http_server = std::make_unique<HttpServer>(global_config.output_directory, 8080);
  89. if (!g_http_server->start()) {
  90. std::cerr << "启动HTTP服务器失败" << std::endl;
  91. return 1;
  92. }
  93. std::cout << "\n🌐 Web管理界面: http://localhost:8080" << std::endl;
  94. std::cout << "📁 文件管理: http://localhost:8080/manager" << std::endl;
  95. std::cout << "📊 API文档: http://localhost:8080/api" << std::endl;
  96. int current_cycle = 0;
  97. // 开始多轮询循环
  98. while ((global_config.poll_cycles == -1 || current_cycle < global_config.poll_cycles) && !g_interrupted.load()) {
  99. current_cycle++;
  100. std::cout << "\n========== 轮询周期 " << current_cycle;
  101. if (global_config.poll_cycles != -1) {
  102. std::cout << "/" << global_config.poll_cycles;
  103. }
  104. std::cout << " ==========" << std::endl;
  105. // 为当前周期创建时间戳目录
  106. std::string timestamp = generate_timestamp();
  107. std::string base_output_dir = global_config.output_directory;
  108. std::string cycle_output_dir = base_output_dir + "/" + timestamp;
  109. // 创建时间戳目录
  110. std::filesystem::create_directories(cycle_output_dir);
  111. std::cout << "当前周期输出目录: " << cycle_output_dir << std::endl;
  112. // 更新配置管理器的输出目录
  113. ConfigManager cycle_config_mgr = config_mgr;
  114. auto cycle_global_config = cycle_config_mgr.get_global_config();
  115. cycle_global_config.output_directory = cycle_output_dir;
  116. cycle_config_mgr.set_global_config(cycle_global_config);
  117. // 使用新的配置创建调度器
  118. g_scheduler = std::make_unique<StreamScheduler>(cycle_config_mgr);
  119. g_scheduler->set_progress_callback(progress_callback);
  120. // 设置当前周期的报告回调
  121. g_scheduler->set_report_callback([&config_mgr, current_cycle, cycle_output_dir](
  122. const std::vector<RTSPClientStats>& client_stats,
  123. const StreamScheduler::SchedulerStats& scheduler_stats) {
  124. report_callback(config_mgr, current_cycle, cycle_output_dir, client_stats, scheduler_stats);
  125. });
  126. // 启动调度执行
  127. std::cout << "开始执行RTSP流拉取任务..." << std::endl;
  128. if (!g_scheduler->start_execution()) {
  129. std::cerr << "启动调度器失败" << std::endl;
  130. return 1;
  131. }
  132. // 等待执行完成或中断
  133. std::cout << "等待任务执行中..." << std::endl;
  134. while (g_scheduler->is_running() && !g_interrupted.load()) {
  135. std::this_thread::sleep_for(std::chrono::milliseconds(500));
  136. }
  137. std::cout << "任务执行完成,等待所有任务线程结束..." << std::endl;
  138. g_scheduler->wait_for_completion();
  139. // 检查是否需要继续下一轮
  140. if (g_interrupted.load()) {
  141. std::cout << "接收到中断信号,退出轮询" << std::endl;
  142. break;
  143. }
  144. if (global_config.poll_cycles == -1 || current_cycle < global_config.poll_cycles) {
  145. std::cout << "\n等待 " << global_config.cycle_interval_seconds << " 秒后开始下一轮询..." << std::endl;
  146. // 在等待期间检查中断信号
  147. for (int i = 0; i < global_config.cycle_interval_seconds && !g_interrupted.load(); ++i) {
  148. std::this_thread::sleep_for(std::chrono::seconds(1));
  149. if ((i + 1) % 10 == 0) {
  150. std::cout << "\r剩余等待时间: " << (global_config.cycle_interval_seconds - i - 1) << " 秒" << std::flush;
  151. }
  152. }
  153. std::cout << std::endl;
  154. }
  155. }
  156. std::cout << "\n程序执行完成" << std::endl;
  157. } catch (const std::exception& e) {
  158. std::cerr << "程序执行异常: " << e.what() << std::endl;
  159. return 1;
  160. }
  161. return 0;
  162. }