This commit is contained in:
cxh 2025-10-14 17:37:56 +08:00
parent 448ea73af9
commit ec4790e65d

View File

@ -66,56 +66,49 @@ static void on_mqtt_message_received(const std::string &topic, const std::string
int switch_val = data.value("switch", 0);
int streamType = data.value("streamType", 0); // 0主 1子
StreamType type = (streamTypeInt == 0) ? StreamType::MAIN : StreamType::SUB;
StreamType type = (streamType == 0) ? StreamType::MAIN : StreamType::SUB;
auto channels = data.value("channels", std::vector<int>{});
nlohmann::json resp_data = nlohmann::json::array();
for (int ch : channels)
{
// 根据通道号找到摄像头
auto it = std::find_if(g_app_config.cameras.begin(), g_app_config.cameras.end(),
[ch](const Camera &c)
{ return c.channel == ch; });
nlohmann::json ch_resp;
ch_resp["loc"] = ch;
if (it != g_app_config.cameras.end())
// 用数组索引找到摄像头
if (ch < 0 || ch >= static_cast<int>(g_app_config.cameras.size()))
{
Camera &cam = *it;
bool op_result = false;
LOG_WARN("[MQTT] Invalid channel: " + std::to_string(ch));
continue;
}
if (switch_val == 0)
{
// 启动流
if (!RTMPManager::is_streaming(cam.name, type))
{
RTMPManager::start_camera(cam, type);
op_result = true;
}
}
else
{
// 停止流
if (RTMPManager::is_streaming(cam.name, type))
{
RTMPManager::stop_camera(cam.name, type);
op_result = true;
}
}
Camera &cam = g_app_config.cameras[ch];
ch_resp["url"] = op_result ? RTMPManager::get_stream_url(cam.name, type) : "";
ch_resp["result"] = op_result ? 0 : 1;
ch_resp["reason"] = op_result ? "" : "already in requested state";
bool op_result = false;
if (switch_val == 0)
{
// 启动流
if (!RTMPManager::is_streaming(cam.name, type))
{
RTMPManager::start_camera(cam, type);
op_result = true;
}
}
else
{
ch_resp["url"] = "";
ch_resp["result"] = 1;
ch_resp["reason"] = "channel not found";
// 停止流
if (RTMPManager::is_streaming(cam.name, type))
{
RTMPManager::stop_camera(cam.name, type);
op_result = true;
}
}
nlohmann::json ch_resp;
ch_resp["loc"] = ch;
ch_resp["url"] = op_result ? RTMPManager::get_stream_url(cam.name, type) : "";
ch_resp["result"] = op_result ? 0 : 1;
ch_resp["reason"] = op_result ? "" : "already in requested state";
resp_data.push_back(ch_resp);
}