ZLMediaKit/src/Record/HlsMaker.cpp

170 lines
5.1 KiB
C++
Raw Normal View History

2019-08-08 19:01:45 +08:00
/*
2020-04-04 20:30:09 +08:00
* Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
2017-09-27 16:20:30 +08:00
*
* This file is part of ZLMediaKit(https://github.com/xia-chu/ZLMediaKit).
2017-09-27 16:20:30 +08:00
*
2020-04-04 20:30:09 +08:00
* Use of this source code is governed by MIT license that can be found in the
* LICENSE file in the root of the source tree. All contributing project authors
* may be found in the AUTHORS file in the root of the source tree.
2017-04-01 16:35:56 +08:00
*/
#include "HlsMaker.h"
#include "Common/config.h"
using namespace std;
2018-10-24 17:17:55 +08:00
namespace mediakit {
2017-04-01 16:35:56 +08:00
HlsMaker::HlsMaker(float seg_duration, uint32_t seg_number, bool seg_keep) {
2019-09-26 14:21:20 +08:00
//最小允许设置为00个切片代表点播
_seg_number = seg_number;
_seg_duration = seg_duration;
_seg_keep = seg_keep;
2017-04-01 16:35:56 +08:00
}
HlsMaker::~HlsMaker() {
2017-04-01 16:35:56 +08:00
}
void HlsMaker::makeIndexFile(bool eof) {
2019-09-26 14:21:20 +08:00
char file_content[1024];
2018-03-28 16:06:51 +08:00
int maxSegmentDuration = 0;
for (auto &tp : _seg_dur_list) {
int dur = std::get<0>(tp);
if (dur > maxSegmentDuration) {
2017-11-09 18:14:16 +08:00
maxSegmentDuration = dur;
}
}
2019-09-26 14:21:20 +08:00
auto sequence = _seg_number ? (_file_index > _seg_number ? _file_index - _seg_number : 0LL) : 0LL;
2019-09-26 14:21:20 +08:00
string m3u8;
if (_seg_number == 0) {
// 录像点播支持时移
snprintf(file_content, sizeof(file_content),
"#EXTM3U\n"
"#EXT-X-PLAYLIST-TYPE:EVENT\n"
"#EXT-X-VERSION:4\n"
"#EXT-X-TARGETDURATION:%u\n"
"#EXT-X-MEDIA-SEQUENCE:%llu\n",
(maxSegmentDuration + 999) / 1000,
sequence);
} else {
snprintf(file_content, sizeof(file_content),
"#EXTM3U\n"
"#EXT-X-VERSION:3\n"
"#EXT-X-ALLOW-CACHE:NO\n"
"#EXT-X-TARGETDURATION:%u\n"
"#EXT-X-MEDIA-SEQUENCE:%llu\n",
(maxSegmentDuration + 999) / 1000,
sequence);
}
2019-09-26 14:21:20 +08:00
m3u8.assign(file_content);
for (auto &tp : _seg_dur_list) {
snprintf(file_content, sizeof(file_content), "#EXTINF:%.3f,\n%s\n", std::get<0>(tp) / 1000.0, std::get<1>(tp).data());
2019-09-26 14:21:20 +08:00
m3u8.append(file_content);
2018-03-28 15:56:47 +08:00
}
2018-11-17 16:34:31 +08:00
if (eof) {
snprintf(file_content, sizeof(file_content), "#EXT-X-ENDLIST\n");
2019-09-26 14:21:20 +08:00
m3u8.append(file_content);
}
2022-02-11 16:21:19 +08:00
onWriteHls(m3u8);
2017-04-01 16:35:56 +08:00
}
2022-08-08 17:13:39 +08:00
void HlsMaker::inputData(void *data, size_t len, uint64_t timestamp, bool is_idr_fast_packet) {
if (data && len) {
if (timestamp < _last_timestamp) {
//时间戳回退了,切片时长重新计时
WarnL << "stamp reduce: " << _last_timestamp << " -> " << timestamp;
_last_seg_timestamp = _last_timestamp = timestamp;
}
if (is_idr_fast_packet) {
//尝试切片ts
2020-01-24 22:00:55 +08:00
addNewSegment(timestamp);
}
if (!_last_file_name.empty()) {
//存在切片才写入ts数据
onWriteSegment((char *) data, len);
_last_timestamp = timestamp;
}
2019-10-11 17:51:46 +08:00
} else {
2020-01-24 22:00:55 +08:00
//resetTracks时触发此逻辑
flushLastSegment(false);
}
2017-04-01 16:35:56 +08:00
}
2019-10-12 10:29:40 +08:00
void HlsMaker::delOldSegment() {
if (_seg_number == 0) {
2019-09-26 14:21:20 +08:00
//如果设置为保留0个切片则认为是保存为点播
return;
}
//在hls m3u8索引文件中,我们保存的切片个数跟_seg_number相关设置一致
if (_file_index > _seg_number) {
_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);
}
2018-10-28 00:15:27 +08:00
}
2022-08-08 17:13:39 +08:00
void HlsMaker::addNewSegment(uint64_t stamp) {
if (!_last_file_name.empty() && stamp - _last_seg_timestamp < _seg_duration * 1000) {
2019-10-12 10:29:40 +08:00
//存在上个切片,并且未到分片时间
return;
2019-10-11 19:01:34 +08:00
}
2019-10-12 10:29:40 +08:00
//关闭并保存上一个切片如果_seg_number==0,那么是点播。
flushLastSegment(false);
2019-10-12 10:29:40 +08:00
//新增切片
_last_file_name = onOpenSegment(_file_index++);
//记录本次切片的起始时间戳
_last_seg_timestamp = _last_timestamp ? _last_timestamp : stamp;
2019-10-12 10:29:40 +08:00
}
void HlsMaker::flushLastSegment(bool eof){
if (_last_file_name.empty()) {
2019-10-12 10:29:40 +08:00
//不存在上个切片
return;
}
//文件创建到最后一次数据写入的时间即为切片长度
auto seg_dur = _last_timestamp - _last_seg_timestamp;
if (seg_dur <= 0) {
2019-10-12 10:29:40 +08:00
seg_dur = 100;
}
_seg_dur_list.emplace_back(seg_dur, std::move(_last_file_name));
2019-10-12 10:29:40 +08:00
delOldSegment();
//先flush ts切片否则可能存在ts文件未写入完毕就被访问的情况
2020-09-13 14:08:05 +08:00
onFlushLastSegment(seg_dur);
//然后写m3u8文件
makeIndexFile(eof);
2020-09-13 14:08:05 +08:00
}
2019-12-17 09:18:11 +08:00
bool HlsMaker::isLive() {
return _seg_number != 0;
}
bool HlsMaker::isKeep() {
return _seg_keep;
}
void HlsMaker::clear() {
_file_index = 0;
_last_timestamp = 0;
_last_seg_timestamp = 0;
2020-09-12 19:20:18 +08:00
_seg_dur_list.clear();
_last_file_name.clear();
}
}//namespace mediakit