2025-09-08 15:09:33 +08:00
|
|
|
// mqtt_client_wrapper.cpp
|
2025-09-08 10:59:08 +08:00
|
|
|
#include "mqtt_client_wrapper.hpp"
|
2025-10-15 15:01:55 +08:00
|
|
|
|
|
|
|
|
#include <algorithm>
|
2025-09-10 10:34:36 +08:00
|
|
|
#include <chrono>
|
2025-10-15 17:01:43 +08:00
|
|
|
#include <condition_variable>
|
|
|
|
|
#include <mutex>
|
2025-09-10 10:34:36 +08:00
|
|
|
#include <nlohmann/json.hpp>
|
2025-10-15 17:01:43 +08:00
|
|
|
#include <queue>
|
2025-10-15 15:01:55 +08:00
|
|
|
#include <thread>
|
2025-09-08 10:59:08 +08:00
|
|
|
|
|
|
|
|
std::shared_ptr<MQTTClient> mqtt_client;
|
|
|
|
|
std::atomic<bool> mqtt_restart_required{false};
|
2025-09-08 15:45:22 +08:00
|
|
|
extern std::atomic<bool> g_running;
|
2025-09-10 10:34:36 +08:00
|
|
|
std::atomic<bool> g_streaming{false};
|
2025-09-09 16:10:56 +08:00
|
|
|
std::string g_dispatch_id;
|
|
|
|
|
std::mutex g_dispatch_id_mutex;
|
|
|
|
|
|
2025-09-09 16:04:58 +08:00
|
|
|
static void send_heartbeat()
|
|
|
|
|
{
|
2025-10-15 15:01:55 +08:00
|
|
|
if (!mqtt_client || !mqtt_client->isConnected()) return;
|
2025-09-09 16:04:58 +08:00
|
|
|
|
|
|
|
|
nlohmann::json hb_data;
|
2025-09-10 10:34:36 +08:00
|
|
|
hb_data["time"] = Logger::get_current_time_utc8();
|
2025-09-09 16:45:54 +08:00
|
|
|
hb_data["status"] = g_streaming ? 0 : 2;
|
|
|
|
|
|
2025-09-10 10:34:36 +08:00
|
|
|
{
|
|
|
|
|
std::lock_guard<std::mutex> lock(g_dispatch_id_mutex);
|
2025-09-10 10:37:13 +08:00
|
|
|
hb_data["dispatchId"] = g_streaming ? g_dispatch_id : "";
|
2025-09-10 10:34:36 +08:00
|
|
|
}
|
2025-09-09 16:04:58 +08:00
|
|
|
|
|
|
|
|
nlohmann::json msg;
|
|
|
|
|
msg["data"] = hb_data;
|
|
|
|
|
msg["isEnc"] = 0;
|
|
|
|
|
msg["type"] = 0;
|
|
|
|
|
|
|
|
|
|
mqtt_client->publish(g_app_config.mqtt.topics.heartbeat_up, msg.dump());
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-10 13:21:43 +08:00
|
|
|
// MQTT 回调
|
2025-09-08 10:59:08 +08:00
|
|
|
static void on_mqtt_connected()
|
|
|
|
|
{
|
2025-09-08 16:40:04 +08:00
|
|
|
LOG_INFO("[MQTT] Connected to broker: " + g_app_config.mqtt.server_ip);
|
2025-09-08 15:09:33 +08:00
|
|
|
|
|
|
|
|
const auto &topics = g_app_config.mqtt.topics;
|
|
|
|
|
mqtt_client->subscribe(topics.video_down);
|
2025-09-08 10:59:08 +08:00
|
|
|
}
|
|
|
|
|
|
2025-10-15 15:01:55 +08:00
|
|
|
static void on_mqtt_disconnected() { LOG_WARN("[MQTT] Disconnected from broker: " + g_app_config.mqtt.server_ip); }
|
2025-09-08 10:59:08 +08:00
|
|
|
|
|
|
|
|
static void on_mqtt_message_received(const std::string &topic, const std::string &message)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2025-10-14 17:13:08 +08:00
|
|
|
auto j = nlohmann::json::parse(message);
|
|
|
|
|
|
2025-10-15 17:01:43 +08:00
|
|
|
if (topic != g_app_config.mqtt.topics.video_down || j["type"] != "request") return;
|
2025-09-09 16:45:54 +08:00
|
|
|
|
2025-10-15 17:01:43 +08:00
|
|
|
LOG_INFO("[MQTT] Received message on topic [" + topic + "], len = " + std::to_string(message.size()));
|
|
|
|
|
LOG_INFO("[MQTT] Message content: " + j.dump(-1));
|
2025-09-09 09:57:24 +08:00
|
|
|
|
2025-10-15 17:01:43 +08:00
|
|
|
VideoPushRequest req;
|
|
|
|
|
req.seqNo = j.value("seqNo", "");
|
2025-10-15 15:01:55 +08:00
|
|
|
|
2025-10-15 17:01:43 +08:00
|
|
|
auto data = j["data"];
|
|
|
|
|
if (data.is_object())
|
|
|
|
|
{
|
|
|
|
|
VideoPushRequest::DataItem item;
|
|
|
|
|
item.switchVal = data.value("switch", 0);
|
|
|
|
|
item.streamType = data.value("streamType", 0);
|
|
|
|
|
item.channels = data.value("channels", std::vector<int>{});
|
|
|
|
|
req.data.push_back(item);
|
|
|
|
|
}
|
|
|
|
|
else if (data.is_array())
|
|
|
|
|
{
|
|
|
|
|
for (auto &d : data)
|
2025-10-14 17:40:48 +08:00
|
|
|
{
|
2025-10-15 17:01:43 +08:00
|
|
|
VideoPushRequest::DataItem item;
|
|
|
|
|
item.switchVal = d.value("switch", 0);
|
|
|
|
|
item.streamType = d.value("streamType", 0);
|
|
|
|
|
item.channels = d.value("channels", std::vector<int>{});
|
|
|
|
|
req.data.push_back(item);
|
2025-09-09 15:07:51 +08:00
|
|
|
}
|
2025-09-09 09:57:24 +08:00
|
|
|
}
|
2025-10-15 17:01:43 +08:00
|
|
|
|
2025-10-16 10:25:51 +08:00
|
|
|
std::thread(
|
|
|
|
|
[req]()
|
|
|
|
|
{
|
|
|
|
|
// 调用 RTMP 模块执行批量推流启停
|
|
|
|
|
auto results = RTMPManager::process_push_request(req);
|
|
|
|
|
|
|
|
|
|
// 组装 MQTT 回复
|
|
|
|
|
nlohmann::json reply;
|
|
|
|
|
reply["type"] = "response";
|
|
|
|
|
reply["seqNo"] = req.seqNo;
|
|
|
|
|
reply["data"] = nlohmann::json::array();
|
|
|
|
|
|
|
|
|
|
for (const auto &r : results)
|
|
|
|
|
{
|
|
|
|
|
nlohmann::json item;
|
|
|
|
|
item["loc"] = r.loc;
|
|
|
|
|
item["url"] = r.url;
|
|
|
|
|
item["result"] = r.result;
|
|
|
|
|
item["reason"] = r.reason;
|
|
|
|
|
reply["data"].push_back(item);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 发送回复到 MQTT
|
|
|
|
|
mqtt_client.publish(g_app_config.mqtt.topics.video_up, reply.dump());
|
|
|
|
|
LOG_INFO("[MQTT] Sent RTMP response: " + reply.dump());
|
|
|
|
|
})
|
|
|
|
|
.detach();
|
2025-10-14 17:40:48 +08:00
|
|
|
}
|
|
|
|
|
catch (const std::exception &e)
|
|
|
|
|
{
|
|
|
|
|
LOG_ERROR(std::string("[MQTT] Failed to process incoming JSON: ") + e.what());
|
2025-09-08 10:59:08 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void mqtt_client_thread_func()
|
|
|
|
|
{
|
2025-09-08 15:45:22 +08:00
|
|
|
const auto &cfg = g_app_config.mqtt;
|
2025-09-09 16:32:28 +08:00
|
|
|
auto heartbeat_interval = std::chrono::milliseconds(static_cast<int>(cfg.keep_alive * 0.9));
|
2025-09-08 15:09:33 +08:00
|
|
|
|
2025-09-08 15:45:22 +08:00
|
|
|
while (g_running)
|
|
|
|
|
{
|
2025-09-08 10:59:08 +08:00
|
|
|
mqtt_client = std::make_unique<MQTTClient>(cfg);
|
|
|
|
|
mqtt_client->setConnectCallback(on_mqtt_connected);
|
|
|
|
|
mqtt_client->setDisconnectCallback(on_mqtt_disconnected);
|
|
|
|
|
mqtt_client->setMessageCallback(on_mqtt_message_received);
|
|
|
|
|
|
2025-09-10 13:33:29 +08:00
|
|
|
// 等待连接
|
2025-09-10 10:34:36 +08:00
|
|
|
while (g_running && !mqtt_client->isConnected())
|
|
|
|
|
{
|
2025-09-10 13:33:29 +08:00
|
|
|
mqtt_client->connect();
|
2025-09-10 11:06:42 +08:00
|
|
|
for (int i = 0; i < 10 && g_running && !mqtt_client->isConnected(); i++)
|
2025-09-10 13:21:43 +08:00
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(50));
|
|
|
|
|
|
2025-10-15 15:01:55 +08:00
|
|
|
if (!g_running && !mqtt_client->isConnected()) mqtt_client->force_disconnect();
|
2025-09-10 10:34:36 +08:00
|
|
|
}
|
2025-09-08 10:59:08 +08:00
|
|
|
|
2025-09-10 13:33:29 +08:00
|
|
|
// 主循环:心跳
|
2025-09-10 10:34:36 +08:00
|
|
|
while (g_running && mqtt_client->isConnected())
|
2025-09-08 10:59:08 +08:00
|
|
|
{
|
2025-09-10 13:33:29 +08:00
|
|
|
send_heartbeat();
|
2025-09-10 13:21:43 +08:00
|
|
|
|
2025-09-10 11:06:42 +08:00
|
|
|
auto sleep_time = heartbeat_interval;
|
|
|
|
|
while (sleep_time.count() > 0 && g_running && mqtt_client->isConnected())
|
|
|
|
|
{
|
2025-09-10 13:21:43 +08:00
|
|
|
auto chunk = std::min(sleep_time, std::chrono::milliseconds(50));
|
2025-09-10 11:06:42 +08:00
|
|
|
std::this_thread::sleep_for(chunk);
|
|
|
|
|
sleep_time -= chunk;
|
|
|
|
|
}
|
2025-09-10 13:21:43 +08:00
|
|
|
|
2025-10-15 15:01:55 +08:00
|
|
|
if (!g_running && mqtt_client->isConnected()) mqtt_client->force_disconnect();
|
2025-09-08 10:59:08 +08:00
|
|
|
}
|
|
|
|
|
|
2025-09-10 13:33:29 +08:00
|
|
|
// 清理
|
2025-09-10 11:06:42 +08:00
|
|
|
if (mqtt_client)
|
|
|
|
|
{
|
2025-09-10 11:18:29 +08:00
|
|
|
if (g_running)
|
|
|
|
|
mqtt_client->disconnect();
|
|
|
|
|
else
|
|
|
|
|
mqtt_client->force_disconnect();
|
2025-09-10 11:06:42 +08:00
|
|
|
mqtt_client.reset();
|
|
|
|
|
}
|
2025-09-10 11:18:29 +08:00
|
|
|
|
2025-09-08 10:59:08 +08:00
|
|
|
mqtt_restart_required = false;
|
2025-09-10 11:18:29 +08:00
|
|
|
|
2025-10-15 15:01:55 +08:00
|
|
|
if (!g_running) break;
|
2025-09-10 11:18:29 +08:00
|
|
|
|
2025-09-10 13:33:29 +08:00
|
|
|
// 短暂等待再重连
|
2025-10-15 15:01:55 +08:00
|
|
|
for (int i = 0; i < 5 && g_running; i++) std::this_thread::sleep_for(std::chrono::milliseconds(200));
|
2025-09-08 10:59:08 +08:00
|
|
|
}
|
2025-09-08 15:45:22 +08:00
|
|
|
LOG_INFO("[MQTT] Client thread exiting.");
|
2025-09-08 10:59:08 +08:00
|
|
|
}
|