修复hls快速重连时,延迟删除文件夹相关bug (#3039)

This commit is contained in:
xiongziliang 2023-12-02 19:51:44 +08:00
parent c12a6f0aed
commit 3137b95406
3 changed files with 34 additions and 15 deletions

@ -1 +1 @@
Subproject commit e7cd1f3d1ec81014f92ab844915db60bb7e34bd5 Subproject commit 7a403274c385f59a7eefd5474a05a05139220ee6

View File

@ -45,6 +45,13 @@ void HlsMakerImp::clearCache() {
clearCache(true, false); clearCache(true, false);
} }
static void clearHls(const std::list<std::string> &files) {
for (auto &file : files) {
File::delete_file(file);
}
File::deleteEmptyDir(File::parentDir(files.back()));
}
void HlsMakerImp::clearCache(bool immediately, bool eof) { void HlsMakerImp::clearCache(bool immediately, bool eof) {
// 录制完了 // 录制完了
flushLastSegment(eof); flushLastSegment(eof);
@ -52,21 +59,31 @@ void HlsMakerImp::clearCache(bool immediately, bool eof) {
return; return;
} }
{
std::list<std::string> lst;
lst.emplace_back(_path_hls);
if (!_path_init.empty()) {
lst.emplace_back(_path_init);
}
for (auto &pr : _segment_file_paths) {
lst.emplace_back(std::move(pr.second));
}
// hls直播才删除文件
GET_CONFIG(uint32_t, delay, Hls::kDeleteDelaySec);
if (!delay || immediately) {
clearHls(lst);
} else {
_poller->doDelayTask(delay * 1000, [lst]() {
clearHls(lst);
return 0;
});
}
}
clear(); clear();
_file = nullptr; _file = nullptr;
_segment_file_paths.clear(); _segment_file_paths.clear();
// hls直播才删除文件
GET_CONFIG(uint32_t, delay, Hls::kDeleteDelaySec);
if (!delay || immediately) {
File::delete_file(_path_prefix.data());
} else {
auto path_prefix = _path_prefix;
_poller->doDelayTask(delay * 1000, [path_prefix]() {
File::delete_file(path_prefix.data());
return 0;
});
}
} }
string HlsMakerImp::onOpenSegment(uint64_t index) { string HlsMakerImp::onOpenSegment(uint64_t index) {
@ -103,16 +120,17 @@ void HlsMakerImp::onDelSegment(uint64_t index) {
if (it == _segment_file_paths.end()) { if (it == _segment_file_paths.end()) {
return; return;
} }
File::delete_file(it->second.data()); File::delete_file(it->second.data(), true);
_segment_file_paths.erase(it); _segment_file_paths.erase(it);
} }
void HlsMakerImp::onWriteInitSegment(const char *data, size_t len) { void HlsMakerImp::onWriteInitSegment(const char *data, size_t len) {
string init_seg_path = _path_prefix + "/init.mp4"; string init_seg_path = _path_prefix + "/init.mp4";
_file = makeFile(init_seg_path, true); _file = makeFile(init_seg_path);
if (_file) { if (_file) {
fwrite(data, len, 1, _file.get()); fwrite(data, len, 1, _file.get());
_path_init = std::move(init_seg_path);
_file = nullptr; _file = nullptr;
} else { } else {
WarnL << "Create file failed," << init_seg_path << " " << get_uv_errmsg(); WarnL << "Create file failed," << init_seg_path << " " << get_uv_errmsg();

View File

@ -60,6 +60,7 @@ private:
int _buf_size; int _buf_size;
std::string _params; std::string _params;
std::string _path_hls; std::string _path_hls;
std::string _path_init;
std::string _path_prefix; std::string _path_prefix;
RecordInfo _info; RecordInfo _info;
std::shared_ptr<FILE> _file; std::shared_ptr<FILE> _file;