Add ICU message format support
[chromium-blink-merge.git] / content / browser / renderer_host / p2p / socket_host.cc
blobf2e76c3716354070daf1228228631e6d86e1fb2e
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "content/browser/renderer_host/p2p/socket_host.h"
7 #include "base/metrics/histogram.h"
8 #include "base/sys_byteorder.h"
9 #include "content/browser/renderer_host/p2p/socket_host_tcp.h"
10 #include "content/browser/renderer_host/p2p/socket_host_tcp_server.h"
11 #include "content/browser/renderer_host/p2p/socket_host_udp.h"
12 #include "content/browser/renderer_host/render_process_host_impl.h"
13 #include "content/public/browser/browser_thread.h"
14 #include "crypto/hmac.h"
15 #include "third_party/webrtc/base/asyncpacketsocket.h"
16 #include "third_party/webrtc/base/byteorder.h"
17 #include "third_party/webrtc/base/messagedigest.h"
18 #include "third_party/webrtc/p2p/base/stun.h"
20 namespace {
22 const uint32 kStunMagicCookie = 0x2112A442;
23 const size_t kMinRtpHeaderLength = 12;
24 const size_t kMinRtcpHeaderLength = 8;
25 const size_t kRtpExtensionHeaderLength = 4;
26 const size_t kDtlsRecordHeaderLength = 13;
27 const size_t kTurnChannelHeaderLength = 4;
28 const size_t kAbsSendTimeExtensionLength = 3;
29 const size_t kOneByteHeaderLength = 1;
30 const size_t kMaxRtpPacketLength = 2048;
32 // Fake auth tag written by the render process if external authentication is
33 // enabled. HMAC in packet will be compared against this value before updating
34 // packet with actual HMAC value.
35 static const unsigned char kFakeAuthTag[10] = {
36 0xba, 0xdd, 0xba, 0xdd, 0xba, 0xdd, 0xba, 0xdd, 0xba, 0xdd
39 bool IsTurnChannelData(const char* data, size_t length) {
40 return length >= kTurnChannelHeaderLength && ((*data & 0xC0) == 0x40);
43 bool IsDtlsPacket(const char* data, size_t length) {
44 const uint8* u = reinterpret_cast<const uint8*>(data);
45 return (length >= kDtlsRecordHeaderLength && (u[0] > 19 && u[0] < 64));
48 bool IsRtcpPacket(const char* data, size_t length) {
49 if (length < kMinRtcpHeaderLength) {
50 return false;
53 int type = (static_cast<uint8>(data[1]) & 0x7F);
54 return (type >= 64 && type < 96);
57 bool IsTurnSendIndicationPacket(const char* data, size_t length) {
58 if (length < content::P2PSocketHost::kStunHeaderSize) {
59 return false;
62 uint16 type = rtc::GetBE16(data);
63 return (type == cricket::TURN_SEND_INDICATION);
66 bool IsRtpPacket(const char* data, size_t length) {
67 return (length >= kMinRtpHeaderLength) && ((*data & 0xC0) == 0x80);
70 // Verifies rtp header and message length.
71 bool ValidateRtpHeader(const char* rtp, size_t length, size_t* header_length) {
72 if (header_length) {
73 *header_length = 0;
76 if (length < kMinRtpHeaderLength) {
77 return false;
80 size_t cc_count = rtp[0] & 0x0F;
81 size_t header_length_without_extension = kMinRtpHeaderLength + 4 * cc_count;
82 if (header_length_without_extension > length) {
83 return false;
86 // If extension bit is not set, we are done with header processing, as input
87 // length is verified above.
88 if (!(rtp[0] & 0x10)) {
89 if (header_length)
90 *header_length = header_length_without_extension;
92 return true;
95 rtp += header_length_without_extension;
97 if (header_length_without_extension + kRtpExtensionHeaderLength > length) {
98 return false;
101 // Getting extension profile length.
102 // Length is in 32 bit words.
103 uint16 extension_length_in_32bits = rtc::GetBE16(rtp + 2);
104 size_t extension_length = extension_length_in_32bits * 4;
106 size_t rtp_header_length = extension_length +
107 header_length_without_extension +
108 kRtpExtensionHeaderLength;
110 // Verify input length against total header size.
111 if (rtp_header_length > length) {
112 return false;
115 if (header_length) {
116 *header_length = rtp_header_length;
118 return true;
121 void UpdateAbsSendTimeExtensionValue(char* extension_data,
122 size_t length,
123 uint32 abs_send_time) {
124 // Absolute send time in RTP streams.
126 // The absolute send time is signaled to the receiver in-band using the
127 // general mechanism for RTP header extensions [RFC5285]. The payload
128 // of this extension (the transmitted value) is a 24-bit unsigned integer
129 // containing the sender's current time in seconds as a fixed point number
130 // with 18 bits fractional part.
132 // The form of the absolute send time extension block:
134 // 0 1 2 3
135 // 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
136 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
137 // | ID | len=2 | absolute send time |
138 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
139 if (length != kAbsSendTimeExtensionLength) {
140 NOTREACHED();
141 return;
144 // Now() has resolution ~1-15ms
145 uint32 now_second = abs_send_time;
146 if (!now_second) {
147 uint64 now_us =
148 (base::TimeTicks::Now() - base::TimeTicks()).InMicroseconds();
149 // Convert second to 24-bit unsigned with 18 bit fractional part
150 now_second =
151 ((now_us << 18) / base::Time::kMicrosecondsPerSecond) & 0x00FFFFFF;
153 // TODO(mallinath) - Add SetBE24 to byteorder.h in libjingle.
154 extension_data[0] = static_cast<uint8>(now_second >> 16);
155 extension_data[1] = static_cast<uint8>(now_second >> 8);
156 extension_data[2] = static_cast<uint8>(now_second);
159 // Assumes |length| is actual packet length + tag length. Updates HMAC at end of
160 // the RTP packet.
161 void UpdateRtpAuthTag(char* rtp,
162 size_t length,
163 const rtc::PacketOptions& options) {
164 // If there is no key, return.
165 if (options.packet_time_params.srtp_auth_key.empty()) {
166 return;
169 size_t tag_length = options.packet_time_params.srtp_auth_tag_len;
171 // ROC (rollover counter) is at the beginning of the auth tag.
172 const size_t kRocLength = 4;
173 if (tag_length < kRocLength || tag_length > length) {
174 NOTREACHED();
175 return;
178 crypto::HMAC hmac(crypto::HMAC::SHA1);
179 if (!hmac.Init(reinterpret_cast<const unsigned char*>(
180 &options.packet_time_params.srtp_auth_key[0]),
181 options.packet_time_params.srtp_auth_key.size())) {
182 NOTREACHED();
183 return;
186 if (tag_length > hmac.DigestLength()) {
187 NOTREACHED();
188 return;
191 char* auth_tag = rtp + (length - tag_length);
193 // We should have a fake HMAC value @ auth_tag.
194 DCHECK_EQ(0, memcmp(auth_tag, kFakeAuthTag, tag_length));
196 // Copy ROC after end of rtp packet.
197 memcpy(auth_tag, &options.packet_time_params.srtp_packet_index, kRocLength);
198 // Authentication of a RTP packet will have RTP packet + ROC size.
199 int auth_required_length = length - tag_length + kRocLength;
201 unsigned char output[64];
202 if (!hmac.Sign(base::StringPiece(rtp, auth_required_length),
203 output, sizeof(output))) {
204 NOTREACHED();
205 return;
207 // Copy HMAC from output to packet. This is required as auth tag length
208 // may not be equal to the actual HMAC length.
209 memcpy(auth_tag, output, tag_length);
212 } // namespace
214 namespace content {
216 namespace packet_processing_helpers {
218 bool ApplyPacketOptions(char* data,
219 size_t length,
220 const rtc::PacketOptions& options,
221 uint32 abs_send_time) {
222 DCHECK(data != NULL);
223 DCHECK(length > 0);
224 // if there is no valid |rtp_sendtime_extension_id| and |srtp_auth_key| in
225 // PacketOptions, nothing to be updated in this packet.
226 if (options.packet_time_params.rtp_sendtime_extension_id == -1 &&
227 options.packet_time_params.srtp_auth_key.empty()) {
228 return true;
231 DCHECK(!IsDtlsPacket(data, length));
232 DCHECK(!IsRtcpPacket(data, length));
234 // If there is a srtp auth key present then packet must be a RTP packet.
235 // RTP packet may have been wrapped in a TURN Channel Data or
236 // TURN send indication.
237 size_t rtp_start_pos;
238 size_t rtp_length;
239 if (!GetRtpPacketStartPositionAndLength(
240 data, length, &rtp_start_pos, &rtp_length)) {
241 // This method should never return false.
242 NOTREACHED();
243 return false;
246 // Skip to rtp packet.
247 char* start = data + rtp_start_pos;
248 // If packet option has non default value (-1) for sendtime extension id,
249 // then we should parse the rtp packet to update the timestamp. Otherwise
250 // just calculate HMAC and update packet with it.
251 if (options.packet_time_params.rtp_sendtime_extension_id != -1) {
252 UpdateRtpAbsSendTimeExtension(
253 start,
254 rtp_length,
255 options.packet_time_params.rtp_sendtime_extension_id,
256 abs_send_time);
259 UpdateRtpAuthTag(start, rtp_length, options);
260 return true;
263 bool GetRtpPacketStartPositionAndLength(const char* packet,
264 size_t length,
265 size_t* rtp_start_pos,
266 size_t* rtp_packet_length) {
267 if (length < kMinRtpHeaderLength || length > kMaxRtpPacketLength) {
268 return false;
271 size_t rtp_begin;
272 size_t rtp_length = 0;
273 if (IsTurnChannelData(packet, length)) {
274 // Turn Channel Message header format.
275 // 0 1 2 3
276 // 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
277 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
278 // | Channel Number | Length |
279 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
280 // | |
281 // / Application Data /
282 // / /
283 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
284 rtp_begin = kTurnChannelHeaderLength;
285 rtp_length = rtc::GetBE16(&packet[2]);
286 if (length < rtp_length + kTurnChannelHeaderLength) {
287 return false;
289 } else if (IsTurnSendIndicationPacket(packet, length)) {
290 // Validate STUN message length.
291 const size_t stun_message_length = rtc::GetBE16(&packet[2]);
292 if (stun_message_length + P2PSocketHost::kStunHeaderSize != length) {
293 return false;
296 // First skip mandatory stun header which is of 20 bytes.
297 rtp_begin = P2PSocketHost::kStunHeaderSize;
298 // Loop through STUN attributes until we find STUN DATA attribute.
299 bool data_attr_present = false;
300 while (rtp_begin < length) {
301 // Keep reading STUN attributes until we hit DATA attribute.
302 // Attribute will be a TLV structure.
303 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
304 // | Type | Length |
305 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
306 // | Value (variable) ....
307 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
308 // The value in the length field MUST contain the length of the Value
309 // part of the attribute, prior to padding, measured in bytes. Since
310 // STUN aligns attributes on 32-bit boundaries, attributes whose content
311 // is not a multiple of 4 bytes are padded with 1, 2, or 3 bytes of
312 // padding so that its value contains a multiple of 4 bytes. The
313 // padding bits are ignored, and may be any value.
314 uint16 attr_type, attr_length;
315 const int kAttrHeaderLength = sizeof(attr_type) + sizeof(attr_length);
317 if (length < rtp_begin + kAttrHeaderLength) {
318 return false;
321 // Getting attribute type and length.
322 attr_type = rtc::GetBE16(&packet[rtp_begin]);
323 attr_length = rtc::GetBE16(
324 &packet[rtp_begin + sizeof(attr_type)]);
326 rtp_begin += kAttrHeaderLength; // Skip STUN_DATA_ATTR header.
328 // Checking for bogus attribute length.
329 if (length < rtp_begin + attr_length) {
330 return false;
333 if (attr_type != cricket::STUN_ATTR_DATA) {
334 rtp_begin += attr_length;
335 if ((attr_length % 4) != 0) {
336 rtp_begin += (4 - (attr_length % 4));
338 continue;
341 data_attr_present = true;
342 rtp_length = attr_length;
344 // We found STUN_DATA_ATTR. We can skip parsing rest of the packet.
345 break;
348 if (!data_attr_present) {
349 // There is no data attribute present in the message. We can't do anything
350 // with the message.
351 return false;
354 } else {
355 // This is a raw RTP packet.
356 rtp_begin = 0;
357 rtp_length = length;
360 // Making sure we have a valid RTP packet at the end.
361 if (IsRtpPacket(packet + rtp_begin, rtp_length) &&
362 ValidateRtpHeader(packet + rtp_begin, rtp_length, NULL)) {
363 *rtp_start_pos = rtp_begin;
364 *rtp_packet_length = rtp_length;
365 return true;
367 return false;
370 // ValidateRtpHeader must be called before this method to make sure, we have
371 // a sane rtp packet.
372 bool UpdateRtpAbsSendTimeExtension(char* rtp,
373 size_t length,
374 int extension_id,
375 uint32 abs_send_time) {
376 // 0 1 2 3
377 // 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
378 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
379 // |V=2|P|X| CC |M| PT | sequence number |
380 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
381 // | timestamp |
382 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
383 // | synchronization source (SSRC) identifier |
384 // +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
385 // | contributing source (CSRC) identifiers |
386 // | .... |
387 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
389 // Return if extension bit is not set.
390 if (!(rtp[0] & 0x10)) {
391 return true;
394 size_t cc_count = rtp[0] & 0x0F;
395 size_t header_length_without_extension = kMinRtpHeaderLength + 4 * cc_count;
397 rtp += header_length_without_extension;
399 // Getting extension profile ID and length.
400 uint16 profile_id = rtc::GetBE16(rtp);
401 // Length is in 32 bit words.
402 uint16 extension_length_in_32bits = rtc::GetBE16(rtp + 2);
403 size_t extension_length = extension_length_in_32bits * 4;
405 rtp += kRtpExtensionHeaderLength; // Moving past extension header.
407 bool found = false;
408 // WebRTC is using one byte header extension.
409 // TODO(mallinath) - Handle two byte header extension.
410 if (profile_id == 0xBEDE) { // OneByte extension header
411 // 0
412 // 0 1 2 3 4 5 6 7
413 // +-+-+-+-+-+-+-+-+
414 // | ID |length |
415 // +-+-+-+-+-+-+-+-+
417 // 0 1 2 3
418 // 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
419 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
420 // | 0xBE | 0xDE | length=3 |
421 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
422 // | ID | L=0 | data | ID | L=1 | data...
423 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
424 // ...data | 0 (pad) | 0 (pad) | ID | L=3 |
425 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
426 // | data |
427 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
428 const char* extension_start = rtp;
429 const char* extension_end = extension_start + extension_length;
431 while (rtp < extension_end) {
432 const int id = (*rtp & 0xF0) >> 4;
433 const size_t length = (*rtp & 0x0F) + 1;
434 if (rtp + kOneByteHeaderLength + length > extension_end) {
435 return false;
437 // The 4-bit length is the number minus one of data bytes of this header
438 // extension element following the one-byte header.
439 if (id == extension_id) {
440 UpdateAbsSendTimeExtensionValue(
441 rtp + kOneByteHeaderLength, length, abs_send_time);
442 found = true;
443 break;
445 rtp += kOneByteHeaderLength + length;
446 // Counting padding bytes.
447 while ((rtp < extension_end) && (*rtp == 0)) {
448 ++rtp;
452 return found;
455 } // packet_processing_helpers
457 P2PSocketHost::P2PSocketHost(IPC::Sender* message_sender,
458 int socket_id,
459 ProtocolType protocol_type)
460 : message_sender_(message_sender),
461 id_(socket_id),
462 state_(STATE_UNINITIALIZED),
463 dump_incoming_rtp_packet_(false),
464 dump_outgoing_rtp_packet_(false),
465 protocol_type_(protocol_type),
466 send_packets_delayed_total_(0),
467 send_packets_total_(0),
468 send_bytes_delayed_max_(0),
469 send_bytes_delayed_cur_(0),
470 weak_ptr_factory_(this) {
473 P2PSocketHost::~P2PSocketHost() {
474 if (protocol_type_ == P2PSocketHost::UDP) {
475 UMA_HISTOGRAM_COUNTS_10000("WebRTC.SystemMaxConsecutiveBytesDelayed_UDP",
476 send_bytes_delayed_max_);
477 } else {
478 UMA_HISTOGRAM_COUNTS_10000("WebRTC.SystemMaxConsecutiveBytesDelayed_TCP",
479 send_bytes_delayed_max_);
482 if (send_packets_total_ > 0) {
483 int delay_rate = (send_packets_delayed_total_ * 100) / send_packets_total_;
484 if (protocol_type_ == P2PSocketHost::UDP) {
485 UMA_HISTOGRAM_PERCENTAGE("WebRTC.SystemPercentPacketsDelayed_UDP",
486 delay_rate);
487 } else {
488 UMA_HISTOGRAM_PERCENTAGE("WebRTC.SystemPercentPacketsDelayed_TCP",
489 delay_rate);
494 // Verifies that the packet |data| has a valid STUN header.
495 // static
496 bool P2PSocketHost::GetStunPacketType(
497 const char* data, int data_size, StunMessageType* type) {
499 if (data_size < kStunHeaderSize) {
500 return false;
503 uint32 cookie = base::NetToHost32(*reinterpret_cast<const uint32*>(data + 4));
504 if (cookie != kStunMagicCookie) {
505 return false;
508 uint16 length = base::NetToHost16(*reinterpret_cast<const uint16*>(data + 2));
509 if (length != data_size - kStunHeaderSize) {
510 return false;
513 int message_type = base::NetToHost16(*reinterpret_cast<const uint16*>(data));
515 // Verify that the type is known:
516 switch (message_type) {
517 case STUN_BINDING_REQUEST:
518 case STUN_BINDING_RESPONSE:
519 case STUN_BINDING_ERROR_RESPONSE:
520 case STUN_SHARED_SECRET_REQUEST:
521 case STUN_SHARED_SECRET_RESPONSE:
522 case STUN_SHARED_SECRET_ERROR_RESPONSE:
523 case STUN_ALLOCATE_REQUEST:
524 case STUN_ALLOCATE_RESPONSE:
525 case STUN_ALLOCATE_ERROR_RESPONSE:
526 case STUN_SEND_REQUEST:
527 case STUN_SEND_RESPONSE:
528 case STUN_SEND_ERROR_RESPONSE:
529 case STUN_DATA_INDICATION:
530 *type = static_cast<StunMessageType>(message_type);
531 return true;
533 default:
534 return false;
538 // static
539 bool P2PSocketHost::IsRequestOrResponse(StunMessageType type) {
540 return type == STUN_BINDING_REQUEST || type == STUN_BINDING_RESPONSE ||
541 type == STUN_ALLOCATE_REQUEST || type == STUN_ALLOCATE_RESPONSE;
544 // static
545 P2PSocketHost* P2PSocketHost::Create(IPC::Sender* message_sender,
546 int socket_id,
547 P2PSocketType type,
548 net::URLRequestContextGetter* url_context,
549 P2PMessageThrottler* throttler) {
550 switch (type) {
551 case P2P_SOCKET_UDP:
552 return new P2PSocketHostUdp(message_sender, socket_id, throttler);
553 case P2P_SOCKET_TCP_SERVER:
554 return new P2PSocketHostTcpServer(
555 message_sender, socket_id, P2P_SOCKET_TCP_CLIENT);
557 case P2P_SOCKET_STUN_TCP_SERVER:
558 return new P2PSocketHostTcpServer(
559 message_sender, socket_id, P2P_SOCKET_STUN_TCP_CLIENT);
561 case P2P_SOCKET_TCP_CLIENT:
562 case P2P_SOCKET_SSLTCP_CLIENT:
563 case P2P_SOCKET_TLS_CLIENT:
564 return new P2PSocketHostTcp(message_sender, socket_id, type, url_context);
566 case P2P_SOCKET_STUN_TCP_CLIENT:
567 case P2P_SOCKET_STUN_SSLTCP_CLIENT:
568 case P2P_SOCKET_STUN_TLS_CLIENT:
569 return new P2PSocketHostStunTcp(
570 message_sender, socket_id, type, url_context);
573 NOTREACHED();
574 return NULL;
577 void P2PSocketHost::StartRtpDump(
578 bool incoming,
579 bool outgoing,
580 const RenderProcessHost::WebRtcRtpPacketCallback& packet_callback) {
581 DCHECK_CURRENTLY_ON(BrowserThread::IO);
582 DCHECK(!packet_callback.is_null());
583 DCHECK(incoming || outgoing);
585 if (incoming) {
586 dump_incoming_rtp_packet_ = true;
589 if (outgoing) {
590 dump_outgoing_rtp_packet_ = true;
593 packet_dump_callback_ = packet_callback;
596 void P2PSocketHost::StopRtpDump(bool incoming, bool outgoing) {
597 DCHECK_CURRENTLY_ON(BrowserThread::IO);
598 DCHECK(incoming || outgoing);
600 if (incoming) {
601 dump_incoming_rtp_packet_ = false;
604 if (outgoing) {
605 dump_outgoing_rtp_packet_ = false;
608 if (!dump_incoming_rtp_packet_ && !dump_outgoing_rtp_packet_) {
609 packet_dump_callback_.Reset();
613 void P2PSocketHost::DumpRtpPacket(const char* packet,
614 size_t length,
615 bool incoming) {
616 if (IsDtlsPacket(packet, length) || IsRtcpPacket(packet, length)) {
617 return;
620 size_t rtp_packet_pos = 0;
621 size_t rtp_packet_length = length;
622 if (!packet_processing_helpers::GetRtpPacketStartPositionAndLength(
623 packet, length, &rtp_packet_pos, &rtp_packet_length)) {
624 return;
627 packet += rtp_packet_pos;
629 size_t header_length = 0;
630 bool valid = ValidateRtpHeader(packet, rtp_packet_length, &header_length);
631 if (!valid) {
632 DCHECK(false);
633 return;
636 scoped_ptr<uint8[]> header_buffer(new uint8[header_length]);
637 memcpy(header_buffer.get(), packet, header_length);
639 // Posts to the IO thread as the data members should be accessed on the IO
640 // thread only.
641 BrowserThread::PostTask(BrowserThread::IO,
642 FROM_HERE,
643 base::Bind(&P2PSocketHost::DumpRtpPacketOnIOThread,
644 weak_ptr_factory_.GetWeakPtr(),
645 Passed(&header_buffer),
646 header_length,
647 rtp_packet_length,
648 incoming));
651 void P2PSocketHost::DumpRtpPacketOnIOThread(scoped_ptr<uint8[]> packet_header,
652 size_t header_length,
653 size_t packet_length,
654 bool incoming) {
655 DCHECK_CURRENTLY_ON(BrowserThread::IO);
657 if ((incoming && !dump_incoming_rtp_packet_) ||
658 (!incoming && !dump_outgoing_rtp_packet_) ||
659 packet_dump_callback_.is_null()) {
660 return;
663 // |packet_dump_callback_| must be called on the UI thread.
664 BrowserThread::PostTask(BrowserThread::UI,
665 FROM_HERE,
666 base::Bind(packet_dump_callback_,
667 Passed(&packet_header),
668 header_length,
669 packet_length,
670 incoming));
673 void P2PSocketHost::IncrementDelayedPackets() {
674 send_packets_delayed_total_++;
677 void P2PSocketHost::IncrementTotalSentPackets() {
678 send_packets_total_++;
681 void P2PSocketHost::IncrementDelayedBytes(uint32 size) {
682 send_bytes_delayed_cur_ += size;
683 if (send_bytes_delayed_cur_ > send_bytes_delayed_max_) {
684 send_bytes_delayed_max_ = send_bytes_delayed_cur_;
688 void P2PSocketHost::DecrementDelayedBytes(uint32 size) {
689 send_bytes_delayed_cur_ -= size;
690 DCHECK_GE(send_bytes_delayed_cur_, 0);
693 } // namespace content