diff --git a/src/Extension/Frame.cpp b/src/Extension/Frame.cpp index a32e389e..d8997dfd 100644 --- a/src/Extension/Frame.cpp +++ b/src/Extension/Frame.cpp @@ -22,67 +22,6 @@ namespace toolkit { namespace mediakit{ -/** - * 该对象的功能是把一个不可缓存的帧转换成可缓存的帧 - */ -class FrameCacheAble : public FrameFromPtr { -public: - typedef std::shared_ptr Ptr; - - FrameCacheAble(const Frame::Ptr &frame){ - if (frame->cacheAble()) { - _frame = frame; - _ptr = frame->data(); - } else { - _buffer = FrameImp::create(); - _buffer->_buffer.assign(frame->data(), frame->size()); - _ptr = _buffer->data(); - } - _size = frame->size(); - _dts = frame->dts(); - _pts = frame->pts(); - _prefix_size = frame->prefixSize(); - _codec_id = frame->getCodecId(); - _key = frame->keyFrame(); - _config = frame->configFrame(); - _drop_able = frame->dropAble(); - _decode_able = frame->decodeAble(); - } - - ~FrameCacheAble() override = default; - - /** - * 可以被缓存 - */ - bool cacheAble() const override { - return true; - } - - bool keyFrame() const override{ - return _key; - } - - bool configFrame() const override{ - return _config; - } - - bool dropAble() const override { - return _drop_able; - } - - bool decodeAble() const override { - return _decode_able; - } - -private: - bool _key; - bool _config; - bool _drop_able; - bool _decode_able; - Frame::Ptr _frame; - FrameImp::Ptr _buffer; -}; - Frame::Ptr Frame::getCacheAbleFrame(const Frame::Ptr &frame){ if(frame->cacheAble()){ return frame; @@ -241,18 +180,6 @@ bool FrameMerger::inputFrame(const Frame::Ptr &frame, const onOutput &cb, Buffer _have_decode_able_frame = false; } - switch (_type) { - case h264_prefix: - case mp4_nal_size: { - if (frame->dropAble()) { - //h264头和mp4头模式过滤无效的帧 - return false; - } - break; - } - default: break; - } - if (frame->decodeAble()) { _have_decode_able_frame = true; } diff --git a/src/Extension/Frame.h b/src/Extension/Frame.h index 46b1b926..4e7d7015 100644 --- a/src/Extension/Frame.h +++ b/src/Extension/Frame.h @@ -444,6 +444,67 @@ protected: CodecId _codec_id = CodecInvalid; }; +/** + * 该对象的功能是把一个不可缓存的帧转换成可缓存的帧 + */ +class FrameCacheAble : public FrameFromPtr { +public: + typedef std::shared_ptr Ptr; + + FrameCacheAble(const Frame::Ptr &frame, bool force_key_frame = false) { + if (frame->cacheAble()) { + _frame = frame; + _ptr = frame->data(); + } else { + _buffer = FrameImp::create(); + _buffer->_buffer.assign(frame->data(), frame->size()); + _ptr = _buffer->data(); + } + _size = frame->size(); + _dts = frame->dts(); + _pts = frame->pts(); + _prefix_size = frame->prefixSize(); + _codec_id = frame->getCodecId(); + _key = force_key_frame ? true : frame->keyFrame(); + _config = frame->configFrame(); + _drop_able = frame->dropAble(); + _decode_able = frame->decodeAble(); + } + + ~FrameCacheAble() override = default; + + /** + * 可以被缓存 + */ + bool cacheAble() const override { + return true; + } + + bool keyFrame() const override{ + return _key; + } + + bool configFrame() const override{ + return _config; + } + + bool dropAble() const override { + return _drop_able; + } + + bool decodeAble() const override { + return _decode_able; + } + +private: + bool _key; + bool _config; + bool _drop_able; + bool _decode_able; + Frame::Ptr _frame; + FrameImp::Ptr _buffer; +}; + /** * 该对象可以把Buffer对象转换成可缓存的Frame对象 */ diff --git a/src/Extension/H264.cpp b/src/Extension/H264.cpp index fd2d3b91..b1874e30 100644 --- a/src/Extension/H264.cpp +++ b/src/Extension/H264.cpp @@ -184,23 +184,22 @@ bool H264Track::inputFrame_l(const Frame::Ptr &frame){ } case H264Frame::NAL_PPS: { _pps = string(frame->data() + frame->prefixSize(), frame->size() - frame->prefixSize()); - break; - } - case H264Frame::NAL_AUD: { - //忽略AUD帧; - ret = false; + _insert_config_frame = true; break; } default: - if (frame->keyFrame()) { + if (_insert_config_frame && !frame->dropAble()) { + if (!frame->keyFrame()) { + const_cast(frame) = std::make_shared(frame, true); + } insertConfigFrame(frame); + _insert_config_frame = false; } ret = VideoTrack::inputFrame(frame); break; } - _is_idr = type == H264Frame::NAL_IDR; if (_width == 0 && ready()) { onReady(); } @@ -208,10 +207,6 @@ bool H264Track::inputFrame_l(const Frame::Ptr &frame){ } void H264Track::insertConfigFrame(const Frame::Ptr &frame){ - if(_is_idr){ - return; - } - if(!_sps.empty()){ auto spsFrame = FrameImp::create(); spsFrame->_prefix_size = 4; diff --git a/src/Extension/H264.h b/src/Extension/H264.h index 13b6096d..6065aad0 100644 --- a/src/Extension/H264.h +++ b/src/Extension/H264.h @@ -138,7 +138,7 @@ private: void insertConfigFrame(const Frame::Ptr &frame); private: - bool _is_idr = false; + bool _insert_config_frame = false; int _width = 0; int _height = 0; float _fps = 0;