Index: webrtc/modules/audio_coding/audio_network_adaptor/fec_controller.cc |
diff --git a/webrtc/modules/audio_coding/audio_network_adaptor/fec_controller.cc b/webrtc/modules/audio_coding/audio_network_adaptor/fec_controller.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..8ae6587fb4337637c14108d7179e37799c780c41 |
--- /dev/null |
+++ b/webrtc/modules/audio_coding/audio_network_adaptor/fec_controller.cc |
@@ -0,0 +1,224 @@ |
+/* |
+ * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. |
+ * |
+ * Use of this source code is governed by a BSD-style license |
+ * that can be found in the LICENSE file in the root of the source |
+ * tree. An additional intellectual property rights grant can be found |
+ * in the file PATENTS. All contributing project authors may |
+ * be found in the AUTHORS file in the root of the source tree. |
+ */ |
+ |
+#include "webrtc/modules/audio_coding/audio_network_adaptor/fec_controller.h" |
+ |
+#include <algorithm> |
+#include <utility> |
+ |
+#include "webrtc/base/checks.h" |
+ |
+namespace webrtc { |
+ |
+namespace { |
+ |
+// Return if two line segments: (l1_x1, l1_y1) - (l1_x2, l1_y2) and |
+// (l2_x1, l2_y1) - (l2_x2, l2_y2) intersect. |
+bool Insersect(int l1_x1, |
+ float l1_y1, |
+ int l1_x2, |
+ float l1_y2, |
+ int l2_x1, |
+ float l2_y1, |
+ int l2_x2, |
+ float l2_y2) { |
+ // Solving alpha and beta for |
+ // l1_x1 * alpha + l1_x2 * (1 - alpha) = l2_x1 * beta + l2_x2 * (1 - beta) |
+ // l1_y1 * alpha + l1_y2 * (1 - alpha) = l2_y1 * beta + l2_y2 * (1 - beta) |
+ // This leads to |
+ int h1 = l1_x1 - l1_x2; |
+ int h2 = l2_x2 - l2_x1; |
+ float h3 = l1_y1 - l1_y2; |
+ float h4 = l2_y2 - l2_y1; |
+ int b1 = l2_x2 - l1_x2; |
+ float b2 = l2_y2 - l1_y2; |
+ float denom = h1 * h4 - h2 * h3; |
+ printf("%f\n", denom); |
minyue-webrtc
2016/09/20 12:01:01
remove
|
+ if (denom == 0.0) |
+ return false; |
+ float alpha = (h4 * b1 - h2 * b2) / denom; |
+ float beta = (-h3 * b1 + h1 * b2) / denom; |
+ printf("%f, %f\n", alpha, beta); |
minyue-webrtc
2016/09/20 12:01:01
remove
|
+ return alpha >= 0 && alpha <= 1 && beta >= 0 && beta <= 1; |
+} |
+ |
+} // namespace |
+ |
+FecController::Config::Threshold::Threshold(int low_bandwidth_bps, |
+ float low_bandwidth_packet_loss, |
+ int high_bandwidth_bps, |
+ float high_bandwidth_packet_loss) |
+ : low_bandwidth_bps(low_bandwidth_bps), |
+ low_bandwidth_packet_loss(low_bandwidth_packet_loss), |
+ high_bandwidth_bps(high_bandwidth_bps), |
+ high_bandwidth_packet_loss(high_bandwidth_packet_loss) {} |
+ |
+FecController::Config::Config(bool initial_fec_enabled, |
+ const Threshold& fec_enabling_threshold, |
+ const Threshold& fec_disabling_threshold, |
+ int time_constant_ms, |
+ Clock* clock) |
+ : initial_fec_enabled(initial_fec_enabled), |
+ fec_enabling_threshold(fec_enabling_threshold), |
+ fec_disabling_threshold(fec_disabling_threshold), |
+ time_constant_ms(time_constant_ms), |
+ clock(clock) {} |
+ |
+FecController::FecController(const Config& config) |
+ : config_(config), |
+ fec_enabled_(config.initial_fec_enabled), |
+ packet_loss_smoothed_( |
+ new SmoothingFilterImpl(config_.time_constant_ms, config_.clock)), |
+ fec_enabling_threshold_info_(config_.fec_enabling_threshold), |
+ fec_disabling_threshold_info_(config_.fec_disabling_threshold) { |
+ RTC_DCHECK_LE(fec_enabling_threshold_info_.slope, 0); |
+ RTC_DCHECK_LE(fec_enabling_threshold_info_.slope, 0); |
+ RTC_DCHECK_GE(config_.fec_enabling_threshold.low_bandwidth_bps, |
+ config_.fec_disabling_threshold.low_bandwidth_bps); |
+ RTC_DCHECK_GE(config_.fec_enabling_threshold.high_bandwidth_packet_loss, |
+ config_.fec_disabling_threshold.high_bandwidth_packet_loss); |
+ // Ensure that the two middle parts of the curves do not intersect. |
hlundin-webrtc
2016/09/20 12:40:37
Please, simplify this as we discussed offline. (To
minyue-webrtc
2016/09/20 16:16:12
thanks the inspiration! FecDisablingDecision and F
|
+ RTC_DCHECK( |
+ !Insersect(config_.fec_enabling_threshold.low_bandwidth_bps, |
+ config_.fec_enabling_threshold.low_bandwidth_packet_loss, |
+ config_.fec_enabling_threshold.high_bandwidth_bps, |
+ config_.fec_enabling_threshold.high_bandwidth_packet_loss, |
+ config_.fec_disabling_threshold.low_bandwidth_bps, |
+ config_.fec_disabling_threshold.low_bandwidth_packet_loss, |
+ config_.fec_disabling_threshold.high_bandwidth_bps, |
+ config_.fec_disabling_threshold.high_bandwidth_packet_loss)); |
+ // Ensure that the middle part of the FEC disabling curve do not intersect |
+ // with the vertical part of the FEC enabling curve. |
+ RTC_DCHECK(!Insersect( |
+ config_.fec_enabling_threshold.low_bandwidth_bps, |
+ config_.fec_enabling_threshold.low_bandwidth_packet_loss, |
+ config_.fec_enabling_threshold.low_bandwidth_bps, |
+ std::max(config_.fec_enabling_threshold.low_bandwidth_packet_loss, |
+ config_.fec_disabling_threshold.low_bandwidth_packet_loss), |
+ config_.fec_disabling_threshold.low_bandwidth_bps, |
+ config_.fec_disabling_threshold.low_bandwidth_packet_loss, |
+ config_.fec_disabling_threshold.high_bandwidth_bps, |
+ config_.fec_disabling_threshold.high_bandwidth_packet_loss)); |
+ // Ensure that the middle part of the FEC disabling curve do not intersect |
+ // with the horizontal part of the FEC enabling curve. |
+ RTC_DCHECK( |
+ !Insersect(std::max(config_.fec_enabling_threshold.high_bandwidth_bps, |
+ config_.fec_disabling_threshold.high_bandwidth_bps), |
+ config_.fec_enabling_threshold.high_bandwidth_packet_loss, |
+ config_.fec_enabling_threshold.high_bandwidth_bps, |
+ config_.fec_enabling_threshold.high_bandwidth_packet_loss, |
+ config_.fec_disabling_threshold.low_bandwidth_bps, |
+ config_.fec_disabling_threshold.low_bandwidth_packet_loss, |
+ config_.fec_disabling_threshold.high_bandwidth_bps, |
+ config_.fec_disabling_threshold.high_bandwidth_packet_loss)); |
+} |
+ |
+FecController::FecController(const Config& config, |
+ std::unique_ptr<SmoothingFilter> smoothing_filter) |
+ : FecController(config) { |
+ packet_loss_smoothed_ = std::move(smoothing_filter); |
+} |
+ |
+FecController::~FecController() = default; |
+ |
+void FecController::MakeDecision( |
+ const NetworkMetrics& metrics, |
+ AudioNetworkAdaptor::EncoderRuntimeConfig* config) { |
+ RTC_DCHECK(!config->enable_fec); |
+ RTC_DCHECK(!config->uplink_packet_loss_fraction); |
+ |
+ if (metrics.uplink_packet_loss_fraction) |
+ packet_loss_smoothed_->AddSample(*metrics.uplink_packet_loss_fraction); |
+ |
+ fec_enabled_ = fec_enabled_ ? !FecDisablingDecision(metrics) |
+ : FecEnablingDecision(metrics); |
+ |
+ config->enable_fec = rtc::Optional<bool>(fec_enabled_); |
+ |
+ if (fec_enabled_) { |
+ auto packet_loss_fraction = packet_loss_smoothed_->GetAverage(); |
+ config->uplink_packet_loss_fraction = rtc::Optional<float>( |
+ packet_loss_fraction ? *packet_loss_fraction : 0.0); |
+ } else { |
+ config->uplink_packet_loss_fraction = rtc::Optional<float>(0.0); |
+ } |
+} |
+ |
+FecController::ThresholdInfo::ThresholdInfo( |
+ const Config::Threshold& threshold) { |
+ int bandwidth_diff_bps = |
+ threshold.high_bandwidth_bps - threshold.low_bandwidth_bps; |
+ float packet_loss_diff = threshold.high_bandwidth_packet_loss - |
+ threshold.low_bandwidth_packet_loss; |
+ slope = bandwidth_diff_bps == 0 ? 0.0 : packet_loss_diff / bandwidth_diff_bps; |
+ offset = |
+ threshold.low_bandwidth_packet_loss - slope * threshold.low_bandwidth_bps; |
+} |
+ |
+float FecController::GetPacketLossThreshold( |
+ int bandwidth_bps, |
+ const FecController::ThresholdInfo& threshold_info) const { |
+ return threshold_info.offset + threshold_info.slope * bandwidth_bps; |
+} |
+ |
+bool FecController::FecEnablingDecision(const NetworkMetrics& metrics) const { |
+ if (!metrics.uplink_bandwidth_bps) |
+ return false; |
+ |
+ auto packet_loss = packet_loss_smoothed_->GetAverage(); |
+ if (!packet_loss) |
+ return false; |
+ |
+ if (*metrics.uplink_bandwidth_bps >= |
+ config_.fec_enabling_threshold.high_bandwidth_bps && |
+ *packet_loss >= config_.fec_enabling_threshold.high_bandwidth_packet_loss) |
+ return true; |
+ |
+ if (*metrics.uplink_bandwidth_bps < |
+ config_.fec_enabling_threshold.high_bandwidth_bps && |
+ *metrics.uplink_bandwidth_bps >= |
+ config_.fec_enabling_threshold.low_bandwidth_bps && |
+ *packet_loss >= GetPacketLossThreshold(*metrics.uplink_bandwidth_bps, |
+ fec_enabling_threshold_info_)) |
+ return true; |
+ |
+ return false; |
+} |
+ |
+bool FecController::FecDisablingDecision(const NetworkMetrics& metrics) const { |
+ if (!metrics.uplink_bandwidth_bps) |
+ return false; |
+ |
+ auto packet_loss = packet_loss_smoothed_->GetAverage(); |
+ if (!packet_loss) |
+ return false; |
+ |
+ if (*metrics.uplink_bandwidth_bps <= |
+ config_.fec_disabling_threshold.low_bandwidth_bps) |
+ return true; |
+ |
+ if (*metrics.uplink_bandwidth_bps >= |
+ config_.fec_disabling_threshold.high_bandwidth_bps && |
+ *packet_loss <= |
+ config_.fec_disabling_threshold.high_bandwidth_packet_loss) |
+ return true; |
+ |
+ if (*metrics.uplink_bandwidth_bps > |
+ config_.fec_disabling_threshold.low_bandwidth_bps && |
+ *metrics.uplink_bandwidth_bps < |
+ config_.fec_disabling_threshold.high_bandwidth_bps && |
+ *packet_loss <= GetPacketLossThreshold(*metrics.uplink_bandwidth_bps, |
+ fec_disabling_threshold_info_)) |
+ return true; |
+ |
+ return false; |
+} |
+ |
+} // namespace webrtc |