blob: 06699333b23c8ffa2a3652347abb28abdf74bb14 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
kjellander1afca732016-02-07 20:46:45 -08002 * Copyright (c) 2011 The WebRTC project authors. All Rights Reserved.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003 *
kjellander1afca732016-02-07 20:46:45 -08004 * 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.org28e20752013-07-10 00:45:36 +00009 */
10
Steve Anton10542f22019-01-11 09:11:00 -080011#include "media/base/rtp_utils.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000012
Yves Gerey3e707812018-11-28 16:47:49 +010013#include <string.h>
14#include <vector>
15
Sergey Ulanovdc305db2016-01-14 17:14:54 -080016// PacketTimeUpdateParams is defined in asyncpacketsocket.h.
17// TODO(sergeyu): Find more appropriate place for PacketTimeUpdateParams.
Steve Anton10542f22019-01-11 09:11:00 -080018#include "media/base/turn_utils.h"
19#include "rtc_base/async_packet_socket.h"
20#include "rtc_base/byte_order.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020021#include "rtc_base/checks.h"
Steve Anton10542f22019-01-11 09:11:00 -080022#include "rtc_base/message_digest.h"
Sergey Ulanovdc305db2016-01-14 17:14:54 -080023
henrike@webrtc.org28e20752013-07-10 00:45:36 +000024namespace cricket {
25
pkasting@chromium.org0e81fdf2015-02-02 23:54:03 +000026static const uint8_t kRtpVersion = 2;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000027static const size_t kRtpFlagsOffset = 0;
28static const size_t kRtpPayloadTypeOffset = 1;
29static const size_t kRtpSeqNumOffset = 2;
30static const size_t kRtpTimestampOffset = 4;
31static const size_t kRtpSsrcOffset = 8;
32static const size_t kRtcpPayloadTypeOffset = 1;
Sergey Ulanovdc305db2016-01-14 17:14:54 -080033static const size_t kRtpExtensionHeaderLen = 4;
34static const size_t kAbsSendTimeExtensionLen = 3;
35static const size_t kOneByteExtensionHeaderLen = 1;
36
37namespace {
38
39// Fake auth tag written by the sender when external authentication is enabled.
40// HMAC in packet will be compared against this value before updating packet
41// with actual HMAC value.
Yves Gerey665174f2018-06-19 15:03:05 +020042static const uint8_t kFakeAuthTag[10] = {0xba, 0xdd, 0xba, 0xdd, 0xba,
43 0xdd, 0xba, 0xdd, 0xba, 0xdd};
Sergey Ulanovdc305db2016-01-14 17:14:54 -080044
45void UpdateAbsSendTimeExtensionValue(uint8_t* extension_data,
46 size_t length,
47 uint64_t time_us) {
48 // Absolute send time in RTP streams.
49 //
50 // The absolute send time is signaled to the receiver in-band using the
51 // general mechanism for RTP header extensions [RFC5285]. The payload
52 // of this extension (the transmitted value) is a 24-bit unsigned integer
53 // containing the sender's current time in seconds as a fixed point number
54 // with 18 bits fractional part.
55 //
56 // The form of the absolute send time extension block:
57 //
58 // 0 1 2 3
59 // 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
60 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
61 // | ID | len=2 | absolute send time |
62 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
63 if (length != kAbsSendTimeExtensionLen) {
64 RTC_NOTREACHED();
65 return;
66 }
67
68 // Convert microseconds to a 6.18 fixed point value in seconds.
69 uint32_t send_time = ((time_us << 18) / 1000000) & 0x00FFFFFF;
70 extension_data[0] = static_cast<uint8_t>(send_time >> 16);
71 extension_data[1] = static_cast<uint8_t>(send_time >> 8);
72 extension_data[2] = static_cast<uint8_t>(send_time);
73}
74
75// Assumes |length| is actual packet length + tag length. Updates HMAC at end of
76// the RTP packet.
77void UpdateRtpAuthTag(uint8_t* rtp,
78 size_t length,
79 const rtc::PacketTimeUpdateParams& packet_time_params) {
80 // If there is no key, return.
81 if (packet_time_params.srtp_auth_key.empty()) {
82 return;
83 }
84
85 size_t tag_length = packet_time_params.srtp_auth_tag_len;
86
87 // ROC (rollover counter) is at the beginning of the auth tag.
88 const size_t kRocLength = 4;
89 if (tag_length < kRocLength || tag_length > length) {
90 RTC_NOTREACHED();
91 return;
92 }
93
94 uint8_t* auth_tag = rtp + (length - tag_length);
95
96 // We should have a fake HMAC value @ auth_tag.
97 RTC_DCHECK_EQ(0, memcmp(auth_tag, kFakeAuthTag, tag_length));
98
99 // Copy ROC after end of rtp packet.
100 memcpy(auth_tag, &packet_time_params.srtp_packet_index, kRocLength);
101 // Authentication of a RTP packet will have RTP packet + ROC size.
102 size_t auth_required_length = length - tag_length + kRocLength;
103
104 uint8_t output[64];
Yves Gerey665174f2018-06-19 15:03:05 +0200105 size_t result =
106 rtc::ComputeHmac(rtc::DIGEST_SHA_1, &packet_time_params.srtp_auth_key[0],
107 packet_time_params.srtp_auth_key.size(), rtp,
108 auth_required_length, output, sizeof(output));
Sergey Ulanovdc305db2016-01-14 17:14:54 -0800109
110 if (result < tag_length) {
111 RTC_NOTREACHED();
112 return;
113 }
114
115 // Copy HMAC from output to packet. This is required as auth tag length
116 // may not be equal to the actual HMAC length.
117 memcpy(auth_tag, output, tag_length);
118}
119
Steve Antone78bcb92017-10-31 09:53:08 -0700120} // namespace
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000121
122bool GetUint8(const void* data, size_t offset, int* value) {
123 if (!data || !value) {
124 return false;
125 }
Peter Boström0c4e06b2015-10-07 12:23:21 +0200126 *value = *(static_cast<const uint8_t*>(data) + offset);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000127 return true;
128}
129
130bool GetUint16(const void* data, size_t offset, int* value) {
131 if (!data || !value) {
132 return false;
133 }
134 *value = static_cast<int>(
Peter Boström0c4e06b2015-10-07 12:23:21 +0200135 rtc::GetBE16(static_cast<const uint8_t*>(data) + offset));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000136 return true;
137}
138
Peter Boström0c4e06b2015-10-07 12:23:21 +0200139bool GetUint32(const void* data, size_t offset, uint32_t* value) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000140 if (!data || !value) {
141 return false;
142 }
Peter Boström0c4e06b2015-10-07 12:23:21 +0200143 *value = rtc::GetBE32(static_cast<const uint8_t*>(data) + offset);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000144 return true;
145}
146
pkasting@chromium.org0e81fdf2015-02-02 23:54:03 +0000147bool SetUint8(void* data, size_t offset, uint8_t value) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000148 if (!data) {
149 return false;
150 }
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000151 rtc::Set8(data, offset, value);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000152 return true;
153}
154
pkasting@chromium.org0e81fdf2015-02-02 23:54:03 +0000155bool SetUint16(void* data, size_t offset, uint16_t value) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000156 if (!data) {
157 return false;
158 }
Peter Boström0c4e06b2015-10-07 12:23:21 +0200159 rtc::SetBE16(static_cast<uint8_t*>(data) + offset, value);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000160 return true;
161}
162
Peter Boström0c4e06b2015-10-07 12:23:21 +0200163bool SetUint32(void* data, size_t offset, uint32_t value) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000164 if (!data) {
165 return false;
166 }
Peter Boström0c4e06b2015-10-07 12:23:21 +0200167 rtc::SetBE32(static_cast<uint8_t*>(data) + offset, value);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000168 return true;
169}
170
171bool GetRtpFlags(const void* data, size_t len, int* value) {
172 if (len < kMinRtpPacketLen) {
173 return false;
174 }
175 return GetUint8(data, kRtpFlagsOffset, value);
176}
177
178bool GetRtpPayloadType(const void* data, size_t len, int* value) {
179 if (len < kMinRtpPacketLen) {
180 return false;
181 }
182 if (!GetUint8(data, kRtpPayloadTypeOffset, value)) {
183 return false;
184 }
185 *value &= 0x7F;
186 return true;
187}
188
189bool GetRtpSeqNum(const void* data, size_t len, int* value) {
190 if (len < kMinRtpPacketLen) {
191 return false;
192 }
193 return GetUint16(data, kRtpSeqNumOffset, value);
194}
195
Peter Boström0c4e06b2015-10-07 12:23:21 +0200196bool GetRtpTimestamp(const void* data, size_t len, uint32_t* value) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000197 if (len < kMinRtpPacketLen) {
198 return false;
199 }
200 return GetUint32(data, kRtpTimestampOffset, value);
201}
202
Peter Boström0c4e06b2015-10-07 12:23:21 +0200203bool GetRtpSsrc(const void* data, size_t len, uint32_t* value) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000204 if (len < kMinRtpPacketLen) {
205 return false;
206 }
207 return GetUint32(data, kRtpSsrcOffset, value);
208}
209
210bool GetRtpHeaderLen(const void* data, size_t len, size_t* value) {
Yves Gerey665174f2018-06-19 15:03:05 +0200211 if (!data || len < kMinRtpPacketLen || !value)
212 return false;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200213 const uint8_t* header = static_cast<const uint8_t*>(data);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000214 // Get base header size + length of CSRCs (not counting extension yet).
Peter Boström0c4e06b2015-10-07 12:23:21 +0200215 size_t header_size = kMinRtpPacketLen + (header[0] & 0xF) * sizeof(uint32_t);
Yves Gerey665174f2018-06-19 15:03:05 +0200216 if (len < header_size)
217 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000218 // If there's an extension, read and add in the extension size.
219 if (header[0] & 0x10) {
Peter Boström0c4e06b2015-10-07 12:23:21 +0200220 if (len < header_size + sizeof(uint32_t))
221 return false;
222 header_size +=
223 ((rtc::GetBE16(header + header_size + 2) + 1) * sizeof(uint32_t));
Yves Gerey665174f2018-06-19 15:03:05 +0200224 if (len < header_size)
225 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000226 }
227 *value = header_size;
228 return true;
229}
230
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000231bool GetRtpHeader(const void* data, size_t len, RtpHeader* header) {
232 return (GetRtpPayloadType(data, len, &(header->payload_type)) &&
233 GetRtpSeqNum(data, len, &(header->seq_num)) &&
234 GetRtpTimestamp(data, len, &(header->timestamp)) &&
235 GetRtpSsrc(data, len, &(header->ssrc)));
236}
237
238bool GetRtcpType(const void* data, size_t len, int* value) {
239 if (len < kMinRtcpPacketLen) {
240 return false;
241 }
242 return GetUint8(data, kRtcpPayloadTypeOffset, value);
243}
244
245// This method returns SSRC first of RTCP packet, except if packet is SDES.
246// TODO(mallinath) - Fully implement RFC 5506. This standard doesn't restrict
247// to send non-compound packets only to feedback messages.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200248bool GetRtcpSsrc(const void* data, size_t len, uint32_t* value) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000249 // Packet should be at least of 8 bytes, to get SSRC from a RTCP packet.
Yves Gerey665174f2018-06-19 15:03:05 +0200250 if (!data || len < kMinRtcpPacketLen + 4 || !value)
251 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000252 int pl_type;
Yves Gerey665174f2018-06-19 15:03:05 +0200253 if (!GetRtcpType(data, len, &pl_type))
254 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000255 // SDES packet parsing is not supported.
Yves Gerey665174f2018-06-19 15:03:05 +0200256 if (pl_type == kRtcpTypeSDES)
257 return false;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200258 *value = rtc::GetBE32(static_cast<const uint8_t*>(data) + 4);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000259 return true;
260}
261
Peter Boström0c4e06b2015-10-07 12:23:21 +0200262bool SetRtpSsrc(void* data, size_t len, uint32_t value) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000263 return SetUint32(data, kRtpSsrcOffset, value);
264}
265
266// Assumes version 2, no padding, no extensions, no csrcs.
267bool SetRtpHeader(void* data, size_t len, const RtpHeader& header) {
Yves Gerey665174f2018-06-19 15:03:05 +0200268 if (!IsValidRtpPayloadType(header.payload_type) || header.seq_num < 0 ||
269 header.seq_num > static_cast<int>(UINT16_MAX)) {
pkasting@chromium.org0e81fdf2015-02-02 23:54:03 +0000270 return false;
271 }
272 return (SetUint8(data, kRtpFlagsOffset, kRtpVersion << 6) &&
273 SetUint8(data, kRtpPayloadTypeOffset, header.payload_type & 0x7F) &&
274 SetUint16(data, kRtpSeqNumOffset,
275 static_cast<uint16_t>(header.seq_num)) &&
276 SetUint32(data, kRtpTimestampOffset, header.timestamp) &&
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000277 SetRtpSsrc(data, len, header.ssrc));
278}
279
Amit Hilbuchedd20542019-03-18 12:33:43 -0700280static bool HasCorrectRtpVersion(rtc::ArrayView<const char> packet) {
281 return reinterpret_cast<const uint8_t*>(packet.data())[0] >> 6 == kRtpVersion;
282}
buildbot@webrtc.org1ef789d2014-06-19 23:54:12 +0000283
Amit Hilbuchedd20542019-03-18 12:33:43 -0700284bool IsRtpPacket(rtc::ArrayView<const char> packet) {
285 return packet.size() >= kMinRtpPacketLen && HasCorrectRtpVersion(packet);
buildbot@webrtc.org1ef789d2014-06-19 23:54:12 +0000286}
287
Zhi Huang365381f2018-04-13 16:44:34 -0700288// Check the RTP payload type. If 63 < payload type < 96, it's RTCP.
289// For additional details, see http://tools.ietf.org/html/rfc5761.
Amit Hilbuchedd20542019-03-18 12:33:43 -0700290bool IsRtcpPacket(rtc::ArrayView<const char> packet) {
291 if (packet.size() < kMinRtcpPacketLen || !HasCorrectRtpVersion(packet)) {
Zhi Huang365381f2018-04-13 16:44:34 -0700292 return false;
293 }
Piotr (Peter) Slatala042bb002019-01-30 14:57:12 -0800294
Amit Hilbuchedd20542019-03-18 12:33:43 -0700295 char pt = packet[1] & 0x7F;
Zhi Huang365381f2018-04-13 16:44:34 -0700296 return (63 < pt) && (pt < 96);
297}
298
pkasting@chromium.orge9facf82015-02-17 20:36:28 +0000299bool IsValidRtpPayloadType(int payload_type) {
300 return payload_type >= 0 && payload_type <= 127;
301}
302
Amit Hilbuchedd20542019-03-18 12:33:43 -0700303bool IsValidRtpPacketSize(RtpPacketType packet_type, size_t size) {
304 // TODO(webrtc:10418): uncomment when relands.
305 // RTC_DCHECK_NE(RtpPacketType::kUnknown, packet_type);
306 size_t min_packet_length = packet_type == RtpPacketType::kRtcp
307 ? kMinRtcpPacketLen
308 : kMinRtpPacketLen;
309 return size >= min_packet_length && size <= kMaxRtpPacketLen;
zstein3dcf0e92017-06-01 13:22:42 -0700310}
311
Amit Hilbuchedd20542019-03-18 12:33:43 -0700312absl::string_view RtpPacketTypeToString(RtpPacketType packet_type) {
313 switch (packet_type) {
314 case RtpPacketType::kRtp:
315 return "RTP";
316 case RtpPacketType::kRtcp:
317 return "RTCP";
318 case RtpPacketType::kUnknown:
319 return "Unknown";
320 }
321}
322
323RtpPacketType InferRtpPacketType(rtc::ArrayView<const char> packet) {
324 // RTCP packets are RTP packets so must check that first.
325 if (IsRtcpPacket(packet)) {
326 return RtpPacketType::kRtcp;
327 }
328 if (IsRtpPacket(packet)) {
329 return RtpPacketType::kRtp;
330 }
331 return RtpPacketType::kUnknown;
zstein3dcf0e92017-06-01 13:22:42 -0700332}
333
Sergey Ulanovdc305db2016-01-14 17:14:54 -0800334bool ValidateRtpHeader(const uint8_t* rtp,
335 size_t length,
336 size_t* header_length) {
337 if (header_length) {
338 *header_length = 0;
339 }
340
341 if (length < kMinRtpPacketLen) {
342 return false;
343 }
344
345 size_t cc_count = rtp[0] & 0x0F;
346 size_t header_length_without_extension = kMinRtpPacketLen + 4 * cc_count;
347 if (header_length_without_extension > length) {
348 return false;
349 }
350
351 // If extension bit is not set, we are done with header processing, as input
352 // length is verified above.
353 if (!(rtp[0] & 0x10)) {
354 if (header_length)
355 *header_length = header_length_without_extension;
356
357 return true;
358 }
359
360 rtp += header_length_without_extension;
361
362 if (header_length_without_extension + kRtpExtensionHeaderLen > length) {
363 return false;
364 }
365
366 // Getting extension profile length.
367 // Length is in 32 bit words.
368 uint16_t extension_length_in_32bits = rtc::GetBE16(rtp + 2);
369 size_t extension_length = extension_length_in_32bits * 4;
370
371 size_t rtp_header_length = extension_length +
372 header_length_without_extension +
373 kRtpExtensionHeaderLen;
374
375 // Verify input length against total header size.
376 if (rtp_header_length > length) {
377 return false;
378 }
379
380 if (header_length) {
381 *header_length = rtp_header_length;
382 }
383 return true;
384}
385
386// ValidateRtpHeader() must be called before this method to make sure, we have
387// a sane rtp packet.
388bool UpdateRtpAbsSendTimeExtension(uint8_t* rtp,
389 size_t length,
390 int extension_id,
391 uint64_t time_us) {
392 // 0 1 2 3
393 // 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
394 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
395 // |V=2|P|X| CC |M| PT | sequence number |
396 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
397 // | timestamp |
398 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
399 // | synchronization source (SSRC) identifier |
400 // +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
401 // | contributing source (CSRC) identifiers |
402 // | .... |
403 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
404
405 // Return if extension bit is not set.
406 if (!(rtp[0] & 0x10)) {
407 return true;
408 }
409
410 size_t cc_count = rtp[0] & 0x0F;
411 size_t header_length_without_extension = kMinRtpPacketLen + 4 * cc_count;
412
413 rtp += header_length_without_extension;
414
415 // Getting extension profile ID and length.
416 uint16_t profile_id = rtc::GetBE16(rtp);
417 // Length is in 32 bit words.
418 uint16_t extension_length_in_32bits = rtc::GetBE16(rtp + 2);
419 size_t extension_length = extension_length_in_32bits * 4;
420
421 rtp += kRtpExtensionHeaderLen; // Moving past extension header.
422
423 bool found = false;
424 // WebRTC is using one byte header extension.
425 // TODO(mallinath) - Handle two byte header extension.
426 if (profile_id == 0xBEDE) { // OneByte extension header
427 // 0
428 // 0 1 2 3 4 5 6 7
429 // +-+-+-+-+-+-+-+-+
430 // | ID |length |
431 // +-+-+-+-+-+-+-+-+
432
433 // 0 1 2 3
434 // 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
435 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
436 // | 0xBE | 0xDE | length=3 |
437 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
438 // | ID | L=0 | data | ID | L=1 | data...
439 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
440 // ...data | 0 (pad) | 0 (pad) | ID | L=3 |
441 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
442 // | data |
443 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
444 const uint8_t* extension_start = rtp;
445 const uint8_t* extension_end = extension_start + extension_length;
446
447 while (rtp < extension_end) {
448 const int id = (*rtp & 0xF0) >> 4;
449 const size_t length = (*rtp & 0x0F) + 1;
450 if (rtp + kOneByteExtensionHeaderLen + length > extension_end) {
451 return false;
452 }
453 // The 4-bit length is the number minus one of data bytes of this header
454 // extension element following the one-byte header.
455 if (id == extension_id) {
456 UpdateAbsSendTimeExtensionValue(rtp + kOneByteExtensionHeaderLen,
457 length, time_us);
458 found = true;
459 break;
460 }
461 rtp += kOneByteExtensionHeaderLen + length;
462 // Counting padding bytes.
463 while ((rtp < extension_end) && (*rtp == 0)) {
464 ++rtp;
465 }
466 }
467 }
468 return found;
469}
470
471bool ApplyPacketOptions(uint8_t* data,
472 size_t length,
473 const rtc::PacketTimeUpdateParams& packet_time_params,
474 uint64_t time_us) {
475 RTC_DCHECK(data);
476 RTC_DCHECK(length);
477
478 // if there is no valid |rtp_sendtime_extension_id| and |srtp_auth_key| in
479 // PacketOptions, nothing to be updated in this packet.
480 if (packet_time_params.rtp_sendtime_extension_id == -1 &&
481 packet_time_params.srtp_auth_key.empty()) {
482 return true;
483 }
484
485 // If there is a srtp auth key present then the packet must be an RTP packet.
486 // RTP packet may have been wrapped in a TURN Channel Data or TURN send
487 // indication.
488 size_t rtp_start_pos;
489 size_t rtp_length;
490 if (!UnwrapTurnPacket(data, length, &rtp_start_pos, &rtp_length)) {
491 RTC_NOTREACHED();
492 return false;
493 }
494
495 // Making sure we have a valid RTP packet at the end.
Amit Hilbuchedd20542019-03-18 12:33:43 -0700496 auto packet = rtc::MakeArrayView(
497 reinterpret_cast<const char*>(data + rtp_start_pos), rtp_length);
498 if (!IsRtpPacket(packet) ||
Sergey Ulanovdc305db2016-01-14 17:14:54 -0800499 !ValidateRtpHeader(data + rtp_start_pos, rtp_length, nullptr)) {
500 RTC_NOTREACHED();
501 return false;
502 }
503
504 uint8_t* start = data + rtp_start_pos;
505 // If packet option has non default value (-1) for sendtime extension id,
506 // then we should parse the rtp packet to update the timestamp. Otherwise
507 // just calculate HMAC and update packet with it.
508 if (packet_time_params.rtp_sendtime_extension_id != -1) {
509 UpdateRtpAbsSendTimeExtension(start, rtp_length,
510 packet_time_params.rtp_sendtime_extension_id,
511 time_us);
512 }
513
514 UpdateRtpAuthTag(start, rtp_length, packet_time_params);
515 return true;
516}
517
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000518} // namespace cricket