Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(161)

Unified Diff: remoting/base/leaky_bucket.cc

Issue 2381153002: Pace outgoing frames in frame scheduler to match target bitrate. (Closed)
Patch Set: Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: remoting/base/leaky_bucket.cc
diff --git a/remoting/base/leaky_bucket.cc b/remoting/base/leaky_bucket.cc
new file mode 100644
index 0000000000000000000000000000000000000000..72c15dc028bd43ba1e378034856db6781ce3e39f
--- /dev/null
+++ b/remoting/base/leaky_bucket.cc
@@ -0,0 +1,48 @@
+// Copyright 2016 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "remoting/base/leaky_bucket.h"
+
+#include "base/logging.h"
+
+namespace remoting {
+
+LeakyBucket::LeakyBucket(int depth, int rate)
+ : depth_(depth),
+ rate_(rate),
+ level_(0),
+ last_update_(base::TimeTicks::Now()) {
+ DCHECK_GE(depth_, 0);
+}
+
+LeakyBucket::~LeakyBucket() {}
+
+bool LeakyBucket::RefillOrSpill(int size, base::TimeTicks now) {
+ UpdateLevel(now);
+
+ int new_level = level_ + size;
+ if (depth_ > 0 && new_level > depth_)
+ return false;
+ level_ = new_level;
+ return true;
+}
+
+base::TimeTicks LeakyBucket::GetEmptyTime() {
+ return last_update_ + base::TimeDelta::FromSecondsD(level_ / rate_);
+}
+
+void LeakyBucket::UpdateRate(int new_rate, base::TimeTicks now) {
+ UpdateLevel(now);
+ rate_ = new_rate;
+}
+
+void LeakyBucket::UpdateLevel(base::TimeTicks now) {
+ level_ -= rate_ * (now - last_update_).InMicroseconds() /
+ base::TimeTicks::kMicrosecondsPerSecond;
+ if (level_ < 0)
+ level_ = 0;
+ last_update_ = now;
+}
+
+} // namespace remoting

Powered by Google App Engine
This is Rietveld 408576698