diff --git a/ext-codec/H265.cpp b/ext-codec/H265.cpp index ae649b1d..7693b9b1 100644 --- a/ext-codec/H265.cpp +++ b/ext-codec/H265.cpp @@ -106,30 +106,35 @@ bool H265Track::inputFrame(const Frame::Ptr &frame) { } bool H265Track::inputFrame_l(const Frame::Ptr &frame) { - if (frame->keyFrame()) { - insertConfigFrame(frame); - _is_idr = true; - return VideoTrack::inputFrame(frame); - } - - _is_idr = false; + int type = H265_TYPE(frame->data()[frame->prefixSize()]); bool ret = true; - - //非idr帧 - switch (H265_TYPE( frame->data()[frame->prefixSize()])) { + switch (type) { case H265Frame::NAL_VPS: { _vps = string(frame->data() + frame->prefixSize(), frame->size() - frame->prefixSize()); + _latest_is_config_frame = true; + ret = VideoTrack::inputFrame(frame); break; } case H265Frame::NAL_SPS: { _sps = string(frame->data() + frame->prefixSize(), frame->size() - frame->prefixSize()); + _latest_is_config_frame = true; + ret = VideoTrack::inputFrame(frame); break; } case H265Frame::NAL_PPS: { _pps = string(frame->data() + frame->prefixSize(), frame->size() - frame->prefixSize()); + _latest_is_config_frame = true; + ret = VideoTrack::inputFrame(frame); break; } default: { + // 判断是否是I帧, 并且如果是,那判断前面是否插入过config帧, 如果插入过就不插入了 + if (frame->keyFrame() && !_latest_is_config_frame) { + insertConfigFrame(frame); + } + if (!frame->dropAble()) { + _latest_is_config_frame = false; + } ret = VideoTrack::inputFrame(frame); break; } @@ -199,9 +204,6 @@ Track::Ptr H265Track::clone() const { } void H265Track::insertConfigFrame(const Frame::Ptr &frame) { - if (_is_idr) { - return; - } if (!_vps.empty()) { VideoTrack::inputFrame(createConfigFrame(_vps, frame->dts(), frame->getIndex())); } diff --git a/ext-codec/H265.h b/ext-codec/H265.h index b95f72e8..bbd8233f 100644 --- a/ext-codec/H265.h +++ b/ext-codec/H265.h @@ -151,7 +151,7 @@ private: void insertConfigFrame(const Frame::Ptr &frame); private: - bool _is_idr = false; + bool _latest_is_config_frame = false; int _width = 0; int _height = 0; float _fps = 0;