Commit 12e1068a by xiongziliang

Merge branch 'master' of https://github.com/xiongziliang/ZLMediaKit into zqsong

parents 69098612 e13ae419
...@@ -80,13 +80,11 @@ public: ...@@ -80,13 +80,11 @@ public:
} }
bool keyFrame() const override { bool keyFrame() const override {
return type == NAL_IDR; return H264_TYPE(buffer[iPrefixSize]) == H264Frame::NAL_IDR;
} }
public: public:
uint16_t sequence;
uint32_t timeStamp; uint32_t timeStamp;
uint32_t ptsStamp = 0; uint32_t ptsStamp = 0;
unsigned char type;
string buffer; string buffer;
uint32_t iPrefixSize = 4; uint32_t iPrefixSize = 4;
}; };
...@@ -326,7 +324,6 @@ private: ...@@ -326,7 +324,6 @@ private:
if(!_sps.empty()){ if(!_sps.empty()){
auto spsFrame = std::make_shared<H264Frame>(); auto spsFrame = std::make_shared<H264Frame>();
spsFrame->type = H264Frame::NAL_SPS;
spsFrame->iPrefixSize = 4; spsFrame->iPrefixSize = 4;
spsFrame->buffer.assign("\x0\x0\x0\x1",4); spsFrame->buffer.assign("\x0\x0\x0\x1",4);
spsFrame->buffer.append(_sps); spsFrame->buffer.append(_sps);
...@@ -336,7 +333,6 @@ private: ...@@ -336,7 +333,6 @@ private:
if(!_pps.empty()){ if(!_pps.empty()){
auto ppsFrame = std::make_shared<H264Frame>(); auto ppsFrame = std::make_shared<H264Frame>();
ppsFrame->type = H264Frame::NAL_PPS;
ppsFrame->iPrefixSize = 4; ppsFrame->iPrefixSize = 4;
ppsFrame->buffer.assign("\x0\x0\x0\x1",4); ppsFrame->buffer.assign("\x0\x0\x0\x1",4);
ppsFrame->buffer.append(_pps); ppsFrame->buffer.append(_pps);
......
...@@ -80,7 +80,6 @@ bool H264RtmpDecoder::decodeRtmp(const RtmpPacket::Ptr &pkt) { ...@@ -80,7 +80,6 @@ bool H264RtmpDecoder::decodeRtmp(const RtmpPacket::Ptr &pkt) {
inline void H264RtmpDecoder::onGetH264(const char* pcData, int iLen, uint32_t dts,uint32_t pts) { inline void H264RtmpDecoder::onGetH264(const char* pcData, int iLen, uint32_t dts,uint32_t pts) {
#if 1 #if 1
_h264frame->type = H264_TYPE(pcData[0]);
_h264frame->timeStamp = dts; _h264frame->timeStamp = dts;
_h264frame->ptsStamp = pts; _h264frame->ptsStamp = pts;
_h264frame->buffer.assign("\x0\x0\x0\x1", 4); //添加264头 _h264frame->buffer.assign("\x0\x0\x0\x1", 4); //添加264头
......
...@@ -100,12 +100,10 @@ bool H264RtpDecoder::decodeRtp(const RtpPacket::Ptr &rtppack) { ...@@ -100,12 +100,10 @@ bool H264RtpDecoder::decodeRtp(const RtpPacket::Ptr &rtppack) {
//a full frame //a full frame
_h264frame->buffer.assign("\x0\x0\x0\x1", 4); _h264frame->buffer.assign("\x0\x0\x0\x1", 4);
_h264frame->buffer.append((char *)frame, length); _h264frame->buffer.append((char *)frame, length);
_h264frame->type = nal.type;
_h264frame->timeStamp = rtppack->timeStamp; _h264frame->timeStamp = rtppack->timeStamp;
_h264frame->sequence = rtppack->sequence; auto key = _h264frame->keyFrame();
auto isIDR = _h264frame->type == H264Frame::NAL_IDR;
onGetH264(_h264frame); onGetH264(_h264frame);
return (isIDR); //i frame return (key); //i frame
} }
switch (nal.type){ switch (nal.type){
...@@ -131,9 +129,7 @@ bool H264RtpDecoder::decodeRtp(const RtpPacket::Ptr &rtppack) { ...@@ -131,9 +129,7 @@ bool H264RtpDecoder::decodeRtp(const RtpPacket::Ptr &rtppack) {
MakeNalu(ptr[0], nal); MakeNalu(ptr[0], nal);
_h264frame->buffer.assign("\x0\x0\x0\x1", 4); _h264frame->buffer.assign("\x0\x0\x0\x1", 4);
_h264frame->buffer.append((char *)ptr, len); _h264frame->buffer.append((char *)ptr, len);
_h264frame->type = nal.type;
_h264frame->timeStamp = rtppack->timeStamp; _h264frame->timeStamp = rtppack->timeStamp;
_h264frame->sequence = rtppack->sequence;
if(nal.type == H264Frame::NAL_IDR){ if(nal.type == H264Frame::NAL_IDR){
haveIDR = true; haveIDR = true;
} }
...@@ -148,35 +144,39 @@ bool H264RtpDecoder::decodeRtp(const RtpPacket::Ptr &rtppack) { ...@@ -148,35 +144,39 @@ bool H264RtpDecoder::decodeRtp(const RtpPacket::Ptr &rtppack) {
//FU-A //FU-A
FU fu; FU fu;
MakeFU(frame[1], fu); MakeFU(frame[1], fu);
if (fu.S == 1) { if (fu.S) {
//FU-A start //该帧的第一个rtp包
char tmp = (nal.forbidden_zero_bit << 7 | nal.nal_ref_idc << 5 | fu.type); char tmp = (nal.forbidden_zero_bit << 7 | nal.nal_ref_idc << 5 | fu.type);
_h264frame->buffer.assign("\x0\x0\x0\x1", 4); _h264frame->buffer.assign("\x0\x0\x0\x1", 4);
_h264frame->buffer.push_back(tmp); _h264frame->buffer.push_back(tmp);
_h264frame->buffer.append((char *)frame + 2, length - 2); _h264frame->buffer.append((char *)frame + 2, length - 2);
_h264frame->type = fu.type;
_h264frame->timeStamp = rtppack->timeStamp; _h264frame->timeStamp = rtppack->timeStamp;
_h264frame->sequence = rtppack->sequence; //该函数return时,保存下当前sequence,以便下次对比seq是否连续
return (_h264frame->type == H264Frame::NAL_IDR); //i frame _lastSeq = rtppack->sequence;
return _h264frame->keyFrame();
} }
if (rtppack->sequence != (uint16_t)(_h264frame->sequence + 1)) { if (rtppack->sequence != _lastSeq + 1 && rtppack->sequence != 0) {
//中间的或末尾的rtp包,其seq必须连续(如果回环了则判定为连续),否则说明rtp丢包,那么该帧不完整,必须得丢弃
_h264frame->buffer.clear(); _h264frame->buffer.clear();
WarnL << "丢包,帧废弃:" << rtppack->sequence << "," << _h264frame->sequence; WarnL << "rtp sequence不连续: " << rtppack->sequence << " != " << _lastSeq << " + 1,该帧被废弃";
return false; return false;
} }
_h264frame->sequence = rtppack->sequence;
if (fu.E == 1) { if (!fu.E) {
//FU-A end //该帧的中间rtp包
_h264frame->buffer.append((char *)frame + 2, length - 2); _h264frame->buffer.append((char *)frame + 2, length - 2);
_h264frame->timeStamp = rtppack->timeStamp; //该函数return时,保存下当前sequence,以便下次对比seq是否连续
auto isIDR = _h264frame->type == H264Frame::NAL_IDR; _lastSeq = rtppack->sequence;
onGetH264(_h264frame); return false;
return isIDR;
} }
//FU-A mid
//该帧最后一个rtp包
_h264frame->buffer.append((char *)frame + 2, length - 2); _h264frame->buffer.append((char *)frame + 2, length - 2);
return false; _h264frame->timeStamp = rtppack->timeStamp;
auto key = _h264frame->keyFrame();
onGetH264(_h264frame);
return key;
} }
default:{ default:{
...@@ -195,10 +195,8 @@ bool H264RtpDecoder::decodeRtp(const RtpPacket::Ptr &rtppack) { ...@@ -195,10 +195,8 @@ bool H264RtpDecoder::decodeRtp(const RtpPacket::Ptr &rtppack) {
void H264RtpDecoder::onGetH264(const H264Frame::Ptr &frame) { void H264RtpDecoder::onGetH264(const H264Frame::Ptr &frame) {
//写入环形缓存 //写入环形缓存
auto lastSeq = _h264frame->sequence;
RtpCodec::inputFrame(frame); RtpCodec::inputFrame(frame);
_h264frame = obtainFrame(); _h264frame = obtainFrame();
_h264frame->sequence = lastSeq;
} }
......
...@@ -64,6 +64,7 @@ private: ...@@ -64,6 +64,7 @@ private:
H264Frame::Ptr obtainFrame(); H264Frame::Ptr obtainFrame();
private: private:
H264Frame::Ptr _h264frame; H264Frame::Ptr _h264frame;
int _lastSeq = 0;
}; };
/** /**
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论