blob: 3f4d5769ba9156d5c399941a2dc0f157256921e1 [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
danilchap96c15872016-11-21 01:35:29 -080015#include "webrtc/base/array_view.h"
danilchap1edb7ab2016-04-20 05:25:10 -070016#include "webrtc/base/basictypes.h"
Danil Chapovalov31e4e802016-08-03 18:27:40 +020017#include "webrtc/base/copyonwritebuffer.h"
danilchap1edb7ab2016-04-20 05:25:10 -070018#include "webrtc/modules/rtp_rtcp/include/rtp_rtcp_defines.h"
19
20namespace webrtc {
21struct RTPHeader;
22class RtpHeaderExtensionMap;
23class Random;
24
25namespace rtp {
26class Packet {
27 public:
28 using ExtensionType = RTPExtensionType;
29 using ExtensionManager = RtpHeaderExtensionMap;
30 static constexpr size_t kMaxExtensionHeaders = 14;
31
32 // Parse and copy given buffer into Packet.
33 bool Parse(const uint8_t* buffer, size_t size);
34
35 // Parse and move given buffer into Packet.
Danil Chapovalov31e4e802016-08-03 18:27:40 +020036 bool Parse(rtc::CopyOnWriteBuffer packet);
danilchap1edb7ab2016-04-20 05:25:10 -070037
38 // Maps parsed extensions to their types to allow use of GetExtension.
39 // Used after parsing when |extensions| can't be provided until base rtp
40 // header is parsed.
41 void IdentifyExtensions(const ExtensionManager* extensions);
42
43 // Header.
44 bool Marker() const;
45 uint8_t PayloadType() const;
46 uint16_t SequenceNumber() const;
47 uint32_t Timestamp() const;
48 uint32_t Ssrc() const;
49 std::vector<uint32_t> Csrcs() const;
50
51 // TODO(danilchap): Remove this function when all code update to use RtpPacket
52 // directly. Function is there just for easier backward compatibilty.
53 void GetHeader(RTPHeader* header) const;
54
55 size_t headers_size() const;
56
57 // Payload.
58 size_t payload_size() const;
59 size_t padding_size() const;
danilchap96c15872016-11-21 01:35:29 -080060 rtc::ArrayView<const uint8_t> payload() const;
danilchap1edb7ab2016-04-20 05:25:10 -070061
62 // Buffer.
Danil Chapovalov31e4e802016-08-03 18:27:40 +020063 rtc::CopyOnWriteBuffer Buffer() const;
danilchap1edb7ab2016-04-20 05:25:10 -070064 size_t capacity() const;
65 size_t size() const;
66 const uint8_t* data() const;
67 size_t FreeCapacity() const;
68 size_t MaxPayloadSize() const;
69
70 // Reset fields and buffer.
71 void Clear();
72
73 // Header setters.
Danil Chapovalov31e4e802016-08-03 18:27:40 +020074 void CopyHeaderFrom(const Packet& packet);
danilchap1edb7ab2016-04-20 05:25:10 -070075 void SetMarker(bool marker_bit);
76 void SetPayloadType(uint8_t payload_type);
77 void SetSequenceNumber(uint16_t seq_no);
78 void SetTimestamp(uint32_t timestamp);
79 void SetSsrc(uint32_t ssrc);
80
81 // Writes csrc list. Assumes:
82 // a) There is enough room left in buffer.
83 // b) Extension headers, payload or padding data has not already been added.
84 void SetCsrcs(const std::vector<uint32_t>& csrcs);
85
86 // Header extensions.
Danil Chapovalov5e57b172016-09-02 19:15:59 +020087 template <typename Extension>
88 bool HasExtension() const;
89
danilchap1edb7ab2016-04-20 05:25:10 -070090 template <typename Extension, typename... Values>
91 bool GetExtension(Values...) const;
92
93 template <typename Extension, typename... Values>
94 bool SetExtension(Values...);
95
96 template <typename Extension>
97 bool ReserveExtension();
98
99 // Reserve size_bytes for payload. Returns nullptr on failure.
100 uint8_t* AllocatePayload(size_t size_bytes);
101 void SetPayloadSize(size_t size_bytes);
102 bool SetPadding(uint8_t size_bytes, Random* random);
103
104 protected:
105 // |extensions| required for SetExtension/ReserveExtension functions during
106 // packet creating and used if available in Parse function.
107 // Adding and getting extensions will fail until |extensions| is
108 // provided via constructor or IdentifyExtensions function.
109 explicit Packet(const ExtensionManager* extensions);
Danil Chapovalov31e4e802016-08-03 18:27:40 +0200110 Packet(const Packet&) = default;
danilchap1edb7ab2016-04-20 05:25:10 -0700111 Packet(const ExtensionManager* extensions, size_t capacity);
112 virtual ~Packet();
113
Danil Chapovalov31e4e802016-08-03 18:27:40 +0200114 Packet& operator=(const Packet&) = default;
115
danilchap1edb7ab2016-04-20 05:25:10 -0700116 private:
117 struct ExtensionInfo {
118 ExtensionType type;
119 uint16_t offset;
120 uint8_t length;
121 };
122
123 // Helper function for Parse. Fill header fields using data in given buffer,
124 // but does not touch packet own buffer, leaving packet in invalid state.
125 bool ParseBuffer(const uint8_t* buffer, size_t size);
126
127 // Find an extension based on the type field of the parameter.
128 // If found, length field would be validated, the offset field will be set
129 // and true returned,
130 // otherwise the parameter will be unchanged and false is returned.
131 bool FindExtension(ExtensionType type,
132 uint8_t length,
133 uint16_t* offset) const;
134
135 // Find or allocate an extension, based on the type field of the parameter.
136 // If found, the length field be checked against what is already registered
137 // and the offset field will be set, then true is returned. If allocated, the
138 // length field will be used for allocation and the offset update to indicate
139 // position, the true is returned.
140 // If not found and allocations fails, false is returned and parameter remains
141 // unchanged.
142 bool AllocateExtension(ExtensionType type, uint8_t length, uint16_t* offset);
143
144 uint8_t* WriteAt(size_t offset);
145 void WriteAt(size_t offset, uint8_t byte);
146
147 const ExtensionManager* extensions_;
148
149 // Header.
150 bool marker_;
151 uint8_t payload_type_;
152 uint8_t padding_size_;
153 uint16_t sequence_number_;
154 uint32_t timestamp_;
155 uint32_t ssrc_;
156 size_t payload_offset_; // Match header size with csrcs and extensions.
157 size_t payload_size_;
158
159 uint8_t num_extensions_ = 0;
160 ExtensionInfo extension_entries_[kMaxExtensionHeaders];
161 uint16_t extensions_size_ = 0; // Unaligned.
Danil Chapovalov31e4e802016-08-03 18:27:40 +0200162 rtc::CopyOnWriteBuffer buffer_;
danilchap1edb7ab2016-04-20 05:25:10 -0700163
Danil Chapovalov31e4e802016-08-03 18:27:40 +0200164 Packet() = delete;
danilchap1edb7ab2016-04-20 05:25:10 -0700165};
166
Danil Chapovalov5e57b172016-09-02 19:15:59 +0200167template <typename Extension>
168bool Packet::HasExtension() const {
169 uint16_t offset = 0;
170 return FindExtension(Extension::kId, Extension::kValueSizeBytes, &offset);
171}
172
danilchap1edb7ab2016-04-20 05:25:10 -0700173template <typename Extension, typename... Values>
174bool Packet::GetExtension(Values... values) const {
175 uint16_t offset = 0;
176 if (!FindExtension(Extension::kId, Extension::kValueSizeBytes, &offset))
177 return false;
178 return Extension::Parse(data() + offset, values...);
179}
180
181template <typename Extension, typename... Values>
182bool Packet::SetExtension(Values... values) {
183 uint16_t offset = 0;
184 if (!AllocateExtension(Extension::kId, Extension::kValueSizeBytes, &offset))
185 return false;
186 return Extension::Write(WriteAt(offset), values...);
187}
188
189template <typename Extension>
190bool Packet::ReserveExtension() {
191 uint16_t offset = 0;
192 if (!AllocateExtension(Extension::kId, Extension::kValueSizeBytes, &offset))
193 return false;
194 memset(WriteAt(offset), 0, Extension::kValueSizeBytes);
195 return true;
196}
197} // namespace rtp
198} // namespace webrtc
199
200#endif // WEBRTC_MODULES_RTP_RTCP_SOURCE_RTP_PACKET_H_