优化hls卡顿现象

This commit is contained in:
xiongziliang 2018-03-28 15:56:47 +08:00
parent f2e82fc2cc
commit 730db98ad8
3 changed files with 18 additions and 13 deletions

View File

@ -128,9 +128,15 @@ bool HLSMaker::write_index_file(int iFirstSegment, unsigned int uiLastSegment, i
} }
void HLSMaker::inputH264(void *data, uint32_t length, uint32_t timeStamp, int type) { void HLSMaker::inputH264(void *data, uint32_t length, uint32_t timeStamp, int type) {
if(m_ui32LastStamp == 0){
m_ui32LastStamp = timeStamp;
}
int stampInc = timeStamp - m_ui32LastStamp;
switch (type) { switch (type) {
case 7: //SPS case 7: //SPS
if (m_Timer.elapsedTime() >= m_ui32SegmentDuration * 1000) { if (stampInc >= m_ui32SegmentDuration * 1000) {
m_ui32LastStamp = timeStamp;
//关闭文件 //关闭文件
m_ts.clear(); m_ts.clear();
auto strTmpFileName = StrPrinter << m_strOutputPrefix << '-' << (++m_ui64TsCnt) << ".ts" << endl; auto strTmpFileName = StrPrinter << m_strOutputPrefix << '-' << (++m_ui64TsCnt) << ".ts" << endl;
@ -139,8 +145,7 @@ void HLSMaker::inputH264(void *data, uint32_t length, uint32_t timeStamp, int ty
return; return;
} }
//记录切片时间 //记录切片时间
m_iDurations.push_back(m_Timer.elapsedTime()); m_iDurations.push_back(stampInc);
m_Timer.resetTime();
if(removets()){ if(removets()){
//删除老的时间戳 //删除老的时间戳
m_iDurations.pop_front(); m_iDurations.pop_front();
@ -149,10 +154,10 @@ void HLSMaker::inputH264(void *data, uint32_t length, uint32_t timeStamp, int ty
} }
case 1: //P case 1: //P
//insert aud frame before p and SPS frame //insert aud frame before p and SPS frame
m_ts.inputH264("\x0\x0\x0\x1\x9\xf0", 6, timeStamp); m_ts.inputH264("\x0\x0\x0\x1\x9\xf0", 6, timeStamp * 90);
case 5: //IDR case 5: //IDR
case 8: //PPS case 8: //PPS
m_ts.inputH264((char *) data, length, timeStamp); m_ts.inputH264((char *) data, length, timeStamp * 90);
break; break;
default: default:
break; break;
@ -160,15 +165,15 @@ void HLSMaker::inputH264(void *data, uint32_t length, uint32_t timeStamp, int ty
} }
void HLSMaker::inputAAC(void *data, uint32_t length, uint32_t timeStamp) { void HLSMaker::inputAAC(void *data, uint32_t length, uint32_t timeStamp) {
m_ts.inputAAC((char *) data, length, timeStamp); m_ts.inputAAC((char *) data, length, timeStamp * 90);
} }
bool HLSMaker::removets() { bool HLSMaker::removets() {
if (m_ui64TsCnt <= m_ui32NumSegments) { if (m_ui64TsCnt < m_ui32NumSegments + 2) {
return false; return false;
} }
File::delete_file((StrPrinter << m_strOutputPrefix << "-" File::delete_file((StrPrinter << m_strOutputPrefix << "-"
<< m_ui64TsCnt - m_ui32NumSegments - 1 << m_ui64TsCnt - m_ui32NumSegments - 2
<< ".ts" << endl).data()); << ".ts" << endl).data());
return true; return true;
} }

View File

@ -49,13 +49,13 @@ public:
virtual ~HLSMaker(); virtual ~HLSMaker();
//时间戳:参考频率90000 //时间戳:参考频率1000
void inputH264( void *pData, void inputH264( void *pData,
uint32_t ui32Length, uint32_t ui32Length,
uint32_t ui32TimeStamp, uint32_t ui32TimeStamp,
int iType); int iType);
//时间戳:参考频率90000 //时间戳:参考频率1000
void inputAAC( void *pData, void inputAAC( void *pData,
uint32_t ui32Length, uint32_t ui32Length,
uint32_t ui32TimeStamp); uint32_t ui32TimeStamp);
@ -68,7 +68,7 @@ private:
uint32_t m_ui32NumSegments; uint32_t m_ui32NumSegments;
uint64_t m_ui64TsCnt; uint64_t m_ui64TsCnt;
uint32_t m_ui32BufSize; uint32_t m_ui32BufSize;
Ticker m_Timer; uint32_t m_ui32LastStamp;
std::deque<int> m_iDurations; std::deque<int> m_iDurations;
bool write_index_file(int iFirstSegment, unsigned int uiLastSegment, int iEnd); bool write_index_file(int iFirstSegment, unsigned int uiLastSegment, int iEnd);

View File

@ -76,7 +76,7 @@ MediaRecorder::~MediaRecorder() {
void MediaRecorder::inputH264(void* pData, uint32_t ui32Length, uint32_t ui32TimeStamp, int iType) { void MediaRecorder::inputH264(void* pData, uint32_t ui32Length, uint32_t ui32TimeStamp, int iType) {
if(m_hlsMaker){ if(m_hlsMaker){
m_hlsMaker->inputH264(pData, ui32Length, ui32TimeStamp * 90, iType); m_hlsMaker->inputH264(pData, ui32Length, ui32TimeStamp, iType);
} }
#ifdef ENABLE_MP4V2 #ifdef ENABLE_MP4V2
if(m_mp4Maker){ if(m_mp4Maker){
@ -87,7 +87,7 @@ void MediaRecorder::inputH264(void* pData, uint32_t ui32Length, uint32_t ui32Tim
void MediaRecorder::inputAAC(void* pData, uint32_t ui32Length, uint32_t ui32TimeStamp) { void MediaRecorder::inputAAC(void* pData, uint32_t ui32Length, uint32_t ui32TimeStamp) {
if(m_hlsMaker){ if(m_hlsMaker){
m_hlsMaker->inputAAC(pData, ui32Length, ui32TimeStamp * 90); m_hlsMaker->inputAAC(pData, ui32Length, ui32TimeStamp);
} }
#ifdef ENABLE_MP4V2 #ifdef ENABLE_MP4V2
if(m_mp4Maker){ if(m_mp4Maker){