kunlang_video/include/record_manager.hpp

91 lines
2.4 KiB
C++
Raw Permalink Normal View History

2025-11-14 09:42:17 +08:00
#pragma once
2025-11-14 18:33:35 +08:00
#include <atomic>
2025-11-14 09:42:17 +08:00
#include <chrono>
#include <filesystem>
2025-12-25 17:22:07 +08:00
#include <mutex>
2025-11-14 09:42:17 +08:00
#include <string>
2025-11-14 14:42:45 +08:00
#include <thread>
2025-11-14 09:42:17 +08:00
#include <unordered_map>
#include <vector>
2025-11-14 10:56:44 +08:00
#include "mp4v2/mp4v2.h"
2025-11-14 09:42:17 +08:00
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:
2025-11-14 14:13:14 +08:00
explicit RecordManager(const std::string& srs_record_cfg_path);
2025-11-14 09:42:17 +08:00
2025-11-14 18:22:10 +08:00
~RecordManager();
2025-11-14 09:42:17 +08:00
// 执行一次全扫描
void scanAll();
2025-11-14 18:36:50 +08:00
void stopAutoScan();
2025-11-14 09:42:17 +08:00
// 查询时间段的录像段
std::vector<RecordSegment> querySegments(const std::string& stream, int64_t start_ms, int64_t end_ms);
// 根据 segmentId 获取可播放文件列表
RecordSegment getSegment(const std::string& segmentId);
2025-11-14 09:55:53 +08:00
static int64_t toMsTimestamp(const std::string& s);
static std::string toReadable(int64_t ms);
2025-11-14 16:00:45 +08:00
static std::string loc_to_stream(int loc);
2025-11-14 09:55:53 +08:00
2025-11-14 17:18:49 +08:00
int getHttpPort() const { return http_port_; }
2025-11-14 09:42:17 +08:00
private:
2025-11-14 14:13:14 +08:00
// === SRS 配置相关 ===
std::string srs_record_cfg_path_;
std::string record_dir_; // /sata/record/
int dvr_duration_sec_ = 60; // 从配置文件读取,默认 60
2025-11-14 17:05:24 +08:00
int http_port_ = 80;
2025-11-14 14:13:14 +08:00
bool loadSrsConfig(); // 解析配置文件,填充 record_dir_ 和 dvr_duration_sec_
2025-11-26 09:13:11 +08:00
void removeExpiredDays();
void removeOldestHoursUntilSafe();
void cleanupStorage();
2025-11-14 14:13:14 +08:00
// === 文件列表索引 ===
2025-11-14 09:42:17 +08:00
std::unordered_map<std::string, std::vector<RecordFileInfo>> index_;
2025-11-14 18:17:25 +08:00
std::mutex index_mutex_;
2025-11-14 09:42:17 +08:00
// 解析文件名得到 start_ms 和 end_ms
RecordFileInfo parseFile(const std::filesystem::path& p);
// 给一个 stream 的文件列表排序
void sortStream(const std::string& stream);
2025-11-14 14:41:55 +08:00
// === 自动扫描线程 ===
void startAutoScan(int interval_sec);
std::thread scan_thread_;
2025-11-14 18:33:35 +08:00
std::atomic<bool> running_{false};
2025-11-14 14:41:55 +08:00
int scan_interval_sec_ = 60;
2025-11-26 09:13:11 +08:00
// ==== Storage management config ====
int retention_days_ = 14; // 保留最近 N 天
double usage_threshold_ = 0.90; // 超过 90% 就进行空间清理
2025-11-14 15:40:14 +08:00
};
extern std::shared_ptr<RecordManager> g_record_manager;