• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_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_direction_t direction = AAUDIO_DIRECTION_OUTPUT) {
36 
37     float *buffer = new float[kNumFrames * kChannelCount];
38 
39     AAudioStreamBuilder *aaudioBuilder = nullptr;
40     AAudioStream *aaudioStream = nullptr;
41 
42     // Use an AAudioStreamBuilder to contain requested parameters.
43     ASSERT_EQ(AAUDIO_OK, AAudio_createStreamBuilder(&aaudioBuilder));
44 
45     // Request stream properties.
46     AAudioStreamBuilder_setPerformanceMode(aaudioBuilder, perfMode);
47     AAudioStreamBuilder_setDirection(aaudioBuilder, direction);
48 
49     // Set the attribute in the builder.
50     if (usage != DONT_SET) {
51         AAudioStreamBuilder_setUsage(aaudioBuilder, usage);
52     }
53     if (contentType != DONT_SET) {
54         AAudioStreamBuilder_setContentType(aaudioBuilder, contentType);
55     }
56     if (preset != DONT_SET) {
57         AAudioStreamBuilder_setInputPreset(aaudioBuilder, preset);
58     }
59 
60     // Create an AAudioStream using the Builder.
61     ASSERT_EQ(AAUDIO_OK, AAudioStreamBuilder_openStream(aaudioBuilder, &aaudioStream));
62     AAudioStreamBuilder_delete(aaudioBuilder);
63 
64     // Make sure we get the same attributes back from the stream.
65     aaudio_usage_t expectedUsage =
66             (usage == DONT_SET || usage == AAUDIO_UNSPECIFIED)
67             ? AAUDIO_USAGE_MEDIA // default
68             : usage;
69     EXPECT_EQ(expectedUsage, AAudioStream_getUsage(aaudioStream));
70 
71     aaudio_content_type_t expectedContentType =
72             (contentType == DONT_SET || contentType == AAUDIO_UNSPECIFIED)
73             ? AAUDIO_CONTENT_TYPE_MUSIC // default
74             : contentType;
75     EXPECT_EQ(expectedContentType, AAudioStream_getContentType(aaudioStream));
76 
77     aaudio_input_preset_t expectedPreset =
78             (preset == DONT_SET || preset == AAUDIO_UNSPECIFIED)
79             ? AAUDIO_INPUT_PRESET_VOICE_RECOGNITION // default
80             : preset;
81     EXPECT_EQ(expectedPreset, AAudioStream_getInputPreset(aaudioStream));
82 
83     EXPECT_EQ(AAUDIO_OK, AAudioStream_requestStart(aaudioStream));
84 
85     if (direction == AAUDIO_DIRECTION_INPUT) {
86         EXPECT_EQ(kNumFrames,
87                   AAudioStream_read(aaudioStream, buffer, kNumFrames, kNanosPerSecond));
88     } else {
89         EXPECT_EQ(kNumFrames,
90                   AAudioStream_write(aaudioStream, buffer, kNumFrames, kNanosPerSecond));
91     }
92 
93     EXPECT_EQ(AAUDIO_OK, AAudioStream_requestStop(aaudioStream));
94 
95     EXPECT_EQ(AAUDIO_OK, AAudioStream_close(aaudioStream));
96     delete[] buffer;
97 }
98 
99 static const aaudio_usage_t sUsages[] = {
100     DONT_SET,
101     AAUDIO_UNSPECIFIED,
102     AAUDIO_USAGE_MEDIA,
103     AAUDIO_USAGE_VOICE_COMMUNICATION,
104     AAUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING,
105     AAUDIO_USAGE_ALARM,
106     AAUDIO_USAGE_NOTIFICATION,
107     AAUDIO_USAGE_NOTIFICATION_RINGTONE,
108     AAUDIO_USAGE_NOTIFICATION_EVENT,
109     AAUDIO_USAGE_ASSISTANCE_ACCESSIBILITY,
110     AAUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE,
111     AAUDIO_USAGE_ASSISTANCE_SONIFICATION,
112     AAUDIO_USAGE_GAME,
113     AAUDIO_USAGE_ASSISTANT
114 };
115 
116 static const aaudio_content_type_t sContentypes[] = {
117     DONT_SET,
118     AAUDIO_UNSPECIFIED,
119     AAUDIO_CONTENT_TYPE_SPEECH,
120     AAUDIO_CONTENT_TYPE_MUSIC,
121     AAUDIO_CONTENT_TYPE_MOVIE,
122     AAUDIO_CONTENT_TYPE_SONIFICATION
123 };
124 
125 static const aaudio_input_preset_t sInputPresets[] = {
126     DONT_SET,
127     AAUDIO_UNSPECIFIED,
128     AAUDIO_INPUT_PRESET_GENERIC,
129     AAUDIO_INPUT_PRESET_CAMCORDER,
130     AAUDIO_INPUT_PRESET_VOICE_RECOGNITION,
131     AAUDIO_INPUT_PRESET_VOICE_COMMUNICATION,
132     AAUDIO_INPUT_PRESET_UNPROCESSED,
133 };
134 
checkAttributesUsage(aaudio_performance_mode_t perfMode)135 static void checkAttributesUsage(aaudio_performance_mode_t perfMode) {
136     for (aaudio_usage_t usage : sUsages) {
137         checkAttributes(perfMode, usage, DONT_SET);
138     }
139 }
140 
checkAttributesContentType(aaudio_input_preset_t perfMode)141 static void checkAttributesContentType(aaudio_input_preset_t perfMode) {
142     for (aaudio_content_type_t contentType : sContentypes) {
143         checkAttributes(perfMode, DONT_SET, contentType);
144     }
145 }
146 
checkAttributesInputPreset(aaudio_performance_mode_t perfMode)147 static void checkAttributesInputPreset(aaudio_performance_mode_t perfMode) {
148     for (aaudio_input_preset_t inputPreset : sInputPresets) {
149         checkAttributes(perfMode,
150                         DONT_SET,
151                         DONT_SET,
152                         inputPreset,
153                         AAUDIO_DIRECTION_INPUT);
154     }
155 }
156 
TEST(test_attributes,aaudio_usage_perfnone)157 TEST(test_attributes, aaudio_usage_perfnone) {
158     checkAttributesUsage(AAUDIO_PERFORMANCE_MODE_NONE);
159 }
160 
TEST(test_attributes,aaudio_content_type_perfnone)161 TEST(test_attributes, aaudio_content_type_perfnone) {
162     checkAttributesContentType(AAUDIO_PERFORMANCE_MODE_NONE);
163 }
164 
TEST(test_attributes,aaudio_input_preset_perfnone)165 TEST(test_attributes, aaudio_input_preset_perfnone) {
166     checkAttributesInputPreset(AAUDIO_PERFORMANCE_MODE_NONE);
167 }
168 
TEST(test_attributes,aaudio_usage_lowlat)169 TEST(test_attributes, aaudio_usage_lowlat) {
170     checkAttributesUsage(AAUDIO_PERFORMANCE_MODE_LOW_LATENCY);
171 }
172 
TEST(test_attributes,aaudio_content_type_lowlat)173 TEST(test_attributes, aaudio_content_type_lowlat) {
174     checkAttributesContentType(AAUDIO_PERFORMANCE_MODE_LOW_LATENCY);
175 }
176 
TEST(test_attributes,aaudio_input_preset_lowlat)177 TEST(test_attributes, aaudio_input_preset_lowlat) {
178     checkAttributesInputPreset(AAUDIO_PERFORMANCE_MODE_LOW_LATENCY);
179 }
180