From 2610ba7c8bbe86532035ff625e1daef69d4e5301 Mon Sep 17 00:00:00 2001 From: xiongziliang <771730766@qq.com> Date: Wed, 15 Jan 2020 11:46:15 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8Drtmp=E6=B3=A8=E5=86=8C?= =?UTF-8?q?=E6=97=B6=E8=BF=98=E6=9C=AA=E7=94=9F=E6=88=90=E5=AE=8C=E6=95=B4?= =?UTF-8?q?config=E5=8C=85=E7=9A=84bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Extension/AACRtmp.cpp | 21 ++++++++++----- src/Extension/AACRtmp.h | 4 +++ src/Extension/H264Rtmp.cpp | 46 ++++++++++++++++----------------- src/Extension/H264Rtmp.h | 5 ++++ src/Rtmp/RtmpCodec.h | 1 + src/Rtmp/RtmpMediaSourceMuxer.h | 1 + src/Rtmp/RtmpMuxer.cpp | 8 ++++++ src/Rtmp/RtmpMuxer.h | 5 ++++ 8 files changed, 61 insertions(+), 30 deletions(-) diff --git a/src/Extension/AACRtmp.cpp b/src/Extension/AACRtmp.cpp index c3efddb1..6a657aab 100644 --- a/src/Extension/AACRtmp.cpp +++ b/src/Extension/AACRtmp.cpp @@ -76,17 +76,24 @@ AACRtmpEncoder::AACRtmpEncoder(const Track::Ptr &track) { _track = dynamic_pointer_cast(track); } +void AACRtmpEncoder::makeConfigPacket() { + if (_track && _track->ready()) { + //从track中和获取aac配置信息 + _aac_cfg = _track->getAacCfg(); + } + + if (!_aac_cfg.empty()) { + makeAudioConfigPkt(); + } +} + void AACRtmpEncoder::inputFrame(const Frame::Ptr &frame) { - if(_aac_cfg.empty()){ - if(frame->prefixSize() >= 7){ + if (_aac_cfg.empty()) { + if (frame->prefixSize() >= 7) { //包含adts头,从adts头获取aac配置信息 _aac_cfg = makeAdtsConfig(reinterpret_cast(frame->data())); - makeAudioConfigPkt(); - } else if(_track && _track->ready()){ - //从track中和获取aac配置信息 - _aac_cfg = _track->getAacCfg(); - makeAudioConfigPkt(); } + makeConfigPacket(); } if(!_aac_cfg.empty()){ diff --git a/src/Extension/AACRtmp.h b/src/Extension/AACRtmp.h index 661a37a1..8181ebfd 100644 --- a/src/Extension/AACRtmp.h +++ b/src/Extension/AACRtmp.h @@ -88,6 +88,10 @@ public: */ void inputFrame(const Frame::Ptr &frame) override; + /** + * 生成config包 + */ + void makeConfigPacket() override; private: void makeAudioConfigPkt(); private: diff --git a/src/Extension/H264Rtmp.cpp b/src/Extension/H264Rtmp.cpp index c995317f..2aa4a7cb 100644 --- a/src/Extension/H264Rtmp.cpp +++ b/src/Extension/H264Rtmp.cpp @@ -99,7 +99,20 @@ inline void H264RtmpDecoder::onGetH264(const char* pcData, int iLen, uint32_t dt H264RtmpEncoder::H264RtmpEncoder(const Track::Ptr &track) { _track = dynamic_pointer_cast(track); +} +void H264RtmpEncoder::makeConfigPacket(){ + if (_track && _track->ready()) { + //尝试从track中获取sps pps信息 + _sps = _track->getSps(); + _pps = _track->getPps(); + } + + if (!_sps.empty() && !_pps.empty()) { + //获取到sps/pps + makeVideoConfigPkt(); + _gotSpsPps = true; + } } void H264RtmpEncoder::inputFrame(const Frame::Ptr &frame) { @@ -107,37 +120,24 @@ void H264RtmpEncoder::inputFrame(const Frame::Ptr &frame) { auto iLen = frame->size() - frame->prefixSize(); auto type = H264_TYPE(((uint8_t*)pcData)[0]); - if(!_gotSpsPps){ + if (!_gotSpsPps) { //尝试从frame中获取sps pps - switch (type){ - case H264Frame::NAL_SPS:{ + switch (type) { + case H264Frame::NAL_SPS: { //sps - if(_sps.empty()){ - _sps = string(pcData,iLen); - } - } + _sps = string(pcData, iLen); + makeConfigPacket(); break; - case H264Frame::NAL_PPS:{ + } + case H264Frame::NAL_PPS: { //pps - if(_pps.empty()){ - _pps = string(pcData,iLen); - } - } + _pps = string(pcData, iLen); + makeConfigPacket(); break; + } default: break; } - - if(_track && _track->ready()){ - //尝试从track中获取sps pps信息 - _sps = _track->getSps(); - _pps = _track->getPps(); - } - - if(!_sps.empty() && !_pps.empty()){ - _gotSpsPps = true; - makeVideoConfigPkt(); - } } if(type == H264Frame::NAL_SEI){ diff --git a/src/Extension/H264Rtmp.h b/src/Extension/H264Rtmp.h index 68802d68..a1cae242 100644 --- a/src/Extension/H264Rtmp.h +++ b/src/Extension/H264Rtmp.h @@ -90,6 +90,11 @@ public: * @param frame 帧数据 */ void inputFrame(const Frame::Ptr &frame) override; + + /** + * 生成config包 + */ + void makeConfigPacket() override; private: void makeVideoConfigPkt(); private: diff --git a/src/Rtmp/RtmpCodec.h b/src/Rtmp/RtmpCodec.h index ccdb967a..25f2fc5a 100644 --- a/src/Rtmp/RtmpCodec.h +++ b/src/Rtmp/RtmpCodec.h @@ -80,6 +80,7 @@ public: typedef std::shared_ptr Ptr; RtmpCodec(){} virtual ~RtmpCodec(){} + virtual void makeConfigPacket() {}; }; diff --git a/src/Rtmp/RtmpMediaSourceMuxer.h b/src/Rtmp/RtmpMediaSourceMuxer.h index 2cecd127..dce7e70e 100644 --- a/src/Rtmp/RtmpMediaSourceMuxer.h +++ b/src/Rtmp/RtmpMediaSourceMuxer.h @@ -58,6 +58,7 @@ public: } void onAllTrackReady(){ + makeConfigPacket(); _mediaSouce->setMetaData(getMetadata()); } diff --git a/src/Rtmp/RtmpMuxer.cpp b/src/Rtmp/RtmpMuxer.cpp index d81a74fa..89d345ca 100644 --- a/src/Rtmp/RtmpMuxer.cpp +++ b/src/Rtmp/RtmpMuxer.cpp @@ -78,6 +78,14 @@ void RtmpMuxer::inputFrame(const Frame::Ptr &frame) { } } +void RtmpMuxer::makeConfigPacket(){ + for(auto &encoder : _encoder){ + if(encoder){ + encoder->makeConfigPacket(); + } + } +} + const AMFValue &RtmpMuxer::getMetadata() const { return _metadata; } diff --git a/src/Rtmp/RtmpMuxer.h b/src/Rtmp/RtmpMuxer.h index c1a007fa..cc09178e 100644 --- a/src/Rtmp/RtmpMuxer.h +++ b/src/Rtmp/RtmpMuxer.h @@ -71,6 +71,11 @@ public: * 重置所有track */ void resetTracks() override ; + + /** + * 生成config包 + */ + void makeConfigPacket(); private: RtmpRing::RingType::Ptr _rtmpRing; AMFValue _metadata;