OLD | NEW |
---|---|
(Empty) | |
1 /* | |
2 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. | |
3 * | |
4 * Use of this source code is governed by a BSD-style license | |
5 * that can be found in the LICENSE file in the root of the source | |
6 * tree. An additional intellectual property rights grant can be found | |
7 * in the file PATENTS. All contributing project authors may | |
8 * be found in the AUTHORS file in the root of the source tree. | |
9 */ | |
10 #ifndef WEBRTC_MODULES_RTP_RTCP_SOURCE_RTP_PACKET_H_ | |
11 #define WEBRTC_MODULES_RTP_RTCP_SOURCE_RTP_PACKET_H_ | |
12 | |
13 #include <vector> | |
14 | |
15 #include "webrtc/base/basictypes.h" | |
16 #include "webrtc/base/buffer.h" | |
17 #include "webrtc/modules/rtp_rtcp/include/rtp_rtcp_defines.h" | |
18 | |
19 namespace webrtc { | |
20 struct RTPHeader; | |
21 class RtpHeaderExtensionMap; | |
22 class Random; | |
23 | |
24 namespace rtp { | |
25 class Packet { | |
26 public: | |
27 using ExtensionType = RTPExtensionType; | |
28 using ExtensionManager = RtpHeaderExtensionMap; | |
29 static const size_t kMaxExtensionHeaders = 14; | |
30 | |
31 // Parse and copy given buffer into Packet. | |
32 bool Parse(const uint8_t* buffer, size_t size); | |
33 // Parse and move given buffer into Packet. | |
stefan-webrtc
2016/04/20 10:53:40
I prefer to have a new line before each comment so
danilchap
2016/04/20 11:05:29
Done, it look nicer with extra empty lines.
| |
34 bool Parse(rtc::Buffer packet); | |
35 // Maps parsed extensions to their types to allow use of GetExtension. | |
36 // Used after parsing when |extensions| can't be provided until base rtp | |
37 // header is parsed. | |
38 void IdentifyExtensions(const ExtensionManager* extensions); | |
39 | |
40 // Header. | |
41 bool Marker() const; | |
42 uint8_t PayloadType() const; | |
43 uint16_t SequenceNumber() const; | |
44 uint32_t Timestamp() const; | |
45 uint32_t Ssrc() const; | |
46 std::vector<uint32_t> Csrcs() const; | |
47 // TODO(danilchap): Remove this function when all code update to use RtpPacket | |
48 // directly. Function is there just for easier backward compatibilty. | |
49 void GetHeader(RTPHeader* header) const; | |
50 // Header extensions. | |
51 template <typename Extension, typename... Values> | |
52 bool GetExtension(Values...) const; | |
53 size_t headers_size() const; | |
54 // Payload. | |
55 size_t payload_size() const; | |
56 size_t padding_size() const; | |
57 const uint8_t* payload() const; | |
58 // Buffer. | |
59 size_t capacity() const; | |
60 size_t size() const; | |
61 const uint8_t* data() const; | |
62 size_t FreeCapacity() const; | |
63 size_t MaxPayloadSize() const; | |
64 | |
65 // Reset fields and buffer. | |
66 void Clear(); | |
67 // Header setters. | |
68 void CopyHeader(const Packet& packet); | |
69 void SetMarker(bool marker_bit); | |
70 void SetPayloadType(uint8_t payload_type); | |
71 void SetSequenceNumber(uint16_t seq_no); | |
72 void SetTimestamp(uint32_t timestamp); | |
73 void SetSsrc(uint32_t ssrc); | |
74 // Writes csrc list. Assumes: | |
75 // a) There is enough room left in buffer. | |
76 // b) Extension headers, payload or padding data has not already been added. | |
77 void SetCsrcs(const std::vector<uint32_t>& csrcs); | |
78 // Header extensions. | |
79 template <typename Extension, typename... Values> | |
80 bool SetExtension(Values...); | |
81 template <typename Extension> | |
82 bool ReserveExtension(); | |
83 // Reserve size_bytes for payload. Returns nullptr on failure. | |
84 uint8_t* AllocatePayload(size_t size_bytes); | |
85 void SetPayloadSize(size_t size_bytes); | |
86 bool SetPadding(uint8_t size_bytes, Random* random); | |
87 | |
88 protected: | |
89 // |extensions| required for SetExtension/ReserveExtension functions during | |
90 // packet creating and used if available in Parse function. | |
91 // Adding and getting extensions will fail until |extensions| is | |
92 // provided via constructor or IdentifyExtensions function. | |
93 explicit Packet(const ExtensionManager* extensions); | |
94 Packet(const ExtensionManager* extensions, size_t capacity); | |
95 virtual ~Packet(); | |
96 | |
97 private: | |
98 struct ExtensionInfo { | |
99 ExtensionType type; | |
100 uint16_t offset; | |
101 uint8_t length; | |
102 }; | |
103 // Helper function for Parse. Fill header fields using data in given buffer, | |
104 // but does not touch packet own buffer, leaving packet in invalid state. | |
105 bool ParseBuffer(const uint8_t* buffer, size_t size); | |
106 // Find an extension based on the type field of the parameter. | |
107 // If found, length field would be validated, the offset field will be set | |
108 // and true returned, | |
109 // otherwise the parameter will be unchanged and false is returned. | |
110 bool FindExtension(ExtensionType type, | |
111 uint8_t length, | |
112 uint16_t* offset) const; | |
113 // Find or allocate an extension, based on the type field of the parameter. | |
114 // If found, the length field be checked against what is already registered | |
115 // and the offset field will be set, then true is returned. If allocated, the | |
116 // length field will be used for allocation and the offset update to indicate | |
117 // position, the true is returned. | |
118 // If not found and allocations fails, false is returned and parameter remains | |
119 // unchanged. | |
120 bool AllocateExtension(ExtensionType type, uint8_t length, uint16_t* offset); | |
121 | |
122 uint8_t* WriteAt(size_t offset); | |
123 void WriteAt(size_t offset, uint8_t byte); | |
124 | |
125 const ExtensionManager* extensions_; | |
126 // Header. | |
127 bool marker_; | |
128 uint8_t payload_type_; | |
129 uint8_t padding_size_; | |
130 uint16_t sequence_number_; | |
131 uint32_t timestamp_; | |
132 uint32_t ssrc_; | |
133 size_t payload_offset_; // Match header size with csrcs and extensions. | |
134 size_t payload_size_; | |
135 | |
136 uint8_t num_extensions_ = 0; | |
137 ExtensionInfo extension_entries_[kMaxExtensionHeaders]; | |
138 uint16_t extensions_size_ = 0; // Unaligned. | |
139 rtc::Buffer buffer_; | |
140 | |
141 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(Packet); | |
142 }; | |
143 | |
144 template <typename Extension, typename... Values> | |
145 bool Packet::GetExtension(Values... values) const { | |
146 uint16_t offset = 0; | |
147 if (!FindExtension(Extension::kId, Extension::kValueSizeBytes, &offset)) | |
148 return false; | |
149 return Extension::Parse(data() + offset, values...); | |
150 } | |
151 | |
152 template <typename Extension, typename... Values> | |
153 bool Packet::SetExtension(Values... values) { | |
154 uint16_t offset = 0; | |
155 if (!AllocateExtension(Extension::kId, Extension::kValueSizeBytes, &offset)) | |
156 return false; | |
157 return Extension::Write(WriteAt(offset), values...); | |
158 } | |
159 | |
160 template <typename Extension> | |
161 bool Packet::ReserveExtension() { | |
162 uint16_t offset = 0; | |
163 if (!AllocateExtension(Extension::kId, Extension::kValueSizeBytes, &offset)) | |
164 return false; | |
165 memset(WriteAt(offset), 0, Extension::kValueSizeBytes); | |
166 return true; | |
167 } | |
168 } // namespace rtp | |
169 } // namespace webrtc | |
170 | |
171 #endif // WEBRTC_MODULES_RTP_RTCP_SOURCE_RTP_PACKET_H_ | |
OLD | NEW |