OLD | NEW |
| (Empty) |
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 "webrtc/modules/audio_coding/audio_network_adaptor/fec_controller.h" | |
12 | |
13 #include <limits> | |
14 #include <utility> | |
15 | |
16 #include "webrtc/base/checks.h" | |
17 | |
18 namespace webrtc { | |
19 | |
20 FecController::Config::Threshold::Threshold(int low_bandwidth_bps, | |
21 float low_bandwidth_packet_loss, | |
22 int high_bandwidth_bps, | |
23 float high_bandwidth_packet_loss) | |
24 : low_bandwidth_bps(low_bandwidth_bps), | |
25 low_bandwidth_packet_loss(low_bandwidth_packet_loss), | |
26 high_bandwidth_bps(high_bandwidth_bps), | |
27 high_bandwidth_packet_loss(high_bandwidth_packet_loss) {} | |
28 | |
29 FecController::Config::Config(bool initial_fec_enabled, | |
30 const Threshold& fec_enabling_threshold, | |
31 const Threshold& fec_disabling_threshold, | |
32 int time_constant_ms, | |
33 const Clock* clock) | |
34 : initial_fec_enabled(initial_fec_enabled), | |
35 fec_enabling_threshold(fec_enabling_threshold), | |
36 fec_disabling_threshold(fec_disabling_threshold), | |
37 time_constant_ms(time_constant_ms), | |
38 clock(clock) {} | |
39 | |
40 FecController::FecController(const Config& config, | |
41 std::unique_ptr<SmoothingFilter> smoothing_filter) | |
42 : config_(config), | |
43 fec_enabled_(config.initial_fec_enabled), | |
44 packet_loss_smoother_(std::move(smoothing_filter)), | |
45 fec_enabling_threshold_info_(config_.fec_enabling_threshold), | |
46 fec_disabling_threshold_info_(config_.fec_disabling_threshold) { | |
47 RTC_DCHECK_LE(fec_enabling_threshold_info_.slope, 0); | |
48 RTC_DCHECK_LE(fec_enabling_threshold_info_.slope, 0); | |
49 RTC_DCHECK_LE( | |
50 GetPacketLossThreshold(config_.fec_enabling_threshold.low_bandwidth_bps, | |
51 config_.fec_disabling_threshold, | |
52 fec_disabling_threshold_info_), | |
53 config_.fec_enabling_threshold.low_bandwidth_packet_loss); | |
54 RTC_DCHECK_LE( | |
55 GetPacketLossThreshold(config_.fec_enabling_threshold.high_bandwidth_bps, | |
56 config_.fec_disabling_threshold, | |
57 fec_disabling_threshold_info_), | |
58 config_.fec_enabling_threshold.high_bandwidth_packet_loss); | |
59 } | |
60 | |
61 FecController::FecController(const Config& config) | |
62 : FecController( | |
63 config, | |
64 std::unique_ptr<SmoothingFilter>( | |
65 new SmoothingFilterImpl(config.time_constant_ms, config.clock))) { | |
66 } | |
67 | |
68 FecController::~FecController() = default; | |
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 | |
80 void FecController::MakeDecision( | |
81 AudioNetworkAdaptor::EncoderRuntimeConfig* config) { | |
82 RTC_DCHECK(!config->enable_fec); | |
83 RTC_DCHECK(!config->uplink_packet_loss_fraction); | |
84 | |
85 const auto& packet_loss = packet_loss_smoother_->GetAverage(); | |
86 | |
87 fec_enabled_ = fec_enabled_ ? !FecDisablingDecision(packet_loss) | |
88 : FecEnablingDecision(packet_loss); | |
89 | |
90 config->enable_fec = rtc::Optional<bool>(fec_enabled_); | |
91 | |
92 config->uplink_packet_loss_fraction = | |
93 rtc::Optional<float>(packet_loss ? *packet_loss : 0.0); | |
94 } | |
95 | |
96 FecController::ThresholdInfo::ThresholdInfo( | |
97 const Config::Threshold& threshold) { | |
98 int bandwidth_diff_bps = | |
99 threshold.high_bandwidth_bps - threshold.low_bandwidth_bps; | |
100 float packet_loss_diff = threshold.high_bandwidth_packet_loss - | |
101 threshold.low_bandwidth_packet_loss; | |
102 slope = bandwidth_diff_bps == 0 ? 0.0 : packet_loss_diff / bandwidth_diff_bps; | |
103 offset = | |
104 threshold.low_bandwidth_packet_loss - slope * threshold.low_bandwidth_bps; | |
105 } | |
106 | |
107 float FecController::GetPacketLossThreshold( | |
108 int bandwidth_bps, | |
109 const Config::Threshold& threshold, | |
110 const ThresholdInfo& threshold_info) const { | |
111 if (bandwidth_bps < threshold.low_bandwidth_bps) | |
112 return std::numeric_limits<float>::max(); | |
113 if (bandwidth_bps >= threshold.high_bandwidth_bps) | |
114 return threshold.high_bandwidth_packet_loss; | |
115 return threshold_info.offset + threshold_info.slope * bandwidth_bps; | |
116 } | |
117 | |
118 bool FecController::FecEnablingDecision( | |
119 const rtc::Optional<float>& packet_loss) const { | |
120 if (!uplink_bandwidth_bps_) | |
121 return false; | |
122 if (!packet_loss) | |
123 return false; | |
124 return *packet_loss >= GetPacketLossThreshold(*uplink_bandwidth_bps_, | |
125 config_.fec_enabling_threshold, | |
126 fec_enabling_threshold_info_); | |
127 } | |
128 | |
129 bool FecController::FecDisablingDecision( | |
130 const rtc::Optional<float>& packet_loss) const { | |
131 if (!uplink_bandwidth_bps_) | |
132 return false; | |
133 if (!packet_loss) | |
134 return false; | |
135 return *packet_loss <= GetPacketLossThreshold(*uplink_bandwidth_bps_, | |
136 config_.fec_disabling_threshold, | |
137 fec_disabling_threshold_info_); | |
138 } | |
139 | |
140 } // namespace webrtc | |
OLD | NEW |