diff --git a/src/Extension/AACRtmp.cpp b/src/Extension/AACRtmp.cpp index 353dd652..7012b73d 100644 --- a/src/Extension/AACRtmp.cpp +++ b/src/Extension/AACRtmp.cpp @@ -22,7 +22,7 @@ static string getAacCfg(const RtmpPacket &thiz) { return ret; } if (thiz.buffer.size() < 4) { - WarnL << "bad aac cfg!"; + WarnL << "get aac config failed, rtmp packet is: " << hexdump(thiz.data(), thiz.size()); return ret; } ret = thiz.buffer.substr(2); @@ -32,7 +32,9 @@ static string getAacCfg(const RtmpPacket &thiz) { void AACRtmpDecoder::inputRtmp(const RtmpPacket::Ptr &pkt) { if (pkt->isCfgFrame()) { _aac_cfg = getAacCfg(*pkt); - onGetAAC(nullptr, 0, 0); + if (!_aac_cfg.empty()) { + onGetAAC(nullptr, 0, 0); + } return; } diff --git a/src/Extension/H264Rtmp.cpp b/src/Extension/H264Rtmp.cpp index 44f5bd65..cb26a9ef 100644 --- a/src/Extension/H264Rtmp.cpp +++ b/src/Extension/H264Rtmp.cpp @@ -23,75 +23,46 @@ H264Frame::Ptr H264RtmpDecoder::obtainFrame() { } /** - * 返回不带0x00 00 00 01头的sps - * @return + * 返回不带0x00 00 00 01头的sps pps */ -static string getH264SPS(const RtmpPacket &thiz) { - string ret; +static bool getH264Config(const RtmpPacket &thiz, string &sps, string &pps) { if (thiz.getMediaType() != FLV_CODEC_H264) { - return ret; + return false; } if (!thiz.isCfgFrame()) { - return ret; + return false; } if (thiz.buffer.size() < 13) { - WarnL << "bad H264 cfg!"; - return ret; + return false; } - uint16_t sps_size ; - memcpy(&sps_size, thiz.buffer.data() + 11, 2); - sps_size = ntohs(sps_size); - if ((int) thiz.buffer.size() < 13 + sps_size) { - WarnL << "bad H264 cfg!"; - return ret; - } - ret.assign(thiz.buffer.data() + 13, sps_size); - return ret; -} - -/** - * 返回不带0x00 00 00 01头的pps - * @return - */ -static string getH264PPS(const RtmpPacket &thiz) { - string ret; - if (thiz.getMediaType() != FLV_CODEC_H264) { - return ret; - } - if (!thiz.isCfgFrame()) { - return ret; - } - if (thiz.buffer.size() < 13) { - WarnL << "bad H264 cfg!"; - return ret; - } - uint16_t sps_size ; + uint16_t sps_size; memcpy(&sps_size, thiz.buffer.data() + 11, 2); sps_size = ntohs(sps_size); if ((int) thiz.buffer.size() < 13 + sps_size + 1 + 2) { - WarnL << "bad H264 cfg!"; - return ret; + return false; } - uint16_t pps_size ; + uint16_t pps_size; memcpy(&pps_size, thiz.buffer.data() + 13 + sps_size + 1, 2); pps_size = ntohs(pps_size); if ((int) thiz.buffer.size() < 13 + sps_size + 1 + 2 + pps_size) { - WarnL << "bad H264 cfg!"; - return ret; + return false; } - ret.assign(thiz.buffer.data() + 13 + sps_size + 1 + 2, pps_size); - return ret; + sps.assign(thiz.buffer.data() + 13, sps_size); + pps.assign(thiz.buffer.data() + 13 + sps_size + 1 + 2, pps_size); + return true; } void H264RtmpDecoder::inputRtmp(const RtmpPacket::Ptr &pkt) { if (pkt->isCfgFrame()) { //缓存sps pps,后续插入到I帧之前 - _sps = getH264SPS(*pkt); - _pps = getH264PPS(*pkt); - onGetH264(_sps.data(), _sps.size(), pkt->time_stamp , pkt->time_stamp); - onGetH264(_pps.data(), _pps.size(), pkt->time_stamp , pkt->time_stamp); + if (!getH264Config(*pkt, _sps, _pps)) { + WarnL << "get h264 sps/pps failed, rtmp packet is: " << hexdump(pkt->data(), pkt->size()); + return; + } + onGetH264(_sps.data(), _sps.size(), pkt->time_stamp, pkt->time_stamp); + onGetH264(_pps.data(), _pps.size(), pkt->time_stamp, pkt->time_stamp); return; } @@ -115,24 +86,18 @@ void H264RtmpDecoder::inputRtmp(const RtmpPacket::Ptr &pkt) { } } -inline void H264RtmpDecoder::onGetH264(const char* pcData, size_t iLen, uint32_t dts,uint32_t pts) { - if(iLen == 0){ +inline void H264RtmpDecoder::onGetH264(const char* data, size_t len, uint32_t dts, uint32_t pts) { + if (!len) { return; } -#if 1 _h264frame->_dts = dts; _h264frame->_pts = pts; _h264frame->_buffer.assign("\x00\x00\x00\x01", 4); //添加264头 - _h264frame->_buffer.append(pcData, iLen); + _h264frame->_buffer.append(data, len); //写入环形缓存 RtmpCodec::inputFrame(_h264frame); _h264frame = obtainFrame(); -#else - //防止内存拷贝,这样产生的264帧不会有0x00 00 01头 - auto frame = std::make_shared((char *)pcData,iLen,dts,pts,0); - RtmpCodec::inputFrame(frame); -#endif } //////////////////////////////////////////////////////////////////////// diff --git a/src/Extension/H264Rtmp.h b/src/Extension/H264Rtmp.h index 19897e8a..fc69c3fe 100644 --- a/src/Extension/H264Rtmp.h +++ b/src/Extension/H264Rtmp.h @@ -40,7 +40,7 @@ public: } protected: - void onGetH264(const char *pcData, size_t iLen, uint32_t dts,uint32_t pts); + void onGetH264(const char *data, size_t len, uint32_t dts, uint32_t pts); H264Frame::Ptr obtainFrame(); protected: