• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright (c) 2013 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 "modules/remote_bitrate_estimator/overuse_estimator.h"
12 
13 #include <assert.h>
14 #include <math.h>
15 #include <string.h>
16 
17 #include <algorithm>
18 
19 #include "modules/remote_bitrate_estimator/include/bwe_defines.h"
20 #include "modules/remote_bitrate_estimator/test/bwe_test_logging.h"
21 #include "rtc_base/logging.h"
22 
23 namespace webrtc {
24 
25 enum { kMinFramePeriodHistoryLength = 60 };
26 enum { kDeltaCounterMax = 1000 };
27 
OveruseEstimator(const OverUseDetectorOptions & options)28 OveruseEstimator::OveruseEstimator(const OverUseDetectorOptions& options)
29     : options_(options),
30       num_of_deltas_(0),
31       slope_(options_.initial_slope),
32       offset_(options_.initial_offset),
33       prev_offset_(options_.initial_offset),
34       E_(),
35       process_noise_(),
36       avg_noise_(options_.initial_avg_noise),
37       var_noise_(options_.initial_var_noise),
38       ts_delta_hist_() {
39   memcpy(E_, options_.initial_e, sizeof(E_));
40   memcpy(process_noise_, options_.initial_process_noise,
41          sizeof(process_noise_));
42 }
43 
~OveruseEstimator()44 OveruseEstimator::~OveruseEstimator() {
45   ts_delta_hist_.clear();
46 }
47 
Update(int64_t t_delta,double ts_delta,int size_delta,BandwidthUsage current_hypothesis,int64_t now_ms)48 void OveruseEstimator::Update(int64_t t_delta,
49                               double ts_delta,
50                               int size_delta,
51                               BandwidthUsage current_hypothesis,
52                               int64_t now_ms) {
53   const double min_frame_period = UpdateMinFramePeriod(ts_delta);
54   const double t_ts_delta = t_delta - ts_delta;
55   BWE_TEST_LOGGING_PLOT(1, "dm_ms", now_ms, t_ts_delta);
56   double fs_delta = size_delta;
57 
58   ++num_of_deltas_;
59   if (num_of_deltas_ > kDeltaCounterMax) {
60     num_of_deltas_ = kDeltaCounterMax;
61   }
62 
63   // Update the Kalman filter.
64   E_[0][0] += process_noise_[0];
65   E_[1][1] += process_noise_[1];
66 
67   if ((current_hypothesis == BandwidthUsage::kBwOverusing &&
68        offset_ < prev_offset_) ||
69       (current_hypothesis == BandwidthUsage::kBwUnderusing &&
70        offset_ > prev_offset_)) {
71     E_[1][1] += 10 * process_noise_[1];
72   }
73 
74   const double h[2] = {fs_delta, 1.0};
75   const double Eh[2] = {E_[0][0] * h[0] + E_[0][1] * h[1],
76                         E_[1][0] * h[0] + E_[1][1] * h[1]};
77 
78   BWE_TEST_LOGGING_PLOT(1, "d_ms", now_ms, slope_ * h[0] - offset_);
79 
80   const double residual = t_ts_delta - slope_ * h[0] - offset_;
81 
82   const bool in_stable_state =
83       (current_hypothesis == BandwidthUsage::kBwNormal);
84   const double max_residual = 3.0 * sqrt(var_noise_);
85   // We try to filter out very late frames. For instance periodic key
86   // frames doesn't fit the Gaussian model well.
87   if (fabs(residual) < max_residual) {
88     UpdateNoiseEstimate(residual, min_frame_period, in_stable_state);
89   } else {
90     UpdateNoiseEstimate(residual < 0 ? -max_residual : max_residual,
91                         min_frame_period, in_stable_state);
92   }
93 
94   const double denom = var_noise_ + h[0] * Eh[0] + h[1] * Eh[1];
95 
96   const double K[2] = {Eh[0] / denom, Eh[1] / denom};
97 
98   const double IKh[2][2] = {{1.0 - K[0] * h[0], -K[0] * h[1]},
99                             {-K[1] * h[0], 1.0 - K[1] * h[1]}};
100   const double e00 = E_[0][0];
101   const double e01 = E_[0][1];
102 
103   // Update state.
104   E_[0][0] = e00 * IKh[0][0] + E_[1][0] * IKh[0][1];
105   E_[0][1] = e01 * IKh[0][0] + E_[1][1] * IKh[0][1];
106   E_[1][0] = e00 * IKh[1][0] + E_[1][0] * IKh[1][1];
107   E_[1][1] = e01 * IKh[1][0] + E_[1][1] * IKh[1][1];
108 
109   // The covariance matrix must be positive semi-definite.
110   bool positive_semi_definite =
111       E_[0][0] + E_[1][1] >= 0 &&
112       E_[0][0] * E_[1][1] - E_[0][1] * E_[1][0] >= 0 && E_[0][0] >= 0;
113   assert(positive_semi_definite);
114   if (!positive_semi_definite) {
115     RTC_LOG(LS_ERROR)
116         << "The over-use estimator's covariance matrix is no longer "
117            "semi-definite.";
118   }
119 
120   slope_ = slope_ + K[0] * residual;
121   prev_offset_ = offset_;
122   offset_ = offset_ + K[1] * residual;
123 
124   BWE_TEST_LOGGING_PLOT(1, "kc", now_ms, K[0]);
125   BWE_TEST_LOGGING_PLOT(1, "km", now_ms, K[1]);
126   BWE_TEST_LOGGING_PLOT(1, "slope_1/bps", now_ms, slope_);
127   BWE_TEST_LOGGING_PLOT(1, "var_noise", now_ms, var_noise_);
128 }
129 
UpdateMinFramePeriod(double ts_delta)130 double OveruseEstimator::UpdateMinFramePeriod(double ts_delta) {
131   double min_frame_period = ts_delta;
132   if (ts_delta_hist_.size() >= kMinFramePeriodHistoryLength) {
133     ts_delta_hist_.pop_front();
134   }
135   for (const double old_ts_delta : ts_delta_hist_) {
136     min_frame_period = std::min(old_ts_delta, min_frame_period);
137   }
138   ts_delta_hist_.push_back(ts_delta);
139   return min_frame_period;
140 }
141 
UpdateNoiseEstimate(double residual,double ts_delta,bool stable_state)142 void OveruseEstimator::UpdateNoiseEstimate(double residual,
143                                            double ts_delta,
144                                            bool stable_state) {
145   if (!stable_state) {
146     return;
147   }
148   // Faster filter during startup to faster adapt to the jitter level
149   // of the network. |alpha| is tuned for 30 frames per second, but is scaled
150   // according to |ts_delta|.
151   double alpha = 0.01;
152   if (num_of_deltas_ > 10 * 30) {
153     alpha = 0.002;
154   }
155   // Only update the noise estimate if we're not over-using. |beta| is a
156   // function of alpha and the time delta since the previous update.
157   const double beta = pow(1 - alpha, ts_delta * 30.0 / 1000.0);
158   avg_noise_ = beta * avg_noise_ + (1 - beta) * residual;
159   var_noise_ = beta * var_noise_ +
160                (1 - beta) * (avg_noise_ - residual) * (avg_noise_ - residual);
161   if (var_noise_ < 1) {
162     var_noise_ = 1;
163   }
164 }
165 }  // namespace webrtc
166