From 6922d82c651405e77bb333f6e30b69089265c010 Mon Sep 17 00:00:00 2001 From: xiongziliang <771730766@qq.com> Date: Thu, 25 Jul 2019 12:09:36 +0800 Subject: [PATCH] =?UTF-8?q?rtmp=E8=A7=A3=E5=A4=8D=E7=94=A8=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E6=97=A0=E5=86=85=E5=AD=98=E6=8B=B7=E8=B4=9D=20hls?= =?UTF-8?q?=E7=94=9F=E6=88=90=E6=94=AF=E6=8C=81=E8=BE=93=E5=85=A5=E6=97=A0?= =?UTF-8?q?0x=2000=2000=2001=E7=9A=84H264/H265=E6=95=B0=E6=8D=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 3rdpart/media-server | 2 +- src/Extension/H264.h | 22 ++++++++++++++++++---- src/Extension/H264Rtmp.cpp | 6 ++++++ src/MediaFile/TsMuxer.cpp | 7 ++++++- 4 files changed, 31 insertions(+), 6 deletions(-) diff --git a/3rdpart/media-server b/3rdpart/media-server index 0e726dd4..e399b938 160000 --- a/3rdpart/media-server +++ b/3rdpart/media-server @@ -1 +1 @@ -Subproject commit 0e726dd4e06ab4ed3723deaf3f73386e100bb10d +Subproject commit e399b93802610dcf574ff64bcb7677572cd028c1 diff --git a/src/Extension/H264.h b/src/Extension/H264.h index bb036a8e..853c7f60 100644 --- a/src/Extension/H264.h +++ b/src/Extension/H264.h @@ -92,6 +92,11 @@ public: }; +/** + * 防止内存拷贝的H264类 + * 用户可以通过该类型快速把一个指针无拷贝的包装成Frame类 + * 该类型在DevChannel中有使用 + */ class H264FrameNoCacheAble : public FrameNoCacheAble { public: typedef std::shared_ptr Ptr; @@ -117,17 +122,26 @@ public: } }; +/** + * 一个H264Frame类中可以有多个帧,他们通过 0x 00 00 01 分隔 + * ZLMediaKit会先把这种复合帧split成单个帧然后再处理 + * 一个复合帧可以通过无内存拷贝的方式切割成多个H264FrameSubFrame + * 提供该类的目的是切换复合帧时防止内存拷贝,提高性能 + */ class H264FrameSubFrame : public H264FrameNoCacheAble{ public: typedef std::shared_ptr Ptr; - H264FrameSubFrame(const Frame::Ptr &strongRef, + H264FrameSubFrame(const Frame::Ptr &parent_frame, char *ptr, uint32_t size, - int prefixeSize) : H264FrameNoCacheAble(ptr,size,strongRef->dts(),strongRef->pts(),prefixeSize){ - _strongRef = strongRef; + int prefixeSize) : H264FrameNoCacheAble(ptr,size,parent_frame->dts(),parent_frame->pts(),prefixeSize){ + _parent_frame = parent_frame; + } + bool cacheAble() const override { + return _parent_frame->cacheAble(); } private: - Frame::Ptr _strongRef; + Frame::Ptr _parent_frame; }; /** diff --git a/src/Extension/H264Rtmp.cpp b/src/Extension/H264Rtmp.cpp index 3adb0b7e..bd9fd7d7 100644 --- a/src/Extension/H264Rtmp.cpp +++ b/src/Extension/H264Rtmp.cpp @@ -108,6 +108,7 @@ inline void H264RtmpDecoder::onGetH264_l(const char* pcData, int iLen, uint32_t } } inline void H264RtmpDecoder::onGetH264(const char* pcData, int iLen, uint32_t dts,uint32_t pts) { +#if 0 _h264frame->type = H264_TYPE(pcData[0]); _h264frame->timeStamp = dts; _h264frame->ptsStamp = pts; @@ -117,6 +118,11 @@ inline void H264RtmpDecoder::onGetH264(const char* pcData, int iLen, uint32_t dt //写入环形缓存 RtmpCodec::inputFrame(_h264frame); _h264frame = obtainFrame(); +#else + //防止内存拷贝,这样产生的264帧不会有0x00 00 01头 + auto frame = std::make_shared((char *)pcData,iLen,dts,pts,0); + RtmpCodec::inputFrame(frame); +#endif } diff --git a/src/MediaFile/TsMuxer.cpp b/src/MediaFile/TsMuxer.cpp index 3393733d..2eabd145 100644 --- a/src/MediaFile/TsMuxer.cpp +++ b/src/MediaFile/TsMuxer.cpp @@ -70,7 +70,12 @@ void TsMuxer::inputFrame(const Frame::Ptr &frame) { if(_frameCached.size() != 1){ string merged; _frameCached.for_each([&](const Frame::Ptr &frame){ - merged.append(frame->data(),frame->size()); + if(frame->prefixSize()){ + merged.append(frame->data(),frame->size()); + } else{ + merged.append("\x00\x00\x00\x01",4); + merged.append(frame->data(),frame->size()); + } }); merged_frame = std::make_shared(std::move(merged)); }