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 auto packet_loss_fraction = packet_loss_smoothed_->GetAverage(); |
| 84 config->uplink_packet_loss_fraction = rtc::Optional<float>( |
| 85 packet_loss_fraction ? *packet_loss_fraction : 0.0); |
| 86 } |
| 87 |
| 88 FecController::ThresholdInfo::ThresholdInfo( |
| 89 const Config::Threshold& threshold) { |
| 90 int bandwidth_diff_bps = |
| 91 threshold.high_bandwidth_bps - threshold.low_bandwidth_bps; |
| 92 float packet_loss_diff = threshold.high_bandwidth_packet_loss - |
| 93 threshold.low_bandwidth_packet_loss; |
| 94 slope = bandwidth_diff_bps == 0 ? 0.0 : packet_loss_diff / bandwidth_diff_bps; |
| 95 offset = |
| 96 threshold.low_bandwidth_packet_loss - slope * threshold.low_bandwidth_bps; |
| 97 } |
| 98 |
| 99 float FecController::GetPacketLossThreshold( |
| 100 int bandwidth_bps, |
| 101 const Config::Threshold& threshold, |
| 102 const ThresholdInfo& threshold_info) const { |
| 103 if (bandwidth_bps < threshold.low_bandwidth_bps) |
| 104 return std::numeric_limits<float>::max(); |
| 105 if (bandwidth_bps >= threshold.high_bandwidth_bps) |
| 106 return threshold.high_bandwidth_packet_loss; |
| 107 return threshold_info.offset + threshold_info.slope * bandwidth_bps; |
| 108 } |
| 109 |
| 110 bool FecController::FecEnablingDecision(const NetworkMetrics& metrics) const { |
| 111 if (!metrics.uplink_bandwidth_bps) |
| 112 return false; |
| 113 |
| 114 auto packet_loss = packet_loss_smoothed_->GetAverage(); |
| 115 if (!packet_loss) |
| 116 return false; |
| 117 |
| 118 return *packet_loss >= GetPacketLossThreshold(*metrics.uplink_bandwidth_bps, |
| 119 config_.fec_enabling_threshold, |
| 120 fec_enabling_threshold_info_); |
| 121 } |
| 122 |
| 123 bool FecController::FecDisablingDecision(const NetworkMetrics& metrics) const { |
| 124 if (!metrics.uplink_bandwidth_bps) |
| 125 return false; |
| 126 |
| 127 auto packet_loss = packet_loss_smoothed_->GetAverage(); |
| 128 if (!packet_loss) |
| 129 return false; |
| 130 |
| 131 return *packet_loss <= GetPacketLossThreshold(*metrics.uplink_bandwidth_bps, |
| 132 config_.fec_disabling_threshold, |
| 133 fec_disabling_threshold_info_); |
| 134 } |
| 135 |
| 136 } // namespace webrtc |
OLD | NEW |