sweeper_video/include/record_manager.hpp
2025-11-14 18:36:50 +08:00

82 lines
2.1 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#pragma once
#include <atomic>
#include <chrono>
#include <filesystem>
#include <string>
#include <thread>
#include <unordered_map>
#include <vector>
#include "mp4v2/mp4v2.h"
struct RecordFileInfo
{
std::string stream; // AHD1_main
std::string path; // /sata/record/AHD1_main/2025-11-13/10/10-23-17.mp4
int64_t start_ms; // 文件起始时间ms
int64_t end_ms; // 文件结束时间ms
};
struct RecordSegment
{
int index; // 段号
std::string segment_id;
int64_t start_ms;
int64_t end_ms;
std::vector<RecordFileInfo> files;
};
class RecordManager
{
public:
explicit RecordManager(const std::string& srs_record_cfg_path);
~RecordManager();
// 执行一次全扫描
void scanAll();
void stopAutoScan();
// 查询时间段的录像段
std::vector<RecordSegment> querySegments(const std::string& stream, int64_t start_ms, int64_t end_ms);
// 根据 segmentId 获取可播放文件列表
RecordSegment getSegment(const std::string& segmentId);
static int64_t toMsTimestamp(const std::string& s);
static std::string toReadable(int64_t ms);
static std::string loc_to_stream(int loc);
int getHttpPort() const { return http_port_; }
private:
// === SRS 配置相关 ===
std::string srs_record_cfg_path_;
std::string record_dir_; // /sata/record/
int dvr_duration_sec_ = 60; // 从配置文件读取,默认 60
int http_port_ = 80;
bool loadSrsConfig(); // 解析配置文件,填充 record_dir_ 和 dvr_duration_sec_
// === 文件列表索引 ===
std::unordered_map<std::string, std::vector<RecordFileInfo>> index_;
std::mutex index_mutex_;
// 解析文件名得到 start_ms 和 end_ms
RecordFileInfo parseFile(const std::filesystem::path& p);
// 给一个 stream 的文件列表排序
void sortStream(const std::string& stream);
// === 自动扫描线程 ===
void startAutoScan(int interval_sec);
std::thread scan_thread_;
std::atomic<bool> running_{false};
int scan_interval_sec_ = 60;
};
extern std::shared_ptr<RecordManager> g_record_manager;