1 /*
2 * Copyright 2018 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 #include "rtc_base/experiments/quality_scaling_experiment.h"
11
12 #include <stdio.h>
13
14 #include <string>
15
16 #include "rtc_base/logging.h"
17 #include "system_wrappers/include/field_trial.h"
18
19 namespace webrtc {
20 namespace {
21 constexpr char kFieldTrial[] = "WebRTC-Video-QualityScaling";
22 constexpr int kMinQp = 1;
23 constexpr int kMaxVp8Qp = 127;
24 constexpr int kMaxVp9Qp = 255;
25 constexpr int kMaxH264Qp = 51;
26 constexpr int kMaxGenericQp = 255;
27
GetThresholds(int low,int high,int max)28 absl::optional<VideoEncoder::QpThresholds> GetThresholds(int low,
29 int high,
30 int max) {
31 if (low < kMinQp || high > max || high < low)
32 return absl::nullopt;
33
34 RTC_LOG(LS_INFO) << "QP thresholds: low: " << low << ", high: " << high;
35 return absl::optional<VideoEncoder::QpThresholds>(
36 VideoEncoder::QpThresholds(low, high));
37 }
38 } // namespace
39
Enabled()40 bool QualityScalingExperiment::Enabled() {
41 return webrtc::field_trial::IsEnabled(kFieldTrial);
42 }
43
44 absl::optional<QualityScalingExperiment::Settings>
ParseSettings()45 QualityScalingExperiment::ParseSettings() {
46 const std::string group = webrtc::field_trial::FindFullName(kFieldTrial);
47 if (group.empty())
48 return absl::nullopt;
49
50 Settings s;
51 if (sscanf(group.c_str(), "Enabled-%d,%d,%d,%d,%d,%d,%d,%d,%f,%f,%d",
52 &s.vp8_low, &s.vp8_high, &s.vp9_low, &s.vp9_high, &s.h264_low,
53 &s.h264_high, &s.generic_low, &s.generic_high, &s.alpha_high,
54 &s.alpha_low, &s.drop) != 11) {
55 RTC_LOG(LS_WARNING) << "Invalid number of parameters provided.";
56 return absl::nullopt;
57 }
58 return s;
59 }
60
61 absl::optional<VideoEncoder::QpThresholds>
GetQpThresholds(VideoCodecType codec_type)62 QualityScalingExperiment::GetQpThresholds(VideoCodecType codec_type) {
63 const auto settings = ParseSettings();
64 if (!settings)
65 return absl::nullopt;
66
67 switch (codec_type) {
68 case kVideoCodecVP8:
69 return GetThresholds(settings->vp8_low, settings->vp8_high, kMaxVp8Qp);
70 case kVideoCodecVP9:
71 return GetThresholds(settings->vp9_low, settings->vp9_high, kMaxVp9Qp);
72 case kVideoCodecH264:
73 return GetThresholds(settings->h264_low, settings->h264_high, kMaxH264Qp);
74 case kVideoCodecGeneric:
75 return GetThresholds(settings->generic_low, settings->generic_high,
76 kMaxGenericQp);
77 default:
78 return absl::nullopt;
79 }
80 }
81
GetConfig()82 QualityScalingExperiment::Config QualityScalingExperiment::GetConfig() {
83 const auto settings = ParseSettings();
84 if (!settings)
85 return Config();
86
87 Config config;
88 config.use_all_drop_reasons = settings->drop > 0;
89
90 if (settings->alpha_high < 0 || settings->alpha_low < settings->alpha_high) {
91 RTC_LOG(LS_WARNING) << "Invalid alpha value provided, using default.";
92 return config;
93 }
94 config.alpha_high = settings->alpha_high;
95 config.alpha_low = settings->alpha_low;
96 return config;
97 }
98
99 } // namespace webrtc
100