Add hls_retain.m3u8 for some use case.

This commit is contained in:
Seto 2023-12-14 17:03:52 +08:00
parent 07f3c6dde4
commit 01b3755f9b
4 changed files with 35 additions and 14 deletions

View File

@ -24,15 +24,23 @@ HlsMaker::HlsMaker(bool is_fmp4, float seg_duration, uint32_t seg_number, bool s
_seg_keep = seg_keep;
}
void HlsMaker::makeIndexFile(bool eof) {
void HlsMaker::makeIndexFile(bool include_retain, bool eof) {
GET_CONFIG(uint32_t, segRetain, Hls::kSegmentRetain);
std::deque<std::tuple<int, std::string>> temp(_seg_dur_list);
if (!include_retain) {
while (temp.size() > _seg_number) {
temp.pop_front();
}
}
int maxSegmentDuration = 0;
for (auto &tp : _seg_dur_list) {
for (auto &tp : temp) {
int dur = std::get<0>(tp);
if (dur > maxSegmentDuration) {
maxSegmentDuration = dur;
}
}
auto index_seq = _seg_number ? (_file_index > _seg_number ? _file_index - _seg_number : 0LL) : 0LL;
auto index_seq
= _seg_number ? (_file_index > _seg_number ? (include_retain ? _file_index - _seg_number - segRetain : _file_index - _seg_number) : 0LL) : 0LL;
string index_str;
index_str.reserve(2048);
@ -50,7 +58,7 @@ void HlsMaker::makeIndexFile(bool eof) {
}
stringstream ss;
for (auto &tp : _seg_dur_list) {
for (auto &tp : temp) {
ss << "#EXTINF:" << std::setprecision(3) << std::get<0>(tp) / 1000.0 << ",\n" << std::get<1>(tp) << "\n";
}
index_str += ss.str();
@ -58,7 +66,7 @@ void HlsMaker::makeIndexFile(bool eof) {
if (eof) {
index_str += "#EXT-X-ENDLIST\n";
}
onWriteHls(index_str);
onWriteHls(index_str, include_retain);
}
void HlsMaker::inputInitSegment(const char *data, size_t len) {
@ -91,19 +99,19 @@ void HlsMaker::inputData(const char *data, size_t len, uint64_t timestamp, bool
}
void HlsMaker::delOldSegment() {
GET_CONFIG(uint32_t, segRetain, Hls::kSegmentRetain);
if (_seg_number == 0) {
//如果设置为保留0个切片则认为是保存为点播
return;
}
//在hls m3u8索引文件中,我们保存的切片个数跟_seg_number相关设置一致
if (_file_index > _seg_number) {
if (_file_index > _seg_number + segRetain) {
_seg_dur_list.pop_front();
}
//如果设置为一直保存,就不删除
if (_seg_keep) {
return;
}
GET_CONFIG(uint32_t, segRetain, Hls::kSegmentRetain);
//但是实际保存的切片个数比m3u8所述多若干个,这样做的目的是防止播放器在切片删除前能下载完毕
if (_file_index > _seg_number + segRetain) {
onDelSegment(_file_index - _seg_number - segRetain - 1);
@ -139,7 +147,8 @@ void HlsMaker::flushLastSegment(bool eof){
//先flush ts切片否则可能存在ts文件未写入完毕就被访问的情况
onFlushLastSegment(seg_dur);
//然后写m3u8文件
makeIndexFile(eof);
makeIndexFile(true, eof);
makeIndexFile(false, eof);
}
bool HlsMaker::isLive() const {

View File

@ -96,7 +96,7 @@ protected:
/**
* m3u8文件回调
*/
virtual void onWriteHls(const std::string &data) = 0;
virtual void onWriteHls(const std::string &data, bool include_retain) = 0;
/**
* ts ,
@ -115,7 +115,7 @@ private:
* m3u8文件
* @param eof true代表点播
*/
void makeIndexFile(bool eof = false);
void makeIndexFile(bool include_retain, bool eof = false);
/**
* ts切片

View File

@ -21,11 +21,20 @@ using namespace toolkit;
namespace mediakit {
std::string getRetainPath(const std::string& originalPath) {
std::size_t pos = originalPath.find(".m3u8");
if (pos != std::string::npos) {
return originalPath.substr(0, pos) + "_retain.m3u8";
}
return originalPath;
}
HlsMakerImp::HlsMakerImp(bool is_fmp4, const string &m3u8_file, const string &params, uint32_t bufSize, float seg_duration,
uint32_t seg_number, bool seg_keep) : HlsMaker(is_fmp4, seg_duration, seg_number, seg_keep) {
_poller = EventPollerPool::Instance().getPoller();
_path_prefix = m3u8_file.substr(0, m3u8_file.rfind('/'));
_path_hls = m3u8_file;
_path_hls_retain = getRetainPath(m3u8_file);
_params = params;
_buf_size = bufSize;
_file_buf.reset(new char[bufSize], [](char *ptr) { delete[] ptr; });
@ -62,6 +71,7 @@ void HlsMakerImp::clearCache(bool immediately, bool eof) {
{
std::list<std::string> lst;
lst.emplace_back(_path_hls);
lst.emplace_back(_path_hls_retain);
if (!_path_init.empty()) {
lst.emplace_back(_path_init);
}
@ -146,8 +156,9 @@ void HlsMakerImp::onWriteSegment(const char *data, size_t len) {
}
}
void HlsMakerImp::onWriteHls(const std::string &data) {
auto hls = makeFile(_path_hls);
void HlsMakerImp::onWriteHls(const std::string &data, bool include_retain) {
auto path = include_retain ? _path_hls_retain : _path_hls;
auto hls = makeFile(path);
if (hls) {
fwrite(data.data(), data.size(), 1, hls.get());
hls.reset();
@ -155,7 +166,7 @@ void HlsMakerImp::onWriteHls(const std::string &data) {
_media_src->setIndexFile(data);
}
} else {
WarnL << "Create hls file failed," << _path_hls << " " << get_uv_errmsg();
WarnL << "Create hls file failed," << path << " " << get_uv_errmsg();
}
}

View File

@ -49,7 +49,7 @@ protected:
void onDelSegment(uint64_t index) override;
void onWriteInitSegment(const char *data, size_t len) override;
void onWriteSegment(const char *data, size_t len) override;
void onWriteHls(const std::string &data) override;
void onWriteHls(const std::string &data, bool include_retain) override;
void onFlushLastSegment(uint64_t duration_ms) override;
private:
@ -60,6 +60,7 @@ private:
int _buf_size;
std::string _params;
std::string _path_hls;
std::string _path_hls_retain;
std::string _path_init;
std::string _path_prefix;
RecordInfo _info;