blob: b88d5627ef0a3d3937dcdaae966d216fabe0f48c [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
Phil Burkdca60f32021-09-21 21:48:16 +000019// TODO Many of these tests are duplicates of CTS tests in
20// "test_aaudio_attributes.cpp". That other file is more current.
21// So these tests could be deleted.
22
Phil Burkf9d1b992017-12-21 16:54:18 -080023#include <stdio.h>
24#include <unistd.h>
25
26#include <aaudio/AAudio.h>
27#include <gtest/gtest.h>
28
29constexpr int64_t kNanosPerSecond = 1000000000;
30constexpr int kNumFrames = 256;
31constexpr int kChannelCount = 2;
32
33constexpr int32_t DONT_SET = -1000;
34
35static void checkAttributes(aaudio_performance_mode_t perfMode,
36 aaudio_usage_t usage,
37 aaudio_content_type_t contentType,
38 aaudio_input_preset_t preset = DONT_SET,
Kevin Rocard68646ba2019-03-20 13:26:49 -070039 aaudio_allowed_capture_policy_t capturePolicy = DONT_SET,
Eric Laurent76373c22020-01-14 12:38:14 -080040 int privacyMode = DONT_SET,
Phil Burkf9d1b992017-12-21 16:54:18 -080041 aaudio_direction_t direction = AAUDIO_DIRECTION_OUTPUT) {
42
43 float *buffer = new float[kNumFrames * kChannelCount];
44
45 AAudioStreamBuilder *aaudioBuilder = nullptr;
46 AAudioStream *aaudioStream = nullptr;
47
48 // Use an AAudioStreamBuilder to contain requested parameters.
49 ASSERT_EQ(AAUDIO_OK, AAudio_createStreamBuilder(&aaudioBuilder));
50
51 // Request stream properties.
52 AAudioStreamBuilder_setPerformanceMode(aaudioBuilder, perfMode);
53 AAudioStreamBuilder_setDirection(aaudioBuilder, direction);
54
55 // Set the attribute in the builder.
56 if (usage != DONT_SET) {
57 AAudioStreamBuilder_setUsage(aaudioBuilder, usage);
58 }
59 if (contentType != DONT_SET) {
60 AAudioStreamBuilder_setContentType(aaudioBuilder, contentType);
61 }
62 if (preset != DONT_SET) {
63 AAudioStreamBuilder_setInputPreset(aaudioBuilder, preset);
64 }
Kevin Rocard68646ba2019-03-20 13:26:49 -070065 if (capturePolicy != DONT_SET) {
66 AAudioStreamBuilder_setAllowedCapturePolicy(aaudioBuilder, capturePolicy);
67 }
Eric Laurent76373c22020-01-14 12:38:14 -080068 if (privacyMode != DONT_SET) {
69 AAudioStreamBuilder_setPrivacySensitive(aaudioBuilder, (bool)privacyMode);
70 }
Phil Burkf9d1b992017-12-21 16:54:18 -080071
72 // Create an AAudioStream using the Builder.
73 ASSERT_EQ(AAUDIO_OK, AAudioStreamBuilder_openStream(aaudioBuilder, &aaudioStream));
74 AAudioStreamBuilder_delete(aaudioBuilder);
75
76 // Make sure we get the same attributes back from the stream.
77 aaudio_usage_t expectedUsage =
78 (usage == DONT_SET || usage == AAUDIO_UNSPECIFIED)
79 ? AAUDIO_USAGE_MEDIA // default
80 : usage;
81 EXPECT_EQ(expectedUsage, AAudioStream_getUsage(aaudioStream));
82
83 aaudio_content_type_t expectedContentType =
84 (contentType == DONT_SET || contentType == AAUDIO_UNSPECIFIED)
85 ? AAUDIO_CONTENT_TYPE_MUSIC // default
86 : contentType;
87 EXPECT_EQ(expectedContentType, AAudioStream_getContentType(aaudioStream));
88
89 aaudio_input_preset_t expectedPreset =
90 (preset == DONT_SET || preset == AAUDIO_UNSPECIFIED)
Phil Burkeaef9b92018-01-18 09:09:42 -080091 ? AAUDIO_INPUT_PRESET_VOICE_RECOGNITION // default
Phil Burkf9d1b992017-12-21 16:54:18 -080092 : preset;
93 EXPECT_EQ(expectedPreset, AAudioStream_getInputPreset(aaudioStream));
94
Kevin Rocard68646ba2019-03-20 13:26:49 -070095 aaudio_allowed_capture_policy_t expectedCapturePolicy =
96 (capturePolicy == DONT_SET || capturePolicy == AAUDIO_UNSPECIFIED)
97 ? AAUDIO_ALLOW_CAPTURE_BY_ALL // default
Phil Burkdca60f32021-09-21 21:48:16 +000098 : capturePolicy;
Kevin Rocard68646ba2019-03-20 13:26:49 -070099 EXPECT_EQ(expectedCapturePolicy, AAudioStream_getAllowedCapturePolicy(aaudioStream));
100
Eric Laurent76373c22020-01-14 12:38:14 -0800101 bool expectedPrivacyMode =
102 (privacyMode == DONT_SET) ?
103 ((preset == AAUDIO_INPUT_PRESET_VOICE_COMMUNICATION
104 || preset == AAUDIO_INPUT_PRESET_CAMCORDER) ? true : false) :
105 privacyMode;
106 EXPECT_EQ(expectedPrivacyMode, AAudioStream_isPrivacySensitive(aaudioStream));
107
Phil Burkf9d1b992017-12-21 16:54:18 -0800108 EXPECT_EQ(AAUDIO_OK, AAudioStream_requestStart(aaudioStream));
109
110 if (direction == AAUDIO_DIRECTION_INPUT) {
111 EXPECT_EQ(kNumFrames,
112 AAudioStream_read(aaudioStream, buffer, kNumFrames, kNanosPerSecond));
113 } else {
114 EXPECT_EQ(kNumFrames,
115 AAudioStream_write(aaudioStream, buffer, kNumFrames, kNanosPerSecond));
116 }
117
118 EXPECT_EQ(AAUDIO_OK, AAudioStream_requestStop(aaudioStream));
119
120 EXPECT_EQ(AAUDIO_OK, AAudioStream_close(aaudioStream));
121 delete[] buffer;
122}
123
124static const aaudio_usage_t sUsages[] = {
125 DONT_SET,
126 AAUDIO_UNSPECIFIED,
127 AAUDIO_USAGE_MEDIA,
128 AAUDIO_USAGE_VOICE_COMMUNICATION,
129 AAUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING,
130 AAUDIO_USAGE_ALARM,
131 AAUDIO_USAGE_NOTIFICATION,
132 AAUDIO_USAGE_NOTIFICATION_RINGTONE,
133 AAUDIO_USAGE_NOTIFICATION_EVENT,
134 AAUDIO_USAGE_ASSISTANCE_ACCESSIBILITY,
135 AAUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE,
136 AAUDIO_USAGE_ASSISTANCE_SONIFICATION,
137 AAUDIO_USAGE_GAME,
Hayden Gomes3e8bbb92020-01-10 13:37:05 -0800138 AAUDIO_USAGE_ASSISTANT,
Phil Burkdca60f32021-09-21 21:48:16 +0000139 // Note that the AAUDIO_SYSTEM_USAGE_* values requires special permission.
Phil Burkf9d1b992017-12-21 16:54:18 -0800140};
141
142static const aaudio_content_type_t sContentypes[] = {
143 DONT_SET,
144 AAUDIO_UNSPECIFIED,
145 AAUDIO_CONTENT_TYPE_SPEECH,
146 AAUDIO_CONTENT_TYPE_MUSIC,
147 AAUDIO_CONTENT_TYPE_MOVIE,
148 AAUDIO_CONTENT_TYPE_SONIFICATION
149};
150
151static const aaudio_input_preset_t sInputPresets[] = {
152 DONT_SET,
153 AAUDIO_UNSPECIFIED,
154 AAUDIO_INPUT_PRESET_GENERIC,
155 AAUDIO_INPUT_PRESET_CAMCORDER,
156 AAUDIO_INPUT_PRESET_VOICE_RECOGNITION,
157 AAUDIO_INPUT_PRESET_VOICE_COMMUNICATION,
158 AAUDIO_INPUT_PRESET_UNPROCESSED,
Eric Laurentae4b6ec2019-01-15 18:34:38 -0800159 AAUDIO_INPUT_PRESET_VOICE_PERFORMANCE,
Phil Burkf9d1b992017-12-21 16:54:18 -0800160};
161
Kevin Rocard68646ba2019-03-20 13:26:49 -0700162static const aaudio_input_preset_t sAllowCapturePolicies[] = {
163 DONT_SET,
164 AAUDIO_UNSPECIFIED,
165 AAUDIO_ALLOW_CAPTURE_BY_ALL,
166 AAUDIO_ALLOW_CAPTURE_BY_SYSTEM,
167 AAUDIO_ALLOW_CAPTURE_BY_NONE,
168};
169
Eric Laurent76373c22020-01-14 12:38:14 -0800170static const int sPrivacyModes[] = {
171 DONT_SET,
172 false,
173 true,
174};
175
Phil Burkf9d1b992017-12-21 16:54:18 -0800176static void checkAttributesUsage(aaudio_performance_mode_t perfMode) {
177 for (aaudio_usage_t usage : sUsages) {
178 checkAttributes(perfMode, usage, DONT_SET);
179 }
180}
181
Kevin Rocard68646ba2019-03-20 13:26:49 -0700182static void checkAttributesContentType(aaudio_performance_mode_t perfMode) {
Phil Burkf9d1b992017-12-21 16:54:18 -0800183 for (aaudio_content_type_t contentType : sContentypes) {
184 checkAttributes(perfMode, DONT_SET, contentType);
185 }
186}
187
188static void checkAttributesInputPreset(aaudio_performance_mode_t perfMode) {
189 for (aaudio_input_preset_t inputPreset : sInputPresets) {
190 checkAttributes(perfMode,
191 DONT_SET,
192 DONT_SET,
193 inputPreset,
Kevin Rocard68646ba2019-03-20 13:26:49 -0700194 DONT_SET,
Eric Laurent76373c22020-01-14 12:38:14 -0800195 DONT_SET,
Kevin Rocard68646ba2019-03-20 13:26:49 -0700196 AAUDIO_DIRECTION_INPUT);
197 }
198}
199
200static void checkAttributesAllowedCapturePolicy(aaudio_performance_mode_t perfMode) {
201 for (aaudio_allowed_capture_policy_t policy : sAllowCapturePolicies) {
202 checkAttributes(perfMode,
203 DONT_SET,
204 DONT_SET,
205 DONT_SET,
206 policy,
Phil Burkf9d1b992017-12-21 16:54:18 -0800207 AAUDIO_DIRECTION_INPUT);
208 }
209}
210
Eric Laurent76373c22020-01-14 12:38:14 -0800211static void checkAttributesPrivacySensitive(aaudio_performance_mode_t perfMode) {
212 for (int privacyMode : sPrivacyModes) {
213 checkAttributes(perfMode,
214 DONT_SET,
215 DONT_SET,
216 DONT_SET,
217 DONT_SET,
218 privacyMode,
219 AAUDIO_DIRECTION_INPUT);
220 }
221}
222
Phil Burkf9d1b992017-12-21 16:54:18 -0800223TEST(test_attributes, aaudio_usage_perfnone) {
224 checkAttributesUsage(AAUDIO_PERFORMANCE_MODE_NONE);
225}
226
227TEST(test_attributes, aaudio_content_type_perfnone) {
228 checkAttributesContentType(AAUDIO_PERFORMANCE_MODE_NONE);
229}
230
231TEST(test_attributes, aaudio_input_preset_perfnone) {
232 checkAttributesInputPreset(AAUDIO_PERFORMANCE_MODE_NONE);
233}
234
Kevin Rocard68646ba2019-03-20 13:26:49 -0700235TEST(test_attributes, aaudio_allowed_capture_policy_perfnone) {
236 checkAttributesAllowedCapturePolicy(AAUDIO_PERFORMANCE_MODE_NONE);
237}
238
Phil Burkf9d1b992017-12-21 16:54:18 -0800239TEST(test_attributes, aaudio_usage_lowlat) {
240 checkAttributesUsage(AAUDIO_PERFORMANCE_MODE_LOW_LATENCY);
241}
242
243TEST(test_attributes, aaudio_content_type_lowlat) {
244 checkAttributesContentType(AAUDIO_PERFORMANCE_MODE_LOW_LATENCY);
245}
246
247TEST(test_attributes, aaudio_input_preset_lowlat) {
248 checkAttributesInputPreset(AAUDIO_PERFORMANCE_MODE_LOW_LATENCY);
249}
Kevin Rocard68646ba2019-03-20 13:26:49 -0700250
251TEST(test_attributes, aaudio_allowed_capture_policy_lowlat) {
252 checkAttributesAllowedCapturePolicy(AAUDIO_PERFORMANCE_MODE_LOW_LATENCY);
253}
Eric Laurent76373c22020-01-14 12:38:14 -0800254
255TEST(test_attributes, aaudio_allowed_privacy_sensitive_lowlat) {
256 checkAttributesPrivacySensitive(AAUDIO_PERFORMANCE_MODE_LOW_LATENCY);
257}