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

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: style fixes 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 const size_t kMaxExtensionHeaders = 14;
30
31 bool Parse(const uint8_t* buffer, size_t size);
32 bool Parse(rtc::Buffer packet);
33 void SetExtensionManager(const ExtensionManager* extensions);
terelius 2016/04/14 11:36:41 Please document what this functions does or how it
sprang_webrtc 2016/04/14 13:41:50 When do we want to dynamically change extension ma
terelius 2016/04/14 14:05:21 Though afaik you need the SSRC of the incoming pac
danilchap 2016/04/14 14:13:19 After parsing in case of bundling: first need to p
terelius 2016/04/14 14:36:03 Could you add a comment that clarifies that changi
danilchap 2016/04/19 13:54:14 Added, both in text and as a test.
34
35 // Header.
36 bool Marker() const;
37 uint8_t PayloadType() const;
38 uint16_t SequenceNumber() const;
39 uint32_t Timestamp() const;
40 uint32_t Ssrc() const;
41 std::vector<uint32_t> Csrcs() const;
42 // TODO(danilchap): Remove this function when all code update to use RtpPacket
43 // directly. Function is there just for easier backward compatibilty.
44 void GetHeader(RTPHeader* header) const;
45 // Header extensions.
46 template <typename Extension, typename... Values>
47 bool GetExtension(Values...) const;
48 size_t headers_size() const;
49 // Payload.
50 size_t payload_size() const;
51 size_t padding_size() const;
52 const uint8_t* payload() const;
53 // Buffer.
54 size_t capacity() const;
55 size_t size() const;
56 const uint8_t* data() const;
57 size_t FreeCapacity() const;
58 size_t MaxPayloadSize() const;
59
60 // Reset fields and buffer.
61 void Clear();
62 // Header setters.
63 void CopyHeader(const Packet& packet);
64 void SetMarker(bool marker_bit);
65 void SetPayloadType(uint8_t payload_type);
66 void SetSequenceNumber(uint16_t seq_no);
67 void SetTimestamp(uint32_t timestamp);
68 void SetSsrc(uint32_t ssrc);
69 // Writes csrc list. Assumes:
70 // a) There is enough room left in buffer.
71 // b) Extension headers, payload or padding data has not already been added.
72 void SetCsrcs(const std::vector<uint32_t>& csrcs);
73 // Header extensions.
74 template <typename Extension, typename... Values>
75 bool SetExtension(Values...);
76 template <typename Extension>
77 bool ReserveExtension();
78 // Reserve size_bytes for payload. Returns nullptr on failure.
79 uint8_t* AllocatePayload(size_t size_bytes);
80 void SetPayloadSize(size_t size_bytes);
81 bool SetPadding(uint8_t size_bytes, Random* random);
82
83 protected:
84 explicit Packet(const ExtensionManager* extensions);
terelius 2016/04/14 11:36:41 Am I allowed to call the constructors with a nullp
sprang_webrtc 2016/04/14 13:41:50 Looks like no (there's a dcheck) and no. You'll pr
terelius 2016/04/14 14:05:21 I can't see any DCHECK in the constructor, so I as
danilchap 2016/04/14 14:13:19 There is no default constructor, but it is allowed
terelius 2016/04/14 14:36:03 Acknowledged. Could you add a short comment about
danilchap 2016/04/19 13:54:14 Done.
85 Packet(const ExtensionManager* extensions, size_t capacity);
86 virtual ~Packet();
87
88 private:
89 struct ExtensionInfo {
90 ExtensionType type;
91 uint16_t offset;
92 uint8_t length;
93 };
94 // Helper function for Parse. Fill header fields using data in given buffer,
95 // but does not touch packet own buffer, leaving packet in invalid state.
96 bool ParseBuffer(const uint8_t* buffer, size_t size);
97 // Find an extension based on the type field of the parameter.
98 // If found, length field would be validated, the offset field will be set
99 // and true returned,
100 // otherwise the parameter will be unchanged and false is returned.
101 bool FindExtension(ExtensionType type,
102 uint8_t length,
103 uint16_t* offset) const;
104 // Find or allocate an extension, based on the type field of the parameter.
105 // If found, the length field be checked against what is already registered
106 // and the offset field will be set, then true is returned. If allocated, the
107 // length field will be used for allocation and the offset update to indicate
108 // position, the true is returned.
109 // If not found and allocations fails, false is returned and parameter remains
110 // unchanged.
111 bool AllocateExtension(ExtensionType type, uint8_t length, uint16_t* offset);
112
113 uint8_t* WriteAt(size_t offset);
114 void WriteAt(size_t offset, uint8_t byte);
115
116 const ExtensionManager* extensions_;
117 // Header.
118 bool marker_;
119 uint8_t payload_type_;
120 uint8_t padding_size_;
121 uint16_t sequence_number_;
122 uint32_t timestamp_;
123 uint32_t ssrc_;
124 size_t payload_offset_; // Match header size with csrcs and extensions.
125 size_t payload_size_;
126
127 uint8_t num_extensions_ = 0;
128 ExtensionInfo extension_entries_[kMaxExtensionHeaders];
129 uint16_t extensions_size_ = 0; // Unaligned.
130 rtc::Buffer buffer_;
131
132 RTC_DISALLOW_COPY_AND_ASSIGN(Packet);
133 };
134
135 template <typename Extension, typename... Values>
136 bool Packet::GetExtension(Values... values) const {
137 uint16_t offset = 0;
138 if (!FindExtension(Extension::kId, Extension::kValueSizeBytes, &offset))
139 return false;
140 return Extension::Parse(data() + offset, values...);
141 }
142
143 template <typename Extension, typename... Values>
144 bool Packet::SetExtension(Values... values) {
145 uint16_t offset = 0;
146 if (!AllocateExtension(Extension::kId, Extension::kValueSizeBytes, &offset))
147 return false;
148 return Extension::Write(WriteAt(offset), values...);
149 }
150
151 template <typename Extension>
152 bool Packet::ReserveExtension() {
153 uint16_t offset = 0;
154 if (!AllocateExtension(Extension::kId, Extension::kValueSizeBytes, &offset))
155 return false;
156 memset(WriteAt(offset), 0, Extension::kValueSizeBytes);
157 return true;
158 }
159 } // namespace rtp
160 } // namespace webrtc
161
162 #endif // WEBRTC_MODULES_RTP_RTCP_SOURCE_RTP_PACKET_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698