修复GB28181粘帧导致花屏的问题: #964,#954

This commit is contained in:
xiongziliang 2021-07-06 23:37:04 +08:00
parent 756b6a4cff
commit c393cc7c0e

View File

@ -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<Frame::Ptr> &frameCached) const {
@ -307,9 +308,6 @@ bool FrameMerger::frameCacheHasVCL(List<Frame::Ptr> &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<BufferOffset<BufferLikeString> >(std::move(merged));
}
cb(back->dts(), back->pts(), merged_frame, have_idr);