alr_detector.cc (3615B)
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 11 #include "modules/congestion_controller/goog_cc/alr_detector.h" 12 13 #include <memory> 14 #include <optional> 15 16 #include "api/environment/environment.h" 17 #include "api/field_trials_view.h" 18 #include "api/rtc_event_log/rtc_event_log.h" 19 #include "api/units/data_rate.h" 20 #include "api/units/data_size.h" 21 #include "api/units/time_delta.h" 22 #include "api/units/timestamp.h" 23 #include "logging/rtc_event_log/events/rtc_event_alr_state.h" 24 #include "rtc_base/checks.h" 25 #include "rtc_base/experiments/alr_experiment.h" 26 #include "rtc_base/experiments/struct_parameters_parser.h" 27 28 namespace webrtc { 29 30 AlrDetector::AlrDetectorConfig::AlrDetectorConfig( 31 const FieldTrialsView& key_value_config) { 32 RTC_CHECK(AlrExperimentSettings::MaxOneFieldTrialEnabled(key_value_config)); 33 std::optional<AlrExperimentSettings> experiment_settings = 34 AlrExperimentSettings::CreateFromFieldTrial( 35 key_value_config, 36 AlrExperimentSettings::kScreenshareProbingBweExperimentName); 37 if (!experiment_settings) { 38 experiment_settings = AlrExperimentSettings::CreateFromFieldTrial( 39 key_value_config, 40 AlrExperimentSettings::kStrictPacingAndProbingExperimentName); 41 } 42 if (experiment_settings) { 43 bandwidth_usage_ratio = 44 experiment_settings->alr_bandwidth_usage_percent / 100.0; 45 start_budget_level_ratio = 46 experiment_settings->alr_start_budget_level_percent / 100.0; 47 stop_budget_level_ratio = 48 experiment_settings->alr_stop_budget_level_percent / 100.0; 49 } 50 StructParametersParser::Create( // 51 "bw_usage", &bandwidth_usage_ratio, // 52 "start", &start_budget_level_ratio, // 53 "stop", &stop_budget_level_ratio) 54 ->Parse(key_value_config.Lookup("WebRTC-AlrDetectorParameters")); 55 } 56 57 AlrDetector::AlrDetector(const Environment& env) 58 : env_(env), conf_(env_.field_trials()), alr_budget_(0, true) {} 59 60 AlrDetector::~AlrDetector() = default; 61 62 void AlrDetector::OnBytesSent(DataSize bytes_sent, Timestamp send_time) { 63 if (!last_send_time_.has_value()) { 64 last_send_time_ = send_time; 65 // Since the duration for sending the bytes is unknwon, return without 66 // updating alr state. 67 return; 68 } 69 TimeDelta delta_time = send_time - *last_send_time_; 70 last_send_time_ = send_time; 71 72 alr_budget_.UseBudget(bytes_sent.bytes()); 73 alr_budget_.IncreaseBudget(delta_time.ms()); 74 bool state_changed = false; 75 if (alr_budget_.budget_ratio() > conf_.start_budget_level_ratio && 76 !alr_started_time_) { 77 alr_started_time_ = env_.clock().CurrentTime(); 78 state_changed = true; 79 } else if (alr_budget_.budget_ratio() < conf_.stop_budget_level_ratio && 80 alr_started_time_) { 81 state_changed = true; 82 alr_started_time_ = std::nullopt; 83 } 84 if (state_changed) { 85 env_.event_log().Log( 86 std::make_unique<RtcEventAlrState>(alr_started_time_.has_value())); 87 } 88 } 89 90 void AlrDetector::SetEstimatedBitrate(DataRate bitrate) { 91 RTC_DCHECK_GT(bitrate, DataRate::Zero()); 92 alr_budget_.set_target_rate_kbps( 93 (bitrate * conf_.bandwidth_usage_ratio).kbps()); 94 } 95 96 std::optional<Timestamp> AlrDetector::GetApplicationLimitedRegionStartTime() 97 const { 98 return alr_started_time_; 99 } 100 101 } // namespace webrtc