From e8c9666af0eaa39591b6571b1ed018bdbb37be47 Mon Sep 17 00:00:00 2001 From: ziyue <1213642868@qq.com> Date: Wed, 23 Jun 2021 10:25:09 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E6=8C=81hls=E7=9B=B4=E6=92=AD?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E5=BB=B6=E6=97=B6=E5=88=A0=E9=99=A4:=20#913?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- conf/config.ini | 4 +++- src/Common/config.cpp | 3 +++ src/Common/config.h | 2 ++ src/Record/HlsMakerImp.cpp | 27 ++++++++++++++++++++------- src/Record/HlsMakerImp.h | 4 +++- 5 files changed, 31 insertions(+), 9 deletions(-) diff --git a/conf/config.ini b/conf/config.ini index 0dce3fa6..10aeab53 100644 --- a/conf/config.ini +++ b/conf/config.ini @@ -92,8 +92,10 @@ segDur=2 segNum=3 #HLS切片从m3u8文件中移除后,继续保留在磁盘上的个数 segRetain=5 -# 是否广播 ts 切片完成通知 +#是否广播 ts 切片完成通知 broadcastRecordTs=0 +#直播hls文件删除延时,单位秒,issue: #913 +deleteDelaySec=0 [hook] #在推流时,如果url参数匹对admin_params,那么可以不经过hook鉴权直接推流成功,播放时亦然 diff --git a/src/Common/config.cpp b/src/Common/config.cpp index 46cdd412..8b0304bb 100644 --- a/src/Common/config.cpp +++ b/src/Common/config.cpp @@ -260,6 +260,8 @@ const string kFileBufSize = HLS_FIELD"fileBufSize"; const string kFilePath = HLS_FIELD"filePath"; // 是否广播 ts 切片完成通知 const string kBroadcastRecordTs = HLS_FIELD"broadcastRecordTs"; +//hls直播文件删除延时,单位秒 +const string kDeleteDelaySec = HLS_FIELD"deleteDelaySec"; onceToken token([](){ mINI::Instance()[kSegmentDuration] = 2; @@ -268,6 +270,7 @@ onceToken token([](){ mINI::Instance()[kFileBufSize] = 64 * 1024; mINI::Instance()[kFilePath] = "./www"; mINI::Instance()[kBroadcastRecordTs] = false; + mINI::Instance()[kDeleteDelaySec] = 0; },nullptr); } //namespace Hls diff --git a/src/Common/config.h b/src/Common/config.h index 9f21fc4a..b110d62b 100644 --- a/src/Common/config.h +++ b/src/Common/config.h @@ -293,6 +293,8 @@ extern const string kFileBufSize; extern const string kFilePath; // 是否广播 ts 切片完成通知 extern const string kBroadcastRecordTs; +//hls直播文件删除延时,单位秒 +extern const string kDeleteDelaySec; } //namespace Hls ////////////Rtp代理相关配置/////////// diff --git a/src/Record/HlsMakerImp.cpp b/src/Record/HlsMakerImp.cpp index ebf34094..569dbac0 100644 --- a/src/Record/HlsMakerImp.cpp +++ b/src/Record/HlsMakerImp.cpp @@ -23,6 +23,7 @@ HlsMakerImp::HlsMakerImp(const string &m3u8_file, uint32_t bufSize, float seg_duration, uint32_t seg_number) : HlsMaker(seg_duration, seg_number) { + _poller = EventPollerPool::Instance().getPoller(); _path_prefix = m3u8_file.substr(0, m3u8_file.rfind('/')); _path_hls = m3u8_file; _params = params; @@ -35,18 +36,30 @@ HlsMakerImp::HlsMakerImp(const string &m3u8_file, } HlsMakerImp::~HlsMakerImp() { - clearCache(); + clearCache(false); } -void HlsMakerImp::clearCache() { +void HlsMakerImp::clearCache(bool immediately) { //录制完了 flushLastSegment(true); - if (isLive()) { - //hls直播才删除文件 - clear(); - _file = nullptr; - _segment_file_paths.clear(); + if (!isLive()) { + return; + } + + 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; + }); } } diff --git a/src/Record/HlsMakerImp.h b/src/Record/HlsMakerImp.h index 9b9b9484..93d73356 100644 --- a/src/Record/HlsMakerImp.h +++ b/src/Record/HlsMakerImp.h @@ -47,8 +47,9 @@ public: /** * 清空缓存 + * @param immediately 时候立即删除 */ - void clearCache(); + void clearCache(bool immediately = true); protected: string onOpenSegment(uint64_t index) override ; @@ -69,6 +70,7 @@ private: std::shared_ptr _file; std::shared_ptr _file_buf; HlsMediaSource::Ptr _media_src; + EventPoller::Ptr _poller; map _segment_file_paths; };