1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 // Test AAudio attributes such as Usage, ContentType and InputPreset.
18 
19 #include <stdio.h>
20 #include <unistd.h>
21 
22 #include <aaudio/AAudio.h>
23 #include <gtest/gtest.h>
24 
25 constexpr int64_t kNanosPerSecond = 1000000000;
26 constexpr int kNumFrames = 256;
27 constexpr int kChannelCount = 2;
28 
29 constexpr int32_t DONT_SET = -1000;
30 
checkAttributes(aaudio_performance_mode_t perfMode,aaudio_usage_t usage,aaudio_content_type_t contentType,aaudio_input_preset_t preset=DONT_SET,aaudio_allowed_capture_policy_t capturePolicy=DONT_SET,int privacyMode=DONT_SET,aaudio_direction_t direction=AAUDIO_DIRECTION_OUTPUT)31 static void checkAttributes(aaudio_performance_mode_t perfMode,
32                             aaudio_usage_t usage,
33                             aaudio_content_type_t contentType,
34                             aaudio_input_preset_t preset = DONT_SET,
35                             aaudio_allowed_capture_policy_t capturePolicy = DONT_SET,
36                             int privacyMode = DONT_SET,
37                             aaudio_direction_t direction = AAUDIO_DIRECTION_OUTPUT) {
38 
39     float *buffer = new float[kNumFrames * kChannelCount];
40 
41     AAudioStreamBuilder *aaudioBuilder = nullptr;
42     AAudioStream *aaudioStream = nullptr;
43 
44     // Use an AAudioStreamBuilder to contain requested parameters.
45     ASSERT_EQ(AAUDIO_OK, AAudio_createStreamBuilder(&aaudioBuilder));
46 
47     // Request stream properties.
48     AAudioStreamBuilder_setPerformanceMode(aaudioBuilder, perfMode);
49     AAudioStreamBuilder_setDirection(aaudioBuilder, direction);
50 
51     // Set the attribute in the builder.
52     if (usage != DONT_SET) {
53         AAudioStreamBuilder_setUsage(aaudioBuilder, usage);
54     }
55     if (contentType != DONT_SET) {
56         AAudioStreamBuilder_setContentType(aaudioBuilder, contentType);
57     }
58     if (preset != DONT_SET) {
59         AAudioStreamBuilder_setInputPreset(aaudioBuilder, preset);
60     }
61     if (capturePolicy != DONT_SET) {
62         AAudioStreamBuilder_setAllowedCapturePolicy(aaudioBuilder, capturePolicy);
63     }
64     if (privacyMode != DONT_SET) {
65         AAudioStreamBuilder_setPrivacySensitive(aaudioBuilder, (bool)privacyMode);
66     }
67 
68     // Create an AAudioStream using the Builder.
69     ASSERT_EQ(AAUDIO_OK, AAudioStreamBuilder_openStream(aaudioBuilder, &aaudioStream));
70     AAudioStreamBuilder_delete(aaudioBuilder);
71 
72     // Make sure we get the same attributes back from the stream.
73     aaudio_usage_t expectedUsage =
74             (usage == DONT_SET || usage == AAUDIO_UNSPECIFIED)
75             ? AAUDIO_USAGE_MEDIA // default
76             : usage;
77     EXPECT_EQ(expectedUsage, AAudioStream_getUsage(aaudioStream));
78 
79     aaudio_content_type_t expectedContentType =
80             (contentType == DONT_SET || contentType == AAUDIO_UNSPECIFIED)
81             ? AAUDIO_CONTENT_TYPE_MUSIC // default
82             : contentType;
83     EXPECT_EQ(expectedContentType, AAudioStream_getContentType(aaudioStream));
84 
85     aaudio_input_preset_t expectedPreset =
86             (preset == DONT_SET || preset == AAUDIO_UNSPECIFIED)
87             ? AAUDIO_INPUT_PRESET_VOICE_RECOGNITION // default
88             : preset;
89     EXPECT_EQ(expectedPreset, AAudioStream_getInputPreset(aaudioStream));
90 
91     aaudio_allowed_capture_policy_t expectedCapturePolicy =
92             (capturePolicy == DONT_SET || capturePolicy == AAUDIO_UNSPECIFIED)
93             ? AAUDIO_ALLOW_CAPTURE_BY_ALL // default
94             : preset;
95     EXPECT_EQ(expectedCapturePolicy, AAudioStream_getAllowedCapturePolicy(aaudioStream));
96 
97     bool expectedPrivacyMode =
98             (privacyMode == DONT_SET) ?
99                 ((preset == AAUDIO_INPUT_PRESET_VOICE_COMMUNICATION
100                     || preset == AAUDIO_INPUT_PRESET_CAMCORDER) ? true : false) :
101                 privacyMode;
102     EXPECT_EQ(expectedPrivacyMode, AAudioStream_isPrivacySensitive(aaudioStream));
103 
104     EXPECT_EQ(AAUDIO_OK, AAudioStream_requestStart(aaudioStream));
105 
106     if (direction == AAUDIO_DIRECTION_INPUT) {
107         EXPECT_EQ(kNumFrames,
108                   AAudioStream_read(aaudioStream, buffer, kNumFrames, kNanosPerSecond));
109     } else {
110         EXPECT_EQ(kNumFrames,
111                   AAudioStream_write(aaudioStream, buffer, kNumFrames, kNanosPerSecond));
112     }
113 
114     EXPECT_EQ(AAUDIO_OK, AAudioStream_requestStop(aaudioStream));
115 
116     EXPECT_EQ(AAUDIO_OK, AAudioStream_close(aaudioStream));
117     delete[] buffer;
118 }
119 
120 static const aaudio_usage_t sUsages[] = {
121     DONT_SET,
122     AAUDIO_UNSPECIFIED,
123     AAUDIO_USAGE_MEDIA,
124     AAUDIO_USAGE_VOICE_COMMUNICATION,
125     AAUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING,
126     AAUDIO_USAGE_ALARM,
127     AAUDIO_USAGE_NOTIFICATION,
128     AAUDIO_USAGE_NOTIFICATION_RINGTONE,
129     AAUDIO_USAGE_NOTIFICATION_EVENT,
130     AAUDIO_USAGE_ASSISTANCE_ACCESSIBILITY,
131     AAUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE,
132     AAUDIO_USAGE_ASSISTANCE_SONIFICATION,
133     AAUDIO_USAGE_GAME,
134     AAUDIO_USAGE_ASSISTANT,
135     AAUDIO_SYSTEM_USAGE_EMERGENCY,
136     AAUDIO_SYSTEM_USAGE_SAFETY,
137     AAUDIO_SYSTEM_USAGE_VEHICLE_STATUS,
138     AAUDIO_SYSTEM_USAGE_ANNOUNCEMENT
139 };
140 
141 static const aaudio_content_type_t sContentypes[] = {
142     DONT_SET,
143     AAUDIO_UNSPECIFIED,
144     AAUDIO_CONTENT_TYPE_SPEECH,
145     AAUDIO_CONTENT_TYPE_MUSIC,
146     AAUDIO_CONTENT_TYPE_MOVIE,
147     AAUDIO_CONTENT_TYPE_SONIFICATION
148 };
149 
150 static const aaudio_input_preset_t sInputPresets[] = {
151     DONT_SET,
152     AAUDIO_UNSPECIFIED,
153     AAUDIO_INPUT_PRESET_GENERIC,
154     AAUDIO_INPUT_PRESET_CAMCORDER,
155     AAUDIO_INPUT_PRESET_VOICE_RECOGNITION,
156     AAUDIO_INPUT_PRESET_VOICE_COMMUNICATION,
157     AAUDIO_INPUT_PRESET_UNPROCESSED,
158     AAUDIO_INPUT_PRESET_VOICE_PERFORMANCE,
159 };
160 
161 static const aaudio_input_preset_t sAllowCapturePolicies[] = {
162     DONT_SET,
163     AAUDIO_UNSPECIFIED,
164     AAUDIO_ALLOW_CAPTURE_BY_ALL,
165     AAUDIO_ALLOW_CAPTURE_BY_SYSTEM,
166     AAUDIO_ALLOW_CAPTURE_BY_NONE,
167 };
168 
169 static const int sPrivacyModes[] = {
170     DONT_SET,
171     false,
172     true,
173 };
174 
checkAttributesUsage(aaudio_performance_mode_t perfMode)175 static void checkAttributesUsage(aaudio_performance_mode_t perfMode) {
176     for (aaudio_usage_t usage : sUsages) {
177         checkAttributes(perfMode, usage, DONT_SET);
178     }
179 }
180 
checkAttributesContentType(aaudio_performance_mode_t perfMode)181 static void checkAttributesContentType(aaudio_performance_mode_t perfMode) {
182     for (aaudio_content_type_t contentType : sContentypes) {
183         checkAttributes(perfMode, DONT_SET, contentType);
184     }
185 }
186 
checkAttributesInputPreset(aaudio_performance_mode_t perfMode)187 static void checkAttributesInputPreset(aaudio_performance_mode_t perfMode) {
188     for (aaudio_input_preset_t inputPreset : sInputPresets) {
189         checkAttributes(perfMode,
190                         DONT_SET,
191                         DONT_SET,
192                         inputPreset,
193                         DONT_SET,
194                         DONT_SET,
195                         AAUDIO_DIRECTION_INPUT);
196     }
197 }
198 
checkAttributesAllowedCapturePolicy(aaudio_performance_mode_t perfMode)199 static void checkAttributesAllowedCapturePolicy(aaudio_performance_mode_t perfMode) {
200     for (aaudio_allowed_capture_policy_t policy : sAllowCapturePolicies) {
201         checkAttributes(perfMode,
202                         DONT_SET,
203                         DONT_SET,
204                         DONT_SET,
205                         policy,
206                         AAUDIO_DIRECTION_INPUT);
207     }
208 }
209 
checkAttributesPrivacySensitive(aaudio_performance_mode_t perfMode)210 static void checkAttributesPrivacySensitive(aaudio_performance_mode_t perfMode) {
211     for (int privacyMode : sPrivacyModes) {
212         checkAttributes(perfMode,
213                         DONT_SET,
214                         DONT_SET,
215                         DONT_SET,
216                         DONT_SET,
217                         privacyMode,
218                         AAUDIO_DIRECTION_INPUT);
219     }
220 }
221 
TEST(test_attributes,aaudio_usage_perfnone)222 TEST(test_attributes, aaudio_usage_perfnone) {
223     checkAttributesUsage(AAUDIO_PERFORMANCE_MODE_NONE);
224 }
225 
TEST(test_attributes,aaudio_content_type_perfnone)226 TEST(test_attributes, aaudio_content_type_perfnone) {
227     checkAttributesContentType(AAUDIO_PERFORMANCE_MODE_NONE);
228 }
229 
TEST(test_attributes,aaudio_input_preset_perfnone)230 TEST(test_attributes, aaudio_input_preset_perfnone) {
231     checkAttributesInputPreset(AAUDIO_PERFORMANCE_MODE_NONE);
232 }
233 
TEST(test_attributes,aaudio_allowed_capture_policy_perfnone)234 TEST(test_attributes, aaudio_allowed_capture_policy_perfnone) {
235     checkAttributesAllowedCapturePolicy(AAUDIO_PERFORMANCE_MODE_NONE);
236 }
237 
TEST(test_attributes,aaudio_usage_lowlat)238 TEST(test_attributes, aaudio_usage_lowlat) {
239     checkAttributesUsage(AAUDIO_PERFORMANCE_MODE_LOW_LATENCY);
240 }
241 
TEST(test_attributes,aaudio_content_type_lowlat)242 TEST(test_attributes, aaudio_content_type_lowlat) {
243     checkAttributesContentType(AAUDIO_PERFORMANCE_MODE_LOW_LATENCY);
244 }
245 
TEST(test_attributes,aaudio_input_preset_lowlat)246 TEST(test_attributes, aaudio_input_preset_lowlat) {
247     checkAttributesInputPreset(AAUDIO_PERFORMANCE_MODE_LOW_LATENCY);
248 }
249 
TEST(test_attributes,aaudio_allowed_capture_policy_lowlat)250 TEST(test_attributes, aaudio_allowed_capture_policy_lowlat) {
251     checkAttributesAllowedCapturePolicy(AAUDIO_PERFORMANCE_MODE_LOW_LATENCY);
252 }
253 
TEST(test_attributes,aaudio_allowed_privacy_sensitive_lowlat)254 TEST(test_attributes, aaudio_allowed_privacy_sensitive_lowlat) {
255     checkAttributesPrivacySensitive(AAUDIO_PERFORMANCE_MODE_LOW_LATENCY);
256 }
257