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
11 #include "modules/audio_device/android/audio_manager.h"
12
13 #include <SLES/OpenSLES_Android.h>
14
15 #include "modules/audio_device/android/build_info.h"
16 #include "modules/audio_device/android/ensure_initialized.h"
17 #include "rtc_base/arraysize.h"
18 #include "test/gtest.h"
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:
AudioManagerTest()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
audio_manager() const38 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.
SetActiveAudioLayer()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
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.
ValidateSLEngine(SLObjectItf engine_object)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
71 std::unique_ptr<AudioManager> audio_manager_;
72 AudioParameters playout_parameters_;
73 AudioParameters record_parameters_;
74 };
75
TEST_F(AudioManagerTest,ConstructDestruct)76 TEST_F(AudioManagerTest, ConstructDestruct) {}
77
78 // It should not be possible to create an OpenSL engine object if Java based
79 // audio is requested in both directions.
TEST_F(AudioManagerTest,GetOpenSLEngineShouldFailForJavaAudioLayer)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.
TEST_F(AudioManagerTest,GetOpenSLEngineShouldSucceedForOpenSLESAudioLayer)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
TEST_F(AudioManagerTest,InitClose)104 TEST_F(AudioManagerTest, InitClose) {
105 EXPECT_TRUE(audio_manager()->Init());
106 EXPECT_TRUE(audio_manager()->Close());
107 }
108
TEST_F(AudioManagerTest,IsAcousticEchoCancelerSupported)109 TEST_F(AudioManagerTest, IsAcousticEchoCancelerSupported) {
110 PRINT("%sAcoustic Echo Canceler support: %s\n", kTag,
111 audio_manager()->IsAcousticEchoCancelerSupported() ? "Yes" : "No");
112 }
113
TEST_F(AudioManagerTest,IsAutomaticGainControlSupported)114 TEST_F(AudioManagerTest, IsAutomaticGainControlSupported) {
115 EXPECT_FALSE(audio_manager()->IsAutomaticGainControlSupported());
116 }
117
TEST_F(AudioManagerTest,IsNoiseSuppressorSupported)118 TEST_F(AudioManagerTest, IsNoiseSuppressorSupported) {
119 PRINT("%sNoise Suppressor support: %s\n", kTag,
120 audio_manager()->IsNoiseSuppressorSupported() ? "Yes" : "No");
121 }
122
TEST_F(AudioManagerTest,IsLowLatencyPlayoutSupported)123 TEST_F(AudioManagerTest, IsLowLatencyPlayoutSupported) {
124 PRINT("%sLow latency output support: %s\n", kTag,
125 audio_manager()->IsLowLatencyPlayoutSupported() ? "Yes" : "No");
126 }
127
TEST_F(AudioManagerTest,IsLowLatencyRecordSupported)128 TEST_F(AudioManagerTest, IsLowLatencyRecordSupported) {
129 PRINT("%sLow latency input support: %s\n", kTag,
130 audio_manager()->IsLowLatencyRecordSupported() ? "Yes" : "No");
131 }
132
TEST_F(AudioManagerTest,IsProAudioSupported)133 TEST_F(AudioManagerTest, IsProAudioSupported) {
134 PRINT("%sPro audio support: %s\n", kTag,
135 audio_manager()->IsProAudioSupported() ? "Yes" : "No");
136 }
137
138 // Verify that playout side is configured for mono by default.
TEST_F(AudioManagerTest,IsStereoPlayoutSupported)139 TEST_F(AudioManagerTest, IsStereoPlayoutSupported) {
140 EXPECT_FALSE(audio_manager()->IsStereoPlayoutSupported());
141 }
142
143 // Verify that recording side is configured for mono by default.
TEST_F(AudioManagerTest,IsStereoRecordSupported)144 TEST_F(AudioManagerTest, IsStereoRecordSupported) {
145 EXPECT_FALSE(audio_manager()->IsStereoRecordSupported());
146 }
147
TEST_F(AudioManagerTest,ShowAudioParameterInfo)148 TEST_F(AudioManagerTest, ShowAudioParameterInfo) {
149 const bool low_latency_out = audio_manager()->IsLowLatencyPlayoutSupported();
150 const bool low_latency_in = audio_manager()->IsLowLatencyRecordSupported();
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());
155 PRINT("%schannels: %zu\n", kTag, playout_parameters_.channels());
156 PRINT("%sframes per buffer: %zu <=> %.2f ms\n", kTag,
157 playout_parameters_.frames_per_buffer(),
158 playout_parameters_.GetBufferSizeInMilliseconds());
159 PRINT("RECORD: \n");
160 PRINT("%saudio layer: %s\n", kTag,
161 low_latency_in ? "Low latency OpenSL" : "Java/JNI based AudioRecord");
162 PRINT("%ssample rate: %d Hz\n", kTag, record_parameters_.sample_rate());
163 PRINT("%schannels: %zu\n", kTag, record_parameters_.channels());
164 PRINT("%sframes per buffer: %zu <=> %.2f ms\n", kTag,
165 record_parameters_.frames_per_buffer(),
166 record_parameters_.GetBufferSizeInMilliseconds());
167 }
168
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.
TEST_F(AudioManagerTest,VerifyAudioParameters)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
184 // Add device-specific information to the test for logging purposes.
TEST_F(AudioManagerTest,ShowDeviceInfo)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());
189 PRINT("%smanufacturer: %s\n", kTag,
190 build_info.GetDeviceManufacturer().c_str());
191 }
192
193 // Add Android build information to the test for logging purposes.
TEST_F(AudioManagerTest,ShowBuildInfo)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());
199 PRINT("%sSDK version: %d\n", kTag, build_info.GetSdkVersion());
200 }
201
202 // Basic test of the AudioParameters class using default construction where
203 // all members are set to zero.
TEST_F(AudioManagerTest,AudioParametersWithDefaultConstruction)204 TEST_F(AudioManagerTest, AudioParametersWithDefaultConstruction) {
205 AudioParameters params;
206 EXPECT_FALSE(params.is_valid());
207 EXPECT_EQ(0, params.sample_rate());
208 EXPECT_EQ(0U, params.channels());
209 EXPECT_EQ(0U, params.frames_per_buffer());
210 EXPECT_EQ(0U, params.frames_per_10ms_buffer());
211 EXPECT_EQ(0U, params.GetBytesPerFrame());
212 EXPECT_EQ(0U, params.GetBytesPerBuffer());
213 EXPECT_EQ(0U, params.GetBytesPer10msBuffer());
214 EXPECT_EQ(0.0f, params.GetBufferSizeInMilliseconds());
215 }
216
217 // Basic test of the AudioParameters class using non default construction.
TEST_F(AudioManagerTest,AudioParametersWithNonDefaultConstruction)218 TEST_F(AudioManagerTest, AudioParametersWithNonDefaultConstruction) {
219 const int kSampleRate = 48000;
220 const size_t kChannels = 1;
221 const size_t kFramesPerBuffer = 480;
222 const size_t kFramesPer10msBuffer = 480;
223 const size_t kBytesPerFrame = 2;
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());
230 EXPECT_EQ(static_cast<size_t>(kSampleRate / 100),
231 params.frames_per_10ms_buffer());
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
240