From e4904623a77b7f812125409893677c6f6bd1ab99 Mon Sep 17 00:00:00 2001 From: xia-chu <771730766@qq.com> Date: Sun, 7 Feb 2021 23:01:22 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8Drtmp=E5=85=BC=E5=AE=B9?= =?UTF-8?q?=E6=80=A7bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Rtmp/Rtmp.h | 11 +++++++++++ src/Rtmp/RtmpProtocol.cpp | 25 +++++++++++++++++-------- src/Rtmp/RtmpProtocol.h | 2 +- 3 files changed, 29 insertions(+), 9 deletions(-) diff --git a/src/Rtmp/Rtmp.h b/src/Rtmp/Rtmp.h index 0e0a6cf4..b644c2e7 100644 --- a/src/Rtmp/Rtmp.h +++ b/src/Rtmp/Rtmp.h @@ -128,6 +128,7 @@ public: class RtmpPacket : public Buffer{ public: + friend class RtmpProtocol; using Ptr = std::shared_ptr; bool is_abs_stamp; uint8_t type_id; @@ -214,6 +215,16 @@ private: clear(); } + RtmpPacket &operator=(const RtmpPacket &that) { + is_abs_stamp = that.is_abs_stamp; + stream_index = that.stream_index; + body_size = that.body_size; + type_id = that.type_id; + ts_field = that.ts_field; + time_stamp = that.time_stamp; + return *this; + } + private: //对象个数统计 ObjectStatistic _statistic; diff --git a/src/Rtmp/RtmpProtocol.cpp b/src/Rtmp/RtmpProtocol.cpp index 9a05d15b..47a7150b 100644 --- a/src/Rtmp/RtmpProtocol.cpp +++ b/src/Rtmp/RtmpProtocol.cpp @@ -565,13 +565,20 @@ const char* RtmpProtocol::handle_rtmp(const char *data, size_t len) { //need more data return ptr; } - RtmpHeader &header = *((RtmpHeader *) (ptr + offset)); - auto &packet_ptr = _map_chunk_data[_now_chunk_id]; - if (!packet_ptr) { - packet_ptr = RtmpPacket::create(); + auto &pr = _map_chunk_data[_now_chunk_id]; + auto &now_packet = pr.first; + auto &last_packet = pr.second; + if (!now_packet) { + now_packet = RtmpPacket::create(); + if (last_packet) { + //恢复chunk上下文 + *now_packet = *last_packet; + } + //绝对时间戳标记复位 + now_packet->is_abs_stamp = false; } - auto &chunk_data = *packet_ptr; + auto &chunk_data = *now_packet; chunk_data.chunk_id = _now_chunk_id; switch (header_len) { case 12: @@ -598,7 +605,7 @@ const char* RtmpProtocol::handle_rtmp(const char *data, size_t len) { throw std::runtime_error("非法的bodySize"); } - auto more = min(_chunk_size_in, (size_t)(chunk_data.body_size - chunk_data.buffer.size())); + auto more = min(_chunk_size_in, (size_t) (chunk_data.body_size - chunk_data.buffer.size())); if (len < header_len + offset + more) { //need more data return ptr; @@ -612,10 +619,12 @@ const char* RtmpProtocol::handle_rtmp(const char *data, size_t len) { //frame is ready _now_stream_index = chunk_data.stream_index; chunk_data.time_stamp = time_stamp + (chunk_data.is_abs_stamp ? 0 : chunk_data.time_stamp); + //保存chunk上下文 + last_packet = now_packet; if (chunk_data.body_size) { - handle_chunk(std::move(packet_ptr)); + handle_chunk(std::move(now_packet)); } else { - packet_ptr = nullptr; + now_packet = nullptr; } } } diff --git a/src/Rtmp/RtmpProtocol.h b/src/Rtmp/RtmpProtocol.h index 794c379f..5a004874 100644 --- a/src/Rtmp/RtmpProtocol.h +++ b/src/Rtmp/RtmpProtocol.h @@ -106,7 +106,7 @@ private: //////////Rtmp parser////////// function _next_step_func; ////////////Chunk//////////// - unordered_map _map_chunk_data; + unordered_map > _map_chunk_data; }; } /* namespace mediakit */