diff --git a/src/Extension/H264Rtp.cpp b/src/Extension/H264Rtp.cpp index 54a7fc5c..ec45a71f 100644 --- a/src/Extension/H264Rtp.cpp +++ b/src/Extension/H264Rtp.cpp @@ -189,8 +189,52 @@ void H264RtpEncoder::inputFrame(const Frame::Ptr &frame) { auto len = frame->size() - frame->prefixSize(); auto pts = frame->pts(); auto nal_type = H264_TYPE(ptr[0]); - auto packet_size = getMaxSize() - 2; + if(nal_type == H264Frame::NAL_SEI || nal_type == H264Frame::NAL_AUD){ + return; + } + if(nal_type == H264Frame::NAL_SPS){ + _sps = std::string(ptr,len); + return; + } + + if(nal_type == H264Frame::NAL_PPS){ + _pps = std::string(ptr,len); + return; + } + + if(!_last_frame){ + _last_frame = frame; + return; + } + // 上一帧打包,保证rtp 的mark是正确的 + bool isMark = _last_frame->pts() != frame->pts(); + ptr = _last_frame->data() + _last_frame->prefixSize(); + len = _last_frame->size() - _last_frame->prefixSize(); + pts = _last_frame->pts(); + nal_type = H264_TYPE(ptr[0]); + if(nal_type == H264Frame::NAL_IDR && (ptr[1]&0x80)) + {// 保证每一个I帧前都有SPS与PPS ,为了兼容webrtc 需要在一个rtp包中,并且只能是 STAP-A + // https://blog.csdn.net/momo0853/article/details/88872873 + // 多slice 一帧的情况下检查 first_mb_in_slice 是否为0 表示其为一帧的开始,SPS PPS 只有在帧开始时,才插入 + auto rtp = makeRtp(getTrackType(), nullptr,_sps.size()+_pps.size()+2*2+1,false,pts); + uint8_t *payload = rtp->getPayload(); + payload[0] = 24; + payload[1] = _sps.size() >> 8; + payload[2] = _sps.size() & 0xff; + memcpy(payload+3,(uint8_t *) _sps.data(),_sps.size()); + + payload[_sps.size()+3] = _pps.size() >> 8; + payload[_sps.size()+4] = _pps.size() & 0xff; + + memcpy(payload+3+_sps.size()+2,(uint8_t *) _pps.data(),_pps.size()); + RtpCodec::inputRtp(rtp,true); + } + + + + auto packet_size = getMaxSize() - 2; + //InfoL<<"nal type = "<