blob: f1e3dd40f90cebcf80e3abe2cd4674f06249122c [file] [log] [blame]
danilchap1edb7ab2016-04-20 05:25:10 -07001/*
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"
Danil Chapovalov31e4e802016-08-03 18:27:40 +020016#include "webrtc/base/copyonwritebuffer.h"
danilchap1edb7ab2016-04-20 05:25:10 -070017#include "webrtc/modules/rtp_rtcp/include/rtp_rtcp_defines.h"
18
19namespace webrtc {
20struct RTPHeader;
21class RtpHeaderExtensionMap;
22class Random;
23
24namespace rtp {
25class 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.
Danil Chapovalov31e4e802016-08-03 18:27:40 +020035 bool Parse(rtc::CopyOnWriteBuffer packet);
danilchap1edb7ab2016-04-20 05:25:10 -070036
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.
Danil Chapovalov31e4e802016-08-03 18:27:40 +020062 rtc::CopyOnWriteBuffer Buffer() const;
danilchap1edb7ab2016-04-20 05:25:10 -070063 size_t capacity() const;
64 size_t size() const;
65 const uint8_t* data() const;
66 size_t FreeCapacity() const;
67 size_t MaxPayloadSize() const;
68
69 // Reset fields and buffer.
70 void Clear();
71
72 // Header setters.
Danil Chapovalov31e4e802016-08-03 18:27:40 +020073 void CopyHeaderFrom(const Packet& packet);
danilchap1edb7ab2016-04-20 05:25:10 -070074 void SetMarker(bool marker_bit);
75 void SetPayloadType(uint8_t payload_type);
76 void SetSequenceNumber(uint16_t seq_no);
77 void SetTimestamp(uint32_t timestamp);
78 void SetSsrc(uint32_t ssrc);
79
80 // Writes csrc list. Assumes:
81 // a) There is enough room left in buffer.
82 // b) Extension headers, payload or padding data has not already been added.
83 void SetCsrcs(const std::vector<uint32_t>& csrcs);
84
85 // Header extensions.
86 template <typename Extension, typename... Values>
87 bool GetExtension(Values...) const;
88
89 template <typename Extension, typename... Values>
90 bool SetExtension(Values...);
91
92 template <typename Extension>
93 bool ReserveExtension();
94
95 // Reserve size_bytes for payload. Returns nullptr on failure.
96 uint8_t* AllocatePayload(size_t size_bytes);
97 void SetPayloadSize(size_t size_bytes);
98 bool SetPadding(uint8_t size_bytes, Random* random);
99
100 protected:
101 // |extensions| required for SetExtension/ReserveExtension functions during
102 // packet creating and used if available in Parse function.
103 // Adding and getting extensions will fail until |extensions| is
104 // provided via constructor or IdentifyExtensions function.
105 explicit Packet(const ExtensionManager* extensions);
Danil Chapovalov31e4e802016-08-03 18:27:40 +0200106 Packet(const Packet&) = default;
danilchap1edb7ab2016-04-20 05:25:10 -0700107 Packet(const ExtensionManager* extensions, size_t capacity);
108 virtual ~Packet();
109
Danil Chapovalov31e4e802016-08-03 18:27:40 +0200110 Packet& operator=(const Packet&) = default;
111
danilchap1edb7ab2016-04-20 05:25:10 -0700112 private:
113 struct ExtensionInfo {
114 ExtensionType type;
115 uint16_t offset;
116 uint8_t length;
117 };
118
119 // Helper function for Parse. Fill header fields using data in given buffer,
120 // but does not touch packet own buffer, leaving packet in invalid state.
121 bool ParseBuffer(const uint8_t* buffer, size_t size);
122
123 // Find an extension based on the type field of the parameter.
124 // If found, length field would be validated, the offset field will be set
125 // and true returned,
126 // otherwise the parameter will be unchanged and false is returned.
127 bool FindExtension(ExtensionType type,
128 uint8_t length,
129 uint16_t* offset) const;
130
131 // Find or allocate an extension, based on the type field of the parameter.
132 // If found, the length field be checked against what is already registered
133 // and the offset field will be set, then true is returned. If allocated, the
134 // length field will be used for allocation and the offset update to indicate
135 // position, the true is returned.
136 // If not found and allocations fails, false is returned and parameter remains
137 // unchanged.
138 bool AllocateExtension(ExtensionType type, uint8_t length, uint16_t* offset);
139
140 uint8_t* WriteAt(size_t offset);
141 void WriteAt(size_t offset, uint8_t byte);
142
143 const ExtensionManager* extensions_;
144
145 // Header.
146 bool marker_;
147 uint8_t payload_type_;
148 uint8_t padding_size_;
149 uint16_t sequence_number_;
150 uint32_t timestamp_;
151 uint32_t ssrc_;
152 size_t payload_offset_; // Match header size with csrcs and extensions.
153 size_t payload_size_;
154
155 uint8_t num_extensions_ = 0;
156 ExtensionInfo extension_entries_[kMaxExtensionHeaders];
157 uint16_t extensions_size_ = 0; // Unaligned.
Danil Chapovalov31e4e802016-08-03 18:27:40 +0200158 rtc::CopyOnWriteBuffer buffer_;
danilchap1edb7ab2016-04-20 05:25:10 -0700159
Danil Chapovalov31e4e802016-08-03 18:27:40 +0200160 Packet() = delete;
danilchap1edb7ab2016-04-20 05:25:10 -0700161};
162
163template <typename Extension, typename... Values>
164bool Packet::GetExtension(Values... values) const {
165 uint16_t offset = 0;
166 if (!FindExtension(Extension::kId, Extension::kValueSizeBytes, &offset))
167 return false;
168 return Extension::Parse(data() + offset, values...);
169}
170
171template <typename Extension, typename... Values>
172bool Packet::SetExtension(Values... values) {
173 uint16_t offset = 0;
174 if (!AllocateExtension(Extension::kId, Extension::kValueSizeBytes, &offset))
175 return false;
176 return Extension::Write(WriteAt(offset), values...);
177}
178
179template <typename Extension>
180bool Packet::ReserveExtension() {
181 uint16_t offset = 0;
182 if (!AllocateExtension(Extension::kId, Extension::kValueSizeBytes, &offset))
183 return false;
184 memset(WriteAt(offset), 0, Extension::kValueSizeBytes);
185 return true;
186}
187} // namespace rtp
188} // namespace webrtc
189
190#endif // WEBRTC_MODULES_RTP_RTCP_SOURCE_RTP_PACKET_H_