henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1 | /* |
kjellander | 1afca73 | 2016-02-07 20:46:45 -0800 | [diff] [blame] | 2 | * Copyright (c) 2011 The WebRTC project authors. All Rights Reserved. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3 | * |
kjellander | 1afca73 | 2016-02-07 20:46:45 -0800 | [diff] [blame] | 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. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 11 | #include "media/base/rtp_utils.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 12 | |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 13 | #include <string.h> |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 14 | |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 15 | #include <vector> |
| 16 | |
Sergey Ulanov | dc305db | 2016-01-14 17:14:54 -0800 | [diff] [blame] | 17 | // PacketTimeUpdateParams is defined in asyncpacketsocket.h. |
| 18 | // TODO(sergeyu): Find more appropriate place for PacketTimeUpdateParams. |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 19 | #include "media/base/turn_utils.h" |
| 20 | #include "rtc_base/async_packet_socket.h" |
| 21 | #include "rtc_base/byte_order.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 22 | #include "rtc_base/checks.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 23 | #include "rtc_base/message_digest.h" |
Sergey Ulanov | dc305db | 2016-01-14 17:14:54 -0800 | [diff] [blame] | 24 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 25 | namespace cricket { |
| 26 | |
pkasting@chromium.org | 0e81fdf | 2015-02-02 23:54:03 +0000 | [diff] [blame] | 27 | static const uint8_t kRtpVersion = 2; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 28 | static const size_t kRtpFlagsOffset = 0; |
| 29 | static const size_t kRtpPayloadTypeOffset = 1; |
| 30 | static const size_t kRtpSeqNumOffset = 2; |
| 31 | static const size_t kRtpTimestampOffset = 4; |
| 32 | static const size_t kRtpSsrcOffset = 8; |
| 33 | static const size_t kRtcpPayloadTypeOffset = 1; |
Sergey Ulanov | dc305db | 2016-01-14 17:14:54 -0800 | [diff] [blame] | 34 | static const size_t kRtpExtensionHeaderLen = 4; |
| 35 | static const size_t kAbsSendTimeExtensionLen = 3; |
| 36 | static const size_t kOneByteExtensionHeaderLen = 1; |
| 37 | |
| 38 | namespace { |
| 39 | |
| 40 | // Fake auth tag written by the sender when external authentication is enabled. |
| 41 | // HMAC in packet will be compared against this value before updating packet |
| 42 | // with actual HMAC value. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 43 | static const uint8_t kFakeAuthTag[10] = {0xba, 0xdd, 0xba, 0xdd, 0xba, |
| 44 | 0xdd, 0xba, 0xdd, 0xba, 0xdd}; |
Sergey Ulanov | dc305db | 2016-01-14 17:14:54 -0800 | [diff] [blame] | 45 | |
| 46 | void UpdateAbsSendTimeExtensionValue(uint8_t* extension_data, |
| 47 | size_t length, |
| 48 | uint64_t time_us) { |
| 49 | // Absolute send time in RTP streams. |
| 50 | // |
| 51 | // The absolute send time is signaled to the receiver in-band using the |
| 52 | // general mechanism for RTP header extensions [RFC5285]. The payload |
| 53 | // of this extension (the transmitted value) is a 24-bit unsigned integer |
| 54 | // containing the sender's current time in seconds as a fixed point number |
| 55 | // with 18 bits fractional part. |
| 56 | // |
| 57 | // The form of the absolute send time extension block: |
| 58 | // |
| 59 | // 0 1 2 3 |
| 60 | // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 |
| 61 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 62 | // | ID | len=2 | absolute send time | |
| 63 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 64 | if (length != kAbsSendTimeExtensionLen) { |
| 65 | RTC_NOTREACHED(); |
| 66 | return; |
| 67 | } |
| 68 | |
| 69 | // Convert microseconds to a 6.18 fixed point value in seconds. |
| 70 | uint32_t send_time = ((time_us << 18) / 1000000) & 0x00FFFFFF; |
| 71 | extension_data[0] = static_cast<uint8_t>(send_time >> 16); |
| 72 | extension_data[1] = static_cast<uint8_t>(send_time >> 8); |
| 73 | extension_data[2] = static_cast<uint8_t>(send_time); |
| 74 | } |
| 75 | |
| 76 | // Assumes |length| is actual packet length + tag length. Updates HMAC at end of |
| 77 | // the RTP packet. |
| 78 | void UpdateRtpAuthTag(uint8_t* rtp, |
| 79 | size_t length, |
| 80 | const rtc::PacketTimeUpdateParams& packet_time_params) { |
| 81 | // If there is no key, return. |
| 82 | if (packet_time_params.srtp_auth_key.empty()) { |
| 83 | return; |
| 84 | } |
| 85 | |
| 86 | size_t tag_length = packet_time_params.srtp_auth_tag_len; |
| 87 | |
| 88 | // ROC (rollover counter) is at the beginning of the auth tag. |
| 89 | const size_t kRocLength = 4; |
| 90 | if (tag_length < kRocLength || tag_length > length) { |
| 91 | RTC_NOTREACHED(); |
| 92 | return; |
| 93 | } |
| 94 | |
| 95 | uint8_t* auth_tag = rtp + (length - tag_length); |
| 96 | |
| 97 | // We should have a fake HMAC value @ auth_tag. |
| 98 | RTC_DCHECK_EQ(0, memcmp(auth_tag, kFakeAuthTag, tag_length)); |
| 99 | |
| 100 | // Copy ROC after end of rtp packet. |
| 101 | memcpy(auth_tag, &packet_time_params.srtp_packet_index, kRocLength); |
| 102 | // Authentication of a RTP packet will have RTP packet + ROC size. |
| 103 | size_t auth_required_length = length - tag_length + kRocLength; |
| 104 | |
| 105 | uint8_t output[64]; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 106 | size_t result = |
| 107 | rtc::ComputeHmac(rtc::DIGEST_SHA_1, &packet_time_params.srtp_auth_key[0], |
| 108 | packet_time_params.srtp_auth_key.size(), rtp, |
| 109 | auth_required_length, output, sizeof(output)); |
Sergey Ulanov | dc305db | 2016-01-14 17:14:54 -0800 | [diff] [blame] | 110 | |
| 111 | if (result < tag_length) { |
| 112 | RTC_NOTREACHED(); |
| 113 | return; |
| 114 | } |
| 115 | |
| 116 | // Copy HMAC from output to packet. This is required as auth tag length |
| 117 | // may not be equal to the actual HMAC length. |
| 118 | memcpy(auth_tag, output, tag_length); |
| 119 | } |
| 120 | |
Steve Anton | e78bcb9 | 2017-10-31 09:53:08 -0700 | [diff] [blame] | 121 | } // namespace |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 122 | |
| 123 | bool GetUint8(const void* data, size_t offset, int* value) { |
| 124 | if (!data || !value) { |
| 125 | return false; |
| 126 | } |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 127 | *value = *(static_cast<const uint8_t*>(data) + offset); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 128 | return true; |
| 129 | } |
| 130 | |
| 131 | bool GetUint16(const void* data, size_t offset, int* value) { |
| 132 | if (!data || !value) { |
| 133 | return false; |
| 134 | } |
| 135 | *value = static_cast<int>( |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 136 | rtc::GetBE16(static_cast<const uint8_t*>(data) + offset)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 137 | return true; |
| 138 | } |
| 139 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 140 | bool GetUint32(const void* data, size_t offset, uint32_t* value) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 141 | if (!data || !value) { |
| 142 | return false; |
| 143 | } |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 144 | *value = rtc::GetBE32(static_cast<const uint8_t*>(data) + offset); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 145 | return true; |
| 146 | } |
| 147 | |
pkasting@chromium.org | 0e81fdf | 2015-02-02 23:54:03 +0000 | [diff] [blame] | 148 | bool SetUint8(void* data, size_t offset, uint8_t value) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 149 | if (!data) { |
| 150 | return false; |
| 151 | } |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 152 | rtc::Set8(data, offset, value); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 153 | return true; |
| 154 | } |
| 155 | |
pkasting@chromium.org | 0e81fdf | 2015-02-02 23:54:03 +0000 | [diff] [blame] | 156 | bool SetUint16(void* data, size_t offset, uint16_t value) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 157 | if (!data) { |
| 158 | return false; |
| 159 | } |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 160 | rtc::SetBE16(static_cast<uint8_t*>(data) + offset, value); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 161 | return true; |
| 162 | } |
| 163 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 164 | bool SetUint32(void* data, size_t offset, uint32_t value) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 165 | if (!data) { |
| 166 | return false; |
| 167 | } |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 168 | rtc::SetBE32(static_cast<uint8_t*>(data) + offset, value); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 169 | return true; |
| 170 | } |
| 171 | |
| 172 | bool GetRtpFlags(const void* data, size_t len, int* value) { |
| 173 | if (len < kMinRtpPacketLen) { |
| 174 | return false; |
| 175 | } |
| 176 | return GetUint8(data, kRtpFlagsOffset, value); |
| 177 | } |
| 178 | |
| 179 | bool GetRtpPayloadType(const void* data, size_t len, int* value) { |
| 180 | if (len < kMinRtpPacketLen) { |
| 181 | return false; |
| 182 | } |
| 183 | if (!GetUint8(data, kRtpPayloadTypeOffset, value)) { |
| 184 | return false; |
| 185 | } |
| 186 | *value &= 0x7F; |
| 187 | return true; |
| 188 | } |
| 189 | |
| 190 | bool GetRtpSeqNum(const void* data, size_t len, int* value) { |
| 191 | if (len < kMinRtpPacketLen) { |
| 192 | return false; |
| 193 | } |
| 194 | return GetUint16(data, kRtpSeqNumOffset, value); |
| 195 | } |
| 196 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 197 | bool GetRtpTimestamp(const void* data, size_t len, uint32_t* value) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 198 | if (len < kMinRtpPacketLen) { |
| 199 | return false; |
| 200 | } |
| 201 | return GetUint32(data, kRtpTimestampOffset, value); |
| 202 | } |
| 203 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 204 | bool GetRtpSsrc(const void* data, size_t len, uint32_t* value) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 205 | if (len < kMinRtpPacketLen) { |
| 206 | return false; |
| 207 | } |
| 208 | return GetUint32(data, kRtpSsrcOffset, value); |
| 209 | } |
| 210 | |
| 211 | bool GetRtpHeaderLen(const void* data, size_t len, size_t* value) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 212 | if (!data || len < kMinRtpPacketLen || !value) |
| 213 | return false; |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 214 | const uint8_t* header = static_cast<const uint8_t*>(data); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 215 | // Get base header size + length of CSRCs (not counting extension yet). |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 216 | size_t header_size = kMinRtpPacketLen + (header[0] & 0xF) * sizeof(uint32_t); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 217 | if (len < header_size) |
| 218 | return false; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 219 | // If there's an extension, read and add in the extension size. |
| 220 | if (header[0] & 0x10) { |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 221 | if (len < header_size + sizeof(uint32_t)) |
| 222 | return false; |
| 223 | header_size += |
| 224 | ((rtc::GetBE16(header + header_size + 2) + 1) * sizeof(uint32_t)); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 225 | if (len < header_size) |
| 226 | return false; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 227 | } |
| 228 | *value = header_size; |
| 229 | return true; |
| 230 | } |
| 231 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 232 | bool GetRtpHeader(const void* data, size_t len, RtpHeader* header) { |
| 233 | return (GetRtpPayloadType(data, len, &(header->payload_type)) && |
| 234 | GetRtpSeqNum(data, len, &(header->seq_num)) && |
| 235 | GetRtpTimestamp(data, len, &(header->timestamp)) && |
| 236 | GetRtpSsrc(data, len, &(header->ssrc))); |
| 237 | } |
| 238 | |
| 239 | bool GetRtcpType(const void* data, size_t len, int* value) { |
| 240 | if (len < kMinRtcpPacketLen) { |
| 241 | return false; |
| 242 | } |
| 243 | return GetUint8(data, kRtcpPayloadTypeOffset, value); |
| 244 | } |
| 245 | |
| 246 | // This method returns SSRC first of RTCP packet, except if packet is SDES. |
| 247 | // TODO(mallinath) - Fully implement RFC 5506. This standard doesn't restrict |
| 248 | // to send non-compound packets only to feedback messages. |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 249 | bool GetRtcpSsrc(const void* data, size_t len, uint32_t* value) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 250 | // Packet should be at least of 8 bytes, to get SSRC from a RTCP packet. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 251 | if (!data || len < kMinRtcpPacketLen + 4 || !value) |
| 252 | return false; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 253 | int pl_type; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 254 | if (!GetRtcpType(data, len, &pl_type)) |
| 255 | return false; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 256 | // SDES packet parsing is not supported. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 257 | if (pl_type == kRtcpTypeSDES) |
| 258 | return false; |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 259 | *value = rtc::GetBE32(static_cast<const uint8_t*>(data) + 4); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 260 | return true; |
| 261 | } |
| 262 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 263 | bool SetRtpSsrc(void* data, size_t len, uint32_t value) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 264 | return SetUint32(data, kRtpSsrcOffset, value); |
| 265 | } |
| 266 | |
| 267 | // Assumes version 2, no padding, no extensions, no csrcs. |
| 268 | bool SetRtpHeader(void* data, size_t len, const RtpHeader& header) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 269 | if (!IsValidRtpPayloadType(header.payload_type) || header.seq_num < 0 || |
| 270 | header.seq_num > static_cast<int>(UINT16_MAX)) { |
pkasting@chromium.org | 0e81fdf | 2015-02-02 23:54:03 +0000 | [diff] [blame] | 271 | return false; |
| 272 | } |
| 273 | return (SetUint8(data, kRtpFlagsOffset, kRtpVersion << 6) && |
| 274 | SetUint8(data, kRtpPayloadTypeOffset, header.payload_type & 0x7F) && |
| 275 | SetUint16(data, kRtpSeqNumOffset, |
| 276 | static_cast<uint16_t>(header.seq_num)) && |
| 277 | SetUint32(data, kRtpTimestampOffset, header.timestamp) && |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 278 | SetRtpSsrc(data, len, header.ssrc)); |
| 279 | } |
| 280 | |
Amit Hilbuch | b947fe1 | 2019-03-22 12:04:26 -0700 | [diff] [blame] | 281 | static bool HasCorrectRtpVersion(rtc::ArrayView<const uint8_t> packet) { |
| 282 | return packet.data()[0] >> 6 == kRtpVersion; |
Amit Hilbuch | edd2054 | 2019-03-18 12:33:43 -0700 | [diff] [blame] | 283 | } |
buildbot@webrtc.org | 1ef789d | 2014-06-19 23:54:12 +0000 | [diff] [blame] | 284 | |
Amit Hilbuch | edd2054 | 2019-03-18 12:33:43 -0700 | [diff] [blame] | 285 | bool IsRtpPacket(rtc::ArrayView<const char> packet) { |
Amit Hilbuch | b947fe1 | 2019-03-22 12:04:26 -0700 | [diff] [blame] | 286 | return packet.size() >= kMinRtpPacketLen && |
| 287 | HasCorrectRtpVersion( |
| 288 | rtc::reinterpret_array_view<const uint8_t>(packet)); |
buildbot@webrtc.org | 1ef789d | 2014-06-19 23:54:12 +0000 | [diff] [blame] | 289 | } |
| 290 | |
Zhi Huang | 365381f | 2018-04-13 16:44:34 -0700 | [diff] [blame] | 291 | // Check the RTP payload type. If 63 < payload type < 96, it's RTCP. |
| 292 | // For additional details, see http://tools.ietf.org/html/rfc5761. |
Amit Hilbuch | edd2054 | 2019-03-18 12:33:43 -0700 | [diff] [blame] | 293 | bool IsRtcpPacket(rtc::ArrayView<const char> packet) { |
Amit Hilbuch | b947fe1 | 2019-03-22 12:04:26 -0700 | [diff] [blame] | 294 | if (packet.size() < kMinRtcpPacketLen || |
| 295 | !HasCorrectRtpVersion( |
| 296 | rtc::reinterpret_array_view<const uint8_t>(packet))) { |
Zhi Huang | 365381f | 2018-04-13 16:44:34 -0700 | [diff] [blame] | 297 | return false; |
| 298 | } |
Piotr (Peter) Slatala | 042bb00 | 2019-01-30 14:57:12 -0800 | [diff] [blame] | 299 | |
Amit Hilbuch | edd2054 | 2019-03-18 12:33:43 -0700 | [diff] [blame] | 300 | char pt = packet[1] & 0x7F; |
Zhi Huang | 365381f | 2018-04-13 16:44:34 -0700 | [diff] [blame] | 301 | return (63 < pt) && (pt < 96); |
| 302 | } |
| 303 | |
pkasting@chromium.org | e9facf8 | 2015-02-17 20:36:28 +0000 | [diff] [blame] | 304 | bool IsValidRtpPayloadType(int payload_type) { |
| 305 | return payload_type >= 0 && payload_type <= 127; |
| 306 | } |
| 307 | |
Amit Hilbuch | edd2054 | 2019-03-18 12:33:43 -0700 | [diff] [blame] | 308 | bool IsValidRtpPacketSize(RtpPacketType packet_type, size_t size) { |
Amit Hilbuch | b947fe1 | 2019-03-22 12:04:26 -0700 | [diff] [blame] | 309 | RTC_DCHECK_NE(RtpPacketType::kUnknown, packet_type); |
Amit Hilbuch | edd2054 | 2019-03-18 12:33:43 -0700 | [diff] [blame] | 310 | size_t min_packet_length = packet_type == RtpPacketType::kRtcp |
| 311 | ? kMinRtcpPacketLen |
| 312 | : kMinRtpPacketLen; |
| 313 | return size >= min_packet_length && size <= kMaxRtpPacketLen; |
zstein | 3dcf0e9 | 2017-06-01 13:22:42 -0700 | [diff] [blame] | 314 | } |
| 315 | |
Amit Hilbuch | edd2054 | 2019-03-18 12:33:43 -0700 | [diff] [blame] | 316 | absl::string_view RtpPacketTypeToString(RtpPacketType packet_type) { |
| 317 | switch (packet_type) { |
| 318 | case RtpPacketType::kRtp: |
| 319 | return "RTP"; |
| 320 | case RtpPacketType::kRtcp: |
| 321 | return "RTCP"; |
| 322 | case RtpPacketType::kUnknown: |
| 323 | return "Unknown"; |
| 324 | } |
| 325 | } |
| 326 | |
| 327 | RtpPacketType InferRtpPacketType(rtc::ArrayView<const char> packet) { |
| 328 | // RTCP packets are RTP packets so must check that first. |
| 329 | if (IsRtcpPacket(packet)) { |
| 330 | return RtpPacketType::kRtcp; |
| 331 | } |
| 332 | if (IsRtpPacket(packet)) { |
| 333 | return RtpPacketType::kRtp; |
| 334 | } |
| 335 | return RtpPacketType::kUnknown; |
zstein | 3dcf0e9 | 2017-06-01 13:22:42 -0700 | [diff] [blame] | 336 | } |
| 337 | |
Sergey Ulanov | dc305db | 2016-01-14 17:14:54 -0800 | [diff] [blame] | 338 | bool ValidateRtpHeader(const uint8_t* rtp, |
| 339 | size_t length, |
| 340 | size_t* header_length) { |
| 341 | if (header_length) { |
| 342 | *header_length = 0; |
| 343 | } |
| 344 | |
| 345 | if (length < kMinRtpPacketLen) { |
| 346 | return false; |
| 347 | } |
| 348 | |
| 349 | size_t cc_count = rtp[0] & 0x0F; |
| 350 | size_t header_length_without_extension = kMinRtpPacketLen + 4 * cc_count; |
| 351 | if (header_length_without_extension > length) { |
| 352 | return false; |
| 353 | } |
| 354 | |
| 355 | // If extension bit is not set, we are done with header processing, as input |
| 356 | // length is verified above. |
| 357 | if (!(rtp[0] & 0x10)) { |
| 358 | if (header_length) |
| 359 | *header_length = header_length_without_extension; |
| 360 | |
| 361 | return true; |
| 362 | } |
| 363 | |
| 364 | rtp += header_length_without_extension; |
| 365 | |
| 366 | if (header_length_without_extension + kRtpExtensionHeaderLen > length) { |
| 367 | return false; |
| 368 | } |
| 369 | |
| 370 | // Getting extension profile length. |
| 371 | // Length is in 32 bit words. |
| 372 | uint16_t extension_length_in_32bits = rtc::GetBE16(rtp + 2); |
| 373 | size_t extension_length = extension_length_in_32bits * 4; |
| 374 | |
| 375 | size_t rtp_header_length = extension_length + |
| 376 | header_length_without_extension + |
| 377 | kRtpExtensionHeaderLen; |
| 378 | |
| 379 | // Verify input length against total header size. |
| 380 | if (rtp_header_length > length) { |
| 381 | return false; |
| 382 | } |
| 383 | |
| 384 | if (header_length) { |
| 385 | *header_length = rtp_header_length; |
| 386 | } |
| 387 | return true; |
| 388 | } |
| 389 | |
| 390 | // ValidateRtpHeader() must be called before this method to make sure, we have |
| 391 | // a sane rtp packet. |
| 392 | bool UpdateRtpAbsSendTimeExtension(uint8_t* rtp, |
| 393 | size_t length, |
| 394 | int extension_id, |
| 395 | uint64_t time_us) { |
| 396 | // 0 1 2 3 |
| 397 | // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 |
| 398 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 399 | // |V=2|P|X| CC |M| PT | sequence number | |
| 400 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 401 | // | timestamp | |
| 402 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 403 | // | synchronization source (SSRC) identifier | |
| 404 | // +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+ |
| 405 | // | contributing source (CSRC) identifiers | |
| 406 | // | .... | |
| 407 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 408 | |
| 409 | // Return if extension bit is not set. |
| 410 | if (!(rtp[0] & 0x10)) { |
| 411 | return true; |
| 412 | } |
| 413 | |
| 414 | size_t cc_count = rtp[0] & 0x0F; |
| 415 | size_t header_length_without_extension = kMinRtpPacketLen + 4 * cc_count; |
| 416 | |
| 417 | rtp += header_length_without_extension; |
| 418 | |
| 419 | // Getting extension profile ID and length. |
| 420 | uint16_t profile_id = rtc::GetBE16(rtp); |
| 421 | // Length is in 32 bit words. |
| 422 | uint16_t extension_length_in_32bits = rtc::GetBE16(rtp + 2); |
| 423 | size_t extension_length = extension_length_in_32bits * 4; |
| 424 | |
| 425 | rtp += kRtpExtensionHeaderLen; // Moving past extension header. |
| 426 | |
| 427 | bool found = false; |
| 428 | // WebRTC is using one byte header extension. |
| 429 | // TODO(mallinath) - Handle two byte header extension. |
| 430 | if (profile_id == 0xBEDE) { // OneByte extension header |
| 431 | // 0 |
| 432 | // 0 1 2 3 4 5 6 7 |
| 433 | // +-+-+-+-+-+-+-+-+ |
| 434 | // | ID |length | |
| 435 | // +-+-+-+-+-+-+-+-+ |
| 436 | |
| 437 | // 0 1 2 3 |
| 438 | // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 |
| 439 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 440 | // | 0xBE | 0xDE | length=3 | |
| 441 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 442 | // | ID | L=0 | data | ID | L=1 | data... |
| 443 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 444 | // ...data | 0 (pad) | 0 (pad) | ID | L=3 | |
| 445 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 446 | // | data | |
| 447 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 448 | const uint8_t* extension_start = rtp; |
| 449 | const uint8_t* extension_end = extension_start + extension_length; |
| 450 | |
| 451 | while (rtp < extension_end) { |
| 452 | const int id = (*rtp & 0xF0) >> 4; |
| 453 | const size_t length = (*rtp & 0x0F) + 1; |
| 454 | if (rtp + kOneByteExtensionHeaderLen + length > extension_end) { |
| 455 | return false; |
| 456 | } |
| 457 | // The 4-bit length is the number minus one of data bytes of this header |
| 458 | // extension element following the one-byte header. |
| 459 | if (id == extension_id) { |
| 460 | UpdateAbsSendTimeExtensionValue(rtp + kOneByteExtensionHeaderLen, |
| 461 | length, time_us); |
| 462 | found = true; |
| 463 | break; |
| 464 | } |
| 465 | rtp += kOneByteExtensionHeaderLen + length; |
| 466 | // Counting padding bytes. |
| 467 | while ((rtp < extension_end) && (*rtp == 0)) { |
| 468 | ++rtp; |
| 469 | } |
| 470 | } |
| 471 | } |
| 472 | return found; |
| 473 | } |
| 474 | |
| 475 | bool ApplyPacketOptions(uint8_t* data, |
| 476 | size_t length, |
| 477 | const rtc::PacketTimeUpdateParams& packet_time_params, |
| 478 | uint64_t time_us) { |
| 479 | RTC_DCHECK(data); |
| 480 | RTC_DCHECK(length); |
| 481 | |
| 482 | // if there is no valid |rtp_sendtime_extension_id| and |srtp_auth_key| in |
| 483 | // PacketOptions, nothing to be updated in this packet. |
| 484 | if (packet_time_params.rtp_sendtime_extension_id == -1 && |
| 485 | packet_time_params.srtp_auth_key.empty()) { |
| 486 | return true; |
| 487 | } |
| 488 | |
| 489 | // If there is a srtp auth key present then the packet must be an RTP packet. |
| 490 | // RTP packet may have been wrapped in a TURN Channel Data or TURN send |
| 491 | // indication. |
| 492 | size_t rtp_start_pos; |
| 493 | size_t rtp_length; |
| 494 | if (!UnwrapTurnPacket(data, length, &rtp_start_pos, &rtp_length)) { |
| 495 | RTC_NOTREACHED(); |
| 496 | return false; |
| 497 | } |
| 498 | |
| 499 | // Making sure we have a valid RTP packet at the end. |
Amit Hilbuch | b947fe1 | 2019-03-22 12:04:26 -0700 | [diff] [blame] | 500 | auto packet = rtc::MakeArrayView(data + rtp_start_pos, rtp_length); |
| 501 | if (!IsRtpPacket(rtc::reinterpret_array_view<const char>(packet)) || |
Sergey Ulanov | dc305db | 2016-01-14 17:14:54 -0800 | [diff] [blame] | 502 | !ValidateRtpHeader(data + rtp_start_pos, rtp_length, nullptr)) { |
| 503 | RTC_NOTREACHED(); |
| 504 | return false; |
| 505 | } |
| 506 | |
| 507 | uint8_t* start = data + rtp_start_pos; |
| 508 | // If packet option has non default value (-1) for sendtime extension id, |
| 509 | // then we should parse the rtp packet to update the timestamp. Otherwise |
| 510 | // just calculate HMAC and update packet with it. |
| 511 | if (packet_time_params.rtp_sendtime_extension_id != -1) { |
| 512 | UpdateRtpAbsSendTimeExtension(start, rtp_length, |
| 513 | packet_time_params.rtp_sendtime_extension_id, |
| 514 | time_us); |
| 515 | } |
| 516 | |
| 517 | UpdateRtpAuthTag(start, rtp_length, packet_time_params); |
| 518 | return true; |
| 519 | } |
| 520 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 521 | } // namespace cricket |