danilchap | 1edb7ab | 2016-04-20 05:25:10 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 15 | #include "webrtc/base/basictypes.h" |
Danil Chapovalov | 31e4e80 | 2016-08-03 18:27:40 +0200 | [diff] [blame] | 16 | #include "webrtc/base/copyonwritebuffer.h" |
danilchap | 1edb7ab | 2016-04-20 05:25:10 -0700 | [diff] [blame] | 17 | #include "webrtc/modules/rtp_rtcp/include/rtp_rtcp_defines.h" |
| 18 | |
| 19 | namespace webrtc { |
| 20 | struct RTPHeader; |
| 21 | class RtpHeaderExtensionMap; |
| 22 | class Random; |
| 23 | |
| 24 | namespace rtp { |
| 25 | class Packet { |
| 26 | public: |
| 27 | using ExtensionType = RTPExtensionType; |
| 28 | using ExtensionManager = RtpHeaderExtensionMap; |
| 29 | static constexpr size_t kMaxExtensionHeaders = 14; |
| 30 | |
| 31 | // Parse and copy given buffer into Packet. |
| 32 | bool Parse(const uint8_t* buffer, size_t size); |
| 33 | |
| 34 | // Parse and move given buffer into Packet. |
Danil Chapovalov | 31e4e80 | 2016-08-03 18:27:40 +0200 | [diff] [blame] | 35 | bool Parse(rtc::CopyOnWriteBuffer packet); |
danilchap | 1edb7ab | 2016-04-20 05:25:10 -0700 | [diff] [blame] | 36 | |
| 37 | // Maps parsed extensions to their types to allow use of GetExtension. |
| 38 | // Used after parsing when |extensions| can't be provided until base rtp |
| 39 | // header is parsed. |
| 40 | void IdentifyExtensions(const ExtensionManager* extensions); |
| 41 | |
| 42 | // Header. |
| 43 | bool Marker() const; |
| 44 | uint8_t PayloadType() const; |
| 45 | uint16_t SequenceNumber() const; |
| 46 | uint32_t Timestamp() const; |
| 47 | uint32_t Ssrc() const; |
| 48 | std::vector<uint32_t> Csrcs() const; |
| 49 | |
| 50 | // TODO(danilchap): Remove this function when all code update to use RtpPacket |
| 51 | // directly. Function is there just for easier backward compatibilty. |
| 52 | void GetHeader(RTPHeader* header) const; |
| 53 | |
| 54 | size_t headers_size() const; |
| 55 | |
| 56 | // Payload. |
| 57 | size_t payload_size() const; |
| 58 | size_t padding_size() const; |
| 59 | const uint8_t* payload() const; |
| 60 | |
| 61 | // Buffer. |
Danil Chapovalov | 31e4e80 | 2016-08-03 18:27:40 +0200 | [diff] [blame] | 62 | rtc::CopyOnWriteBuffer Buffer() const; |
danilchap | 1edb7ab | 2016-04-20 05:25:10 -0700 | [diff] [blame] | 63 | size_t capacity() const; |
| 64 | size_t size() const; |
| 65 | const uint8_t* data() const; |
| 66 | size_t FreeCapacity() const; |
| 67 | size_t MaxPayloadSize() const; |
| 68 | |
| 69 | // Reset fields and buffer. |
| 70 | void Clear(); |
| 71 | |
| 72 | // Header setters. |
Danil Chapovalov | 31e4e80 | 2016-08-03 18:27:40 +0200 | [diff] [blame] | 73 | void CopyHeaderFrom(const Packet& packet); |
danilchap | 1edb7ab | 2016-04-20 05:25:10 -0700 | [diff] [blame] | 74 | void SetMarker(bool marker_bit); |
| 75 | void SetPayloadType(uint8_t payload_type); |
| 76 | void SetSequenceNumber(uint16_t seq_no); |
| 77 | void SetTimestamp(uint32_t timestamp); |
| 78 | void SetSsrc(uint32_t ssrc); |
| 79 | |
| 80 | // Writes csrc list. Assumes: |
| 81 | // a) There is enough room left in buffer. |
| 82 | // b) Extension headers, payload or padding data has not already been added. |
| 83 | void SetCsrcs(const std::vector<uint32_t>& csrcs); |
| 84 | |
| 85 | // Header extensions. |
Danil Chapovalov | 5e57b17 | 2016-09-02 19:15:59 +0200 | [diff] [blame] | 86 | template <typename Extension> |
| 87 | bool HasExtension() const; |
| 88 | |
danilchap | 1edb7ab | 2016-04-20 05:25:10 -0700 | [diff] [blame] | 89 | template <typename Extension, typename... Values> |
| 90 | bool GetExtension(Values...) const; |
| 91 | |
| 92 | template <typename Extension, typename... Values> |
| 93 | bool SetExtension(Values...); |
| 94 | |
| 95 | template <typename Extension> |
| 96 | bool ReserveExtension(); |
| 97 | |
| 98 | // Reserve size_bytes for payload. Returns nullptr on failure. |
| 99 | uint8_t* AllocatePayload(size_t size_bytes); |
| 100 | void SetPayloadSize(size_t size_bytes); |
| 101 | bool SetPadding(uint8_t size_bytes, Random* random); |
| 102 | |
| 103 | protected: |
| 104 | // |extensions| required for SetExtension/ReserveExtension functions during |
| 105 | // packet creating and used if available in Parse function. |
| 106 | // Adding and getting extensions will fail until |extensions| is |
| 107 | // provided via constructor or IdentifyExtensions function. |
| 108 | explicit Packet(const ExtensionManager* extensions); |
Danil Chapovalov | 31e4e80 | 2016-08-03 18:27:40 +0200 | [diff] [blame] | 109 | Packet(const Packet&) = default; |
danilchap | 1edb7ab | 2016-04-20 05:25:10 -0700 | [diff] [blame] | 110 | Packet(const ExtensionManager* extensions, size_t capacity); |
| 111 | virtual ~Packet(); |
| 112 | |
Danil Chapovalov | 31e4e80 | 2016-08-03 18:27:40 +0200 | [diff] [blame] | 113 | Packet& operator=(const Packet&) = default; |
| 114 | |
danilchap | 1edb7ab | 2016-04-20 05:25:10 -0700 | [diff] [blame] | 115 | private: |
| 116 | struct ExtensionInfo { |
| 117 | ExtensionType type; |
| 118 | uint16_t offset; |
| 119 | uint8_t length; |
| 120 | }; |
| 121 | |
| 122 | // Helper function for Parse. Fill header fields using data in given buffer, |
| 123 | // but does not touch packet own buffer, leaving packet in invalid state. |
| 124 | bool ParseBuffer(const uint8_t* buffer, size_t size); |
| 125 | |
| 126 | // Find an extension based on the type field of the parameter. |
| 127 | // If found, length field would be validated, the offset field will be set |
| 128 | // and true returned, |
| 129 | // otherwise the parameter will be unchanged and false is returned. |
| 130 | bool FindExtension(ExtensionType type, |
| 131 | uint8_t length, |
| 132 | uint16_t* offset) const; |
| 133 | |
| 134 | // Find or allocate an extension, based on the type field of the parameter. |
| 135 | // If found, the length field be checked against what is already registered |
| 136 | // and the offset field will be set, then true is returned. If allocated, the |
| 137 | // length field will be used for allocation and the offset update to indicate |
| 138 | // position, the true is returned. |
| 139 | // If not found and allocations fails, false is returned and parameter remains |
| 140 | // unchanged. |
| 141 | bool AllocateExtension(ExtensionType type, uint8_t length, uint16_t* offset); |
| 142 | |
| 143 | uint8_t* WriteAt(size_t offset); |
| 144 | void WriteAt(size_t offset, uint8_t byte); |
| 145 | |
| 146 | const ExtensionManager* extensions_; |
| 147 | |
| 148 | // Header. |
| 149 | bool marker_; |
| 150 | uint8_t payload_type_; |
| 151 | uint8_t padding_size_; |
| 152 | uint16_t sequence_number_; |
| 153 | uint32_t timestamp_; |
| 154 | uint32_t ssrc_; |
| 155 | size_t payload_offset_; // Match header size with csrcs and extensions. |
| 156 | size_t payload_size_; |
| 157 | |
| 158 | uint8_t num_extensions_ = 0; |
| 159 | ExtensionInfo extension_entries_[kMaxExtensionHeaders]; |
| 160 | uint16_t extensions_size_ = 0; // Unaligned. |
Danil Chapovalov | 31e4e80 | 2016-08-03 18:27:40 +0200 | [diff] [blame] | 161 | rtc::CopyOnWriteBuffer buffer_; |
danilchap | 1edb7ab | 2016-04-20 05:25:10 -0700 | [diff] [blame] | 162 | |
Danil Chapovalov | 31e4e80 | 2016-08-03 18:27:40 +0200 | [diff] [blame] | 163 | Packet() = delete; |
danilchap | 1edb7ab | 2016-04-20 05:25:10 -0700 | [diff] [blame] | 164 | }; |
| 165 | |
Danil Chapovalov | 5e57b17 | 2016-09-02 19:15:59 +0200 | [diff] [blame] | 166 | template <typename Extension> |
| 167 | bool Packet::HasExtension() const { |
| 168 | uint16_t offset = 0; |
| 169 | return FindExtension(Extension::kId, Extension::kValueSizeBytes, &offset); |
| 170 | } |
| 171 | |
danilchap | 1edb7ab | 2016-04-20 05:25:10 -0700 | [diff] [blame] | 172 | template <typename Extension, typename... Values> |
| 173 | bool Packet::GetExtension(Values... values) const { |
| 174 | uint16_t offset = 0; |
| 175 | if (!FindExtension(Extension::kId, Extension::kValueSizeBytes, &offset)) |
| 176 | return false; |
| 177 | return Extension::Parse(data() + offset, values...); |
| 178 | } |
| 179 | |
| 180 | template <typename Extension, typename... Values> |
| 181 | bool Packet::SetExtension(Values... values) { |
| 182 | uint16_t offset = 0; |
| 183 | if (!AllocateExtension(Extension::kId, Extension::kValueSizeBytes, &offset)) |
| 184 | return false; |
| 185 | return Extension::Write(WriteAt(offset), values...); |
| 186 | } |
| 187 | |
| 188 | template <typename Extension> |
| 189 | bool Packet::ReserveExtension() { |
| 190 | uint16_t offset = 0; |
| 191 | if (!AllocateExtension(Extension::kId, Extension::kValueSizeBytes, &offset)) |
| 192 | return false; |
| 193 | memset(WriteAt(offset), 0, Extension::kValueSizeBytes); |
| 194 | return true; |
| 195 | } |
| 196 | } // namespace rtp |
| 197 | } // namespace webrtc |
| 198 | |
| 199 | #endif // WEBRTC_MODULES_RTP_RTCP_SOURCE_RTP_PACKET_H_ |