blob: c49e0709a3f4e29735e90646731f052159e68f96 [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 */
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020010#ifndef MODULES_RTP_RTCP_SOURCE_RTP_PACKET_H_
11#define MODULES_RTP_RTCP_SOURCE_RTP_PACKET_H_
danilchap1edb7ab2016-04-20 05:25:10 -070012
Anton Sukhanovff25b872019-07-09 13:04:07 -070013#include <string>
danilchap1edb7ab2016-04-20 05:25:10 -070014#include <vector>
15
Danil Chapovalovc5dd3002018-11-08 15:12:45 +010016#include "absl/types/optional.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#include "api/array_view.h"
Johannes Kronc5744b82018-09-24 14:50:48 +020018#include "modules/rtp_rtcp/include/rtp_header_extension_map.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#include "modules/rtp_rtcp/include/rtp_rtcp_defines.h"
Steve Anton10542f22019-01-11 09:11:00 -080020#include "rtc_base/copy_on_write_buffer.h"
danilchap1edb7ab2016-04-20 05:25:10 -070021
22namespace webrtc {
danilchap1edb7ab2016-04-20 05:25:10 -070023
Danil Chapovalov8769e172017-09-14 14:08:22 +020024class RtpPacket {
danilchap1edb7ab2016-04-20 05:25:10 -070025 public:
26 using ExtensionType = RTPExtensionType;
27 using ExtensionManager = RtpHeaderExtensionMap;
danilchap1edb7ab2016-04-20 05:25:10 -070028
eladalon87443ee2017-09-13 02:45:15 -070029 // |extensions| required for SetExtension/ReserveExtension functions during
30 // packet creating and used if available in Parse function.
31 // Adding and getting extensions will fail until |extensions| is
32 // provided via constructor or IdentifyExtensions function.
Danil Chapovalov8769e172017-09-14 14:08:22 +020033 RtpPacket();
34 explicit RtpPacket(const ExtensionManager* extensions);
35 RtpPacket(const RtpPacket&);
36 RtpPacket(const ExtensionManager* extensions, size_t capacity);
37 ~RtpPacket();
eladalon87443ee2017-09-13 02:45:15 -070038
Danil Chapovalov8769e172017-09-14 14:08:22 +020039 RtpPacket& operator=(const RtpPacket&) = default;
eladalon87443ee2017-09-13 02:45:15 -070040
danilchap1edb7ab2016-04-20 05:25:10 -070041 // Parse and copy given buffer into Packet.
Anton Sukhanovff25b872019-07-09 13:04:07 -070042 // Does not require extension map to be registered (map is only required to
43 // read or allocate extensions in methods GetExtension, AllocateExtension,
44 // etc.)
danilchap1edb7ab2016-04-20 05:25:10 -070045 bool Parse(const uint8_t* buffer, size_t size);
brandtrb29e6522016-12-21 06:37:18 -080046 bool Parse(rtc::ArrayView<const uint8_t> packet);
danilchap1edb7ab2016-04-20 05:25:10 -070047
48 // Parse and move given buffer into Packet.
Danil Chapovalov31e4e802016-08-03 18:27:40 +020049 bool Parse(rtc::CopyOnWriteBuffer packet);
danilchap1edb7ab2016-04-20 05:25:10 -070050
danilchap70f39a32016-12-16 05:48:18 -080051 // Maps extensions id to their types.
52 void IdentifyExtensions(const ExtensionManager& extensions);
danilchap1edb7ab2016-04-20 05:25:10 -070053
54 // Header.
Danil Chapovalov7d2df3f2018-08-14 17:18:07 +020055 bool Marker() const { return marker_; }
56 uint8_t PayloadType() const { return payload_type_; }
57 uint16_t SequenceNumber() const { return sequence_number_; }
58 uint32_t Timestamp() const { return timestamp_; }
59 uint32_t Ssrc() const { return ssrc_; }
danilchap1edb7ab2016-04-20 05:25:10 -070060 std::vector<uint32_t> Csrcs() const;
61
Danil Chapovalov7d2df3f2018-08-14 17:18:07 +020062 size_t headers_size() const { return payload_offset_; }
danilchap1edb7ab2016-04-20 05:25:10 -070063
64 // Payload.
Danil Chapovalov7d2df3f2018-08-14 17:18:07 +020065 size_t payload_size() const { return payload_size_; }
66 size_t padding_size() const { return padding_size_; }
67 rtc::ArrayView<const uint8_t> payload() const {
68 return rtc::MakeArrayView(data() + payload_offset_, payload_size_);
69 }
danilchap1edb7ab2016-04-20 05:25:10 -070070
71 // Buffer.
Danil Chapovalov7d2df3f2018-08-14 17:18:07 +020072 rtc::CopyOnWriteBuffer Buffer() const { return buffer_; }
73 size_t capacity() const { return buffer_.capacity(); }
74 size_t size() const {
75 return payload_offset_ + payload_size_ + padding_size_;
76 }
77 const uint8_t* data() const { return buffer_.cdata(); }
78 size_t FreeCapacity() const { return capacity() - size(); }
79 size_t MaxPayloadSize() const { return capacity() - headers_size(); }
danilchap1edb7ab2016-04-20 05:25:10 -070080
81 // Reset fields and buffer.
82 void Clear();
83
84 // Header setters.
Danil Chapovalov8769e172017-09-14 14:08:22 +020085 void CopyHeaderFrom(const RtpPacket& packet);
danilchap1edb7ab2016-04-20 05:25:10 -070086 void SetMarker(bool marker_bit);
87 void SetPayloadType(uint8_t payload_type);
88 void SetSequenceNumber(uint16_t seq_no);
89 void SetTimestamp(uint32_t timestamp);
90 void SetSsrc(uint32_t ssrc);
91
Ilya Nikolaevskiy082696e2019-09-03 07:52:52 +000092 // Copies the buffer with zero-ed mutable extensions,
Ilya Nikolaevskiy2d821c32019-06-26 14:39:36 +020093 // which are modified after FEC protection is generated.
Ilya Nikolaevskiy082696e2019-09-03 07:52:52 +000094 void CopyAndZeroMutableExtensions(rtc::ArrayView<uint8_t> buffer) const;
Ilya Nikolaevskiy2d821c32019-06-26 14:39:36 +020095
Anton Sukhanovff25b872019-07-09 13:04:07 -070096 // Removes extension of given |type|, returns false is extension was not
97 // registered in packet's extension map or not present in the packet. Only
98 // extension that should be removed must be registered, other extensions may
99 // not be registered and will be preserved as is.
100 bool RemoveExtension(ExtensionType type);
101
danilchap1edb7ab2016-04-20 05:25:10 -0700102 // Writes csrc list. Assumes:
103 // a) There is enough room left in buffer.
104 // b) Extension headers, payload or padding data has not already been added.
Danil Chapovalov7d2df3f2018-08-14 17:18:07 +0200105 void SetCsrcs(rtc::ArrayView<const uint32_t> csrcs);
danilchap1edb7ab2016-04-20 05:25:10 -0700106
107 // Header extensions.
Danil Chapovalov5e57b172016-09-02 19:15:59 +0200108 template <typename Extension>
109 bool HasExtension() const;
Amit Hilbuch77938e62018-12-21 09:23:38 -0800110 bool HasExtension(ExtensionType type) const;
Danil Chapovalov5e57b172016-09-02 19:15:59 +0200111
Erik Språng1d46f9c2019-07-02 21:24:47 +0200112 template <typename Extension>
113 bool IsExtensionReserved() const;
114 bool IsExtensionReserved(ExtensionType type) const;
115
Danil Chapovalovc5dd3002018-11-08 15:12:45 +0100116 template <typename Extension, typename FirstValue, typename... Values>
117 bool GetExtension(FirstValue, Values...) const;
118
119 template <typename Extension>
120 absl::optional<typename Extension::value_type> GetExtension() const;
danilchap1edb7ab2016-04-20 05:25:10 -0700121
Danil Chapovalove19953b2018-10-01 16:12:28 +0200122 // Returns view of the raw extension or empty view on failure.
123 template <typename Extension>
124 rtc::ArrayView<const uint8_t> GetRawExtension() const;
125
danilchap1edb7ab2016-04-20 05:25:10 -0700126 template <typename Extension, typename... Values>
127 bool SetExtension(Values...);
128
129 template <typename Extension>
130 bool ReserveExtension();
131
Amit Hilbuch77938e62018-12-21 09:23:38 -0800132 // Find or allocate an extension |type|. Returns view of size |length|
133 // to write raw extension to or an empty view on failure.
134 rtc::ArrayView<uint8_t> AllocateExtension(ExtensionType type, size_t length);
135
136 // Find an extension |type|.
137 // Returns view of the raw extension or empty view on failure.
138 rtc::ArrayView<const uint8_t> FindExtension(ExtensionType type) const;
139
danilchap1edb7ab2016-04-20 05:25:10 -0700140 // Reserve size_bytes for payload. Returns nullptr on failure.
danilchap07a01b32017-03-29 07:33:13 -0700141 uint8_t* SetPayloadSize(size_t size_bytes);
142 // Same as SetPayloadSize but doesn't guarantee to keep current payload.
danilchap1edb7ab2016-04-20 05:25:10 -0700143 uint8_t* AllocatePayload(size_t size_bytes);
Danil Chapovalovf7fcaf02018-10-10 14:56:01 +0200144
145 bool SetPadding(size_t padding_size);
danilchap1edb7ab2016-04-20 05:25:10 -0700146
Anton Sukhanovff25b872019-07-09 13:04:07 -0700147 // Returns debug string of RTP packet (without detailed extension info).
148 std::string ToString() const;
149
danilchap1edb7ab2016-04-20 05:25:10 -0700150 private:
151 struct ExtensionInfo {
Johannes Kronc5744b82018-09-24 14:50:48 +0200152 explicit ExtensionInfo(uint8_t id) : ExtensionInfo(id, 0, 0) {}
153 ExtensionInfo(uint8_t id, uint8_t length, uint16_t offset)
154 : id(id), length(length), offset(offset) {}
155 uint8_t id;
danilchap1edb7ab2016-04-20 05:25:10 -0700156 uint8_t length;
Johannes Kronc5744b82018-09-24 14:50:48 +0200157 uint16_t offset;
danilchap1edb7ab2016-04-20 05:25:10 -0700158 };
159
160 // Helper function for Parse. Fill header fields using data in given buffer,
161 // but does not touch packet own buffer, leaving packet in invalid state.
162 bool ParseBuffer(const uint8_t* buffer, size_t size);
163
Johannes Kronc5744b82018-09-24 14:50:48 +0200164 // Returns pointer to extension info for a given id. Returns nullptr if not
165 // found.
166 const ExtensionInfo* FindExtensionInfo(int id) const;
167
168 // Returns reference to extension info for a given id. Creates a new entry
169 // with the specified id if not found.
170 ExtensionInfo& FindOrCreateExtensionInfo(int id);
171
Danil Chapovalov527ff1e2018-08-06 19:34:32 +0200172 // Allocates and returns place to store rtp header extension.
173 // Returns empty arrayview on failure.
174 rtc::ArrayView<uint8_t> AllocateRawExtension(int id, size_t length);
175
Johannes Kron78cdde32018-10-05 10:00:46 +0200176 // Promotes existing one-byte header extensions to two-byte header extensions
177 // by rewriting the data and updates the corresponding extension offsets.
178 void PromoteToTwoByteHeaderExtension();
179
180 uint16_t SetExtensionLengthMaybeAddZeroPadding(size_t extensions_offset);
181
Danil Chapovalov7d2df3f2018-08-14 17:18:07 +0200182 uint8_t* WriteAt(size_t offset) { return buffer_.data() + offset; }
183 void WriteAt(size_t offset, uint8_t byte) { buffer_.data()[offset] = byte; }
Anton Sukhanovff25b872019-07-09 13:04:07 -0700184 const uint8_t* ReadAt(size_t offset) const { return buffer_.data() + offset; }
danilchap1edb7ab2016-04-20 05:25:10 -0700185
danilchap1edb7ab2016-04-20 05:25:10 -0700186 // Header.
187 bool marker_;
188 uint8_t payload_type_;
189 uint8_t padding_size_;
190 uint16_t sequence_number_;
191 uint32_t timestamp_;
192 uint32_t ssrc_;
193 size_t payload_offset_; // Match header size with csrcs and extensions.
194 size_t payload_size_;
195
Johannes Kronc5744b82018-09-24 14:50:48 +0200196 ExtensionManager extensions_;
197 std::vector<ExtensionInfo> extension_entries_;
Danil Chapovalov61405bc2018-02-13 13:55:30 +0100198 size_t extensions_size_ = 0; // Unaligned.
Danil Chapovalov31e4e802016-08-03 18:27:40 +0200199 rtc::CopyOnWriteBuffer buffer_;
danilchap1edb7ab2016-04-20 05:25:10 -0700200};
201
Danil Chapovalov5e57b172016-09-02 19:15:59 +0200202template <typename Extension>
Danil Chapovalov8769e172017-09-14 14:08:22 +0200203bool RtpPacket::HasExtension() const {
Amit Hilbuch77938e62018-12-21 09:23:38 -0800204 return HasExtension(Extension::kId);
Danil Chapovalov5e57b172016-09-02 19:15:59 +0200205}
206
Erik Språng1d46f9c2019-07-02 21:24:47 +0200207template <typename Extension>
208bool RtpPacket::IsExtensionReserved() const {
209 return IsExtensionReserved(Extension::kId);
210}
211
Danil Chapovalovc5dd3002018-11-08 15:12:45 +0100212template <typename Extension, typename FirstValue, typename... Values>
213bool RtpPacket::GetExtension(FirstValue first, Values... values) const {
danilchap978504e2017-04-06 01:03:53 -0700214 auto raw = FindExtension(Extension::kId);
215 if (raw.empty())
danilchap1edb7ab2016-04-20 05:25:10 -0700216 return false;
Danil Chapovalovc5dd3002018-11-08 15:12:45 +0100217 return Extension::Parse(raw, first, values...);
218}
219
220template <typename Extension>
221absl::optional<typename Extension::value_type> RtpPacket::GetExtension() const {
222 absl::optional<typename Extension::value_type> result;
223 auto raw = FindExtension(Extension::kId);
224 if (raw.empty() || !Extension::Parse(raw, &result.emplace()))
225 result = absl::nullopt;
226 return result;
danilchap1edb7ab2016-04-20 05:25:10 -0700227}
228
Danil Chapovalove19953b2018-10-01 16:12:28 +0200229template <typename Extension>
230rtc::ArrayView<const uint8_t> RtpPacket::GetRawExtension() const {
231 return FindExtension(Extension::kId);
232}
233
danilchap1edb7ab2016-04-20 05:25:10 -0700234template <typename Extension, typename... Values>
Danil Chapovalov8769e172017-09-14 14:08:22 +0200235bool RtpPacket::SetExtension(Values... values) {
erikvargae6b16192017-05-11 02:36:32 -0700236 const size_t value_size = Extension::ValueSize(values...);
erikvargae6b16192017-05-11 02:36:32 -0700237 auto buffer = AllocateExtension(Extension::kId, value_size);
danilchap653063f2017-04-03 06:16:30 -0700238 if (buffer.empty())
danilchap1edb7ab2016-04-20 05:25:10 -0700239 return false;
Danil Chapovalov9bf31582018-06-18 13:48:20 +0200240 return Extension::Write(buffer, values...);
danilchap1edb7ab2016-04-20 05:25:10 -0700241}
242
243template <typename Extension>
Danil Chapovalov8769e172017-09-14 14:08:22 +0200244bool RtpPacket::ReserveExtension() {
danilchap653063f2017-04-03 06:16:30 -0700245 auto buffer = AllocateExtension(Extension::kId, Extension::kValueSizeBytes);
246 if (buffer.empty())
danilchap1edb7ab2016-04-20 05:25:10 -0700247 return false;
danilchap653063f2017-04-03 06:16:30 -0700248 memset(buffer.data(), 0, Extension::kValueSizeBytes);
danilchap1edb7ab2016-04-20 05:25:10 -0700249 return true;
250}
Danil Chapovalov8769e172017-09-14 14:08:22 +0200251
danilchap1edb7ab2016-04-20 05:25:10 -0700252} // namespace webrtc
253
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200254#endif // MODULES_RTP_RTCP_SOURCE_RTP_PACKET_H_