2019-08-08 19:01:45 +08:00
|
|
|
|
/*
|
2017-09-27 16:20:30 +08:00
|
|
|
|
* MIT License
|
2017-04-01 16:35:56 +08:00
|
|
|
|
*
|
2019-05-08 15:40:07 +08:00
|
|
|
|
* Copyright (c) 2016-2019 xiongziliang <771730766@qq.com>
|
2017-09-27 16:20:30 +08:00
|
|
|
|
*
|
|
|
|
|
* This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit).
|
|
|
|
|
*
|
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
|
*
|
|
|
|
|
* The above copyright notice and this permission notice shall be included in all
|
|
|
|
|
* copies or substantial portions of the Software.
|
|
|
|
|
*
|
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
|
* SOFTWARE.
|
2017-04-01 16:35:56 +08:00
|
|
|
|
*/
|
|
|
|
|
|
2019-04-01 12:57:33 +08:00
|
|
|
|
#include "HlsMaker.h"
|
2018-10-24 17:17:55 +08:00
|
|
|
|
namespace mediakit {
|
2017-04-01 16:35:56 +08:00
|
|
|
|
|
2019-04-01 12:57:33 +08:00
|
|
|
|
HlsMaker::HlsMaker(float seg_duration, uint32_t seg_number) {
|
2019-09-26 14:21:20 +08:00
|
|
|
|
//最小允许设置为0,0个切片代表点播
|
|
|
|
|
seg_number = MAX(0,seg_number);
|
2019-04-01 12:57:33 +08:00
|
|
|
|
seg_duration = MAX(1,seg_duration);
|
|
|
|
|
_seg_number = seg_number;
|
|
|
|
|
_seg_duration = seg_duration;
|
2017-04-01 16:35:56 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-04-01 12:57:33 +08:00
|
|
|
|
HlsMaker::~HlsMaker() {
|
2017-04-01 16:35:56 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-04-01 12:57:33 +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;
|
2019-09-29 22:05:18 +08:00
|
|
|
|
|
2019-04-01 12:57:33 +08:00
|
|
|
|
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
|
|
|
|
|
|
|
|
|
string m3u8;
|
|
|
|
|
snprintf(file_content,sizeof(file_content),
|
|
|
|
|
"#EXTM3U\n"
|
2019-04-01 12:57:33 +08:00
|
|
|
|
"#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,
|
2019-09-29 22:05:18 +08:00
|
|
|
|
_seg_number ? _file_index : 0);
|
2019-04-01 12:57:33 +08:00
|
|
|
|
|
2019-09-26 14:21:20 +08:00
|
|
|
|
m3u8.assign(file_content);
|
|
|
|
|
|
2019-04-01 12:57:33 +08:00
|
|
|
|
for (auto &tp : _seg_dur_list) {
|
2019-09-26 14:21:20 +08:00
|
|
|
|
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);
|
2018-03-28 15:56:47 +08:00
|
|
|
|
}
|
2018-11-17 16:34:31 +08:00
|
|
|
|
|
2019-04-01 12:57:33 +08:00
|
|
|
|
if (eof) {
|
2019-09-26 14:21:20 +08:00
|
|
|
|
snprintf(file_content,sizeof(file_content),"#EXT-X-ENDLIST\n");
|
|
|
|
|
m3u8.append(file_content);
|
2019-04-01 12:57:33 +08:00
|
|
|
|
}
|
2019-09-26 14:21:20 +08:00
|
|
|
|
onWriteHls(m3u8.data(), m3u8.size());
|
2017-04-01 16:35:56 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2019-04-01 12:57:33 +08:00
|
|
|
|
void HlsMaker::inputData(void *data, uint32_t len, uint32_t timestamp) {
|
2019-10-11 16:51:10 +08:00
|
|
|
|
//分片数据中断结束
|
|
|
|
|
if (data && len) {
|
2019-10-12 10:29:40 +08:00
|
|
|
|
addNewSegment(timestamp);
|
|
|
|
|
onWriteSegment((char *) data, len);
|
|
|
|
|
//记录上次写入数据时间
|
|
|
|
|
_ticker_last_data.resetTime();
|
2019-10-11 17:51:46 +08:00
|
|
|
|
} else {
|
2019-10-12 10:29:40 +08:00
|
|
|
|
flushLastSegment(true);
|
2019-10-11 16:51:10 +08:00
|
|
|
|
}
|
2017-04-01 16:35:56 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-10-12 10:29:40 +08:00
|
|
|
|
void HlsMaker::delOldSegment() {
|
2019-09-26 14:21:20 +08:00
|
|
|
|
if(_seg_number == 0){
|
|
|
|
|
//如果设置为保留0个切片,则认为是保存为点播
|
|
|
|
|
return;
|
|
|
|
|
}
|
2019-04-01 12:57:33 +08:00
|
|
|
|
//在hls m3u8索引文件中,我们保存的切片个数跟_seg_number相关设置一致
|
2019-11-05 10:27:28 +08:00
|
|
|
|
if (_file_index > _seg_number) {
|
2019-04-01 12:57:33 +08:00
|
|
|
|
_seg_dur_list.pop_front();
|
|
|
|
|
}
|
2018-10-28 00:15:27 +08:00
|
|
|
|
|
2019-11-05 10:27:28 +08:00
|
|
|
|
GET_CONFIG(uint32_t,segRetain,Hls::kSegmentRetain);
|
|
|
|
|
//但是实际保存的切片个数比m3u8所述多若干个,这样做的目的是防止播放器在切片删除前能下载完毕
|
|
|
|
|
if (_file_index > _seg_number + segRetain) {
|
|
|
|
|
onDelSegment(_file_index - _seg_number - segRetain - 1);
|
2019-04-01 12:57:33 +08:00
|
|
|
|
}
|
2018-10-28 00:15:27 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-10-12 10:29:40 +08:00
|
|
|
|
void HlsMaker::addNewSegment(uint32_t) {
|
|
|
|
|
if(!_last_file_name.empty() && _ticker.elapsedTime() < _seg_duration * 1000){
|
|
|
|
|
//存在上个切片,并且未到分片时间
|
|
|
|
|
return;
|
2019-10-11 19:01:34 +08:00
|
|
|
|
}
|
2019-10-12 10:29:40 +08:00
|
|
|
|
|
2019-11-29 11:59:39 +08:00
|
|
|
|
//关闭并保存上一个切片,如果_seg_number==0,那么是点播。
|
|
|
|
|
flushLastSegment(_seg_number == 0);
|
2019-10-12 10:29:40 +08:00
|
|
|
|
//新增切片
|
|
|
|
|
_last_file_name = onOpenSegment(_file_index++);
|
|
|
|
|
//重置切片计时器
|
|
|
|
|
_ticker.resetTime();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void HlsMaker::flushLastSegment(bool eof){
|
|
|
|
|
if(_last_file_name.empty()){
|
|
|
|
|
//不存在上个切片
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
//文件创建到最后一次数据写入的时间即为切片长度
|
|
|
|
|
auto seg_dur = _ticker.elapsedTime() - _ticker_last_data.elapsedTime();
|
|
|
|
|
if(seg_dur <= 0){
|
|
|
|
|
seg_dur = 100;
|
2019-04-01 12:57:33 +08:00
|
|
|
|
}
|
2019-10-12 10:29:40 +08:00
|
|
|
|
_seg_dur_list.push_back(std::make_tuple(seg_dur, _last_file_name));
|
|
|
|
|
delOldSegment();
|
|
|
|
|
makeIndexFile(eof);
|
|
|
|
|
_last_file_name.clear();
|
2019-04-01 12:57:33 +08:00
|
|
|
|
}
|
2017-04-01 16:35:56 +08:00
|
|
|
|
|
2019-04-01 12:57:33 +08:00
|
|
|
|
}//namespace mediakit
|