删除直播生成的8个ts文件。以免多次播放导致ts文件累积过多占用内存。

This commit is contained in:
renlu 2022-11-21 17:10:36 +08:00
parent 31293d9fe3
commit 5364d8a001
4 changed files with 34 additions and 24 deletions

View File

@ -26,7 +26,6 @@ HlsMakerImpSub::HlsMakerImpSub(
float seg_duration, float seg_duration,
uint32_t seg_number, uint32_t seg_number,
bool seg_keep):HlsMakerSub(seg_duration, seg_number, seg_keep) { bool seg_keep):HlsMakerSub(seg_duration, seg_number, seg_keep) {
_poller = EventPollerPool::Instance().getPoller();
_path_prefix = m3u8_file.substr(0, m3u8_file.rfind('/')); _path_prefix = m3u8_file.substr(0, m3u8_file.rfind('/'));
_path_hls = m3u8_file; _path_hls = m3u8_file;
_params = params; _params = params;
@ -55,22 +54,24 @@ void HlsMakerImpSub::clearCache(bool immediately, bool eof) {
clear(); clear();
_file = nullptr; _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(); _segment_file_paths.clear();
//程序异常退出的情况下直播的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());
}
//删除缓存的m3u8文件 //删除缓存的m3u8文件
File::delete_file((_path_prefix + "/hls.m3u8").data()); File::delete_file((_path_prefix + "/hls.m3u8").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;
// });
//}
} }
string HlsMakerImpSub::onOpenSegment(uint64_t index) { string HlsMakerImpSub::onOpenSegment(uint64_t index) {
@ -81,13 +82,7 @@ string HlsMakerImpSub::onOpenSegment(uint64_t index) {
auto strTime = getTimeStr("%M-%S"); auto strTime = getTimeStr("%M-%S");
segment_name = StrPrinter << strDate + "_" + strHour + "-" + strTime << ".ts"; segment_name = StrPrinter << strDate + "_" + strHour + "-" + strTime << ".ts";
segment_path = _path_prefix + "/" + strDate + "/" + strHour + "/" + segment_name; segment_path = _path_prefix + "/" + strDate + "/" + strHour + "/" + segment_name;
if (isLive()) { if ((!isKeep())) {
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);
}
_segment_file_paths.emplace(index, segment_path); _segment_file_paths.emplace(index, segment_path);
} }

View File

@ -71,7 +71,6 @@ private:
std::shared_ptr<FILE> _file; std::shared_ptr<FILE> _file;
std::shared_ptr<char> _file_buf; std::shared_ptr<char> _file_buf;
HlsMediaSource::Ptr _media_src; HlsMediaSource::Ptr _media_src;
toolkit::EventPoller::Ptr _poller;
}; };

View File

@ -30,6 +30,7 @@ HlsMakerSub::HlsMakerSub(float seg_duration, uint32_t seg_number, bool seg_keep)
_seg_duration = seg_duration; _seg_duration = seg_duration;
_seg_keep = seg_keep; _seg_keep = seg_keep;
_is_record = false; _is_record = false;
_poller = EventPollerPool::Instance().getPoller();
} }
HlsMakerSub::~HlsMakerSub() { HlsMakerSub::~HlsMakerSub() {
@ -38,8 +39,22 @@ HlsMakerSub::~HlsMakerSub() {
} }
void HlsMakerSub::startRecord(bool isRecord) { void HlsMakerSub::startRecord(bool isRecord) {
if (_is_record) { //检测到上一次是在录像清空_segment_file_paths //本来已经在录像,再次点击录像,或者本来已经停止录像,再次点击停止录像,直接返回
if (isRecord == _is_record) {
return;
}
//如果是录像则删除之前直播的8个ts文件
if (isRecord) {
std::map<uint64_t, std::string> delete_file_paths = _segment_file_paths;
_segment_file_paths.clear(); _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) { if(isRecord) {

View File

@ -18,7 +18,7 @@
#include "Util/File.h" #include "Util/File.h"
#include "Util/util.h" #include "Util/util.h"
#include "Util/logger.h" #include "Util/logger.h"
#include "ZLToolKit/src/Poller/EventPoller.h"
namespace mediakit { namespace mediakit {
class HlsMakerSub { class HlsMakerSub {
@ -131,6 +131,7 @@ private:
bool _is_record = false; bool _is_record = false;
bool _is_close_stream = false; bool _is_close_stream = false;
std::string _m3u8_file_path; std::string _m3u8_file_path;
toolkit::EventPoller::Ptr _poller;
public: public:
std::map<uint64_t /*index*/, std::string /*file_path*/> _segment_file_paths; std::map<uint64_t /*index*/, std::string /*file_path*/> _segment_file_paths;