This commit is contained in:
cxh 2025-11-14 16:00:45 +08:00
parent 2af451d123
commit 204f1786b1
3 changed files with 72 additions and 1 deletions

View File

@ -42,6 +42,7 @@ class RecordManager
static int64_t toMsTimestamp(const std::string& s);
static std::string toReadable(int64_t ms);
static std::string loc_to_stream(int loc);
private:
// === SRS 配置相关 ===

View File

@ -110,7 +110,69 @@ static void handle_video_down_request(const nlohmann::json& req)
mqtt_client->publish(g_app_config.mqtt.topics.video_down, resp.dump(-1), 1);
}
static void handle_record_query_request(const nlohmann::json& req) {}
static void handle_record_query_request(const nlohmann::json& req)
{
// 1. 解析字段
if (!req.contains("data") || !req["data"].is_object())
{
LOG_WARN("[record_query] missing data");
return;
}
const auto& d = req["data"];
int loc = d.value("loc", -1);
int64_t start = d.value("startTime", -1LL);
int64_t end = d.value("endTime", -1LL);
if (loc < 0 || start <= 0 || end <= 0)
{
LOG_WARN("[record_query] invalid parameters");
return;
}
LOG_INFO("[record_query] loc=" + std::to_string(loc) + " start=" + std::to_string(start) +
" end=" + std::to_string(end));
// 2. 计算 stream 名
// loc → stream比如
// 0 → "AHD1_main"
std::string stream = RTMPManager::loc_to_stream(loc);
if (stream.empty())
{
LOG_WARN("[record_query] invalid loc: " + std::to_string(loc));
return;
}
// 3. 向 RecordManager 查询录像段
auto segs = g_record_manager->querySegments(stream, start, end);
LOG_INFO("[record_query] Found segments=" + std::to_string(segs.size()));
// 4. 构造响应 JSON
nlohmann::json resp;
resp["type"] = "response";
resp["seqNo"] = req.value("seqNo", "0");
nlohmann::json data;
data["loc"] = loc;
data["segments"] = nlohmann::json::array();
int idx = 1;
for (const auto& seg : segs)
{
nlohmann::json s;
s["index"] = idx++;
s["segmentId"] = seg.segment_id;
s["startTime"] = seg.start_ms;
s["endTime"] = seg.end_ms;
data["segments"].push_back(s);
}
resp["data"] = data;
// 5. 发送 MQTT
mqtt_client->publish(g_app_config.mqtt.topics.record_query, resp.dump(-1), 1);
}
static void handle_record_play_request(const nlohmann::json& req) {}

View File

@ -329,3 +329,11 @@ std::string RecordManager::toReadable(int64_t ms)
strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", tm);
return buf;
}
std::string RecordManager::loc_to_stream(int loc)
{
// loc 必须 0~7
if (loc < 0 || loc > 7) return "";
return "AHD" + std::to_string(loc + 1) + "_main";
}