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 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 : config_(config), | |
42 fec_enabled_(config.initial_fec_enabled), | |
43 packet_loss_smoothed_( | |
44 new SmoothingFilterImpl(config_.time_constant_ms, config_.clock)), | |
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 std::unique_ptr<SmoothingFilter> smoothing_filter) | |
63 : FecController(config) { | |
64 packet_loss_smoothed_ = std::move(smoothing_filter); | |
65 } | |
66 | |
67 FecController::~FecController() = default; | |
68 | |
69 void FecController::MakeDecision( | |
70 const NetworkMetrics& metrics, | |
71 AudioNetworkAdaptor::EncoderRuntimeConfig* config) { | |
72 RTC_DCHECK(!config->enable_fec); | |
73 RTC_DCHECK(!config->uplink_packet_loss_fraction); | |
74 | |
75 if (metrics.uplink_packet_loss_fraction) | |
76 packet_loss_smoothed_->AddSample(*metrics.uplink_packet_loss_fraction); | |
77 | |
78 fec_enabled_ = fec_enabled_ ? !FecDisablingDecision(metrics) | |
79 : FecEnablingDecision(metrics); | |
80 | |
81 config->enable_fec = rtc::Optional<bool>(fec_enabled_); | |
82 | |
83 if (fec_enabled_) { | |
84 auto packet_loss_fraction = packet_loss_smoothed_->GetAverage(); | |
85 config->uplink_packet_loss_fraction = rtc::Optional<float>( | |
86 packet_loss_fraction ? *packet_loss_fraction : 0.0); | |
87 } else { | |
88 config->uplink_packet_loss_fraction = rtc::Optional<float>(0.0); | |
michaelt
2016/09/21 12:51:54
OPUS uses the packet loss information not only for
minyue-webrtc
2016/09/21 13:37:52
Good point!
| |
89 } | |
90 } | |
91 | |
92 FecController::ThresholdInfo::ThresholdInfo( | |
93 const Config::Threshold& threshold) { | |
94 int bandwidth_diff_bps = | |
95 threshold.high_bandwidth_bps - threshold.low_bandwidth_bps; | |
96 float packet_loss_diff = threshold.high_bandwidth_packet_loss - | |
97 threshold.low_bandwidth_packet_loss; | |
98 slope = bandwidth_diff_bps == 0 ? 0.0 : packet_loss_diff / bandwidth_diff_bps; | |
99 offset = | |
100 threshold.low_bandwidth_packet_loss - slope * threshold.low_bandwidth_bps; | |
101 } | |
102 | |
103 float FecController::GetPacketLossThreshold( | |
104 int bandwidth_bps, | |
105 const Config::Threshold& threshold, | |
106 const ThresholdInfo& threshold_info) const { | |
107 if (bandwidth_bps < threshold.low_bandwidth_bps) | |
108 return std::numeric_limits<float>::max(); | |
109 if (bandwidth_bps >= threshold.high_bandwidth_bps) | |
110 return threshold.high_bandwidth_packet_loss; | |
111 return threshold_info.offset + threshold_info.slope * bandwidth_bps; | |
112 } | |
113 | |
114 bool FecController::FecEnablingDecision(const NetworkMetrics& metrics) const { | |
115 if (!metrics.uplink_bandwidth_bps) | |
116 return false; | |
117 | |
118 auto packet_loss = packet_loss_smoothed_->GetAverage(); | |
119 if (!packet_loss) | |
120 return false; | |
121 | |
122 return *packet_loss >= GetPacketLossThreshold(*metrics.uplink_bandwidth_bps, | |
123 config_.fec_enabling_threshold, | |
124 fec_enabling_threshold_info_); | |
125 } | |
126 | |
127 bool FecController::FecDisablingDecision(const NetworkMetrics& metrics) const { | |
128 if (!metrics.uplink_bandwidth_bps) | |
129 return false; | |
130 | |
131 auto packet_loss = packet_loss_smoothed_->GetAverage(); | |
132 if (!packet_loss) | |
133 return false; | |
134 | |
135 return *packet_loss <= GetPacketLossThreshold(*metrics.uplink_bandwidth_bps, | |
136 config_.fec_disabling_threshold, | |
137 fec_disabling_threshold_info_); | |
138 } | |
139 | |
140 } // namespace webrtc | |
OLD | NEW |