ZLMediaKit/src/MediaFile/HLSMaker.cpp

177 lines
5.6 KiB
C++
Raw Normal View History

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 {
HLSMaker::HLSMaker(const string& strM3u8File, const string& strHttpUrl,
uint32_t ui32BufSize, uint32_t ui32Duration, uint32_t ui32Num) {
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_strHttpUrl = strHttpUrl.substr(0, strHttpUrl.find_last_of('/') + 1);
m_ui32NumSegments = ui32Num;
m_ui32SegmentDuration = ui32Duration;
m_strOutputPrefix = strM3u8File.substr(0, strM3u8File.find_last_of('.'));
m_strFileName = m_strOutputPrefix.substr(m_strOutputPrefix.find_last_of('/') + 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();
string strDir = m_strOutputPrefix.substr(0,m_strOutputPrefix.find_last_of('/'));
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){
fclose(fp);
});
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秒
int maxSegmentDuration = 1;
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"
"#EXT-X-TARGETDURATION:%u\n"
"#EXT-X-MEDIA-SEQUENCE:%u\n",
maxSegmentDuration,
iFirstSegment);
2017-04-01 16:35:56 +08:00
} else {
2017-11-09 18:14:16 +08:00
snprintf(acWriteBuf,
sizeof(acWriteBuf),
"#EXTM3U\n"
"#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),
"#EXTINF:%.3f,\n%s%s-%u.ts\n",
m_iDurations[i-iFirstSegment]/1000.0,
m_strHttpUrl.c_str(),
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) {
2017-04-01 16:35:56 +08:00
switch (type) {
case 7: //SPS
if (m_Timer.elapsedTime() >= m_ui32SegmentDuration * 1000) {
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
//记录切片时间
m_iDurations.push_back(m_Timer.elapsedTime());
m_Timer.resetTime();
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
m_ts.inputH264("\x0\x0\x0\x1\x9\xf0", 6, timeStamp);
case 5: //IDR
case 8: //PPS
m_ts.inputH264((char *) data, length, timeStamp);
break;
default:
break;
}
}
void HLSMaker::inputAAC(void *data, uint32_t length, uint32_t timeStamp) {
m_ts.inputAAC((char *) data, length, timeStamp);
}
2017-11-09 18:14:16 +08:00
bool HLSMaker::removets() {
2017-04-01 16:35:56 +08:00
if (m_ui64TsCnt <= m_ui32NumSegments) {
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 << "-"
<< m_ui64TsCnt - m_ui32NumSegments - 1
<< ".ts" << endl).data());
return true;
2017-04-01 16:35:56 +08:00
}
} /* namespace MediaFile */
} /* namespace ZL */