- add sources.
[platform/framework/web/crosswalk.git] / src / content / browser / renderer_host / p2p / socket_host_throttler.cc
1 // Copyright (c) 2013 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.
4
5 #include "content/browser/renderer_host/p2p/socket_host_throttler.h"
6 #include "third_party/libjingle/source/talk/base/ratelimiter.h"
7 #include "third_party/libjingle/source/talk/base/timing.h"
8
9 namespace content {
10
11 namespace  {
12
13 const int kMaxIceMessageBandwidth = 256 * 1024;
14
15 }  // namespace
16
17
18 P2PMessageThrottler::P2PMessageThrottler()
19     : timing_(new talk_base::Timing()),
20       rate_limiter_(new talk_base::RateLimiter(kMaxIceMessageBandwidth, 1.0)) {
21 }
22
23 P2PMessageThrottler::~P2PMessageThrottler() {
24 }
25
26 void P2PMessageThrottler::SetTiming(scoped_ptr<talk_base::Timing> timing) {
27   timing_ = timing.Pass();
28 }
29
30 void P2PMessageThrottler::SetSendIceBandwidth(int bandwidth_kbps) {
31   rate_limiter_.reset(new talk_base::RateLimiter(bandwidth_kbps, 1.0));
32 }
33
34 bool P2PMessageThrottler::DropNextPacket(size_t packet_len) {
35   double now = timing_->TimerNow();
36   if (!rate_limiter_->CanUse(packet_len, now)) {
37     // Exceeding the send rate, this packet should be dropped.
38     return true;
39   }
40
41   rate_limiter_->Use(packet_len, now);
42   return false;
43 }
44
45 }  // namespace content