Rtmp: 获取h264sps/aac config失败时打印rtmp包内容

This commit is contained in:
xiongziliang 2022-01-08 16:21:00 +08:00
parent 692febcadd
commit f4d8eb4515
3 changed files with 26 additions and 59 deletions

View File

@ -22,7 +22,7 @@ static string getAacCfg(const RtmpPacket &thiz) {
return ret; return ret;
} }
if (thiz.buffer.size() < 4) { if (thiz.buffer.size() < 4) {
WarnL << "bad aac cfg!"; WarnL << "get aac config failed, rtmp packet is: " << hexdump(thiz.data(), thiz.size());
return ret; return ret;
} }
ret = thiz.buffer.substr(2); ret = thiz.buffer.substr(2);
@ -32,7 +32,9 @@ static string getAacCfg(const RtmpPacket &thiz) {
void AACRtmpDecoder::inputRtmp(const RtmpPacket::Ptr &pkt) { void AACRtmpDecoder::inputRtmp(const RtmpPacket::Ptr &pkt) {
if (pkt->isCfgFrame()) { if (pkt->isCfgFrame()) {
_aac_cfg = getAacCfg(*pkt); _aac_cfg = getAacCfg(*pkt);
if (!_aac_cfg.empty()) {
onGetAAC(nullptr, 0, 0); onGetAAC(nullptr, 0, 0);
}
return; return;
} }

View File

@ -23,73 +23,44 @@ H264Frame::Ptr H264RtmpDecoder::obtainFrame() {
} }
/** /**
* 0x00 00 00 01sps * 0x00 00 00 01sps pps
* @return
*/ */
static string getH264SPS(const RtmpPacket &thiz) { static bool getH264Config(const RtmpPacket &thiz, string &sps, string &pps) {
string ret;
if (thiz.getMediaType() != FLV_CODEC_H264) { if (thiz.getMediaType() != FLV_CODEC_H264) {
return ret; return false;
} }
if (!thiz.isCfgFrame()) { if (!thiz.isCfgFrame()) {
return ret; return false;
} }
if (thiz.buffer.size() < 13) { if (thiz.buffer.size() < 13) {
WarnL << "bad H264 cfg!"; return false;
return ret;
}
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 01pps
* @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); memcpy(&sps_size, thiz.buffer.data() + 11, 2);
sps_size = ntohs(sps_size); sps_size = ntohs(sps_size);
if ((int) thiz.buffer.size() < 13 + sps_size + 1 + 2) { if ((int) thiz.buffer.size() < 13 + sps_size + 1 + 2) {
WarnL << "bad H264 cfg!"; return false;
return ret;
} }
uint16_t pps_size; uint16_t pps_size;
memcpy(&pps_size, thiz.buffer.data() + 13 + sps_size + 1, 2); memcpy(&pps_size, thiz.buffer.data() + 13 + sps_size + 1, 2);
pps_size = ntohs(pps_size); pps_size = ntohs(pps_size);
if ((int) thiz.buffer.size() < 13 + sps_size + 1 + 2 + pps_size) { if ((int) thiz.buffer.size() < 13 + sps_size + 1 + 2 + pps_size) {
WarnL << "bad H264 cfg!"; return false;
return ret;
} }
ret.assign(thiz.buffer.data() + 13 + sps_size + 1 + 2, pps_size); sps.assign(thiz.buffer.data() + 13, sps_size);
return ret; pps.assign(thiz.buffer.data() + 13 + sps_size + 1 + 2, pps_size);
return true;
} }
void H264RtmpDecoder::inputRtmp(const RtmpPacket::Ptr &pkt) { void H264RtmpDecoder::inputRtmp(const RtmpPacket::Ptr &pkt) {
if (pkt->isCfgFrame()) { if (pkt->isCfgFrame()) {
//缓存sps pps后续插入到I帧之前 //缓存sps pps后续插入到I帧之前
_sps = getH264SPS(*pkt); if (!getH264Config(*pkt, _sps, _pps)) {
_pps = getH264PPS(*pkt); 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(_sps.data(), _sps.size(), pkt->time_stamp, pkt->time_stamp);
onGetH264(_pps.data(), _pps.size(), pkt->time_stamp, pkt->time_stamp); onGetH264(_pps.data(), _pps.size(), pkt->time_stamp, pkt->time_stamp);
return; 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) { inline void H264RtmpDecoder::onGetH264(const char* data, size_t len, uint32_t dts, uint32_t pts) {
if(iLen == 0){ if (!len) {
return; return;
} }
#if 1
_h264frame->_dts = dts; _h264frame->_dts = dts;
_h264frame->_pts = pts; _h264frame->_pts = pts;
_h264frame->_buffer.assign("\x00\x00\x00\x01", 4); //添加264头 _h264frame->_buffer.assign("\x00\x00\x00\x01", 4); //添加264头
_h264frame->_buffer.append(pcData, iLen); _h264frame->_buffer.append(data, len);
//写入环形缓存 //写入环形缓存
RtmpCodec::inputFrame(_h264frame); RtmpCodec::inputFrame(_h264frame);
_h264frame = obtainFrame(); _h264frame = obtainFrame();
#else
//防止内存拷贝这样产生的264帧不会有0x00 00 01头
auto frame = std::make_shared<H264FrameNoCacheAble>((char *)pcData,iLen,dts,pts,0);
RtmpCodec::inputFrame(frame);
#endif
} }
//////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////

View File

@ -40,7 +40,7 @@ public:
} }
protected: 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(); H264Frame::Ptr obtainFrame();
protected: protected: