完成部分265 rtp编码功能

This commit is contained in:
xiongziliang 2018-10-30 21:05:48 +08:00
parent 86c37b8c49
commit 5f511eb295
4 changed files with 90 additions and 29 deletions

View File

@ -48,6 +48,19 @@ Sdp::Ptr Factory::getSdpByTrack(const Track::Ptr &track) {
return std::make_shared<H264Sdp>(h264Track->getSps(),h264Track->getPps()); return std::make_shared<H264Sdp>(h264Track->getSps(),h264Track->getPps());
} }
case CodecH265:{
H265Track::Ptr h265Track = dynamic_pointer_cast<H265Track>(track);
if(!h265Track){
WarnL << "该Track不是H265Track类型";
return nullptr;
}
if(!h265Track->ready()){
WarnL << "该Track未准备好";
return nullptr;
}
return std::make_shared<H265Sdp>(h265Track->getSps(),h265Track->getPps());
}
case CodecAAC:{ case CodecAAC:{
AACTrack::Ptr aacTrack = dynamic_pointer_cast<AACTrack>(track); AACTrack::Ptr aacTrack = dynamic_pointer_cast<AACTrack>(track);
if(!aacTrack){ if(!aacTrack){
@ -115,7 +128,9 @@ Track::Ptr Factory::getTrackBySdp(const SdpTrack::Ptr &track) {
if (3 != sscanf(track->_fmtp.c_str(), "%d sprop-sps=%127[^;]; sprop-pps=%127[^;]", &pt, sprop_sps, sprop_pps)) { if (3 != sscanf(track->_fmtp.c_str(), "%d sprop-sps=%127[^;]; sprop-pps=%127[^;]", &pt, sprop_sps, sprop_pps)) {
return std::make_shared<H265Track>(); return std::make_shared<H265Track>();
} }
return std::make_shared<H265Track>("",sprop_sps,sprop_pps,0,0,0); auto sps = decodeBase64(sprop_sps);
auto pps = decodeBase64(sprop_pps);
return std::make_shared<H265Track>("",sps,pps,0,0,0);
} }

View File

@ -29,6 +29,7 @@
#include "Frame.h" #include "Frame.h"
#include "Track.h" #include "Track.h"
#include "RtspMuxer/RtspSdp.h"
namespace mediakit { namespace mediakit {
@ -228,7 +229,7 @@ public:
} }
bool ready() override { bool ready() override {
return !_sps.empty() && !_pps.empty() && !_vps.empty(); return !_sps.empty() && !_pps.empty();
} }
@ -251,7 +252,7 @@ public:
_vpsFrame = insertFrame; _vpsFrame = insertFrame;
} }
_vpsFrame->timeStamp = frame->stamp(); _vpsFrame->timeStamp = frame->stamp();
VideoTrack::inputFrame(_vpsFrame); // VideoTrack::inputFrame(_vpsFrame);
} }
if (!_sps.empty()) { if (!_sps.empty()) {
if (!_spsFrame) { if (!_spsFrame) {
@ -264,7 +265,7 @@ public:
_spsFrame = insertFrame; _spsFrame = insertFrame;
} }
_spsFrame->timeStamp = frame->stamp(); _spsFrame->timeStamp = frame->stamp();
VideoTrack::inputFrame(_spsFrame); // VideoTrack::inputFrame(_spsFrame);
} }
if (!_pps.empty()) { if (!_pps.empty()) {
@ -278,7 +279,7 @@ public:
_ppsFrame = insertFrame; _ppsFrame = insertFrame;
} }
_ppsFrame->timeStamp = frame->stamp(); _ppsFrame->timeStamp = frame->stamp();
VideoTrack::inputFrame(_ppsFrame); //VideoTrack::inputFrame(_ppsFrame);
} }
VideoTrack::inputFrame(frame); VideoTrack::inputFrame(frame);
return; return;
@ -289,17 +290,20 @@ public:
case H265Frame::NAL_VPS: { case H265Frame::NAL_VPS: {
//vps //vps
_vps = string(frame->data() + frame->prefixSize(), frame->size() - frame->prefixSize()); _vps = string(frame->data() + frame->prefixSize(), frame->size() - frame->prefixSize());
VideoTrack::inputFrame(frame);
} }
break; break;
case H265Frame::NAL_SPS: { case H265Frame::NAL_SPS: {
//sps //sps
_sps = string(frame->data() + frame->prefixSize(), frame->size() - frame->prefixSize()); _sps = string(frame->data() + frame->prefixSize(), frame->size() - frame->prefixSize());
VideoTrack::inputFrame(frame);
} }
break; break;
case H265Frame::NAL_PPS: { case H265Frame::NAL_PPS: {
//pps //pps
_pps = string(frame->data() + frame->prefixSize(), frame->size() - frame->prefixSize()); _pps = string(frame->data() + frame->prefixSize(), frame->size() - frame->prefixSize());
VideoTrack::inputFrame(frame);
} }
break; break;
@ -341,6 +345,51 @@ private:
}; };
/**
* h265类型sdp
*/
class H265Sdp : public Sdp {
public:
/**
*
* @param sps 265 sps,0x00000001
* @param pps 265 pps,0x00000001
* @param playload_type rtp playload type 96
* @param bitrate
*/
H265Sdp(const string &strSPS,
const string &strPPS,
int playload_type = 96,
int bitrate = 4000) : Sdp(90000,playload_type) {
//视频通道
_printer << "m=video 0 RTP/AVP " << playload_type << "\r\n";
_printer << "b=AS:" << bitrate << "\r\n";
_printer << "a=rtpmap:" << playload_type << " H265/" << 90000 << "\r\n";
_printer << "a=fmtp:" << playload_type << " sprop-sps=";
_printer << encodeBase64(strSPS) << "; ";
_printer << "sprop-pps=";
_printer << encodeBase64(strPPS) << "\r\n";
_printer << "a=control:trackID=" << getTrackType() << "\r\n";
}
string getSdp() const override {
return _printer;
}
TrackType getTrackType() const override {
return TrackVideo;
}
CodecId getCodecId() const override {
return CodecH265;
}
private:
_StrPrinter _printer;
};
}//namespace mediakit }//namespace mediakit

View File

@ -146,6 +146,7 @@ bool H265RtpDecoder::decodeRtp(const RtpPacket::Ptr &rtppack) {
void H265RtpDecoder::onGetH265(const H265Frame::Ptr &frame) { void H265RtpDecoder::onGetH265(const H265Frame::Ptr &frame) {
//写入环形缓存 //写入环形缓存
auto lastSeq = _h265frame->sequence; auto lastSeq = _h265frame->sequence;
DebugL << (int)frame->type;
RtpCodec::inputFrame(frame); RtpCodec::inputFrame(frame);
_h265frame = obtainFrame(); _h265frame = obtainFrame();
_h265frame->sequence = lastSeq; _h265frame->sequence = lastSeq;
@ -170,49 +171,47 @@ void H265RtpEncoder::inputFrame(const Frame::Ptr &frame) {
RtpCodec::inputFrame(frame); RtpCodec::inputFrame(frame);
GET_CONFIG_AND_REGISTER(uint32_t,cycleMS,Rtp::kCycleMS); GET_CONFIG_AND_REGISTER(uint32_t,cycleMS,Rtp::kCycleMS);
auto pcData = frame->data() + frame->prefixSize(); uint8_t *pcData = (uint8_t*)frame->data() + frame->prefixSize();
auto uiStamp = frame->stamp(); auto uiStamp = frame->stamp();
auto iLen = frame->size() - frame->prefixSize(); auto iLen = frame->size() - frame->prefixSize();
uiStamp %= cycleMS;
int iSize = _ui32MtuSize - 3;
if (iLen > iSize) { //超过MTU
const unsigned char s_e_Start = 1 << 7;
const unsigned char s_e_Mid = 0x00;
const unsigned char s_e_End = 1 << 6;
//获取帧头数据1byte
unsigned char naluType = H265_TYPE(pcData[0]); //获取NALU的5bit 帧类型 unsigned char naluType = H265_TYPE(pcData[0]); //获取NALU的5bit 帧类型
unsigned char s_e_type = naluType; uiStamp %= cycleMS;
WarnL << (int)naluType;
int maxSize = _ui32MtuSize - 3;
if (iLen > maxSize) { //超过MTU
//获取帧头数据1byte
unsigned char s_e_type;
bool bFirst = true; bool bFirst = true;
bool mark = false; bool mark = false;
int nOffset = 2; int nOffset = 2;
while (!mark) { while (!mark) {
if (iLen < nOffset + iSize) { //是否拆分结束 if (iLen < nOffset + maxSize) { //是否拆分结束
iSize = iLen - nOffset; maxSize = iLen - nOffset;
mark = true; mark = true;
s_e_type = s_e_End + naluType; s_e_type = 1 << 7 | naluType;
} else { } else {
if (bFirst == true) { if (bFirst == true) {
s_e_type = s_e_Start + naluType; s_e_type = 1 << 6 | naluType;
bFirst = false; bFirst = false;
} else { } else {
s_e_type = s_e_Mid + naluType; s_e_type = naluType;
} }
} }
//FU type //FU type
_aucSectionBuf[0] = 49 << 1; _aucSectionBuf[0] = 49 << 1;
_aucSectionBuf[1] = 1; _aucSectionBuf[1] = 1;
_aucSectionBuf[2] = s_e_type; _aucSectionBuf[2] = s_e_type;
memcpy(_aucSectionBuf + 3, (unsigned char *) pcData + nOffset, iSize); memcpy(_aucSectionBuf + 3, pcData + nOffset, maxSize);
nOffset += iSize; nOffset += maxSize;
makeH265Rtp(_aucSectionBuf, iSize + 3, mark, uiStamp); makeH265Rtp(naluType,_aucSectionBuf, maxSize + 3, mark, uiStamp);
} }
} else { } else {
makeH265Rtp(pcData, iLen, true, uiStamp); makeH265Rtp(naluType,pcData, iLen, true, uiStamp);
} }
} }
void H265RtpEncoder::makeH265Rtp(const void* data, unsigned int len, bool mark, uint32_t uiStamp) { void H265RtpEncoder::makeH265Rtp(int nal_type,const void* data, unsigned int len, bool mark, uint32_t uiStamp) {
uint16_t ui16RtpLen = len + 12; uint16_t ui16RtpLen = len + 12;
uint32_t ts = htonl((_ui32SampleRate / 1000) * uiStamp); uint32_t ts = htonl((_ui32SampleRate / 1000) * uiStamp);
uint16_t sq = htons(_ui16Sequence); uint16_t sq = htons(_ui16Sequence);
@ -242,9 +241,7 @@ void H265RtpEncoder::makeH265Rtp(const void* data, unsigned int len, bool mark,
rtppkt->ssrc = _ui32Ssrc; rtppkt->ssrc = _ui32Ssrc;
rtppkt->type = TrackVideo; rtppkt->type = TrackVideo;
rtppkt->offset = 16; rtppkt->offset = 16;
RtpCodec::inputRtp(rtppkt,nal_type == H265Frame::NAL_VPS);
uint8_t type = H265_TYPE(((uint8_t *) (data))[0]);
RtpCodec::inputRtp(rtppkt,type == H265Frame::isKeyFrame(type));
_ui16Sequence++; _ui16Sequence++;
_ui32TimeStamp = uiStamp; _ui32TimeStamp = uiStamp;
} }

View File

@ -94,7 +94,7 @@ public:
*/ */
void inputFrame(const Frame::Ptr &frame) override; void inputFrame(const Frame::Ptr &frame) override;
private: private:
void makeH265Rtp(const void *pData, unsigned int uiLen, bool bMark, uint32_t uiStamp); void makeH265Rtp(int nal_type,const void *pData, unsigned int uiLen, bool bMark, uint32_t uiStamp);
private: private:
unsigned char _aucSectionBuf[1600]; unsigned char _aucSectionBuf[1600];
}; };