blob: d540866c094138497319905325a7b2dcd57b9abc [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,
Hayden Gomes3e8bbb92020-01-10 13:37:05 -0800134 AAUDIO_USAGE_ASSISTANT,
135 AAUDIO_SYSTEM_USAGE_EMERGENCY,
136 AAUDIO_SYSTEM_USAGE_SAFETY,
137 AAUDIO_SYSTEM_USAGE_VEHICLE_STATUS,
138 AAUDIO_SYSTEM_USAGE_ANNOUNCEMENT
Phil Burkf9d1b992017-12-21 16:54:18 -0800139};
140
141static 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
150static 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,
Eric Laurentae4b6ec2019-01-15 18:34:38 -0800158 AAUDIO_INPUT_PRESET_VOICE_PERFORMANCE,
Phil Burkf9d1b992017-12-21 16:54:18 -0800159};
160
Kevin Rocard68646ba2019-03-20 13:26:49 -0700161static 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
Eric Laurent76373c22020-01-14 12:38:14 -0800169static const int sPrivacyModes[] = {
170 DONT_SET,
171 false,
172 true,
173};
174
Phil Burkf9d1b992017-12-21 16:54:18 -0800175static void checkAttributesUsage(aaudio_performance_mode_t perfMode) {
176 for (aaudio_usage_t usage : sUsages) {
177 checkAttributes(perfMode, usage, DONT_SET);
178 }
179}
180
Kevin Rocard68646ba2019-03-20 13:26:49 -0700181static void checkAttributesContentType(aaudio_performance_mode_t perfMode) {
Phil Burkf9d1b992017-12-21 16:54:18 -0800182 for (aaudio_content_type_t contentType : sContentypes) {
183 checkAttributes(perfMode, DONT_SET, contentType);
184 }
185}
186
187static 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,
Kevin Rocard68646ba2019-03-20 13:26:49 -0700193 DONT_SET,
Eric Laurent76373c22020-01-14 12:38:14 -0800194 DONT_SET,
Kevin Rocard68646ba2019-03-20 13:26:49 -0700195 AAUDIO_DIRECTION_INPUT);
196 }
197}
198
199static 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,
Phil Burkf9d1b992017-12-21 16:54:18 -0800206 AAUDIO_DIRECTION_INPUT);
207 }
208}
209
Eric Laurent76373c22020-01-14 12:38:14 -0800210static 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
Phil Burkf9d1b992017-12-21 16:54:18 -0800222TEST(test_attributes, aaudio_usage_perfnone) {
223 checkAttributesUsage(AAUDIO_PERFORMANCE_MODE_NONE);
224}
225
226TEST(test_attributes, aaudio_content_type_perfnone) {
227 checkAttributesContentType(AAUDIO_PERFORMANCE_MODE_NONE);
228}
229
230TEST(test_attributes, aaudio_input_preset_perfnone) {
231 checkAttributesInputPreset(AAUDIO_PERFORMANCE_MODE_NONE);
232}
233
Kevin Rocard68646ba2019-03-20 13:26:49 -0700234TEST(test_attributes, aaudio_allowed_capture_policy_perfnone) {
235 checkAttributesAllowedCapturePolicy(AAUDIO_PERFORMANCE_MODE_NONE);
236}
237
Phil Burkf9d1b992017-12-21 16:54:18 -0800238TEST(test_attributes, aaudio_usage_lowlat) {
239 checkAttributesUsage(AAUDIO_PERFORMANCE_MODE_LOW_LATENCY);
240}
241
242TEST(test_attributes, aaudio_content_type_lowlat) {
243 checkAttributesContentType(AAUDIO_PERFORMANCE_MODE_LOW_LATENCY);
244}
245
246TEST(test_attributes, aaudio_input_preset_lowlat) {
247 checkAttributesInputPreset(AAUDIO_PERFORMANCE_MODE_LOW_LATENCY);
248}
Kevin Rocard68646ba2019-03-20 13:26:49 -0700249
250TEST(test_attributes, aaudio_allowed_capture_policy_lowlat) {
251 checkAttributesAllowedCapturePolicy(AAUDIO_PERFORMANCE_MODE_LOW_LATENCY);
252}
Eric Laurent76373c22020-01-14 12:38:14 -0800253
254TEST(test_attributes, aaudio_allowed_privacy_sensitive_lowlat) {
255 checkAttributesPrivacySensitive(AAUDIO_PERFORMANCE_MODE_LOW_LATENCY);
256}