yituo_video/src/main.cpp

84 lines
2.2 KiB
C++

// main.cpp
#include <unistd.h>
#include <atomic>
#include <chrono>
#include <csignal>
#include <thread>
#include "app_config.hpp"
#include "logger.hpp"
#include "mqtt_client_wrapper.hpp"
#include "rtmp_manager.hpp"
#include "serial_AT.hpp"
std::atomic<bool> g_running(true);
// ---------- 信号处理 ----------
static void signal_handler(int)
{
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::set_log_to_file(get_executable_dir_file_path("app.log"));
LOG_INFO("[MAIN] ===== Video RTMP Publisher Starting =====");
// ---------- 配置 ----------
try
{
g_app_config = AppConfig::load_from_file(get_executable_dir_file_path("config.json"));
LOG_INFO("[MAIN] Config loaded.");
}
catch (const std::exception& e)
{
LOG_ERROR(std::string("[MAIN] Failed to load config: ") + e.what());
return -1;
}
init_serial_at("/dev/ttyUSB3", 115200);
// ---------- GStreamer ----------
RTMPManager::init();
// ---------- 启动 RTMP 推流 ----------
LOG_INFO("[MAIN] Starting RTMP pipelines...");
RTMPManager::start_all();
// ---------- MQTT ----------
std::thread mqtt_thread(
[]
{
LOG_INFO("[MAIN] MQTT thread started.");
mqtt_client_thread_func();
LOG_INFO("[MAIN] MQTT thread exited.");
});
// ---------- 主循环 ----------
while (g_running.load(std::memory_order_relaxed)) std::this_thread::sleep_for(std::chrono::milliseconds(200));
// ---------- 退出 ----------
LOG_INFO("[MAIN] Shutdown requested. Stopping services...");
stop_serial_at();
RTMPManager::stop_all();
if (mqtt_thread.joinable()) mqtt_thread.join();
LOG_INFO("[MAIN] ===== Video RTMP Publisher Exited Cleanly =====");
return 0;
}