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