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