#include #include #include #include #include #include "app_config.hpp" #include "config_server.hpp" #include "logger.hpp" #include "mqtt_client_wrapper.hpp" #include "record_manager.hpp" #include "rtmp_manager.hpp" std::atomic g_running(true); static void signal_handler(int signum) { (void)signum; g_running.store(false, std::memory_order_relaxed); const char msg[] = "[MAIN] Signal received, shutting down...\n"; write(STDERR_FILENO, msg, sizeof(msg) - 1); } int main() { struct sigaction sa{}; sa.sa_handler = signal_handler; sigemptyset(&sa.sa_mask); sa.sa_flags = 0; sigaction(SIGINT, &sa, nullptr); sigaction(SIGTERM, &sa, nullptr); signal(SIGPIPE, SIG_IGN); Logger::init(get_executable_dir_file_path("logs"), 7); LOG_INFO("[MAIN] ===== Vehicle Video Service Starting ====="); try { g_app_config = AppConfig::load_from_file(get_executable_dir_file_path("config.json")); LOG_INFO("[MAIN] Loaded config from config.json"); } catch (const std::exception& e) { LOG_ERROR(std::string("[MAIN] Failed to load config: ") + e.what()); return -1; } const std::string config_path = get_executable_dir_file_path("config.json"); g_record_manager = std::make_shared(g_app_config.srs.record_config); ConfigServer config_server; config_server.start(config_path); RTMPManager::init(); LOG_INFO("[MAIN] Starting all record streams..."); RTMPManager::start_all(); std::thread mqtt_thread( [] { try { LOG_INFO("[MAIN] MQTT thread started."); mqtt_client_thread_func(); } catch (const std::exception& e) { LOG_ERROR(std::string("[MAIN] MQTT thread crashed: ") + e.what()); } LOG_INFO("[MAIN] MQTT thread exiting..."); }); while (g_running.load(std::memory_order_relaxed)) std::this_thread::sleep_for(std::chrono::milliseconds(200)); LOG_INFO("[MAIN] Shutdown requested. Stopping RTMP streams..."); if (g_record_manager) g_record_manager->stopAutoScan(); config_server.stop(); RTMPManager::stop_all(); if (mqtt_thread.joinable()) { mqtt_thread.join(); LOG_INFO("[MAIN] MQTT thread joined."); } LOG_INFO("[MAIN] ===== Vehicle Video Service Exited Cleanly ====="); return 0; }