blob: 3c02125e082bfbce2921bd6057f9f4bcc74d9bdc [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.
99 uint8_t* AllocatePayload(size_t size_bytes);
100 void SetPayloadSize(size_t size_bytes);
101 bool SetPadding(uint8_t size_bytes, Random* random);
102
103 protected:
104 // |extensions| required for SetExtension/ReserveExtension functions during
105 // packet creating and used if available in Parse function.
106 // Adding and getting extensions will fail until |extensions| is
107 // provided via constructor or IdentifyExtensions function.
danilchap70f39a32016-12-16 05:48:18 -0800108 Packet();
danilchap1edb7ab2016-04-20 05:25:10 -0700109 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
danilchap1edb7ab2016-04-20 05:25:10 -0700147 // Header.
148 bool marker_;
149 uint8_t payload_type_;
150 uint8_t padding_size_;
151 uint16_t sequence_number_;
152 uint32_t timestamp_;
153 uint32_t ssrc_;
154 size_t payload_offset_; // Match header size with csrcs and extensions.
155 size_t payload_size_;
156
danilchap1edb7ab2016-04-20 05:25:10 -0700157 ExtensionInfo extension_entries_[kMaxExtensionHeaders];
158 uint16_t extensions_size_ = 0; // Unaligned.
Danil Chapovalov31e4e802016-08-03 18:27:40 +0200159 rtc::CopyOnWriteBuffer buffer_;
danilchap1edb7ab2016-04-20 05:25:10 -0700160};
161
Danil Chapovalov5e57b172016-09-02 19:15:59 +0200162template <typename Extension>
163bool Packet::HasExtension() const {
164 uint16_t offset = 0;
165 return FindExtension(Extension::kId, Extension::kValueSizeBytes, &offset);
166}
167
danilchap1edb7ab2016-04-20 05:25:10 -0700168template <typename Extension, typename... Values>
169bool Packet::GetExtension(Values... values) const {
170 uint16_t offset = 0;
171 if (!FindExtension(Extension::kId, Extension::kValueSizeBytes, &offset))
172 return false;
173 return Extension::Parse(data() + offset, values...);
174}
175
176template <typename Extension, typename... Values>
177bool Packet::SetExtension(Values... values) {
178 uint16_t offset = 0;
179 if (!AllocateExtension(Extension::kId, Extension::kValueSizeBytes, &offset))
180 return false;
181 return Extension::Write(WriteAt(offset), values...);
182}
183
184template <typename Extension>
185bool Packet::ReserveExtension() {
186 uint16_t offset = 0;
187 if (!AllocateExtension(Extension::kId, Extension::kValueSizeBytes, &offset))
188 return false;
189 memset(WriteAt(offset), 0, Extension::kValueSizeBytes);
190 return true;
191}
192} // namespace rtp
193} // namespace webrtc
194
195#endif // WEBRTC_MODULES_RTP_RTCP_SOURCE_RTP_PACKET_H_