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"
|
|
|
|
|
|
|
|
|
|
using namespace ZL::Util;
|
2017-04-01 16:35:56 +08:00
|
|
|
|
|
|
|
|
|
namespace ZL {
|
|
|
|
|
namespace MediaFile {
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
m_ui32BufSize = ui32BufSize;
|
2017-04-01 16:35:56 +08:00
|
|
|
|
m_ui64TsCnt = 0;
|
|
|
|
|
m_strM3u8File = strM3u8File;
|
|
|
|
|
m_ui32NumSegments = ui32Num;
|
|
|
|
|
m_ui32SegmentDuration = ui32Duration;
|
2018-05-22 17:22:31 +08:00
|
|
|
|
m_ui32LastStamp = 0;
|
2017-04-01 16:35:56 +08:00
|
|
|
|
|
2018-05-18 18:48:17 +08:00
|
|
|
|
m_strOutputPrefix = strM3u8File.substr(0, strM3u8File.rfind('.'));
|
|
|
|
|
m_strFileName = m_strOutputPrefix.substr(m_strOutputPrefix.rfind('/') + 1);
|
2017-11-09 18:14:16 +08:00
|
|
|
|
m_ts.init(m_strOutputPrefix + "-0.ts", m_ui32BufSize);
|
2017-04-01 16:35:56 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
HLSMaker::~HLSMaker() {
|
|
|
|
|
m_ts.clear();
|
2018-05-18 18:48:17 +08:00
|
|
|
|
string strDir = m_strOutputPrefix.substr(0,m_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];
|
|
|
|
|
std::shared_ptr<FILE> pM3u8File(File::createfile_file(m_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) {
|
|
|
|
|
WarnL << "Could not open temporary m3u8 index file (" << m_strM3u8File << "), no index file will be created";
|
|
|
|
|
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;
|
2017-11-09 18:14:16 +08:00
|
|
|
|
for (auto dur : m_iDurations) {
|
|
|
|
|
dur /=1000;
|
|
|
|
|
if(dur > maxSegmentDuration){
|
|
|
|
|
maxSegmentDuration = dur;
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-04-01 16:35:56 +08:00
|
|
|
|
if (m_ui32NumSegments) {
|
2017-11-09 18:14:16 +08:00
|
|
|
|
snprintf(acWriteBuf,
|
|
|
|
|
sizeof(acWriteBuf),
|
|
|
|
|
"#EXTM3U\n"
|
2018-03-27 12:42:06 +08:00
|
|
|
|
"#EXT-X-VERSION:3\n"
|
2018-03-28 16:06:51 +08:00
|
|
|
|
"#EXT-X-ALLOW-CACHE:NO\n"
|
2017-11-09 18:14:16 +08:00
|
|
|
|
"#EXT-X-TARGETDURATION:%u\n"
|
|
|
|
|
"#EXT-X-MEDIA-SEQUENCE:%u\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),
|
|
|
|
|
"#EXTM3U\n"
|
2018-03-27 12:42:06 +08:00
|
|
|
|
"#EXT-X-VERSION:3\n"
|
2018-03-28 16:06:51 +08:00
|
|
|
|
"#EXT-X-ALLOW-CACHE:NO\n"
|
2017-11-09 18:14:16 +08:00
|
|
|
|
"#EXT-X-TARGETDURATION:%u\n",
|
|
|
|
|
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-03-27 12:42:06 +08:00
|
|
|
|
"#EXTINF:%.3f,\n%s-%u.ts\n",
|
2017-11-09 18:14:16 +08:00
|
|
|
|
m_iDurations[i-iFirstSegment]/1000.0,
|
|
|
|
|
m_strFileName.c_str(),
|
|
|
|
|
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) {
|
2017-11-09 18:14:16 +08:00
|
|
|
|
snprintf(acWriteBuf, sizeof(acWriteBuf), "#EXT-X-ENDLIST\n");
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
2017-11-09 18:14:16 +08:00
|
|
|
|
void HLSMaker::inputH264(void *data, uint32_t length, uint32_t timeStamp, int type) {
|
2018-03-28 15:56:47 +08:00
|
|
|
|
if(m_ui32LastStamp == 0){
|
|
|
|
|
m_ui32LastStamp = timeStamp;
|
|
|
|
|
}
|
|
|
|
|
int stampInc = timeStamp - m_ui32LastStamp;
|
|
|
|
|
|
2017-04-01 16:35:56 +08:00
|
|
|
|
switch (type) {
|
|
|
|
|
case 7: //SPS
|
2018-03-28 15:56:47 +08:00
|
|
|
|
if (stampInc >= m_ui32SegmentDuration * 1000) {
|
|
|
|
|
m_ui32LastStamp = timeStamp;
|
2017-11-09 18:14:16 +08:00
|
|
|
|
//关闭文件
|
2017-04-01 16:35:56 +08:00
|
|
|
|
m_ts.clear();
|
2017-11-09 18:14:16 +08:00
|
|
|
|
auto strTmpFileName = StrPrinter << m_strOutputPrefix << '-' << (++m_ui64TsCnt) << ".ts" << endl;
|
|
|
|
|
if (!m_ts.init(strTmpFileName, m_ui32BufSize)) {
|
2017-04-01 16:35:56 +08:00
|
|
|
|
//创建文件失败
|
|
|
|
|
return;
|
|
|
|
|
}
|
2017-11-09 18:14:16 +08:00
|
|
|
|
//记录切片时间
|
2018-03-28 15:56:47 +08:00
|
|
|
|
m_iDurations.push_back(stampInc);
|
2017-11-09 18:14:16 +08:00
|
|
|
|
if(removets()){
|
|
|
|
|
//删除老的时间戳
|
|
|
|
|
m_iDurations.pop_front();
|
|
|
|
|
}
|
|
|
|
|
write_index_file(m_ui64TsCnt - m_ui32NumSegments, m_ui64TsCnt, 0);
|
2017-04-01 16:35:56 +08:00
|
|
|
|
}
|
|
|
|
|
case 1: //P
|
|
|
|
|
//insert aud frame before p and SPS frame
|
2018-03-28 15:56:47 +08:00
|
|
|
|
m_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-03-28 15:56:47 +08:00
|
|
|
|
m_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-03-28 15:56:47 +08:00
|
|
|
|
m_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-03-28 15:56:47 +08:00
|
|
|
|
if (m_ui64TsCnt < m_ui32NumSegments + 2) {
|
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
|
|
|
|
File::delete_file((StrPrinter << m_strOutputPrefix << "-"
|
2018-03-28 15:56:47 +08:00
|
|
|
|
<< m_ui64TsCnt - m_ui32NumSegments - 2
|
2017-11-09 18:14:16 +08:00
|
|
|
|
<< ".ts" << endl).data());
|
|
|
|
|
return true;
|
2017-04-01 16:35:56 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} /* namespace MediaFile */
|
|
|
|
|
} /* namespace ZL */
|
|
|
|
|
|