From 7b90daab24038c89f3bdf59ac7a0e9cfae8c2bc6 Mon Sep 17 00:00:00 2001 From: xiongziliang <771730766@qq.com> Date: Tue, 26 May 2020 17:00:51 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dh264/h265=20prefixSize?= =?UTF-8?q?=E8=AE=A1=E7=AE=97=E9=94=99=E8=AF=AF=E7=9A=84bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Common/Device.cpp | 26 ++++---------------------- src/Extension/H264.cpp | 15 +++++++++++++++ src/Extension/H264.h | 2 +- src/Rtp/Decoder.cpp | 4 ++-- 4 files changed, 22 insertions(+), 25 deletions(-) diff --git a/src/Common/Device.cpp b/src/Common/Device.cpp index 71f42b39..37a0056b 100644 --- a/src/Common/Device.cpp +++ b/src/Common/Device.cpp @@ -78,14 +78,6 @@ void DevChannel::inputH264(const char *data, int len, uint32_t dts, uint32_t pts if(pts == 0){ pts = dts; } - int prefixeSize; - if (memcmp("\x00\x00\x00\x01", data, 4) == 0) { - prefixeSize = 4; - } else if (memcmp("\x00\x00\x01", data, 3) == 0) { - prefixeSize = 3; - } else { - prefixeSize = 0; - } //由于rtmp/hls/mp4需要缓存时间戳相同的帧, //所以使用FrameNoCacheAble类型的帧反而会在转换成FrameCacheAble时多次内存拷贝 @@ -93,9 +85,8 @@ void DevChannel::inputH264(const char *data, int len, uint32_t dts, uint32_t pts H264Frame::Ptr frame = std::make_shared(); frame->_dts = dts; frame->_pts = pts; - frame->_buffer.assign("\x00\x00\x00\x01",4); - frame->_buffer.append(data + prefixeSize, len - prefixeSize); - frame->_prefix_size = 4; + frame->_buffer.assign(data, len); + frame->_prefix_size = prefixSize(data,len); inputFrame(frame); } @@ -106,14 +97,6 @@ void DevChannel::inputH265(const char *data, int len, uint32_t dts, uint32_t pts if(pts == 0){ pts = dts; } - int prefixeSize; - if (memcmp("\x00\x00\x00\x01", data, 4) == 0) { - prefixeSize = 4; - } else if (memcmp("\x00\x00\x01", data, 3) == 0) { - prefixeSize = 3; - } else { - prefixeSize = 0; - } //由于rtmp/hls/mp4需要缓存时间戳相同的帧, //所以使用FrameNoCacheAble类型的帧反而会在转换成FrameCacheAble时多次内存拷贝 @@ -121,9 +104,8 @@ void DevChannel::inputH265(const char *data, int len, uint32_t dts, uint32_t pts H265Frame::Ptr frame = std::make_shared(); frame->_dts = dts; frame->_pts = pts; - frame->_buffer.assign("\x00\x00\x00\x01",4); - frame->_buffer.append(data + prefixeSize, len - prefixeSize); - frame->_prefix_size = 4; + frame->_buffer.assign(data, len); + frame->_prefix_size = prefixSize(data,len); inputFrame(frame); } diff --git a/src/Extension/H264.cpp b/src/Extension/H264.cpp index 0a5314ae..0d591ea4 100644 --- a/src/Extension/H264.cpp +++ b/src/Extension/H264.cpp @@ -74,6 +74,21 @@ void splitH264(const char *ptr, int len, int prefix, const std::function &cb); - +int prefixSize(const char *ptr, int len); /** * 264帧类 */ diff --git a/src/Rtp/Decoder.cpp b/src/Rtp/Decoder.cpp index 5c8a7662..e177ed8e 100644 --- a/src/Rtp/Decoder.cpp +++ b/src/Rtp/Decoder.cpp @@ -129,7 +129,7 @@ void DecoderImp::onDecode(int stream,int codecid,int flags,int64_t pts,int64_t d auto frame = std::make_shared((char *) data, bytes, dts, pts,0); _merger.inputFrame(frame,[this](uint32_t dts, uint32_t pts, const Buffer::Ptr &buffer) { - onFrame(std::make_shared(buffer->data(), buffer->size(), dts, pts,4)); + onFrame(std::make_shared(buffer->data(), buffer->size(), dts, pts, prefixSize(buffer->data(), buffer->size()))); }); break; } @@ -148,7 +148,7 @@ void DecoderImp::onDecode(int stream,int codecid,int flags,int64_t pts,int64_t d } auto frame = std::make_shared((char *) data, bytes, dts, pts, 0); _merger.inputFrame(frame,[this](uint32_t dts, uint32_t pts, const Buffer::Ptr &buffer) { - onFrame(std::make_shared(buffer->data(), buffer->size(), dts, pts, 4)); + onFrame(std::make_shared(buffer->data(), buffer->size(), dts, pts, prefixSize(buffer->data(), buffer->size()))); }); break; }