初步整理Rtp打包解包

This commit is contained in:
xiongziliang
2018-10-18 23:48:00 +08:00
parent 4b87ec36c6
commit 8ed205878f
19 changed files with 521 additions and 89 deletions

104
src/RTP/AACRtpEncoder.cpp Normal file
View File

@@ -0,0 +1,104 @@
//
// Created by xzl on 2018/10/18.
//
#include "AACRtpEncoder.h"
void AACRtpEncoder::inputFame(const Frame::Ptr &frame, bool key_pos) {
RtpCodec::inputFame(frame, key_pos);
GET_CONFIG_AND_REGISTER(uint32_t,cycleMS,Config::Rtp::kCycleMS);
auto uiStamp = frame->stamp();
auto pcData = frame->data();
auto iLen = frame->size();
uiStamp %= cycleMS;
char *ptr = (char *) pcData;
int iSize = iLen;
while (iSize > 0 ) {
if (iSize <= m_ui32MtuSize - 20) {
m_aucSectionBuf[0] = 0;
m_aucSectionBuf[1] = 16;
m_aucSectionBuf[2] = iLen >> 5;
m_aucSectionBuf[3] = (iLen & 0x1F) << 3;
memcpy(m_aucSectionBuf + 4, ptr, iSize);
makeAACRtp(m_aucSectionBuf, iSize + 4, true, uiStamp);
break;
}
m_aucSectionBuf[0] = 0;
m_aucSectionBuf[1] = 16;
m_aucSectionBuf[2] = (iLen) >> 5;
m_aucSectionBuf[3] = (iLen & 0x1F) << 3;
memcpy(m_aucSectionBuf + 4, ptr, m_ui32MtuSize - 20);
makeAACRtp(m_aucSectionBuf, m_ui32MtuSize - 16, false, uiStamp);
ptr += (m_ui32MtuSize - 20);
iSize -= (m_ui32MtuSize - 20);
}
}
void AACRtpEncoder::makeAACRtp(const void *pData, unsigned int uiLen, bool bMark, uint32_t uiStamp) {
uint16_t u16RtpLen = uiLen + 12;
m_ui32TimeStamp = (m_ui32SampleRate / 1000) * uiStamp;
uint32_t ts = htonl(m_ui32TimeStamp);
uint16_t sq = htons(m_ui16Sequence);
uint32_t sc = htonl(m_ui32Ssrc);
auto pRtppkt = obtainRtp();
auto &rtppkt = *(pRtppkt.get());
unsigned char *pucRtp = rtppkt.payload;
pucRtp[0] = '$';
pucRtp[1] = m_ui8Interleaved;
pucRtp[2] = u16RtpLen >> 8;
pucRtp[3] = u16RtpLen & 0x00FF;
pucRtp[4] = 0x80;
pucRtp[5] = (bMark << 7) | m_ui8PlayloadType;
memcpy(&pucRtp[6], &sq, 2);
memcpy(&pucRtp[8], &ts, 4);
//ssrc
memcpy(&pucRtp[12], &sc, 4);
//playload
memcpy(&pucRtp[16], pData, uiLen);
rtppkt.PT = m_ui8PlayloadType;
rtppkt.interleaved = m_ui8Interleaved;
rtppkt.mark = bMark;
rtppkt.length = uiLen + 16;
rtppkt.sequence = m_ui16Sequence;
rtppkt.timeStamp = m_ui32TimeStamp;
rtppkt.ssrc = m_ui32Ssrc;
rtppkt.type = TrackAudio;
rtppkt.offset = 16;
RtpCodec::inputRtp(pRtppkt, false);
m_ui16Sequence++;
}
/////////////////////////////////////////////////////////////////////////////////////
void AACRtpDecoder::inputRtp(const RtpPacket::Ptr &rtp, bool key_pos) {
RtpCodec::inputRtp(rtp, key_pos);
auto &rtppack = *rtp;
char *frame = (char *) rtppack.payload + rtppack.offset;
int length = rtppack.length - rtppack.offset;
if (m_adts->aac_frame_length + length - 4 > sizeof(AdtsFrame::buffer)) {
m_adts->aac_frame_length = 7;
return ;
}
memcpy(m_adts->buffer + m_adts->aac_frame_length, frame + 4, length - 4);
m_adts->aac_frame_length += (length - 4);
if (rtppack.mark == true) {
m_adts->sequence = rtppack.sequence;
//todo(xzl) 完成时间戳转换
// m_adts->timeStamp = rtppack.timeStamp * (1000.0 / m_iSampleRate);
writeAdtsHeader(*m_adts, m_adts->buffer);
RtpCodec::inputFame(m_adts,false);
m_adts->aac_frame_length = 7;
}
}
AACRtpDecoder::AACRtpDecoder() {
m_adts = std::make_shared<AdtsFrame>();
}

41
src/RTP/AACRtpEncoder.h Normal file
View File

@@ -0,0 +1,41 @@
//
// Created by xzl on 2018/10/18.
//
#ifndef ZLMEDIAKIT_AACRTPCODEC_H
#define ZLMEDIAKIT_AACRTPCODEC_H
#include "RtpCodec.h"
class AACRtpEncoder : public RtpEncoder {
public:
AACRtpEncoder(uint32_t ui32Ssrc,
uint32_t ui32MtuSize ,
uint32_t ui32SampleRate,
uint8_t ui8PlayloadType = 97,
uint8_t ui8Interleaved = TrackAudio * 2) :
RtpEncoder(ui32Ssrc,ui32MtuSize,ui32SampleRate,ui8PlayloadType,ui8Interleaved) {
}
~AACRtpEncoder(){}
void inputFame(const Frame::Ptr &frame,bool key_pos) override;
private:
void makeAACRtp(const void *pData, unsigned int uiLen, bool bMark, uint32_t uiStamp);
private:
unsigned char m_aucSectionBuf[1600];
};
class AACRtpDecoder : public RtpCodec{
public:
AACRtpDecoder();
~AACRtpDecoder(){}
void inputRtp(const RtpPacket::Ptr &rtp, bool key_pos) override ;
private:
AdtsFrame::Ptr m_adts;
};
#endif //ZLMEDIAKIT_AACRTPCODEC_H

View File

@@ -0,0 +1,92 @@
//
// Created by xzl on 2018/10/18.
//
#include "H264RtpEncoder.h"
void H264RtpEncoder::inputFame(const Frame::Ptr &frame, bool key_pos) {
RtpCodec::inputFame(frame, key_pos);
GET_CONFIG_AND_REGISTER(uint32_t,cycleMS,Config::Rtp::kCycleMS);
auto uiStamp = frame->stamp();
auto pcData = frame->data();
auto iLen = frame->size();
uiStamp %= cycleMS;
int iSize = m_ui32MtuSize - 2;
if (iLen > iSize) { //超过MTU
const unsigned char s_e_r_Start = 0x80;
const unsigned char s_e_r_Mid = 0x00;
const unsigned char s_e_r_End = 0x40;
//获取帧头数据1byte
unsigned char naluType = *((unsigned char *) pcData) & 0x1f; //获取NALU的5bit 帧类型
unsigned char nal_ref_idc = *((unsigned char *) pcData) & 0x60; //获取NALU的2bit 帧重要程度 00 可以丢 11不能丢
//nal_ref_idc = 0x60;
//组装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 mark = false;
int nOffset = 1;
while (!mark) {
if (iLen < nOffset + iSize) { //是否拆分结束
iSize = iLen - nOffset;
mark = true;
s_e_r_type = s_e_r_End + naluType;
} else {
if (bFirst == true) {
s_e_r_type = s_e_r_Start + naluType;
bFirst = false;
} else {
s_e_r_type = s_e_r_Mid + naluType;
}
}
memcpy(m_aucSectionBuf, &f_nri_type, 1);
memcpy(m_aucSectionBuf + 1, &s_e_r_type, 1);
memcpy(m_aucSectionBuf + 2, (unsigned char *) pcData + nOffset, iSize);
nOffset += iSize;
makeH264Rtp(m_aucSectionBuf, iSize + 2, mark, uiStamp);
}
} else {
makeH264Rtp(pcData, iLen, true, uiStamp);
}
}
void H264RtpEncoder::makeH264Rtp(const void* data, unsigned int len, bool mark, uint32_t uiStamp) {
uint16_t ui16RtpLen = len + 12;
m_ui32TimeStamp = (m_ui32SampleRate / 1000) * uiStamp;
uint32_t ts = htonl(m_ui32TimeStamp);
uint16_t sq = htons(m_ui16Sequence);
uint32_t sc = htonl(m_ui32Ssrc);
auto pRtppkt = obtainRtp();
auto &rtppkt = *(pRtppkt.get());
unsigned char *pucRtp = rtppkt.payload;
pucRtp[0] = '$';
pucRtp[1] = m_ui8Interleaved;
pucRtp[2] = ui16RtpLen >> 8;
pucRtp[3] = ui16RtpLen & 0x00FF;
pucRtp[4] = 0x80;
pucRtp[5] = (mark << 7) | m_ui8PlayloadType;
memcpy(&pucRtp[6], &sq, 2);
memcpy(&pucRtp[8], &ts, 4);
//ssrc
memcpy(&pucRtp[12], &sc, 4);
//playload
memcpy(&pucRtp[16], data, len);
rtppkt.PT = m_ui8PlayloadType;
rtppkt.interleaved = m_ui8Interleaved;
rtppkt.mark = mark;
rtppkt.length = len + 16;
rtppkt.sequence = m_ui16Sequence;
rtppkt.timeStamp = m_ui32TimeStamp;
rtppkt.ssrc = m_ui32Ssrc;
rtppkt.type = TrackVideo;
rtppkt.offset = 16;
uint8_t type = ((uint8_t *) (data))[0] & 0x1F;
RtpCodec::inputRtp(pRtppkt,type == 5);
m_ui16Sequence++;
}

29
src/RTP/H264RtpEncoder.h Normal file
View File

@@ -0,0 +1,29 @@
//
// Created by xzl on 2018/10/18.
//
#ifndef ZLMEDIAKIT_H264RTPCODEC_H
#define ZLMEDIAKIT_H264RTPCODEC_H
#include "RtpCodec.h"
class H264RtpEncoder : public RtpEncoder {
public:
H264RtpEncoder(uint32_t ui32Ssrc,
uint32_t ui32MtuSize = 1400,
uint32_t ui32SampleRate = 90000,
uint8_t ui8PlayloadType = 96,
uint8_t ui8Interleaved = TrackVideo * 2) :
RtpEncoder(ui32Ssrc,ui32MtuSize,ui32SampleRate,ui8PlayloadType,ui8Interleaved) {
}
~H264RtpEncoder(){}
void inputFame(const Frame::Ptr &frame,bool key_pos) override;
private:
void makeH264Rtp(const void *pData, unsigned int uiLen, bool bMark, uint32_t uiStamp);
private:
unsigned char m_aucSectionBuf[1600];
};
#endif //ZLMEDIAKIT_H264RTPCODEC_H

5
src/RTP/RtpCodec.cpp Normal file
View File

@@ -0,0 +1,5 @@
//
// Created by xzl on 2018/10/18.
//
#include "RtpCodec.h"

112
src/RTP/RtpCodec.h Normal file
View File

@@ -0,0 +1,112 @@
//
// Created by xzl on 2018/10/18.
//
#ifndef ZLMEDIAKIT_RTPCODEC_H
#define ZLMEDIAKIT_RTPCODEC_H
#include <memory>
#include "Util/RingBuffer.h"
#include "Rtsp/Rtsp.h"
#include "Player/PlayerBase.h"
using namespace std;
using namespace ZL::Util;
using namespace ZL::Player;
class RtpCodec{
public:
typedef std::shared_ptr<RtpCodec> Ptr;
typedef RingBuffer<Frame::Ptr> FrameRing;
typedef RingBuffer<RtpPacket::Ptr> RtpRing;
RtpCodec(){
_frameRing = std::make_shared<FrameRing>();
_rtpRing = std::make_shared<RtpRing>();
}
virtual ~RtpCodec(){}
FrameRing::Ptr &getFrameRing() {
return _frameRing;
}
RtpRing::Ptr &getRtpRing() {
return _rtpRing;
}
virtual void inputFame(const Frame::Ptr &frame,bool key_pos){
_frameRing->write(frame,key_pos);
}
virtual void inputRtp(const RtpPacket::Ptr &rtp, bool key_pos){
_rtpRing->write(rtp,key_pos);
}
private:
FrameRing::Ptr _frameRing;
RtpRing::Ptr _rtpRing;
};
class RtpEncoder : public RtpCodec{
public:
typedef std::shared_ptr<RtpEncoder> Ptr;
RtpEncoder(uint32_t ui32Ssrc,
uint32_t ui32MtuSize,
uint32_t ui32SampleRate,
uint8_t ui8PlayloadType,
uint8_t ui8Interleaved) {
m_ui32Ssrc = ui32Ssrc;
m_ui32SampleRate = ui32SampleRate;
m_ui32MtuSize = ui32MtuSize;
m_ui8PlayloadType = ui8PlayloadType;
m_ui8Interleaved = ui8Interleaved;
m_rtpPool.setSize(32);
}
~RtpEncoder(){}
int getInterleaved() const {
return m_ui8Interleaved;
}
int getPlayloadType() const {
return m_ui8PlayloadType;
}
int getSampleRate() const {
return m_ui32SampleRate;
}
uint32_t getSsrc() const {
return m_ui32Ssrc;
}
uint16_t getSeqence() const {
return m_ui16Sequence;
}
uint32_t getTimestamp() const {
return m_ui32TimeStamp;
}
uint32_t getMtuSize() const {
return m_ui32MtuSize;
}
protected:
RtpPacket::Ptr obtainRtp(){
return m_rtpPool.obtain();
}
protected:
uint32_t m_ui32Ssrc;
uint32_t m_ui32SampleRate;
uint32_t m_ui32MtuSize;
uint8_t m_ui8PlayloadType;
uint8_t m_ui8Interleaved;
uint16_t m_ui16Sequence = 0;
uint32_t m_ui32TimeStamp = 0;
ResourcePool<RtpPacket> m_rtpPool;
};
#endif //ZLMEDIAKIT_RTPCODEC_H