From 730db98ad867cb74aa43692752d0a3a3918166cf Mon Sep 17 00:00:00 2001 From: xiongziliang <771730766@qq.com> Date: Wed, 28 Mar 2018 15:56:47 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96hls=E5=8D=A1=E9=A1=BF?= =?UTF-8?q?=E7=8E=B0=E8=B1=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/MediaFile/HLSMaker.cpp | 21 +++++++++++++-------- src/MediaFile/HLSMaker.h | 6 +++--- src/MediaFile/MediaRecorder.cpp | 4 ++-- 3 files changed, 18 insertions(+), 13 deletions(-) diff --git a/src/MediaFile/HLSMaker.cpp b/src/MediaFile/HLSMaker.cpp index ac4b1fa3..121ee64a 100644 --- a/src/MediaFile/HLSMaker.cpp +++ b/src/MediaFile/HLSMaker.cpp @@ -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) { + if(m_ui32LastStamp == 0){ + m_ui32LastStamp = timeStamp; + } + int stampInc = timeStamp - m_ui32LastStamp; + switch (type) { case 7: //SPS - if (m_Timer.elapsedTime() >= m_ui32SegmentDuration * 1000) { + if (stampInc >= m_ui32SegmentDuration * 1000) { + m_ui32LastStamp = timeStamp; //关闭文件 m_ts.clear(); 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; } //记录切片时间 - m_iDurations.push_back(m_Timer.elapsedTime()); - m_Timer.resetTime(); + m_iDurations.push_back(stampInc); if(removets()){ //删除老的时间戳 m_iDurations.pop_front(); @@ -149,10 +154,10 @@ void HLSMaker::inputH264(void *data, uint32_t length, uint32_t timeStamp, int ty } case 1: //P //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 8: //PPS - m_ts.inputH264((char *) data, length, timeStamp); + m_ts.inputH264((char *) data, length, timeStamp * 90); break; default: 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) { - m_ts.inputAAC((char *) data, length, timeStamp); + m_ts.inputAAC((char *) data, length, timeStamp * 90); } bool HLSMaker::removets() { - if (m_ui64TsCnt <= m_ui32NumSegments) { + if (m_ui64TsCnt < m_ui32NumSegments + 2) { return false; } File::delete_file((StrPrinter << m_strOutputPrefix << "-" - << m_ui64TsCnt - m_ui32NumSegments - 1 + << m_ui64TsCnt - m_ui32NumSegments - 2 << ".ts" << endl).data()); return true; } diff --git a/src/MediaFile/HLSMaker.h b/src/MediaFile/HLSMaker.h index ff32920f..04999832 100644 --- a/src/MediaFile/HLSMaker.h +++ b/src/MediaFile/HLSMaker.h @@ -49,13 +49,13 @@ public: virtual ~HLSMaker(); - //时间戳:参考频率90000 + //时间戳:参考频率1000 void inputH264( void *pData, uint32_t ui32Length, uint32_t ui32TimeStamp, int iType); - //时间戳:参考频率90000 + //时间戳:参考频率1000 void inputAAC( void *pData, uint32_t ui32Length, uint32_t ui32TimeStamp); @@ -68,7 +68,7 @@ private: uint32_t m_ui32NumSegments; uint64_t m_ui64TsCnt; uint32_t m_ui32BufSize; - Ticker m_Timer; + uint32_t m_ui32LastStamp; std::deque m_iDurations; bool write_index_file(int iFirstSegment, unsigned int uiLastSegment, int iEnd); diff --git a/src/MediaFile/MediaRecorder.cpp b/src/MediaFile/MediaRecorder.cpp index 5614b921..8f9530da 100644 --- a/src/MediaFile/MediaRecorder.cpp +++ b/src/MediaFile/MediaRecorder.cpp @@ -76,7 +76,7 @@ MediaRecorder::~MediaRecorder() { void MediaRecorder::inputH264(void* pData, uint32_t ui32Length, uint32_t ui32TimeStamp, int iType) { if(m_hlsMaker){ - m_hlsMaker->inputH264(pData, ui32Length, ui32TimeStamp * 90, iType); + m_hlsMaker->inputH264(pData, ui32Length, ui32TimeStamp, iType); } #ifdef ENABLE_MP4V2 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) { if(m_hlsMaker){ - m_hlsMaker->inputAAC(pData, ui32Length, ui32TimeStamp * 90); + m_hlsMaker->inputAAC(pData, ui32Length, ui32TimeStamp); } #ifdef ENABLE_MP4V2 if(m_mp4Maker){