Add hls_retain.m3u8 for some use case.
This commit is contained in:
parent
07f3c6dde4
commit
01b3755f9b
|
|
@ -24,15 +24,23 @@ HlsMaker::HlsMaker(bool is_fmp4, float seg_duration, uint32_t seg_number, bool s
|
||||||
_seg_keep = seg_keep;
|
_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;
|
int maxSegmentDuration = 0;
|
||||||
for (auto &tp : _seg_dur_list) {
|
for (auto &tp : temp) {
|
||||||
int dur = std::get<0>(tp);
|
int dur = std::get<0>(tp);
|
||||||
if (dur > maxSegmentDuration) {
|
if (dur > maxSegmentDuration) {
|
||||||
maxSegmentDuration = dur;
|
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;
|
string index_str;
|
||||||
index_str.reserve(2048);
|
index_str.reserve(2048);
|
||||||
|
|
@ -50,7 +58,7 @@ void HlsMaker::makeIndexFile(bool eof) {
|
||||||
}
|
}
|
||||||
|
|
||||||
stringstream ss;
|
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";
|
ss << "#EXTINF:" << std::setprecision(3) << std::get<0>(tp) / 1000.0 << ",\n" << std::get<1>(tp) << "\n";
|
||||||
}
|
}
|
||||||
index_str += ss.str();
|
index_str += ss.str();
|
||||||
|
|
@ -58,7 +66,7 @@ void HlsMaker::makeIndexFile(bool eof) {
|
||||||
if (eof) {
|
if (eof) {
|
||||||
index_str += "#EXT-X-ENDLIST\n";
|
index_str += "#EXT-X-ENDLIST\n";
|
||||||
}
|
}
|
||||||
onWriteHls(index_str);
|
onWriteHls(index_str, include_retain);
|
||||||
}
|
}
|
||||||
|
|
||||||
void HlsMaker::inputInitSegment(const char *data, size_t len) {
|
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() {
|
void HlsMaker::delOldSegment() {
|
||||||
|
GET_CONFIG(uint32_t, segRetain, Hls::kSegmentRetain);
|
||||||
if (_seg_number == 0) {
|
if (_seg_number == 0) {
|
||||||
//如果设置为保留0个切片,则认为是保存为点播
|
//如果设置为保留0个切片,则认为是保存为点播
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
//在hls m3u8索引文件中,我们保存的切片个数跟_seg_number相关设置一致
|
//在hls m3u8索引文件中,我们保存的切片个数跟_seg_number相关设置一致
|
||||||
if (_file_index > _seg_number) {
|
if (_file_index > _seg_number + segRetain) {
|
||||||
_seg_dur_list.pop_front();
|
_seg_dur_list.pop_front();
|
||||||
}
|
}
|
||||||
//如果设置为一直保存,就不删除
|
//如果设置为一直保存,就不删除
|
||||||
if (_seg_keep) {
|
if (_seg_keep) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
GET_CONFIG(uint32_t, segRetain, Hls::kSegmentRetain);
|
|
||||||
//但是实际保存的切片个数比m3u8所述多若干个,这样做的目的是防止播放器在切片删除前能下载完毕
|
//但是实际保存的切片个数比m3u8所述多若干个,这样做的目的是防止播放器在切片删除前能下载完毕
|
||||||
if (_file_index > _seg_number + segRetain) {
|
if (_file_index > _seg_number + segRetain) {
|
||||||
onDelSegment(_file_index - _seg_number - segRetain - 1);
|
onDelSegment(_file_index - _seg_number - segRetain - 1);
|
||||||
|
|
@ -139,7 +147,8 @@ void HlsMaker::flushLastSegment(bool eof){
|
||||||
//先flush ts切片,否则可能存在ts文件未写入完毕就被访问的情况
|
//先flush ts切片,否则可能存在ts文件未写入完毕就被访问的情况
|
||||||
onFlushLastSegment(seg_dur);
|
onFlushLastSegment(seg_dur);
|
||||||
//然后写m3u8文件
|
//然后写m3u8文件
|
||||||
makeIndexFile(eof);
|
makeIndexFile(true, eof);
|
||||||
|
makeIndexFile(false, eof);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool HlsMaker::isLive() const {
|
bool HlsMaker::isLive() const {
|
||||||
|
|
|
||||||
|
|
@ -96,7 +96,7 @@ protected:
|
||||||
/**
|
/**
|
||||||
* 写m3u8文件回调
|
* 写m3u8文件回调
|
||||||
*/
|
*/
|
||||||
virtual void onWriteHls(const std::string &data) = 0;
|
virtual void onWriteHls(const std::string &data, bool include_retain) = 0;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 上一个 ts 切片写入完成, 可在这里进行通知处理
|
* 上一个 ts 切片写入完成, 可在这里进行通知处理
|
||||||
|
|
@ -115,7 +115,7 @@ private:
|
||||||
* 生成m3u8文件
|
* 生成m3u8文件
|
||||||
* @param eof true代表点播
|
* @param eof true代表点播
|
||||||
*/
|
*/
|
||||||
void makeIndexFile(bool eof = false);
|
void makeIndexFile(bool include_retain, bool eof = false);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 删除旧的ts切片
|
* 删除旧的ts切片
|
||||||
|
|
|
||||||
|
|
@ -21,11 +21,20 @@ using namespace toolkit;
|
||||||
|
|
||||||
namespace mediakit {
|
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 ¶ms, uint32_t bufSize, float seg_duration,
|
HlsMakerImp::HlsMakerImp(bool is_fmp4, const string &m3u8_file, const string ¶ms, uint32_t bufSize, float seg_duration,
|
||||||
uint32_t seg_number, bool seg_keep) : HlsMaker(is_fmp4, seg_duration, seg_number, seg_keep) {
|
uint32_t seg_number, bool seg_keep) : HlsMaker(is_fmp4, 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;
|
||||||
|
_path_hls_retain = getRetainPath(m3u8_file);
|
||||||
_params = params;
|
_params = params;
|
||||||
_buf_size = bufSize;
|
_buf_size = bufSize;
|
||||||
_file_buf.reset(new char[bufSize], [](char *ptr) { delete[] ptr; });
|
_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;
|
std::list<std::string> lst;
|
||||||
lst.emplace_back(_path_hls);
|
lst.emplace_back(_path_hls);
|
||||||
|
lst.emplace_back(_path_hls_retain);
|
||||||
if (!_path_init.empty()) {
|
if (!_path_init.empty()) {
|
||||||
lst.emplace_back(_path_init);
|
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) {
|
void HlsMakerImp::onWriteHls(const std::string &data, bool include_retain) {
|
||||||
auto hls = makeFile(_path_hls);
|
auto path = include_retain ? _path_hls_retain : _path_hls;
|
||||||
|
auto hls = makeFile(path);
|
||||||
if (hls) {
|
if (hls) {
|
||||||
fwrite(data.data(), data.size(), 1, hls.get());
|
fwrite(data.data(), data.size(), 1, hls.get());
|
||||||
hls.reset();
|
hls.reset();
|
||||||
|
|
@ -155,7 +166,7 @@ void HlsMakerImp::onWriteHls(const std::string &data) {
|
||||||
_media_src->setIndexFile(data);
|
_media_src->setIndexFile(data);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
WarnL << "Create hls file failed," << _path_hls << " " << get_uv_errmsg();
|
WarnL << "Create hls file failed," << path << " " << get_uv_errmsg();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -49,7 +49,7 @@ protected:
|
||||||
void onDelSegment(uint64_t index) override;
|
void onDelSegment(uint64_t index) override;
|
||||||
void onWriteInitSegment(const char *data, size_t len) override;
|
void onWriteInitSegment(const char *data, size_t len) override;
|
||||||
void onWriteSegment(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;
|
void onFlushLastSegment(uint64_t duration_ms) override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
@ -60,6 +60,7 @@ private:
|
||||||
int _buf_size;
|
int _buf_size;
|
||||||
std::string _params;
|
std::string _params;
|
||||||
std::string _path_hls;
|
std::string _path_hls;
|
||||||
|
std::string _path_hls_retain;
|
||||||
std::string _path_init;
|
std::string _path_init;
|
||||||
std::string _path_prefix;
|
std::string _path_prefix;
|
||||||
RecordInfo _info;
|
RecordInfo _info;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue