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