diff --git a/conf/config.ini b/conf/config.ini index 902d2215..7ce085e8 100644 --- a/conf/config.ini +++ b/conf/config.ini @@ -39,6 +39,7 @@ filePath=/Users/xzl/git/ZLMediaKit/release/mac/Release/httpRoot #hls最大切片时间 segDur=3 #m3u8索引中,hls保留切片个数(实际保留切片个数大2~3个) +#如果设置为0,则不删除切片,而是保存为点播 segNum=3 [hook] diff --git a/src/Common/config.h b/src/Common/config.h index 89ed9cb1..d6ab955c 100644 --- a/src/Common/config.h +++ b/src/Common/config.h @@ -276,7 +276,7 @@ extern const string kFileRepeat; namespace Hls { //HLS切片时长,单位秒 extern const string kSegmentDuration; -//HLS切片个数 +//HLS切片个数,如果设置为0,则不删除切片,而是保存为点播 extern const string kSegmentNum; //HLS文件写缓存大小 extern const string kFileBufSize; diff --git a/src/MediaFile/HlsMaker.cpp b/src/MediaFile/HlsMaker.cpp index 5e88b31a..36f88c39 100644 --- a/src/MediaFile/HlsMaker.cpp +++ b/src/MediaFile/HlsMaker.cpp @@ -28,7 +28,8 @@ namespace mediakit { HlsMaker::HlsMaker(float seg_duration, uint32_t seg_number) { - seg_number = MAX(1,seg_number); + //最小允许设置为0,0个切片代表点播 + seg_number = MAX(0,seg_number); seg_duration = MAX(1,seg_duration); _seg_number = seg_number; _seg_duration = seg_duration; @@ -37,12 +38,9 @@ HlsMaker::HlsMaker(float seg_duration, uint32_t seg_number) { HlsMaker::~HlsMaker() { } -#define PRINT(...) file_size += snprintf(file_content + file_size,sizeof(file_content) - file_size, ##__VA_ARGS__) void HlsMaker::makeIndexFile(bool eof) { - char file_content[4 * 1024]; - int file_size = 0; - + char file_content[1024]; int maxSegmentDuration = 0; for (auto &tp : _seg_dur_list) { int dur = std::get<0>(tp); @@ -50,7 +48,10 @@ void HlsMaker::makeIndexFile(bool eof) { maxSegmentDuration = dur; } } - PRINT("#EXTM3U\n" + + string m3u8; + snprintf(file_content,sizeof(file_content), + "#EXTM3U\n" "#EXT-X-VERSION:3\n" "#EXT-X-ALLOW-CACHE:NO\n" "#EXT-X-TARGETDURATION:%u\n" @@ -58,14 +59,18 @@ void HlsMaker::makeIndexFile(bool eof) { (maxSegmentDuration + 999) / 1000, _file_index); + m3u8.assign(file_content); + for (auto &tp : _seg_dur_list) { - PRINT("#EXTINF:%.3f,\n%s\n", std::get<0>(tp) / 1000.0, std::get<1>(tp).data()); + snprintf(file_content,sizeof(file_content), "#EXTINF:%.3f,\n%s\n", std::get<0>(tp) / 1000.0, std::get<1>(tp).data()); + m3u8.append(file_content); } if (eof) { - PRINT("#EXT-X-ENDLIST\n"); + snprintf(file_content,sizeof(file_content),"#EXT-X-ENDLIST\n"); + m3u8.append(file_content); } - onWriteHls(file_content, file_size); + onWriteHls(m3u8.data(), m3u8.size()); } @@ -75,6 +80,10 @@ void HlsMaker::inputData(void *data, uint32_t len, uint32_t timestamp) { } void HlsMaker::delOldFile() { + if(_seg_number == 0){ + //如果设置为保留0个切片,则认为是保存为点播 + return; + } //在hls m3u8索引文件中,我们保存的切片个数跟_seg_number相关设置一致 if (_file_index >= _seg_number + 2) { _seg_dur_list.pop_front(); diff --git a/src/MediaFile/HlsMaker.h b/src/MediaFile/HlsMaker.h index 37f6d671..0a1e9617 100644 --- a/src/MediaFile/HlsMaker.h +++ b/src/MediaFile/HlsMaker.h @@ -81,13 +81,18 @@ protected: * @param len */ virtual void onWriteHls(const char *data, int len) = 0; -private: + + /** + * 生成m3u8文件 + * @param eof true代表点播 + */ + void makeIndexFile(bool eof = false); void delOldFile(); void addNewFile(uint32_t timestamp); - void makeIndexFile(bool eof = false); +protected: + uint32_t _seg_number = 0; private: float _seg_duration = 0; - uint32_t _seg_number = 0; uint64_t _file_index = 0; Ticker _ticker; string _last_file_name; diff --git a/src/MediaFile/HlsMakerImp.cpp b/src/MediaFile/HlsMakerImp.cpp index ece25b73..77763e30 100644 --- a/src/MediaFile/HlsMakerImp.cpp +++ b/src/MediaFile/HlsMakerImp.cpp @@ -46,8 +46,12 @@ HlsMakerImp::HlsMakerImp(const string &m3u8_file, } HlsMakerImp::~HlsMakerImp() { - _file.reset(); - File::delete_file(_path_prefix.data()); + //录制完了 + makeIndexFile(true); + if(_seg_number){ + //hls直播才删除文件 + File::delete_file(_path_prefix.data()); + } } string HlsMakerImp::onOpenFile(int index) {