diff --git a/src/Record/HlsMakerImpSub.cpp b/src/Record/HlsMakerImpSub.cpp index 71ebc1e6..b5541e78 100644 --- a/src/Record/HlsMakerImpSub.cpp +++ b/src/Record/HlsMakerImpSub.cpp @@ -26,7 +26,6 @@ HlsMakerImpSub::HlsMakerImpSub( float seg_duration, uint32_t seg_number, bool seg_keep):HlsMakerSub(seg_duration, seg_number, seg_keep) { - _poller = EventPollerPool::Instance().getPoller(); _path_prefix = m3u8_file.substr(0, m3u8_file.rfind('/')); _path_hls = m3u8_file; _params = params; @@ -55,22 +54,24 @@ void HlsMakerImpSub::clearCache(bool immediately, bool eof) { clear(); _file = nullptr; + //删除_segment_file_paths路径对应的直播文件 + for (auto it : _segment_file_paths) { + auto ts_path = it.second; + File::delete_file(ts_path.data()); + } _segment_file_paths.clear(); - //删除缓存的m3u8文件 - File::delete_file((_path_prefix + "/hls.m3u8").data() ); + //程序异常退出的情况下,直播的8个ts文件还是无法删除, + //这里先删除掉m3u8文件对应的3个ts文件。还有保留的5个ts文件无法删除,能删除几个是几个吧。 + fstream file(_path_prefix + "/hls.m3u8"); + string data; + while (getline(file,data)) { + string ts_path = _path_prefix + "/" + data; + File::delete_file(ts_path.data()); + } - ////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; - // }); - //} + //删除缓存的m3u8文件 + File::delete_file((_path_prefix + "/hls.m3u8").data()); } string HlsMakerImpSub::onOpenSegment(uint64_t index) { @@ -81,13 +82,7 @@ string HlsMakerImpSub::onOpenSegment(uint64_t index) { auto strTime = getTimeStr("%M-%S"); segment_name = StrPrinter << strDate + "_" + strHour + "-" + strTime << ".ts"; segment_path = _path_prefix + "/" + strDate + "/" + strHour + "/" + segment_name; - if (isLive()) { - - GET_CONFIG(uint32_t, segRetain, Hls::kSegmentRetain); - GET_CONFIG(uint32_t, segNum, Hls::kSegmentNum); - if (_segment_file_paths.size() > segRetain + segNum) { - _segment_file_paths.erase(index - segRetain - segNum - 1); - } + if ((!isKeep())) { _segment_file_paths.emplace(index, segment_path); } diff --git a/src/Record/HlsMakerImpSub.h b/src/Record/HlsMakerImpSub.h index 6a9d7fdd..043937b7 100644 --- a/src/Record/HlsMakerImpSub.h +++ b/src/Record/HlsMakerImpSub.h @@ -71,7 +71,6 @@ private: std::shared_ptr _file; std::shared_ptr _file_buf; HlsMediaSource::Ptr _media_src; - toolkit::EventPoller::Ptr _poller; }; diff --git a/src/Record/HlsMakerSub.cpp b/src/Record/HlsMakerSub.cpp index fb20592f..55de73cf 100644 --- a/src/Record/HlsMakerSub.cpp +++ b/src/Record/HlsMakerSub.cpp @@ -30,6 +30,7 @@ HlsMakerSub::HlsMakerSub(float seg_duration, uint32_t seg_number, bool seg_keep) _seg_duration = seg_duration; _seg_keep = seg_keep; _is_record = false; + _poller = EventPollerPool::Instance().getPoller(); } HlsMakerSub::~HlsMakerSub() { @@ -38,8 +39,22 @@ HlsMakerSub::~HlsMakerSub() { } void HlsMakerSub::startRecord(bool isRecord) { - if (_is_record) { //检测到上一次是在录像,清空_segment_file_paths + //本来已经在录像,再次点击录像,或者本来已经停止录像,再次点击停止录像,直接返回 + if (isRecord == _is_record) { + return; + } + //如果是录像,则删除之前直播的8个ts文件 + if (isRecord) { + std::map delete_file_paths = _segment_file_paths; _segment_file_paths.clear(); + //删除_segment_file_paths路径对应的直播文件,过30s再删除,免得hls直播突然断掉 + for (auto it : delete_file_paths) { + auto ts_path = it.second; + _poller->doDelayTask(30 * 1000, [ts_path]() { + File::delete_file(ts_path.data()); + return 0; + }); + } } if(isRecord) { diff --git a/src/Record/HlsMakerSub.h b/src/Record/HlsMakerSub.h index f8a244a5..3904c358 100644 --- a/src/Record/HlsMakerSub.h +++ b/src/Record/HlsMakerSub.h @@ -18,7 +18,7 @@ #include "Util/File.h" #include "Util/util.h" #include "Util/logger.h" - +#include "ZLToolKit/src/Poller/EventPoller.h" namespace mediakit { class HlsMakerSub { @@ -131,6 +131,7 @@ private: bool _is_record = false; bool _is_close_stream = false; std::string _m3u8_file_path; + toolkit::EventPoller::Ptr _poller; public: std::map _segment_file_paths;