1 /*
2  *  Copyright (c) 2016 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 #ifndef WEBRTC_MODULES_CONGESTION_CONTROLLER_TRENDLINE_ESTIMATOR_H_
11 #define WEBRTC_MODULES_CONGESTION_CONTROLLER_TRENDLINE_ESTIMATOR_H_
12 
13 #include <stddef.h>
14 #include <stdint.h>
15 
16 #include <list>
17 #include <utility>
18 
19 #include "webrtc/base/constructormagic.h"
20 
21 namespace webrtc {
22 
23 class TrendlineEstimator {
24  public:
25   // |window_size| is the number of points required to compute a trend line.
26   // |smoothing_coef| controls how much we smooth out the delay before fitting
27   // the trend line. |threshold_gain| is used to scale the trendline slope for
28   // comparison to the old threshold. Once the old estimator has been removed
29   // (or the thresholds been merged into the estimators), we can just set the
30   // threshold instead of setting a gain.
31   TrendlineEstimator(size_t window_size,
32                      double smoothing_coef,
33                      double threshold_gain);
34   ~TrendlineEstimator();
35 
36   // Update the estimator with a new sample. The deltas should represent deltas
37   // between timestamp groups as defined by the InterArrival class.
38   void Update(double recv_delta_ms,
39               double send_delta_ms,
40               int64_t arrival_time_ms);
41 
42   // Returns the estimated trend k multiplied by some gain.
43   // 0 < k < 1   ->  the delay increases, queues are filling up
44   //   k == 0    ->  the delay does not change
45   //   k < 0     ->  the delay decreases, queues are being emptied
trendline_slope()46   double trendline_slope() const { return trendline_ * threshold_gain_; }
47 
48   // Returns the number of deltas which the current estimator state is based on.
num_of_deltas()49   unsigned int num_of_deltas() const { return num_of_deltas_; }
50 
51  private:
52   // Parameters.
53   const size_t window_size_;
54   const double smoothing_coef_;
55   const double threshold_gain_;
56   // Used by the existing threshold.
57   unsigned int num_of_deltas_;
58   // Keep the arrival times small by using the change from the first packet.
59   int64_t first_arrival_time_ms;
60   // Exponential backoff filtering.
61   double accumulated_delay_;
62   double smoothed_delay_;
63   // Linear least squares regression.
64   std::list<std::pair<double, double>> delay_hist_;
65   double trendline_;
66 
67   RTC_DISALLOW_COPY_AND_ASSIGN(TrendlineEstimator);
68 };
69 }  // namespace webrtc
70 
71 #endif  // WEBRTC_MODULES_CONGESTION_CONTROLLER_TRENDLINE_ESTIMATOR_H_
72