From c393cc7c0ed445f358dbecd220af42292af2b99f Mon Sep 17 00:00:00 2001 From: xiongziliang <771730766@qq.com> Date: Tue, 6 Jul 2021 23:37:04 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8DGB28181=E7=B2=98=E5=B8=A7?= =?UTF-8?q?=E5=AF=BC=E8=87=B4=E8=8A=B1=E5=B1=8F=E7=9A=84=E9=97=AE=E9=A2=98?= =?UTF-8?q?:=20#964,#954?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Extension/Frame.cpp | 74 +++++++++++++++++++++-------------------- 1 file changed, 38 insertions(+), 36 deletions(-) diff --git a/src/Extension/Frame.cpp b/src/Extension/Frame.cpp index 2b25ea19..e7066bb3 100644 --- a/src/Extension/Frame.cpp +++ b/src/Extension/Frame.cpp @@ -180,6 +180,7 @@ bool FrameMerger::willFlush(const Frame::Ptr &frame) const{ } default: break; } + //遇到新帧、或时间戳变化或缓存太多,防止内存溢出,则flush输出 return new_frame || _frameCached.back()->dts() != frame->dts() || _frameCached.size() > kMaxFrameCacheSize; } @@ -192,30 +193,20 @@ bool FrameMerger::willFlush(const Frame::Ptr &frame) const{ switch (frame->getCodecId()) { case CodecH264 : { auto type = H264_TYPE(frame->data()[frame->prefixSize()]); - if ((frame->data()[frame->prefixSize() + 1] & 0x80) != 0 && type >= H264Frame::NAL_B_P && - type <= H264Frame::NAL_IDR) {// sei aud pps sps 不判断 - //264 新一帧的开始,刷新输出 - return true; - } else { - // 不刷新输出 - return false; - } - break; + // sei aud pps sps 不判断;264 新一帧的开始,刷新输出 + return (frame->data()[frame->prefixSize() + 1] & 0x80) != 0 && type >= H264Frame::NAL_B_P && + type <= H264Frame::NAL_IDR; } case CodecH265 : { auto type = H265_TYPE(frame->data()[frame->prefixSize()]); - if ((type >= H265Frame::NAL_TRAIL_R && type <= H265Frame::NAL_RSV_IRAP_VCL23) && - ((frame->data()[frame->prefixSize() + 2] >> 7 & 0x01) != 0)) { - //first_slice_segment_in_pic_flag is frame start - return true; - } else { - return false; - } - break; + //first_slice_segment_in_pic_flag is frame start + return (type >= H265Frame::NAL_TRAIL_R && type <= H265Frame::NAL_RSV_IRAP_VCL23) && + ((frame->data()[frame->prefixSize() + 2] >> 7 & 0x01) != 0); } - default : break; + default : + //缓存太多,防止内存溢出 + return _frameCached.size() > kMaxFrameCacheSize; } - return _frameCached.size() > kMaxFrameCacheSize; } default: /*不可达*/ assert(0); return true; } @@ -224,10 +215,16 @@ bool FrameMerger::willFlush(const Frame::Ptr &frame) const{ void FrameMerger::doMerge(BufferLikeString &merged, const Frame::Ptr &frame) const{ switch (_type) { case none : { + //此处是合并ps解析输出的流,解析出的流可能是半帧或多帧,不能简单的根据nal type过滤 + //此流程只用于合并ps解析输出为H264/H265,后面流程有split和忽略无效帧操作 merged.append(frame->data(), frame->size()); break; } case h264_prefix: { + if (shouldDrop(frame)) { + //h264头模式过滤无效的帧 + break; + } if (frame->prefixSize()) { merged.append(frame->data(), frame->size()); } else { @@ -237,6 +234,10 @@ void FrameMerger::doMerge(BufferLikeString &merged, const Frame::Ptr &frame) con break; } case mp4_nal_size: { + if (shouldDrop(frame)) { + //MP4头模式过滤无效的帧 + break; + } uint32_t nalu_size = (uint32_t) (frame->size() - frame->prefixSize()); nalu_size = htonl(nalu_size); merged.append((char *) &nalu_size, 4); @@ -250,26 +251,26 @@ void FrameMerger::doMerge(BufferLikeString &merged, const Frame::Ptr &frame) con bool FrameMerger::shouldDrop(const Frame::Ptr &frame) const{ switch (frame->getCodecId()) { case CodecH264: { - auto type = H264_TYPE(frame->data()[frame->prefixSize()]); - if (type == H264Frame::NAL_SEI || type == H264Frame::NAL_AUD) { - // 防止吧AUD或者SEI当成一帧 - return true; + switch (H264_TYPE(frame->data()[frame->prefixSize()])) { + // 防止把AUD或者SEI当成一帧 + case H264Frame::NAL_SEI: + case H264Frame::NAL_AUD: return true; + default: return false; } - break; } + case CodecH265: { - //如果是新的一帧,前面的缓存需要输出 - auto type = H265_TYPE(frame->data()[frame->prefixSize()]); - if (type == H265Frame::NAL_AUD || type == H265Frame::NAL_SEI_PREFIX || type == H265Frame::NAL_SEI_SUFFIX) { - // 防止吧AUD或者SEI当成一帧 - return true; + switch (H265_TYPE(frame->data()[frame->prefixSize()])) { + // 防止把AUD或者SEI当成一帧 + case H265Frame::NAL_AUD: + case H265Frame::NAL_SEI_SUFFIX: + case H265Frame::NAL_SEI_PREFIX: return true; + default: return false; } - break; } - default: - break; + + default: return false; } - return false; } bool FrameMerger::frameCacheHasVCL(List &frameCached) const { @@ -307,9 +308,6 @@ bool FrameMerger::frameCacheHasVCL(List &frameCached) const { } void FrameMerger::inputFrame(const Frame::Ptr &frame, const onOutput &cb) { - if (shouldDrop(frame)) { - return; - } if (willFlush(frame) && frameCacheHasVCL(_frameCached)) { Frame::Ptr back = _frameCached.back(); Buffer::Ptr merged_frame = back; @@ -325,6 +323,10 @@ void FrameMerger::inputFrame(const Frame::Ptr &frame, const onOutput &cb) { have_idr = true; } }); + if (merged.empty()) { + _frameCached.clear(); + return; + } merged_frame = std::make_shared >(std::move(merged)); } cb(back->dts(), back->pts(), merged_frame, have_idr);