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 MODULES_CONGESTION_CONTROLLER_GOOG_CC_TRENDLINE_ESTIMATOR_H_ 11 #define MODULES_CONGESTION_CONTROLLER_GOOG_CC_TRENDLINE_ESTIMATOR_H_ 12 13 #include <stddef.h> 14 #include <stdint.h> 15 16 #include <deque> 17 #include <memory> 18 #include <utility> 19 20 #include "api/network_state_predictor.h" 21 #include "api/transport/webrtc_key_value_config.h" 22 #include "modules/congestion_controller/goog_cc/delay_increase_detector_interface.h" 23 #include "modules/remote_bitrate_estimator/include/bwe_defines.h" 24 #include "rtc_base/constructor_magic.h" 25 #include "rtc_base/experiments/struct_parameters_parser.h" 26 27 namespace webrtc { 28 29 struct TrendlineEstimatorSettings { 30 static constexpr char kKey[] = "WebRTC-Bwe-TrendlineEstimatorSettings"; 31 static constexpr unsigned kDefaultTrendlineWindowSize = 20; 32 33 TrendlineEstimatorSettings() = delete; 34 explicit TrendlineEstimatorSettings( 35 const WebRtcKeyValueConfig* key_value_config); 36 37 // Sort the packets in the window. Should be redundant, 38 // but then almost no cost. 39 bool enable_sort = false; 40 41 // Cap the trendline slope based on the minimum delay seen 42 // in the beginning_packets and end_packets respectively. 43 bool enable_cap = false; 44 unsigned beginning_packets = 7; 45 unsigned end_packets = 7; 46 double cap_uncertainty = 0.0; 47 48 // Size (in packets) of the window. 49 unsigned window_size = kDefaultTrendlineWindowSize; 50 51 std::unique_ptr<StructParametersParser> Parser(); 52 }; 53 54 class TrendlineEstimator : public DelayIncreaseDetectorInterface { 55 public: 56 TrendlineEstimator(const WebRtcKeyValueConfig* key_value_config, 57 NetworkStatePredictor* network_state_predictor); 58 59 ~TrendlineEstimator() override; 60 61 // Update the estimator with a new sample. The deltas should represent deltas 62 // between timestamp groups as defined by the InterArrival class. 63 void Update(double recv_delta_ms, 64 double send_delta_ms, 65 int64_t send_time_ms, 66 int64_t arrival_time_ms, 67 size_t packet_size, 68 bool calculated_deltas) override; 69 70 void UpdateTrendline(double recv_delta_ms, 71 double send_delta_ms, 72 int64_t send_time_ms, 73 int64_t arrival_time_ms, 74 size_t packet_size); 75 76 BandwidthUsage State() const override; 77 78 struct PacketTiming { PacketTimingPacketTiming79 PacketTiming(double arrival_time_ms, 80 double smoothed_delay_ms, 81 double raw_delay_ms) 82 : arrival_time_ms(arrival_time_ms), 83 smoothed_delay_ms(smoothed_delay_ms), 84 raw_delay_ms(raw_delay_ms) {} 85 double arrival_time_ms; 86 double smoothed_delay_ms; 87 double raw_delay_ms; 88 }; 89 90 private: 91 friend class GoogCcStatePrinter; 92 void Detect(double trend, double ts_delta, int64_t now_ms); 93 94 void UpdateThreshold(double modified_offset, int64_t now_ms); 95 96 // Parameters. 97 TrendlineEstimatorSettings settings_; 98 const double smoothing_coef_; 99 const double threshold_gain_; 100 // Used by the existing threshold. 101 int num_of_deltas_; 102 // Keep the arrival times small by using the change from the first packet. 103 int64_t first_arrival_time_ms_; 104 // Exponential backoff filtering. 105 double accumulated_delay_; 106 double smoothed_delay_; 107 // Linear least squares regression. 108 std::deque<PacketTiming> delay_hist_; 109 110 const double k_up_; 111 const double k_down_; 112 double overusing_time_threshold_; 113 double threshold_; 114 double prev_modified_trend_; 115 int64_t last_update_ms_; 116 double prev_trend_; 117 double time_over_using_; 118 int overuse_counter_; 119 BandwidthUsage hypothesis_; 120 BandwidthUsage hypothesis_predicted_; 121 NetworkStatePredictor* network_state_predictor_; 122 123 RTC_DISALLOW_COPY_AND_ASSIGN(TrendlineEstimator); 124 }; 125 } // namespace webrtc 126 127 #endif // MODULES_CONGESTION_CONTROLLER_GOOG_CC_TRENDLINE_ESTIMATOR_H_ 128