Commit 5bc313ba by WuPeng

增加长期保存hls切片的功能,segKeep=0为不长期保存,segKeep=1为长期保存。此功能部分等效于segNum=0的情况,不同的是这个保留不会在m3u8文件中体现。

parent a9d78744
...@@ -111,6 +111,11 @@ segRetain=5 ...@@ -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鉴权直接推流成功,播放时亦然
......
...@@ -252,6 +252,7 @@ namespace Hls { ...@@ -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"; ...@@ -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";
......
...@@ -295,6 +295,8 @@ namespace Hls { ...@@ -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文件写缓存大小
......
...@@ -14,10 +14,11 @@ using namespace std; ...@@ -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) {
//最小允许设置为0,0个切片代表点播 //最小允许设置为0,0个切片代表点播
_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() { ...@@ -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() { ...@@ -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;
......
...@@ -26,8 +26,9 @@ public: ...@@ -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();
/** /**
...@@ -45,6 +46,11 @@ public: ...@@ -45,6 +46,11 @@ public:
bool isLive(); bool isLive();
/** /**
* 是否保留切片文件
*/
bool isKeep();
/**
* 清空记录 * 清空记录
*/ */
void clear(); void clear();
...@@ -108,6 +114,7 @@ private: ...@@ -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;
......
...@@ -23,7 +23,8 @@ HlsMakerImp::HlsMakerImp(const string &m3u8_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() { ...@@ -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;
} }
......
...@@ -25,7 +25,8 @@ public: ...@@ -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;
......
...@@ -22,9 +22,10 @@ public: ...@@ -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();
} }
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论