blob: 8513a7f815d012e62b553305457d1b909cb98711 [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,
Phil Burkf9d1b992017-12-21 16:54:18 -080036 aaudio_direction_t direction = AAUDIO_DIRECTION_OUTPUT) {
37
38 float *buffer = new float[kNumFrames * kChannelCount];
39
40 AAudioStreamBuilder *aaudioBuilder = nullptr;
41 AAudioStream *aaudioStream = nullptr;
42
43 // Use an AAudioStreamBuilder to contain requested parameters.
44 ASSERT_EQ(AAUDIO_OK, AAudio_createStreamBuilder(&aaudioBuilder));
45
46 // Request stream properties.
47 AAudioStreamBuilder_setPerformanceMode(aaudioBuilder, perfMode);
48 AAudioStreamBuilder_setDirection(aaudioBuilder, direction);
49
50 // Set the attribute in the builder.
51 if (usage != DONT_SET) {
52 AAudioStreamBuilder_setUsage(aaudioBuilder, usage);
53 }
54 if (contentType != DONT_SET) {
55 AAudioStreamBuilder_setContentType(aaudioBuilder, contentType);
56 }
57 if (preset != DONT_SET) {
58 AAudioStreamBuilder_setInputPreset(aaudioBuilder, preset);
59 }
Kevin Rocard68646ba2019-03-20 13:26:49 -070060 if (capturePolicy != DONT_SET) {
61 AAudioStreamBuilder_setAllowedCapturePolicy(aaudioBuilder, capturePolicy);
62 }
Phil Burkf9d1b992017-12-21 16:54:18 -080063
64 // Create an AAudioStream using the Builder.
65 ASSERT_EQ(AAUDIO_OK, AAudioStreamBuilder_openStream(aaudioBuilder, &aaudioStream));
66 AAudioStreamBuilder_delete(aaudioBuilder);
67
68 // Make sure we get the same attributes back from the stream.
69 aaudio_usage_t expectedUsage =
70 (usage == DONT_SET || usage == AAUDIO_UNSPECIFIED)
71 ? AAUDIO_USAGE_MEDIA // default
72 : usage;
73 EXPECT_EQ(expectedUsage, AAudioStream_getUsage(aaudioStream));
74
75 aaudio_content_type_t expectedContentType =
76 (contentType == DONT_SET || contentType == AAUDIO_UNSPECIFIED)
77 ? AAUDIO_CONTENT_TYPE_MUSIC // default
78 : contentType;
79 EXPECT_EQ(expectedContentType, AAudioStream_getContentType(aaudioStream));
80
81 aaudio_input_preset_t expectedPreset =
82 (preset == DONT_SET || preset == AAUDIO_UNSPECIFIED)
Phil Burkeaef9b92018-01-18 09:09:42 -080083 ? AAUDIO_INPUT_PRESET_VOICE_RECOGNITION // default
Phil Burkf9d1b992017-12-21 16:54:18 -080084 : preset;
85 EXPECT_EQ(expectedPreset, AAudioStream_getInputPreset(aaudioStream));
86
Kevin Rocard68646ba2019-03-20 13:26:49 -070087 aaudio_allowed_capture_policy_t expectedCapturePolicy =
88 (capturePolicy == DONT_SET || capturePolicy == AAUDIO_UNSPECIFIED)
89 ? AAUDIO_ALLOW_CAPTURE_BY_ALL // default
90 : preset;
91 EXPECT_EQ(expectedCapturePolicy, AAudioStream_getAllowedCapturePolicy(aaudioStream));
92
Phil Burkf9d1b992017-12-21 16:54:18 -080093 EXPECT_EQ(AAUDIO_OK, AAudioStream_requestStart(aaudioStream));
94
95 if (direction == AAUDIO_DIRECTION_INPUT) {
96 EXPECT_EQ(kNumFrames,
97 AAudioStream_read(aaudioStream, buffer, kNumFrames, kNanosPerSecond));
98 } else {
99 EXPECT_EQ(kNumFrames,
100 AAudioStream_write(aaudioStream, buffer, kNumFrames, kNanosPerSecond));
101 }
102
103 EXPECT_EQ(AAUDIO_OK, AAudioStream_requestStop(aaudioStream));
104
105 EXPECT_EQ(AAUDIO_OK, AAudioStream_close(aaudioStream));
106 delete[] buffer;
107}
108
109static const aaudio_usage_t sUsages[] = {
110 DONT_SET,
111 AAUDIO_UNSPECIFIED,
112 AAUDIO_USAGE_MEDIA,
113 AAUDIO_USAGE_VOICE_COMMUNICATION,
114 AAUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING,
115 AAUDIO_USAGE_ALARM,
116 AAUDIO_USAGE_NOTIFICATION,
117 AAUDIO_USAGE_NOTIFICATION_RINGTONE,
118 AAUDIO_USAGE_NOTIFICATION_EVENT,
119 AAUDIO_USAGE_ASSISTANCE_ACCESSIBILITY,
120 AAUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE,
121 AAUDIO_USAGE_ASSISTANCE_SONIFICATION,
122 AAUDIO_USAGE_GAME,
Hayden Gomes3e8bbb92020-01-10 13:37:05 -0800123 AAUDIO_USAGE_ASSISTANT,
124 AAUDIO_SYSTEM_USAGE_EMERGENCY,
125 AAUDIO_SYSTEM_USAGE_SAFETY,
126 AAUDIO_SYSTEM_USAGE_VEHICLE_STATUS,
127 AAUDIO_SYSTEM_USAGE_ANNOUNCEMENT
Phil Burkf9d1b992017-12-21 16:54:18 -0800128};
129
130static const aaudio_content_type_t sContentypes[] = {
131 DONT_SET,
132 AAUDIO_UNSPECIFIED,
133 AAUDIO_CONTENT_TYPE_SPEECH,
134 AAUDIO_CONTENT_TYPE_MUSIC,
135 AAUDIO_CONTENT_TYPE_MOVIE,
136 AAUDIO_CONTENT_TYPE_SONIFICATION
137};
138
139static const aaudio_input_preset_t sInputPresets[] = {
140 DONT_SET,
141 AAUDIO_UNSPECIFIED,
142 AAUDIO_INPUT_PRESET_GENERIC,
143 AAUDIO_INPUT_PRESET_CAMCORDER,
144 AAUDIO_INPUT_PRESET_VOICE_RECOGNITION,
145 AAUDIO_INPUT_PRESET_VOICE_COMMUNICATION,
146 AAUDIO_INPUT_PRESET_UNPROCESSED,
Eric Laurentae4b6ec2019-01-15 18:34:38 -0800147 AAUDIO_INPUT_PRESET_VOICE_PERFORMANCE,
Phil Burkf9d1b992017-12-21 16:54:18 -0800148};
149
Kevin Rocard68646ba2019-03-20 13:26:49 -0700150static const aaudio_input_preset_t sAllowCapturePolicies[] = {
151 DONT_SET,
152 AAUDIO_UNSPECIFIED,
153 AAUDIO_ALLOW_CAPTURE_BY_ALL,
154 AAUDIO_ALLOW_CAPTURE_BY_SYSTEM,
155 AAUDIO_ALLOW_CAPTURE_BY_NONE,
156};
157
Phil Burkf9d1b992017-12-21 16:54:18 -0800158static void checkAttributesUsage(aaudio_performance_mode_t perfMode) {
159 for (aaudio_usage_t usage : sUsages) {
160 checkAttributes(perfMode, usage, DONT_SET);
161 }
162}
163
Kevin Rocard68646ba2019-03-20 13:26:49 -0700164static void checkAttributesContentType(aaudio_performance_mode_t perfMode) {
Phil Burkf9d1b992017-12-21 16:54:18 -0800165 for (aaudio_content_type_t contentType : sContentypes) {
166 checkAttributes(perfMode, DONT_SET, contentType);
167 }
168}
169
170static void checkAttributesInputPreset(aaudio_performance_mode_t perfMode) {
171 for (aaudio_input_preset_t inputPreset : sInputPresets) {
172 checkAttributes(perfMode,
173 DONT_SET,
174 DONT_SET,
175 inputPreset,
Kevin Rocard68646ba2019-03-20 13:26:49 -0700176 DONT_SET,
177 AAUDIO_DIRECTION_INPUT);
178 }
179}
180
181static void checkAttributesAllowedCapturePolicy(aaudio_performance_mode_t perfMode) {
182 for (aaudio_allowed_capture_policy_t policy : sAllowCapturePolicies) {
183 checkAttributes(perfMode,
184 DONT_SET,
185 DONT_SET,
186 DONT_SET,
187 policy,
Phil Burkf9d1b992017-12-21 16:54:18 -0800188 AAUDIO_DIRECTION_INPUT);
189 }
190}
191
192TEST(test_attributes, aaudio_usage_perfnone) {
193 checkAttributesUsage(AAUDIO_PERFORMANCE_MODE_NONE);
194}
195
196TEST(test_attributes, aaudio_content_type_perfnone) {
197 checkAttributesContentType(AAUDIO_PERFORMANCE_MODE_NONE);
198}
199
200TEST(test_attributes, aaudio_input_preset_perfnone) {
201 checkAttributesInputPreset(AAUDIO_PERFORMANCE_MODE_NONE);
202}
203
Kevin Rocard68646ba2019-03-20 13:26:49 -0700204TEST(test_attributes, aaudio_allowed_capture_policy_perfnone) {
205 checkAttributesAllowedCapturePolicy(AAUDIO_PERFORMANCE_MODE_NONE);
206}
207
Phil Burkf9d1b992017-12-21 16:54:18 -0800208TEST(test_attributes, aaudio_usage_lowlat) {
209 checkAttributesUsage(AAUDIO_PERFORMANCE_MODE_LOW_LATENCY);
210}
211
212TEST(test_attributes, aaudio_content_type_lowlat) {
213 checkAttributesContentType(AAUDIO_PERFORMANCE_MODE_LOW_LATENCY);
214}
215
216TEST(test_attributes, aaudio_input_preset_lowlat) {
217 checkAttributesInputPreset(AAUDIO_PERFORMANCE_MODE_LOW_LATENCY);
218}
Kevin Rocard68646ba2019-03-20 13:26:49 -0700219
220TEST(test_attributes, aaudio_allowed_capture_policy_lowlat) {
221 checkAttributesAllowedCapturePolicy(AAUDIO_PERFORMANCE_MODE_LOW_LATENCY);
222}