!16 增加长期保存hls切片的功能

增加长期保存hls切片的功能,segKeep=0为不长期保存,segKeep=1为长期保存。此功能部分等效于segNum=0的情况,不同的是这个保留不会在m3u8文件中体现。
This commit is contained in:
夏楚 2022-05-11 03:39:59 +00:00 committed by Gitee
commit 845b2ea8d8
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
8 changed files with 34 additions and 7 deletions

View File

@ -111,6 +111,11 @@ segRetain=5
broadcastRecordTs=0 broadcastRecordTs=0
#直播hls文件删除延时单位秒issue: #913 #直播hls文件删除延时单位秒issue: #913
deleteDelaySec=0 deleteDelaySec=0
#是否保留hls文件此功能部分等效于segNum=0的情况
#不同的是这个保留不会在m3u8文件中体现
#0为不保留不起作用
#1为保留则不删除hls文件如果开启此功能注意磁盘大小或者定期手动清理hls文件
segKeep=0
[hook] [hook]
#在推流时如果url参数匹对admin_params那么可以不经过hook鉴权直接推流成功播放时亦然 #在推流时如果url参数匹对admin_params那么可以不经过hook鉴权直接推流成功播放时亦然

View File

@ -252,6 +252,7 @@ namespace Hls {
#define HLS_FIELD "hls." #define HLS_FIELD "hls."
const string kSegmentDuration = HLS_FIELD"segDur"; const string kSegmentDuration = HLS_FIELD"segDur";
const string kSegmentNum = HLS_FIELD"segNum"; const string kSegmentNum = HLS_FIELD"segNum";
const string kSegmentKeep = HLS_FIELD"segKeep";
const string kSegmentRetain = HLS_FIELD"segRetain"; const string kSegmentRetain = HLS_FIELD"segRetain";
const string kFileBufSize = HLS_FIELD"fileBufSize"; const string kFileBufSize = HLS_FIELD"fileBufSize";
const string kFilePath = HLS_FIELD"filePath"; const string kFilePath = HLS_FIELD"filePath";
@ -261,6 +262,7 @@ const string kDeleteDelaySec = HLS_FIELD"deleteDelaySec";
static onceToken token([](){ static onceToken token([](){
mINI::Instance()[kSegmentDuration] = 2; mINI::Instance()[kSegmentDuration] = 2;
mINI::Instance()[kSegmentNum] = 3; mINI::Instance()[kSegmentNum] = 3;
mINI::Instance()[kSegmentKeep] = false;
mINI::Instance()[kSegmentRetain] = 5; mINI::Instance()[kSegmentRetain] = 5;
mINI::Instance()[kFileBufSize] = 64 * 1024; mINI::Instance()[kFileBufSize] = 64 * 1024;
mINI::Instance()[kFilePath] = "./www"; mINI::Instance()[kFilePath] = "./www";

View File

@ -295,6 +295,8 @@ namespace Hls {
extern const std::string kSegmentDuration; extern const std::string kSegmentDuration;
//m3u8文件中HLS切片个数如果设置为0则不删除切片而是保存为点播 //m3u8文件中HLS切片个数如果设置为0则不删除切片而是保存为点播
extern const std::string kSegmentNum; extern const std::string kSegmentNum;
//如果设置为0则不保留切片设置为1则一直保留切片
extern const std::string kSegmentKeep;
//HLS切片从m3u8文件中移除后继续保留在磁盘上的个数 //HLS切片从m3u8文件中移除后继续保留在磁盘上的个数
extern const std::string kSegmentRetain; extern const std::string kSegmentRetain;
//HLS文件写缓存大小 //HLS文件写缓存大小

View File

@ -14,10 +14,11 @@ using namespace std;
namespace mediakit { namespace mediakit {
HlsMaker::HlsMaker(float seg_duration, uint32_t seg_number) { HlsMaker::HlsMaker(float seg_duration, uint32_t seg_number, bool seg_keep) {
//最小允许设置为00个切片代表点播 //最小允许设置为00个切片代表点播
_seg_number = seg_number; _seg_number = seg_number;
_seg_duration = seg_duration; _seg_duration = seg_duration;
_seg_keep = seg_keep;
} }
HlsMaker::~HlsMaker() { HlsMaker::~HlsMaker() {
@ -105,7 +106,10 @@ void HlsMaker::delOldSegment() {
if (_file_index > _seg_number) { if (_file_index > _seg_number) {
_seg_dur_list.pop_front(); _seg_dur_list.pop_front();
} }
//如果设置为一直保存,就不删除
if (_seg_keep) {
return;
}
GET_CONFIG(uint32_t, segRetain, Hls::kSegmentRetain); GET_CONFIG(uint32_t, segRetain, Hls::kSegmentRetain);
//但是实际保存的切片个数比m3u8所述多若干个,这样做的目的是防止播放器在切片删除前能下载完毕 //但是实际保存的切片个数比m3u8所述多若干个,这样做的目的是防止播放器在切片删除前能下载完毕
if (_file_index > _seg_number + segRetain) { if (_file_index > _seg_number + segRetain) {
@ -149,6 +153,10 @@ bool HlsMaker::isLive() {
return _seg_number != 0; return _seg_number != 0;
} }
bool HlsMaker::isKeep() {
return _seg_keep;
}
void HlsMaker::clear() { void HlsMaker::clear() {
_file_index = 0; _file_index = 0;
_last_timestamp = 0; _last_timestamp = 0;

View File

@ -26,8 +26,9 @@ public:
/** /**
* @param seg_duration * @param seg_duration
* @param seg_number * @param seg_number
* @param seg_keep
*/ */
HlsMaker(float seg_duration = 5, uint32_t seg_number = 3); HlsMaker(float seg_duration = 5, uint32_t seg_number = 3, bool seg_keep = false);
virtual ~HlsMaker(); virtual ~HlsMaker();
/** /**
@ -44,6 +45,11 @@ public:
*/ */
bool isLive(); bool isLive();
/**
*
*/
bool isKeep();
/** /**
* *
*/ */
@ -108,6 +114,7 @@ private:
private: private:
float _seg_duration = 0; float _seg_duration = 0;
uint32_t _seg_number = 0; uint32_t _seg_number = 0;
bool _seg_keep = false;
uint32_t _last_timestamp = 0; uint32_t _last_timestamp = 0;
uint32_t _last_seg_timestamp = 0; uint32_t _last_seg_timestamp = 0;
uint64_t _file_index = 0; uint64_t _file_index = 0;

View File

@ -23,7 +23,8 @@ HlsMakerImp::HlsMakerImp(const string &m3u8_file,
const string &params, const string &params,
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,
bool seg_keep):HlsMaker(seg_duration, seg_number, seg_keep) {
_poller = EventPollerPool::Instance().getPoller(); _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;
@ -47,7 +48,7 @@ void HlsMakerImp::clearCache() {
void HlsMakerImp::clearCache(bool immediately, bool eof) { void HlsMakerImp::clearCache(bool immediately, bool eof) {
//录制完了 //录制完了
flushLastSegment(eof); flushLastSegment(eof);
if (!isLive()) { if (!isLive()||isKeep()) {
return; return;
} }

View File

@ -25,7 +25,8 @@ public:
const std::string &params, const std::string &params,
uint32_t bufSize = 64 * 1024, uint32_t bufSize = 64 * 1024,
float seg_duration = 5, float seg_duration = 5,
uint32_t seg_number = 3); uint32_t seg_number = 3,
bool seg_keep = false);
~HlsMakerImp() override; ~HlsMakerImp() override;

View File

@ -22,9 +22,10 @@ public:
HlsRecorder(const std::string &m3u8_file, const std::string &params) : MpegMuxer(false) { HlsRecorder(const std::string &m3u8_file, const std::string &params) : MpegMuxer(false) {
GET_CONFIG(uint32_t, hlsNum, Hls::kSegmentNum); GET_CONFIG(uint32_t, hlsNum, Hls::kSegmentNum);
GET_CONFIG(bool, hlsKeep, Hls::kSegmentKeep);
GET_CONFIG(uint32_t, hlsBufSize, Hls::kFileBufSize); GET_CONFIG(uint32_t, hlsBufSize, Hls::kFileBufSize);
GET_CONFIG(float, hlsDuration, Hls::kSegmentDuration); GET_CONFIG(float, hlsDuration, Hls::kSegmentDuration);
_hls = std::make_shared<HlsMakerImp>(m3u8_file, params, hlsBufSize, hlsDuration, hlsNum); _hls = std::make_shared<HlsMakerImp>(m3u8_file, params, hlsBufSize, hlsDuration, hlsNum, hlsKeep);
//清空上次的残余文件 //清空上次的残余文件
_hls->clearCache(); _hls->clearCache();
} }