blob: 09a1887eb6d43989afc39d76b8a7d2d7964b0f3d [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
24namespace rtp {
25class Packet {
26 public:
27 using ExtensionType = RTPExtensionType;
28 using ExtensionManager = RtpHeaderExtensionMap;
danilchap772bd8b2017-09-13 03:24:28 -070029 static constexpr int kMaxExtensionHeaders = 14;
danilchap653063f2017-04-03 06:16:30 -070030 static constexpr int kMinExtensionId = 1;
31 static constexpr int kMaxExtensionId = 14;
danilchap1edb7ab2016-04-20 05:25:10 -070032
eladalon87443ee2017-09-13 02:45:15 -070033 // |extensions| required for SetExtension/ReserveExtension functions during
34 // packet creating and used if available in Parse function.
35 // Adding and getting extensions will fail until |extensions| is
36 // provided via constructor or IdentifyExtensions function.
37 Packet();
38 explicit Packet(const ExtensionManager* extensions);
39 Packet(const Packet&);
40 Packet(const ExtensionManager* extensions, size_t capacity);
41 ~Packet();
42
43 Packet& operator=(const Packet&) = default;
44
danilchap1edb7ab2016-04-20 05:25:10 -070045 // Parse and copy given buffer into Packet.
46 bool Parse(const uint8_t* buffer, size_t size);
brandtrb29e6522016-12-21 06:37:18 -080047 bool Parse(rtc::ArrayView<const uint8_t> packet);
danilchap1edb7ab2016-04-20 05:25:10 -070048
49 // Parse and move given buffer into Packet.
Danil Chapovalov31e4e802016-08-03 18:27:40 +020050 bool Parse(rtc::CopyOnWriteBuffer packet);
danilchap1edb7ab2016-04-20 05:25:10 -070051
danilchap70f39a32016-12-16 05:48:18 -080052 // Maps extensions id to their types.
53 void IdentifyExtensions(const ExtensionManager& extensions);
danilchap1edb7ab2016-04-20 05:25:10 -070054
55 // Header.
56 bool Marker() const;
57 uint8_t PayloadType() const;
58 uint16_t SequenceNumber() const;
59 uint32_t Timestamp() const;
60 uint32_t Ssrc() const;
61 std::vector<uint32_t> Csrcs() const;
62
danilchap1edb7ab2016-04-20 05:25:10 -070063 size_t headers_size() const;
64
65 // Payload.
66 size_t payload_size() const;
67 size_t padding_size() const;
danilchap96c15872016-11-21 01:35:29 -080068 rtc::ArrayView<const uint8_t> payload() const;
danilchap1edb7ab2016-04-20 05:25:10 -070069
70 // Buffer.
Danil Chapovalov31e4e802016-08-03 18:27:40 +020071 rtc::CopyOnWriteBuffer Buffer() const;
danilchap1edb7ab2016-04-20 05:25:10 -070072 size_t capacity() const;
73 size_t size() const;
74 const uint8_t* data() const;
75 size_t FreeCapacity() const;
76 size_t MaxPayloadSize() const;
77
78 // Reset fields and buffer.
79 void Clear();
80
81 // Header setters.
Danil Chapovalov31e4e802016-08-03 18:27:40 +020082 void CopyHeaderFrom(const Packet& packet);
danilchap1edb7ab2016-04-20 05:25:10 -070083 void SetMarker(bool marker_bit);
84 void SetPayloadType(uint8_t payload_type);
85 void SetSequenceNumber(uint16_t seq_no);
86 void SetTimestamp(uint32_t timestamp);
87 void SetSsrc(uint32_t ssrc);
88
89 // Writes csrc list. Assumes:
90 // a) There is enough room left in buffer.
91 // b) Extension headers, payload or padding data has not already been added.
92 void SetCsrcs(const std::vector<uint32_t>& csrcs);
93
94 // Header extensions.
Danil Chapovalov5e57b172016-09-02 19:15:59 +020095 template <typename Extension>
96 bool HasExtension() const;
97
danilchap1edb7ab2016-04-20 05:25:10 -070098 template <typename Extension, typename... Values>
99 bool GetExtension(Values...) const;
100
101 template <typename Extension, typename... Values>
102 bool SetExtension(Values...);
103
104 template <typename Extension>
105 bool ReserveExtension();
106
danilchap653063f2017-04-03 06:16:30 -0700107 // Following 4 helpers identify rtp header extension by |id| negotiated with
108 // remote peer and written in an rtp packet.
109 bool HasRawExtension(int id) const;
110
111 // Returns place where extension with |id| is stored.
112 // Returns empty arrayview if extension is not present.
113 rtc::ArrayView<const uint8_t> GetRawExtension(int id) const;
114
115 // Allocates and store header extension. Returns true on success.
116 bool SetRawExtension(int id, rtc::ArrayView<const uint8_t> data);
117
118 // Allocates and returns place to store rtp header extension.
119 // Returns empty arrayview on failure.
120 rtc::ArrayView<uint8_t> AllocateRawExtension(int id, size_t length);
121
danilchap1edb7ab2016-04-20 05:25:10 -0700122 // Reserve size_bytes for payload. Returns nullptr on failure.
danilchap07a01b32017-03-29 07:33:13 -0700123 uint8_t* SetPayloadSize(size_t size_bytes);
124 // Same as SetPayloadSize but doesn't guarantee to keep current payload.
danilchap1edb7ab2016-04-20 05:25:10 -0700125 uint8_t* AllocatePayload(size_t size_bytes);
danilchap1edb7ab2016-04-20 05:25:10 -0700126 bool SetPadding(uint8_t size_bytes, Random* random);
127
danilchap1edb7ab2016-04-20 05:25:10 -0700128 private:
129 struct ExtensionInfo {
130 ExtensionType type;
131 uint16_t offset;
132 uint8_t length;
133 };
134
135 // Helper function for Parse. Fill header fields using data in given buffer,
136 // but does not touch packet own buffer, leaving packet in invalid state.
137 bool ParseBuffer(const uint8_t* buffer, size_t size);
138
danilchap978504e2017-04-06 01:03:53 -0700139 // Find an extension |type|.
140 // Returns view of the raw extension or empty view on failure.
141 rtc::ArrayView<const uint8_t> FindExtension(ExtensionType type) const;
danilchap1edb7ab2016-04-20 05:25:10 -0700142
danilchap653063f2017-04-03 06:16:30 -0700143 // Find or allocate an extension |type|. Returns view of size |length|
144 // to write raw extension to or an empty view on failure.
145 rtc::ArrayView<uint8_t> AllocateExtension(ExtensionType type, size_t length);
danilchap1edb7ab2016-04-20 05:25:10 -0700146
147 uint8_t* WriteAt(size_t offset);
148 void WriteAt(size_t offset, uint8_t byte);
149
danilchap1edb7ab2016-04-20 05:25:10 -0700150 // Header.
151 bool marker_;
152 uint8_t payload_type_;
153 uint8_t padding_size_;
154 uint16_t sequence_number_;
155 uint32_t timestamp_;
156 uint32_t ssrc_;
157 size_t payload_offset_; // Match header size with csrcs and extensions.
158 size_t payload_size_;
159
danilchap1edb7ab2016-04-20 05:25:10 -0700160 ExtensionInfo extension_entries_[kMaxExtensionHeaders];
161 uint16_t extensions_size_ = 0; // Unaligned.
Danil Chapovalov31e4e802016-08-03 18:27:40 +0200162 rtc::CopyOnWriteBuffer buffer_;
danilchap1edb7ab2016-04-20 05:25:10 -0700163};
164
Danil Chapovalov5e57b172016-09-02 19:15:59 +0200165template <typename Extension>
166bool Packet::HasExtension() const {
danilchap978504e2017-04-06 01:03:53 -0700167 return !FindExtension(Extension::kId).empty();
Danil Chapovalov5e57b172016-09-02 19:15:59 +0200168}
169
danilchap1edb7ab2016-04-20 05:25:10 -0700170template <typename Extension, typename... Values>
171bool Packet::GetExtension(Values... values) const {
danilchap978504e2017-04-06 01:03:53 -0700172 auto raw = FindExtension(Extension::kId);
173 if (raw.empty())
danilchap1edb7ab2016-04-20 05:25:10 -0700174 return false;
danilchap978504e2017-04-06 01:03:53 -0700175 return Extension::Parse(raw, values...);
danilchap1edb7ab2016-04-20 05:25:10 -0700176}
177
178template <typename Extension, typename... Values>
179bool Packet::SetExtension(Values... values) {
erikvargae6b16192017-05-11 02:36:32 -0700180 const size_t value_size = Extension::ValueSize(values...);
181 if (value_size == 0 || value_size > 16)
182 return false;
183 auto buffer = AllocateExtension(Extension::kId, value_size);
danilchap653063f2017-04-03 06:16:30 -0700184 if (buffer.empty())
danilchap1edb7ab2016-04-20 05:25:10 -0700185 return false;
danilchap653063f2017-04-03 06:16:30 -0700186 return Extension::Write(buffer.data(), values...);
danilchap1edb7ab2016-04-20 05:25:10 -0700187}
188
189template <typename Extension>
190bool Packet::ReserveExtension() {
danilchap653063f2017-04-03 06:16:30 -0700191 auto buffer = AllocateExtension(Extension::kId, Extension::kValueSizeBytes);
192 if (buffer.empty())
danilchap1edb7ab2016-04-20 05:25:10 -0700193 return false;
danilchap653063f2017-04-03 06:16:30 -0700194 memset(buffer.data(), 0, Extension::kValueSizeBytes);
danilchap1edb7ab2016-04-20 05:25:10 -0700195 return true;
196}
197} // namespace rtp
198} // namespace webrtc
199
200#endif // WEBRTC_MODULES_RTP_RTCP_SOURCE_RTP_PACKET_H_