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
28 #if !defined(WEBRTC_IOS)
29 constexpr char kDefaultQualityScalingSetttings[] =
30 "Enabled-29,95,149,205,24,37,26,36,0.9995,0.9999,1";
31 #endif
32
GetThresholds(int low,int high,int max)33 absl::optional<VideoEncoder::QpThresholds> GetThresholds(int low,
34 int high,
35 int max) {
36 if (low < kMinQp || high > max || high < low)
37 return absl::nullopt;
38
39 RTC_LOG(LS_INFO) << "QP thresholds: low: " << low << ", high: " << high;
40 return absl::optional<VideoEncoder::QpThresholds>(
41 VideoEncoder::QpThresholds(low, high));
42 }
43 } // namespace
44
Enabled()45 bool QualityScalingExperiment::Enabled() {
46 #if defined(WEBRTC_IOS)
47 return webrtc::field_trial::IsEnabled(kFieldTrial);
48 #else
49 return !webrtc::field_trial::IsDisabled(kFieldTrial);
50 #endif
51 }
52
53 absl::optional<QualityScalingExperiment::Settings>
ParseSettings()54 QualityScalingExperiment::ParseSettings() {
55 std::string group = webrtc::field_trial::FindFullName(kFieldTrial);
56 // TODO(http://crbug.com/webrtc/12401): Completely remove the experiment code
57 // after few releases.
58 #if !defined(WEBRTC_IOS)
59 if (group.empty())
60 group = kDefaultQualityScalingSetttings;
61 #endif
62 Settings s;
63 if (sscanf(group.c_str(), "Enabled-%d,%d,%d,%d,%d,%d,%d,%d,%f,%f,%d",
64 &s.vp8_low, &s.vp8_high, &s.vp9_low, &s.vp9_high, &s.h264_low,
65 &s.h264_high, &s.generic_low, &s.generic_high, &s.alpha_high,
66 &s.alpha_low, &s.drop) != 11) {
67 RTC_LOG(LS_WARNING) << "Invalid number of parameters provided.";
68 return absl::nullopt;
69 }
70 return s;
71 }
72
73 absl::optional<VideoEncoder::QpThresholds>
GetQpThresholds(VideoCodecType codec_type)74 QualityScalingExperiment::GetQpThresholds(VideoCodecType codec_type) {
75 const auto settings = ParseSettings();
76 if (!settings)
77 return absl::nullopt;
78
79 switch (codec_type) {
80 case kVideoCodecVP8:
81 return GetThresholds(settings->vp8_low, settings->vp8_high, kMaxVp8Qp);
82 case kVideoCodecVP9:
83 return GetThresholds(settings->vp9_low, settings->vp9_high, kMaxVp9Qp);
84 case kVideoCodecH264:
85 return GetThresholds(settings->h264_low, settings->h264_high, kMaxH264Qp);
86 case kVideoCodecGeneric:
87 return GetThresholds(settings->generic_low, settings->generic_high,
88 kMaxGenericQp);
89 default:
90 return absl::nullopt;
91 }
92 }
93
GetConfig()94 QualityScalingExperiment::Config QualityScalingExperiment::GetConfig() {
95 const auto settings = ParseSettings();
96 if (!settings)
97 return Config();
98
99 Config config;
100 config.use_all_drop_reasons = settings->drop > 0;
101
102 if (settings->alpha_high < 0 || settings->alpha_low < settings->alpha_high) {
103 RTC_LOG(LS_WARNING) << "Invalid alpha value provided, using default.";
104 return config;
105 }
106 config.alpha_high = settings->alpha_high;
107 config.alpha_low = settings->alpha_low;
108 return config;
109 }
110
111 } // namespace webrtc
112