henrika | b261989 | 2015-05-18 16:49:16 +0200 | [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 | |
henrika | 521f7a8 | 2016-05-31 07:03:17 -0700 | [diff] [blame] | 11 | #include <SLES/OpenSLES_Android.h> |
kwiberg | f01633e | 2016-02-24 05:00:36 -0800 | [diff] [blame] | 12 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 13 | #include "modules/audio_device/android/audio_manager.h" |
| 14 | #include "modules/audio_device/android/build_info.h" |
| 15 | #include "modules/audio_device/android/ensure_initialized.h" |
| 16 | #include "rtc_base/arraysize.h" |
| 17 | #include "rtc_base/format_macros.h" |
| 18 | #include "test/gtest.h" |
henrika | b261989 | 2015-05-18 16:49:16 +0200 | [diff] [blame] | 19 | |
| 20 | #define PRINT(...) fprintf(stderr, __VA_ARGS__); |
| 21 | |
| 22 | namespace webrtc { |
| 23 | |
| 24 | static const char kTag[] = " "; |
| 25 | |
| 26 | class AudioManagerTest : public ::testing::Test { |
| 27 | protected: |
| 28 | AudioManagerTest() { |
| 29 | // One-time initialization of JVM and application context. Ensures that we |
| 30 | // can do calls between C++ and Java. |
| 31 | webrtc::audiodevicemodule::EnsureInitialized(); |
| 32 | audio_manager_.reset(new AudioManager()); |
| 33 | SetActiveAudioLayer(); |
| 34 | playout_parameters_ = audio_manager()->GetPlayoutAudioParameters(); |
| 35 | record_parameters_ = audio_manager()->GetRecordAudioParameters(); |
| 36 | } |
| 37 | |
| 38 | AudioManager* audio_manager() const { return audio_manager_.get(); } |
| 39 | |
| 40 | // A valid audio layer must always be set before calling Init(), hence we |
| 41 | // might as well make it a part of the test fixture. |
| 42 | void SetActiveAudioLayer() { |
| 43 | EXPECT_EQ(0, audio_manager()->GetDelayEstimateInMilliseconds()); |
| 44 | audio_manager()->SetActiveAudioLayer(AudioDeviceModule::kAndroidJavaAudio); |
| 45 | EXPECT_NE(0, audio_manager()->GetDelayEstimateInMilliseconds()); |
| 46 | } |
| 47 | |
henrika | 521f7a8 | 2016-05-31 07:03:17 -0700 | [diff] [blame] | 48 | // One way to ensure that the engine object is valid is to create an |
| 49 | // SL Engine interface since it exposes creation methods of all the OpenSL ES |
| 50 | // object types and it is only supported on the engine object. This method |
| 51 | // also verifies that the engine interface supports at least one interface. |
| 52 | // Note that, the test below is not a full test of the SLEngineItf object |
| 53 | // but only a simple sanity test to check that the global engine object is OK. |
| 54 | void ValidateSLEngine(SLObjectItf engine_object) { |
| 55 | EXPECT_NE(nullptr, engine_object); |
| 56 | // Get the SL Engine interface which is exposed by the engine object. |
| 57 | SLEngineItf engine; |
| 58 | SLresult result = |
| 59 | (*engine_object)->GetInterface(engine_object, SL_IID_ENGINE, &engine); |
| 60 | EXPECT_EQ(result, SL_RESULT_SUCCESS) << "GetInterface() on engine failed"; |
| 61 | // Ensure that the SL Engine interface exposes at least one interface. |
| 62 | SLuint32 object_id = SL_OBJECTID_ENGINE; |
| 63 | SLuint32 num_supported_interfaces = 0; |
| 64 | result = (*engine)->QueryNumSupportedInterfaces(engine, object_id, |
| 65 | &num_supported_interfaces); |
| 66 | EXPECT_EQ(result, SL_RESULT_SUCCESS) |
| 67 | << "QueryNumSupportedInterfaces() failed"; |
| 68 | EXPECT_GE(num_supported_interfaces, 1u); |
| 69 | } |
| 70 | |
kwiberg | f01633e | 2016-02-24 05:00:36 -0800 | [diff] [blame] | 71 | std::unique_ptr<AudioManager> audio_manager_; |
henrika | b261989 | 2015-05-18 16:49:16 +0200 | [diff] [blame] | 72 | AudioParameters playout_parameters_; |
| 73 | AudioParameters record_parameters_; |
| 74 | }; |
| 75 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 76 | TEST_F(AudioManagerTest, ConstructDestruct) {} |
henrika | b261989 | 2015-05-18 16:49:16 +0200 | [diff] [blame] | 77 | |
henrika | 521f7a8 | 2016-05-31 07:03:17 -0700 | [diff] [blame] | 78 | // It should not be possible to create an OpenSL engine object if Java based |
| 79 | // audio is requested in both directions. |
| 80 | TEST_F(AudioManagerTest, GetOpenSLEngineShouldFailForJavaAudioLayer) { |
| 81 | audio_manager()->SetActiveAudioLayer(AudioDeviceModule::kAndroidJavaAudio); |
| 82 | SLObjectItf engine_object = audio_manager()->GetOpenSLEngine(); |
| 83 | EXPECT_EQ(nullptr, engine_object); |
| 84 | } |
| 85 | |
| 86 | // It should be possible to create an OpenSL engine object if OpenSL ES based |
| 87 | // audio is requested in any direction. |
| 88 | TEST_F(AudioManagerTest, GetOpenSLEngineShouldSucceedForOpenSLESAudioLayer) { |
| 89 | // List of supported audio layers that uses OpenSL ES audio. |
| 90 | const AudioDeviceModule::AudioLayer opensles_audio[] = { |
| 91 | AudioDeviceModule::kAndroidOpenSLESAudio, |
| 92 | AudioDeviceModule::kAndroidJavaInputAndOpenSLESOutputAudio}; |
| 93 | // Verify that the global (singleton) OpenSL Engine can be acquired for all |
| 94 | // audio layes that uses OpenSL ES. Note that the engine is only created once. |
| 95 | for (const AudioDeviceModule::AudioLayer audio_layer : opensles_audio) { |
| 96 | audio_manager()->SetActiveAudioLayer(audio_layer); |
| 97 | SLObjectItf engine_object = audio_manager()->GetOpenSLEngine(); |
| 98 | EXPECT_NE(nullptr, engine_object); |
| 99 | // Perform a simple sanity check of the created engine object. |
| 100 | ValidateSLEngine(engine_object); |
| 101 | } |
| 102 | } |
| 103 | |
henrika | b261989 | 2015-05-18 16:49:16 +0200 | [diff] [blame] | 104 | TEST_F(AudioManagerTest, InitClose) { |
| 105 | EXPECT_TRUE(audio_manager()->Init()); |
| 106 | EXPECT_TRUE(audio_manager()->Close()); |
| 107 | } |
| 108 | |
| 109 | TEST_F(AudioManagerTest, IsAcousticEchoCancelerSupported) { |
| 110 | PRINT("%sAcoustic Echo Canceler support: %s\n", kTag, |
| 111 | audio_manager()->IsAcousticEchoCancelerSupported() ? "Yes" : "No"); |
| 112 | } |
| 113 | |
henrika | c14f5ff | 2015-09-23 14:08:33 +0200 | [diff] [blame] | 114 | TEST_F(AudioManagerTest, IsAutomaticGainControlSupported) { |
henrika | defc21e | 2016-10-11 01:29:09 -0700 | [diff] [blame] | 115 | EXPECT_FALSE(audio_manager()->IsAutomaticGainControlSupported()); |
henrika | c14f5ff | 2015-09-23 14:08:33 +0200 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | TEST_F(AudioManagerTest, IsNoiseSuppressorSupported) { |
| 119 | PRINT("%sNoise Suppressor support: %s\n", kTag, |
| 120 | audio_manager()->IsNoiseSuppressorSupported() ? "Yes" : "No"); |
| 121 | } |
| 122 | |
henrika | b261989 | 2015-05-18 16:49:16 +0200 | [diff] [blame] | 123 | TEST_F(AudioManagerTest, IsLowLatencyPlayoutSupported) { |
| 124 | PRINT("%sLow latency output support: %s\n", kTag, |
| 125 | audio_manager()->IsLowLatencyPlayoutSupported() ? "Yes" : "No"); |
| 126 | } |
| 127 | |
henrika | 918b554 | 2016-09-19 15:44:09 +0200 | [diff] [blame] | 128 | TEST_F(AudioManagerTest, IsLowLatencyRecordSupported) { |
| 129 | PRINT("%sLow latency input support: %s\n", kTag, |
| 130 | audio_manager()->IsLowLatencyRecordSupported() ? "Yes" : "No"); |
| 131 | } |
| 132 | |
henrika | 1f0ad10 | 2016-05-25 05:15:10 -0700 | [diff] [blame] | 133 | TEST_F(AudioManagerTest, IsProAudioSupported) { |
| 134 | PRINT("%sPro audio support: %s\n", kTag, |
| 135 | audio_manager()->IsProAudioSupported() ? "Yes" : "No"); |
| 136 | } |
| 137 | |
henrika | 76535de | 2017-09-11 01:25:55 -0700 | [diff] [blame] | 138 | // Verify that playout side is configured for mono by default. |
| 139 | TEST_F(AudioManagerTest, IsStereoPlayoutSupported) { |
| 140 | EXPECT_FALSE(audio_manager()->IsStereoPlayoutSupported()); |
| 141 | } |
| 142 | |
| 143 | // Verify that recording side is configured for mono by default. |
| 144 | TEST_F(AudioManagerTest, IsStereoRecordSupported) { |
| 145 | EXPECT_FALSE(audio_manager()->IsStereoRecordSupported()); |
| 146 | } |
| 147 | |
henrika | b261989 | 2015-05-18 16:49:16 +0200 | [diff] [blame] | 148 | TEST_F(AudioManagerTest, ShowAudioParameterInfo) { |
| 149 | const bool low_latency_out = audio_manager()->IsLowLatencyPlayoutSupported(); |
henrika | 918b554 | 2016-09-19 15:44:09 +0200 | [diff] [blame] | 150 | const bool low_latency_in = audio_manager()->IsLowLatencyRecordSupported(); |
henrika | b261989 | 2015-05-18 16:49:16 +0200 | [diff] [blame] | 151 | PRINT("PLAYOUT:\n"); |
| 152 | PRINT("%saudio layer: %s\n", kTag, |
| 153 | low_latency_out ? "Low latency OpenSL" : "Java/JNI based AudioTrack"); |
| 154 | PRINT("%ssample rate: %d Hz\n", kTag, playout_parameters_.sample_rate()); |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 155 | PRINT("%schannels: %" PRIuS "\n", kTag, playout_parameters_.channels()); |
Peter Kasting | 1380e26 | 2015-08-28 17:31:03 -0700 | [diff] [blame] | 156 | PRINT("%sframes per buffer: %" PRIuS " <=> %.2f ms\n", kTag, |
henrika | b261989 | 2015-05-18 16:49:16 +0200 | [diff] [blame] | 157 | playout_parameters_.frames_per_buffer(), |
| 158 | playout_parameters_.GetBufferSizeInMilliseconds()); |
| 159 | PRINT("RECORD: \n"); |
henrika | 918b554 | 2016-09-19 15:44:09 +0200 | [diff] [blame] | 160 | PRINT("%saudio layer: %s\n", kTag, |
| 161 | low_latency_in ? "Low latency OpenSL" : "Java/JNI based AudioRecord"); |
henrika | b261989 | 2015-05-18 16:49:16 +0200 | [diff] [blame] | 162 | PRINT("%ssample rate: %d Hz\n", kTag, record_parameters_.sample_rate()); |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 163 | PRINT("%schannels: %" PRIuS "\n", kTag, record_parameters_.channels()); |
Peter Kasting | 1380e26 | 2015-08-28 17:31:03 -0700 | [diff] [blame] | 164 | PRINT("%sframes per buffer: %" PRIuS " <=> %.2f ms\n", kTag, |
henrika | b261989 | 2015-05-18 16:49:16 +0200 | [diff] [blame] | 165 | record_parameters_.frames_per_buffer(), |
| 166 | record_parameters_.GetBufferSizeInMilliseconds()); |
| 167 | } |
| 168 | |
henrika | 918b554 | 2016-09-19 15:44:09 +0200 | [diff] [blame] | 169 | // The audio device module only suppors the same sample rate in both directions. |
| 170 | // In addition, in full-duplex low-latency mode (OpenSL ES), both input and |
| 171 | // output must use the same native buffer size to allow for usage of the fast |
| 172 | // audio track in Android. |
| 173 | TEST_F(AudioManagerTest, VerifyAudioParameters) { |
| 174 | const bool low_latency_out = audio_manager()->IsLowLatencyPlayoutSupported(); |
| 175 | const bool low_latency_in = audio_manager()->IsLowLatencyRecordSupported(); |
| 176 | EXPECT_EQ(playout_parameters_.sample_rate(), |
| 177 | record_parameters_.sample_rate()); |
| 178 | if (low_latency_out && low_latency_in) { |
| 179 | EXPECT_EQ(playout_parameters_.frames_per_buffer(), |
| 180 | record_parameters_.frames_per_buffer()); |
| 181 | } |
| 182 | } |
| 183 | |
henrika | b261989 | 2015-05-18 16:49:16 +0200 | [diff] [blame] | 184 | // Add device-specific information to the test for logging purposes. |
| 185 | TEST_F(AudioManagerTest, ShowDeviceInfo) { |
| 186 | BuildInfo build_info; |
| 187 | PRINT("%smodel: %s\n", kTag, build_info.GetDeviceModel().c_str()); |
| 188 | PRINT("%sbrand: %s\n", kTag, build_info.GetBrand().c_str()); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 189 | PRINT("%smanufacturer: %s\n", kTag, |
| 190 | build_info.GetDeviceManufacturer().c_str()); |
henrika | b261989 | 2015-05-18 16:49:16 +0200 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | // Add Android build information to the test for logging purposes. |
| 194 | TEST_F(AudioManagerTest, ShowBuildInfo) { |
| 195 | BuildInfo build_info; |
| 196 | PRINT("%sbuild release: %s\n", kTag, build_info.GetBuildRelease().c_str()); |
| 197 | PRINT("%sbuild id: %s\n", kTag, build_info.GetAndroidBuildId().c_str()); |
| 198 | PRINT("%sbuild type: %s\n", kTag, build_info.GetBuildType().c_str()); |
henrika | 918b554 | 2016-09-19 15:44:09 +0200 | [diff] [blame] | 199 | PRINT("%sSDK version: %d\n", kTag, build_info.GetSdkVersion()); |
henrika | b261989 | 2015-05-18 16:49:16 +0200 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | // Basic test of the AudioParameters class using default construction where |
| 203 | // all members are set to zero. |
| 204 | TEST_F(AudioManagerTest, AudioParametersWithDefaultConstruction) { |
| 205 | AudioParameters params; |
| 206 | EXPECT_FALSE(params.is_valid()); |
| 207 | EXPECT_EQ(0, params.sample_rate()); |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 208 | EXPECT_EQ(0U, params.channels()); |
Peter Kasting | 1380e26 | 2015-08-28 17:31:03 -0700 | [diff] [blame] | 209 | EXPECT_EQ(0U, params.frames_per_buffer()); |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 210 | EXPECT_EQ(0U, params.frames_per_10ms_buffer()); |
Peter Kasting | 1380e26 | 2015-08-28 17:31:03 -0700 | [diff] [blame] | 211 | EXPECT_EQ(0U, params.GetBytesPerFrame()); |
| 212 | EXPECT_EQ(0U, params.GetBytesPerBuffer()); |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 213 | EXPECT_EQ(0U, params.GetBytesPer10msBuffer()); |
henrika | b261989 | 2015-05-18 16:49:16 +0200 | [diff] [blame] | 214 | EXPECT_EQ(0.0f, params.GetBufferSizeInMilliseconds()); |
| 215 | } |
| 216 | |
| 217 | // Basic test of the AudioParameters class using non default construction. |
| 218 | TEST_F(AudioManagerTest, AudioParametersWithNonDefaultConstruction) { |
| 219 | const int kSampleRate = 48000; |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 220 | const size_t kChannels = 1; |
Peter Kasting | 1380e26 | 2015-08-28 17:31:03 -0700 | [diff] [blame] | 221 | const size_t kFramesPerBuffer = 480; |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 222 | const size_t kFramesPer10msBuffer = 480; |
Peter Kasting | 1380e26 | 2015-08-28 17:31:03 -0700 | [diff] [blame] | 223 | const size_t kBytesPerFrame = 2; |
henrika | b261989 | 2015-05-18 16:49:16 +0200 | [diff] [blame] | 224 | const float kBufferSizeInMs = 10.0f; |
| 225 | AudioParameters params(kSampleRate, kChannels, kFramesPerBuffer); |
| 226 | EXPECT_TRUE(params.is_valid()); |
| 227 | EXPECT_EQ(kSampleRate, params.sample_rate()); |
| 228 | EXPECT_EQ(kChannels, params.channels()); |
| 229 | EXPECT_EQ(kFramesPerBuffer, params.frames_per_buffer()); |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 230 | EXPECT_EQ(static_cast<size_t>(kSampleRate / 100), |
| 231 | params.frames_per_10ms_buffer()); |
henrika | b261989 | 2015-05-18 16:49:16 +0200 | [diff] [blame] | 232 | EXPECT_EQ(kBytesPerFrame, params.GetBytesPerFrame()); |
| 233 | EXPECT_EQ(kBytesPerFrame * kFramesPerBuffer, params.GetBytesPerBuffer()); |
| 234 | EXPECT_EQ(kBytesPerFrame * kFramesPer10msBuffer, |
| 235 | params.GetBytesPer10msBuffer()); |
| 236 | EXPECT_EQ(kBufferSizeInMs, params.GetBufferSizeInMilliseconds()); |
| 237 | } |
| 238 | |
| 239 | } // namespace webrtc |