| 1 |
/* |
| 2 |
* Copyright (c) 2020 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 "modules/audio_processing/aec3/transparent_mode.h" |
| 12 |
|
| 13 |
#include "rtc_base/checks.h" |
| 14 |
#include "system_wrappers/include/field_trial.h" |
| 15 |
|
| 16 |
namespace webrtc { |
| 17 |
namespace { |
| 18 |
|
| 19 |
constexpr size_t kBlocksSinceConvergencedFilterInit = 10000; |
| 20 |
constexpr size_t kBlocksSinceConsistentEstimateInit = 10000; |
| 21 |
|
| 22 |
bool DeactivateTransparentMode() { |
| 23 |
return field_trial::IsEnabled("WebRTC-Aec3TransparentModeKillSwitch"); |
| 24 |
} |
| 25 |
|
| 26 |
bool ActivateTransparentModeHmm() { |
| 27 |
return field_trial::IsEnabled("WebRTC-Aec3TransparentModeHmm"); |
| 28 |
} |
| 29 |
|
| 30 |
} // namespace |
| 31 |
|
| 32 |
// Classifier that toggles transparent mode which reduces echo suppression when |
| 33 |
// headsets are used. |
| 34 |
class TransparentModeImpl : public TransparentMode { |
| 35 |
public: |
| 36 |
bool Active() const override { return transparency_activated_; } |
| 37 |
|
| 38 |
void Reset() override { |
| 39 |
// Determines if transparent mode is used. |
| 40 |
transparency_activated_ = false; |
| 41 |
|
| 42 |
// The estimated probability of being transparent mode. |
| 43 |
prob_transparent_state_ = 0.f; |
| 44 |
} |
| 45 |
|
| 46 |
void Update(int filter_delay_blocks, |
| 47 |
bool any_filter_consistent, |
| 48 |
bool any_filter_converged, |
| 49 |
bool any_coarse_filter_converged, |
| 50 |
bool all_filters_diverged, |
| 51 |
bool active_render, |
| 52 |
bool saturated_capture) override { |
| 53 |
// The classifier is implemented as a Hidden Markov Model (HMM) with two |
| 54 |
// hidden states: "normal" and "transparent". The estimated probabilities of |
| 55 |
// the two states are updated by observing filter convergence during active |
| 56 |
// render. The filters are less likely to be reported as converged when |
| 57 |
// there is no echo present in the microphone signal. |
| 58 |
|
| 59 |
// The constants have been obtained by observing active_render and |
| 60 |
// any_coarse_filter_converged under varying call scenarios. They |
| 61 |
// have further been hand tuned to prefer normal state during uncertain |
| 62 |
// regions (to avoid echo leaks). |
| 63 |
|
| 64 |
// The model is only updated during active render. |
| 65 |
if (!active_render) |
| 66 |
return; |
| 67 |
|
| 68 |
// Probability of switching from one state to the other. |
| 69 |
constexpr float kSwitch = 0.000001f; |
| 70 |
|
| 71 |
// Probability of observing converged filters in states "normal" and |
| 72 |
// "transparent" during active render. |
| 73 |
constexpr float kConvergedNormal = 0.01f; |
| 74 |
constexpr float kConvergedTransparent = 0.001f; |
| 75 |
|
| 76 |
// Probability of transitioning to transparent state from normal state and |
| 77 |
// transparent state respectively. |
| 78 |
constexpr float kA[2] = {kSwitch, 1.f - kSwitch}; |
| 79 |
|
| 80 |
// Probability of the two observations (converged filter or not converged |
| 81 |
// filter) in normal state and transparent state respectively. |
| 82 |
constexpr float kB[2][2] = { |
| 83 |
{1.f - kConvergedNormal, kConvergedNormal}, |
| 84 |
{1.f - kConvergedTransparent, kConvergedTransparent}}; |
| 85 |
|
| 86 |
// Probability of the two states before the update. |
| 87 |
const float prob_transparent = prob_transparent_state_; |
| 88 |
const float prob_normal = 1.f - prob_transparent; |
| 89 |
|
| 90 |
// Probability of transitioning to transparent state. |
| 91 |
const float prob_transition_transparent = |
| 92 |
prob_normal * kA[0] + prob_transparent * kA[1]; |
| 93 |
const float prob_transition_normal = 1.f - prob_transition_transparent; |
| 94 |
|
| 95 |
// Observed output. |
| 96 |
const int out = static_cast<int>(any_coarse_filter_converged); |
| 97 |
|
| 98 |
// Joint probabilites of the observed output and respective states. |
| 99 |
const float prob_joint_normal = prob_transition_normal * kB[0][out]; |
| 100 |
const float prob_joint_transparent = |
| 101 |
prob_transition_transparent * kB[1][out]; |
| 102 |
|
| 103 |
// Conditional probability of transparent state and the observed output. |
| 104 |
RTC_DCHECK_GT(prob_joint_normal + prob_joint_transparent, 0.f); |
| 105 |
prob_transparent_state_ = |
| 106 |
prob_joint_transparent / (prob_joint_normal + prob_joint_transparent); |
| 107 |
|
| 108 |
// Transparent mode is only activated when its state probability is high. |
| 109 |
// Dead zone between activation/deactivation thresholds to avoid switching |
| 110 |
// back and forth. |
| 111 |
if (prob_transparent_state_ > 0.95f) { |
| 112 |
transparency_activated_ = true; |
| 113 |
} else if (prob_transparent_state_ < 0.5f) { |
| 114 |
transparency_activated_ = false; |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
private: |
| 119 |
bool transparency_activated_ = false; |
| 120 |
float prob_transparent_state_ = 0.f; |
| 121 |
}; |
| 122 |
|
| 123 |
// Legacy classifier for toggling transparent mode. |
| 124 |
class LegacyTransparentModeImpl : public TransparentMode { |
| 125 |
public: |
| 126 |
explicit LegacyTransparentModeImpl(const EchoCanceller3Config& config) |
| 127 |
: linear_and_stable_echo_path_( |
| 128 |
config.echo_removal_control.linear_and_stable_echo_path), |
| 129 |
active_blocks_since_sane_filter_(kBlocksSinceConsistentEstimateInit), |
| 130 |
non_converged_sequence_size_(kBlocksSinceConvergencedFilterInit) {} |
| 131 |
|
| 132 |
bool Active() const override { return transparency_activated_; } |
| 133 |
|
| 134 |
void Reset() override { |
| 135 |
non_converged_sequence_size_ = kBlocksSinceConvergencedFilterInit; |
| 136 |
diverged_sequence_size_ = 0; |
| 137 |
strong_not_saturated_render_blocks_ = 0; |
| 138 |
if (linear_and_stable_echo_path_) { |
| 139 |
recent_convergence_during_activity_ = false; |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
void Update(int filter_delay_blocks, |
| 144 |
bool any_filter_consistent, |
| 145 |
bool any_filter_converged, |
| 146 |
bool any_coarse_filter_converged, |
| 147 |
bool all_filters_diverged, |
| 148 |
bool active_render, |
| 149 |
bool saturated_capture) override { |
| 150 |
++capture_block_counter_; |
| 151 |
strong_not_saturated_render_blocks_ += |
| 152 |
active_render && !saturated_capture ? 1 : 0; |
| 153 |
|
| 154 |
if (any_filter_consistent && filter_delay_blocks < 5) { |
| 155 |
sane_filter_observed_ = true; |
| 156 |
active_blocks_since_sane_filter_ = 0; |
| 157 |
} else if (active_render) { |
| 158 |
++active_blocks_since_sane_filter_; |
| 159 |
} |
| 160 |
|
| 161 |
bool sane_filter_recently_seen; |
| 162 |
if (!sane_filter_observed_) { |
| 163 |
sane_filter_recently_seen = |
| 164 |
capture_block_counter_ <= 5 * kNumBlocksPerSecond; |
| 165 |
} else { |
| 166 |
sane_filter_recently_seen = |
| 167 |
active_blocks_since_sane_filter_ <= 30 * kNumBlocksPerSecond; |
| 168 |
} |
| 169 |
|
| 170 |
if (any_filter_converged) { |
| 171 |
recent_convergence_during_activity_ = true; |
| 172 |
active_non_converged_sequence_size_ = 0; |
| 173 |
non_converged_sequence_size_ = 0; |
| 174 |
++num_converged_blocks_; |
| 175 |
} else { |
| 176 |
if (++non_converged_sequence_size_ > 20 * kNumBlocksPerSecond) { |
| 177 |
num_converged_blocks_ = 0; |
| 178 |
} |
| 179 |
|
| 180 |
if (active_render && |
| 181 |
++active_non_converged_sequence_size_ > 60 * kNumBlocksPerSecond) { |
| 182 |
recent_convergence_during_activity_ = false; |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
if (!all_filters_diverged) { |
| 187 |
diverged_sequence_size_ = 0; |
| 188 |
} else if (++diverged_sequence_size_ >= 60) { |
| 189 |
// TODO(peah): Change these lines to ensure proper triggering of usable |
| 190 |
// filter. |
| 191 |
non_converged_sequence_size_ = kBlocksSinceConvergencedFilterInit; |
| 192 |
} |
| 193 |
|
| 194 |
if (active_non_converged_sequence_size_ > 60 * kNumBlocksPerSecond) { |
| 195 |
finite_erl_recently_detected_ = false; |
| 196 |
} |
| 197 |
if (num_converged_blocks_ > 50) { |
| 198 |
finite_erl_recently_detected_ = true; |
| 199 |
} |
| 200 |
|
| 201 |
if (finite_erl_recently_detected_) { |
| 202 |
transparency_activated_ = false; |
| 203 |
} else if (sane_filter_recently_seen && |
| 204 |
recent_convergence_during_activity_) { |
| 205 |
transparency_activated_ = false; |
| 206 |
} else { |
| 207 |
const bool filter_should_have_converged = |
| 208 |
strong_not_saturated_render_blocks_ > 6 * kNumBlocksPerSecond; |
| 209 |
transparency_activated_ = filter_should_have_converged; |
| 210 |
} |
| 211 |
} |
| 212 |
|
| 213 |
private: |
| 214 |
const bool linear_and_stable_echo_path_; |
| 215 |
size_t capture_block_counter_ = 0; |
| 216 |
bool transparency_activated_ = false; |
| 217 |
size_t active_blocks_since_sane_filter_; |
| 218 |
bool sane_filter_observed_ = false; |
| 219 |
bool finite_erl_recently_detected_ = false; |
| 220 |
size_t non_converged_sequence_size_; |
| 221 |
size_t diverged_sequence_size_ = 0; |
| 222 |
size_t active_non_converged_sequence_size_ = 0; |
| 223 |
size_t num_converged_blocks_ = 0; |
| 224 |
bool recent_convergence_during_activity_ = false; |
| 225 |
size_t strong_not_saturated_render_blocks_ = 0; |
| 226 |
}; |
| 227 |
|
| 228 |
std::unique_ptr<TransparentMode> TransparentMode::Create( |
| 229 |
const EchoCanceller3Config& config) { |
| 230 |
if (config.ep_strength.bounded_erl || DeactivateTransparentMode()) { |
| 231 |
return nullptr; |
| 232 |
} |
| 233 |
if (ActivateTransparentModeHmm()) { |
| 234 |
return std::make_unique<TransparentModeImpl>(); |
| 235 |
} |
| 236 |
return std::make_unique<LegacyTransparentModeImpl>(config); |
| 237 |
} |
| 238 |
|
| 239 |
} // namespace webrtc |