blob: 75c73cfbcc92ccb07f7a1331a6e6b3345f79d432 [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);
brandtrb29e6522016-12-21 06:37:18 -080034 bool Parse(rtc::ArrayView<const uint8_t> packet);
danilchap1edb7ab2016-04-20 05:25:10 -070035
36 // Parse and move given buffer into Packet.
Danil Chapovalov31e4e802016-08-03 18:27:40 +020037 bool Parse(rtc::CopyOnWriteBuffer packet);
danilchap1edb7ab2016-04-20 05:25:10 -070038
danilchap70f39a32016-12-16 05:48:18 -080039 // Maps extensions id to their types.
40 void IdentifyExtensions(const ExtensionManager& extensions);
danilchap1edb7ab2016-04-20 05:25:10 -070041
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;
danilchap96c15872016-11-21 01:35:29 -080059 rtc::ArrayView<const uint8_t> payload() const;
danilchap1edb7ab2016-04-20 05:25:10 -070060
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.
Danil Chapovalov5e57b172016-09-02 19:15:59 +020086 template <typename Extension>
87 bool HasExtension() const;
88
danilchap1edb7ab2016-04-20 05:25:10 -070089 template <typename Extension, typename... Values>
90 bool GetExtension(Values...) const;
91
92 template <typename Extension, typename... Values>
93 bool SetExtension(Values...);
94
95 template <typename Extension>
96 bool ReserveExtension();
97
98 // Reserve size_bytes for payload. Returns nullptr on failure.
danilchap07a01b32017-03-29 07:33:13 -070099 uint8_t* SetPayloadSize(size_t size_bytes);
100 // Same as SetPayloadSize but doesn't guarantee to keep current payload.
danilchap1edb7ab2016-04-20 05:25:10 -0700101 uint8_t* AllocatePayload(size_t size_bytes);
danilchap1edb7ab2016-04-20 05:25:10 -0700102 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.
danilchap70f39a32016-12-16 05:48:18 -0800109 Packet();
danilchap1edb7ab2016-04-20 05:25:10 -0700110 explicit Packet(const ExtensionManager* extensions);
Danil Chapovalov31e4e802016-08-03 18:27:40 +0200111 Packet(const Packet&) = default;
danilchap1edb7ab2016-04-20 05:25:10 -0700112 Packet(const ExtensionManager* extensions, size_t capacity);
113 virtual ~Packet();
114
Danil Chapovalov31e4e802016-08-03 18:27:40 +0200115 Packet& operator=(const Packet&) = default;
116
danilchap1edb7ab2016-04-20 05:25:10 -0700117 private:
118 struct ExtensionInfo {
119 ExtensionType type;
120 uint16_t offset;
121 uint8_t length;
122 };
123
124 // Helper function for Parse. Fill header fields using data in given buffer,
125 // but does not touch packet own buffer, leaving packet in invalid state.
126 bool ParseBuffer(const uint8_t* buffer, size_t size);
127
128 // Find an extension based on the type field of the parameter.
129 // If found, length field would be validated, the offset field will be set
130 // and true returned,
131 // otherwise the parameter will be unchanged and false is returned.
132 bool FindExtension(ExtensionType type,
133 uint8_t length,
134 uint16_t* offset) const;
135
136 // Find or allocate an extension, based on the type field of the parameter.
137 // If found, the length field be checked against what is already registered
138 // and the offset field will be set, then true is returned. If allocated, the
139 // length field will be used for allocation and the offset update to indicate
140 // position, the true is returned.
141 // If not found and allocations fails, false is returned and parameter remains
142 // unchanged.
143 bool AllocateExtension(ExtensionType type, uint8_t length, uint16_t* offset);
144
145 uint8_t* WriteAt(size_t offset);
146 void WriteAt(size_t offset, uint8_t byte);
147
danilchap1edb7ab2016-04-20 05:25:10 -0700148 // Header.
149 bool marker_;
150 uint8_t payload_type_;
151 uint8_t padding_size_;
152 uint16_t sequence_number_;
153 uint32_t timestamp_;
154 uint32_t ssrc_;
155 size_t payload_offset_; // Match header size with csrcs and extensions.
156 size_t payload_size_;
157
danilchap1edb7ab2016-04-20 05:25:10 -0700158 ExtensionInfo extension_entries_[kMaxExtensionHeaders];
159 uint16_t extensions_size_ = 0; // Unaligned.
Danil Chapovalov31e4e802016-08-03 18:27:40 +0200160 rtc::CopyOnWriteBuffer buffer_;
danilchap1edb7ab2016-04-20 05:25:10 -0700161};
162
Danil Chapovalov5e57b172016-09-02 19:15:59 +0200163template <typename Extension>
164bool Packet::HasExtension() const {
165 uint16_t offset = 0;
166 return FindExtension(Extension::kId, Extension::kValueSizeBytes, &offset);
167}
168
danilchap1edb7ab2016-04-20 05:25:10 -0700169template <typename Extension, typename... Values>
170bool Packet::GetExtension(Values... values) const {
171 uint16_t offset = 0;
172 if (!FindExtension(Extension::kId, Extension::kValueSizeBytes, &offset))
173 return false;
174 return Extension::Parse(data() + offset, values...);
175}
176
177template <typename Extension, typename... Values>
178bool Packet::SetExtension(Values... values) {
179 uint16_t offset = 0;
180 if (!AllocateExtension(Extension::kId, Extension::kValueSizeBytes, &offset))
181 return false;
182 return Extension::Write(WriteAt(offset), values...);
183}
184
185template <typename Extension>
186bool Packet::ReserveExtension() {
187 uint16_t offset = 0;
188 if (!AllocateExtension(Extension::kId, Extension::kValueSizeBytes, &offset))
189 return false;
190 memset(WriteAt(offset), 0, Extension::kValueSizeBytes);
191 return true;
192}
193} // namespace rtp
194} // namespace webrtc
195
196#endif // WEBRTC_MODULES_RTP_RTCP_SOURCE_RTP_PACKET_H_