完善H264 rtp解包打包相关代码

This commit is contained in:
xiongziliang 2020-07-26 19:56:39 +08:00
parent c0fde62480
commit 3acfb15c99
2 changed files with 44 additions and 66 deletions

View File

@ -12,13 +12,6 @@
namespace mediakit{ namespace mediakit{
typedef struct {
unsigned forbidden_zero_bit :1;
unsigned nal_ref_idc :2;
unsigned type :5;
} NALU;
typedef struct { typedef struct {
unsigned S :1; unsigned S :1;
unsigned E :1; unsigned E :1;
@ -26,15 +19,6 @@ typedef struct {
unsigned type :5; unsigned type :5;
} FU; } FU;
static bool MakeNalu(uint8_t in, NALU &nal) {
nal.forbidden_zero_bit = in >> 7;
if (nal.forbidden_zero_bit) {
return false;
}
nal.nal_ref_idc = (in & 0x60) >> 5;
nal.type = in & 0x1f;
return true;
}
static bool MakeFU(uint8_t in, FU &fu) { static bool MakeFU(uint8_t in, FU &fu) {
fu.S = in >> 7; fu.S = in >> 7;
fu.E = (in >> 6) & 0x01; fu.E = (in >> 6) & 0x01;
@ -86,15 +70,13 @@ bool H264RtpDecoder::decodeRtp(const RtpPacket::Ptr &rtppack) {
28 FU-A Fragmentation unit 5.8 28 FU-A Fragmentation unit 5.8
29 FU-B Fragmentation unit 5.8 29 FU-B Fragmentation unit 5.8
30-31 undefined - 30-31 undefined -
*/ */
const uint8_t *frame = (uint8_t *) rtppack->data() + rtppack->offset; const uint8_t *frame = (uint8_t *) rtppack->data() + rtppack->offset;
int length = rtppack->size() - rtppack->offset; int length = rtppack->size() - rtppack->offset;
NALU nal; int nal_type = *frame & 0x1F;
MakeNalu(*frame, nal); int nal_suffix = *frame & (~0x1F);
if (nal.type >= 0 && nal.type < 24) { if (nal_type >= 0 && nal_type < 24) {
//a full frame //a full frame
_h264frame->_buffer.assign("\x0\x0\x0\x1", 4); _h264frame->_buffer.assign("\x0\x0\x0\x1", 4);
_h264frame->_buffer.append((char *) frame, length); _h264frame->_buffer.append((char *) frame, length);
@ -104,7 +86,7 @@ bool H264RtpDecoder::decodeRtp(const RtpPacket::Ptr &rtppack) {
return (key); //i frame return (key); //i frame
} }
switch (nal.type){ switch (nal_type){
case 24:{ case 24:{
// 24 STAP-A 单一时间的组合包 // 24 STAP-A 单一时间的组合包
bool haveIDR = false; bool haveIDR = false;
@ -121,14 +103,12 @@ bool H264RtpDecoder::decodeRtp(const RtpPacket::Ptr &rtppack) {
if (off + len > length) { if (off + len > length) {
break; break;
} }
if(len >= 10){ if (len > 4) {
//过小的帧丢弃 //过小的帧丢弃
NALU nal;
MakeNalu(ptr[0], nal);
_h264frame->_buffer.assign("\x0\x0\x0\x1", 4); _h264frame->_buffer.assign("\x0\x0\x0\x1", 4);
_h264frame->_buffer.append((char *) ptr, len); _h264frame->_buffer.append((char *) ptr, len);
_h264frame->_pts = rtppack->timeStamp; _h264frame->_pts = rtppack->timeStamp;
if(nal.type == H264Frame::NAL_IDR){ if ((ptr[0] & 0x1F) == H264Frame::NAL_IDR) {
haveIDR = true; haveIDR = true;
} }
onGetH264(_h264frame); onGetH264(_h264frame);
@ -144,9 +124,8 @@ bool H264RtpDecoder::decodeRtp(const RtpPacket::Ptr &rtppack) {
MakeFU(frame[1], fu); MakeFU(frame[1], fu);
if (fu.S) { if (fu.S) {
//该帧的第一个rtp包 FU-A start //该帧的第一个rtp包 FU-A start
char tmp = (nal.forbidden_zero_bit << 7 | nal.nal_ref_idc << 5 | fu.type);
_h264frame->_buffer.assign("\x0\x0\x0\x1", 4); _h264frame->_buffer.assign("\x0\x0\x0\x1", 4);
_h264frame->_buffer.push_back(tmp); _h264frame->_buffer.push_back(nal_suffix | fu.type);
_h264frame->_buffer.append((char *) frame + 2, length - 2); _h264frame->_buffer.append((char *) frame + 2, length - 2);
_h264frame->_pts = rtppack->timeStamp; _h264frame->_pts = rtppack->timeStamp;
//该函数return时保存下当前sequence,以便下次对比seq是否连续 //该函数return时保存下当前sequence,以便下次对比seq是否连续
@ -184,7 +163,7 @@ bool H264RtpDecoder::decodeRtp(const RtpPacket::Ptr &rtppack) {
// 0 udef // 0 udef
// 30 udef // 30 udef
// 31 udef // 31 udef
WarnL << "不支持的rtp类型:" << (int)nal.type << " " << rtppack->sequence; WarnL << "不支持的rtp类型:" << (int) nal_type << " " << rtppack->sequence;
return false; return false;
} }
} }
@ -215,63 +194,62 @@ H264RtpEncoder::H264RtpEncoder(uint32_t ui32Ssrc,
void H264RtpEncoder::inputFrame(const Frame::Ptr &frame) { void H264RtpEncoder::inputFrame(const Frame::Ptr &frame) {
GET_CONFIG(uint32_t,cycleMS,Rtp::kCycleMS); GET_CONFIG(uint32_t,cycleMS,Rtp::kCycleMS);
auto pcData = frame->data() + frame->prefixSize(); auto ptr = frame->data() + frame->prefixSize();
auto uiStamp = frame->pts(); auto pts = frame->pts() % cycleMS;
auto iLen = frame->size() - frame->prefixSize(); auto len = frame->size() - frame->prefixSize();
//获取NALU的5bit 帧类型 auto nal_type = H264_TYPE(ptr[0]);
unsigned char naluType = H264_TYPE(pcData[0]); auto max_rtp_size = _ui32MtuSize - 2;
uiStamp %= cycleMS;
int iSize = _ui32MtuSize - 2;
//超过MTU则按照FU-A模式打包 //超过MTU则按照FU-A模式打包
if (iLen > iSize) { if (len > max_rtp_size) {
//最高位bit为forbidden_zero_bit, //最高位bit为forbidden_zero_bit,
//后面2bit为nal_ref_idc(帧重要程度),00:可以丢,11:不能丢 //后面2bit为nal_ref_idc(帧重要程度),00:可以丢,11:不能丢
//末尾5bit为nalu type固定为28(FU-A) //末尾5bit为nalu type固定为28(FU-A)
unsigned char f_nri_flags = (*((unsigned char *) pcData) & 0x60) | 28; unsigned char nal_fu_a = (*((unsigned char *) ptr) & (~0x1F)) | 28;
unsigned char s_e_r_flags; unsigned char s_e_r_flags;
bool bFirst = true; bool fu_a_start = true;
bool mark = false; bool mark_bit = false;
int nOffset = 1; int offset = 1;
while (!mark) { while (!mark_bit) {
if (iLen <= nOffset + iSize) { if (len <= offset + max_rtp_size) {
//已经拆分结束 //已经拆分结束
iSize = iLen - nOffset; max_rtp_size = len - offset;
mark = true; mark_bit = true;
//FU-A end //FU-A end
s_e_r_flags = (1 << 6) | naluType; s_e_r_flags = (1 << 6) | nal_type;
} else if (bFirst) { } else if (fu_a_start) {
//FU-A start //FU-A start
s_e_r_flags = (1 << 7) | naluType; s_e_r_flags = (1 << 7) | nal_type;
} else { } else {
//FU-A mid //FU-A mid
s_e_r_flags = naluType; s_e_r_flags = nal_type;
} }
{ {
//传入nullptr先不做payload的内存拷贝 //传入nullptr先不做payload的内存拷贝
auto rtp = makeRtp(getTrackType(), nullptr, iSize + 2, mark, uiStamp); auto rtp = makeRtp(getTrackType(), nullptr, max_rtp_size + 2, mark_bit, pts);
//rtp payload 负载部分 //rtp payload 负载部分
uint8_t *payload = (uint8_t*)rtp->data() + rtp->offset; uint8_t *payload = (uint8_t*)rtp->data() + rtp->offset;
//FU-A 第1个字节 //FU-A 第1个字节
payload[0] = f_nri_flags; payload[0] = nal_fu_a;
//FU-A 第2个字节 //FU-A 第2个字节
payload[1] = s_e_r_flags; payload[1] = s_e_r_flags;
//H264 数据 //H264 数据
memcpy(payload + 2, (unsigned char *) pcData + nOffset, iSize); memcpy(payload + 2, (unsigned char *) ptr + offset, max_rtp_size);
//输入到rtp环形缓存 //输入到rtp环形缓存
RtpCodec::inputRtp(rtp,bFirst && naluType == H264Frame::NAL_IDR); RtpCodec::inputRtp(rtp, fu_a_start && nal_type == H264Frame::NAL_IDR);
} }
nOffset += iSize; offset += max_rtp_size;
bFirst = false; fu_a_start = false;
} }
} else { } else {
makeH264Rtp(naluType,pcData, iLen, true, true, uiStamp); //如果帧长度不超过mtu, 则按照Single NAL unit packet per H.264 方式打包
makeH264Rtp(ptr, len, false, false, pts);
} }
} }
void H264RtpEncoder::makeH264Rtp(int nal_type,const void* data, unsigned int len, bool mark, bool first_packet, uint32_t uiStamp) { void H264RtpEncoder::makeH264Rtp(const void* data, unsigned int len, bool mark, bool gop_pos, uint32_t uiStamp) {
RtpCodec::inputRtp(makeRtp(getTrackType(),data,len,mark,uiStamp),first_packet && nal_type == H264Frame::NAL_IDR); RtpCodec::inputRtp(makeRtp(getTrackType(), data, len, mark, uiStamp), gop_pos);
} }
}//namespace mediakit }//namespace mediakit

View File

@ -78,7 +78,7 @@ public:
*/ */
void inputFrame(const Frame::Ptr &frame) override; void inputFrame(const Frame::Ptr &frame) override;
private: private:
void makeH264Rtp(int nal_type,const void *pData, unsigned int uiLen, bool bMark, bool first_packet, uint32_t uiStamp); void makeH264Rtp(const void *pData, unsigned int uiLen, bool bMark, bool gop_pos, uint32_t uiStamp);
}; };
}//namespace mediakit{ }//namespace mediakit{