支持hls点播录制

This commit is contained in:
xiongziliang 2019-09-26 14:21:20 +08:00
parent 86fcf69f81
commit 4bc355b08a
5 changed files with 34 additions and 15 deletions

View File

@ -39,6 +39,7 @@ filePath=/Users/xzl/git/ZLMediaKit/release/mac/Release/httpRoot
#hls最大切片时间 #hls最大切片时间
segDur=3 segDur=3
#m3u8索引中,hls保留切片个数(实际保留切片个数大2~3个) #m3u8索引中,hls保留切片个数(实际保留切片个数大2~3个)
#如果设置为0则不删除切片而是保存为点播
segNum=3 segNum=3
[hook] [hook]

View File

@ -276,7 +276,7 @@ extern const string kFileRepeat;
namespace Hls { namespace Hls {
//HLS切片时长,单位秒 //HLS切片时长,单位秒
extern const string kSegmentDuration; extern const string kSegmentDuration;
//HLS切片个数 //HLS切片个数如果设置为0则不删除切片而是保存为点播
extern const string kSegmentNum; extern const string kSegmentNum;
//HLS文件写缓存大小 //HLS文件写缓存大小
extern const string kFileBufSize; extern const string kFileBufSize;

View File

@ -28,7 +28,8 @@
namespace mediakit { namespace mediakit {
HlsMaker::HlsMaker(float seg_duration, uint32_t seg_number) { HlsMaker::HlsMaker(float seg_duration, uint32_t seg_number) {
seg_number = MAX(1,seg_number); //最小允许设置为00个切片代表点播
seg_number = MAX(0,seg_number);
seg_duration = MAX(1,seg_duration); seg_duration = MAX(1,seg_duration);
_seg_number = seg_number; _seg_number = seg_number;
_seg_duration = seg_duration; _seg_duration = seg_duration;
@ -37,12 +38,9 @@ HlsMaker::HlsMaker(float seg_duration, uint32_t seg_number) {
HlsMaker::~HlsMaker() { HlsMaker::~HlsMaker() {
} }
#define PRINT(...) file_size += snprintf(file_content + file_size,sizeof(file_content) - file_size, ##__VA_ARGS__)
void HlsMaker::makeIndexFile(bool eof) { void HlsMaker::makeIndexFile(bool eof) {
char file_content[4 * 1024]; char file_content[1024];
int file_size = 0;
int maxSegmentDuration = 0; int maxSegmentDuration = 0;
for (auto &tp : _seg_dur_list) { for (auto &tp : _seg_dur_list) {
int dur = std::get<0>(tp); int dur = std::get<0>(tp);
@ -50,7 +48,10 @@ void HlsMaker::makeIndexFile(bool eof) {
maxSegmentDuration = dur; maxSegmentDuration = dur;
} }
} }
PRINT("#EXTM3U\n"
string m3u8;
snprintf(file_content,sizeof(file_content),
"#EXTM3U\n"
"#EXT-X-VERSION:3\n" "#EXT-X-VERSION:3\n"
"#EXT-X-ALLOW-CACHE:NO\n" "#EXT-X-ALLOW-CACHE:NO\n"
"#EXT-X-TARGETDURATION:%u\n" "#EXT-X-TARGETDURATION:%u\n"
@ -58,14 +59,18 @@ void HlsMaker::makeIndexFile(bool eof) {
(maxSegmentDuration + 999) / 1000, (maxSegmentDuration + 999) / 1000,
_file_index); _file_index);
m3u8.assign(file_content);
for (auto &tp : _seg_dur_list) { 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) { 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() { void HlsMaker::delOldFile() {
if(_seg_number == 0){
//如果设置为保留0个切片则认为是保存为点播
return;
}
//在hls m3u8索引文件中,我们保存的切片个数跟_seg_number相关设置一致 //在hls m3u8索引文件中,我们保存的切片个数跟_seg_number相关设置一致
if (_file_index >= _seg_number + 2) { if (_file_index >= _seg_number + 2) {
_seg_dur_list.pop_front(); _seg_dur_list.pop_front();

View File

@ -81,13 +81,18 @@ protected:
* @param len * @param len
*/ */
virtual void onWriteHls(const char *data, int len) = 0; virtual void onWriteHls(const char *data, int len) = 0;
private:
/**
* m3u8文件
* @param eof true代表点播
*/
void makeIndexFile(bool eof = false);
void delOldFile(); void delOldFile();
void addNewFile(uint32_t timestamp); void addNewFile(uint32_t timestamp);
void makeIndexFile(bool eof = false); protected:
uint32_t _seg_number = 0;
private: private:
float _seg_duration = 0; float _seg_duration = 0;
uint32_t _seg_number = 0;
uint64_t _file_index = 0; uint64_t _file_index = 0;
Ticker _ticker; Ticker _ticker;
string _last_file_name; string _last_file_name;

View File

@ -46,9 +46,13 @@ HlsMakerImp::HlsMakerImp(const string &m3u8_file,
} }
HlsMakerImp::~HlsMakerImp() { HlsMakerImp::~HlsMakerImp() {
_file.reset(); //录制完了
makeIndexFile(true);
if(_seg_number){
//hls直播才删除文件
File::delete_file(_path_prefix.data()); File::delete_file(_path_prefix.data());
} }
}
string HlsMakerImp::onOpenFile(int index) { string HlsMakerImp::onOpenFile(int index) {
auto full_path = fullPath(index); auto full_path = fullPath(index);