整理代码 添加注释

This commit is contained in:
xiongziliang 2018-10-21 22:24:24 +08:00
parent 0f6a7c1656
commit 393f123e28
5 changed files with 116 additions and 44 deletions

View File

@ -13,7 +13,16 @@ class Frame : public Buffer {
public: public:
typedef std::shared_ptr<Frame> Ptr; typedef std::shared_ptr<Frame> Ptr;
virtual ~Frame(){} virtual ~Frame(){}
/**
*
*/
virtual uint32_t stamp() = 0; virtual uint32_t stamp() = 0;
/**
* 2640x00 00 00 01,4
* aac前缀则为7个字节
*/
virtual uint32_t prefixSize() = 0;
}; };
class H264Frame : public Frame { class H264Frame : public Frame {
@ -29,15 +38,17 @@ public:
uint32_t stamp() override { uint32_t stamp() override {
return timeStamp; return timeStamp;
} }
uint32_t prefixSize() override{
return iPrefixSize;
}
public: public:
uint16_t sequence; uint16_t sequence;
uint32_t timeStamp; uint32_t timeStamp;
unsigned char type; unsigned char type;
string buffer; string buffer;
uint32_t iPrefixSize = 4;
}; };
//ADTS 头中相对有用的信息 采样率、声道数、帧长度 //ADTS 头中相对有用的信息 采样率、声道数、帧长度
class AdtsFrame : public Frame { class AdtsFrame : public Frame {
public: public:
@ -52,6 +63,9 @@ public:
uint32_t stamp() override { uint32_t stamp() override {
return timeStamp; return timeStamp;
} }
uint32_t prefixSize() override{
return iPrefixSize;
}
public: public:
unsigned int syncword; //12 bslbf 同步字The bit string 1111 1111 1111说明一个ADTS帧的开始 unsigned int syncword; //12 bslbf 同步字The bit string 1111 1111 1111说明一个ADTS帧的开始
unsigned int id; //1 bslbf MPEG 标示符, 设置为1 unsigned int id; //1 bslbf MPEG 标示符, 设置为1
@ -75,6 +89,7 @@ public:
unsigned char buffer[2 * 1024 + 7]; unsigned char buffer[2 * 1024 + 7];
uint16_t sequence; uint16_t sequence;
uint32_t timeStamp; uint32_t timeStamp;
uint32_t iPrefixSize = 4;
} ; } ;

View File

@ -18,12 +18,12 @@ AACRtpEncoder::AACRtpEncoder(uint32_t ui32Ssrc,
} }
void AACRtpEncoder::inputFame(const Frame::Ptr &frame, bool key_pos) { void AACRtpEncoder::inputFame(const Frame::Ptr &frame, bool key_pos) {
RtpCodec::inputFame(frame, key_pos); RtpCodec::inputFame(frame, false);
GET_CONFIG_AND_REGISTER(uint32_t, cycleMS, Config::Rtp::kCycleMS); GET_CONFIG_AND_REGISTER(uint32_t, cycleMS, Config::Rtp::kCycleMS);
auto uiStamp = frame->stamp(); auto uiStamp = frame->stamp();
auto pcData = frame->data(); auto pcData = frame->data() + frame->prefixSize();
auto iLen = frame->size(); auto iLen = frame->size() - frame->prefixSize();
uiStamp %= cycleMS; uiStamp %= cycleMS;
char *ptr = (char *) pcData; char *ptr = (char *) pcData;
@ -87,9 +87,21 @@ void AACRtpEncoder::makeAACRtp(const void *pData, unsigned int uiLen, bool bMark
} }
///////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////
AACRtpDecoder::AACRtpDecoder(uint32_t ui32SampleRate) {
m_adts = obtainFrame();
m_sampleRate = ui32SampleRate;
}
AdtsFrame::Ptr AACRtpDecoder::obtainFrame() {
//从缓存池重新申请对象,防止覆盖已经写入环形缓存的对象
auto frame = m_framePool.obtain();
frame->aac_frame_length = 7;
frame->iPrefixSize = 7;
return frame;
}
void AACRtpDecoder::inputRtp(const RtpPacket::Ptr &rtppack, bool key_pos) { void AACRtpDecoder::inputRtp(const RtpPacket::Ptr &rtppack, bool key_pos) {
RtpCodec::inputRtp(rtppack, key_pos); RtpCodec::inputRtp(rtppack, false);
int length = rtppack->length - rtppack->offset; int length = rtppack->length - rtppack->offset;
if (m_adts->aac_frame_length + length - 4 > sizeof(AdtsFrame::buffer)) { if (m_adts->aac_frame_length + length - 4 > sizeof(AdtsFrame::buffer)) {
@ -102,7 +114,7 @@ void AACRtpDecoder::inputRtp(const RtpPacket::Ptr &rtppack, bool key_pos) {
if (rtppack->mark == true) { if (rtppack->mark == true) {
m_adts->sequence = rtppack->sequence; m_adts->sequence = rtppack->sequence;
//todo(xzl) 此处完成时间戳转换 //todo(xzl) 此处完成时间戳转换
// m_adts->timeStamp = rtppack->timeStamp * (1000.0 / m_iSampleRate); m_adts->timeStamp = rtppack->timeStamp * (1000.0 / m_sampleRate);
writeAdtsHeader(*m_adts, m_adts->buffer); writeAdtsHeader(*m_adts, m_adts->buffer);
onGetAdts(m_adts); onGetAdts(m_adts);
} }
@ -111,9 +123,9 @@ void AACRtpDecoder::inputRtp(const RtpPacket::Ptr &rtppack, bool key_pos) {
void AACRtpDecoder::onGetAdts(const AdtsFrame::Ptr &frame) { void AACRtpDecoder::onGetAdts(const AdtsFrame::Ptr &frame) {
//写入环形缓存 //写入环形缓存
RtpCodec::inputFame(frame, false); RtpCodec::inputFame(frame, false);
//从缓存池重新申请对象,防止覆盖已经写入环形缓存的对象 m_adts = obtainFrame();
m_adts = m_framePool.obtain();
m_adts->aac_frame_length = 7;
} }

View File

@ -5,44 +5,62 @@
#ifndef ZLMEDIAKIT_AACRTPCODEC_H #ifndef ZLMEDIAKIT_AACRTPCODEC_H
#define ZLMEDIAKIT_AACRTPCODEC_H #define ZLMEDIAKIT_AACRTPCODEC_H
#include "RtpCodec.h" #include "RtpCodec.h"
/**
* aac rtp转adts类
*/
class AACRtpDecoder : public RtpCodec { class AACRtpDecoder : public RtpCodec {
public: public:
AACRtpDecoder() { /**
m_framePool.setSize(32); * @param ui32SampleRate
m_adts = m_framePool.obtain(); */
} AACRtpDecoder(uint32_t ui32SampleRate);
~AACRtpDecoder() {} ~AACRtpDecoder() {}
void inputRtp(const RtpPacket::Ptr &rtp, bool key_pos) override; /**
* rtp并解码
* @param rtp rtp数据包
* @param key_pos false,
*/
void inputRtp(const RtpPacket::Ptr &rtp, bool key_pos = false) override;
private: private:
void onGetAdts(const AdtsFrame::Ptr &frame); void onGetAdts(const AdtsFrame::Ptr &frame);
AdtsFrame::Ptr obtainFrame();
private: private:
AdtsFrame::Ptr m_adts; AdtsFrame::Ptr m_adts;
ResourcePool<AdtsFrame> m_framePool; ResourcePool<AdtsFrame> m_framePool;
uint32_t m_sampleRate;
}; };
/**
* aac adts转rtp类
*/
class AACRtpEncoder : public RtpInfo, public RtpCodec { class AACRtpEncoder : public RtpInfo, public RtpCodec {
public: public:
/**
* @param ui32Ssrc ssrc
* @param ui32MtuSize mtu
* @param ui32SampleRate
* @param ui8PlayloadType pt类型
* @param ui8Interleaved rtsp interleaved
*/
AACRtpEncoder(uint32_t ui32Ssrc, AACRtpEncoder(uint32_t ui32Ssrc,
uint32_t ui32MtuSize, uint32_t ui32MtuSize,
uint32_t ui32SampleRate, uint32_t ui32SampleRate,
uint8_t ui8PlayloadType = 97, uint8_t ui8PlayloadType = 97,
uint8_t ui8Interleaved = TrackAudio * 2); uint8_t ui8Interleaved = TrackAudio * 2);
~AACRtpEncoder() {} ~AACRtpEncoder() {}
void inputFame(const Frame::Ptr &frame, bool key_pos) override; /**
* aac dats头
* @param frame dats头的aac数据
* @param key_pos false,
*/
void inputFame(const Frame::Ptr &frame, bool key_pos = false) override;
private: private:
void makeAACRtp(const void *pData, unsigned int uiLen, bool bMark, uint32_t uiStamp); void makeAACRtp(const void *pData, unsigned int uiLen, bool bMark, uint32_t uiStamp);
private: private:
unsigned char m_aucSectionBuf[1600]; unsigned char m_aucSectionBuf[1600];
}; };

View File

@ -4,11 +4,24 @@
#include "H264RtpCodec.h" #include "H264RtpCodec.h"
void H264RtpDecoder::inputRtp(const RtpPacket::Ptr &rtp, bool key_pos) {
RtpCodec::inputRtp(rtp, decodeRtp(rtp,key_pos)); H264RtpDecoder::H264RtpDecoder() {
m_h264frame = obtainFrame();
} }
bool H264RtpDecoder::decodeRtp(const RtpPacket::Ptr &rtppack, bool key_pos) { H264Frame::Ptr H264RtpDecoder::obtainFrame() {
//从缓存池重新申请对象,防止覆盖已经写入环形缓存的对象
auto frame = m_framePool.obtain();
frame->buffer.clear();
frame->iPrefixSize = 4;
return frame;
}
void H264RtpDecoder::inputRtp(const RtpPacket::Ptr &rtp, bool key_pos) {
RtpCodec::inputRtp(rtp, decodeRtp(rtp));
}
bool H264RtpDecoder::decodeRtp(const RtpPacket::Ptr &rtppack) {
/** /**
* h264帧类型 * h264帧类型
* Type==1:P/B frame * Type==1:P/B frame
@ -85,11 +98,10 @@ bool H264RtpDecoder::decodeRtp(const RtpPacket::Ptr &rtppack, bool key_pos) {
void H264RtpDecoder::onGetH264(const H264Frame::Ptr &frame) { void H264RtpDecoder::onGetH264(const H264Frame::Ptr &frame) {
//写入环形缓存 //写入环形缓存
RtpCodec::inputFame(frame,frame->type == 5); RtpCodec::inputFame(frame,frame->type == 5);
//从缓存池重新申请对象,防止覆盖已经写入环形缓存的对象 m_h264frame = obtainFrame();
m_h264frame = m_framePool.obtain();
m_h264frame->buffer.clear();
} }
//////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////
H264RtpEncoder::H264RtpEncoder(uint32_t ui32Ssrc, H264RtpEncoder::H264RtpEncoder(uint32_t ui32Ssrc,
@ -109,8 +121,8 @@ void H264RtpEncoder::inputFame(const Frame::Ptr &frame, bool key_pos) {
GET_CONFIG_AND_REGISTER(uint32_t,cycleMS,Config::Rtp::kCycleMS); GET_CONFIG_AND_REGISTER(uint32_t,cycleMS,Config::Rtp::kCycleMS);
auto uiStamp = frame->stamp(); auto uiStamp = frame->stamp();
auto pcData = frame->data(); auto pcData = frame->data() + frame->prefixSize();
auto iLen = frame->size(); auto iLen = frame->size() - frame->prefixSize();
uiStamp %= cycleMS; uiStamp %= cycleMS;
int iSize = m_ui32MtuSize - 2; int iSize = m_ui32MtuSize - 2;

View File

@ -10,42 +10,57 @@
using namespace ZL::Util; using namespace ZL::Util;
/**
* h264 rtp解码类
*/
class H264RtpDecoder : public RtpCodec { class H264RtpDecoder : public RtpCodec {
public: public:
H264RtpDecoder() { H264RtpDecoder();
m_framePool.setSize(32);
m_h264frame = m_framePool.obtain();
}
~H264RtpDecoder() {} ~H264RtpDecoder() {}
void inputRtp(const RtpPacket::Ptr &rtp, bool key_pos) override; /**
* 264 rtp包
* @param rtp rtp包
* @param key_pos
*/
void inputRtp(const RtpPacket::Ptr &rtp, bool key_pos = true) override;
private: private:
bool decodeRtp(const RtpPacket::Ptr &rtp, bool key_pos); bool decodeRtp(const RtpPacket::Ptr &rtp);
void onGetH264(const H264Frame::Ptr &frame); void onGetH264(const H264Frame::Ptr &frame);
H264Frame::Ptr obtainFrame();
private: private:
H264Frame::Ptr m_h264frame; H264Frame::Ptr m_h264frame;
ResourcePool<H264Frame> m_framePool; ResourcePool<H264Frame> m_framePool;
}; };
/**
* 264 rtp打包类
*/
class H264RtpEncoder : public RtpInfo, public RtpCodec { class H264RtpEncoder : public RtpInfo, public RtpCodec {
public: public:
/**
* @param ui32Ssrc ssrc
* @param ui32MtuSize mtu大小
* @param ui32SampleRate 90000
* @param ui8PlayloadType pt类型
* @param ui8Interleaved rtsp interleaved
*/
H264RtpEncoder(uint32_t ui32Ssrc, H264RtpEncoder(uint32_t ui32Ssrc,
uint32_t ui32MtuSize = 1400, uint32_t ui32MtuSize = 1400,
uint32_t ui32SampleRate = 90000, uint32_t ui32SampleRate = 90000,
uint8_t ui8PlayloadType = 96, uint8_t ui8PlayloadType = 96,
uint8_t ui8Interleaved = TrackVideo * 2); uint8_t ui8Interleaved = TrackVideo * 2);
~H264RtpEncoder() {} ~H264RtpEncoder() {}
/**
* 264
* @param frame
* @param key_pos
*/
void inputFame(const Frame::Ptr &frame, bool key_pos) override; void inputFame(const Frame::Ptr &frame, bool key_pos) override;
private: private:
void makeH264Rtp(const void *pData, unsigned int uiLen, bool bMark, uint32_t uiStamp); void makeH264Rtp(const void *pData, unsigned int uiLen, bool bMark, uint32_t uiStamp);
private: private:
unsigned char m_aucSectionBuf[1600]; unsigned char m_aucSectionBuf[1600];
}; };