1
This commit is contained in:
parent
7a972c1f61
commit
773bb07d85
@ -28,7 +28,7 @@ struct RecordSegment
|
||||
class RecordManager
|
||||
{
|
||||
public:
|
||||
explicit RecordManager(const std::string& base_dir);
|
||||
explicit RecordManager(const std::string& srs_record_cfg_path);
|
||||
|
||||
// 执行一次全扫描
|
||||
void scanAll();
|
||||
@ -42,11 +42,15 @@ class RecordManager
|
||||
static int64_t toMsTimestamp(const std::string& s);
|
||||
static std::string toReadable(int64_t ms);
|
||||
|
||||
// 调试打印全部索引
|
||||
void dumpIndex() const;
|
||||
|
||||
private:
|
||||
std::string base_dir_; // /sata/record/
|
||||
// === SRS 配置相关 ===
|
||||
std::string srs_record_cfg_path_;
|
||||
std::string record_dir_; // /sata/record/
|
||||
int dvr_duration_sec_ = 60; // 从配置文件读取,默认 60
|
||||
|
||||
bool loadSrsConfig(); // 解析配置文件,填充 record_dir_ 和 dvr_duration_sec_
|
||||
|
||||
// === 文件列表索引 ===
|
||||
std::unordered_map<std::string, std::vector<RecordFileInfo>> index_;
|
||||
|
||||
// 解析文件名得到 start_ms 和 end_ms
|
||||
|
||||
38
src/main.cpp
38
src/main.cpp
@ -36,43 +36,7 @@ int main()
|
||||
Logger::init(get_executable_dir_file_path("logs"), 7);
|
||||
LOG_INFO("[MAIN] ===== Vehicle Video Service Starting =====");
|
||||
|
||||
std::string base = "/sata/record/";
|
||||
RecordManager rm(base);
|
||||
|
||||
std::cout << "[RecordManager] scanning " << base << " ..." << std::endl;
|
||||
|
||||
rm.scanAll();
|
||||
|
||||
std::cout << "\n=== 全部扫描结果 ===\n";
|
||||
|
||||
// 遍历所有 stream
|
||||
rm.dumpIndex();
|
||||
|
||||
// 测试一次查询:你可以换成真实时间戳
|
||||
std::cout << "\n=== 测试 querySegments() ===\n";
|
||||
|
||||
// 随便选一个时间区间,比如 2025-11-13 10:23:00 ~ 11:00:00
|
||||
// 你可以换成真实值
|
||||
int64_t start = RecordManager::toMsTimestamp("2025-11-13 10:00:00");
|
||||
int64_t end = RecordManager::toMsTimestamp("2025-11-13 14:30:00");
|
||||
|
||||
auto segments = rm.querySegments("AHD1_main", start, end);
|
||||
|
||||
std::cout << "找到录像段数量 = " << segments.size() << "\n";
|
||||
|
||||
for (auto& seg : segments)
|
||||
{
|
||||
std::cout << "\n--- Segment " << seg.index << " ---\n";
|
||||
std::cout << "segmentId = " << seg.segment_id << "\n";
|
||||
std::cout << "start_ms = " << seg.start_ms << "\n";
|
||||
std::cout << "end_ms = " << seg.end_ms << "\n";
|
||||
std::cout << "files = " << seg.files.size() << "\n";
|
||||
|
||||
for (auto& f : seg.files)
|
||||
{
|
||||
std::cout << " file: " << f.path << "\n [" << f.start_ms << " ~ " << f.end_ms << "]\n";
|
||||
}
|
||||
}
|
||||
RecordManager rm("/home/aiec/srs/conf/kun_record.conf");
|
||||
|
||||
// try
|
||||
// {
|
||||
|
||||
@ -6,7 +6,84 @@
|
||||
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
RecordManager::RecordManager(const std::string& base_dir) : base_dir_(base_dir) {}
|
||||
//
|
||||
// 构造函数:自动加载 SRS 配置
|
||||
//
|
||||
RecordManager::RecordManager(const std::string& srs_record_cfg_path) : srs_record_cfg_path_(srs_record_cfg_path)
|
||||
{
|
||||
std::cout << "[RecordManager] Loading SRS config: " << srs_record_cfg_path_ << "\n";
|
||||
|
||||
bool ok = loadSrsConfig();
|
||||
if (!ok)
|
||||
{
|
||||
std::cout << "[RecordManager] ERROR loading SRS config\n";
|
||||
}
|
||||
else
|
||||
{
|
||||
std::cout << "[RecordManager] SRS config loaded successfully.\n";
|
||||
std::cout << " record_dir_ = " << record_dir_ << "\n";
|
||||
std::cout << " dvr_duration_sec = " << dvr_duration_sec_ << "\n";
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
// 解析 SRS DVR 配置,提取 record_dir_ 与 dvr_duration_sec_
|
||||
//
|
||||
bool RecordManager::loadSrsConfig()
|
||||
{
|
||||
std::ifstream ifs(srs_record_cfg_path_);
|
||||
if (!ifs.is_open())
|
||||
{
|
||||
std::cerr << "[RecordManager] Failed to open SRS config: " << srs_record_cfg_path_ << "\n";
|
||||
return false;
|
||||
}
|
||||
|
||||
std::string line;
|
||||
std::string dvr_path;
|
||||
while (std::getline(ifs, line))
|
||||
{
|
||||
// 去掉前后空白
|
||||
line.erase(0, line.find_first_not_of(" \t"));
|
||||
if (line.empty() || line[0] == '#') continue;
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
else if (line.find("dvr_duration") != std::string::npos)
|
||||
{
|
||||
int sec = 0;
|
||||
if (sscanf(line.c_str(), "dvr_duration %d", &sec) == 1)
|
||||
{
|
||||
dvr_duration_sec_ = sec;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!dvr_path.empty())
|
||||
{
|
||||
// 例子:/sata/record/[stream]/[2006]-[01]-[02]/...
|
||||
// → record_dir_ = /sata/record/
|
||||
auto pos = dvr_path.find("[stream]");
|
||||
if (pos != std::string::npos)
|
||||
{
|
||||
record_dir_ = dvr_path.substr(0, pos);
|
||||
}
|
||||
}
|
||||
|
||||
if (record_dir_.empty())
|
||||
{
|
||||
std::cerr << "[RecordManager] ERROR: cannot parse dvr_path.\n";
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
//
|
||||
// 扫描所有文件
|
||||
@ -223,21 +300,3 @@ std::string RecordManager::toReadable(int64_t ms)
|
||||
strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", tm);
|
||||
return buf;
|
||||
}
|
||||
|
||||
void RecordManager::dumpIndex() const
|
||||
{
|
||||
std::cout << "\n=== dumpIndex() ===\n";
|
||||
|
||||
for (const auto& kv : index_)
|
||||
{
|
||||
const auto& stream = kv.first;
|
||||
const auto& files = kv.second;
|
||||
|
||||
std::cout << "\n>>> Stream = " << stream << ", file_count = " << files.size() << "\n";
|
||||
|
||||
for (const auto& f : files)
|
||||
{
|
||||
std::cout << " - " << f.path << "\n start_ms=" << f.start_ms << ", end_ms=" << f.end_ms << "\n";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user