kunlang_video/src/main.cpp

85 lines
2.2 KiB
C++
Raw Normal View History

2025-09-08 14:55:07 +08:00
// main.cpp
#include "app_config.hpp"
2025-10-14 17:26:07 +08:00
#include "rtmp_manager.hpp"
2025-09-08 14:55:07 +08:00
#include "logger.hpp"
2025-09-08 15:25:59 +08:00
#include "mqtt_client_wrapper.hpp"
2025-10-15 08:50:01 +08:00
2025-09-08 15:25:59 +08:00
#include <thread>
2025-09-08 14:55:07 +08:00
#include <atomic>
#include <csignal>
2025-10-15 08:50:01 +08:00
#include <unistd.h>
2025-09-10 12:49:41 +08:00
#include <chrono>
2025-09-08 14:55:07 +08:00
2025-10-15 08:50:01 +08:00
static std::atomic<bool> g_running(true);
2025-09-08 14:55:07 +08:00
2025-10-15 08:50:01 +08:00
static void signal_handler(int signum)
2025-09-08 14:55:07 +08:00
{
2025-09-10 12:49:41 +08:00
g_running.store(false, std::memory_order_relaxed);
2025-10-15 08:50:01 +08:00
const char msg[] = "[MAIN] Signal received, shutting down...\n";
2025-09-10 12:49:41 +08:00
write(STDERR_FILENO, msg, sizeof(msg) - 1);
2025-09-08 14:55:07 +08:00
}
int main()
{
2025-10-15 08:50:01 +08:00
// 安装信号处理器
struct sigaction sa{};
sa.sa_handler = signal_handler;
sigemptyset(&sa.sa_mask);
sa.sa_flags = 0;
sigaction(SIGINT, &sa, nullptr);
sigaction(SIGTERM, &sa, nullptr);
2025-09-08 14:55:07 +08:00
signal(SIGPIPE, SIG_IGN);
2025-10-15 08:50:01 +08:00
// 初始化日志
2025-09-08 15:32:07 +08:00
Logger::set_log_to_file(get_executable_dir_file_path("app.log"));
2025-10-15 08:50:01 +08:00
LOG_INFO("[MAIN] Application starting...");
2025-09-08 14:55:07 +08:00
try
{
2025-10-15 08:50:01 +08:00
// 加载配置
2025-09-08 15:32:07 +08:00
g_app_config = AppConfig::load_from_file(get_executable_dir_file_path("config.json"));
2025-10-15 08:50:01 +08:00
LOG_INFO("[MAIN] Loaded config from config.json");
2025-09-08 14:55:07 +08:00
}
catch (const std::exception &e)
{
2025-10-15 08:50:01 +08:00
LOG_ERROR(std::string("[MAIN] Failed to load config: ") + e.what());
2025-09-08 14:55:07 +08:00
return -1;
}
2025-10-15 08:50:01 +08:00
// 初始化 GStreamer
2025-10-14 17:29:53 +08:00
RTMPManager::init();
2025-09-09 11:08:34 +08:00
2025-10-15 08:50:01 +08:00
// 启动 MQTT 线程
std::thread mqtt_thread([]
{
try
2025-09-10 12:49:41 +08:00
{
2025-10-15 08:50:01 +08:00
LOG_INFO("[MAIN] MQTT thread started.");
mqtt_client_thread_func(); // 在回调里执行推流控制
2025-09-10 12:49:41 +08:00
}
2025-10-15 08:50:01 +08:00
catch (const std::exception &e)
2025-09-10 12:49:41 +08:00
{
2025-10-15 08:50:01 +08:00
LOG_ERROR(std::string("[MAIN] MQTT thread crashed: ") + e.what());
2025-09-10 12:49:41 +08:00
}
2025-10-15 08:50:01 +08:00
LOG_INFO("[MAIN] MQTT thread exiting..."); });
2025-09-10 12:49:41 +08:00
2025-10-15 08:50:01 +08:00
// 主循环,仅等待退出信号
while (g_running.load(std::memory_order_relaxed))
std::this_thread::sleep_for(std::chrono::milliseconds(200));
LOG_INFO("[MAIN] Shutdown requested. Cleaning up...");
2025-09-10 13:03:16 +08:00
2025-10-15 08:50:01 +08:00
// 停止所有 RTMP 流
RTMPManager::stop_all();
// 等待 MQTT 线程退出
if (mqtt_thread.joinable())
2025-09-10 12:49:41 +08:00
{
2025-10-15 08:50:01 +08:00
mqtt_thread.join();
LOG_INFO("[MAIN] MQTT thread joined.");
2025-09-10 11:06:42 +08:00
}
2025-09-08 14:55:07 +08:00
2025-10-15 08:50:01 +08:00
LOG_INFO("[MAIN] Application exited cleanly.");
2025-09-08 14:55:07 +08:00
return 0;
2025-10-15 08:50:01 +08:00
}