1 /* 2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. 3 * 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. 9 */ 10 11 #include "webrtc/base/ratetracker.h" 12 #include "webrtc/base/timeutils.h" 13 14 namespace rtc { 15 RateTracker()16RateTracker::RateTracker() 17 : total_units_(0), units_second_(0), 18 last_units_second_time_(static_cast<uint32>(-1)), 19 last_units_second_calc_(0) { 20 } 21 total_units() const22size_t RateTracker::total_units() const { 23 return total_units_; 24 } 25 units_second()26size_t RateTracker::units_second() { 27 // Snapshot units / second calculator. Determine how many seconds have 28 // elapsed since our last reference point. If over 1 second, establish 29 // a new reference point that is an integer number of seconds since the 30 // last one, and compute the units over that interval. 31 uint32 current_time = Time(); 32 if (last_units_second_time_ != static_cast<uint32>(-1)) { 33 int delta = rtc::TimeDiff(current_time, last_units_second_time_); 34 if (delta >= 1000) { 35 int fraction_time = delta % 1000; 36 int seconds = delta / 1000; 37 int fraction_units = 38 static_cast<int>(total_units_ - last_units_second_calc_) * 39 fraction_time / delta; 40 // Compute "units received during the interval" / "seconds in interval" 41 units_second_ = 42 (total_units_ - last_units_second_calc_ - fraction_units) / seconds; 43 last_units_second_time_ = current_time - fraction_time; 44 last_units_second_calc_ = total_units_ - fraction_units; 45 } 46 } 47 if (last_units_second_time_ == static_cast<uint32>(-1)) { 48 last_units_second_time_ = current_time; 49 last_units_second_calc_ = total_units_; 50 } 51 52 return units_second_; 53 } 54 Update(size_t units)55void RateTracker::Update(size_t units) { 56 total_units_ += units; 57 } 58 Time() const59uint32 RateTracker::Time() const { 60 return rtc::Time(); 61 } 62 63 } // namespace rtc 64