From efdc105b40fd19079022ec93a6dd7bdf71461a40 Mon Sep 17 00:00:00 2001 From: xiongziliang <771730766@qq.com> Date: Mon, 21 Sep 2020 14:56:58 +0800 Subject: [PATCH] =?UTF-8?q?=E5=87=8F=E5=B0=91=E5=86=85=E5=AD=98=E6=8B=B7?= =?UTF-8?q?=E8=B4=9D=EF=BC=8C=E6=8F=90=E5=8D=87=E6=80=A7=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Extension/Frame.h | 44 +++++++++++++++++++++++++++++++++++++++ src/Record/MP4Demuxer.cpp | 27 ++++-------------------- src/Rtp/Decoder.cpp | 4 ++-- 3 files changed, 50 insertions(+), 25 deletions(-) diff --git a/src/Extension/Frame.h b/src/Extension/Frame.h index 4de0278f..8f2ab8a3 100644 --- a/src/Extension/Frame.h +++ b/src/Extension/Frame.h @@ -383,5 +383,49 @@ protected: CodecId _codec_id = CodecInvalid; }; +/** + * 该对象可以把Buffer对象转换成可缓存的Frame对象 + */ +template +class FrameWrapper : public Parent{ +public: + ~FrameWrapper() = default; + + /** + * 构造frame + * @param buf 数据缓存 + * @param dts 解码时间戳 + * @param pts 显示时间戳 + * @param prefix 帧前缀长度 + * @param offset buffer有效数据偏移量 + */ + FrameWrapper(const Buffer::Ptr &buf, int64_t dts, int64_t pts, int prefix, int offset) : Parent(buf->data() + offset, buf->size() - offset, dts, pts, prefix){ + _buf = buf; + } + + /** + * 构造frame + * @param buf 数据缓存 + * @param dts 解码时间戳 + * @param pts 显示时间戳 + * @param prefix 帧前缀长度 + * @param offset buffer有效数据偏移量 + * @param codec 帧类型 + */ + FrameWrapper(const Buffer::Ptr &buf, int64_t dts, int64_t pts, int prefix, int offset, CodecId codec) : Parent(codec, buf->data() + offset, buf->size() - offset, dts, pts, prefix){ + _buf = buf; + } + + /** + * 该帧可缓存 + */ + bool cacheAble() const override { + return true; + } + +private: + Buffer::Ptr _buf; +}; + }//namespace mediakit #endif //ZLMEDIAKIT_FRAME_H \ No newline at end of file diff --git a/src/Record/MP4Demuxer.cpp b/src/Record/MP4Demuxer.cpp index df70ff7d..f4fa5a13 100644 --- a/src/Record/MP4Demuxer.cpp +++ b/src/Record/MP4Demuxer.cpp @@ -202,25 +202,6 @@ Frame::Ptr MP4Demuxer::readFrame(bool &keyFrame, bool &eof) { } } -template -class FrameWrapper : public Parent{ -public: - ~FrameWrapper() = default; - FrameWrapper(const Buffer::Ptr &buf, int64_t pts, int64_t dts, int prefix, int offset) : Parent(buf->data() + offset, buf->size() - offset, dts, pts, prefix){ - _buf = buf; - } - - FrameWrapper(const Buffer::Ptr &buf, int64_t pts, int64_t dts, int prefix, int offset, CodecId codec) : Parent(codec, buf->data() + offset, buf->size() - offset, dts, pts, prefix){ - _buf = buf; - } - - bool cacheAble() const override { - return true; - } -private: - Buffer::Ptr _buf; -}; - Frame::Ptr MP4Demuxer::makeFrame(uint32_t track_id, const Buffer::Ptr &buf, int64_t pts, int64_t dts) { auto it = _track_to_codec.find(track_id); if (it == _track_to_codec.end()) { @@ -244,9 +225,9 @@ Frame::Ptr MP4Demuxer::makeFrame(uint32_t track_id, const Buffer::Ptr &buf, int6 offset += (frame_len + 4); } if (codec == CodecH264) { - return std::make_shared >(buf, pts, dts, 4, DATA_OFFSET); + return std::make_shared >(buf, dts, pts, 4, DATA_OFFSET); } - return std::make_shared >(buf, pts, dts, 4, DATA_OFFSET); + return std::make_shared >(buf, dts, pts, 4, DATA_OFFSET); } case CodecAAC: { @@ -254,13 +235,13 @@ Frame::Ptr MP4Demuxer::makeFrame(uint32_t track_id, const Buffer::Ptr &buf, int6 assert(track); //加上adts头 dumpAacConfig(track->getAacCfg(), buf->size() - DATA_OFFSET, (uint8_t *) buf->data() + (DATA_OFFSET - ADTS_HEADER_LEN), ADTS_HEADER_LEN); - return std::make_shared >(buf, pts, dts, ADTS_HEADER_LEN, DATA_OFFSET - ADTS_HEADER_LEN, codec); + return std::make_shared >(buf, dts, pts, ADTS_HEADER_LEN, DATA_OFFSET - ADTS_HEADER_LEN, codec); } case CodecOpus: case CodecG711A: case CodecG711U: { - return std::make_shared >(buf, pts, dts, 0, DATA_OFFSET, codec); + return std::make_shared >(buf, dts, pts, 0, DATA_OFFSET, codec); } default: return nullptr; diff --git a/src/Rtp/Decoder.cpp b/src/Rtp/Decoder.cpp index a4f77715..09090d76 100644 --- a/src/Rtp/Decoder.cpp +++ b/src/Rtp/Decoder.cpp @@ -135,7 +135,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, prefixSize(buffer->data(), buffer->size()))); + onFrame(std::make_shared >(buffer, dts, pts, prefixSize(buffer->data(), buffer->size()), 0)); }); break; } @@ -154,7 +154,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, prefixSize(buffer->data(), buffer->size()))); + onFrame(std::make_shared >(buffer, dts, pts, prefixSize(buffer->data(), buffer->size()), 0)); }); break; }