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,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                             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     }
60     if (capturePolicy != DONT_SET) {
61         AAudioStreamBuilder_setAllowedCapturePolicy(aaudioBuilder, capturePolicy);
62     }
63 
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)
83             ? AAUDIO_INPUT_PRESET_VOICE_RECOGNITION // default
84             : preset;
85     EXPECT_EQ(expectedPreset, AAudioStream_getInputPreset(aaudioStream));
86 
87     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 
93     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 
109 static 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,
123     AAUDIO_USAGE_ASSISTANT
124 };
125 
126 static const aaudio_content_type_t sContentypes[] = {
127     DONT_SET,
128     AAUDIO_UNSPECIFIED,
129     AAUDIO_CONTENT_TYPE_SPEECH,
130     AAUDIO_CONTENT_TYPE_MUSIC,
131     AAUDIO_CONTENT_TYPE_MOVIE,
132     AAUDIO_CONTENT_TYPE_SONIFICATION
133 };
134 
135 static const aaudio_input_preset_t sInputPresets[] = {
136     DONT_SET,
137     AAUDIO_UNSPECIFIED,
138     AAUDIO_INPUT_PRESET_GENERIC,
139     AAUDIO_INPUT_PRESET_CAMCORDER,
140     AAUDIO_INPUT_PRESET_VOICE_RECOGNITION,
141     AAUDIO_INPUT_PRESET_VOICE_COMMUNICATION,
142     AAUDIO_INPUT_PRESET_UNPROCESSED,
143     AAUDIO_INPUT_PRESET_VOICE_PERFORMANCE,
144 };
145 
146 static const aaudio_input_preset_t sAllowCapturePolicies[] = {
147     DONT_SET,
148     AAUDIO_UNSPECIFIED,
149     AAUDIO_ALLOW_CAPTURE_BY_ALL,
150     AAUDIO_ALLOW_CAPTURE_BY_SYSTEM,
151     AAUDIO_ALLOW_CAPTURE_BY_NONE,
152 };
153 
checkAttributesUsage(aaudio_performance_mode_t perfMode)154 static void checkAttributesUsage(aaudio_performance_mode_t perfMode) {
155     for (aaudio_usage_t usage : sUsages) {
156         checkAttributes(perfMode, usage, DONT_SET);
157     }
158 }
159 
checkAttributesContentType(aaudio_performance_mode_t perfMode)160 static void checkAttributesContentType(aaudio_performance_mode_t perfMode) {
161     for (aaudio_content_type_t contentType : sContentypes) {
162         checkAttributes(perfMode, DONT_SET, contentType);
163     }
164 }
165 
checkAttributesInputPreset(aaudio_performance_mode_t perfMode)166 static void checkAttributesInputPreset(aaudio_performance_mode_t perfMode) {
167     for (aaudio_input_preset_t inputPreset : sInputPresets) {
168         checkAttributes(perfMode,
169                         DONT_SET,
170                         DONT_SET,
171                         inputPreset,
172                         DONT_SET,
173                         AAUDIO_DIRECTION_INPUT);
174     }
175 }
176 
checkAttributesAllowedCapturePolicy(aaudio_performance_mode_t perfMode)177 static void checkAttributesAllowedCapturePolicy(aaudio_performance_mode_t perfMode) {
178     for (aaudio_allowed_capture_policy_t policy : sAllowCapturePolicies) {
179         checkAttributes(perfMode,
180                         DONT_SET,
181                         DONT_SET,
182                         DONT_SET,
183                         policy,
184                         AAUDIO_DIRECTION_INPUT);
185     }
186 }
187 
TEST(test_attributes,aaudio_usage_perfnone)188 TEST(test_attributes, aaudio_usage_perfnone) {
189     checkAttributesUsage(AAUDIO_PERFORMANCE_MODE_NONE);
190 }
191 
TEST(test_attributes,aaudio_content_type_perfnone)192 TEST(test_attributes, aaudio_content_type_perfnone) {
193     checkAttributesContentType(AAUDIO_PERFORMANCE_MODE_NONE);
194 }
195 
TEST(test_attributes,aaudio_input_preset_perfnone)196 TEST(test_attributes, aaudio_input_preset_perfnone) {
197     checkAttributesInputPreset(AAUDIO_PERFORMANCE_MODE_NONE);
198 }
199 
TEST(test_attributes,aaudio_allowed_capture_policy_perfnone)200 TEST(test_attributes, aaudio_allowed_capture_policy_perfnone) {
201     checkAttributesAllowedCapturePolicy(AAUDIO_PERFORMANCE_MODE_NONE);
202 }
203 
TEST(test_attributes,aaudio_usage_lowlat)204 TEST(test_attributes, aaudio_usage_lowlat) {
205     checkAttributesUsage(AAUDIO_PERFORMANCE_MODE_LOW_LATENCY);
206 }
207 
TEST(test_attributes,aaudio_content_type_lowlat)208 TEST(test_attributes, aaudio_content_type_lowlat) {
209     checkAttributesContentType(AAUDIO_PERFORMANCE_MODE_LOW_LATENCY);
210 }
211 
TEST(test_attributes,aaudio_input_preset_lowlat)212 TEST(test_attributes, aaudio_input_preset_lowlat) {
213     checkAttributesInputPreset(AAUDIO_PERFORMANCE_MODE_LOW_LATENCY);
214 }
215 
TEST(test_attributes,aaudio_allowed_capture_policy_lowlat)216 TEST(test_attributes, aaudio_allowed_capture_policy_lowlat) {
217     checkAttributesAllowedCapturePolicy(AAUDIO_PERFORMANCE_MODE_LOW_LATENCY);
218 }
219