yituo_video/src/main.cpp

84 lines
2.2 KiB
C++
Raw Normal View History

2025-09-08 14:55:07 +08:00
// main.cpp
2025-11-24 14:49:37 +08:00
#include <unistd.h>
2025-12-17 17:21:11 +08:00
#include <atomic>
2025-09-10 12:49:41 +08:00
#include <chrono>
2025-12-17 17:21:11 +08:00
#include <csignal>
#include <thread>
2025-09-08 14:55:07 +08:00
2025-12-17 17:21:11 +08:00
#include "app_config.hpp"
#include "logger.hpp"
#include "mqtt_client_wrapper.hpp"
#include "rtmp_manager.hpp"
2026-01-06 09:38:59 +08:00
#include "serial_AT.hpp"
2025-09-10 13:03:16 +08:00
2025-09-08 14:55:07 +08:00
std::atomic<bool> g_running(true);
2025-12-17 17:21:11 +08:00
// ---------- 信号处理 ----------
static void signal_handler(int)
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-12-17 17:21:11 +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-12-17 17:21:11 +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-12-17 17:21:11 +08:00
// ---------- 日志 ----------
2025-12-17 17:27:52 +08:00
Logger::set_log_to_file(get_executable_dir_file_path("app.log"));
2025-12-17 17:21:11 +08:00
LOG_INFO("[MAIN] ===== Video RTMP Publisher Starting =====");
2025-09-08 14:55:07 +08:00
2025-12-17 17:21:11 +08:00
// ---------- 配置 ----------
2025-09-08 14:55:07 +08:00
try
{
2025-09-08 15:32:07 +08:00
g_app_config = AppConfig::load_from_file(get_executable_dir_file_path("config.json"));
2025-12-17 17:21:11 +08:00
LOG_INFO("[MAIN] Config loaded.");
2025-09-08 14:55:07 +08:00
}
2025-12-17 17:21:11 +08:00
catch (const std::exception& e)
2025-09-08 14:55:07 +08:00
{
2025-12-17 17:21:11 +08:00
LOG_ERROR(std::string("[MAIN] Failed to load config: ") + e.what());
2025-09-08 14:55:07 +08:00
return -1;
}
2026-01-06 09:38:59 +08:00
init_serial_at("/dev/ttyUSB3", 115200);
2025-12-17 17:21:11 +08:00
// ---------- GStreamer ----------
RTMPManager::init();
2025-11-24 14:49:37 +08:00
2025-12-17 17:21:11 +08:00
// ---------- 启动 RTMP 推流 ----------
LOG_INFO("[MAIN] Starting RTMP pipelines...");
RTMPManager::start_all();
2025-09-10 12:49:41 +08:00
2025-12-17 17:21:11 +08:00
// ---------- MQTT ----------
std::thread mqtt_thread(
[]
{
LOG_INFO("[MAIN] MQTT thread started.");
2025-09-10 13:03:16 +08:00
mqtt_client_thread_func();
2025-12-17 17:21:11 +08:00
LOG_INFO("[MAIN] MQTT thread exited.");
});
2025-09-10 13:03:16 +08:00
2025-12-17 17:21:11 +08:00
// ---------- 主循环 ----------
while (g_running.load(std::memory_order_relaxed)) std::this_thread::sleep_for(std::chrono::milliseconds(200));
2025-11-20 11:06:02 +08:00
2025-12-17 17:21:11 +08:00
// ---------- 退出 ----------
LOG_INFO("[MAIN] Shutdown requested. Stopping services...");
2025-09-10 13:03:16 +08:00
2026-01-06 09:38:59 +08:00
stop_serial_at();
2025-12-17 17:21:11 +08:00
RTMPManager::stop_all();
2025-09-10 13:03:16 +08:00
2025-12-17 17:21:11 +08:00
if (mqtt_thread.joinable()) mqtt_thread.join();
2025-09-08 14:55:07 +08:00
2025-12-17 17:21:11 +08:00
LOG_INFO("[MAIN] ===== Video RTMP Publisher Exited Cleanly =====");
2025-09-08 14:55:07 +08:00
return 0;
2025-11-24 14:49:37 +08:00
}