支持hls直播文件延时删除: #913

This commit is contained in:
ziyue 2021-06-23 10:25:09 +08:00
parent ce2122d3b6
commit e8c9666af0
5 changed files with 31 additions and 9 deletions

View File

@ -92,8 +92,10 @@ segDur=2
segNum=3 segNum=3
#HLS切片从m3u8文件中移除后继续保留在磁盘上的个数 #HLS切片从m3u8文件中移除后继续保留在磁盘上的个数
segRetain=5 segRetain=5
# 是否广播 ts 切片完成通知 #是否广播 ts 切片完成通知
broadcastRecordTs=0 broadcastRecordTs=0
#直播hls文件删除延时单位秒issue: #913
deleteDelaySec=0
[hook] [hook]
#在推流时如果url参数匹对admin_params那么可以不经过hook鉴权直接推流成功播放时亦然 #在推流时如果url参数匹对admin_params那么可以不经过hook鉴权直接推流成功播放时亦然

View File

@ -260,6 +260,8 @@ const string kFileBufSize = HLS_FIELD"fileBufSize";
const string kFilePath = HLS_FIELD"filePath"; const string kFilePath = HLS_FIELD"filePath";
// 是否广播 ts 切片完成通知 // 是否广播 ts 切片完成通知
const string kBroadcastRecordTs = HLS_FIELD"broadcastRecordTs"; const string kBroadcastRecordTs = HLS_FIELD"broadcastRecordTs";
//hls直播文件删除延时单位秒
const string kDeleteDelaySec = HLS_FIELD"deleteDelaySec";
onceToken token([](){ onceToken token([](){
mINI::Instance()[kSegmentDuration] = 2; mINI::Instance()[kSegmentDuration] = 2;
@ -268,6 +270,7 @@ onceToken token([](){
mINI::Instance()[kFileBufSize] = 64 * 1024; mINI::Instance()[kFileBufSize] = 64 * 1024;
mINI::Instance()[kFilePath] = "./www"; mINI::Instance()[kFilePath] = "./www";
mINI::Instance()[kBroadcastRecordTs] = false; mINI::Instance()[kBroadcastRecordTs] = false;
mINI::Instance()[kDeleteDelaySec] = 0;
},nullptr); },nullptr);
} //namespace Hls } //namespace Hls

View File

@ -293,6 +293,8 @@ extern const string kFileBufSize;
extern const string kFilePath; extern const string kFilePath;
// 是否广播 ts 切片完成通知 // 是否广播 ts 切片完成通知
extern const string kBroadcastRecordTs; extern const string kBroadcastRecordTs;
//hls直播文件删除延时单位秒
extern const string kDeleteDelaySec;
} //namespace Hls } //namespace Hls
////////////Rtp代理相关配置/////////// ////////////Rtp代理相关配置///////////

View File

@ -23,6 +23,7 @@ HlsMakerImp::HlsMakerImp(const string &m3u8_file,
uint32_t bufSize, uint32_t bufSize,
float seg_duration, float seg_duration,
uint32_t seg_number) : HlsMaker(seg_duration, seg_number) { uint32_t seg_number) : HlsMaker(seg_duration, seg_number) {
_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;
@ -35,18 +36,30 @@ HlsMakerImp::HlsMakerImp(const string &m3u8_file,
} }
HlsMakerImp::~HlsMakerImp() { HlsMakerImp::~HlsMakerImp() {
clearCache(); clearCache(false);
} }
void HlsMakerImp::clearCache() { void HlsMakerImp::clearCache(bool immediately) {
//录制完了 //录制完了
flushLastSegment(true); flushLastSegment(true);
if (isLive()) { if (!isLive()) {
//hls直播才删除文件 return;
clear(); }
_file = nullptr;
_segment_file_paths.clear(); clear();
_file = nullptr;
_segment_file_paths.clear();
//hls直播才删除文件
GET_CONFIG(uint32_t, delay, Hls::kDeleteDelaySec);
if (!delay || immediately) {
File::delete_file(_path_prefix.data()); 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;
});
} }
} }

View File

@ -47,8 +47,9 @@ public:
/** /**
* *
* @param immediately
*/ */
void clearCache(); void clearCache(bool immediately = true);
protected: protected:
string onOpenSegment(uint64_t index) override ; string onOpenSegment(uint64_t index) override ;
@ -69,6 +70,7 @@ 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;
EventPoller::Ptr _poller;
map<uint64_t/*index*/,string/*file_path*/> _segment_file_paths; map<uint64_t/*index*/,string/*file_path*/> _segment_file_paths;
}; };