修复fu rtp打包的bug

This commit is contained in:
xiongziliang 2020-10-24 23:29:21 +08:00
parent 84784f21c4
commit fd898254f2
2 changed files with 38 additions and 41 deletions

View File

@ -195,13 +195,13 @@ 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 ptr = frame->data() + frame->prefixSize(); auto ptr = frame->data() + frame->prefixSize();
auto pts = frame->pts() % cycleMS;
auto len = frame->size() - frame->prefixSize(); auto len = frame->size() - frame->prefixSize();
auto pts = frame->pts() % cycleMS;
auto nal_type = H264_TYPE(ptr[0]); auto nal_type = H264_TYPE(ptr[0]);
auto max_rtp_size = _ui32MtuSize - 2; auto payload_size = _ui32MtuSize - 2;
//超过MTU则按照FU-A模式打包 //超过MTU则按照FU-A模式打包
if (len > max_rtp_size) { if (len > payload_size + 1) {
//最高位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)
@ -211,11 +211,10 @@ void H264RtpEncoder::inputFrame(const Frame::Ptr &frame) {
bool mark_bit = false; bool mark_bit = false;
int offset = 1; int offset = 1;
while (!mark_bit) { while (!mark_bit) {
if (len <= offset + max_rtp_size) { if (len <= offset + payload_size) {
//已经拆分结束
max_rtp_size = len - offset;
mark_bit = true;
//FU-A end //FU-A end
mark_bit = true;
payload_size = len - offset;
s_e_r_flags = (1 << 6) | nal_type; s_e_r_flags = (1 << 6) | nal_type;
} else if (fu_a_start) { } else if (fu_a_start) {
//FU-A start //FU-A start
@ -227,7 +226,7 @@ void H264RtpEncoder::inputFrame(const Frame::Ptr &frame) {
{ {
//传入nullptr先不做payload的内存拷贝 //传入nullptr先不做payload的内存拷贝
auto rtp = makeRtp(getTrackType(), nullptr, max_rtp_size + 2, mark_bit, pts); auto rtp = makeRtp(getTrackType(), nullptr, payload_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个字节
@ -235,11 +234,11 @@ void H264RtpEncoder::inputFrame(const Frame::Ptr &frame) {
//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 *) ptr + offset, max_rtp_size); memcpy(payload + 2, (unsigned char *) ptr + offset, payload_size);
//输入到rtp环形缓存 //输入到rtp环形缓存
RtpCodec::inputRtp(rtp, fu_a_start && nal_type == H264Frame::NAL_IDR); RtpCodec::inputRtp(rtp, fu_a_start && nal_type == H264Frame::NAL_IDR);
} }
offset += max_rtp_size; offset += payload_size;
fu_a_start = false; fu_a_start = false;
} }
} else { } else {

View File

@ -18,10 +18,10 @@ namespace mediakit{
//44 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ //44 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
//45 |F| Type | LayerId | TID | //45 |F| Type | LayerId | TID |
//46 +-------------+-----------------+ //46 +-------------+-----------------+
//48 F = 0 //48 F = 0, 1bit
//49 Type = 49 (fragmentation unit (FU)) //49 Type = 49 (fragmentation unit (FU)), 6bit
//50 LayerId = 0 //50 LayerId = 0, 6bit
//51 TID = 1 //51 TID = 1, 3bit
//56 /* //56 /*
//57 create the FU header //57 create the FU header
//58 //58
@ -29,7 +29,6 @@ namespace mediakit{
//60 +-+-+-+-+-+-+-+-+ //60 +-+-+-+-+-+-+-+-+
//61 |S|E| FuType | //61 |S|E| FuType |
//62 +---------------+ //62 +---------------+
//63
//64 S = variable //64 S = variable
//65 E = variable //65 E = variable
//66 FuType = NAL unit type //66 FuType = NAL unit type
@ -150,40 +149,39 @@ H265RtpEncoder::H265RtpEncoder(uint32_t ui32Ssrc,
} }
void H265RtpEncoder::inputFrame(const Frame::Ptr &frame) { void H265RtpEncoder::inputFrame(const Frame::Ptr &frame) {
GET_CONFIG(uint32_t,cycleMS,Rtp::kCycleMS); GET_CONFIG(uint32_t, cycleMS, Rtp::kCycleMS);
uint8_t *pcData = (uint8_t*)frame->data() + frame->prefixSize(); auto ptr = (uint8_t *) frame->data() + frame->prefixSize();
auto uiStamp = frame->pts(); auto len = frame->size() - frame->prefixSize();
auto iLen = frame->size() - frame->prefixSize(); auto pts = frame->pts() % cycleMS;
unsigned char naluType = H265_TYPE(pcData[0]); //获取NALU的5bit 帧类型 auto nal_type = H265_TYPE(ptr[0]); //获取NALU的5bit 帧类型
uiStamp %= cycleMS; auto payload_size = _ui32MtuSize - 3;
int maxSize = _ui32MtuSize - 3;
//超过MTU,按照FU方式打包 //超过MTU,按照FU方式打包
if (iLen > maxSize) { if (len > payload_size + 2) {
//获取帧头数据1byte //获取帧头数据1byte
unsigned char s_e_flags; unsigned char s_e_flags;
bool bFirst = true; bool fu_start = true;
bool mark = false; bool mark_bit = false;
int nOffset = 2; int offset = 2;
while (!mark) { while (!mark_bit) {
if (iLen <= nOffset + maxSize) { //是否拆分结束 if (len <= offset + payload_size) {
maxSize = iLen - nOffset;
mark = true;
//FU end //FU end
s_e_flags = (1 << 6) | naluType; mark_bit = true;
} else if (bFirst) { payload_size = len - offset;
s_e_flags = (1 << 6) | nal_type;
} else if (fu_start) {
//FU start //FU start
s_e_flags = (1 << 7) | naluType; s_e_flags = (1 << 7) | nal_type;
} else { } else {
//FU mid //FU mid
s_e_flags = naluType; s_e_flags = nal_type;
} }
{ {
//传入nullptr先不做payload的内存拷贝 //传入nullptr先不做payload的内存拷贝
auto rtp = makeRtp(getTrackType(), nullptr, maxSize + 3, mark, uiStamp); auto rtp = makeRtp(getTrackType(), nullptr, payload_size + 3, 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 第1个字节表明为FU //FU 第1个字节表明为FU
payload[0] = 49 << 1; payload[0] = 49 << 1;
//FU 第2个字节貌似固定为1 //FU 第2个字节貌似固定为1
@ -191,16 +189,16 @@ void H265RtpEncoder::inputFrame(const Frame::Ptr &frame) {
//FU 第3个字节 //FU 第3个字节
payload[2] = s_e_flags; payload[2] = s_e_flags;
//H265 数据 //H265 数据
memcpy(payload + 3,pcData + nOffset, maxSize); memcpy(payload + 3, ptr + offset, payload_size);
//输入到rtp环形缓存 //输入到rtp环形缓存
RtpCodec::inputRtp(rtp,bFirst && H265Frame::isKeyFrame(naluType)); RtpCodec::inputRtp(rtp, fu_start && H265Frame::isKeyFrame(nal_type));
} }
nOffset += maxSize; offset += payload_size;
bFirst = false; fu_start = false;
} }
} else { } else {
makeH265Rtp(naluType,pcData, iLen, false, true, uiStamp); makeH265Rtp(nal_type, ptr, len, false, true, pts);
} }
} }