1 /*
2  *  Copyright (c) 2019 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 "rtc_base/experiments/quality_rampup_experiment.h"
12 
13 #include <algorithm>
14 
15 #include "api/transport/field_trial_based_config.h"
16 #include "rtc_base/logging.h"
17 
18 namespace webrtc {
19 
QualityRampupExperiment(const WebRtcKeyValueConfig * const key_value_config)20 QualityRampupExperiment::QualityRampupExperiment(
21     const WebRtcKeyValueConfig* const key_value_config)
22     : min_pixels_("min_pixels"),
23       min_duration_ms_("min_duration_ms"),
24       max_bitrate_factor_("max_bitrate_factor") {
25   ParseFieldTrial(
26       {&min_pixels_, &min_duration_ms_, &max_bitrate_factor_},
27       key_value_config->Lookup("WebRTC-Video-QualityRampupSettings"));
28 }
29 
ParseSettings()30 QualityRampupExperiment QualityRampupExperiment::ParseSettings() {
31   FieldTrialBasedConfig field_trial_config;
32   return QualityRampupExperiment(&field_trial_config);
33 }
34 
MinPixels() const35 absl::optional<int> QualityRampupExperiment::MinPixels() const {
36   return min_pixels_.GetOptional();
37 }
38 
MinDurationMs() const39 absl::optional<int> QualityRampupExperiment::MinDurationMs() const {
40   return min_duration_ms_.GetOptional();
41 }
42 
MaxBitrateFactor() const43 absl::optional<double> QualityRampupExperiment::MaxBitrateFactor() const {
44   return max_bitrate_factor_.GetOptional();
45 }
46 
SetMaxBitrate(int pixels,uint32_t max_bitrate_kbps)47 void QualityRampupExperiment::SetMaxBitrate(int pixels,
48                                             uint32_t max_bitrate_kbps) {
49   if (!min_pixels_ || pixels < min_pixels_.Value() || max_bitrate_kbps == 0) {
50     return;
51   }
52   max_bitrate_kbps_ = std::max(max_bitrate_kbps_.value_or(0), max_bitrate_kbps);
53 }
54 
BwHigh(int64_t now_ms,uint32_t available_bw_kbps)55 bool QualityRampupExperiment::BwHigh(int64_t now_ms,
56                                      uint32_t available_bw_kbps) {
57   if (!min_pixels_ || !min_duration_ms_ || !max_bitrate_kbps_) {
58     return false;
59   }
60 
61   if (available_bw_kbps <
62       max_bitrate_kbps_.value() * MaxBitrateFactor().value_or(1)) {
63     start_ms_.reset();
64     return false;
65   }
66 
67   if (!start_ms_)
68     start_ms_ = now_ms;
69 
70   return (now_ms - *start_ms_) >= min_duration_ms_.Value();
71 }
72 
Enabled() const73 bool QualityRampupExperiment::Enabled() const {
74   return min_pixels_ || min_duration_ms_ || max_bitrate_kbps_;
75 }
76 
77 }  // namespace webrtc
78