| 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 |  | 
|  | 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 |  | 
|  | 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) | 
| Phil Burk | eaef9b9 | 2018-01-18 09:09:42 -0800 | [diff] [blame] | 79 | ? AAUDIO_INPUT_PRESET_VOICE_RECOGNITION // default | 
| Phil Burk | f9d1b99 | 2017-12-21 16:54:18 -0800 | [diff] [blame] | 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, | 
| Eric Laurent | ae4b6ec | 2019-01-15 18:34:38 -0800 | [diff] [blame] | 133 | AAUDIO_INPUT_PRESET_VOICE_PERFORMANCE, | 
| Phil Burk | f9d1b99 | 2017-12-21 16:54:18 -0800 | [diff] [blame] | 134 | }; | 
|  | 135 |  | 
|  | 136 | static void checkAttributesUsage(aaudio_performance_mode_t perfMode) { | 
|  | 137 | for (aaudio_usage_t usage : sUsages) { | 
|  | 138 | checkAttributes(perfMode, usage, DONT_SET); | 
|  | 139 | } | 
|  | 140 | } | 
|  | 141 |  | 
|  | 142 | static void checkAttributesContentType(aaudio_input_preset_t perfMode) { | 
|  | 143 | for (aaudio_content_type_t contentType : sContentypes) { | 
|  | 144 | checkAttributes(perfMode, DONT_SET, contentType); | 
|  | 145 | } | 
|  | 146 | } | 
|  | 147 |  | 
|  | 148 | static void checkAttributesInputPreset(aaudio_performance_mode_t perfMode) { | 
|  | 149 | for (aaudio_input_preset_t inputPreset : sInputPresets) { | 
|  | 150 | checkAttributes(perfMode, | 
|  | 151 | DONT_SET, | 
|  | 152 | DONT_SET, | 
|  | 153 | inputPreset, | 
|  | 154 | AAUDIO_DIRECTION_INPUT); | 
|  | 155 | } | 
|  | 156 | } | 
|  | 157 |  | 
|  | 158 | TEST(test_attributes, aaudio_usage_perfnone) { | 
|  | 159 | checkAttributesUsage(AAUDIO_PERFORMANCE_MODE_NONE); | 
|  | 160 | } | 
|  | 161 |  | 
|  | 162 | TEST(test_attributes, aaudio_content_type_perfnone) { | 
|  | 163 | checkAttributesContentType(AAUDIO_PERFORMANCE_MODE_NONE); | 
|  | 164 | } | 
|  | 165 |  | 
|  | 166 | TEST(test_attributes, aaudio_input_preset_perfnone) { | 
|  | 167 | checkAttributesInputPreset(AAUDIO_PERFORMANCE_MODE_NONE); | 
|  | 168 | } | 
|  | 169 |  | 
|  | 170 | TEST(test_attributes, aaudio_usage_lowlat) { | 
|  | 171 | checkAttributesUsage(AAUDIO_PERFORMANCE_MODE_LOW_LATENCY); | 
|  | 172 | } | 
|  | 173 |  | 
|  | 174 | TEST(test_attributes, aaudio_content_type_lowlat) { | 
|  | 175 | checkAttributesContentType(AAUDIO_PERFORMANCE_MODE_LOW_LATENCY); | 
|  | 176 | } | 
|  | 177 |  | 
|  | 178 | TEST(test_attributes, aaudio_input_preset_lowlat) { | 
|  | 179 | checkAttributesInputPreset(AAUDIO_PERFORMANCE_MODE_LOW_LATENCY); | 
|  | 180 | } |