peah | 1f1912d | 2015-11-09 11:13:19 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2015 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 04:47:31 | [diff] [blame] | 11 | #include "modules/audio_processing/audio_processing_impl.h" |
peah | 1f1912d | 2015-11-09 11:13:19 | [diff] [blame] | 12 | |
| 13 | #include <algorithm> |
kwiberg | 88788ad | 2016-02-19 15:04:49 | [diff] [blame] | 14 | #include <memory> |
peah | 1f1912d | 2015-11-09 11:13:19 | [diff] [blame] | 15 | #include <vector> |
| 16 | |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 17 | #include "api/array_view.h" |
| 18 | #include "modules/audio_processing/test/test_utils.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 19 | #include "rtc_base/criticalsection.h" |
| 20 | #include "rtc_base/event.h" |
| 21 | #include "rtc_base/platform_thread.h" |
| 22 | #include "rtc_base/random.h" |
| 23 | #include "system_wrappers/include/sleep.h" |
| 24 | #include "test/gtest.h" |
peah | 1f1912d | 2015-11-09 11:13:19 | [diff] [blame] | 25 | |
| 26 | namespace webrtc { |
| 27 | |
| 28 | namespace { |
| 29 | |
| 30 | class AudioProcessingImplLockTest; |
| 31 | |
peah | 1f1912d | 2015-11-09 11:13:19 | [diff] [blame] | 32 | // Type of the render thread APM API call to use in the test. |
| 33 | enum class RenderApiImpl { |
| 34 | ProcessReverseStreamImpl1, |
| 35 | ProcessReverseStreamImpl2, |
aluebs | b031955 | 2016-03-18 03:39:53 | [diff] [blame] | 36 | AnalyzeReverseStreamImpl |
peah | 1f1912d | 2015-11-09 11:13:19 | [diff] [blame] | 37 | }; |
| 38 | |
| 39 | // Type of the capture thread APM API call to use in the test. |
| 40 | enum class CaptureApiImpl { |
| 41 | ProcessStreamImpl1, |
| 42 | ProcessStreamImpl2, |
| 43 | ProcessStreamImpl3 |
| 44 | }; |
| 45 | |
| 46 | // The runtime parameter setting scheme to use in the test. |
| 47 | enum class RuntimeParameterSettingScheme { |
| 48 | SparseStreamMetadataChangeScheme, |
| 49 | ExtremeStreamMetadataChangeScheme, |
| 50 | FixedMonoStreamMetadataScheme, |
| 51 | FixedStereoStreamMetadataScheme |
| 52 | }; |
| 53 | |
| 54 | // Variant of echo canceller settings to use in the test. |
| 55 | enum class AecType { |
| 56 | BasicWebRtcAecSettings, |
| 57 | AecTurnedOff, |
| 58 | BasicWebRtcAecSettingsWithExtentedFilter, |
| 59 | BasicWebRtcAecSettingsWithDelayAgnosticAec, |
| 60 | BasicWebRtcAecSettingsWithAecMobile |
| 61 | }; |
| 62 | |
peah | df3efa8 | 2015-11-28 20:35:15 | [diff] [blame] | 63 | // Thread-safe random number generator wrapper. |
| 64 | class RandomGenerator { |
| 65 | public: |
| 66 | RandomGenerator() : rand_gen_(42U) {} |
| 67 | |
| 68 | int RandInt(int min, int max) { |
| 69 | rtc::CritScope cs(&crit_); |
| 70 | return rand_gen_.Rand(min, max); |
| 71 | } |
| 72 | |
| 73 | int RandInt(int max) { |
| 74 | rtc::CritScope cs(&crit_); |
| 75 | return rand_gen_.Rand(max); |
| 76 | } |
| 77 | |
| 78 | float RandFloat() { |
| 79 | rtc::CritScope cs(&crit_); |
| 80 | return rand_gen_.Rand<float>(); |
| 81 | } |
| 82 | |
| 83 | private: |
| 84 | rtc::CriticalSection crit_; |
danilchap | 56359be | 2017-09-07 14:53:45 | [diff] [blame] | 85 | Random rand_gen_ RTC_GUARDED_BY(crit_); |
peah | df3efa8 | 2015-11-28 20:35:15 | [diff] [blame] | 86 | }; |
| 87 | |
peah | 1f1912d | 2015-11-09 11:13:19 | [diff] [blame] | 88 | // Variables related to the audio data and formats. |
| 89 | struct AudioFrameData { |
| 90 | explicit AudioFrameData(int max_frame_size) { |
| 91 | // Set up the two-dimensional arrays needed for the APM API calls. |
| 92 | input_framechannels.resize(2 * max_frame_size); |
| 93 | input_frame.resize(2); |
| 94 | input_frame[0] = &input_framechannels[0]; |
| 95 | input_frame[1] = &input_framechannels[max_frame_size]; |
| 96 | |
| 97 | output_frame_channels.resize(2 * max_frame_size); |
| 98 | output_frame.resize(2); |
| 99 | output_frame[0] = &output_frame_channels[0]; |
| 100 | output_frame[1] = &output_frame_channels[max_frame_size]; |
| 101 | } |
| 102 | |
| 103 | AudioFrame frame; |
| 104 | std::vector<float*> output_frame; |
| 105 | std::vector<float> output_frame_channels; |
| 106 | AudioProcessing::ChannelLayout output_channel_layout = |
| 107 | AudioProcessing::ChannelLayout::kMono; |
| 108 | int input_sample_rate_hz = 16000; |
| 109 | int input_number_of_channels = -1; |
| 110 | std::vector<float*> input_frame; |
| 111 | std::vector<float> input_framechannels; |
| 112 | AudioProcessing::ChannelLayout input_channel_layout = |
| 113 | AudioProcessing::ChannelLayout::kMono; |
| 114 | int output_sample_rate_hz = 16000; |
| 115 | int output_number_of_channels = -1; |
| 116 | StreamConfig input_stream_config; |
| 117 | StreamConfig output_stream_config; |
| 118 | int input_samples_per_channel = -1; |
| 119 | int output_samples_per_channel = -1; |
| 120 | }; |
| 121 | |
| 122 | // The configuration for the test. |
| 123 | struct TestConfig { |
| 124 | // Test case generator for the test configurations to use in the brief tests. |
| 125 | static std::vector<TestConfig> GenerateBriefTestConfigs() { |
| 126 | std::vector<TestConfig> test_configs; |
| 127 | AecType aec_types[] = {AecType::BasicWebRtcAecSettingsWithDelayAgnosticAec, |
| 128 | AecType::BasicWebRtcAecSettingsWithAecMobile}; |
| 129 | for (auto aec_type : aec_types) { |
| 130 | TestConfig test_config; |
| 131 | test_config.aec_type = aec_type; |
| 132 | |
| 133 | test_config.min_number_of_calls = 300; |
| 134 | |
| 135 | // Perform tests only with the extreme runtime parameter setting scheme. |
| 136 | test_config.runtime_parameter_setting_scheme = |
| 137 | RuntimeParameterSettingScheme::ExtremeStreamMetadataChangeScheme; |
| 138 | |
| 139 | // Only test 16 kHz for this test suite. |
| 140 | test_config.initial_sample_rate_hz = 16000; |
| 141 | |
| 142 | // Create test config for the second processing API function set. |
| 143 | test_config.render_api_function = |
| 144 | RenderApiImpl::ProcessReverseStreamImpl2; |
| 145 | test_config.capture_api_function = CaptureApiImpl::ProcessStreamImpl2; |
| 146 | |
| 147 | // Create test config for the first processing API function set. |
| 148 | test_configs.push_back(test_config); |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 149 | test_config.render_api_function = RenderApiImpl::AnalyzeReverseStreamImpl; |
peah | 1f1912d | 2015-11-09 11:13:19 | [diff] [blame] | 150 | test_config.capture_api_function = CaptureApiImpl::ProcessStreamImpl3; |
| 151 | test_configs.push_back(test_config); |
| 152 | } |
| 153 | |
| 154 | // Return the created test configurations. |
| 155 | return test_configs; |
| 156 | } |
| 157 | |
| 158 | // Test case generator for the test configurations to use in the extensive |
| 159 | // tests. |
| 160 | static std::vector<TestConfig> GenerateExtensiveTestConfigs() { |
| 161 | // Lambda functions for the test config generation. |
| 162 | auto add_processing_apis = [](TestConfig test_config) { |
| 163 | struct AllowedApiCallCombinations { |
| 164 | RenderApiImpl render_api; |
| 165 | CaptureApiImpl capture_api; |
| 166 | }; |
| 167 | |
| 168 | const AllowedApiCallCombinations api_calls[] = { |
| 169 | {RenderApiImpl::ProcessReverseStreamImpl1, |
| 170 | CaptureApiImpl::ProcessStreamImpl1}, |
peah | 1f1912d | 2015-11-09 11:13:19 | [diff] [blame] | 171 | {RenderApiImpl::ProcessReverseStreamImpl2, |
| 172 | CaptureApiImpl::ProcessStreamImpl2}, |
| 173 | {RenderApiImpl::ProcessReverseStreamImpl2, |
| 174 | CaptureApiImpl::ProcessStreamImpl3}, |
aluebs | b031955 | 2016-03-18 03:39:53 | [diff] [blame] | 175 | {RenderApiImpl::AnalyzeReverseStreamImpl, |
peah | 1f1912d | 2015-11-09 11:13:19 | [diff] [blame] | 176 | CaptureApiImpl::ProcessStreamImpl2}, |
aluebs | b031955 | 2016-03-18 03:39:53 | [diff] [blame] | 177 | {RenderApiImpl::AnalyzeReverseStreamImpl, |
peah | 1f1912d | 2015-11-09 11:13:19 | [diff] [blame] | 178 | CaptureApiImpl::ProcessStreamImpl3}}; |
| 179 | std::vector<TestConfig> out; |
| 180 | for (auto api_call : api_calls) { |
| 181 | test_config.render_api_function = api_call.render_api; |
| 182 | test_config.capture_api_function = api_call.capture_api; |
| 183 | out.push_back(test_config); |
| 184 | } |
| 185 | return out; |
| 186 | }; |
| 187 | |
| 188 | auto add_aec_settings = [](const std::vector<TestConfig>& in) { |
| 189 | std::vector<TestConfig> out; |
| 190 | AecType aec_types[] = { |
| 191 | AecType::BasicWebRtcAecSettings, AecType::AecTurnedOff, |
| 192 | AecType::BasicWebRtcAecSettingsWithExtentedFilter, |
| 193 | AecType::BasicWebRtcAecSettingsWithDelayAgnosticAec, |
| 194 | AecType::BasicWebRtcAecSettingsWithAecMobile}; |
| 195 | for (auto test_config : in) { |
conceptgenesis | 3f70562 | 2016-01-30 22:40:44 | [diff] [blame] | 196 | // Due to a VisualStudio 2015 compiler issue, the internal loop |
| 197 | // variable here cannot override a previously defined name. |
| 198 | // In other words "type" cannot be named "aec_type" here. |
| 199 | // https://connect.microsoft.com/VisualStudio/feedback/details/2291755 |
| 200 | for (auto type : aec_types) { |
| 201 | test_config.aec_type = type; |
peah | 1f1912d | 2015-11-09 11:13:19 | [diff] [blame] | 202 | out.push_back(test_config); |
| 203 | } |
| 204 | } |
| 205 | return out; |
| 206 | }; |
| 207 | |
| 208 | auto add_settings_scheme = [](const std::vector<TestConfig>& in) { |
| 209 | std::vector<TestConfig> out; |
| 210 | RuntimeParameterSettingScheme schemes[] = { |
| 211 | RuntimeParameterSettingScheme::SparseStreamMetadataChangeScheme, |
| 212 | RuntimeParameterSettingScheme::ExtremeStreamMetadataChangeScheme, |
| 213 | RuntimeParameterSettingScheme::FixedMonoStreamMetadataScheme, |
| 214 | RuntimeParameterSettingScheme::FixedStereoStreamMetadataScheme}; |
| 215 | |
| 216 | for (auto test_config : in) { |
| 217 | for (auto scheme : schemes) { |
| 218 | test_config.runtime_parameter_setting_scheme = scheme; |
| 219 | out.push_back(test_config); |
| 220 | } |
| 221 | } |
| 222 | return out; |
| 223 | }; |
| 224 | |
| 225 | auto add_sample_rates = [](const std::vector<TestConfig>& in) { |
| 226 | const int sample_rates[] = {8000, 16000, 32000, 48000}; |
| 227 | |
| 228 | std::vector<TestConfig> out; |
| 229 | for (auto test_config : in) { |
| 230 | auto available_rates = |
| 231 | (test_config.aec_type == |
| 232 | AecType::BasicWebRtcAecSettingsWithAecMobile |
| 233 | ? rtc::ArrayView<const int>(sample_rates, 2) |
| 234 | : rtc::ArrayView<const int>(sample_rates)); |
| 235 | |
| 236 | for (auto rate : available_rates) { |
| 237 | test_config.initial_sample_rate_hz = rate; |
| 238 | out.push_back(test_config); |
| 239 | } |
| 240 | } |
| 241 | return out; |
| 242 | }; |
| 243 | |
| 244 | // Generate test configurations of the relevant combinations of the |
| 245 | // parameters to |
| 246 | // test. |
| 247 | TestConfig test_config; |
| 248 | test_config.min_number_of_calls = 10000; |
| 249 | return add_sample_rates(add_settings_scheme( |
| 250 | add_aec_settings(add_processing_apis(test_config)))); |
| 251 | } |
| 252 | |
| 253 | RenderApiImpl render_api_function = RenderApiImpl::ProcessReverseStreamImpl2; |
| 254 | CaptureApiImpl capture_api_function = CaptureApiImpl::ProcessStreamImpl2; |
| 255 | RuntimeParameterSettingScheme runtime_parameter_setting_scheme = |
| 256 | RuntimeParameterSettingScheme::ExtremeStreamMetadataChangeScheme; |
| 257 | int initial_sample_rate_hz = 16000; |
| 258 | AecType aec_type = AecType::BasicWebRtcAecSettingsWithDelayAgnosticAec; |
| 259 | int min_number_of_calls = 300; |
| 260 | }; |
| 261 | |
| 262 | // Handler for the frame counters. |
| 263 | class FrameCounters { |
| 264 | public: |
| 265 | void IncreaseRenderCounter() { |
| 266 | rtc::CritScope cs(&crit_); |
| 267 | render_count++; |
| 268 | } |
| 269 | |
| 270 | void IncreaseCaptureCounter() { |
| 271 | rtc::CritScope cs(&crit_); |
| 272 | capture_count++; |
| 273 | } |
| 274 | |
peah | 631e134 | 2015-12-03 09:15:27 | [diff] [blame] | 275 | int GetCaptureCounter() const { |
peah | 1f1912d | 2015-11-09 11:13:19 | [diff] [blame] | 276 | rtc::CritScope cs(&crit_); |
| 277 | return capture_count; |
| 278 | } |
| 279 | |
peah | 631e134 | 2015-12-03 09:15:27 | [diff] [blame] | 280 | int GetRenderCounter() const { |
peah | 1f1912d | 2015-11-09 11:13:19 | [diff] [blame] | 281 | rtc::CritScope cs(&crit_); |
| 282 | return render_count; |
| 283 | } |
| 284 | |
peah | 631e134 | 2015-12-03 09:15:27 | [diff] [blame] | 285 | int CaptureMinusRenderCounters() const { |
peah | 1f1912d | 2015-11-09 11:13:19 | [diff] [blame] | 286 | rtc::CritScope cs(&crit_); |
| 287 | return capture_count - render_count; |
| 288 | } |
| 289 | |
peah | 631e134 | 2015-12-03 09:15:27 | [diff] [blame] | 290 | int RenderMinusCaptureCounters() const { |
| 291 | return -CaptureMinusRenderCounters(); |
| 292 | } |
| 293 | |
peah | 1f1912d | 2015-11-09 11:13:19 | [diff] [blame] | 294 | bool BothCountersExceedeThreshold(int threshold) { |
| 295 | rtc::CritScope cs(&crit_); |
| 296 | return (render_count > threshold && capture_count > threshold); |
| 297 | } |
| 298 | |
| 299 | private: |
pbos | 5ad935c | 2016-01-25 11:52:44 | [diff] [blame] | 300 | rtc::CriticalSection crit_; |
danilchap | 56359be | 2017-09-07 14:53:45 | [diff] [blame] | 301 | int render_count RTC_GUARDED_BY(crit_) = 0; |
| 302 | int capture_count RTC_GUARDED_BY(crit_) = 0; |
peah | 1f1912d | 2015-11-09 11:13:19 | [diff] [blame] | 303 | }; |
| 304 | |
peah | 1f1912d | 2015-11-09 11:13:19 | [diff] [blame] | 305 | // Class for handling the capture side processing. |
| 306 | class CaptureProcessor { |
| 307 | public: |
| 308 | CaptureProcessor(int max_frame_size, |
peah | df3efa8 | 2015-11-28 20:35:15 | [diff] [blame] | 309 | RandomGenerator* rand_gen, |
peah | 631e134 | 2015-12-03 09:15:27 | [diff] [blame] | 310 | rtc::Event* render_call_event, |
| 311 | rtc::Event* capture_call_event, |
peah | 1f1912d | 2015-11-09 11:13:19 | [diff] [blame] | 312 | FrameCounters* shared_counters_state, |
peah | 1f1912d | 2015-11-09 11:13:19 | [diff] [blame] | 313 | AudioProcessingImplLockTest* test_framework, |
| 314 | TestConfig* test_config, |
| 315 | AudioProcessing* apm); |
| 316 | bool Process(); |
| 317 | |
| 318 | private: |
| 319 | static const int kMaxCallDifference = 10; |
| 320 | static const float kCaptureInputFloatLevel; |
| 321 | static const int kCaptureInputFixLevel = 1024; |
| 322 | |
| 323 | void PrepareFrame(); |
| 324 | void CallApmCaptureSide(); |
| 325 | void ApplyRuntimeSettingScheme(); |
| 326 | |
peah | 631e134 | 2015-12-03 09:15:27 | [diff] [blame] | 327 | RandomGenerator* const rand_gen_ = nullptr; |
| 328 | rtc::Event* const render_call_event_ = nullptr; |
| 329 | rtc::Event* const capture_call_event_ = nullptr; |
| 330 | FrameCounters* const frame_counters_ = nullptr; |
| 331 | AudioProcessingImplLockTest* const test_ = nullptr; |
| 332 | const TestConfig* const test_config_ = nullptr; |
| 333 | AudioProcessing* const apm_ = nullptr; |
peah | 1f1912d | 2015-11-09 11:13:19 | [diff] [blame] | 334 | AudioFrameData frame_data_; |
| 335 | }; |
| 336 | |
| 337 | // Class for handling the stats processing. |
| 338 | class StatsProcessor { |
| 339 | public: |
peah | df3efa8 | 2015-11-28 20:35:15 | [diff] [blame] | 340 | StatsProcessor(RandomGenerator* rand_gen, |
peah | 1f1912d | 2015-11-09 11:13:19 | [diff] [blame] | 341 | TestConfig* test_config, |
| 342 | AudioProcessing* apm); |
| 343 | bool Process(); |
| 344 | |
| 345 | private: |
peah | df3efa8 | 2015-11-28 20:35:15 | [diff] [blame] | 346 | RandomGenerator* rand_gen_ = nullptr; |
peah | 1f1912d | 2015-11-09 11:13:19 | [diff] [blame] | 347 | TestConfig* test_config_ = nullptr; |
| 348 | AudioProcessing* apm_ = nullptr; |
| 349 | }; |
| 350 | |
| 351 | // Class for handling the render side processing. |
| 352 | class RenderProcessor { |
| 353 | public: |
| 354 | RenderProcessor(int max_frame_size, |
peah | df3efa8 | 2015-11-28 20:35:15 | [diff] [blame] | 355 | RandomGenerator* rand_gen, |
peah | 631e134 | 2015-12-03 09:15:27 | [diff] [blame] | 356 | rtc::Event* render_call_event, |
| 357 | rtc::Event* capture_call_event, |
peah | 1f1912d | 2015-11-09 11:13:19 | [diff] [blame] | 358 | FrameCounters* shared_counters_state, |
peah | 1f1912d | 2015-11-09 11:13:19 | [diff] [blame] | 359 | AudioProcessingImplLockTest* test_framework, |
| 360 | TestConfig* test_config, |
| 361 | AudioProcessing* apm); |
| 362 | bool Process(); |
| 363 | |
| 364 | private: |
| 365 | static const int kMaxCallDifference = 10; |
| 366 | static const int kRenderInputFixLevel = 16384; |
| 367 | static const float kRenderInputFloatLevel; |
| 368 | |
| 369 | void PrepareFrame(); |
| 370 | void CallApmRenderSide(); |
| 371 | void ApplyRuntimeSettingScheme(); |
| 372 | |
peah | 631e134 | 2015-12-03 09:15:27 | [diff] [blame] | 373 | RandomGenerator* const rand_gen_ = nullptr; |
| 374 | rtc::Event* const render_call_event_ = nullptr; |
| 375 | rtc::Event* const capture_call_event_ = nullptr; |
| 376 | FrameCounters* const frame_counters_ = nullptr; |
| 377 | AudioProcessingImplLockTest* const test_ = nullptr; |
| 378 | const TestConfig* const test_config_ = nullptr; |
| 379 | AudioProcessing* const apm_ = nullptr; |
peah | 1f1912d | 2015-11-09 11:13:19 | [diff] [blame] | 380 | AudioFrameData frame_data_; |
peah | 631e134 | 2015-12-03 09:15:27 | [diff] [blame] | 381 | bool first_render_call_ = true; |
peah | 1f1912d | 2015-11-09 11:13:19 | [diff] [blame] | 382 | }; |
| 383 | |
| 384 | class AudioProcessingImplLockTest |
| 385 | : public ::testing::TestWithParam<TestConfig> { |
| 386 | public: |
| 387 | AudioProcessingImplLockTest(); |
peah | 631e134 | 2015-12-03 09:15:27 | [diff] [blame] | 388 | bool RunTest(); |
| 389 | bool MaybeEndTest(); |
peah | 1f1912d | 2015-11-09 11:13:19 | [diff] [blame] | 390 | |
| 391 | private: |
| 392 | static const int kTestTimeOutLimit = 10 * 60 * 1000; |
| 393 | static const int kMaxFrameSize = 480; |
| 394 | |
| 395 | // ::testing::TestWithParam<> implementation |
| 396 | void SetUp() override; |
| 397 | void TearDown() override; |
| 398 | |
| 399 | // Thread callback for the render thread |
| 400 | static bool RenderProcessorThreadFunc(void* context) { |
| 401 | return reinterpret_cast<AudioProcessingImplLockTest*>(context) |
| 402 | ->render_thread_state_.Process(); |
| 403 | } |
| 404 | |
| 405 | // Thread callback for the capture thread |
| 406 | static bool CaptureProcessorThreadFunc(void* context) { |
| 407 | return reinterpret_cast<AudioProcessingImplLockTest*>(context) |
| 408 | ->capture_thread_state_.Process(); |
| 409 | } |
| 410 | |
| 411 | // Thread callback for the stats thread |
| 412 | static bool StatsProcessorThreadFunc(void* context) { |
| 413 | return reinterpret_cast<AudioProcessingImplLockTest*>(context) |
| 414 | ->stats_thread_state_.Process(); |
| 415 | } |
| 416 | |
| 417 | // Tests whether all the required render and capture side calls have been |
| 418 | // done. |
| 419 | bool TestDone() { |
| 420 | return frame_counters_.BothCountersExceedeThreshold( |
| 421 | test_config_.min_number_of_calls); |
| 422 | } |
| 423 | |
| 424 | // Start the threads used in the test. |
| 425 | void StartThreads() { |
Peter Boström | 8c38e8b | 2015-11-26 16:45:47 | [diff] [blame] | 426 | render_thread_.Start(); |
| 427 | render_thread_.SetPriority(rtc::kRealtimePriority); |
| 428 | capture_thread_.Start(); |
| 429 | capture_thread_.SetPriority(rtc::kRealtimePriority); |
| 430 | stats_thread_.Start(); |
| 431 | stats_thread_.SetPriority(rtc::kNormalPriority); |
peah | 1f1912d | 2015-11-09 11:13:19 | [diff] [blame] | 432 | } |
| 433 | |
peah | 631e134 | 2015-12-03 09:15:27 | [diff] [blame] | 434 | // Event handlers for the test. |
| 435 | rtc::Event test_complete_; |
| 436 | rtc::Event render_call_event_; |
| 437 | rtc::Event capture_call_event_; |
peah | 1f1912d | 2015-11-09 11:13:19 | [diff] [blame] | 438 | |
| 439 | // Thread related variables. |
Peter Boström | 8c38e8b | 2015-11-26 16:45:47 | [diff] [blame] | 440 | rtc::PlatformThread render_thread_; |
| 441 | rtc::PlatformThread capture_thread_; |
| 442 | rtc::PlatformThread stats_thread_; |
peah | df3efa8 | 2015-11-28 20:35:15 | [diff] [blame] | 443 | mutable RandomGenerator rand_gen_; |
peah | 1f1912d | 2015-11-09 11:13:19 | [diff] [blame] | 444 | |
kwiberg | 88788ad | 2016-02-19 15:04:49 | [diff] [blame] | 445 | std::unique_ptr<AudioProcessing> apm_; |
peah | 1f1912d | 2015-11-09 11:13:19 | [diff] [blame] | 446 | TestConfig test_config_; |
| 447 | FrameCounters frame_counters_; |
peah | 1f1912d | 2015-11-09 11:13:19 | [diff] [blame] | 448 | RenderProcessor render_thread_state_; |
| 449 | CaptureProcessor capture_thread_state_; |
| 450 | StatsProcessor stats_thread_state_; |
| 451 | }; |
| 452 | |
peah | df3efa8 | 2015-11-28 20:35:15 | [diff] [blame] | 453 | // Sleeps a random time between 0 and max_sleep milliseconds. |
| 454 | void SleepRandomMs(int max_sleep, RandomGenerator* rand_gen) { |
| 455 | int sleeptime = rand_gen->RandInt(0, max_sleep); |
| 456 | SleepMs(sleeptime); |
| 457 | } |
| 458 | |
| 459 | // Populates a float audio frame with random data. |
| 460 | void PopulateAudioFrame(float** frame, |
| 461 | float amplitude, |
| 462 | size_t num_channels, |
| 463 | size_t samples_per_channel, |
| 464 | RandomGenerator* rand_gen) { |
| 465 | for (size_t ch = 0; ch < num_channels; ch++) { |
| 466 | for (size_t k = 0; k < samples_per_channel; k++) { |
| 467 | // Store random 16 bit quantized float number between +-amplitude. |
| 468 | frame[ch][k] = amplitude * (2 * rand_gen->RandFloat() - 1); |
| 469 | } |
| 470 | } |
| 471 | } |
| 472 | |
| 473 | // Populates an audioframe frame of AudioFrame type with random data. |
| 474 | void PopulateAudioFrame(AudioFrame* frame, |
| 475 | int16_t amplitude, |
| 476 | RandomGenerator* rand_gen) { |
| 477 | ASSERT_GT(amplitude, 0); |
| 478 | ASSERT_LE(amplitude, 32767); |
yujo | 36b1a5f | 2017-06-12 19:45:32 | [diff] [blame] | 479 | int16_t* frame_data = frame->mutable_data(); |
Peter Kasting | 6955870 | 2016-01-13 00:26:35 | [diff] [blame] | 480 | for (size_t ch = 0; ch < frame->num_channels_; ch++) { |
pkasting | 25702cb | 2016-01-08 21:50:27 | [diff] [blame] | 481 | for (size_t k = 0; k < frame->samples_per_channel_; k++) { |
peah | df3efa8 | 2015-11-28 20:35:15 | [diff] [blame] | 482 | // Store random 16 bit number between -(amplitude+1) and |
| 483 | // amplitude. |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 484 | frame_data[k * ch] = rand_gen->RandInt(2 * amplitude + 1) - amplitude - 1; |
peah | df3efa8 | 2015-11-28 20:35:15 | [diff] [blame] | 485 | } |
| 486 | } |
| 487 | } |
| 488 | |
peah | 1f1912d | 2015-11-09 11:13:19 | [diff] [blame] | 489 | AudioProcessingImplLockTest::AudioProcessingImplLockTest() |
Niels Möller | c572ff3 | 2018-11-07 07:43:50 | [diff] [blame] | 490 | : render_thread_(RenderProcessorThreadFunc, this, "render"), |
Peter Boström | 8c38e8b | 2015-11-26 16:45:47 | [diff] [blame] | 491 | capture_thread_(CaptureProcessorThreadFunc, this, "capture"), |
| 492 | stats_thread_(StatsProcessorThreadFunc, this, "stats"), |
Ivo Creusen | 62337e5 | 2018-01-09 13:17:33 | [diff] [blame] | 493 | apm_(AudioProcessingBuilder().Create()), |
peah | 1f1912d | 2015-11-09 11:13:19 | [diff] [blame] | 494 | render_thread_state_(kMaxFrameSize, |
| 495 | &rand_gen_, |
peah | 631e134 | 2015-12-03 09:15:27 | [diff] [blame] | 496 | &render_call_event_, |
| 497 | &capture_call_event_, |
peah | 1f1912d | 2015-11-09 11:13:19 | [diff] [blame] | 498 | &frame_counters_, |
peah | 1f1912d | 2015-11-09 11:13:19 | [diff] [blame] | 499 | this, |
| 500 | &test_config_, |
| 501 | apm_.get()), |
| 502 | capture_thread_state_(kMaxFrameSize, |
| 503 | &rand_gen_, |
peah | 631e134 | 2015-12-03 09:15:27 | [diff] [blame] | 504 | &render_call_event_, |
| 505 | &capture_call_event_, |
peah | 1f1912d | 2015-11-09 11:13:19 | [diff] [blame] | 506 | &frame_counters_, |
peah | 1f1912d | 2015-11-09 11:13:19 | [diff] [blame] | 507 | this, |
| 508 | &test_config_, |
| 509 | apm_.get()), |
| 510 | stats_thread_state_(&rand_gen_, &test_config_, apm_.get()) {} |
| 511 | |
| 512 | // Run the test with a timeout. |
peah | 631e134 | 2015-12-03 09:15:27 | [diff] [blame] | 513 | bool AudioProcessingImplLockTest::RunTest() { |
peah | 1f1912d | 2015-11-09 11:13:19 | [diff] [blame] | 514 | StartThreads(); |
peah | 631e134 | 2015-12-03 09:15:27 | [diff] [blame] | 515 | return test_complete_.Wait(kTestTimeOutLimit); |
peah | 1f1912d | 2015-11-09 11:13:19 | [diff] [blame] | 516 | } |
| 517 | |
peah | 631e134 | 2015-12-03 09:15:27 | [diff] [blame] | 518 | bool AudioProcessingImplLockTest::MaybeEndTest() { |
peah | 1f1912d | 2015-11-09 11:13:19 | [diff] [blame] | 519 | if (HasFatalFailure() || TestDone()) { |
peah | 631e134 | 2015-12-03 09:15:27 | [diff] [blame] | 520 | test_complete_.Set(); |
| 521 | return true; |
peah | 1f1912d | 2015-11-09 11:13:19 | [diff] [blame] | 522 | } |
peah | 631e134 | 2015-12-03 09:15:27 | [diff] [blame] | 523 | return false; |
peah | 1f1912d | 2015-11-09 11:13:19 | [diff] [blame] | 524 | } |
| 525 | |
| 526 | // Setup of test and APM. |
| 527 | void AudioProcessingImplLockTest::SetUp() { |
| 528 | test_config_ = static_cast<TestConfig>(GetParam()); |
| 529 | |
| 530 | ASSERT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(true)); |
| 531 | ASSERT_EQ(apm_->kNoError, apm_->gain_control()->Enable(true)); |
| 532 | |
| 533 | ASSERT_EQ(apm_->kNoError, |
peah | df3efa8 | 2015-11-28 20:35:15 | [diff] [blame] | 534 | apm_->gain_control()->set_mode(GainControl::kAdaptiveDigital)); |
peah | 1f1912d | 2015-11-09 11:13:19 | [diff] [blame] | 535 | ASSERT_EQ(apm_->kNoError, apm_->gain_control()->Enable(true)); |
| 536 | |
| 537 | ASSERT_EQ(apm_->kNoError, apm_->noise_suppression()->Enable(true)); |
| 538 | ASSERT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(true)); |
| 539 | |
Sam Zackrisson | cdf0e6d | 2018-09-17 09:05:17 | [diff] [blame] | 540 | AudioProcessing::Config apm_config; |
| 541 | apm_config.echo_canceller.enabled = |
| 542 | (test_config_.aec_type != AecType::AecTurnedOff); |
| 543 | apm_config.echo_canceller.mobile_mode = |
| 544 | (test_config_.aec_type == AecType::BasicWebRtcAecSettingsWithAecMobile); |
| 545 | apm_->ApplyConfig(apm_config); |
| 546 | |
peah | 1f1912d | 2015-11-09 11:13:19 | [diff] [blame] | 547 | Config config; |
Sam Zackrisson | cdf0e6d | 2018-09-17 09:05:17 | [diff] [blame] | 548 | config.Set<ExtendedFilter>( |
| 549 | new ExtendedFilter(test_config_.aec_type == |
| 550 | AecType::BasicWebRtcAecSettingsWithExtentedFilter)); |
peah | 1f1912d | 2015-11-09 11:13:19 | [diff] [blame] | 551 | |
Sam Zackrisson | cdf0e6d | 2018-09-17 09:05:17 | [diff] [blame] | 552 | config.Set<DelayAgnostic>( |
| 553 | new DelayAgnostic(test_config_.aec_type == |
| 554 | AecType::BasicWebRtcAecSettingsWithDelayAgnosticAec)); |
peah | 1f1912d | 2015-11-09 11:13:19 | [diff] [blame] | 555 | |
Sam Zackrisson | cdf0e6d | 2018-09-17 09:05:17 | [diff] [blame] | 556 | apm_->SetExtraOptions(config); |
peah | 1f1912d | 2015-11-09 11:13:19 | [diff] [blame] | 557 | } |
| 558 | |
| 559 | void AudioProcessingImplLockTest::TearDown() { |
peah | 631e134 | 2015-12-03 09:15:27 | [diff] [blame] | 560 | render_call_event_.Set(); |
| 561 | capture_call_event_.Set(); |
Peter Boström | 8c38e8b | 2015-11-26 16:45:47 | [diff] [blame] | 562 | render_thread_.Stop(); |
| 563 | capture_thread_.Stop(); |
| 564 | stats_thread_.Stop(); |
peah | 1f1912d | 2015-11-09 11:13:19 | [diff] [blame] | 565 | } |
| 566 | |
peah | df3efa8 | 2015-11-28 20:35:15 | [diff] [blame] | 567 | StatsProcessor::StatsProcessor(RandomGenerator* rand_gen, |
peah | 1f1912d | 2015-11-09 11:13:19 | [diff] [blame] | 568 | TestConfig* test_config, |
| 569 | AudioProcessing* apm) |
| 570 | : rand_gen_(rand_gen), test_config_(test_config), apm_(apm) {} |
| 571 | |
| 572 | // Implements the callback functionality for the statistics |
| 573 | // collection thread. |
| 574 | bool StatsProcessor::Process() { |
| 575 | SleepRandomMs(100, rand_gen_); |
| 576 | |
Sam Zackrisson | cdf0e6d | 2018-09-17 09:05:17 | [diff] [blame] | 577 | AudioProcessing::Config apm_config = apm_->GetConfig(); |
| 578 | if (test_config_->aec_type != AecType::AecTurnedOff) { |
| 579 | EXPECT_TRUE(apm_config.echo_canceller.enabled); |
| 580 | EXPECT_EQ(apm_config.echo_canceller.mobile_mode, |
| 581 | (test_config_->aec_type == |
| 582 | AecType::BasicWebRtcAecSettingsWithAecMobile)); |
| 583 | } else { |
| 584 | EXPECT_FALSE(apm_config.echo_canceller.enabled); |
| 585 | } |
peah | 1f1912d | 2015-11-09 11:13:19 | [diff] [blame] | 586 | EXPECT_TRUE(apm_->gain_control()->is_enabled()); |
peah | 1f1912d | 2015-11-09 11:13:19 | [diff] [blame] | 587 | EXPECT_TRUE(apm_->noise_suppression()->is_enabled()); |
| 588 | |
| 589 | // The below return values are not testable. |
| 590 | apm_->noise_suppression()->speech_probability(); |
| 591 | apm_->voice_detection()->is_enabled(); |
| 592 | |
| 593 | return true; |
| 594 | } |
| 595 | |
| 596 | const float CaptureProcessor::kCaptureInputFloatLevel = 0.03125f; |
| 597 | |
peah | 631e134 | 2015-12-03 09:15:27 | [diff] [blame] | 598 | CaptureProcessor::CaptureProcessor(int max_frame_size, |
| 599 | RandomGenerator* rand_gen, |
| 600 | rtc::Event* render_call_event, |
| 601 | rtc::Event* capture_call_event, |
| 602 | FrameCounters* shared_counters_state, |
| 603 | AudioProcessingImplLockTest* test_framework, |
| 604 | TestConfig* test_config, |
| 605 | AudioProcessing* apm) |
peah | 1f1912d | 2015-11-09 11:13:19 | [diff] [blame] | 606 | : rand_gen_(rand_gen), |
peah | 631e134 | 2015-12-03 09:15:27 | [diff] [blame] | 607 | render_call_event_(render_call_event), |
| 608 | capture_call_event_(capture_call_event), |
peah | 1f1912d | 2015-11-09 11:13:19 | [diff] [blame] | 609 | frame_counters_(shared_counters_state), |
peah | 1f1912d | 2015-11-09 11:13:19 | [diff] [blame] | 610 | test_(test_framework), |
| 611 | test_config_(test_config), |
| 612 | apm_(apm), |
| 613 | frame_data_(max_frame_size) {} |
| 614 | |
| 615 | // Implements the callback functionality for the capture thread. |
| 616 | bool CaptureProcessor::Process() { |
| 617 | // Sleep a random time to simulate thread jitter. |
| 618 | SleepRandomMs(3, rand_gen_); |
| 619 | |
peah | 631e134 | 2015-12-03 09:15:27 | [diff] [blame] | 620 | // Check whether the test is done. |
| 621 | if (test_->MaybeEndTest()) { |
| 622 | return false; |
| 623 | } |
peah | 1f1912d | 2015-11-09 11:13:19 | [diff] [blame] | 624 | |
peah | 631e134 | 2015-12-03 09:15:27 | [diff] [blame] | 625 | // Ensure that the number of render and capture calls do not |
| 626 | // differ too much. |
| 627 | if (frame_counters_->CaptureMinusRenderCounters() > kMaxCallDifference) { |
| 628 | render_call_event_->Wait(rtc::Event::kForever); |
peah | 1f1912d | 2015-11-09 11:13:19 | [diff] [blame] | 629 | } |
| 630 | |
| 631 | // Apply any specified capture side APM non-processing runtime calls. |
| 632 | ApplyRuntimeSettingScheme(); |
| 633 | |
| 634 | // Apply the capture side processing call. |
| 635 | CallApmCaptureSide(); |
| 636 | |
| 637 | // Increase the number of capture-side calls. |
| 638 | frame_counters_->IncreaseCaptureCounter(); |
| 639 | |
peah | 631e134 | 2015-12-03 09:15:27 | [diff] [blame] | 640 | // Flag to the render thread that another capture API call has occurred |
| 641 | // by triggering this threads call event. |
| 642 | capture_call_event_->Set(); |
peah | 1f1912d | 2015-11-09 11:13:19 | [diff] [blame] | 643 | |
| 644 | return true; |
| 645 | } |
| 646 | |
| 647 | // Prepares a frame with relevant audio data and metadata. |
| 648 | void CaptureProcessor::PrepareFrame() { |
| 649 | // Restrict to a common fixed sample rate if the AudioFrame |
| 650 | // interface is used. |
| 651 | if (test_config_->capture_api_function == |
| 652 | CaptureApiImpl::ProcessStreamImpl1) { |
| 653 | frame_data_.input_sample_rate_hz = test_config_->initial_sample_rate_hz; |
| 654 | frame_data_.output_sample_rate_hz = test_config_->initial_sample_rate_hz; |
| 655 | } |
| 656 | |
| 657 | // Prepare the audioframe data and metadata. |
| 658 | frame_data_.input_samples_per_channel = |
| 659 | frame_data_.input_sample_rate_hz * AudioProcessing::kChunkSizeMs / 1000; |
| 660 | frame_data_.frame.sample_rate_hz_ = frame_data_.input_sample_rate_hz; |
| 661 | frame_data_.frame.num_channels_ = frame_data_.input_number_of_channels; |
| 662 | frame_data_.frame.samples_per_channel_ = |
| 663 | frame_data_.input_samples_per_channel; |
| 664 | PopulateAudioFrame(&frame_data_.frame, kCaptureInputFixLevel, rand_gen_); |
| 665 | |
| 666 | // Prepare the float audio input data and metadata. |
| 667 | frame_data_.input_stream_config.set_sample_rate_hz( |
| 668 | frame_data_.input_sample_rate_hz); |
| 669 | frame_data_.input_stream_config.set_num_channels( |
| 670 | frame_data_.input_number_of_channels); |
| 671 | frame_data_.input_stream_config.set_has_keyboard(false); |
| 672 | PopulateAudioFrame(&frame_data_.input_frame[0], kCaptureInputFloatLevel, |
| 673 | frame_data_.input_number_of_channels, |
| 674 | frame_data_.input_samples_per_channel, rand_gen_); |
| 675 | frame_data_.input_channel_layout = |
| 676 | (frame_data_.input_number_of_channels == 1 |
| 677 | ? AudioProcessing::ChannelLayout::kMono |
| 678 | : AudioProcessing::ChannelLayout::kStereo); |
| 679 | |
| 680 | // Prepare the float audio output data and metadata. |
| 681 | frame_data_.output_samples_per_channel = |
| 682 | frame_data_.output_sample_rate_hz * AudioProcessing::kChunkSizeMs / 1000; |
| 683 | frame_data_.output_stream_config.set_sample_rate_hz( |
| 684 | frame_data_.output_sample_rate_hz); |
| 685 | frame_data_.output_stream_config.set_num_channels( |
| 686 | frame_data_.output_number_of_channels); |
| 687 | frame_data_.output_stream_config.set_has_keyboard(false); |
| 688 | frame_data_.output_channel_layout = |
| 689 | (frame_data_.output_number_of_channels == 1 |
| 690 | ? AudioProcessing::ChannelLayout::kMono |
| 691 | : AudioProcessing::ChannelLayout::kStereo); |
| 692 | } |
| 693 | |
| 694 | // Applies the capture side processing API call. |
| 695 | void CaptureProcessor::CallApmCaptureSide() { |
| 696 | // Prepare a proper capture side processing API call input. |
| 697 | PrepareFrame(); |
| 698 | |
peah | be61562 | 2016-02-14 00:40:47 | [diff] [blame] | 699 | // Set the stream delay. |
peah | 1f1912d | 2015-11-09 11:13:19 | [diff] [blame] | 700 | apm_->set_stream_delay_ms(30); |
| 701 | |
peah | be61562 | 2016-02-14 00:40:47 | [diff] [blame] | 702 | // Set the analog level. |
| 703 | apm_->gain_control()->set_stream_analog_level(80); |
| 704 | |
peah | 1f1912d | 2015-11-09 11:13:19 | [diff] [blame] | 705 | // Call the specified capture side API processing method. |
| 706 | int result = AudioProcessing::kNoError; |
| 707 | switch (test_config_->capture_api_function) { |
| 708 | case CaptureApiImpl::ProcessStreamImpl1: |
| 709 | result = apm_->ProcessStream(&frame_data_.frame); |
| 710 | break; |
| 711 | case CaptureApiImpl::ProcessStreamImpl2: |
| 712 | result = apm_->ProcessStream( |
| 713 | &frame_data_.input_frame[0], frame_data_.input_samples_per_channel, |
| 714 | frame_data_.input_sample_rate_hz, frame_data_.input_channel_layout, |
| 715 | frame_data_.output_sample_rate_hz, frame_data_.output_channel_layout, |
| 716 | &frame_data_.output_frame[0]); |
| 717 | break; |
| 718 | case CaptureApiImpl::ProcessStreamImpl3: |
| 719 | result = apm_->ProcessStream( |
| 720 | &frame_data_.input_frame[0], frame_data_.input_stream_config, |
| 721 | frame_data_.output_stream_config, &frame_data_.output_frame[0]); |
| 722 | break; |
| 723 | default: |
| 724 | FAIL(); |
| 725 | } |
| 726 | |
peah | be61562 | 2016-02-14 00:40:47 | [diff] [blame] | 727 | // Retrieve the new analog level. |
| 728 | apm_->gain_control()->stream_analog_level(); |
| 729 | |
peah | 1f1912d | 2015-11-09 11:13:19 | [diff] [blame] | 730 | // Check the return code for error. |
| 731 | ASSERT_EQ(AudioProcessing::kNoError, result); |
| 732 | } |
| 733 | |
| 734 | // Applies any runtime capture APM API calls and audio stream characteristics |
| 735 | // specified by the scheme for the test. |
| 736 | void CaptureProcessor::ApplyRuntimeSettingScheme() { |
| 737 | const int capture_count_local = frame_counters_->GetCaptureCounter(); |
| 738 | |
| 739 | // Update the number of channels and sample rates for the input and output. |
| 740 | // Note that the counts frequencies for when to set parameters |
| 741 | // are set using prime numbers in order to ensure that the |
| 742 | // permutation scheme in the parameter setting changes. |
| 743 | switch (test_config_->runtime_parameter_setting_scheme) { |
| 744 | case RuntimeParameterSettingScheme::SparseStreamMetadataChangeScheme: |
| 745 | if (capture_count_local == 0) |
| 746 | frame_data_.input_sample_rate_hz = 16000; |
| 747 | else if (capture_count_local % 11 == 0) |
| 748 | frame_data_.input_sample_rate_hz = 32000; |
| 749 | else if (capture_count_local % 73 == 0) |
| 750 | frame_data_.input_sample_rate_hz = 48000; |
| 751 | else if (capture_count_local % 89 == 0) |
| 752 | frame_data_.input_sample_rate_hz = 16000; |
| 753 | else if (capture_count_local % 97 == 0) |
| 754 | frame_data_.input_sample_rate_hz = 8000; |
| 755 | |
| 756 | if (capture_count_local == 0) |
| 757 | frame_data_.input_number_of_channels = 1; |
| 758 | else if (capture_count_local % 4 == 0) |
| 759 | frame_data_.input_number_of_channels = |
| 760 | (frame_data_.input_number_of_channels == 1 ? 2 : 1); |
| 761 | |
| 762 | if (capture_count_local == 0) |
| 763 | frame_data_.output_sample_rate_hz = 16000; |
| 764 | else if (capture_count_local % 5 == 0) |
| 765 | frame_data_.output_sample_rate_hz = 32000; |
| 766 | else if (capture_count_local % 47 == 0) |
| 767 | frame_data_.output_sample_rate_hz = 48000; |
| 768 | else if (capture_count_local % 53 == 0) |
| 769 | frame_data_.output_sample_rate_hz = 16000; |
| 770 | else if (capture_count_local % 71 == 0) |
| 771 | frame_data_.output_sample_rate_hz = 8000; |
| 772 | |
| 773 | if (capture_count_local == 0) |
| 774 | frame_data_.output_number_of_channels = 1; |
| 775 | else if (capture_count_local % 8 == 0) |
| 776 | frame_data_.output_number_of_channels = |
| 777 | (frame_data_.output_number_of_channels == 1 ? 2 : 1); |
| 778 | break; |
| 779 | case RuntimeParameterSettingScheme::ExtremeStreamMetadataChangeScheme: |
| 780 | if (capture_count_local % 2 == 0) { |
| 781 | frame_data_.input_number_of_channels = 1; |
| 782 | frame_data_.input_sample_rate_hz = 16000; |
| 783 | frame_data_.output_number_of_channels = 1; |
| 784 | frame_data_.output_sample_rate_hz = 16000; |
| 785 | } else { |
| 786 | frame_data_.input_number_of_channels = |
| 787 | (frame_data_.input_number_of_channels == 1 ? 2 : 1); |
| 788 | if (frame_data_.input_sample_rate_hz == 8000) |
| 789 | frame_data_.input_sample_rate_hz = 16000; |
| 790 | else if (frame_data_.input_sample_rate_hz == 16000) |
| 791 | frame_data_.input_sample_rate_hz = 32000; |
| 792 | else if (frame_data_.input_sample_rate_hz == 32000) |
| 793 | frame_data_.input_sample_rate_hz = 48000; |
| 794 | else if (frame_data_.input_sample_rate_hz == 48000) |
| 795 | frame_data_.input_sample_rate_hz = 8000; |
| 796 | |
| 797 | frame_data_.output_number_of_channels = |
| 798 | (frame_data_.output_number_of_channels == 1 ? 2 : 1); |
| 799 | if (frame_data_.output_sample_rate_hz == 8000) |
| 800 | frame_data_.output_sample_rate_hz = 16000; |
| 801 | else if (frame_data_.output_sample_rate_hz == 16000) |
| 802 | frame_data_.output_sample_rate_hz = 32000; |
| 803 | else if (frame_data_.output_sample_rate_hz == 32000) |
| 804 | frame_data_.output_sample_rate_hz = 48000; |
| 805 | else if (frame_data_.output_sample_rate_hz == 48000) |
| 806 | frame_data_.output_sample_rate_hz = 8000; |
| 807 | } |
| 808 | break; |
| 809 | case RuntimeParameterSettingScheme::FixedMonoStreamMetadataScheme: |
| 810 | if (capture_count_local == 0) { |
| 811 | frame_data_.input_sample_rate_hz = 16000; |
| 812 | frame_data_.input_number_of_channels = 1; |
| 813 | frame_data_.output_sample_rate_hz = 16000; |
| 814 | frame_data_.output_number_of_channels = 1; |
| 815 | } |
| 816 | break; |
| 817 | case RuntimeParameterSettingScheme::FixedStereoStreamMetadataScheme: |
| 818 | if (capture_count_local == 0) { |
| 819 | frame_data_.input_sample_rate_hz = 16000; |
| 820 | frame_data_.input_number_of_channels = 2; |
| 821 | frame_data_.output_sample_rate_hz = 16000; |
| 822 | frame_data_.output_number_of_channels = 2; |
| 823 | } |
| 824 | break; |
| 825 | default: |
| 826 | FAIL(); |
| 827 | } |
| 828 | |
| 829 | // Call any specified runtime APM setter and |
| 830 | // getter calls. |
| 831 | switch (test_config_->runtime_parameter_setting_scheme) { |
| 832 | case RuntimeParameterSettingScheme::SparseStreamMetadataChangeScheme: |
| 833 | case RuntimeParameterSettingScheme::FixedMonoStreamMetadataScheme: |
| 834 | break; |
| 835 | case RuntimeParameterSettingScheme::ExtremeStreamMetadataChangeScheme: |
| 836 | case RuntimeParameterSettingScheme::FixedStereoStreamMetadataScheme: |
| 837 | if (capture_count_local % 2 == 0) { |
| 838 | ASSERT_EQ(AudioProcessing::Error::kNoError, |
| 839 | apm_->set_stream_delay_ms(30)); |
| 840 | apm_->set_stream_key_pressed(true); |
peah | 1f1912d | 2015-11-09 11:13:19 | [diff] [blame] | 841 | apm_->set_delay_offset_ms(15); |
| 842 | EXPECT_EQ(apm_->delay_offset_ms(), 15); |
peah | 1f1912d | 2015-11-09 11:13:19 | [diff] [blame] | 843 | } else { |
| 844 | ASSERT_EQ(AudioProcessing::Error::kNoError, |
| 845 | apm_->set_stream_delay_ms(50)); |
| 846 | apm_->set_stream_key_pressed(false); |
peah | 1f1912d | 2015-11-09 11:13:19 | [diff] [blame] | 847 | apm_->set_delay_offset_ms(20); |
| 848 | EXPECT_EQ(apm_->delay_offset_ms(), 20); |
| 849 | apm_->delay_offset_ms(); |
peah | 1f1912d | 2015-11-09 11:13:19 | [diff] [blame] | 850 | } |
| 851 | break; |
| 852 | default: |
| 853 | FAIL(); |
| 854 | } |
| 855 | |
| 856 | // Restric the number of output channels not to exceed |
| 857 | // the number of input channels. |
| 858 | frame_data_.output_number_of_channels = |
| 859 | std::min(frame_data_.output_number_of_channels, |
| 860 | frame_data_.input_number_of_channels); |
| 861 | } |
| 862 | |
| 863 | const float RenderProcessor::kRenderInputFloatLevel = 0.5f; |
| 864 | |
| 865 | RenderProcessor::RenderProcessor(int max_frame_size, |
peah | df3efa8 | 2015-11-28 20:35:15 | [diff] [blame] | 866 | RandomGenerator* rand_gen, |
peah | 631e134 | 2015-12-03 09:15:27 | [diff] [blame] | 867 | rtc::Event* render_call_event, |
| 868 | rtc::Event* capture_call_event, |
peah | 1f1912d | 2015-11-09 11:13:19 | [diff] [blame] | 869 | FrameCounters* shared_counters_state, |
peah | 1f1912d | 2015-11-09 11:13:19 | [diff] [blame] | 870 | AudioProcessingImplLockTest* test_framework, |
| 871 | TestConfig* test_config, |
| 872 | AudioProcessing* apm) |
| 873 | : rand_gen_(rand_gen), |
peah | 631e134 | 2015-12-03 09:15:27 | [diff] [blame] | 874 | render_call_event_(render_call_event), |
| 875 | capture_call_event_(capture_call_event), |
peah | 1f1912d | 2015-11-09 11:13:19 | [diff] [blame] | 876 | frame_counters_(shared_counters_state), |
peah | 1f1912d | 2015-11-09 11:13:19 | [diff] [blame] | 877 | test_(test_framework), |
| 878 | test_config_(test_config), |
| 879 | apm_(apm), |
| 880 | frame_data_(max_frame_size) {} |
| 881 | |
| 882 | // Implements the callback functionality for the render thread. |
| 883 | bool RenderProcessor::Process() { |
| 884 | // Conditional wait to ensure that a capture call has been done |
| 885 | // before the first render call is performed (implicitly |
| 886 | // required by the APM API). |
peah | 631e134 | 2015-12-03 09:15:27 | [diff] [blame] | 887 | if (first_render_call_) { |
| 888 | capture_call_event_->Wait(rtc::Event::kForever); |
| 889 | first_render_call_ = false; |
peah | 1f1912d | 2015-11-09 11:13:19 | [diff] [blame] | 890 | } |
| 891 | |
| 892 | // Sleep a random time to simulate thread jitter. |
| 893 | SleepRandomMs(3, rand_gen_); |
| 894 | |
peah | 631e134 | 2015-12-03 09:15:27 | [diff] [blame] | 895 | // Check whether the test is done. |
| 896 | if (test_->MaybeEndTest()) { |
| 897 | return false; |
| 898 | } |
peah | 1f1912d | 2015-11-09 11:13:19 | [diff] [blame] | 899 | |
| 900 | // Ensure that the number of render and capture calls do not |
| 901 | // differ too much. |
peah | 631e134 | 2015-12-03 09:15:27 | [diff] [blame] | 902 | if (frame_counters_->RenderMinusCaptureCounters() > kMaxCallDifference) { |
| 903 | capture_call_event_->Wait(rtc::Event::kForever); |
peah | 1f1912d | 2015-11-09 11:13:19 | [diff] [blame] | 904 | } |
| 905 | |
| 906 | // Apply any specified render side APM non-processing runtime calls. |
| 907 | ApplyRuntimeSettingScheme(); |
| 908 | |
| 909 | // Apply the render side processing call. |
| 910 | CallApmRenderSide(); |
| 911 | |
| 912 | // Increase the number of render-side calls. |
| 913 | frame_counters_->IncreaseRenderCounter(); |
| 914 | |
peah | 631e134 | 2015-12-03 09:15:27 | [diff] [blame] | 915 | // Flag to the capture thread that another render API call has occurred |
| 916 | // by triggering this threads call event. |
| 917 | render_call_event_->Set(); |
peah | 1f1912d | 2015-11-09 11:13:19 | [diff] [blame] | 918 | return true; |
| 919 | } |
| 920 | |
| 921 | // Prepares the render side frame and the accompanying metadata |
| 922 | // with the appropriate information. |
| 923 | void RenderProcessor::PrepareFrame() { |
| 924 | // Restrict to a common fixed sample rate if the AudioFrame interface is |
| 925 | // used. |
| 926 | if ((test_config_->render_api_function == |
peah | 1f1912d | 2015-11-09 11:13:19 | [diff] [blame] | 927 | RenderApiImpl::ProcessReverseStreamImpl1) || |
| 928 | (test_config_->aec_type != |
| 929 | AecType::BasicWebRtcAecSettingsWithAecMobile)) { |
| 930 | frame_data_.input_sample_rate_hz = test_config_->initial_sample_rate_hz; |
| 931 | frame_data_.output_sample_rate_hz = test_config_->initial_sample_rate_hz; |
| 932 | } |
| 933 | |
| 934 | // Prepare the audioframe data and metadata |
| 935 | frame_data_.input_samples_per_channel = |
| 936 | frame_data_.input_sample_rate_hz * AudioProcessing::kChunkSizeMs / 1000; |
| 937 | frame_data_.frame.sample_rate_hz_ = frame_data_.input_sample_rate_hz; |
| 938 | frame_data_.frame.num_channels_ = frame_data_.input_number_of_channels; |
| 939 | frame_data_.frame.samples_per_channel_ = |
| 940 | frame_data_.input_samples_per_channel; |
| 941 | PopulateAudioFrame(&frame_data_.frame, kRenderInputFixLevel, rand_gen_); |
| 942 | |
| 943 | // Prepare the float audio input data and metadata. |
| 944 | frame_data_.input_stream_config.set_sample_rate_hz( |
| 945 | frame_data_.input_sample_rate_hz); |
| 946 | frame_data_.input_stream_config.set_num_channels( |
| 947 | frame_data_.input_number_of_channels); |
| 948 | frame_data_.input_stream_config.set_has_keyboard(false); |
| 949 | PopulateAudioFrame(&frame_data_.input_frame[0], kRenderInputFloatLevel, |
| 950 | frame_data_.input_number_of_channels, |
| 951 | frame_data_.input_samples_per_channel, rand_gen_); |
| 952 | frame_data_.input_channel_layout = |
| 953 | (frame_data_.input_number_of_channels == 1 |
| 954 | ? AudioProcessing::ChannelLayout::kMono |
| 955 | : AudioProcessing::ChannelLayout::kStereo); |
| 956 | |
| 957 | // Prepare the float audio output data and metadata. |
| 958 | frame_data_.output_samples_per_channel = |
| 959 | frame_data_.output_sample_rate_hz * AudioProcessing::kChunkSizeMs / 1000; |
| 960 | frame_data_.output_stream_config.set_sample_rate_hz( |
| 961 | frame_data_.output_sample_rate_hz); |
| 962 | frame_data_.output_stream_config.set_num_channels( |
| 963 | frame_data_.output_number_of_channels); |
| 964 | frame_data_.output_stream_config.set_has_keyboard(false); |
| 965 | frame_data_.output_channel_layout = |
| 966 | (frame_data_.output_number_of_channels == 1 |
| 967 | ? AudioProcessing::ChannelLayout::kMono |
| 968 | : AudioProcessing::ChannelLayout::kStereo); |
| 969 | } |
| 970 | |
| 971 | // Makes the render side processing API call. |
| 972 | void RenderProcessor::CallApmRenderSide() { |
| 973 | // Prepare a proper render side processing API call input. |
| 974 | PrepareFrame(); |
| 975 | |
| 976 | // Call the specified render side API processing method. |
| 977 | int result = AudioProcessing::kNoError; |
| 978 | switch (test_config_->render_api_function) { |
| 979 | case RenderApiImpl::ProcessReverseStreamImpl1: |
| 980 | result = apm_->ProcessReverseStream(&frame_data_.frame); |
| 981 | break; |
| 982 | case RenderApiImpl::ProcessReverseStreamImpl2: |
| 983 | result = apm_->ProcessReverseStream( |
| 984 | &frame_data_.input_frame[0], frame_data_.input_stream_config, |
| 985 | frame_data_.output_stream_config, &frame_data_.output_frame[0]); |
| 986 | break; |
aluebs | b031955 | 2016-03-18 03:39:53 | [diff] [blame] | 987 | case RenderApiImpl::AnalyzeReverseStreamImpl: |
peah | 1f1912d | 2015-11-09 11:13:19 | [diff] [blame] | 988 | result = apm_->AnalyzeReverseStream( |
| 989 | &frame_data_.input_frame[0], frame_data_.input_samples_per_channel, |
| 990 | frame_data_.input_sample_rate_hz, frame_data_.input_channel_layout); |
| 991 | break; |
| 992 | default: |
| 993 | FAIL(); |
| 994 | } |
| 995 | |
| 996 | // Check the return code for error. |
| 997 | ASSERT_EQ(AudioProcessing::kNoError, result); |
| 998 | } |
| 999 | |
| 1000 | // Applies any render capture side APM API calls and audio stream |
| 1001 | // characteristics |
| 1002 | // specified by the scheme for the test. |
| 1003 | void RenderProcessor::ApplyRuntimeSettingScheme() { |
| 1004 | const int render_count_local = frame_counters_->GetRenderCounter(); |
| 1005 | |
| 1006 | // Update the number of channels and sample rates for the input and output. |
| 1007 | // Note that the counts frequencies for when to set parameters |
| 1008 | // are set using prime numbers in order to ensure that the |
| 1009 | // permutation scheme in the parameter setting changes. |
| 1010 | switch (test_config_->runtime_parameter_setting_scheme) { |
| 1011 | case RuntimeParameterSettingScheme::SparseStreamMetadataChangeScheme: |
| 1012 | if (render_count_local == 0) |
| 1013 | frame_data_.input_sample_rate_hz = 16000; |
| 1014 | else if (render_count_local % 47 == 0) |
| 1015 | frame_data_.input_sample_rate_hz = 32000; |
| 1016 | else if (render_count_local % 71 == 0) |
| 1017 | frame_data_.input_sample_rate_hz = 48000; |
| 1018 | else if (render_count_local % 79 == 0) |
| 1019 | frame_data_.input_sample_rate_hz = 16000; |
| 1020 | else if (render_count_local % 83 == 0) |
| 1021 | frame_data_.input_sample_rate_hz = 8000; |
| 1022 | |
| 1023 | if (render_count_local == 0) |
| 1024 | frame_data_.input_number_of_channels = 1; |
| 1025 | else if (render_count_local % 4 == 0) |
| 1026 | frame_data_.input_number_of_channels = |
| 1027 | (frame_data_.input_number_of_channels == 1 ? 2 : 1); |
| 1028 | |
| 1029 | if (render_count_local == 0) |
| 1030 | frame_data_.output_sample_rate_hz = 16000; |
| 1031 | else if (render_count_local % 17 == 0) |
| 1032 | frame_data_.output_sample_rate_hz = 32000; |
| 1033 | else if (render_count_local % 19 == 0) |
| 1034 | frame_data_.output_sample_rate_hz = 48000; |
| 1035 | else if (render_count_local % 29 == 0) |
| 1036 | frame_data_.output_sample_rate_hz = 16000; |
| 1037 | else if (render_count_local % 61 == 0) |
| 1038 | frame_data_.output_sample_rate_hz = 8000; |
| 1039 | |
| 1040 | if (render_count_local == 0) |
| 1041 | frame_data_.output_number_of_channels = 1; |
| 1042 | else if (render_count_local % 8 == 0) |
| 1043 | frame_data_.output_number_of_channels = |
| 1044 | (frame_data_.output_number_of_channels == 1 ? 2 : 1); |
| 1045 | break; |
| 1046 | case RuntimeParameterSettingScheme::ExtremeStreamMetadataChangeScheme: |
| 1047 | if (render_count_local == 0) { |
| 1048 | frame_data_.input_number_of_channels = 1; |
| 1049 | frame_data_.input_sample_rate_hz = 16000; |
| 1050 | frame_data_.output_number_of_channels = 1; |
| 1051 | frame_data_.output_sample_rate_hz = 16000; |
| 1052 | } else { |
| 1053 | frame_data_.input_number_of_channels = |
| 1054 | (frame_data_.input_number_of_channels == 1 ? 2 : 1); |
| 1055 | if (frame_data_.input_sample_rate_hz == 8000) |
| 1056 | frame_data_.input_sample_rate_hz = 16000; |
| 1057 | else if (frame_data_.input_sample_rate_hz == 16000) |
| 1058 | frame_data_.input_sample_rate_hz = 32000; |
| 1059 | else if (frame_data_.input_sample_rate_hz == 32000) |
| 1060 | frame_data_.input_sample_rate_hz = 48000; |
| 1061 | else if (frame_data_.input_sample_rate_hz == 48000) |
| 1062 | frame_data_.input_sample_rate_hz = 8000; |
| 1063 | |
| 1064 | frame_data_.output_number_of_channels = |
| 1065 | (frame_data_.output_number_of_channels == 1 ? 2 : 1); |
| 1066 | if (frame_data_.output_sample_rate_hz == 8000) |
| 1067 | frame_data_.output_sample_rate_hz = 16000; |
| 1068 | else if (frame_data_.output_sample_rate_hz == 16000) |
| 1069 | frame_data_.output_sample_rate_hz = 32000; |
| 1070 | else if (frame_data_.output_sample_rate_hz == 32000) |
| 1071 | frame_data_.output_sample_rate_hz = 48000; |
| 1072 | else if (frame_data_.output_sample_rate_hz == 48000) |
| 1073 | frame_data_.output_sample_rate_hz = 8000; |
| 1074 | } |
| 1075 | break; |
| 1076 | case RuntimeParameterSettingScheme::FixedMonoStreamMetadataScheme: |
| 1077 | if (render_count_local == 0) { |
| 1078 | frame_data_.input_sample_rate_hz = 16000; |
| 1079 | frame_data_.input_number_of_channels = 1; |
| 1080 | frame_data_.output_sample_rate_hz = 16000; |
| 1081 | frame_data_.output_number_of_channels = 1; |
| 1082 | } |
| 1083 | break; |
| 1084 | case RuntimeParameterSettingScheme::FixedStereoStreamMetadataScheme: |
| 1085 | if (render_count_local == 0) { |
| 1086 | frame_data_.input_sample_rate_hz = 16000; |
| 1087 | frame_data_.input_number_of_channels = 2; |
| 1088 | frame_data_.output_sample_rate_hz = 16000; |
| 1089 | frame_data_.output_number_of_channels = 2; |
| 1090 | } |
| 1091 | break; |
| 1092 | default: |
| 1093 | FAIL(); |
| 1094 | } |
| 1095 | |
| 1096 | // Restric the number of output channels not to exceed |
| 1097 | // the number of input channels. |
| 1098 | frame_data_.output_number_of_channels = |
| 1099 | std::min(frame_data_.output_number_of_channels, |
| 1100 | frame_data_.input_number_of_channels); |
| 1101 | } |
| 1102 | |
| 1103 | } // anonymous namespace |
| 1104 | |
| 1105 | TEST_P(AudioProcessingImplLockTest, LockTest) { |
| 1106 | // Run test and verify that it did not time out. |
peah | 631e134 | 2015-12-03 09:15:27 | [diff] [blame] | 1107 | ASSERT_TRUE(RunTest()); |
peah | 1f1912d | 2015-11-09 11:13:19 | [diff] [blame] | 1108 | } |
| 1109 | |
| 1110 | // Instantiate tests from the extreme test configuration set. |
| 1111 | INSTANTIATE_TEST_CASE_P( |
| 1112 | DISABLED_AudioProcessingImplLockExtensive, |
| 1113 | AudioProcessingImplLockTest, |
| 1114 | ::testing::ValuesIn(TestConfig::GenerateExtensiveTestConfigs())); |
| 1115 | |
| 1116 | INSTANTIATE_TEST_CASE_P( |
peah | df3efa8 | 2015-11-28 20:35:15 | [diff] [blame] | 1117 | AudioProcessingImplLockBrief, |
peah | 1f1912d | 2015-11-09 11:13:19 | [diff] [blame] | 1118 | AudioProcessingImplLockTest, |
| 1119 | ::testing::ValuesIn(TestConfig::GenerateBriefTestConfigs())); |
| 1120 | |
| 1121 | } // namespace webrtc |