2025-11-14 09:42:17 +08:00
|
|
|
#include "record_manager.hpp"
|
|
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
#include <ctime>
|
2025-11-14 14:21:06 +08:00
|
|
|
#include <fstream>
|
2025-11-14 09:42:17 +08:00
|
|
|
#include <iostream>
|
|
|
|
|
|
2025-11-14 14:31:04 +08:00
|
|
|
#include "logger.hpp"
|
|
|
|
|
|
2025-11-14 09:42:17 +08:00
|
|
|
namespace fs = std::filesystem;
|
|
|
|
|
|
2025-11-14 14:13:14 +08:00
|
|
|
//
|
|
|
|
|
// 构造函数:自动加载 SRS 配置
|
|
|
|
|
//
|
|
|
|
|
RecordManager::RecordManager(const std::string& srs_record_cfg_path) : srs_record_cfg_path_(srs_record_cfg_path)
|
|
|
|
|
{
|
2025-11-14 14:31:04 +08:00
|
|
|
LOG_INFO("[RecordManager] Loading SRS config: " + srs_record_cfg_path_);
|
2025-11-14 14:13:14 +08:00
|
|
|
|
|
|
|
|
bool ok = loadSrsConfig();
|
|
|
|
|
if (!ok)
|
|
|
|
|
{
|
2025-11-14 14:31:04 +08:00
|
|
|
LOG_ERROR("[RecordManager] Failed to load SRS config.");
|
2025-11-14 14:41:55 +08:00
|
|
|
return;
|
2025-11-14 14:13:14 +08:00
|
|
|
}
|
2025-11-14 14:41:55 +08:00
|
|
|
|
|
|
|
|
LOG_INFO("[RecordManager] SRS config loaded successfully.");
|
|
|
|
|
LOG_INFO("record_dir = " + record_dir_);
|
|
|
|
|
LOG_INFO("dvr_duration_sec = " + std::to_string(dvr_duration_sec_));
|
|
|
|
|
|
|
|
|
|
// 自动根据 dvr_duration 启动扫描线程
|
|
|
|
|
startAutoScan(dvr_duration_sec_);
|
2025-11-14 14:13:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
|
// 解析 SRS DVR 配置,提取 record_dir_ 与 dvr_duration_sec_
|
|
|
|
|
//
|
|
|
|
|
bool RecordManager::loadSrsConfig()
|
|
|
|
|
{
|
|
|
|
|
std::ifstream ifs(srs_record_cfg_path_);
|
|
|
|
|
if (!ifs.is_open())
|
|
|
|
|
{
|
2025-11-14 14:31:04 +08:00
|
|
|
LOG_ERROR("[RecordManager] Cannot open SRS config file: " + srs_record_cfg_path_);
|
2025-11-14 14:13:14 +08:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::string line;
|
|
|
|
|
std::string dvr_path;
|
2025-11-14 14:31:04 +08:00
|
|
|
|
2025-11-14 14:13:14 +08:00
|
|
|
while (std::getline(ifs, line))
|
|
|
|
|
{
|
|
|
|
|
line.erase(0, line.find_first_not_of(" \t"));
|
|
|
|
|
if (line.empty() || line[0] == '#') continue;
|
|
|
|
|
|
2025-11-14 14:31:04 +08:00
|
|
|
// 解析 dvr_path
|
2025-11-14 14:13:14 +08:00
|
|
|
if (line.find("dvr_path") != std::string::npos)
|
|
|
|
|
{
|
|
|
|
|
auto pos = line.find('/');
|
|
|
|
|
auto semicolon = line.find(';');
|
|
|
|
|
if (pos != std::string::npos && semicolon != std::string::npos)
|
|
|
|
|
dvr_path = line.substr(pos, semicolon - pos);
|
|
|
|
|
}
|
2025-11-14 14:31:04 +08:00
|
|
|
|
|
|
|
|
// 解析 dvr_duration
|
2025-11-14 14:13:14 +08:00
|
|
|
else if (line.find("dvr_duration") != std::string::npos)
|
|
|
|
|
{
|
|
|
|
|
int sec = 0;
|
2025-11-14 14:31:04 +08:00
|
|
|
if (sscanf(line.c_str(), "dvr_duration %d", &sec) == 1) dvr_duration_sec_ = sec;
|
2025-11-14 14:13:14 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-14 14:31:04 +08:00
|
|
|
if (dvr_path.empty())
|
2025-11-14 14:13:14 +08:00
|
|
|
{
|
2025-11-14 14:31:04 +08:00
|
|
|
LOG_ERROR("[RecordManager] dvr_path not found in config.");
|
|
|
|
|
return false;
|
2025-11-14 14:13:14 +08:00
|
|
|
}
|
|
|
|
|
|
2025-11-14 14:31:04 +08:00
|
|
|
// /sata/record/[stream]/[2006]-[01]-[02]/...
|
|
|
|
|
auto pos = dvr_path.find("[stream]");
|
|
|
|
|
if (pos != std::string::npos)
|
|
|
|
|
record_dir_ = dvr_path.substr(0, pos);
|
|
|
|
|
else
|
2025-11-14 14:13:14 +08:00
|
|
|
{
|
2025-11-14 14:31:04 +08:00
|
|
|
LOG_ERROR("[RecordManager] Cannot extract record_dir from dvr_path: " + dvr_path);
|
2025-11-14 14:13:14 +08:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2025-11-14 09:42:17 +08:00
|
|
|
|
2025-11-14 14:41:55 +08:00
|
|
|
void RecordManager::startAutoScan(int interval_sec)
|
|
|
|
|
{
|
|
|
|
|
scan_interval_sec_ = interval_sec; // ← 保存扫描周期
|
|
|
|
|
if (running_) return;
|
|
|
|
|
|
|
|
|
|
running_ = true;
|
|
|
|
|
|
|
|
|
|
scan_thread_ = std::thread(
|
|
|
|
|
[this]()
|
|
|
|
|
{
|
|
|
|
|
LOG_INFO("[RecordManager] Auto-scan thread started, interval = " + std::to_string(scan_interval_sec_) +
|
|
|
|
|
" seconds.");
|
|
|
|
|
|
|
|
|
|
while (running_)
|
|
|
|
|
{
|
|
|
|
|
auto t0 = std::chrono::steady_clock::now();
|
|
|
|
|
|
|
|
|
|
this->scanAll();
|
|
|
|
|
LOG_INFO("[RecordManager] scanAll() completed.");
|
|
|
|
|
|
|
|
|
|
// 休眠剩余时间
|
|
|
|
|
auto t1 = std::chrono::steady_clock::now();
|
|
|
|
|
auto used_ms = std::chrono::duration_cast<std::chrono::milliseconds>(t1 - t0).count();
|
|
|
|
|
int sleep_ms = scan_interval_sec_ * 1000 - used_ms;
|
|
|
|
|
if (sleep_ms < 100) sleep_ms = 100;
|
|
|
|
|
|
|
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(sleep_ms));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
LOG_INFO("[RecordManager] Auto-scan thread stopped.");
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void RecordManager::stopAutoScan()
|
|
|
|
|
{
|
|
|
|
|
if (!running_) return;
|
|
|
|
|
|
|
|
|
|
running_ = false;
|
|
|
|
|
if (scan_thread_.joinable()) scan_thread_.join();
|
|
|
|
|
|
|
|
|
|
LOG_INFO("[RecordManager] Auto-scan thread stopped.");
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-14 09:42:17 +08:00
|
|
|
//
|
|
|
|
|
// 扫描所有文件
|
|
|
|
|
//
|
|
|
|
|
void RecordManager::scanAll()
|
|
|
|
|
{
|
|
|
|
|
index_.clear();
|
|
|
|
|
|
2025-11-14 14:31:04 +08:00
|
|
|
if (!fs::exists(record_dir_))
|
|
|
|
|
{
|
|
|
|
|
LOG_WARN("[RecordManager] record_dir not exist: " + record_dir_);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2025-11-14 09:42:17 +08:00
|
|
|
|
2025-11-14 14:21:06 +08:00
|
|
|
for (auto const& entry : fs::recursive_directory_iterator(record_dir_))
|
2025-11-14 09:42:17 +08:00
|
|
|
{
|
|
|
|
|
if (!entry.is_regular_file()) continue;
|
|
|
|
|
|
|
|
|
|
auto p = entry.path();
|
|
|
|
|
if (p.extension() != ".mp4") continue;
|
|
|
|
|
|
|
|
|
|
auto info = parseFile(p);
|
|
|
|
|
if (info.start_ms <= 0) continue;
|
|
|
|
|
|
|
|
|
|
index_[info.stream].push_back(info);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (auto& kv : index_)
|
|
|
|
|
{
|
|
|
|
|
sortStream(kv.first);
|
|
|
|
|
}
|
2025-11-14 14:31:04 +08:00
|
|
|
|
|
|
|
|
LOG_INFO("[RecordManager] scanAll completed. streams=" + std::to_string(index_.size()));
|
2025-11-14 09:42:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
|
// 排序函数:按 start_ms 升序
|
|
|
|
|
//
|
|
|
|
|
void RecordManager::sortStream(const std::string& stream)
|
|
|
|
|
{
|
|
|
|
|
auto& files = index_[stream];
|
|
|
|
|
std::sort(files.begin(), files.end(),
|
|
|
|
|
[](const RecordFileInfo& a, const RecordFileInfo& b) { return a.start_ms < b.start_ms; });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
|
// 解析路径得到文件时间戳
|
|
|
|
|
//
|
|
|
|
|
RecordFileInfo RecordManager::parseFile(const fs::path& p)
|
|
|
|
|
{
|
|
|
|
|
RecordFileInfo info;
|
|
|
|
|
info.path = p.string();
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
auto filename = p.filename().string(); // 10-23-17.mp4
|
2025-11-14 14:31:04 +08:00
|
|
|
auto date_dir = p.parent_path().parent_path().filename().string(); // yyyy-mm-dd
|
2025-11-14 09:42:17 +08:00
|
|
|
auto stream_dir = p.parent_path().parent_path().parent_path().filename().string();
|
|
|
|
|
|
|
|
|
|
info.stream = stream_dir;
|
|
|
|
|
|
2025-11-14 11:09:01 +08:00
|
|
|
int y = 0, mon = 0, d = 0, h = 0, m = 0, s = 0;
|
2025-11-14 09:42:17 +08:00
|
|
|
sscanf(date_dir.c_str(), "%d-%d-%d", &y, &mon, &d);
|
|
|
|
|
sscanf(filename.c_str(), "%d-%d-%d", &h, &m, &s);
|
|
|
|
|
|
2025-11-14 11:09:01 +08:00
|
|
|
std::tm tm{};
|
|
|
|
|
tm.tm_year = y - 1900;
|
|
|
|
|
tm.tm_mon = mon - 1;
|
|
|
|
|
tm.tm_mday = d;
|
|
|
|
|
tm.tm_hour = h;
|
|
|
|
|
tm.tm_min = m;
|
|
|
|
|
tm.tm_sec = s;
|
|
|
|
|
|
|
|
|
|
int64_t start = std::mktime(&tm) * 1000LL;
|
|
|
|
|
info.start_ms = start;
|
|
|
|
|
|
2025-11-14 14:31:04 +08:00
|
|
|
// 读取 mp4 时长
|
2025-11-14 11:09:01 +08:00
|
|
|
MP4FileHandle hFile = MP4Read(info.path.c_str());
|
|
|
|
|
if (hFile != MP4_INVALID_FILE_HANDLE)
|
|
|
|
|
{
|
|
|
|
|
double durSec = MP4GetDuration(hFile) / (double)MP4GetTimeScale(hFile);
|
|
|
|
|
MP4Close(hFile);
|
2025-11-14 14:31:04 +08:00
|
|
|
info.end_ms = start + (int64_t)(durSec * 1000.0);
|
2025-11-14 11:09:01 +08:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2025-11-14 14:31:04 +08:00
|
|
|
info.end_ms = start + 60000; // fallback
|
2025-11-14 11:09:01 +08:00
|
|
|
}
|
2025-11-14 09:42:17 +08:00
|
|
|
}
|
|
|
|
|
catch (...)
|
|
|
|
|
{
|
|
|
|
|
info.start_ms = -1;
|
|
|
|
|
info.end_ms = -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return info;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//
|
2025-11-14 14:31:04 +08:00
|
|
|
// 查询录像段
|
2025-11-14 09:42:17 +08:00
|
|
|
//
|
|
|
|
|
std::vector<RecordSegment> RecordManager::querySegments(const std::string& stream, int64_t start_ms, int64_t end_ms)
|
|
|
|
|
{
|
|
|
|
|
std::vector<RecordSegment> result;
|
|
|
|
|
if (!index_.count(stream)) return result;
|
|
|
|
|
|
|
|
|
|
const auto& files = index_[stream];
|
|
|
|
|
std::vector<RecordFileInfo> hits;
|
|
|
|
|
|
|
|
|
|
for (const auto& f : files)
|
|
|
|
|
{
|
|
|
|
|
bool overlap = !(f.end_ms <= start_ms || f.start_ms >= end_ms);
|
|
|
|
|
if (overlap) hits.push_back(f);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (hits.empty()) return result;
|
|
|
|
|
|
|
|
|
|
std::sort(hits.begin(), hits.end(), [](auto& a, auto& b) { return a.start_ms < b.start_ms; });
|
|
|
|
|
|
|
|
|
|
int idx = 1;
|
|
|
|
|
RecordSegment seg{};
|
|
|
|
|
seg.index = idx;
|
|
|
|
|
seg.start_ms = hits[0].start_ms;
|
|
|
|
|
seg.end_ms = hits[0].end_ms;
|
2025-11-14 14:31:04 +08:00
|
|
|
seg.files.push_back(hits[0]);
|
2025-11-14 09:42:17 +08:00
|
|
|
|
|
|
|
|
for (size_t i = 1; i < hits.size(); ++i)
|
|
|
|
|
{
|
|
|
|
|
auto& prev = hits[i - 1];
|
|
|
|
|
auto& cur = hits[i];
|
|
|
|
|
|
2025-11-14 11:09:01 +08:00
|
|
|
bool continuous = (cur.start_ms - prev.end_ms) <= 2000;
|
2025-11-14 09:42:17 +08:00
|
|
|
|
|
|
|
|
if (continuous)
|
|
|
|
|
{
|
|
|
|
|
seg.files.push_back(cur);
|
|
|
|
|
seg.end_ms = cur.end_ms;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2025-11-14 14:31:04 +08:00
|
|
|
seg.segment_id = stream + "_" + std::to_string(seg.start_ms) + "_" + std::to_string(seg.end_ms);
|
2025-11-14 09:42:17 +08:00
|
|
|
result.push_back(seg);
|
|
|
|
|
|
|
|
|
|
idx++;
|
|
|
|
|
seg = RecordSegment{};
|
|
|
|
|
seg.index = idx;
|
|
|
|
|
seg.start_ms = cur.start_ms;
|
|
|
|
|
seg.end_ms = cur.end_ms;
|
2025-11-14 14:31:04 +08:00
|
|
|
seg.files.push_back(cur);
|
2025-11-14 09:42:17 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-14 14:31:04 +08:00
|
|
|
seg.segment_id = stream + "_" + std::to_string(seg.start_ms) + "_" + std::to_string(seg.end_ms);
|
2025-11-14 09:42:17 +08:00
|
|
|
result.push_back(seg);
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//
|
2025-11-14 14:31:04 +08:00
|
|
|
// 根据 segmentId 获取 segment
|
2025-11-14 09:42:17 +08:00
|
|
|
//
|
|
|
|
|
RecordSegment RecordManager::getSegment(const std::string& segmentId)
|
|
|
|
|
{
|
|
|
|
|
auto pos1 = segmentId.find('_');
|
|
|
|
|
auto pos2 = segmentId.find('_', pos1 + 1);
|
|
|
|
|
if (pos1 == std::string::npos || pos2 == std::string::npos) return {};
|
|
|
|
|
|
|
|
|
|
std::string stream = segmentId.substr(0, pos1);
|
|
|
|
|
int64_t start_ms = std::stoll(segmentId.substr(pos1 + 1, pos2 - pos1 - 1));
|
|
|
|
|
int64_t end_ms = std::stoll(segmentId.substr(pos2 + 1));
|
|
|
|
|
|
|
|
|
|
auto segs = querySegments(stream, start_ms, end_ms);
|
|
|
|
|
for (auto& s : segs)
|
|
|
|
|
{
|
|
|
|
|
if (s.start_ms == start_ms && s.end_ms == end_ms) return s;
|
|
|
|
|
}
|
|
|
|
|
return {};
|
|
|
|
|
}
|
2025-11-14 09:45:39 +08:00
|
|
|
|
2025-11-14 09:55:53 +08:00
|
|
|
int64_t RecordManager::toMsTimestamp(const std::string& s)
|
|
|
|
|
{
|
2025-11-14 14:31:04 +08:00
|
|
|
std::tm tm{};
|
2025-11-14 09:55:53 +08:00
|
|
|
char* ret = strptime(s.c_str(), "%Y-%m-%d %H:%M:%S", &tm);
|
|
|
|
|
if (!ret) return -1;
|
|
|
|
|
|
2025-11-14 14:31:04 +08:00
|
|
|
time_t t = mktime(&tm);
|
|
|
|
|
return (int64_t)t * 1000;
|
2025-11-14 09:55:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::string RecordManager::toReadable(int64_t ms)
|
|
|
|
|
{
|
|
|
|
|
time_t t = ms / 1000;
|
|
|
|
|
std::tm* tm = localtime(&t);
|
|
|
|
|
|
|
|
|
|
char buf[32];
|
|
|
|
|
strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", tm);
|
|
|
|
|
return buf;
|
|
|
|
|
}
|