Commit 0f6a7c16 by xiongziliang

整理代码

parent 872c0c8b
...@@ -4,10 +4,23 @@ ...@@ -4,10 +4,23 @@
#include "AACRtpCodec.h" #include "AACRtpCodec.h"
void AACRtpCodec::inputFame(const Frame::Ptr &frame, bool key_pos) { AACRtpEncoder::AACRtpEncoder(uint32_t ui32Ssrc,
uint32_t ui32MtuSize,
uint32_t ui32SampleRate,
uint8_t ui8PlayloadType,
uint8_t ui8Interleaved) :
RtpInfo(ui32Ssrc,
ui32MtuSize,
ui32SampleRate,
ui8PlayloadType,
ui8Interleaved) {
}
void AACRtpEncoder::inputFame(const Frame::Ptr &frame, bool key_pos) {
RtpCodec::inputFame(frame, key_pos); RtpCodec::inputFame(frame, 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();
auto iLen = frame->size(); auto iLen = frame->size();
...@@ -15,7 +28,7 @@ void AACRtpCodec::inputFame(const Frame::Ptr &frame, bool key_pos) { ...@@ -15,7 +28,7 @@ void AACRtpCodec::inputFame(const Frame::Ptr &frame, bool key_pos) {
uiStamp %= cycleMS; uiStamp %= cycleMS;
char *ptr = (char *) pcData; char *ptr = (char *) pcData;
int iSize = iLen; int iSize = iLen;
while (iSize > 0 ) { while (iSize > 0) {
if (iSize <= m_ui32MtuSize - 20) { if (iSize <= m_ui32MtuSize - 20) {
m_aucSectionBuf[0] = 0; m_aucSectionBuf[0] = 0;
m_aucSectionBuf[1] = 16; m_aucSectionBuf[1] = 16;
...@@ -37,7 +50,7 @@ void AACRtpCodec::inputFame(const Frame::Ptr &frame, bool key_pos) { ...@@ -37,7 +50,7 @@ void AACRtpCodec::inputFame(const Frame::Ptr &frame, bool key_pos) {
} }
} }
void AACRtpCodec::makeAACRtp(const void *pData, unsigned int uiLen, bool bMark, uint32_t uiStamp) { void AACRtpEncoder::makeAACRtp(const void *pData, unsigned int uiLen, bool bMark, uint32_t uiStamp) {
uint16_t u16RtpLen = uiLen + 12; uint16_t u16RtpLen = uiLen + 12;
m_ui32TimeStamp = (m_ui32SampleRate / 1000) * uiStamp; m_ui32TimeStamp = (m_ui32SampleRate / 1000) * uiStamp;
uint32_t ts = htonl(m_ui32TimeStamp); uint32_t ts = htonl(m_ui32TimeStamp);
...@@ -75,7 +88,7 @@ void AACRtpCodec::makeAACRtp(const void *pData, unsigned int uiLen, bool bMark, ...@@ -75,7 +88,7 @@ void AACRtpCodec::makeAACRtp(const void *pData, unsigned int uiLen, bool bMark,
///////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////
void AACRtpCodec::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, key_pos);
int length = rtppack->length - rtppack->offset; int length = rtppack->length - rtppack->offset;
...@@ -95,7 +108,7 @@ void AACRtpCodec::inputRtp(const RtpPacket::Ptr &rtppack, bool key_pos) { ...@@ -95,7 +108,7 @@ void AACRtpCodec::inputRtp(const RtpPacket::Ptr &rtppack, bool key_pos) {
} }
} }
void AACRtpCodec::onGetAdts(const AdtsFrame::Ptr &frame) { void AACRtpDecoder::onGetAdts(const AdtsFrame::Ptr &frame) {
//写入环形缓存 //写入环形缓存
RtpCodec::inputFame(frame, false); RtpCodec::inputFame(frame, false);
//从缓存池重新申请对象,防止覆盖已经写入环形缓存的对象 //从缓存池重新申请对象,防止覆盖已经写入环形缓存的对象
...@@ -103,3 +116,4 @@ void AACRtpCodec::onGetAdts(const AdtsFrame::Ptr &frame) { ...@@ -103,3 +116,4 @@ void AACRtpCodec::onGetAdts(const AdtsFrame::Ptr &frame) {
m_adts->aac_frame_length = 7; m_adts->aac_frame_length = 7;
} }
...@@ -8,30 +8,44 @@ ...@@ -8,30 +8,44 @@
#include "RtpCodec.h" #include "RtpCodec.h"
class AACRtpCodec : public RtpEncoder { class AACRtpDecoder : public RtpCodec {
public: public:
AACRtpCodec(uint32_t ui32Ssrc, AACRtpDecoder() {
uint32_t ui32MtuSize ,
uint32_t ui32SampleRate,
uint8_t ui8PlayloadType = 97,
uint8_t ui8Interleaved = TrackAudio * 2) :
RtpEncoder(ui32Ssrc,ui32MtuSize,ui32SampleRate,ui8PlayloadType,ui8Interleaved) {
m_framePool.setSize(32); m_framePool.setSize(32);
m_adts = m_framePool.obtain(); m_adts = m_framePool.obtain();
} }
~AACRtpCodec(){}
void inputFame(const Frame::Ptr &frame,bool key_pos) override; ~AACRtpDecoder() {}
void inputRtp(const RtpPacket::Ptr &rtp, bool key_pos) override ;
void inputRtp(const RtpPacket::Ptr &rtp, bool key_pos) override;
private: private:
void makeAACRtp(const void *pData, unsigned int uiLen, bool bMark, uint32_t uiStamp);
void onGetAdts(const AdtsFrame::Ptr &frame); void onGetAdts(const AdtsFrame::Ptr &frame);
private: private:
unsigned char m_aucSectionBuf[1600];
AdtsFrame::Ptr m_adts; AdtsFrame::Ptr m_adts;
ResourcePool<AdtsFrame> m_framePool; ResourcePool<AdtsFrame> m_framePool;
}; };
class AACRtpEncoder : public RtpInfo, public RtpCodec {
public:
AACRtpEncoder(uint32_t ui32Ssrc,
uint32_t ui32MtuSize,
uint32_t ui32SampleRate,
uint8_t ui8PlayloadType = 97,
uint8_t ui8Interleaved = TrackAudio * 2);
~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];
};
#endif //ZLMEDIAKIT_AACRTPCODEC_H #endif //ZLMEDIAKIT_AACRTPCODEC_H
...@@ -4,7 +4,107 @@ ...@@ -4,7 +4,107 @@
#include "H264RtpCodec.h" #include "H264RtpCodec.h"
void H264RtpCodec::inputFame(const Frame::Ptr &frame, bool key_pos) { void H264RtpDecoder::inputRtp(const RtpPacket::Ptr &rtp, bool key_pos) {
RtpCodec::inputRtp(rtp, decodeRtp(rtp,key_pos));
}
bool H264RtpDecoder::decodeRtp(const RtpPacket::Ptr &rtppack, bool key_pos) {
/**
* h264帧类型
* Type==1:P/B frame
* Type==5:IDR frame
* Type==6:SEI frame
* Type==7:SPS frame
* Type==8:PPS frame
*/
const uint8_t *frame = (uint8_t *) rtppack->payload + rtppack->offset;
int length = rtppack->length - rtppack->offset;
NALU nal;
MakeNalu(*frame, nal);
if (nal.type >= 0 && nal.type < 24) {
//a full frame
m_h264frame->buffer.assign("\x0\x0\x0\x1", 4);
m_h264frame->buffer.append((char *)frame, length);
m_h264frame->type = nal.type;
m_h264frame->timeStamp = rtppack->timeStamp / 90;
m_h264frame->sequence = rtppack->sequence;
auto isIDR = m_h264frame->type == 5;
onGetH264(m_h264frame);
return (isIDR); //i frame
}
if (nal.type == 28) {
//FU-A
FU fu;
MakeFU(frame[1], fu);
if (fu.S == 1) {
//FU-A start
char tmp = (nal.forbidden_zero_bit << 7 | nal.nal_ref_idc << 5 | fu.type);
m_h264frame->buffer.assign("\x0\x0\x0\x1", 4);
m_h264frame->buffer.push_back(tmp);
m_h264frame->buffer.append((char *)frame + 2, length - 2);
m_h264frame->type = fu.type;
m_h264frame->timeStamp = rtppack->timeStamp / 90;
m_h264frame->sequence = rtppack->sequence;
return (m_h264frame->type == 5); //i frame
}
if (rtppack->sequence != (uint16_t)(m_h264frame->sequence + 1)) {
m_h264frame->buffer.clear();
WarnL << "丢包,帧废弃:" << rtppack->sequence << "," << m_h264frame->sequence;
return false;
}
m_h264frame->sequence = rtppack->sequence;
if (fu.E == 1) {
//FU-A end
m_h264frame->buffer.append((char *)frame + 2, length - 2);
m_h264frame->timeStamp = rtppack->timeStamp / 90;
auto isIDR = m_h264frame->type == 5;
onGetH264(m_h264frame);
return isIDR;
}
//FU-A mid
m_h264frame->buffer.append((char *)frame + 2, length - 2);
return false;
}
WarnL << "不支持的rtp类型:" << nal.type << " " << rtppack->sequence;
return false;
// 29 FU-B 单NAL单元B模式
// 24 STAP-A 单一时间的组合包
// 25 STAP-B 单一时间的组合包
// 26 MTAP16 多个时间的组合包
// 27 MTAP24 多个时间的组合包
// 0 udef
// 30 udef
// 31 udef
}
void H264RtpDecoder::onGetH264(const H264Frame::Ptr &frame) {
//写入环形缓存
RtpCodec::inputFame(frame,frame->type == 5);
//从缓存池重新申请对象,防止覆盖已经写入环形缓存的对象
m_h264frame = m_framePool.obtain();
m_h264frame->buffer.clear();
}
////////////////////////////////////////////////////////////////////////
H264RtpEncoder::H264RtpEncoder(uint32_t ui32Ssrc,
uint32_t ui32MtuSize,
uint32_t ui32SampleRate,
uint8_t ui8PlayloadType,
uint8_t ui8Interleaved) :
RtpInfo(ui32Ssrc,
ui32MtuSize,
ui32SampleRate,
ui8PlayloadType,
ui8Interleaved) {
}
void H264RtpEncoder::inputFame(const Frame::Ptr &frame, bool key_pos) {
RtpCodec::inputFame(frame, key_pos); RtpCodec::inputFame(frame, key_pos);
GET_CONFIG_AND_REGISTER(uint32_t,cycleMS,Config::Rtp::kCycleMS); GET_CONFIG_AND_REGISTER(uint32_t,cycleMS,Config::Rtp::kCycleMS);
...@@ -53,7 +153,7 @@ void H264RtpCodec::inputFame(const Frame::Ptr &frame, bool key_pos) { ...@@ -53,7 +153,7 @@ void H264RtpCodec::inputFame(const Frame::Ptr &frame, bool key_pos) {
} }
} }
void H264RtpCodec::makeH264Rtp(const void* data, unsigned int len, bool mark, uint32_t uiStamp) { void H264RtpEncoder::makeH264Rtp(const void* data, unsigned int len, bool mark, uint32_t uiStamp) {
uint16_t ui16RtpLen = len + 12; uint16_t ui16RtpLen = len + 12;
m_ui32TimeStamp = (m_ui32SampleRate / 1000) * uiStamp; m_ui32TimeStamp = (m_ui32SampleRate / 1000) * uiStamp;
uint32_t ts = htonl(m_ui32TimeStamp); uint32_t ts = htonl(m_ui32TimeStamp);
...@@ -89,92 +189,3 @@ void H264RtpCodec::makeH264Rtp(const void* data, unsigned int len, bool mark, ui ...@@ -89,92 +189,3 @@ void H264RtpCodec::makeH264Rtp(const void* data, unsigned int len, bool mark, ui
RtpCodec::inputRtp(rtppkt,type == 5); RtpCodec::inputRtp(rtppkt,type == 5);
m_ui16Sequence++; m_ui16Sequence++;
} }
\ No newline at end of file
////////////////////////////////////////////////////////////////////////
void H264RtpCodec::inputRtp(const RtpPacket::Ptr &rtp, bool key_pos) {
RtpCodec::inputRtp(rtp, decodeRtp(rtp,key_pos));
}
bool H264RtpCodec::decodeRtp(const RtpPacket::Ptr &rtppack, bool key_pos) {
/**
* h264帧类型
* Type==1:P/B frame
* Type==5:IDR frame
* Type==6:SEI frame
* Type==7:SPS frame
* Type==8:PPS frame
*/
const uint8_t *frame = (uint8_t *) rtppack->payload + rtppack->offset;
int length = rtppack->length - rtppack->offset;
NALU nal;
MakeNalu(*frame, nal);
if (nal.type >= 0 && nal.type < 24) {
//a full frame
m_h264frame->buffer.assign("\x0\x0\x0\x1", 4);
m_h264frame->buffer.append((char *)frame, length);
m_h264frame->type = nal.type;
m_h264frame->timeStamp = rtppack->timeStamp / 90;
m_h264frame->sequence = rtppack->sequence;
auto isIDR = m_h264frame->type == 5;
onGetH264(m_h264frame);
return (isIDR); //i frame
}
if (nal.type == 28) {
//FU-A
FU fu;
MakeFU(frame[1], fu);
if (fu.S == 1) {
//FU-A start
char tmp = (nal.forbidden_zero_bit << 7 | nal.nal_ref_idc << 5 | fu.type);
m_h264frame->buffer.assign("\x0\x0\x0\x1", 4);
m_h264frame->buffer.push_back(tmp);
m_h264frame->buffer.append((char *)frame + 2, length - 2);
m_h264frame->type = fu.type;
m_h264frame->timeStamp = rtppack->timeStamp / 90;
m_h264frame->sequence = rtppack->sequence;
return (m_h264frame->type == 5); //i frame
}
if (rtppack->sequence != (uint16_t)(m_h264frame->sequence + 1)) {
m_h264frame->buffer.clear();
WarnL << "丢包,帧废弃:" << rtppack->sequence << "," << m_h264frame->sequence;
return false;
}
m_h264frame->sequence = rtppack->sequence;
if (fu.E == 1) {
//FU-A end
m_h264frame->buffer.append((char *)frame + 2, length - 2);
m_h264frame->timeStamp = rtppack->timeStamp / 90;
auto isIDR = m_h264frame->type == 5;
onGetH264(m_h264frame);
return isIDR;
}
//FU-A mid
m_h264frame->buffer.append((char *)frame + 2, length - 2);
return false;
}
WarnL << "不支持的rtp类型:" << nal.type << " " << rtppack->sequence;
return false;
// 29 FU-B 单NAL单元B模式
// 24 STAP-A 单一时间的组合包
// 25 STAP-B 单一时间的组合包
// 26 MTAP16 多个时间的组合包
// 27 MTAP24 多个时间的组合包
// 0 udef
// 30 udef
// 31 udef
}
void H264RtpCodec::onGetH264(const H264Frame::Ptr &frame) {
//写入环形缓存
RtpCodec::inputFame(frame,frame->type == 5);
//从缓存池重新申请对象,防止覆盖已经写入环形缓存的对象
m_h264frame = m_framePool.obtain();
m_h264frame->buffer.clear();
}
...@@ -10,30 +10,45 @@ ...@@ -10,30 +10,45 @@
using namespace ZL::Util; using namespace ZL::Util;
class H264RtpCodec : public RtpEncoder { class H264RtpDecoder : public RtpCodec {
public: public:
H264RtpCodec(uint32_t ui32Ssrc, H264RtpDecoder() {
uint32_t ui32MtuSize = 1400,
uint32_t ui32SampleRate = 90000,
uint8_t ui8PlayloadType = 96,
uint8_t ui8Interleaved = TrackVideo * 2) :
RtpEncoder(ui32Ssrc,ui32MtuSize,ui32SampleRate,ui8PlayloadType,ui8Interleaved) {
m_framePool.setSize(32); m_framePool.setSize(32);
m_h264frame = m_framePool.obtain(); m_h264frame = m_framePool.obtain();
} }
~H264RtpCodec(){}
void inputFame(const Frame::Ptr &frame,bool key_pos) override; ~H264RtpDecoder() {}
void inputRtp(const RtpPacket::Ptr &rtp, bool key_pos) override ;
void inputRtp(const RtpPacket::Ptr &rtp, bool key_pos) override;
private: private:
void makeH264Rtp(const void *pData, unsigned int uiLen, bool bMark, uint32_t uiStamp);
bool decodeRtp(const RtpPacket::Ptr &rtp, bool key_pos); bool decodeRtp(const RtpPacket::Ptr &rtp, bool key_pos);
void onGetH264(const H264Frame::Ptr &frame); void onGetH264(const H264Frame::Ptr &frame);
private: private:
unsigned char m_aucSectionBuf[1600];
H264Frame::Ptr m_h264frame; H264Frame::Ptr m_h264frame;
ResourcePool<H264Frame> m_framePool; ResourcePool<H264Frame> m_framePool;
}; };
class H264RtpEncoder : public RtpInfo, public RtpCodec {
public:
H264RtpEncoder(uint32_t ui32Ssrc,
uint32_t ui32MtuSize = 1400,
uint32_t ui32SampleRate = 90000,
uint8_t ui8PlayloadType = 96,
uint8_t ui8Interleaved = TrackVideo * 2);
~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 #endif //ZLMEDIAKIT_H264RTPCODEC_H
...@@ -21,8 +21,9 @@ public: ...@@ -21,8 +21,9 @@ public:
typedef RingBuffer<RtpPacket::Ptr> RtpRing; typedef RingBuffer<RtpPacket::Ptr> RtpRing;
RtpCodec(){ RtpCodec(){
_frameRing = std::make_shared<FrameRing>(); //禁用缓存
_rtpRing = std::make_shared<RtpRing>(); _frameRing = std::make_shared<FrameRing>(1);
_rtpRing = std::make_shared<RtpRing>(1);
} }
virtual ~RtpCodec(){} virtual ~RtpCodec(){}
...@@ -45,11 +46,11 @@ private: ...@@ -45,11 +46,11 @@ private:
}; };
class RtpEncoder : public RtpCodec{ class RtpInfo{
public: public:
typedef std::shared_ptr<RtpEncoder> Ptr; typedef std::shared_ptr<RtpInfo> Ptr;
RtpEncoder(uint32_t ui32Ssrc, RtpInfo(uint32_t ui32Ssrc,
uint32_t ui32MtuSize, uint32_t ui32MtuSize,
uint32_t ui32SampleRate, uint32_t ui32SampleRate,
uint8_t ui8PlayloadType, uint8_t ui8PlayloadType,
...@@ -62,7 +63,7 @@ public: ...@@ -62,7 +63,7 @@ public:
m_rtpPool.setSize(32); m_rtpPool.setSize(32);
} }
~RtpEncoder(){} virtual ~RtpInfo(){}
int getInterleaved() const { int getInterleaved() const {
return m_ui8Interleaved; return m_ui8Interleaved;
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论