88// ==============================================================================
99#pragma once
1010
11- #include < base/info/info.h>
12-
13- #include < deque>
14- #include < map>
15- #include < memory>
16-
11+ #include " ../rtmp_chunk_parser_common.h"
1712#include " rtmp_datastructure.h"
1813#include " rtmp_define.h"
1914#include " rtmp_mux_util.h"
2015
21- class RtmpChunkParser
16+ class RtmpChunkParser : public RtmpChunkParserCommon <RtmpChunkHeader, RtmpMessage>
2217{
2318public:
24- enum class ParseResult
25- {
26- Error,
27- NeedMoreData,
28- Parsed,
29- };
30-
31- enum class ParseResultForExtendedTimestamp
32- {
33- NeedMoreData,
34- Extended,
35- NotExtended,
36- };
19+ using ParseResult = typename RtmpChunkParserCommon<RtmpChunkHeader, RtmpMessage>::ParseResult;
3720
38- public:
3921 RtmpChunkParser (size_t chunk_size);
4022 virtual ~RtmpChunkParser ();
4123
24+ // / Parses as much of a single RTMP chunk as possible from the supplied data.
25+ // /
26+ // / @param data Input bytes beginning at the next unread RTMP chunk boundary.
27+ // / @param bytes_used Receives the number of bytes consumed from @p data.
28+ // /
29+ // / @return `Parsed` when a chunk payload step completed, `NeedMoreData` when
30+ // / more bytes are required, or `Error` when the chunk stream is
31+ // / malformed.
32+ // /
33+ // / @note When this returns `NeedMoreData`, @p bytes_used is set to `0` and
34+ // / the caller must replay the same unconsumed prefix again with more
35+ // / bytes appended. Header parsing is not resumable from the middle of
36+ // / a partially received header.
4237 ParseResult Parse (const std::shared_ptr<const ov::Data> &data, size_t *bytes_used);
4338
4439 std::shared_ptr<const RtmpMessage> GetMessage ();
@@ -49,47 +44,46 @@ class RtmpChunkParser
4944 _chunk_size = chunk_size;
5045 }
5146
52- info::NamePath GetNamePath () const ;
53- void UpdateNamePath (const info::NamePath &stream_name_path);
54-
5547 void Destroy ();
5648
5749private:
50+ // / Returns the most recent completed header for a chunk stream.
51+ // /
52+ // / @param chunk_stream_id RTMP chunk stream identifier.
53+ // /
54+ // / @return The preceding parsed header for @p chunk_stream_id, or `nullptr`
55+ // / if no header has been completed on that chunk stream yet.
5856 std::shared_ptr<const RtmpChunkHeader> GetPrecedingChunkHeader (const uint32_t chunk_stream_id);
5957
58+ // / Checks whether the next header belongs to an unfinished message.
59+ // /
60+ // / @param chunk_stream_id RTMP chunk stream identifier for the incoming chunk.
61+ // /
62+ // / @return `true` when the parser is expecting a continuation chunk for the
63+ // / same chunk stream, including interleaved pending-message cases.
64+ bool IsContinuationChunk (const uint32_t chunk_stream_id) const ;
65+
66+ // / Parses the RTMP basic header.
67+ // /
68+ // / @param stream Input byte stream positioned at the RTMP basic header.
69+ // / @param chunk_header Destination header object to populate.
70+ // /
71+ // / @return `Parsed`, `NeedMoreData`, or `Error`.
6072 ParseResult ParseBasicHeader (ov::ByteStream &stream, RtmpChunkHeader *chunk_header);
61- ParseResultForExtendedTimestamp ParseExtendedTimestamp (
62- const uint32_t stream_id,
63- ov::ByteStream &stream,
64- RtmpChunkHeader *chunk_header,
65- const int64_t timestamp,
66- RtmpChunkHeader::CompletedHeader *completed_header);
67- ParseResultForExtendedTimestamp ParseExtendedTimestampDelta (
68- const uint32_t stream_id,
69- ov::ByteStream &stream,
70- RtmpChunkHeader *chunk_header,
71- const int64_t preceding_timestamp,
72- const int64_t timestamp_delta,
73- RtmpChunkHeader::CompletedHeader *completed_header);
74- ParseResult ParseMessageHeader (ov::ByteStream &stream, RtmpChunkHeader *chunk_header);
75- ParseResult ParseHeader (ov::ByteStream &stream, RtmpChunkHeader *chunk_header);
76-
77- int64_t CalculateRolledTimestamp (const uint32_t stream_id, const int64_t last_timestamp, int64_t parsed_timestamp);
78-
79- private:
80- #if DEBUG
81- uint64_t _chunk_index = 0ULL ;
82- uint64_t _total_read_bytes = 0ULL ;
83- #endif // DEBUG
8473
85- bool _need_to_parse_new_header = true ;
86- std::shared_ptr<RtmpMessage> _current_message;
87- std::map< uint32_t , std::shared_ptr<RtmpMessage>> _pending_message_map;
88- std::map< uint32_t , std::shared_ptr< const RtmpChunkHeader>> _preceding_chunk_header_map;
89-
90- ov::Queue<std::shared_ptr< const RtmpMessage>> _message_queue{ nullptr , 500 };
91- size_t _chunk_size ;
74+ // / Parses the RTMP message header for an already-parsed basic header.
75+ // /
76+ // / @param stream Input byte stream positioned at the RTMP message header.
77+ // / @param chunk_header Destination header object to populate.
78+ // /
79+ // / @return `Parsed`, `NeedMoreData`, or `Error`.
80+ ParseResult ParseMessageHeader (ov::ByteStream &stream, RtmpChunkHeader *chunk_header) ;
9281
93- mutable std::mutex _name_path_mutex;
94- info::NamePath _name_path;
82+ // / Parses both the RTMP basic header and message header.
83+ // /
84+ // / @param stream Input byte stream positioned at the start of an RTMP header.
85+ // / @param chunk_header Destination header object to populate.
86+ // /
87+ // / @return `Parsed`, `NeedMoreData`, or `Error`.
88+ ParseResult ParseHeader (ov::ByteStream &stream, RtmpChunkHeader *chunk_header);
9589};
0 commit comments