From 1e0db964574ccfba878b84eeff29e71bac1da3a9 Mon Sep 17 00:00:00 2001 From: xiongziliang <771730766@qq.com> Date: Thu, 25 Oct 2018 14:23:02 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84Rtmp=E5=A4=8D=E7=94=A8?= =?UTF-8?q?=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/RtmpMuxer/H264RtmpCodec.cpp | 62 +++++++++++++++++++-------------- src/RtmpMuxer/H264RtmpCodec.h | 1 + 2 files changed, 36 insertions(+), 27 deletions(-) diff --git a/src/RtmpMuxer/H264RtmpCodec.cpp b/src/RtmpMuxer/H264RtmpCodec.cpp index 06ceee49..f1f59df2 100644 --- a/src/RtmpMuxer/H264RtmpCodec.cpp +++ b/src/RtmpMuxer/H264RtmpCodec.cpp @@ -116,42 +116,48 @@ void H264RtmpEncoder::inputFrame(const Frame::Ptr &frame) { auto iLen = frame->size() - frame->prefixSize(); auto type = ((uint8_t*)pcData)[0] & 0x1F; - //尝试从frame中获取sps pps - switch (type){ - case 7:{ - //sps - if(_sps.empty()){ - _sps = string(pcData,iLen); - if(!_pps.empty()){ - makeVideoConfigPkt(); + if(!_gotSpsPps){ + //尝试从frame中获取sps pps + switch (type){ + case 7:{ + //sps + if(_sps.empty()){ + _sps = string(pcData,iLen); + if(!_pps.empty()){ + makeVideoConfigPkt(); + } } } - } - break; - case 8:{ - //pps - if(_pps.empty()){ - _pps = string(pcData,iLen); - if(!_sps.empty()){ - makeVideoConfigPkt(); + break; + case 8:{ + //pps + if(_pps.empty()){ + _pps = string(pcData,iLen); + if(!_sps.empty()){ + makeVideoConfigPkt(); + } } } + break; + default: + break; } - break; - default: - break; - } - //尝试从track中获取sps pps信息 - if((!_sps.empty() || !_pps.empty()) && _track && _track->ready()){ - _sps = _track->getSps(); - _pps = _track->getPps(); - makeVideoConfigPkt(); + //尝试从track中获取sps pps信息 + if((!_sps.empty() || !_pps.empty()) && _track && _track->ready()){ + _sps = _track->getSps(); + _pps = _track->getPps(); + makeVideoConfigPkt(); + } } switch (type){ - case 1: - case 5:{ + case 5: + //在IDR帧之前插入config包 + if(_gotSpsPps){ + makeVideoConfigPkt(); + } + case 1:{ //I or P or B frame int8_t flags = 7; //h.264 bool is_config = false; @@ -184,6 +190,8 @@ void H264RtmpEncoder::inputFrame(const Frame::Ptr &frame) { void H264RtmpEncoder::makeVideoConfigPkt() { + _gotSpsPps = true; + int8_t flags = 7; //h.264 flags |= (FLV_KEY_FRAME << 4); bool is_config = true; diff --git a/src/RtmpMuxer/H264RtmpCodec.h b/src/RtmpMuxer/H264RtmpCodec.h index b6c463d8..6648574a 100644 --- a/src/RtmpMuxer/H264RtmpCodec.h +++ b/src/RtmpMuxer/H264RtmpCodec.h @@ -93,6 +93,7 @@ private: void makeVideoConfigPkt(); private: H264Track::Ptr _track; + bool _gotSpsPps = false; }; }//namespace mediakit