solenberg | 76377c5 | 2017-02-21 00:54:31 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017 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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "media/engine/apm_helpers.h" |
solenberg | 76377c5 | 2017-02-21 00:54:31 -0800 | [diff] [blame] | 12 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 13 | #include "modules/audio_processing/include/audio_processing.h" |
Fredrik Solenberg | 2a87797 | 2017-12-15 16:42:15 +0100 | [diff] [blame] | 14 | #include "rtc_base/scoped_ref_ptr.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 15 | #include "test/gmock.h" |
| 16 | #include "test/gtest.h" |
solenberg | 76377c5 | 2017-02-21 00:54:31 -0800 | [diff] [blame] | 17 | |
| 18 | namespace webrtc { |
| 19 | namespace { |
| 20 | |
| 21 | constexpr AgcConfig kDefaultAgcConfig = { 3, 9, true }; |
| 22 | |
| 23 | struct TestHelper { |
| 24 | TestHelper() { |
solenberg | 76377c5 | 2017-02-21 00:54:31 -0800 | [diff] [blame] | 25 | // This replicates the conditions from voe_auto_test. |
| 26 | Config config; |
| 27 | config.Set<ExperimentalAgc>(new ExperimentalAgc(false)); |
Ivo Creusen | 62337e5 | 2018-01-09 14:17:33 +0100 | [diff] [blame^] | 28 | apm_ = rtc::scoped_refptr<AudioProcessing>( |
| 29 | AudioProcessingBuilder().Create(config)); |
Fredrik Solenberg | 55900fd | 2017-11-23 20:22:55 +0100 | [diff] [blame] | 30 | apm_helpers::Init(apm()); |
solenberg | 76377c5 | 2017-02-21 00:54:31 -0800 | [diff] [blame] | 31 | } |
| 32 | |
peah | a9cc40b | 2017-06-29 08:32:09 -0700 | [diff] [blame] | 33 | AudioProcessing* apm() { return apm_.get(); } |
solenberg | 76377c5 | 2017-02-21 00:54:31 -0800 | [diff] [blame] | 34 | |
peah | a9cc40b | 2017-06-29 08:32:09 -0700 | [diff] [blame] | 35 | const AudioProcessing* apm() const { return apm_.get(); } |
solenberg | 76377c5 | 2017-02-21 00:54:31 -0800 | [diff] [blame] | 36 | |
solenberg | 76377c5 | 2017-02-21 00:54:31 -0800 | [diff] [blame] | 37 | bool GetEcMetricsStatus() const { |
| 38 | EchoCancellation* ec = apm()->echo_cancellation(); |
| 39 | bool metrics_enabled = ec->are_metrics_enabled(); |
| 40 | EXPECT_EQ(metrics_enabled, ec->is_delay_logging_enabled()); |
| 41 | return metrics_enabled; |
| 42 | } |
| 43 | |
| 44 | bool CanGetEcMetrics() const { |
| 45 | EchoCancellation* ec = apm()->echo_cancellation(); |
| 46 | EchoCancellation::Metrics metrics; |
| 47 | int metrics_result = ec->GetMetrics(&metrics); |
| 48 | int median = 0; |
| 49 | int std = 0; |
| 50 | float fraction = 0; |
| 51 | int delay_metrics_result = ec->GetDelayMetrics(&median, &std, &fraction); |
| 52 | return metrics_result == AudioProcessing::kNoError && |
| 53 | delay_metrics_result == AudioProcessing::kNoError; |
| 54 | } |
| 55 | |
| 56 | private: |
peah | a9cc40b | 2017-06-29 08:32:09 -0700 | [diff] [blame] | 57 | rtc::scoped_refptr<AudioProcessing> apm_; |
solenberg | 76377c5 | 2017-02-21 00:54:31 -0800 | [diff] [blame] | 58 | }; |
| 59 | } // namespace |
| 60 | |
| 61 | TEST(ApmHelpersTest, AgcConfig_DefaultConfiguration) { |
| 62 | TestHelper helper; |
| 63 | AgcConfig agc_config = |
| 64 | apm_helpers::GetAgcConfig(helper.apm()); |
| 65 | |
| 66 | EXPECT_EQ(kDefaultAgcConfig.targetLeveldBOv, agc_config.targetLeveldBOv); |
| 67 | EXPECT_EQ(kDefaultAgcConfig.digitalCompressionGaindB, |
| 68 | agc_config.digitalCompressionGaindB); |
| 69 | EXPECT_EQ(kDefaultAgcConfig.limiterEnable, agc_config.limiterEnable); |
| 70 | } |
| 71 | |
| 72 | TEST(ApmHelpersTest, AgcConfig_GetAndSet) { |
| 73 | const AgcConfig agc_config = { 11, 17, false }; |
| 74 | |
| 75 | TestHelper helper; |
| 76 | apm_helpers::SetAgcConfig(helper.apm(), agc_config); |
| 77 | AgcConfig actual_config = |
| 78 | apm_helpers::GetAgcConfig(helper.apm()); |
| 79 | |
| 80 | EXPECT_EQ(agc_config.digitalCompressionGaindB, |
| 81 | actual_config.digitalCompressionGaindB); |
| 82 | EXPECT_EQ(agc_config.limiterEnable, |
| 83 | actual_config.limiterEnable); |
| 84 | EXPECT_EQ(agc_config.targetLeveldBOv, |
| 85 | actual_config.targetLeveldBOv); |
| 86 | } |
| 87 | |
| 88 | TEST(ApmHelpersTest, AgcStatus_DefaultMode) { |
| 89 | TestHelper helper; |
| 90 | GainControl* gc = helper.apm()->gain_control(); |
Fredrik Solenberg | 55900fd | 2017-11-23 20:22:55 +0100 | [diff] [blame] | 91 | EXPECT_FALSE(gc->is_enabled()); |
oprypin | 4519752 | 2017-06-22 01:47:20 -0700 | [diff] [blame] | 92 | #if defined(TARGET_IPHONE_SIMULATOR) && TARGET_IPHONE_SIMULATOR |
Fredrik Solenberg | 55900fd | 2017-11-23 20:22:55 +0100 | [diff] [blame] | 93 | EXPECT_EQ(GainControl::kAdaptiveAnalog, gc->mode()); |
oprypin | 4519752 | 2017-06-22 01:47:20 -0700 | [diff] [blame] | 94 | #elif defined(WEBRTC_IOS) || defined(WEBRTC_ANDROID) |
solenberg | 76377c5 | 2017-02-21 00:54:31 -0800 | [diff] [blame] | 95 | EXPECT_EQ(GainControl::kFixedDigital, gc->mode()); |
| 96 | #else |
solenberg | 76377c5 | 2017-02-21 00:54:31 -0800 | [diff] [blame] | 97 | EXPECT_EQ(GainControl::kAdaptiveAnalog, gc->mode()); |
| 98 | #endif |
| 99 | } |
| 100 | |
| 101 | TEST(ApmHelpersTest, AgcStatus_EnableDisable) { |
| 102 | TestHelper helper; |
| 103 | GainControl* gc = helper.apm()->gain_control(); |
| 104 | #if defined(WEBRTC_IOS) || defined(WEBRTC_ANDROID) |
henrika | e26456a | 2017-12-13 14:08:48 +0100 | [diff] [blame] | 105 | apm_helpers::SetAgcStatus(helper.apm(), false); |
solenberg | 76377c5 | 2017-02-21 00:54:31 -0800 | [diff] [blame] | 106 | EXPECT_FALSE(gc->is_enabled()); |
| 107 | EXPECT_EQ(GainControl::kFixedDigital, gc->mode()); |
| 108 | |
henrika | e26456a | 2017-12-13 14:08:48 +0100 | [diff] [blame] | 109 | apm_helpers::SetAgcStatus(helper.apm(), true); |
solenberg | 76377c5 | 2017-02-21 00:54:31 -0800 | [diff] [blame] | 110 | EXPECT_TRUE(gc->is_enabled()); |
| 111 | EXPECT_EQ(GainControl::kFixedDigital, gc->mode()); |
| 112 | #else |
henrika | e26456a | 2017-12-13 14:08:48 +0100 | [diff] [blame] | 113 | apm_helpers::SetAgcStatus(helper.apm(), false); |
solenberg | 76377c5 | 2017-02-21 00:54:31 -0800 | [diff] [blame] | 114 | EXPECT_FALSE(gc->is_enabled()); |
| 115 | EXPECT_EQ(GainControl::kAdaptiveAnalog, gc->mode()); |
henrika | e26456a | 2017-12-13 14:08:48 +0100 | [diff] [blame] | 116 | apm_helpers::SetAgcStatus(helper.apm(), true); |
solenberg | 76377c5 | 2017-02-21 00:54:31 -0800 | [diff] [blame] | 117 | EXPECT_TRUE(gc->is_enabled()); |
| 118 | EXPECT_EQ(GainControl::kAdaptiveAnalog, gc->mode()); |
| 119 | #endif |
| 120 | } |
| 121 | |
| 122 | TEST(ApmHelpersTest, EcStatus_DefaultMode) { |
| 123 | TestHelper helper; |
| 124 | EchoCancellation* ec = helper.apm()->echo_cancellation(); |
| 125 | EchoControlMobile* ecm = helper.apm()->echo_control_mobile(); |
| 126 | EXPECT_FALSE(ec->is_enabled()); |
| 127 | EXPECT_FALSE(ecm->is_enabled()); |
| 128 | } |
| 129 | |
| 130 | TEST(ApmHelpersTest, EcStatus_EnableDisable) { |
| 131 | TestHelper helper; |
| 132 | EchoCancellation* ec = helper.apm()->echo_cancellation(); |
| 133 | EchoControlMobile* ecm = helper.apm()->echo_control_mobile(); |
| 134 | |
| 135 | apm_helpers::SetEcStatus(helper.apm(), true, kEcAecm); |
| 136 | EXPECT_FALSE(ec->is_enabled()); |
| 137 | EXPECT_TRUE(ecm->is_enabled()); |
| 138 | |
| 139 | apm_helpers::SetEcStatus(helper.apm(), false, kEcAecm); |
| 140 | EXPECT_FALSE(ec->is_enabled()); |
| 141 | EXPECT_FALSE(ecm->is_enabled()); |
| 142 | |
| 143 | apm_helpers::SetEcStatus(helper.apm(), true, kEcConference); |
| 144 | EXPECT_TRUE(ec->is_enabled()); |
| 145 | EXPECT_FALSE(ecm->is_enabled()); |
| 146 | EXPECT_EQ(EchoCancellation::kHighSuppression, ec->suppression_level()); |
| 147 | |
| 148 | apm_helpers::SetEcStatus(helper.apm(), false, kEcConference); |
| 149 | EXPECT_FALSE(ec->is_enabled()); |
| 150 | EXPECT_FALSE(ecm->is_enabled()); |
| 151 | EXPECT_EQ(EchoCancellation::kHighSuppression, ec->suppression_level()); |
| 152 | |
| 153 | apm_helpers::SetEcStatus(helper.apm(), true, kEcAecm); |
| 154 | EXPECT_FALSE(ec->is_enabled()); |
| 155 | EXPECT_TRUE(ecm->is_enabled()); |
| 156 | } |
| 157 | |
| 158 | TEST(ApmHelpersTest, EcMetrics_DefaultMode) { |
| 159 | TestHelper helper; |
| 160 | apm_helpers::SetEcStatus(helper.apm(), true, kEcConference); |
| 161 | EXPECT_TRUE(helper.GetEcMetricsStatus()); |
| 162 | } |
| 163 | |
| 164 | TEST(ApmHelpersTest, EcMetrics_CanEnableDisable) { |
| 165 | TestHelper helper; |
| 166 | apm_helpers::SetEcStatus(helper.apm(), true, kEcConference); |
| 167 | |
| 168 | apm_helpers::SetEcMetricsStatus(helper.apm(), true); |
| 169 | EXPECT_TRUE(helper.GetEcMetricsStatus()); |
| 170 | apm_helpers::SetEcMetricsStatus(helper.apm(), false); |
| 171 | EXPECT_FALSE(helper.GetEcMetricsStatus()); |
| 172 | } |
| 173 | |
| 174 | TEST(ApmHelpersTest, EcMetrics_NoStatsUnlessEcMetricsAndEcEnabled) { |
| 175 | TestHelper helper; |
| 176 | EXPECT_FALSE(helper.CanGetEcMetrics()); |
| 177 | |
| 178 | apm_helpers::SetEcMetricsStatus(helper.apm(), true); |
| 179 | EXPECT_FALSE(helper.CanGetEcMetrics()); |
| 180 | |
| 181 | apm_helpers::SetEcStatus(helper.apm(), true, kEcConference); |
| 182 | EXPECT_TRUE(helper.CanGetEcMetrics()); |
| 183 | |
| 184 | apm_helpers::SetEcMetricsStatus(helper.apm(), false); |
| 185 | EXPECT_FALSE(helper.CanGetEcMetrics()); |
| 186 | } |
| 187 | |
| 188 | TEST(ApmHelpersTest, AecmMode_DefaultMode) { |
| 189 | TestHelper helper; |
| 190 | EchoControlMobile* ecm = helper.apm()->echo_control_mobile(); |
| 191 | EXPECT_EQ(EchoControlMobile::kSpeakerphone, ecm->routing_mode()); |
| 192 | EXPECT_TRUE(ecm->is_comfort_noise_enabled()); |
| 193 | } |
| 194 | |
| 195 | TEST(ApmHelpersTest, AecmMode_EnableDisableCng) { |
| 196 | TestHelper helper; |
| 197 | EchoControlMobile* ecm = helper.apm()->echo_control_mobile(); |
| 198 | apm_helpers::SetAecmMode(helper.apm(), false); |
| 199 | EXPECT_FALSE(ecm->is_comfort_noise_enabled()); |
| 200 | apm_helpers::SetAecmMode(helper.apm(), true); |
| 201 | EXPECT_TRUE(ecm->is_comfort_noise_enabled()); |
| 202 | } |
| 203 | |
| 204 | TEST(ApmHelpersTest, NsStatus_DefaultMode) { |
| 205 | TestHelper helper; |
| 206 | NoiseSuppression* ns = helper.apm()->noise_suppression(); |
| 207 | EXPECT_EQ(NoiseSuppression::kModerate, ns->level()); |
| 208 | EXPECT_FALSE(ns->is_enabled()); |
| 209 | } |
| 210 | |
| 211 | TEST(ApmHelpersTest, NsStatus_EnableDisable) { |
| 212 | TestHelper helper; |
| 213 | NoiseSuppression* ns = helper.apm()->noise_suppression(); |
| 214 | apm_helpers::SetNsStatus(helper.apm(), true); |
| 215 | EXPECT_EQ(NoiseSuppression::kHigh, ns->level()); |
| 216 | EXPECT_TRUE(ns->is_enabled()); |
| 217 | apm_helpers::SetNsStatus(helper.apm(), false); |
| 218 | EXPECT_EQ(NoiseSuppression::kHigh, ns->level()); |
| 219 | EXPECT_FALSE(ns->is_enabled()); |
| 220 | } |
| 221 | |
| 222 | TEST(ApmHelpersTest, TypingDetectionStatus_DefaultMode) { |
| 223 | TestHelper helper; |
| 224 | VoiceDetection* vd = helper.apm()->voice_detection(); |
| 225 | EXPECT_FALSE(vd->is_enabled()); |
| 226 | } |
| 227 | |
Fredrik Solenberg | 2a87797 | 2017-12-15 16:42:15 +0100 | [diff] [blame] | 228 | TEST(ApmHelpersTest, TypingDetectionStatus_EnableDisable) { |
solenberg | 76377c5 | 2017-02-21 00:54:31 -0800 | [diff] [blame] | 229 | TestHelper helper; |
| 230 | VoiceDetection* vd = helper.apm()->voice_detection(); |
| 231 | apm_helpers::SetTypingDetectionStatus(helper.apm(), true); |
Fredrik Solenberg | 2a87797 | 2017-12-15 16:42:15 +0100 | [diff] [blame] | 232 | EXPECT_EQ(VoiceDetection::kVeryLowLikelihood, vd->likelihood()); |
solenberg | 76377c5 | 2017-02-21 00:54:31 -0800 | [diff] [blame] | 233 | EXPECT_TRUE(vd->is_enabled()); |
| 234 | apm_helpers::SetTypingDetectionStatus(helper.apm(), false); |
Fredrik Solenberg | 2a87797 | 2017-12-15 16:42:15 +0100 | [diff] [blame] | 235 | EXPECT_EQ(VoiceDetection::kVeryLowLikelihood, vd->likelihood()); |
solenberg | 76377c5 | 2017-02-21 00:54:31 -0800 | [diff] [blame] | 236 | EXPECT_FALSE(vd->is_enabled()); |
| 237 | } |
| 238 | |
| 239 | // TODO(solenberg): Move this test to a better place - added here for the sake |
| 240 | // of duplicating all relevant tests from audio_processing_test.cc. |
| 241 | TEST(ApmHelpersTest, HighPassFilter_DefaultMode) { |
| 242 | TestHelper helper; |
Fredrik Solenberg | 55900fd | 2017-11-23 20:22:55 +0100 | [diff] [blame] | 243 | EXPECT_FALSE(helper.apm()->high_pass_filter()->is_enabled()); |
solenberg | 76377c5 | 2017-02-21 00:54:31 -0800 | [diff] [blame] | 244 | } |
solenberg | 76377c5 | 2017-02-21 00:54:31 -0800 | [diff] [blame] | 245 | } // namespace webrtc |