OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
(...skipping 19 matching lines...) Expand all Loading... |
30 const Threshold& fec_enabling_threshold, | 30 const Threshold& fec_enabling_threshold, |
31 const Threshold& fec_disabling_threshold, | 31 const Threshold& fec_disabling_threshold, |
32 int time_constant_ms, | 32 int time_constant_ms, |
33 const Clock* clock) | 33 const Clock* clock) |
34 : initial_fec_enabled(initial_fec_enabled), | 34 : initial_fec_enabled(initial_fec_enabled), |
35 fec_enabling_threshold(fec_enabling_threshold), | 35 fec_enabling_threshold(fec_enabling_threshold), |
36 fec_disabling_threshold(fec_disabling_threshold), | 36 fec_disabling_threshold(fec_disabling_threshold), |
37 time_constant_ms(time_constant_ms), | 37 time_constant_ms(time_constant_ms), |
38 clock(clock) {} | 38 clock(clock) {} |
39 | 39 |
40 FecController::FecController(const Config& config) | 40 FecController::FecController(const Config& config, |
| 41 std::unique_ptr<SmoothingFilter> smoothing_filter) |
41 : config_(config), | 42 : config_(config), |
42 fec_enabled_(config.initial_fec_enabled), | 43 fec_enabled_(config.initial_fec_enabled), |
43 packet_loss_smoothed_( | 44 packet_loss_smoother_(std::move(smoothing_filter)), |
44 new SmoothingFilterImpl(config_.time_constant_ms, config_.clock)), | |
45 fec_enabling_threshold_info_(config_.fec_enabling_threshold), | 45 fec_enabling_threshold_info_(config_.fec_enabling_threshold), |
46 fec_disabling_threshold_info_(config_.fec_disabling_threshold) { | 46 fec_disabling_threshold_info_(config_.fec_disabling_threshold) { |
47 RTC_DCHECK_LE(fec_enabling_threshold_info_.slope, 0); | 47 RTC_DCHECK_LE(fec_enabling_threshold_info_.slope, 0); |
48 RTC_DCHECK_LE(fec_enabling_threshold_info_.slope, 0); | 48 RTC_DCHECK_LE(fec_enabling_threshold_info_.slope, 0); |
49 RTC_DCHECK_LE( | 49 RTC_DCHECK_LE( |
50 GetPacketLossThreshold(config_.fec_enabling_threshold.low_bandwidth_bps, | 50 GetPacketLossThreshold(config_.fec_enabling_threshold.low_bandwidth_bps, |
51 config_.fec_disabling_threshold, | 51 config_.fec_disabling_threshold, |
52 fec_disabling_threshold_info_), | 52 fec_disabling_threshold_info_), |
53 config_.fec_enabling_threshold.low_bandwidth_packet_loss); | 53 config_.fec_enabling_threshold.low_bandwidth_packet_loss); |
54 RTC_DCHECK_LE( | 54 RTC_DCHECK_LE( |
55 GetPacketLossThreshold(config_.fec_enabling_threshold.high_bandwidth_bps, | 55 GetPacketLossThreshold(config_.fec_enabling_threshold.high_bandwidth_bps, |
56 config_.fec_disabling_threshold, | 56 config_.fec_disabling_threshold, |
57 fec_disabling_threshold_info_), | 57 fec_disabling_threshold_info_), |
58 config_.fec_enabling_threshold.high_bandwidth_packet_loss); | 58 config_.fec_enabling_threshold.high_bandwidth_packet_loss); |
59 } | 59 } |
60 | 60 |
61 FecController::FecController(const Config& config, | 61 FecController::FecController(const Config& config) |
62 std::unique_ptr<SmoothingFilter> smoothing_filter) | 62 : FecController( |
63 : FecController(config) { | 63 config, |
64 packet_loss_smoothed_ = std::move(smoothing_filter); | 64 std::unique_ptr<SmoothingFilter>( |
| 65 new SmoothingFilterImpl(config.time_constant_ms, config.clock))) { |
65 } | 66 } |
66 | 67 |
67 FecController::~FecController() = default; | 68 FecController::~FecController() = default; |
68 | 69 |
| 70 void FecController::UpdateNetworkMetrics( |
| 71 const NetworkMetrics& network_metrics) { |
| 72 if (network_metrics.uplink_bandwidth_bps) |
| 73 uplink_bandwidth_bps_ = network_metrics.uplink_bandwidth_bps; |
| 74 if (network_metrics.uplink_packet_loss_fraction) { |
| 75 packet_loss_smoother_->AddSample( |
| 76 *network_metrics.uplink_packet_loss_fraction); |
| 77 } |
| 78 } |
| 79 |
69 void FecController::MakeDecision( | 80 void FecController::MakeDecision( |
70 const NetworkMetrics& metrics, | |
71 AudioNetworkAdaptor::EncoderRuntimeConfig* config) { | 81 AudioNetworkAdaptor::EncoderRuntimeConfig* config) { |
72 RTC_DCHECK(!config->enable_fec); | 82 RTC_DCHECK(!config->enable_fec); |
73 RTC_DCHECK(!config->uplink_packet_loss_fraction); | 83 RTC_DCHECK(!config->uplink_packet_loss_fraction); |
74 | 84 |
75 if (metrics.uplink_packet_loss_fraction) | 85 const auto& packet_loss = packet_loss_smoother_->GetAverage(); |
76 packet_loss_smoothed_->AddSample(*metrics.uplink_packet_loss_fraction); | |
77 | 86 |
78 const auto& packet_loss = packet_loss_smoothed_->GetAverage(); | 87 fec_enabled_ = fec_enabled_ ? !FecDisablingDecision(packet_loss) |
79 | 88 : FecEnablingDecision(packet_loss); |
80 fec_enabled_ = fec_enabled_ ? !FecDisablingDecision(metrics, packet_loss) | |
81 : FecEnablingDecision(metrics, packet_loss); | |
82 | 89 |
83 config->enable_fec = rtc::Optional<bool>(fec_enabled_); | 90 config->enable_fec = rtc::Optional<bool>(fec_enabled_); |
84 | 91 |
85 auto packet_loss_fraction = packet_loss_smoothed_->GetAverage(); | 92 config->uplink_packet_loss_fraction = |
86 config->uplink_packet_loss_fraction = rtc::Optional<float>( | 93 rtc::Optional<float>(packet_loss ? *packet_loss : 0.0); |
87 packet_loss_fraction ? *packet_loss_fraction : 0.0); | |
88 } | 94 } |
89 | 95 |
90 FecController::ThresholdInfo::ThresholdInfo( | 96 FecController::ThresholdInfo::ThresholdInfo( |
91 const Config::Threshold& threshold) { | 97 const Config::Threshold& threshold) { |
92 int bandwidth_diff_bps = | 98 int bandwidth_diff_bps = |
93 threshold.high_bandwidth_bps - threshold.low_bandwidth_bps; | 99 threshold.high_bandwidth_bps - threshold.low_bandwidth_bps; |
94 float packet_loss_diff = threshold.high_bandwidth_packet_loss - | 100 float packet_loss_diff = threshold.high_bandwidth_packet_loss - |
95 threshold.low_bandwidth_packet_loss; | 101 threshold.low_bandwidth_packet_loss; |
96 slope = bandwidth_diff_bps == 0 ? 0.0 : packet_loss_diff / bandwidth_diff_bps; | 102 slope = bandwidth_diff_bps == 0 ? 0.0 : packet_loss_diff / bandwidth_diff_bps; |
97 offset = | 103 offset = |
98 threshold.low_bandwidth_packet_loss - slope * threshold.low_bandwidth_bps; | 104 threshold.low_bandwidth_packet_loss - slope * threshold.low_bandwidth_bps; |
99 } | 105 } |
100 | 106 |
101 float FecController::GetPacketLossThreshold( | 107 float FecController::GetPacketLossThreshold( |
102 int bandwidth_bps, | 108 int bandwidth_bps, |
103 const Config::Threshold& threshold, | 109 const Config::Threshold& threshold, |
104 const ThresholdInfo& threshold_info) const { | 110 const ThresholdInfo& threshold_info) const { |
105 if (bandwidth_bps < threshold.low_bandwidth_bps) | 111 if (bandwidth_bps < threshold.low_bandwidth_bps) |
106 return std::numeric_limits<float>::max(); | 112 return std::numeric_limits<float>::max(); |
107 if (bandwidth_bps >= threshold.high_bandwidth_bps) | 113 if (bandwidth_bps >= threshold.high_bandwidth_bps) |
108 return threshold.high_bandwidth_packet_loss; | 114 return threshold.high_bandwidth_packet_loss; |
109 return threshold_info.offset + threshold_info.slope * bandwidth_bps; | 115 return threshold_info.offset + threshold_info.slope * bandwidth_bps; |
110 } | 116 } |
111 | 117 |
112 bool FecController::FecEnablingDecision( | 118 bool FecController::FecEnablingDecision( |
113 const NetworkMetrics& metrics, | |
114 const rtc::Optional<float>& packet_loss) const { | 119 const rtc::Optional<float>& packet_loss) const { |
115 if (!metrics.uplink_bandwidth_bps) | 120 if (!uplink_bandwidth_bps_) |
116 return false; | 121 return false; |
117 if (!packet_loss) | 122 if (!packet_loss) |
118 return false; | 123 return false; |
119 return *packet_loss >= GetPacketLossThreshold(*metrics.uplink_bandwidth_bps, | 124 return *packet_loss >= GetPacketLossThreshold(*uplink_bandwidth_bps_, |
120 config_.fec_enabling_threshold, | 125 config_.fec_enabling_threshold, |
121 fec_enabling_threshold_info_); | 126 fec_enabling_threshold_info_); |
122 } | 127 } |
123 | 128 |
124 bool FecController::FecDisablingDecision( | 129 bool FecController::FecDisablingDecision( |
125 const NetworkMetrics& metrics, | |
126 const rtc::Optional<float>& packet_loss) const { | 130 const rtc::Optional<float>& packet_loss) const { |
127 if (!metrics.uplink_bandwidth_bps) | 131 if (!uplink_bandwidth_bps_) |
128 return false; | 132 return false; |
129 if (!packet_loss) | 133 if (!packet_loss) |
130 return false; | 134 return false; |
131 return *packet_loss <= GetPacketLossThreshold(*metrics.uplink_bandwidth_bps, | 135 return *packet_loss <= GetPacketLossThreshold(*uplink_bandwidth_bps_, |
132 config_.fec_disabling_threshold, | 136 config_.fec_disabling_threshold, |
133 fec_disabling_threshold_info_); | 137 fec_disabling_threshold_info_); |
134 } | 138 } |
135 | 139 |
136 } // namespace webrtc | 140 } // namespace webrtc |
OLD | NEW |