Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(105)

Side by Side Diff: webrtc/modules/rtp_rtcp/source/rtp_packet.h

Issue 1841453004: RtpPacket class introduced. (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: added empty lines Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 constexpr size_t kMaxExtensionHeaders = 14;
30
31 // Parse and copy given buffer into Packet.
32 bool Parse(const uint8_t* buffer, size_t size);
33
34 // Parse and move given buffer into Packet.
35 bool Parse(rtc::Buffer packet);
36
37 // Maps parsed extensions to their types to allow use of GetExtension.
38 // Used after parsing when |extensions| can't be provided until base rtp
39 // header is parsed.
40 void IdentifyExtensions(const ExtensionManager* extensions);
41
42 // Header.
43 bool Marker() const;
44 uint8_t PayloadType() const;
45 uint16_t SequenceNumber() const;
46 uint32_t Timestamp() const;
47 uint32_t Ssrc() const;
48 std::vector<uint32_t> Csrcs() const;
49
50 // TODO(danilchap): Remove this function when all code update to use RtpPacket
51 // directly. Function is there just for easier backward compatibilty.
52 void GetHeader(RTPHeader* header) const;
53
54 size_t headers_size() const;
55
56 // Payload.
57 size_t payload_size() const;
58 size_t padding_size() const;
59 const uint8_t* payload() const;
60
61 // Buffer.
62 size_t capacity() const;
63 size_t size() const;
64 const uint8_t* data() const;
65 size_t FreeCapacity() const;
66 size_t MaxPayloadSize() const;
67
68 // Reset fields and buffer.
69 void Clear();
70
71 // Header setters.
72 void CopyHeader(const Packet& packet);
73 void SetMarker(bool marker_bit);
74 void SetPayloadType(uint8_t payload_type);
75 void SetSequenceNumber(uint16_t seq_no);
76 void SetTimestamp(uint32_t timestamp);
77 void SetSsrc(uint32_t ssrc);
78
79 // Writes csrc list. Assumes:
80 // a) There is enough room left in buffer.
81 // b) Extension headers, payload or padding data has not already been added.
82 void SetCsrcs(const std::vector<uint32_t>& csrcs);
83
84 // Header extensions.
85 template <typename Extension, typename... Values>
86 bool GetExtension(Values...) const;
87
88 template <typename Extension, typename... Values>
89 bool SetExtension(Values...);
90
91 template <typename Extension>
92 bool ReserveExtension();
93
94 // Reserve size_bytes for payload. Returns nullptr on failure.
95 uint8_t* AllocatePayload(size_t size_bytes);
96 void SetPayloadSize(size_t size_bytes);
97 bool SetPadding(uint8_t size_bytes, Random* random);
98
99 protected:
100 // |extensions| required for SetExtension/ReserveExtension functions during
101 // packet creating and used if available in Parse function.
102 // Adding and getting extensions will fail until |extensions| is
103 // provided via constructor or IdentifyExtensions function.
104 explicit Packet(const ExtensionManager* extensions);
105 Packet(const ExtensionManager* extensions, size_t capacity);
106 virtual ~Packet();
107
108 private:
109 struct ExtensionInfo {
110 ExtensionType type;
111 uint16_t offset;
112 uint8_t length;
113 };
114
115 // Helper function for Parse. Fill header fields using data in given buffer,
116 // but does not touch packet own buffer, leaving packet in invalid state.
117 bool ParseBuffer(const uint8_t* buffer, size_t size);
118
119 // Find an extension based on the type field of the parameter.
120 // If found, length field would be validated, the offset field will be set
121 // and true returned,
122 // otherwise the parameter will be unchanged and false is returned.
123 bool FindExtension(ExtensionType type,
124 uint8_t length,
125 uint16_t* offset) const;
126
127 // Find or allocate an extension, based on the type field of the parameter.
128 // If found, the length field be checked against what is already registered
129 // and the offset field will be set, then true is returned. If allocated, the
130 // length field will be used for allocation and the offset update to indicate
131 // position, the true is returned.
132 // If not found and allocations fails, false is returned and parameter remains
133 // unchanged.
134 bool AllocateExtension(ExtensionType type, uint8_t length, uint16_t* offset);
135
136 uint8_t* WriteAt(size_t offset);
137 void WriteAt(size_t offset, uint8_t byte);
138
139 const ExtensionManager* extensions_;
140
141 // Header.
142 bool marker_;
143 uint8_t payload_type_;
144 uint8_t padding_size_;
145 uint16_t sequence_number_;
146 uint32_t timestamp_;
147 uint32_t ssrc_;
148 size_t payload_offset_; // Match header size with csrcs and extensions.
149 size_t payload_size_;
150
151 uint8_t num_extensions_ = 0;
152 ExtensionInfo extension_entries_[kMaxExtensionHeaders];
153 uint16_t extensions_size_ = 0; // Unaligned.
154 rtc::Buffer buffer_;
155
156 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(Packet);
157 };
158
159 template <typename Extension, typename... Values>
160 bool Packet::GetExtension(Values... values) const {
161 uint16_t offset = 0;
162 if (!FindExtension(Extension::kId, Extension::kValueSizeBytes, &offset))
163 return false;
164 return Extension::Parse(data() + offset, values...);
165 }
166
167 template <typename Extension, typename... Values>
168 bool Packet::SetExtension(Values... values) {
169 uint16_t offset = 0;
170 if (!AllocateExtension(Extension::kId, Extension::kValueSizeBytes, &offset))
171 return false;
172 return Extension::Write(WriteAt(offset), values...);
173 }
174
175 template <typename Extension>
176 bool Packet::ReserveExtension() {
177 uint16_t offset = 0;
178 if (!AllocateExtension(Extension::kId, Extension::kValueSizeBytes, &offset))
179 return false;
180 memset(WriteAt(offset), 0, Extension::kValueSizeBytes);
181 return true;
182 }
183 } // namespace rtp
184 } // namespace webrtc
185
186 #endif // WEBRTC_MODULES_RTP_RTCP_SOURCE_RTP_PACKET_H_
OLDNEW
« no previous file with comments | « webrtc/modules/rtp_rtcp/source/rtp_header_extensions.cc ('k') | webrtc/modules/rtp_rtcp/source/rtp_packet.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698