blob: 3bab25ce4e4d9d1204f63adc22a1beec67204f5f [file] [log] [blame]
Phil Burkf9d1b992017-12-21 16:54:18 -08001/*
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
25constexpr int64_t kNanosPerSecond = 1000000000;
26constexpr int kNumFrames = 256;
27constexpr int kChannelCount = 2;
28
29constexpr int32_t DONT_SET = -1000;
30
31static 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,
Kevin Rocard68646ba2019-03-20 13:26:49 -070035 aaudio_allowed_capture_policy_t capturePolicy = DONT_SET,
Eric Laurent76373c22020-01-14 12:38:14 -080036 int privacyMode = DONT_SET,
Phil Burkf9d1b992017-12-21 16:54:18 -080037 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 }
Kevin Rocard68646ba2019-03-20 13:26:49 -070061 if (capturePolicy != DONT_SET) {
62 AAudioStreamBuilder_setAllowedCapturePolicy(aaudioBuilder, capturePolicy);
63 }
Eric Laurent76373c22020-01-14 12:38:14 -080064 if (privacyMode != DONT_SET) {
65 AAudioStreamBuilder_setPrivacySensitive(aaudioBuilder, (bool)privacyMode);
66 }
Phil Burkf9d1b992017-12-21 16:54:18 -080067
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)
Phil Burkeaef9b92018-01-18 09:09:42 -080087 ? AAUDIO_INPUT_PRESET_VOICE_RECOGNITION // default
Phil Burkf9d1b992017-12-21 16:54:18 -080088 : preset;
89 EXPECT_EQ(expectedPreset, AAudioStream_getInputPreset(aaudioStream));
90
Kevin Rocard68646ba2019-03-20 13:26:49 -070091 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
Eric Laurent76373c22020-01-14 12:38:14 -080097 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
Phil Burkf9d1b992017-12-21 16:54:18 -0800104 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
120static 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};
136
137static const aaudio_content_type_t sContentypes[] = {
138 DONT_SET,
139 AAUDIO_UNSPECIFIED,
140 AAUDIO_CONTENT_TYPE_SPEECH,
141 AAUDIO_CONTENT_TYPE_MUSIC,
142 AAUDIO_CONTENT_TYPE_MOVIE,
143 AAUDIO_CONTENT_TYPE_SONIFICATION
144};
145
146static const aaudio_input_preset_t sInputPresets[] = {
147 DONT_SET,
148 AAUDIO_UNSPECIFIED,
149 AAUDIO_INPUT_PRESET_GENERIC,
150 AAUDIO_INPUT_PRESET_CAMCORDER,
151 AAUDIO_INPUT_PRESET_VOICE_RECOGNITION,
152 AAUDIO_INPUT_PRESET_VOICE_COMMUNICATION,
153 AAUDIO_INPUT_PRESET_UNPROCESSED,
Eric Laurentae4b6ec2019-01-15 18:34:38 -0800154 AAUDIO_INPUT_PRESET_VOICE_PERFORMANCE,
Phil Burkf9d1b992017-12-21 16:54:18 -0800155};
156
Kevin Rocard68646ba2019-03-20 13:26:49 -0700157static const aaudio_input_preset_t sAllowCapturePolicies[] = {
158 DONT_SET,
159 AAUDIO_UNSPECIFIED,
160 AAUDIO_ALLOW_CAPTURE_BY_ALL,
161 AAUDIO_ALLOW_CAPTURE_BY_SYSTEM,
162 AAUDIO_ALLOW_CAPTURE_BY_NONE,
163};
164
Eric Laurent76373c22020-01-14 12:38:14 -0800165static const int sPrivacyModes[] = {
166 DONT_SET,
167 false,
168 true,
169};
170
Phil Burkf9d1b992017-12-21 16:54:18 -0800171static void checkAttributesUsage(aaudio_performance_mode_t perfMode) {
172 for (aaudio_usage_t usage : sUsages) {
173 checkAttributes(perfMode, usage, DONT_SET);
174 }
175}
176
Kevin Rocard68646ba2019-03-20 13:26:49 -0700177static void checkAttributesContentType(aaudio_performance_mode_t perfMode) {
Phil Burkf9d1b992017-12-21 16:54:18 -0800178 for (aaudio_content_type_t contentType : sContentypes) {
179 checkAttributes(perfMode, DONT_SET, contentType);
180 }
181}
182
183static void checkAttributesInputPreset(aaudio_performance_mode_t perfMode) {
184 for (aaudio_input_preset_t inputPreset : sInputPresets) {
185 checkAttributes(perfMode,
186 DONT_SET,
187 DONT_SET,
188 inputPreset,
Kevin Rocard68646ba2019-03-20 13:26:49 -0700189 DONT_SET,
Eric Laurent76373c22020-01-14 12:38:14 -0800190 DONT_SET,
Kevin Rocard68646ba2019-03-20 13:26:49 -0700191 AAUDIO_DIRECTION_INPUT);
192 }
193}
194
195static void checkAttributesAllowedCapturePolicy(aaudio_performance_mode_t perfMode) {
196 for (aaudio_allowed_capture_policy_t policy : sAllowCapturePolicies) {
197 checkAttributes(perfMode,
198 DONT_SET,
199 DONT_SET,
200 DONT_SET,
201 policy,
Phil Burkf9d1b992017-12-21 16:54:18 -0800202 AAUDIO_DIRECTION_INPUT);
203 }
204}
205
Eric Laurent76373c22020-01-14 12:38:14 -0800206static void checkAttributesPrivacySensitive(aaudio_performance_mode_t perfMode) {
207 for (int privacyMode : sPrivacyModes) {
208 checkAttributes(perfMode,
209 DONT_SET,
210 DONT_SET,
211 DONT_SET,
212 DONT_SET,
213 privacyMode,
214 AAUDIO_DIRECTION_INPUT);
215 }
216}
217
Phil Burkf9d1b992017-12-21 16:54:18 -0800218TEST(test_attributes, aaudio_usage_perfnone) {
219 checkAttributesUsage(AAUDIO_PERFORMANCE_MODE_NONE);
220}
221
222TEST(test_attributes, aaudio_content_type_perfnone) {
223 checkAttributesContentType(AAUDIO_PERFORMANCE_MODE_NONE);
224}
225
226TEST(test_attributes, aaudio_input_preset_perfnone) {
227 checkAttributesInputPreset(AAUDIO_PERFORMANCE_MODE_NONE);
228}
229
Kevin Rocard68646ba2019-03-20 13:26:49 -0700230TEST(test_attributes, aaudio_allowed_capture_policy_perfnone) {
231 checkAttributesAllowedCapturePolicy(AAUDIO_PERFORMANCE_MODE_NONE);
232}
233
Phil Burkf9d1b992017-12-21 16:54:18 -0800234TEST(test_attributes, aaudio_usage_lowlat) {
235 checkAttributesUsage(AAUDIO_PERFORMANCE_MODE_LOW_LATENCY);
236}
237
238TEST(test_attributes, aaudio_content_type_lowlat) {
239 checkAttributesContentType(AAUDIO_PERFORMANCE_MODE_LOW_LATENCY);
240}
241
242TEST(test_attributes, aaudio_input_preset_lowlat) {
243 checkAttributesInputPreset(AAUDIO_PERFORMANCE_MODE_LOW_LATENCY);
244}
Kevin Rocard68646ba2019-03-20 13:26:49 -0700245
246TEST(test_attributes, aaudio_allowed_capture_policy_lowlat) {
247 checkAttributesAllowedCapturePolicy(AAUDIO_PERFORMANCE_MODE_LOW_LATENCY);
248}
Eric Laurent76373c22020-01-14 12:38:14 -0800249
250TEST(test_attributes, aaudio_allowed_privacy_sensitive_lowlat) {
251 checkAttributesPrivacySensitive(AAUDIO_PERFORMANCE_MODE_LOW_LATENCY);
252}