完善rtp打包

This commit is contained in:
xiongziliang 2019-09-06 23:22:04 +08:00
parent 9f1a91cda6
commit 203edc5cec
2 changed files with 30 additions and 24 deletions

View File

@ -238,32 +238,34 @@ void H264RtpEncoder::inputFrame(const Frame::Ptr &frame) {
auto pcData = frame->data() + frame->prefixSize(); auto pcData = frame->data() + frame->prefixSize();
auto uiStamp = frame->stamp(); auto uiStamp = frame->stamp();
auto iLen = frame->size() - frame->prefixSize(); auto iLen = frame->size() - frame->prefixSize();
unsigned char naluType = H264_TYPE(pcData[0]); //获取NALU的5bit 帧类型 //获取NALU的5bit 帧类型
unsigned char naluType = H264_TYPE(pcData[0]);
uiStamp %= cycleMS; uiStamp %= cycleMS;
int iSize = _ui32MtuSize - 2; int iSize = _ui32MtuSize - 2;
if (iLen > iSize) { //超过MTU //超过MTU则按照FU-A模式打包
const unsigned char s_e_r_Start = 0x80; if (iLen > iSize) {
const unsigned char s_e_r_Mid = 0x00; //最高位bit为forbidden_zero_bit,
const unsigned char s_e_r_End = 0x40; //后面2bit为nal_ref_idc(帧重要程度),00:可以丢,11:不能丢
//获取帧头数据1byte //末尾5bit为nalu type固定为28(FU-A)
unsigned char nal_ref_idc = *((unsigned char *) pcData) & 0x60; //获取NALU的2bit 帧重要程度 00 可以丢 11不能丢 unsigned char f_nri_flags = (*((unsigned char *) pcData) & 0x60) | 28;
//nal_ref_idc = 0x60; unsigned char s_e_r_flags;
//组装FU-A帧头数据 2byte
unsigned char f_nri_type = nal_ref_idc + 28;//F为0 1bit,nri上面获取到2bit,28为FU-A分片类型5bit
unsigned char s_e_r_type = naluType;
bool bFirst = true; bool bFirst = true;
bool mark = false; bool mark = false;
int nOffset = 1; int nOffset = 1;
while (!mark) { while (!mark) {
if (iLen < nOffset + iSize) { //是否拆分结束 if (iLen < nOffset + iSize) {
//已经拆分结束
iSize = iLen - nOffset; iSize = iLen - nOffset;
mark = true; mark = true;
s_e_r_type = s_e_r_End + naluType; //FU-A end
} else if (bFirst) { s_e_r_flags = (1 << 6) | naluType;
s_e_r_type = s_e_r_Start + naluType; } else if (bFirst) {
//FU-A start
s_e_r_flags = (1 << 7) | naluType;
} else { } else {
s_e_r_type = s_e_r_Mid + naluType; //FU-A mid
s_e_r_flags = naluType;
} }
{ {
@ -272,9 +274,9 @@ void H264RtpEncoder::inputFrame(const Frame::Ptr &frame) {
//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个字节
memcpy(payload, &f_nri_type, 1); payload[0] = f_nri_flags;
//FU-A 第2个字节 //FU-A 第2个字节
memcpy(payload + 1, &s_e_r_type, 1); payload[1] = s_e_r_flags;
//H264 数据 //H264 数据
memcpy(payload + 2, (unsigned char *) pcData + nOffset, iSize); memcpy(payload + 2, (unsigned char *) pcData + nOffset, iSize);
//输入到rtp环形缓存 //输入到rtp环形缓存

View File

@ -177,7 +177,8 @@ void H265RtpEncoder::inputFrame(const Frame::Ptr &frame) {
uiStamp %= cycleMS; uiStamp %= cycleMS;
int maxSize = _ui32MtuSize - 3; int maxSize = _ui32MtuSize - 3;
if (iLen > maxSize) { //超过MTU //超过MTU,按照FU方式打包
if (iLen > maxSize) {
//获取帧头数据1byte //获取帧头数据1byte
unsigned char s_e_type; unsigned char s_e_type;
bool bFirst = true; bool bFirst = true;
@ -187,10 +188,13 @@ void H265RtpEncoder::inputFrame(const Frame::Ptr &frame) {
if (iLen < nOffset + maxSize) { //是否拆分结束 if (iLen < nOffset + maxSize) { //是否拆分结束
maxSize = iLen - nOffset; maxSize = iLen - nOffset;
mark = true; mark = true;
s_e_type = 1 << 6 | naluType; //FU end
s_e_type = (1 << 6) | naluType;
} else if (bFirst) { } else if (bFirst) {
s_e_type = 1 << 7 | naluType; //FU start
s_e_type = (1 << 7) | naluType;
} else { } else {
//FU mid
s_e_type = naluType; s_e_type = naluType;
} }
@ -199,11 +203,11 @@ void H265RtpEncoder::inputFrame(const Frame::Ptr &frame) {
auto rtp = makeRtp(getTrackType(), nullptr, maxSize + 3, mark, uiStamp); auto rtp = makeRtp(getTrackType(), nullptr, maxSize + 3, mark, uiStamp);
//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 第1个字节表明为FU
payload[0] = 49 << 1; payload[0] = 49 << 1;
//FU-A 第2个字节貌似固定为1 //FU 第2个字节貌似固定为1
payload[1] = 1; payload[1] = 1;
//FU-A 第3个字节 //FU 第3个字节
payload[2] = s_e_type; payload[2] = s_e_type;
//H265 数据 //H265 数据
memcpy(payload + 3,pcData + nOffset, maxSize); memcpy(payload + 3,pcData + nOffset, maxSize);