blob: 4d6f2f78abdded6417b769994bfb0a6504df1759 [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
kwiberg529662a2017-09-04 05:43:17 -070015#include "webrtc/api/array_view.h"
danilchap1edb7ab2016-04-20 05:25:10 -070016#include "webrtc/modules/rtp_rtcp/include/rtp_rtcp_defines.h"
Edward Lemurc20978e2017-07-06 19:44:34 +020017#include "webrtc/rtc_base/basictypes.h"
18#include "webrtc/rtc_base/copyonwritebuffer.h"
danilchap1edb7ab2016-04-20 05:25:10 -070019
20namespace webrtc {
danilchap1edb7ab2016-04-20 05:25:10 -070021class RtpHeaderExtensionMap;
22class Random;
23
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;
danilchap772bd8b2017-09-13 03:24:28 -070028 static constexpr int kMaxExtensionHeaders = 14;
danilchap653063f2017-04-03 06:16:30 -070029 static constexpr int kMinExtensionId = 1;
30 static constexpr int kMaxExtensionId = 14;
danilchap1edb7ab2016-04-20 05:25:10 -070031
eladalon87443ee2017-09-13 02:45:15 -070032 // |extensions| required for SetExtension/ReserveExtension functions during
33 // packet creating and used if available in Parse function.
34 // Adding and getting extensions will fail until |extensions| is
35 // provided via constructor or IdentifyExtensions function.
Danil Chapovalov8769e172017-09-14 14:08:22 +020036 RtpPacket();
37 explicit RtpPacket(const ExtensionManager* extensions);
38 RtpPacket(const RtpPacket&);
39 RtpPacket(const ExtensionManager* extensions, size_t capacity);
40 ~RtpPacket();
eladalon87443ee2017-09-13 02:45:15 -070041
Danil Chapovalov8769e172017-09-14 14:08:22 +020042 RtpPacket& operator=(const RtpPacket&) = default;
eladalon87443ee2017-09-13 02:45:15 -070043
danilchap1edb7ab2016-04-20 05:25:10 -070044 // Parse and copy given buffer into Packet.
45 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.
55 bool Marker() const;
56 uint8_t PayloadType() const;
57 uint16_t SequenceNumber() const;
58 uint32_t Timestamp() const;
59 uint32_t Ssrc() const;
60 std::vector<uint32_t> Csrcs() const;
61
danilchap1edb7ab2016-04-20 05:25:10 -070062 size_t headers_size() const;
63
64 // Payload.
65 size_t payload_size() const;
66 size_t padding_size() const;
danilchap96c15872016-11-21 01:35:29 -080067 rtc::ArrayView<const uint8_t> payload() const;
danilchap1edb7ab2016-04-20 05:25:10 -070068
69 // Buffer.
Danil Chapovalov31e4e802016-08-03 18:27:40 +020070 rtc::CopyOnWriteBuffer Buffer() const;
danilchap1edb7ab2016-04-20 05:25:10 -070071 size_t capacity() const;
72 size_t size() const;
73 const uint8_t* data() const;
74 size_t FreeCapacity() const;
75 size_t MaxPayloadSize() const;
76
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.
91 void SetCsrcs(const std::vector<uint32_t>& csrcs);
92
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
100 template <typename Extension, typename... Values>
101 bool SetExtension(Values...);
102
103 template <typename Extension>
104 bool ReserveExtension();
105
danilchap653063f2017-04-03 06:16:30 -0700106 // Following 4 helpers identify rtp header extension by |id| negotiated with
107 // remote peer and written in an rtp packet.
108 bool HasRawExtension(int id) const;
109
110 // Returns place where extension with |id| is stored.
111 // Returns empty arrayview if extension is not present.
112 rtc::ArrayView<const uint8_t> GetRawExtension(int id) const;
113
114 // Allocates and store header extension. Returns true on success.
115 bool SetRawExtension(int id, rtc::ArrayView<const uint8_t> data);
116
117 // Allocates and returns place to store rtp header extension.
118 // Returns empty arrayview on failure.
119 rtc::ArrayView<uint8_t> AllocateRawExtension(int id, size_t length);
120
danilchap1edb7ab2016-04-20 05:25:10 -0700121 // Reserve size_bytes for payload. Returns nullptr on failure.
danilchap07a01b32017-03-29 07:33:13 -0700122 uint8_t* SetPayloadSize(size_t size_bytes);
123 // Same as SetPayloadSize but doesn't guarantee to keep current payload.
danilchap1edb7ab2016-04-20 05:25:10 -0700124 uint8_t* AllocatePayload(size_t size_bytes);
danilchap1edb7ab2016-04-20 05:25:10 -0700125 bool SetPadding(uint8_t size_bytes, Random* random);
126
danilchap1edb7ab2016-04-20 05:25:10 -0700127 private:
128 struct ExtensionInfo {
129 ExtensionType type;
130 uint16_t offset;
131 uint8_t length;
132 };
133
134 // Helper function for Parse. Fill header fields using data in given buffer,
135 // but does not touch packet own buffer, leaving packet in invalid state.
136 bool ParseBuffer(const uint8_t* buffer, size_t size);
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
danilchap653063f2017-04-03 06:16:30 -0700142 // Find or allocate an extension |type|. Returns view of size |length|
143 // to write raw extension to or an empty view on failure.
144 rtc::ArrayView<uint8_t> AllocateExtension(ExtensionType type, size_t length);
danilchap1edb7ab2016-04-20 05:25:10 -0700145
146 uint8_t* WriteAt(size_t offset);
147 void WriteAt(size_t offset, uint8_t byte);
148
danilchap1edb7ab2016-04-20 05:25:10 -0700149 // Header.
150 bool marker_;
151 uint8_t payload_type_;
152 uint8_t padding_size_;
153 uint16_t sequence_number_;
154 uint32_t timestamp_;
155 uint32_t ssrc_;
156 size_t payload_offset_; // Match header size with csrcs and extensions.
157 size_t payload_size_;
158
danilchap1edb7ab2016-04-20 05:25:10 -0700159 ExtensionInfo extension_entries_[kMaxExtensionHeaders];
160 uint16_t extensions_size_ = 0; // Unaligned.
Danil Chapovalov31e4e802016-08-03 18:27:40 +0200161 rtc::CopyOnWriteBuffer buffer_;
danilchap1edb7ab2016-04-20 05:25:10 -0700162};
163
Danil Chapovalov5e57b172016-09-02 19:15:59 +0200164template <typename Extension>
Danil Chapovalov8769e172017-09-14 14:08:22 +0200165bool RtpPacket::HasExtension() const {
danilchap978504e2017-04-06 01:03:53 -0700166 return !FindExtension(Extension::kId).empty();
Danil Chapovalov5e57b172016-09-02 19:15:59 +0200167}
168
danilchap1edb7ab2016-04-20 05:25:10 -0700169template <typename Extension, typename... Values>
Danil Chapovalov8769e172017-09-14 14:08:22 +0200170bool RtpPacket::GetExtension(Values... values) const {
danilchap978504e2017-04-06 01:03:53 -0700171 auto raw = FindExtension(Extension::kId);
172 if (raw.empty())
danilchap1edb7ab2016-04-20 05:25:10 -0700173 return false;
danilchap978504e2017-04-06 01:03:53 -0700174 return Extension::Parse(raw, values...);
danilchap1edb7ab2016-04-20 05:25:10 -0700175}
176
177template <typename Extension, typename... Values>
Danil Chapovalov8769e172017-09-14 14:08:22 +0200178bool RtpPacket::SetExtension(Values... values) {
erikvargae6b16192017-05-11 02:36:32 -0700179 const size_t value_size = Extension::ValueSize(values...);
180 if (value_size == 0 || value_size > 16)
181 return false;
182 auto buffer = AllocateExtension(Extension::kId, value_size);
danilchap653063f2017-04-03 06:16:30 -0700183 if (buffer.empty())
danilchap1edb7ab2016-04-20 05:25:10 -0700184 return false;
danilchap653063f2017-04-03 06:16:30 -0700185 return Extension::Write(buffer.data(), values...);
danilchap1edb7ab2016-04-20 05:25:10 -0700186}
187
188template <typename Extension>
Danil Chapovalov8769e172017-09-14 14:08:22 +0200189bool RtpPacket::ReserveExtension() {
danilchap653063f2017-04-03 06:16:30 -0700190 auto buffer = AllocateExtension(Extension::kId, Extension::kValueSizeBytes);
191 if (buffer.empty())
danilchap1edb7ab2016-04-20 05:25:10 -0700192 return false;
danilchap653063f2017-04-03 06:16:30 -0700193 memset(buffer.data(), 0, Extension::kValueSizeBytes);
danilchap1edb7ab2016-04-20 05:25:10 -0700194 return true;
195}
Danil Chapovalov8769e172017-09-14 14:08:22 +0200196
danilchap1edb7ab2016-04-20 05:25:10 -0700197} // namespace webrtc
198
199#endif // WEBRTC_MODULES_RTP_RTCP_SOURCE_RTP_PACKET_H_