first commit
This commit is contained in:
parent
649b6cf0a2
commit
84be23c2dc
13
src/main.cpp
13
src/main.cpp
@ -17,7 +17,8 @@ void signalHandler(int signum)
|
|||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
signal(SIGINT, signalHandler);
|
signal(SIGINT, [](int)
|
||||||
|
{ g_running = false; RTSPManager::stop(); });
|
||||||
signal(SIGPIPE, SIG_IGN);
|
signal(SIGPIPE, SIG_IGN);
|
||||||
|
|
||||||
// 初始化日志文件
|
// 初始化日志文件
|
||||||
@ -34,16 +35,18 @@ int main()
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 启动 RTSP
|
|
||||||
RTSPManager::init();
|
|
||||||
std::thread rtsp_thread([&]()
|
std::thread rtsp_thread([&]()
|
||||||
{ RTSPManager::start(g_app_config.cameras); });
|
{ RTSPManager::start(g_app_config.cameras); });
|
||||||
|
|
||||||
// 启动 MQTT 客户端线程
|
|
||||||
std::thread mqtt_thread(mqtt_client_thread_func);
|
std::thread mqtt_thread(mqtt_client_thread_func);
|
||||||
|
|
||||||
// 等待退出信号
|
// 等待退出信号
|
||||||
|
while (g_running)
|
||||||
|
std::this_thread::sleep_for(std::chrono::milliseconds(200));
|
||||||
|
|
||||||
|
// 等待退出信号
|
||||||
|
if (rtsp_thread.joinable())
|
||||||
rtsp_thread.join();
|
rtsp_thread.join();
|
||||||
|
if (mqtt_thread.joinable())
|
||||||
mqtt_thread.join();
|
mqtt_thread.join();
|
||||||
|
|
||||||
LOG_INFO("[MAIN] Program exited cleanly.");
|
LOG_INFO("[MAIN] Program exited cleanly.");
|
||||||
|
|||||||
@ -54,25 +54,30 @@ void RTSPManager::start(const std::vector<Camera> &cameras)
|
|||||||
|
|
||||||
LOG_INFO("[RTSP] Server running on rtsp://localhost:8554");
|
LOG_INFO("[RTSP] Server running on rtsp://localhost:8554");
|
||||||
g_main_loop_run(loop);
|
g_main_loop_run(loop);
|
||||||
|
|
||||||
|
// loop 退出后再释放资源
|
||||||
|
if (server)
|
||||||
|
{
|
||||||
|
g_object_unref(server);
|
||||||
|
server = nullptr;
|
||||||
|
}
|
||||||
|
if (loop)
|
||||||
|
{
|
||||||
|
g_main_loop_unref(loop);
|
||||||
|
loop = nullptr;
|
||||||
|
}
|
||||||
|
LOG_INFO("[RTSP] Server stopped.");
|
||||||
}
|
}
|
||||||
|
|
||||||
void RTSPManager::stop()
|
void RTSPManager::stop()
|
||||||
{
|
{
|
||||||
if (loop)
|
if (loop)
|
||||||
{
|
{
|
||||||
// 在 loop 所在线程安全退出
|
// 仅退出 loop,不 unref server
|
||||||
g_main_context_invoke(nullptr, [](gpointer data) -> gboolean
|
g_main_context_invoke(nullptr, [](gpointer data) -> gboolean
|
||||||
{
|
{
|
||||||
GMainLoop *loop = static_cast<GMainLoop *>(data);
|
GMainLoop *loop = static_cast<GMainLoop*>(data);
|
||||||
g_main_loop_quit(loop);
|
g_main_loop_quit(loop);
|
||||||
return G_SOURCE_REMOVE; }, loop);
|
return G_SOURCE_REMOVE; }, loop);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (server)
|
|
||||||
{
|
|
||||||
g_object_unref(server);
|
|
||||||
server = nullptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
LOG_INFO("[RTSP] Server stopped.");
|
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user