blob: ed713cbede16e3e1ca37a62ac8f5c9088919dce1 [file] [log] [blame]
henrikab2619892015-05-18 16:49:16 +02001/*
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
henrika521f7a82016-05-31 07:03:17 -070011#include <SLES/OpenSLES_Android.h>
kwibergf01633e2016-02-24 05:00:36 -080012
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020013#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"
henrikab2619892015-05-18 16:49:16 +020019
20#define PRINT(...) fprintf(stderr, __VA_ARGS__);
21
22namespace webrtc {
23
24static const char kTag[] = " ";
25
26class 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
henrika521f7a82016-05-31 07:03:17 -070048 // 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
kwibergf01633e2016-02-24 05:00:36 -080071 std::unique_ptr<AudioManager> audio_manager_;
henrikab2619892015-05-18 16:49:16 +020072 AudioParameters playout_parameters_;
73 AudioParameters record_parameters_;
74};
75
Yves Gerey665174f2018-06-19 15:03:05 +020076TEST_F(AudioManagerTest, ConstructDestruct) {}
henrikab2619892015-05-18 16:49:16 +020077
henrika521f7a82016-05-31 07:03:17 -070078// It should not be possible to create an OpenSL engine object if Java based
79// audio is requested in both directions.
80TEST_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.
88TEST_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
henrikab2619892015-05-18 16:49:16 +0200104TEST_F(AudioManagerTest, InitClose) {
105 EXPECT_TRUE(audio_manager()->Init());
106 EXPECT_TRUE(audio_manager()->Close());
107}
108
109TEST_F(AudioManagerTest, IsAcousticEchoCancelerSupported) {
110 PRINT("%sAcoustic Echo Canceler support: %s\n", kTag,
111 audio_manager()->IsAcousticEchoCancelerSupported() ? "Yes" : "No");
112}
113
henrikac14f5ff2015-09-23 14:08:33 +0200114TEST_F(AudioManagerTest, IsAutomaticGainControlSupported) {
henrikadefc21e2016-10-11 01:29:09 -0700115 EXPECT_FALSE(audio_manager()->IsAutomaticGainControlSupported());
henrikac14f5ff2015-09-23 14:08:33 +0200116}
117
118TEST_F(AudioManagerTest, IsNoiseSuppressorSupported) {
119 PRINT("%sNoise Suppressor support: %s\n", kTag,
120 audio_manager()->IsNoiseSuppressorSupported() ? "Yes" : "No");
121}
122
henrikab2619892015-05-18 16:49:16 +0200123TEST_F(AudioManagerTest, IsLowLatencyPlayoutSupported) {
124 PRINT("%sLow latency output support: %s\n", kTag,
125 audio_manager()->IsLowLatencyPlayoutSupported() ? "Yes" : "No");
126}
127
henrika918b5542016-09-19 15:44:09 +0200128TEST_F(AudioManagerTest, IsLowLatencyRecordSupported) {
129 PRINT("%sLow latency input support: %s\n", kTag,
130 audio_manager()->IsLowLatencyRecordSupported() ? "Yes" : "No");
131}
132
henrika1f0ad102016-05-25 05:15:10 -0700133TEST_F(AudioManagerTest, IsProAudioSupported) {
134 PRINT("%sPro audio support: %s\n", kTag,
135 audio_manager()->IsProAudioSupported() ? "Yes" : "No");
136}
137
henrika76535de2017-09-11 01:25:55 -0700138// Verify that playout side is configured for mono by default.
139TEST_F(AudioManagerTest, IsStereoPlayoutSupported) {
140 EXPECT_FALSE(audio_manager()->IsStereoPlayoutSupported());
141}
142
143// Verify that recording side is configured for mono by default.
144TEST_F(AudioManagerTest, IsStereoRecordSupported) {
145 EXPECT_FALSE(audio_manager()->IsStereoRecordSupported());
146}
147
henrikab2619892015-05-18 16:49:16 +0200148TEST_F(AudioManagerTest, ShowAudioParameterInfo) {
149 const bool low_latency_out = audio_manager()->IsLowLatencyPlayoutSupported();
henrika918b5542016-09-19 15:44:09 +0200150 const bool low_latency_in = audio_manager()->IsLowLatencyRecordSupported();
henrikab2619892015-05-18 16:49:16 +0200151 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 Kasting69558702016-01-12 16:26:35 -0800155 PRINT("%schannels: %" PRIuS "\n", kTag, playout_parameters_.channels());
Peter Kasting1380e262015-08-28 17:31:03 -0700156 PRINT("%sframes per buffer: %" PRIuS " <=> %.2f ms\n", kTag,
henrikab2619892015-05-18 16:49:16 +0200157 playout_parameters_.frames_per_buffer(),
158 playout_parameters_.GetBufferSizeInMilliseconds());
159 PRINT("RECORD: \n");
henrika918b5542016-09-19 15:44:09 +0200160 PRINT("%saudio layer: %s\n", kTag,
161 low_latency_in ? "Low latency OpenSL" : "Java/JNI based AudioRecord");
henrikab2619892015-05-18 16:49:16 +0200162 PRINT("%ssample rate: %d Hz\n", kTag, record_parameters_.sample_rate());
Peter Kasting69558702016-01-12 16:26:35 -0800163 PRINT("%schannels: %" PRIuS "\n", kTag, record_parameters_.channels());
Peter Kasting1380e262015-08-28 17:31:03 -0700164 PRINT("%sframes per buffer: %" PRIuS " <=> %.2f ms\n", kTag,
henrikab2619892015-05-18 16:49:16 +0200165 record_parameters_.frames_per_buffer(),
166 record_parameters_.GetBufferSizeInMilliseconds());
167}
168
henrika918b5542016-09-19 15:44:09 +0200169// 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.
173TEST_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
henrikab2619892015-05-18 16:49:16 +0200184// Add device-specific information to the test for logging purposes.
185TEST_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 Gerey665174f2018-06-19 15:03:05 +0200189 PRINT("%smanufacturer: %s\n", kTag,
190 build_info.GetDeviceManufacturer().c_str());
henrikab2619892015-05-18 16:49:16 +0200191}
192
193// Add Android build information to the test for logging purposes.
194TEST_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());
henrika918b5542016-09-19 15:44:09 +0200199 PRINT("%sSDK version: %d\n", kTag, build_info.GetSdkVersion());
henrikab2619892015-05-18 16:49:16 +0200200}
201
202// Basic test of the AudioParameters class using default construction where
203// all members are set to zero.
204TEST_F(AudioManagerTest, AudioParametersWithDefaultConstruction) {
205 AudioParameters params;
206 EXPECT_FALSE(params.is_valid());
207 EXPECT_EQ(0, params.sample_rate());
Peter Kasting69558702016-01-12 16:26:35 -0800208 EXPECT_EQ(0U, params.channels());
Peter Kasting1380e262015-08-28 17:31:03 -0700209 EXPECT_EQ(0U, params.frames_per_buffer());
Peter Kastingdce40cf2015-08-24 14:52:23 -0700210 EXPECT_EQ(0U, params.frames_per_10ms_buffer());
Peter Kasting1380e262015-08-28 17:31:03 -0700211 EXPECT_EQ(0U, params.GetBytesPerFrame());
212 EXPECT_EQ(0U, params.GetBytesPerBuffer());
Peter Kastingdce40cf2015-08-24 14:52:23 -0700213 EXPECT_EQ(0U, params.GetBytesPer10msBuffer());
henrikab2619892015-05-18 16:49:16 +0200214 EXPECT_EQ(0.0f, params.GetBufferSizeInMilliseconds());
215}
216
217// Basic test of the AudioParameters class using non default construction.
218TEST_F(AudioManagerTest, AudioParametersWithNonDefaultConstruction) {
219 const int kSampleRate = 48000;
Peter Kasting69558702016-01-12 16:26:35 -0800220 const size_t kChannels = 1;
Peter Kasting1380e262015-08-28 17:31:03 -0700221 const size_t kFramesPerBuffer = 480;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700222 const size_t kFramesPer10msBuffer = 480;
Peter Kasting1380e262015-08-28 17:31:03 -0700223 const size_t kBytesPerFrame = 2;
henrikab2619892015-05-18 16:49:16 +0200224 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 Kastingdce40cf2015-08-24 14:52:23 -0700230 EXPECT_EQ(static_cast<size_t>(kSampleRate / 100),
231 params.frames_per_10ms_buffer());
henrikab2619892015-05-18 16:49:16 +0200232 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