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/base/checks.h" | |
18 #include "webrtc/common_types.h" | |
19 #include "webrtc/modules/rtp_rtcp/include/rtp_rtcp_defines.h" | |
20 #include "webrtc/system_wrappers/include/ntp_time.h" | |
21 | |
22 namespace webrtc { | |
23 class RtpHeaderExtensionMap; | |
24 class Random; | |
25 | |
26 namespace rtp { | |
27 class Packet { | |
28 public: | |
29 using ExtensionType = RTPExtensionType; | |
30 using ExtensionManager = RtpHeaderExtensionMap; | |
31 static const size_t kMaxExtensionHeaders = 14; | |
32 | |
33 bool Parse(const uint8_t* buffer, size_t size); | |
34 bool Parse(rtc::Buffer packet); | |
35 | |
36 // Header. | |
37 bool Marker() const; | |
38 uint8_t PayloadType() const; | |
39 uint16_t SequenceNumber() const; | |
40 uint32_t Timestamp() const; | |
41 uint32_t Ssrc() const; | |
42 std::vector<uint32_t> Csrcs() const; | |
43 // TODO(danilchap): Remove this function when all code update to use RtpPacket | |
44 // directly. Function is there just for easier backward compatibilty. | |
45 void GetHeader(RTPHeader* header) const; | |
46 // Header extensions. | |
47 template <typename Extension, typename... Values> | |
48 bool GetExtension(Values...) const; | |
49 size_t HeadersSize() const; | |
50 // Payload. | |
51 size_t PayloadSize() const; | |
52 size_t PaddingSize() const; | |
53 const uint8_t* Payload() const; | |
54 // Buffer. | |
55 size_t capacity() const; | |
56 size_t size() const; | |
57 const uint8_t* data() const; | |
58 size_t FreeCapacity() const { return capacity() - size(); } | |
59 size_t MaxPayloadSize() const { return capacity() - payload_offset_; } | |
60 | |
61 // Reset fields and buffer. | |
62 void Clear(); | |
63 // Header setters. | |
64 void CopyHeader(const Packet& packet); | |
65 void SetMarker(bool marker_bit); | |
66 void SetPayloadType(uint8_t payload_type); | |
67 void SetSequenceNumber(uint16_t seq_no); | |
68 void SetTimestamp(uint32_t timestamp); | |
69 void SetSsrc(uint32_t ssrc); | |
70 // Writes csrc list. Assumes: | |
71 // a) There is enough room left in buffer. | |
72 // b) Extension headers, payload or padding data has not already been added. | |
73 void SetCsrcs(const std::vector<uint32_t>& csrcs); | |
74 // Header extensions. | |
75 template <typename Extension, typename... Values> | |
76 bool SetExtension(Values...); | |
77 template <typename Extension> | |
78 bool ReserveExtension(); | |
79 // Reserve size_bytes for payload. Returns nullptr on failure. | |
80 uint8_t* AllocatePayload(size_t size_bytes); | |
81 void SetPayloadSize(size_t size_bytes); | |
82 bool SetPadding(uint8_t size_bytes, Random* random); | |
83 | |
84 protected: | |
85 explicit Packet(const ExtensionManager* extensions); | |
terelius
2016/04/13 13:17:00
So you have to know what header extensions are use
danilchap
2016/04/13 16:18:34
Good point.
Adjusted. Added function SetExtensionM
| |
86 Packet(const ExtensionManager* extensions, size_t capacity); | |
87 virtual ~Packet(); | |
88 | |
89 private: | |
90 struct ExtensionInfo { | |
91 ExtensionType type; | |
92 uint16_t offset; | |
93 uint8_t length; | |
94 }; | |
95 // Tries to Parse data in given buffer. | |
96 // Does not touch packet own buffer, leaving packet in desync state on return. | |
terelius
2016/04/13 13:17:00
I don't understand what this comment means. Update
danilchap
2016/04/13 16:18:34
this function is private, you shouldn't call it al
| |
97 bool ParseBuffer(const uint8_t* buffer, size_t size); | |
98 // Find and extension, based on the type field of the parameter. | |
philipel
2016/04/13 12:01:19
Find an extension
danilchap
2016/04/13 16:18:34
Done.
| |
99 // If found, length field would be validated, the offset field will be set | |
100 // and true returned, | |
101 // otherwise the parameter will be unchanged and false is returned. | |
102 bool FindExtension(ExtensionType type, | |
103 uint8_t length, | |
104 uint16_t* offset) const; | |
105 // Find or allocate an extension, based on the type field of the parameter. | |
106 // If found, the length field be checked against what is already registered | |
107 // and the offset field will be set, then true is returned. If allocated, the | |
108 // length field will be used for allocation and the offset update to indicate | |
109 // position, the true is returned. | |
110 // If not found and allocations fails, false is returned and paramter remains | |
terelius
2016/04/13 13:17:00
nit: parameter
danilchap
2016/04/13 16:18:34
Done.
| |
111 // unchanged. | |
112 bool AllocateExtension(ExtensionType type, uint8_t length, uint16_t* offset); | |
113 | |
114 uint8_t* WriteAt(size_t offset) { return buffer_.data() + offset; } | |
philipel
2016/04/13 12:01:19
Move implementation to .cc file.
danilchap
2016/04/13 16:18:34
Done.
| |
115 void WriteAt(size_t offset, uint8_t byte) { buffer_.data()[offset] = byte; } | |
philipel
2016/04/13 12:01:19
Ditto.
danilchap
2016/04/13 16:18:34
Done.
| |
116 | |
117 const ExtensionManager* const extensions_; | |
118 // Header. | |
119 bool marker_; | |
120 uint8_t payload_type_; | |
121 uint8_t padding_size_; | |
122 uint16_t sequence_number_; | |
123 uint32_t timestamp_; | |
124 uint32_t ssrc_; | |
125 size_t payload_offset_; // Match header size with csrcs and extensions. | |
126 size_t payload_size_; | |
127 | |
128 uint8_t num_extensions_ = 0; | |
129 ExtensionInfo extension_entries_[kMaxExtensionHeaders]; | |
130 uint16_t extensions_size_ = 0; // Unaligned. | |
131 rtc::Buffer buffer_; | |
132 | |
133 RTC_DISALLOW_COPY_AND_ASSIGN(Packet); | |
134 }; | |
135 | |
136 template <typename Extension, typename... Values> | |
137 bool Packet::GetExtension(Values... values) const { | |
138 uint16_t offset = 0; | |
139 if (!FindExtension(Extension::kId, Extension::kValueSizeBytes, &offset)) | |
140 return false; | |
141 return Extension::Parse(data() + offset, values...); | |
142 } | |
143 | |
144 template <typename Extension, typename... Values> | |
145 bool Packet::SetExtension(Values... values) { | |
146 uint16_t offset = 0; | |
147 if (!AllocateExtension(Extension::kId, Extension::kValueSizeBytes, &offset)) | |
148 return false; | |
149 return Extension::Write(WriteAt(offset), values...); | |
150 } | |
151 | |
152 template <typename Extension> | |
153 bool Packet::ReserveExtension() { | |
154 uint16_t offset = 0; | |
155 if (!AllocateExtension(Extension::kId, Extension::kValueSizeBytes, &offset)) | |
156 return false; | |
157 memset(WriteAt(offset), 0, Extension::kValueSizeBytes); | |
158 return true; | |
159 } | |
160 } // namespace rtp | |
161 | |
162 // Class to hold rtp packet with metadata for sender side. | |
163 class RtpPacket : public rtp::Packet { | |
philipel
2016/04/13 12:01:19
Rename to SendRtpPacket or something that indicate
danilchap
2016/04/13 16:18:34
Done.
| |
164 public: | |
165 explicit RtpPacket(const ExtensionManager* extensions) : Packet(extensions) {} | |
166 RtpPacket(const ExtensionManager* extensions, size_t capacity) | |
167 : Packet(extensions, capacity) {} | |
philipel
2016/04/13 12:01:19
Why is the implementation for RtpPacket and Receiv
danilchap
2016/04/13 16:18:34
Definition is here for no good reason, moved to ow
philipel
2016/04/14 10:32:02
Acknowledged.
| |
168 | |
169 // Time in local time base as close as it can to frame capture time. | |
170 int64_t capture_time_ms() const { return capture_time_ms_; } | |
171 void set_capture_time_ms(int64_t time) { capture_time_ms_ = time; } | |
172 | |
173 private: | |
174 int64_t capture_time_ms_ = 0; | |
175 }; | |
176 | |
177 // Class to hold rtp packet with metadata for receiver side. | |
178 class ReceivedRtpPacket : public rtp::Packet { | |
179 public: | |
180 explicit ReceivedRtpPacket(const ExtensionManager* extensions) | |
181 : Packet(extensions) {} | |
182 | |
183 void GetHeader(RTPHeader* header) const; | |
184 // Time in local time base as close as it can to packet arrived on the | |
185 // network. | |
186 int64_t arrival_time_ms() const { return arrival_time_ms_; } | |
187 void set_arrival_time_ms(int64_t time) { arrival_time_ms_ = time; } | |
188 | |
189 // Estimated from Timestamp() using rtcp Sender Reports. | |
190 NtpTime capture_ntp_time() const { return capture_time_; } | |
191 void set_capture_ntp_time(NtpTime time) { capture_time_ = time; } | |
192 | |
193 // Flag if packet arrived via rtx. | |
194 bool retransmit() const { return retransmit_; } | |
195 void set_retransmit(bool value) { retransmit_ = value; } | |
196 | |
197 int payload_type_frequency() const { return payload_type_frequency_; } | |
198 void set_payload_type_frequency(int value) { | |
199 payload_type_frequency_ = value; | |
200 } | |
201 | |
202 private: | |
203 NtpTime capture_time_; | |
204 int64_t arrival_time_ms_ = 0; | |
205 int payload_type_frequency_ = 0; | |
206 bool retransmit_ = false; | |
207 }; | |
208 } // namespace webrtc | |
209 | |
210 #endif // WEBRTC_MODULES_RTP_RTCP_SOURCE_RTP_PACKET_H_ | |
OLD | NEW |