From 3137b954068db4a850a32b6a089b93b1506c7757 Mon Sep 17 00:00:00 2001 From: xiongziliang <771730766@qq.com> Date: Sat, 2 Dec 2023 19:51:44 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dhls=E5=BF=AB=E9=80=9F?= =?UTF-8?q?=E9=87=8D=E8=BF=9E=E6=97=B6=EF=BC=8C=E5=BB=B6=E8=BF=9F=E5=88=A0?= =?UTF-8?q?=E9=99=A4=E6=96=87=E4=BB=B6=E5=A4=B9=E7=9B=B8=E5=85=B3bug=20(#3?= =?UTF-8?q?039)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 3rdpart/ZLToolKit | 2 +- src/Record/HlsMakerImp.cpp | 46 ++++++++++++++++++++++++++------------ src/Record/HlsMakerImp.h | 1 + 3 files changed, 34 insertions(+), 15 deletions(-) diff --git a/3rdpart/ZLToolKit b/3rdpart/ZLToolKit index e7cd1f3d..7a403274 160000 --- a/3rdpart/ZLToolKit +++ b/3rdpart/ZLToolKit @@ -1 +1 @@ -Subproject commit e7cd1f3d1ec81014f92ab844915db60bb7e34bd5 +Subproject commit 7a403274c385f59a7eefd5474a05a05139220ee6 diff --git a/src/Record/HlsMakerImp.cpp b/src/Record/HlsMakerImp.cpp index 2ba99f76..a3980651 100644 --- a/src/Record/HlsMakerImp.cpp +++ b/src/Record/HlsMakerImp.cpp @@ -45,6 +45,13 @@ void HlsMakerImp::clearCache() { clearCache(true, false); } +static void clearHls(const std::list &files) { + for (auto &file : files) { + File::delete_file(file); + } + File::deleteEmptyDir(File::parentDir(files.back())); +} + void HlsMakerImp::clearCache(bool immediately, bool eof) { // 录制完了 flushLastSegment(eof); @@ -52,21 +59,31 @@ void HlsMakerImp::clearCache(bool immediately, bool eof) { return; } + { + std::list 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(); _file = nullptr; _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) { @@ -103,16 +120,17 @@ void HlsMakerImp::onDelSegment(uint64_t index) { if (it == _segment_file_paths.end()) { return; } - File::delete_file(it->second.data()); + File::delete_file(it->second.data(), true); _segment_file_paths.erase(it); } void HlsMakerImp::onWriteInitSegment(const char *data, size_t len) { string init_seg_path = _path_prefix + "/init.mp4"; - _file = makeFile(init_seg_path, true); + _file = makeFile(init_seg_path); if (_file) { fwrite(data, len, 1, _file.get()); + _path_init = std::move(init_seg_path); _file = nullptr; } else { WarnL << "Create file failed," << init_seg_path << " " << get_uv_errmsg(); diff --git a/src/Record/HlsMakerImp.h b/src/Record/HlsMakerImp.h index 07bef1d6..3633bd7e 100644 --- a/src/Record/HlsMakerImp.h +++ b/src/Record/HlsMakerImp.h @@ -60,6 +60,7 @@ private: int _buf_size; std::string _params; std::string _path_hls; + std::string _path_init; std::string _path_prefix; RecordInfo _info; std::shared_ptr _file;