blob: 15c3865b3594dc876e41806a4783f1746253dd74 [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
13#include <vector>
14
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020015#include "api/array_view.h"
Johannes Kronc5744b82018-09-24 14:50:48 +020016#include "modules/rtp_rtcp/include/rtp_header_extension_map.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#include "modules/rtp_rtcp/include/rtp_rtcp_defines.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "rtc_base/copyonwritebuffer.h"
danilchap1edb7ab2016-04-20 05:25:10 -070019
20namespace webrtc {
danilchap1edb7ab2016-04-20 05:25:10 -070021class Random;
22
Danil Chapovalov8769e172017-09-14 14:08:22 +020023class RtpPacket {
danilchap1edb7ab2016-04-20 05:25:10 -070024 public:
25 using ExtensionType = RTPExtensionType;
26 using ExtensionManager = RtpHeaderExtensionMap;
danilchap1edb7ab2016-04-20 05:25:10 -070027
eladalon87443ee2017-09-13 02:45:15 -070028 // |extensions| required for SetExtension/ReserveExtension functions during
29 // packet creating and used if available in Parse function.
30 // Adding and getting extensions will fail until |extensions| is
31 // provided via constructor or IdentifyExtensions function.
Danil Chapovalov8769e172017-09-14 14:08:22 +020032 RtpPacket();
33 explicit RtpPacket(const ExtensionManager* extensions);
34 RtpPacket(const RtpPacket&);
35 RtpPacket(const ExtensionManager* extensions, size_t capacity);
36 ~RtpPacket();
eladalon87443ee2017-09-13 02:45:15 -070037
Danil Chapovalov8769e172017-09-14 14:08:22 +020038 RtpPacket& operator=(const RtpPacket&) = default;
eladalon87443ee2017-09-13 02:45:15 -070039
danilchap1edb7ab2016-04-20 05:25:10 -070040 // Parse and copy given buffer into Packet.
41 bool Parse(const uint8_t* buffer, size_t size);
brandtrb29e6522016-12-21 06:37:18 -080042 bool Parse(rtc::ArrayView<const uint8_t> packet);
danilchap1edb7ab2016-04-20 05:25:10 -070043
44 // Parse and move given buffer into Packet.
Danil Chapovalov31e4e802016-08-03 18:27:40 +020045 bool Parse(rtc::CopyOnWriteBuffer packet);
danilchap1edb7ab2016-04-20 05:25:10 -070046
danilchap70f39a32016-12-16 05:48:18 -080047 // Maps extensions id to their types.
48 void IdentifyExtensions(const ExtensionManager& extensions);
danilchap1edb7ab2016-04-20 05:25:10 -070049
50 // Header.
Danil Chapovalov7d2df3f2018-08-14 17:18:07 +020051 bool Marker() const { return marker_; }
52 uint8_t PayloadType() const { return payload_type_; }
53 uint16_t SequenceNumber() const { return sequence_number_; }
54 uint32_t Timestamp() const { return timestamp_; }
55 uint32_t Ssrc() const { return ssrc_; }
danilchap1edb7ab2016-04-20 05:25:10 -070056 std::vector<uint32_t> Csrcs() const;
57
Danil Chapovalov7d2df3f2018-08-14 17:18:07 +020058 size_t headers_size() const { return payload_offset_; }
danilchap1edb7ab2016-04-20 05:25:10 -070059
60 // Payload.
Danil Chapovalov7d2df3f2018-08-14 17:18:07 +020061 size_t payload_size() const { return payload_size_; }
62 size_t padding_size() const { return padding_size_; }
63 rtc::ArrayView<const uint8_t> payload() const {
64 return rtc::MakeArrayView(data() + payload_offset_, payload_size_);
65 }
danilchap1edb7ab2016-04-20 05:25:10 -070066
67 // Buffer.
Danil Chapovalov7d2df3f2018-08-14 17:18:07 +020068 rtc::CopyOnWriteBuffer Buffer() const { return buffer_; }
69 size_t capacity() const { return buffer_.capacity(); }
70 size_t size() const {
71 return payload_offset_ + payload_size_ + padding_size_;
72 }
73 const uint8_t* data() const { return buffer_.cdata(); }
74 size_t FreeCapacity() const { return capacity() - size(); }
75 size_t MaxPayloadSize() const { return capacity() - headers_size(); }
danilchap1edb7ab2016-04-20 05:25:10 -070076
77 // Reset fields and buffer.
78 void Clear();
79
80 // Header setters.
Danil Chapovalov8769e172017-09-14 14:08:22 +020081 void CopyHeaderFrom(const RtpPacket& packet);
danilchap1edb7ab2016-04-20 05:25:10 -070082 void SetMarker(bool marker_bit);
83 void SetPayloadType(uint8_t payload_type);
84 void SetSequenceNumber(uint16_t seq_no);
85 void SetTimestamp(uint32_t timestamp);
86 void SetSsrc(uint32_t ssrc);
87
88 // Writes csrc list. Assumes:
89 // a) There is enough room left in buffer.
90 // b) Extension headers, payload or padding data has not already been added.
Danil Chapovalov7d2df3f2018-08-14 17:18:07 +020091 void SetCsrcs(rtc::ArrayView<const uint32_t> csrcs);
danilchap1edb7ab2016-04-20 05:25:10 -070092
93 // Header extensions.
Danil Chapovalov5e57b172016-09-02 19:15:59 +020094 template <typename Extension>
95 bool HasExtension() const;
96
danilchap1edb7ab2016-04-20 05:25:10 -070097 template <typename Extension, typename... Values>
98 bool GetExtension(Values...) const;
99
Danil Chapovalove19953b2018-10-01 16:12:28 +0200100 // Returns view of the raw extension or empty view on failure.
101 template <typename Extension>
102 rtc::ArrayView<const uint8_t> GetRawExtension() const;
103
danilchap1edb7ab2016-04-20 05:25:10 -0700104 template <typename Extension, typename... Values>
105 bool SetExtension(Values...);
106
107 template <typename Extension>
108 bool ReserveExtension();
109
110 // Reserve size_bytes for payload. Returns nullptr on failure.
danilchap07a01b32017-03-29 07:33:13 -0700111 uint8_t* SetPayloadSize(size_t size_bytes);
112 // Same as SetPayloadSize but doesn't guarantee to keep current payload.
danilchap1edb7ab2016-04-20 05:25:10 -0700113 uint8_t* AllocatePayload(size_t size_bytes);
danilchap1edb7ab2016-04-20 05:25:10 -0700114 bool SetPadding(uint8_t size_bytes, Random* random);
115
danilchap1edb7ab2016-04-20 05:25:10 -0700116 private:
117 struct ExtensionInfo {
Johannes Kronc5744b82018-09-24 14:50:48 +0200118 explicit ExtensionInfo(uint8_t id) : ExtensionInfo(id, 0, 0) {}
119 ExtensionInfo(uint8_t id, uint8_t length, uint16_t offset)
120 : id(id), length(length), offset(offset) {}
121 uint8_t id;
danilchap1edb7ab2016-04-20 05:25:10 -0700122 uint8_t length;
Johannes Kronc5744b82018-09-24 14:50:48 +0200123 uint16_t offset;
danilchap1edb7ab2016-04-20 05:25:10 -0700124 };
125
126 // Helper function for Parse. Fill header fields using data in given buffer,
127 // but does not touch packet own buffer, leaving packet in invalid state.
128 bool ParseBuffer(const uint8_t* buffer, size_t size);
129
Johannes Kronc5744b82018-09-24 14:50:48 +0200130 // Returns pointer to extension info for a given id. Returns nullptr if not
131 // found.
132 const ExtensionInfo* FindExtensionInfo(int id) const;
133
134 // Returns reference to extension info for a given id. Creates a new entry
135 // with the specified id if not found.
136 ExtensionInfo& FindOrCreateExtensionInfo(int id);
137
danilchap978504e2017-04-06 01:03:53 -0700138 // Find an extension |type|.
139 // Returns view of the raw extension or empty view on failure.
140 rtc::ArrayView<const uint8_t> FindExtension(ExtensionType type) const;
danilchap1edb7ab2016-04-20 05:25:10 -0700141
Danil Chapovalov527ff1e2018-08-06 19:34:32 +0200142 // Allocates and returns place to store rtp header extension.
143 // Returns empty arrayview on failure.
144 rtc::ArrayView<uint8_t> AllocateRawExtension(int id, size_t length);
145
Johannes Kron78cdde32018-10-05 10:00:46 +0200146 // Promotes existing one-byte header extensions to two-byte header extensions
147 // by rewriting the data and updates the corresponding extension offsets.
148 void PromoteToTwoByteHeaderExtension();
149
150 uint16_t SetExtensionLengthMaybeAddZeroPadding(size_t extensions_offset);
151
danilchap653063f2017-04-03 06:16:30 -0700152 // Find or allocate an extension |type|. Returns view of size |length|
153 // to write raw extension to or an empty view on failure.
154 rtc::ArrayView<uint8_t> AllocateExtension(ExtensionType type, size_t length);
danilchap1edb7ab2016-04-20 05:25:10 -0700155
Danil Chapovalov7d2df3f2018-08-14 17:18:07 +0200156 uint8_t* WriteAt(size_t offset) { return buffer_.data() + offset; }
157 void WriteAt(size_t offset, uint8_t byte) { buffer_.data()[offset] = byte; }
danilchap1edb7ab2016-04-20 05:25:10 -0700158
danilchap1edb7ab2016-04-20 05:25:10 -0700159 // Header.
160 bool marker_;
161 uint8_t payload_type_;
162 uint8_t padding_size_;
163 uint16_t sequence_number_;
164 uint32_t timestamp_;
165 uint32_t ssrc_;
166 size_t payload_offset_; // Match header size with csrcs and extensions.
167 size_t payload_size_;
168
Johannes Kronc5744b82018-09-24 14:50:48 +0200169 ExtensionManager extensions_;
170 std::vector<ExtensionInfo> extension_entries_;
Danil Chapovalov61405bc2018-02-13 13:55:30 +0100171 size_t extensions_size_ = 0; // Unaligned.
Danil Chapovalov31e4e802016-08-03 18:27:40 +0200172 rtc::CopyOnWriteBuffer buffer_;
danilchap1edb7ab2016-04-20 05:25:10 -0700173};
174
Danil Chapovalov5e57b172016-09-02 19:15:59 +0200175template <typename Extension>
Danil Chapovalov8769e172017-09-14 14:08:22 +0200176bool RtpPacket::HasExtension() const {
danilchap978504e2017-04-06 01:03:53 -0700177 return !FindExtension(Extension::kId).empty();
Danil Chapovalov5e57b172016-09-02 19:15:59 +0200178}
179
danilchap1edb7ab2016-04-20 05:25:10 -0700180template <typename Extension, typename... Values>
Danil Chapovalov8769e172017-09-14 14:08:22 +0200181bool RtpPacket::GetExtension(Values... values) const {
danilchap978504e2017-04-06 01:03:53 -0700182 auto raw = FindExtension(Extension::kId);
183 if (raw.empty())
danilchap1edb7ab2016-04-20 05:25:10 -0700184 return false;
danilchap978504e2017-04-06 01:03:53 -0700185 return Extension::Parse(raw, values...);
danilchap1edb7ab2016-04-20 05:25:10 -0700186}
187
Danil Chapovalove19953b2018-10-01 16:12:28 +0200188template <typename Extension>
189rtc::ArrayView<const uint8_t> RtpPacket::GetRawExtension() const {
190 return FindExtension(Extension::kId);
191}
192
danilchap1edb7ab2016-04-20 05:25:10 -0700193template <typename Extension, typename... Values>
Danil Chapovalov8769e172017-09-14 14:08:22 +0200194bool RtpPacket::SetExtension(Values... values) {
erikvargae6b16192017-05-11 02:36:32 -0700195 const size_t value_size = Extension::ValueSize(values...);
erikvargae6b16192017-05-11 02:36:32 -0700196 auto buffer = AllocateExtension(Extension::kId, value_size);
danilchap653063f2017-04-03 06:16:30 -0700197 if (buffer.empty())
danilchap1edb7ab2016-04-20 05:25:10 -0700198 return false;
Danil Chapovalov9bf31582018-06-18 13:48:20 +0200199 return Extension::Write(buffer, values...);
danilchap1edb7ab2016-04-20 05:25:10 -0700200}
201
202template <typename Extension>
Danil Chapovalov8769e172017-09-14 14:08:22 +0200203bool RtpPacket::ReserveExtension() {
danilchap653063f2017-04-03 06:16:30 -0700204 auto buffer = AllocateExtension(Extension::kId, Extension::kValueSizeBytes);
205 if (buffer.empty())
danilchap1edb7ab2016-04-20 05:25:10 -0700206 return false;
danilchap653063f2017-04-03 06:16:30 -0700207 memset(buffer.data(), 0, Extension::kValueSizeBytes);
danilchap1edb7ab2016-04-20 05:25:10 -0700208 return true;
209}
Danil Chapovalov8769e172017-09-14 14:08:22 +0200210
danilchap1edb7ab2016-04-20 05:25:10 -0700211} // namespace webrtc
212
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200213#endif // MODULES_RTP_RTCP_SOURCE_RTP_PACKET_H_