blob: 4a3b7e954355c07449b5be7b36f5f9b2526c495b [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;
danilchap772bd8b2017-09-13 03:24:28 -070027 static constexpr int kMaxExtensionHeaders = 14;
danilchap653063f2017-04-03 06:16:30 -070028 static constexpr int kMinExtensionId = 1;
29 static constexpr int kMaxExtensionId = 14;
danilchap1edb7ab2016-04-20 05:25:10 -070030
eladalon87443ee2017-09-13 02:45:15 -070031 // |extensions| required for SetExtension/ReserveExtension functions during
32 // packet creating and used if available in Parse function.
33 // Adding and getting extensions will fail until |extensions| is
34 // provided via constructor or IdentifyExtensions function.
Danil Chapovalov8769e172017-09-14 14:08:22 +020035 RtpPacket();
36 explicit RtpPacket(const ExtensionManager* extensions);
37 RtpPacket(const RtpPacket&);
38 RtpPacket(const ExtensionManager* extensions, size_t capacity);
39 ~RtpPacket();
eladalon87443ee2017-09-13 02:45:15 -070040
Danil Chapovalov8769e172017-09-14 14:08:22 +020041 RtpPacket& operator=(const RtpPacket&) = default;
eladalon87443ee2017-09-13 02:45:15 -070042
danilchap1edb7ab2016-04-20 05:25:10 -070043 // Parse and copy given buffer into Packet.
44 bool Parse(const uint8_t* buffer, size_t size);
brandtrb29e6522016-12-21 06:37:18 -080045 bool Parse(rtc::ArrayView<const uint8_t> packet);
danilchap1edb7ab2016-04-20 05:25:10 -070046
47 // Parse and move given buffer into Packet.
Danil Chapovalov31e4e802016-08-03 18:27:40 +020048 bool Parse(rtc::CopyOnWriteBuffer packet);
danilchap1edb7ab2016-04-20 05:25:10 -070049
danilchap70f39a32016-12-16 05:48:18 -080050 // Maps extensions id to their types.
51 void IdentifyExtensions(const ExtensionManager& extensions);
danilchap1edb7ab2016-04-20 05:25:10 -070052
53 // Header.
Danil Chapovalov7d2df3f2018-08-14 17:18:07 +020054 bool Marker() const { return marker_; }
55 uint8_t PayloadType() const { return payload_type_; }
56 uint16_t SequenceNumber() const { return sequence_number_; }
57 uint32_t Timestamp() const { return timestamp_; }
58 uint32_t Ssrc() const { return ssrc_; }
danilchap1edb7ab2016-04-20 05:25:10 -070059 std::vector<uint32_t> Csrcs() const;
60
Danil Chapovalov7d2df3f2018-08-14 17:18:07 +020061 size_t headers_size() const { return payload_offset_; }
danilchap1edb7ab2016-04-20 05:25:10 -070062
63 // Payload.
Danil Chapovalov7d2df3f2018-08-14 17:18:07 +020064 size_t payload_size() const { return payload_size_; }
65 size_t padding_size() const { return padding_size_; }
66 rtc::ArrayView<const uint8_t> payload() const {
67 return rtc::MakeArrayView(data() + payload_offset_, payload_size_);
68 }
danilchap1edb7ab2016-04-20 05:25:10 -070069
70 // Buffer.
Danil Chapovalov7d2df3f2018-08-14 17:18:07 +020071 rtc::CopyOnWriteBuffer Buffer() const { return buffer_; }
72 size_t capacity() const { return buffer_.capacity(); }
73 size_t size() const {
74 return payload_offset_ + payload_size_ + padding_size_;
75 }
76 const uint8_t* data() const { return buffer_.cdata(); }
77 size_t FreeCapacity() const { return capacity() - size(); }
78 size_t MaxPayloadSize() const { return capacity() - headers_size(); }
danilchap1edb7ab2016-04-20 05:25:10 -070079
80 // Reset fields and buffer.
81 void Clear();
82
83 // Header setters.
Danil Chapovalov8769e172017-09-14 14:08:22 +020084 void CopyHeaderFrom(const RtpPacket& packet);
danilchap1edb7ab2016-04-20 05:25:10 -070085 void SetMarker(bool marker_bit);
86 void SetPayloadType(uint8_t payload_type);
87 void SetSequenceNumber(uint16_t seq_no);
88 void SetTimestamp(uint32_t timestamp);
89 void SetSsrc(uint32_t ssrc);
90
91 // Writes csrc list. Assumes:
92 // a) There is enough room left in buffer.
93 // b) Extension headers, payload or padding data has not already been added.
Danil Chapovalov7d2df3f2018-08-14 17:18:07 +020094 void SetCsrcs(rtc::ArrayView<const uint32_t> csrcs);
danilchap1edb7ab2016-04-20 05:25:10 -070095
96 // Header extensions.
Danil Chapovalov5e57b172016-09-02 19:15:59 +020097 template <typename Extension>
98 bool HasExtension() const;
99
danilchap1edb7ab2016-04-20 05:25:10 -0700100 template <typename Extension, typename... Values>
101 bool GetExtension(Values...) const;
102
103 template <typename Extension, typename... Values>
104 bool SetExtension(Values...);
105
106 template <typename Extension>
107 bool ReserveExtension();
108
109 // Reserve size_bytes for payload. Returns nullptr on failure.
danilchap07a01b32017-03-29 07:33:13 -0700110 uint8_t* SetPayloadSize(size_t size_bytes);
111 // Same as SetPayloadSize but doesn't guarantee to keep current payload.
danilchap1edb7ab2016-04-20 05:25:10 -0700112 uint8_t* AllocatePayload(size_t size_bytes);
danilchap1edb7ab2016-04-20 05:25:10 -0700113 bool SetPadding(uint8_t size_bytes, Random* random);
114
danilchap1edb7ab2016-04-20 05:25:10 -0700115 private:
116 struct ExtensionInfo {
Johannes Kronc5744b82018-09-24 14:50:48 +0200117 explicit ExtensionInfo(uint8_t id) : ExtensionInfo(id, 0, 0) {}
118 ExtensionInfo(uint8_t id, uint8_t length, uint16_t offset)
119 : id(id), length(length), offset(offset) {}
120 uint8_t id;
danilchap1edb7ab2016-04-20 05:25:10 -0700121 uint8_t length;
Johannes Kronc5744b82018-09-24 14:50:48 +0200122 uint16_t offset;
danilchap1edb7ab2016-04-20 05:25:10 -0700123 };
124
125 // Helper function for Parse. Fill header fields using data in given buffer,
126 // but does not touch packet own buffer, leaving packet in invalid state.
127 bool ParseBuffer(const uint8_t* buffer, size_t size);
128
Johannes Kronc5744b82018-09-24 14:50:48 +0200129 // Returns pointer to extension info for a given id. Returns nullptr if not
130 // found.
131 const ExtensionInfo* FindExtensionInfo(int id) const;
132
133 // Returns reference to extension info for a given id. Creates a new entry
134 // with the specified id if not found.
135 ExtensionInfo& FindOrCreateExtensionInfo(int id);
136
danilchap978504e2017-04-06 01:03:53 -0700137 // Find an extension |type|.
138 // Returns view of the raw extension or empty view on failure.
139 rtc::ArrayView<const uint8_t> FindExtension(ExtensionType type) const;
danilchap1edb7ab2016-04-20 05:25:10 -0700140
Danil Chapovalov527ff1e2018-08-06 19:34:32 +0200141 // Allocates and returns place to store rtp header extension.
142 // Returns empty arrayview on failure.
143 rtc::ArrayView<uint8_t> AllocateRawExtension(int id, size_t length);
144
danilchap653063f2017-04-03 06:16:30 -0700145 // Find or allocate an extension |type|. Returns view of size |length|
146 // to write raw extension to or an empty view on failure.
147 rtc::ArrayView<uint8_t> AllocateExtension(ExtensionType type, size_t length);
danilchap1edb7ab2016-04-20 05:25:10 -0700148
Danil Chapovalov7d2df3f2018-08-14 17:18:07 +0200149 uint8_t* WriteAt(size_t offset) { return buffer_.data() + offset; }
150 void WriteAt(size_t offset, uint8_t byte) { buffer_.data()[offset] = byte; }
danilchap1edb7ab2016-04-20 05:25:10 -0700151
danilchap1edb7ab2016-04-20 05:25:10 -0700152 // Header.
153 bool marker_;
154 uint8_t payload_type_;
155 uint8_t padding_size_;
156 uint16_t sequence_number_;
157 uint32_t timestamp_;
158 uint32_t ssrc_;
159 size_t payload_offset_; // Match header size with csrcs and extensions.
160 size_t payload_size_;
161
Johannes Kronc5744b82018-09-24 14:50:48 +0200162 ExtensionManager extensions_;
163 std::vector<ExtensionInfo> extension_entries_;
Danil Chapovalov61405bc2018-02-13 13:55:30 +0100164 size_t extensions_size_ = 0; // Unaligned.
Danil Chapovalov31e4e802016-08-03 18:27:40 +0200165 rtc::CopyOnWriteBuffer buffer_;
danilchap1edb7ab2016-04-20 05:25:10 -0700166};
167
Danil Chapovalov5e57b172016-09-02 19:15:59 +0200168template <typename Extension>
Danil Chapovalov8769e172017-09-14 14:08:22 +0200169bool RtpPacket::HasExtension() const {
danilchap978504e2017-04-06 01:03:53 -0700170 return !FindExtension(Extension::kId).empty();
Danil Chapovalov5e57b172016-09-02 19:15:59 +0200171}
172
danilchap1edb7ab2016-04-20 05:25:10 -0700173template <typename Extension, typename... Values>
Danil Chapovalov8769e172017-09-14 14:08:22 +0200174bool RtpPacket::GetExtension(Values... values) const {
danilchap978504e2017-04-06 01:03:53 -0700175 auto raw = FindExtension(Extension::kId);
176 if (raw.empty())
danilchap1edb7ab2016-04-20 05:25:10 -0700177 return false;
danilchap978504e2017-04-06 01:03:53 -0700178 return Extension::Parse(raw, values...);
danilchap1edb7ab2016-04-20 05:25:10 -0700179}
180
181template <typename Extension, typename... Values>
Danil Chapovalov8769e172017-09-14 14:08:22 +0200182bool RtpPacket::SetExtension(Values... values) {
erikvargae6b16192017-05-11 02:36:32 -0700183 const size_t value_size = Extension::ValueSize(values...);
184 if (value_size == 0 || value_size > 16)
185 return false;
186 auto buffer = AllocateExtension(Extension::kId, value_size);
danilchap653063f2017-04-03 06:16:30 -0700187 if (buffer.empty())
danilchap1edb7ab2016-04-20 05:25:10 -0700188 return false;
Danil Chapovalov9bf31582018-06-18 13:48:20 +0200189 return Extension::Write(buffer, values...);
danilchap1edb7ab2016-04-20 05:25:10 -0700190}
191
192template <typename Extension>
Danil Chapovalov8769e172017-09-14 14:08:22 +0200193bool RtpPacket::ReserveExtension() {
danilchap653063f2017-04-03 06:16:30 -0700194 auto buffer = AllocateExtension(Extension::kId, Extension::kValueSizeBytes);
195 if (buffer.empty())
danilchap1edb7ab2016-04-20 05:25:10 -0700196 return false;
danilchap653063f2017-04-03 06:16:30 -0700197 memset(buffer.data(), 0, Extension::kValueSizeBytes);
danilchap1edb7ab2016-04-20 05:25:10 -0700198 return true;
199}
Danil Chapovalov8769e172017-09-14 14:08:22 +0200200
danilchap1edb7ab2016-04-20 05:25:10 -0700201} // namespace webrtc
202
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200203#endif // MODULES_RTP_RTCP_SOURCE_RTP_PACKET_H_