Phil Burk | f9d1b99 | 2017-12-21 16:54:18 -0800 | [diff] [blame] | 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 | |
Phil Burk | dca60f3 | 2021-09-21 21:48:16 +0000 | [diff] [blame^] | 19 | // 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 Burk | f9d1b99 | 2017-12-21 16:54:18 -0800 | [diff] [blame] | 23 | #include <stdio.h> |
| 24 | #include <unistd.h> |
| 25 | |
| 26 | #include <aaudio/AAudio.h> |
| 27 | #include <gtest/gtest.h> |
| 28 | |
| 29 | constexpr int64_t kNanosPerSecond = 1000000000; |
| 30 | constexpr int kNumFrames = 256; |
| 31 | constexpr int kChannelCount = 2; |
| 32 | |
| 33 | constexpr int32_t DONT_SET = -1000; |
| 34 | |
| 35 | static 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 Rocard | 68646ba | 2019-03-20 13:26:49 -0700 | [diff] [blame] | 39 | aaudio_allowed_capture_policy_t capturePolicy = DONT_SET, |
Eric Laurent | 76373c2 | 2020-01-14 12:38:14 -0800 | [diff] [blame] | 40 | int privacyMode = DONT_SET, |
Phil Burk | f9d1b99 | 2017-12-21 16:54:18 -0800 | [diff] [blame] | 41 | 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 Rocard | 68646ba | 2019-03-20 13:26:49 -0700 | [diff] [blame] | 65 | if (capturePolicy != DONT_SET) { |
| 66 | AAudioStreamBuilder_setAllowedCapturePolicy(aaudioBuilder, capturePolicy); |
| 67 | } |
Eric Laurent | 76373c2 | 2020-01-14 12:38:14 -0800 | [diff] [blame] | 68 | if (privacyMode != DONT_SET) { |
| 69 | AAudioStreamBuilder_setPrivacySensitive(aaudioBuilder, (bool)privacyMode); |
| 70 | } |
Phil Burk | f9d1b99 | 2017-12-21 16:54:18 -0800 | [diff] [blame] | 71 | |
| 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 Burk | eaef9b9 | 2018-01-18 09:09:42 -0800 | [diff] [blame] | 91 | ? AAUDIO_INPUT_PRESET_VOICE_RECOGNITION // default |
Phil Burk | f9d1b99 | 2017-12-21 16:54:18 -0800 | [diff] [blame] | 92 | : preset; |
| 93 | EXPECT_EQ(expectedPreset, AAudioStream_getInputPreset(aaudioStream)); |
| 94 | |
Kevin Rocard | 68646ba | 2019-03-20 13:26:49 -0700 | [diff] [blame] | 95 | aaudio_allowed_capture_policy_t expectedCapturePolicy = |
| 96 | (capturePolicy == DONT_SET || capturePolicy == AAUDIO_UNSPECIFIED) |
| 97 | ? AAUDIO_ALLOW_CAPTURE_BY_ALL // default |
Phil Burk | dca60f3 | 2021-09-21 21:48:16 +0000 | [diff] [blame^] | 98 | : capturePolicy; |
Kevin Rocard | 68646ba | 2019-03-20 13:26:49 -0700 | [diff] [blame] | 99 | EXPECT_EQ(expectedCapturePolicy, AAudioStream_getAllowedCapturePolicy(aaudioStream)); |
| 100 | |
Eric Laurent | 76373c2 | 2020-01-14 12:38:14 -0800 | [diff] [blame] | 101 | 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 Burk | f9d1b99 | 2017-12-21 16:54:18 -0800 | [diff] [blame] | 108 | 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 | |
| 124 | static 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 Gomes | 3e8bbb9 | 2020-01-10 13:37:05 -0800 | [diff] [blame] | 138 | AAUDIO_USAGE_ASSISTANT, |
Phil Burk | dca60f3 | 2021-09-21 21:48:16 +0000 | [diff] [blame^] | 139 | // Note that the AAUDIO_SYSTEM_USAGE_* values requires special permission. |
Phil Burk | f9d1b99 | 2017-12-21 16:54:18 -0800 | [diff] [blame] | 140 | }; |
| 141 | |
| 142 | static 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 | |
| 151 | static 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 Laurent | ae4b6ec | 2019-01-15 18:34:38 -0800 | [diff] [blame] | 159 | AAUDIO_INPUT_PRESET_VOICE_PERFORMANCE, |
Phil Burk | f9d1b99 | 2017-12-21 16:54:18 -0800 | [diff] [blame] | 160 | }; |
| 161 | |
Kevin Rocard | 68646ba | 2019-03-20 13:26:49 -0700 | [diff] [blame] | 162 | static 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 Laurent | 76373c2 | 2020-01-14 12:38:14 -0800 | [diff] [blame] | 170 | static const int sPrivacyModes[] = { |
| 171 | DONT_SET, |
| 172 | false, |
| 173 | true, |
| 174 | }; |
| 175 | |
Phil Burk | f9d1b99 | 2017-12-21 16:54:18 -0800 | [diff] [blame] | 176 | static void checkAttributesUsage(aaudio_performance_mode_t perfMode) { |
| 177 | for (aaudio_usage_t usage : sUsages) { |
| 178 | checkAttributes(perfMode, usage, DONT_SET); |
| 179 | } |
| 180 | } |
| 181 | |
Kevin Rocard | 68646ba | 2019-03-20 13:26:49 -0700 | [diff] [blame] | 182 | static void checkAttributesContentType(aaudio_performance_mode_t perfMode) { |
Phil Burk | f9d1b99 | 2017-12-21 16:54:18 -0800 | [diff] [blame] | 183 | for (aaudio_content_type_t contentType : sContentypes) { |
| 184 | checkAttributes(perfMode, DONT_SET, contentType); |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | static 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 Rocard | 68646ba | 2019-03-20 13:26:49 -0700 | [diff] [blame] | 194 | DONT_SET, |
Eric Laurent | 76373c2 | 2020-01-14 12:38:14 -0800 | [diff] [blame] | 195 | DONT_SET, |
Kevin Rocard | 68646ba | 2019-03-20 13:26:49 -0700 | [diff] [blame] | 196 | AAUDIO_DIRECTION_INPUT); |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | static 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 Burk | f9d1b99 | 2017-12-21 16:54:18 -0800 | [diff] [blame] | 207 | AAUDIO_DIRECTION_INPUT); |
| 208 | } |
| 209 | } |
| 210 | |
Eric Laurent | 76373c2 | 2020-01-14 12:38:14 -0800 | [diff] [blame] | 211 | static 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 Burk | f9d1b99 | 2017-12-21 16:54:18 -0800 | [diff] [blame] | 223 | TEST(test_attributes, aaudio_usage_perfnone) { |
| 224 | checkAttributesUsage(AAUDIO_PERFORMANCE_MODE_NONE); |
| 225 | } |
| 226 | |
| 227 | TEST(test_attributes, aaudio_content_type_perfnone) { |
| 228 | checkAttributesContentType(AAUDIO_PERFORMANCE_MODE_NONE); |
| 229 | } |
| 230 | |
| 231 | TEST(test_attributes, aaudio_input_preset_perfnone) { |
| 232 | checkAttributesInputPreset(AAUDIO_PERFORMANCE_MODE_NONE); |
| 233 | } |
| 234 | |
Kevin Rocard | 68646ba | 2019-03-20 13:26:49 -0700 | [diff] [blame] | 235 | TEST(test_attributes, aaudio_allowed_capture_policy_perfnone) { |
| 236 | checkAttributesAllowedCapturePolicy(AAUDIO_PERFORMANCE_MODE_NONE); |
| 237 | } |
| 238 | |
Phil Burk | f9d1b99 | 2017-12-21 16:54:18 -0800 | [diff] [blame] | 239 | TEST(test_attributes, aaudio_usage_lowlat) { |
| 240 | checkAttributesUsage(AAUDIO_PERFORMANCE_MODE_LOW_LATENCY); |
| 241 | } |
| 242 | |
| 243 | TEST(test_attributes, aaudio_content_type_lowlat) { |
| 244 | checkAttributesContentType(AAUDIO_PERFORMANCE_MODE_LOW_LATENCY); |
| 245 | } |
| 246 | |
| 247 | TEST(test_attributes, aaudio_input_preset_lowlat) { |
| 248 | checkAttributesInputPreset(AAUDIO_PERFORMANCE_MODE_LOW_LATENCY); |
| 249 | } |
Kevin Rocard | 68646ba | 2019-03-20 13:26:49 -0700 | [diff] [blame] | 250 | |
| 251 | TEST(test_attributes, aaudio_allowed_capture_policy_lowlat) { |
| 252 | checkAttributesAllowedCapturePolicy(AAUDIO_PERFORMANCE_MODE_LOW_LATENCY); |
| 253 | } |
Eric Laurent | 76373c2 | 2020-01-14 12:38:14 -0800 | [diff] [blame] | 254 | |
| 255 | TEST(test_attributes, aaudio_allowed_privacy_sensitive_lowlat) { |
| 256 | checkAttributesPrivacySensitive(AAUDIO_PERFORMANCE_MODE_LOW_LATENCY); |
| 257 | } |