1 /* 2 * Copyright 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 #ifndef CTS_MEDIA_TEST_AAUDIO_UTILS_H 17 #define CTS_MEDIA_TEST_AAUDIO_UTILS_H 18 19 #include <dlfcn.h> 20 #include <atomic> 21 #include <map> 22 #include <unordered_set> 23 #include <gtest/gtest.h> 24 #include <sys/system_properties.h> 25 26 #include <android/binder_auto_utils.h> 27 #include <android/binder_ibinder.h> 28 29 #include <aaudio/AAudio.h> 30 #include <system/audio.h> /* FCC_LIMIT */ 31 32 #include "test_aaudio.h" // NANOS_PER_MILLISECOND 33 34 int64_t getNanoseconds(clockid_t clockId = CLOCK_MONOTONIC); 35 const char* performanceModeToString(aaudio_performance_mode_t mode); 36 const char* sharingModeToString(aaudio_sharing_mode_t mode); 37 38 static constexpr const char* FEATURE_PLAYBACK = "android.hardware.audio.output"; 39 static constexpr const char* FEATURE_RECORDING = "android.hardware.microphone"; 40 static constexpr const char* FEATURE_LOW_LATENCY = "android.hardware.audio.low_latency"; 41 bool deviceSupportsFeature(const char* feature); 42 43 class StreamBuilderHelper { 44 public: 45 struct Parameters { 46 int32_t sampleRate; 47 int32_t channelCount; 48 aaudio_format_t dataFormat; 49 aaudio_sharing_mode_t sharingMode; 50 aaudio_performance_mode_t perfMode; 51 }; 52 53 void initBuilder(); 54 void createAndVerifyStream(bool *success); 55 void close(); 56 startStream()57 void startStream() { 58 streamCommand(&AAudioStream_requestStart, 59 AAUDIO_STREAM_STATE_STARTING, AAUDIO_STREAM_STATE_STARTED); 60 } pauseStream()61 void pauseStream() { 62 streamCommand(&AAudioStream_requestPause, 63 AAUDIO_STREAM_STATE_PAUSING, AAUDIO_STREAM_STATE_PAUSED); 64 } stopStream()65 void stopStream() { 66 streamCommand(&AAudioStream_requestStop, 67 AAUDIO_STREAM_STATE_STOPPING, AAUDIO_STREAM_STATE_STOPPED); 68 } 69 waitForState(aaudio_stream_state_t targetState)70 void waitForState(aaudio_stream_state_t targetState) { 71 aaudio_stream_state_t state = AAUDIO_STREAM_STATE_UNKNOWN; 72 const int kNumTries = 4; // max number of states we expect to transition through 73 for (int i = 0; ((i < kNumTries) && (state != targetState)); i++) { 74 EXPECT_EQ(AAUDIO_OK, AAudioStream_waitForStateChange(stream(), 75 state, 76 &state, 77 DEFAULT_STATE_TIMEOUT)); 78 } 79 } 80 flushStream()81 void flushStream() { 82 streamCommand(&AAudioStream_requestFlush, 83 AAUDIO_STREAM_STATE_FLUSHING, AAUDIO_STREAM_STATE_FLUSHED); 84 } 85 builder()86 AAudioStreamBuilder* builder() const { return mBuilder; } stream()87 AAudioStream* stream() const { return mStream; } actual()88 const Parameters& actual() const { return mActual; } framesPerBurst()89 int32_t framesPerBurst() const { return mFramesPerBurst; } 90 91 protected: 92 StreamBuilderHelper(aaudio_direction_t direction, int32_t sampleRate, 93 int32_t channelCount, aaudio_format_t dataFormat, 94 aaudio_sharing_mode_t sharingMode, aaudio_performance_mode_t perfMode); 95 ~StreamBuilderHelper(); 96 97 typedef aaudio_result_t (StreamCommand)(AAudioStream*); 98 void streamCommand( 99 StreamCommand cmd, aaudio_stream_state_t fromState, aaudio_stream_state_t toState); 100 101 static const std::map<aaudio_performance_mode_t, int64_t> sMaxFramesPerBurstMs; 102 static const std::unordered_set<aaudio_format_t> sValidStreamFormats; 103 const aaudio_direction_t mDirection; 104 const Parameters mRequested; 105 Parameters mActual; 106 int32_t mFramesPerBurst; 107 AAudioStreamBuilder *mBuilder; 108 AAudioStream *mStream; 109 110 private: 111 const int32_t kMinValidSampleRate = 8000; // 8 kHz 112 const int32_t kMaxValidSampleRate = 2000000; // 2 MHz 113 const int32_t kMinValidChannelCount = 1; 114 const int32_t kMaxValidChannelCount = FCC_LIMIT; 115 }; 116 117 class InputStreamBuilderHelper : public StreamBuilderHelper { 118 public: 119 InputStreamBuilderHelper( 120 aaudio_sharing_mode_t requestedSharingMode, 121 aaudio_performance_mode_t requestedPerfMode, 122 aaudio_format_t requestedFormat = AAUDIO_FORMAT_PCM_FLOAT, 123 int32_t requestedSampleRate = 48000); 124 }; 125 126 class OutputStreamBuilderHelper : public StreamBuilderHelper { 127 public: 128 OutputStreamBuilderHelper( 129 aaudio_sharing_mode_t requestedSharingMode, 130 aaudio_performance_mode_t requestedPerfMode, 131 aaudio_format_t requestedFormat = AAUDIO_FORMAT_PCM_I16, 132 int32_t requestSampleRate = 48000); 133 void initBuilder(); 134 135 private: 136 const int32_t kBufferCapacityFrames = 2000; 137 }; 138 139 class AAudioExtensions { 140 public: 141 AAudioExtensions(); 142 isPolicyEnabled(int32_t policy)143 static bool isPolicyEnabled(int32_t policy) { 144 return (policy == AAUDIO_POLICY_AUTO || policy == AAUDIO_POLICY_ALWAYS); 145 } 146 getInstance()147 static AAudioExtensions &getInstance() { 148 static AAudioExtensions instance; 149 return instance; 150 } 151 getMMapPolicyProperty()152 static int getMMapPolicyProperty() { 153 return getIntegerProperty("aaudio.mmap_policy", AAUDIO_UNSPECIFIED); 154 } 155 getMMapPolicy()156 aaudio_policy_t getMMapPolicy() { return AAudio_getMMapPolicy(); } 157 setMMapPolicy(aaudio_policy_t policy)158 int32_t setMMapPolicy(aaudio_policy_t policy) { return AAudio_setMMapPolicy(policy); } 159 isMMapUsed(AAudioStream * aaudioStream)160 bool isMMapUsed(AAudioStream* aaudioStream) { return AAudioStream_isMMapUsed(aaudioStream); } 161 setMMapEnabled(bool enabled)162 int32_t setMMapEnabled(bool enabled) { 163 return setMMapPolicy(enabled ? AAUDIO_POLICY_AUTO : AAUDIO_POLICY_NEVER); 164 } 165 isMMapEnabled()166 bool isMMapEnabled() { return isPolicyEnabled(AAudio_getMMapPolicy()); } 167 isMMapSupported()168 bool isMMapSupported() const { 169 return mMMapSupported; 170 } 171 isMMapExclusiveSupported()172 bool isMMapExclusiveSupported() const { 173 return mMMapExclusiveSupported; 174 } 175 getPlatformMMapPolicy(AAudio_DeviceType device,aaudio_direction_t direction)176 aaudio_policy_t getPlatformMMapPolicy(AAudio_DeviceType device, 177 aaudio_direction_t direction) const { 178 return AAudio_getPlatformMMapPolicy(device, direction); 179 } 180 getPlatformMMapExclusivePolicy(AAudio_DeviceType device,aaudio_direction_t direction)181 aaudio_policy_t getPlatformMMapExclusivePolicy(AAudio_DeviceType device, 182 aaudio_direction_t direction) const { 183 return AAudio_getPlatformMMapExclusivePolicy(device, direction); 184 } 185 186 private: 187 188 static int getIntegerProperty(const char *name, int defaultValue); 189 190 const bool mMMapSupported; 191 const bool mMMapExclusiveSupported; 192 }; 193 194 class AudioServerCrashMonitor { 195 public: getInstance()196 static AudioServerCrashMonitor& getInstance() { 197 static AudioServerCrashMonitor instance; 198 return instance; 199 } 200 ~AudioServerCrashMonitor(); 201 202 void linkToDeath(); 203 isDeathRecipientLinked()204 bool isDeathRecipientLinked() const { return mDeathRecipientLinked; } 205 void onAudioServerCrash(); 206 207 private: 208 AudioServerCrashMonitor(); 209 210 ::ndk::SpAIBinder getAudioFlinger(); 211 212 ::ndk::SpAIBinder mAudioFlinger; 213 ::ndk::ScopedAIBinder_DeathRecipient mDeathRecipient; 214 bool mDeathRecipientLinked = false; 215 }; 216 217 class AAudioCtsBase : public ::testing::Test { 218 protected: 219 void SetUp() override; 220 void TearDown() override; 221 222 private: 223 void checkIfAudioServerCrash(); 224 }; 225 226 bool isIEC61937Supported(); 227 228 bool isEchoReferenceSupported(); 229 230 void enableAudioOutputPermission(); 231 232 void enableAudioHotwordPermission(); 233 234 void disablePermissions(); 235 236 bool isCompressedFormat(aaudio_format_t format); 237 238 #endif // CTS_MEDIA_TEST_AAUDIO_UTILS_H 239