2017-10-09 22:11:01 +08:00
|
|
|
|
/*
|
2017-09-27 16:20:30 +08:00
|
|
|
|
* MIT License
|
2017-04-01 16:35:56 +08:00
|
|
|
|
*
|
2017-09-27 16:20:30 +08:00
|
|
|
|
* Copyright (c) 2016 xiongziliang <771730766@qq.com>
|
|
|
|
|
*
|
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
|
2017-04-25 11:35:41 +08:00
|
|
|
|
#include "HLSMaker.h"
|
2017-08-09 18:39:30 +08:00
|
|
|
|
#include "Util/File.h"
|
|
|
|
|
#include "Util/uv_errno.h"
|
2018-10-24 17:17:55 +08:00
|
|
|
|
using namespace toolkit;
|
2017-08-09 18:39:30 +08:00
|
|
|
|
|
2018-10-24 17:17:55 +08:00
|
|
|
|
namespace mediakit {
|
2017-04-01 16:35:56 +08:00
|
|
|
|
|
2018-03-27 12:42:06 +08:00
|
|
|
|
HLSMaker::HLSMaker(const string& strM3u8File,
|
|
|
|
|
uint32_t ui32BufSize,
|
|
|
|
|
uint32_t ui32Duration,
|
|
|
|
|
uint32_t ui32Num) {
|
2017-04-01 16:35:56 +08:00
|
|
|
|
if (ui32BufSize < 16 * 1024) {
|
|
|
|
|
ui32BufSize = 16 * 1024;
|
|
|
|
|
}
|
2017-11-09 18:14:16 +08:00
|
|
|
|
if(ui32Num < 1){
|
|
|
|
|
ui32Num = 1;
|
|
|
|
|
}
|
2018-10-24 15:43:52 +08:00
|
|
|
|
_ui32BufSize = ui32BufSize;
|
|
|
|
|
_ui64TsCnt = 0;
|
|
|
|
|
_strM3u8File = strM3u8File;
|
|
|
|
|
_ui32NumSegments = ui32Num;
|
|
|
|
|
_ui32SegmentDuration = ui32Duration;
|
|
|
|
|
_ui32LastStamp = 0;
|
|
|
|
|
|
|
|
|
|
_strOutputPrefix = strM3u8File.substr(0, strM3u8File.rfind('.'));
|
|
|
|
|
_strFileName = _strOutputPrefix.substr(_strOutputPrefix.rfind('/') + 1);
|
|
|
|
|
_ts.init(_strOutputPrefix + "-0.ts", _ui32BufSize);
|
2017-04-01 16:35:56 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
HLSMaker::~HLSMaker() {
|
2018-10-24 15:43:52 +08:00
|
|
|
|
_ts.clear();
|
|
|
|
|
string strDir = _strOutputPrefix.substr(0,_strOutputPrefix.rfind('/'));
|
2017-04-01 16:35:56 +08:00
|
|
|
|
File::delete_file(strDir.data());
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-09 18:14:16 +08:00
|
|
|
|
bool HLSMaker::write_index_file(int iFirstSegment, unsigned int uiLastSegment, int iEnd) {
|
|
|
|
|
char acWriteBuf[1024];
|
2018-10-24 15:43:52 +08:00
|
|
|
|
std::shared_ptr<FILE> pM3u8File(File::createfile_file(_strM3u8File.data(), "w"),[](FILE *fp){
|
2018-03-28 16:06:51 +08:00
|
|
|
|
if(fp){
|
|
|
|
|
fflush(fp);
|
|
|
|
|
fclose(fp);
|
|
|
|
|
}
|
2017-11-09 18:14:16 +08:00
|
|
|
|
});
|
|
|
|
|
if (!pM3u8File) {
|
2018-10-24 15:43:52 +08:00
|
|
|
|
WarnL << "Could not open temporary m3u8 index file (" << _strM3u8File << "), no index file will be created";
|
2017-11-09 18:14:16 +08:00
|
|
|
|
return false;
|
2017-04-01 16:35:56 +08:00
|
|
|
|
}
|
2017-11-09 18:14:16 +08:00
|
|
|
|
if (iFirstSegment < 0) {
|
|
|
|
|
iFirstSegment = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//最少1秒
|
2018-03-28 16:06:51 +08:00
|
|
|
|
int maxSegmentDuration = 0;
|
2018-10-24 15:43:52 +08:00
|
|
|
|
for (auto dur : _iDurations) {
|
2017-11-09 18:14:16 +08:00
|
|
|
|
dur /=1000;
|
|
|
|
|
if(dur > maxSegmentDuration){
|
|
|
|
|
maxSegmentDuration = dur;
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-10-24 15:43:52 +08:00
|
|
|
|
if (_ui32NumSegments) {
|
2017-11-09 18:14:16 +08:00
|
|
|
|
snprintf(acWriteBuf,
|
|
|
|
|
sizeof(acWriteBuf),
|
2018-09-03 10:28:41 +08:00
|
|
|
|
"#EXTM3U\r\n"
|
|
|
|
|
"#EXT-X-VERSION:3\r\n"
|
|
|
|
|
"#EXT-X-ALLOW-CACHE:NO\r\n"
|
|
|
|
|
"#EXT-X-TARGETDURATION:%u\r\n"
|
|
|
|
|
"#EXT-X-MEDIA-SEQUENCE:%u\r\n",
|
2018-03-28 16:06:51 +08:00
|
|
|
|
maxSegmentDuration + 1,
|
2017-11-09 18:14:16 +08:00
|
|
|
|
iFirstSegment);
|
2017-04-01 16:35:56 +08:00
|
|
|
|
} else {
|
2017-11-09 18:14:16 +08:00
|
|
|
|
snprintf(acWriteBuf,
|
|
|
|
|
sizeof(acWriteBuf),
|
2018-09-03 10:28:41 +08:00
|
|
|
|
"#EXTM3U\r\n"
|
|
|
|
|
"#EXT-X-VERSION:3\r\n"
|
|
|
|
|
"#EXT-X-ALLOW-CACHE:NO\r\n"
|
|
|
|
|
"#EXT-X-TARGETDURATION:%u\r\n",
|
2017-11-09 18:14:16 +08:00
|
|
|
|
maxSegmentDuration);
|
2017-04-01 16:35:56 +08:00
|
|
|
|
}
|
2017-11-09 18:14:16 +08:00
|
|
|
|
if (fwrite(acWriteBuf, strlen(acWriteBuf), 1, pM3u8File.get()) != 1) {
|
2017-04-01 16:35:56 +08:00
|
|
|
|
WarnL << "Could not write to m3u8 index file, will not continue writing to index file";
|
2017-11-09 18:14:16 +08:00
|
|
|
|
return false;
|
2017-04-01 16:35:56 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (unsigned int i = iFirstSegment; i < uiLastSegment; i++) {
|
2017-11-09 18:14:16 +08:00
|
|
|
|
snprintf(acWriteBuf,
|
|
|
|
|
sizeof(acWriteBuf),
|
2018-09-03 10:28:41 +08:00
|
|
|
|
"#EXTINF:%.3f,\r\n%s-%u.ts\r\n",
|
2018-10-24 15:43:52 +08:00
|
|
|
|
_iDurations[i-iFirstSegment]/1000.0,
|
|
|
|
|
_strFileName.c_str(),
|
2017-11-09 18:14:16 +08:00
|
|
|
|
i);
|
|
|
|
|
if (fwrite(acWriteBuf, strlen(acWriteBuf), 1, pM3u8File.get()) != 1) {
|
2017-04-01 16:35:56 +08:00
|
|
|
|
WarnL << "Could not write to m3u8 index file, will not continue writing to index file";
|
2017-11-09 18:14:16 +08:00
|
|
|
|
return false;
|
2017-04-01 16:35:56 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (iEnd) {
|
2018-09-03 10:28:41 +08:00
|
|
|
|
snprintf(acWriteBuf, sizeof(acWriteBuf), "#EXT-X-ENDLIST\r\n");
|
2017-11-09 18:14:16 +08:00
|
|
|
|
if (fwrite(acWriteBuf, strlen(acWriteBuf), 1, pM3u8File.get()) != 1) {
|
2017-04-01 16:35:56 +08:00
|
|
|
|
WarnL << "Could not write last file and endlist tag to m3u8 index file";
|
2017-11-09 18:14:16 +08:00
|
|
|
|
return false;
|
2017-04-01 16:35:56 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2017-11-09 18:14:16 +08:00
|
|
|
|
return true;
|
2017-04-01 16:35:56 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-10-28 00:15:27 +08:00
|
|
|
|
void HLSMaker::inputH264(void *data, uint32_t length, uint32_t timeStamp) {
|
2018-10-24 15:43:52 +08:00
|
|
|
|
if(_ui32LastStamp == 0){
|
|
|
|
|
_ui32LastStamp = timeStamp;
|
2018-03-28 15:56:47 +08:00
|
|
|
|
}
|
2018-10-24 15:43:52 +08:00
|
|
|
|
int stampInc = timeStamp - _ui32LastStamp;
|
2018-10-28 00:15:27 +08:00
|
|
|
|
auto type = ((uint8_t*)data)[4] & 0x1F;
|
2017-04-01 16:35:56 +08:00
|
|
|
|
switch (type) {
|
|
|
|
|
case 7: //SPS
|
2018-10-24 15:43:52 +08:00
|
|
|
|
if (stampInc >= _ui32SegmentDuration * 1000) {
|
|
|
|
|
_ui32LastStamp = timeStamp;
|
2017-11-09 18:14:16 +08:00
|
|
|
|
//关闭文件
|
2018-10-24 15:43:52 +08:00
|
|
|
|
_ts.clear();
|
|
|
|
|
auto strTmpFileName = StrPrinter << _strOutputPrefix << '-' << (++_ui64TsCnt) << ".ts" << endl;
|
|
|
|
|
if (!_ts.init(strTmpFileName, _ui32BufSize)) {
|
2017-04-01 16:35:56 +08:00
|
|
|
|
//创建文件失败
|
|
|
|
|
return;
|
|
|
|
|
}
|
2017-11-09 18:14:16 +08:00
|
|
|
|
//记录切片时间
|
2018-10-24 15:43:52 +08:00
|
|
|
|
_iDurations.push_back(stampInc);
|
2017-11-09 18:14:16 +08:00
|
|
|
|
if(removets()){
|
|
|
|
|
//删除老的时间戳
|
2018-10-24 15:43:52 +08:00
|
|
|
|
_iDurations.pop_front();
|
2017-11-09 18:14:16 +08:00
|
|
|
|
}
|
2018-10-24 15:43:52 +08:00
|
|
|
|
write_index_file(_ui64TsCnt - _ui32NumSegments, _ui64TsCnt, 0);
|
2017-04-01 16:35:56 +08:00
|
|
|
|
}
|
|
|
|
|
case 1: //P
|
|
|
|
|
//insert aud frame before p and SPS frame
|
2018-10-24 15:43:52 +08:00
|
|
|
|
_ts.inputH264("\x0\x0\x0\x1\x9\xf0", 6, timeStamp * 90);
|
2017-04-01 16:35:56 +08:00
|
|
|
|
case 5: //IDR
|
|
|
|
|
case 8: //PPS
|
2018-10-24 15:43:52 +08:00
|
|
|
|
_ts.inputH264((char *) data, length, timeStamp * 90);
|
2017-04-01 16:35:56 +08:00
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void HLSMaker::inputAAC(void *data, uint32_t length, uint32_t timeStamp) {
|
2018-10-24 15:43:52 +08:00
|
|
|
|
_ts.inputAAC((char *) data, length, timeStamp * 90);
|
2017-04-01 16:35:56 +08:00
|
|
|
|
}
|
|
|
|
|
|
2017-11-09 18:14:16 +08:00
|
|
|
|
bool HLSMaker::removets() {
|
2018-10-24 15:43:52 +08:00
|
|
|
|
if (_ui64TsCnt < _ui32NumSegments + 2) {
|
2017-11-09 18:14:16 +08:00
|
|
|
|
return false;
|
2017-04-01 16:35:56 +08:00
|
|
|
|
}
|
2018-10-24 15:43:52 +08:00
|
|
|
|
File::delete_file((StrPrinter << _strOutputPrefix << "-"
|
|
|
|
|
<< _ui64TsCnt - _ui32NumSegments - 2
|
2017-11-09 18:14:16 +08:00
|
|
|
|
<< ".ts" << endl).data());
|
|
|
|
|
return true;
|
2017-04-01 16:35:56 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-10-28 00:15:27 +08:00
|
|
|
|
void HLSMaker::onTrackFrame(const Frame::Ptr &frame) {
|
|
|
|
|
switch (frame->getCodecId()){
|
|
|
|
|
case CodecH264:{
|
|
|
|
|
if( frame->prefixSize() == 4){
|
|
|
|
|
inputH264(frame->data(), frame->size(),frame->stamp());
|
|
|
|
|
}else{
|
|
|
|
|
WarnL << "h264必须要有头4个字节的前缀";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case CodecAAC:{
|
|
|
|
|
if( frame->prefixSize() == 7) {
|
|
|
|
|
inputAAC(frame->data(), frame->size(), frame->stamp());
|
|
|
|
|
}else{
|
|
|
|
|
WarnL << "adts必须要有头7个字节的adts头";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-24 17:17:55 +08:00
|
|
|
|
} /* namespace mediakit */
|
2017-04-01 16:35:56 +08:00
|
|
|
|
|