1 /* 2 * Copyright 2016 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 #ifndef OBOE_AAUDIO_LOADER_H_ 18 #define OBOE_AAUDIO_LOADER_H_ 19 20 #include <unistd.h> 21 #include "oboe/Definitions.h" 22 23 // If the NDK is before O then define this in your build 24 // so that AAudio.h will not be included. 25 #ifdef OBOE_NO_INCLUDE_AAUDIO 26 27 // Define missing types from AAudio.h 28 typedef int32_t aaudio_stream_state_t; 29 typedef int32_t aaudio_direction_t; 30 typedef int32_t aaudio_format_t; 31 typedef int32_t aaudio_data_callback_result_t; 32 typedef int32_t aaudio_result_t; 33 typedef int32_t aaudio_sharing_mode_t; 34 typedef int32_t aaudio_performance_mode_t; 35 36 typedef struct AAudioStreamStruct AAudioStream; 37 typedef struct AAudioStreamBuilderStruct AAudioStreamBuilder; 38 39 typedef aaudio_data_callback_result_t (*AAudioStream_dataCallback)( 40 AAudioStream *stream, 41 void *userData, 42 void *audioData, 43 int32_t numFrames); 44 45 typedef void (*AAudioStream_errorCallback)( 46 AAudioStream *stream, 47 void *userData, 48 aaudio_result_t error); 49 50 // These were defined in P 51 typedef int32_t aaudio_usage_t; 52 typedef int32_t aaudio_content_type_t; 53 typedef int32_t aaudio_input_preset_t; 54 typedef int32_t aaudio_session_id_t; 55 56 // There are a few definitions used by Oboe. 57 #define AAUDIO_OK static_cast<aaudio_result_t>(Result::OK) 58 #define AAUDIO_ERROR_TIMEOUT static_cast<aaudio_result_t>(Result::ErrorTimeout) 59 #define AAUDIO_STREAM_STATE_STARTING static_cast<aaudio_stream_state_t>(StreamState::Starting) 60 #define AAUDIO_STREAM_STATE_STARTED static_cast<aaudio_stream_state_t>(StreamState::Started) 61 #else 62 #include <aaudio/AAudio.h> 63 #endif 64 65 #ifdef __NDK_MAJOR__ 66 #define OBOE_USING_NDK 1 67 #else 68 #define __NDK_MAJOR__ 0 69 #define OBOE_USING_NDK 0 70 #endif 71 72 #if __NDK_MAJOR__ < 24 73 // Defined in SC_V2 74 typedef uint32_t aaudio_channel_mask_t; 75 typedef int32_t aaudio_spatialization_behavior_t; 76 #endif 77 78 #if OBOE_USING_NDK && __NDK_MAJOR__ < 29 79 // Defined in Android B 80 typedef void (*AAudioStream_presentationEndCallback)( 81 AAudioStream* stream, 82 void* userData); 83 #endif 84 85 #ifndef __ANDROID_API_Q__ 86 #define __ANDROID_API_Q__ 29 87 #endif 88 89 #ifndef __ANDROID_API_R__ 90 #define __ANDROID_API_R__ 30 91 #endif 92 93 #ifndef __ANDROID_API_S__ 94 #define __ANDROID_API_S__ 31 95 #endif 96 97 #ifndef __ANDROID_API_S_V2__ 98 #define __ANDROID_API_S_V2__ 32 99 #endif 100 101 #ifndef __ANDROID_API_U__ 102 #define __ANDROID_API_U__ 34 103 #endif 104 105 #ifndef __ANDROID_API_B__ 106 #define __ANDROID_API_B__ 36 107 #endif 108 109 #if OBOE_USING_NDK && __NDK_MAJOR__ < 29 110 // These were defined in Android B 111 typedef int32_t AAudio_DeviceType; 112 typedef int32_t aaudio_policy_t; 113 #endif 114 115 namespace oboe { 116 117 /** 118 * The AAudio API was not available in early versions of Android. 119 * To avoid linker errors, we dynamically link with the functions by name using dlsym(). 120 * On older versions this linkage will safely fail. 121 */ 122 class AAudioLoader { 123 public: 124 // Use signatures for common functions. 125 // Key to letter abbreviations. 126 // S = Stream 127 // B = Builder 128 // I = int32_t 129 // L = int64_t 130 // T = sTate 131 // K = clocKid_t 132 // P = Pointer to following data type 133 // C = Const prefix 134 // H = cHar 135 // U = uint32_t 136 // O = bOol 137 // R = pResentation end callback 138 139 typedef int32_t (*signature_I_PPB)(AAudioStreamBuilder **builder); 140 141 typedef const char * (*signature_CPH_I)(int32_t); 142 143 typedef int32_t (*signature_I_PBPPS)(AAudioStreamBuilder *, 144 AAudioStream **stream); // AAudioStreamBuilder_open() 145 146 typedef int32_t (*signature_I_PB)(AAudioStreamBuilder *); // AAudioStreamBuilder_delete() 147 // AAudioStreamBuilder_setSampleRate() 148 typedef void (*signature_V_PBI)(AAudioStreamBuilder *, int32_t); 149 150 // AAudioStreamBuilder_setChannelMask() 151 typedef void (*signature_V_PBU)(AAudioStreamBuilder *, uint32_t); 152 153 typedef void (*signature_V_PBCPH)(AAudioStreamBuilder *, const char *); 154 155 // AAudioStreamBuilder_setPrivacySensitive 156 typedef void (*signature_V_PBO)(AAudioStreamBuilder *, bool); 157 158 typedef int32_t (*signature_I_PS)(AAudioStream *); // AAudioStream_getSampleRate() 159 typedef int64_t (*signature_L_PS)(AAudioStream *); // AAudioStream_getFramesRead() 160 // AAudioStream_setBufferSizeInFrames() 161 typedef int32_t (*signature_I_PSI)(AAudioStream *, int32_t); 162 163 typedef void (*signature_V_PBPDPV)(AAudioStreamBuilder *, 164 AAudioStream_dataCallback, 165 void *); 166 167 typedef void (*signature_V_PBPEPV)(AAudioStreamBuilder *, 168 AAudioStream_errorCallback, 169 void *); 170 171 typedef void (*signature_V_PBPRPV)(AAudioStreamBuilder *, 172 AAudioStream_presentationEndCallback, 173 void *); 174 175 typedef aaudio_format_t (*signature_F_PS)(AAudioStream *stream); 176 177 typedef int32_t (*signature_I_PSPVIL)(AAudioStream *, void *, int32_t, int64_t); 178 typedef int32_t (*signature_I_PSCPVIL)(AAudioStream *, const void *, int32_t, int64_t); 179 180 typedef int32_t (*signature_I_PSTPTL)(AAudioStream *, 181 aaudio_stream_state_t, 182 aaudio_stream_state_t *, 183 int64_t); 184 185 typedef int32_t (*signature_I_PSKPLPL)(AAudioStream *, clockid_t, int64_t *, int64_t *); 186 187 typedef bool (*signature_O_PS)(AAudioStream *); 188 189 typedef uint32_t (*signature_U_PS)(AAudioStream *); 190 191 typedef int32_t (*signature_I_II)(int32_t, int32_t); 192 typedef int32_t (*signature_I_I)(int32_t); 193 typedef int32_t (*signature_I)(); 194 typedef int32_t (*signature_I_PSII)(AAudioStream *, int32_t, int32_t); 195 196 static AAudioLoader* getInstance(); // singleton 197 198 /** 199 * Open the AAudio shared library and load the function pointers. 200 * This can be called multiple times. 201 * It should only be called from one thread. 202 * 203 * The destructor will clean up after the open. 204 * 205 * @return 0 if successful or negative error. 206 */ 207 int open(); 208 getLibHandle()209 void *getLibHandle() const { return mLibHandle; } 210 211 // Function pointers into the AAudio shared library. 212 signature_I_PPB createStreamBuilder = nullptr; 213 214 signature_I_PBPPS builder_openStream = nullptr; 215 216 signature_V_PBI builder_setBufferCapacityInFrames = nullptr; 217 signature_V_PBI builder_setChannelCount = nullptr; 218 signature_V_PBI builder_setDeviceId = nullptr; 219 signature_V_PBI builder_setDirection = nullptr; 220 signature_V_PBI builder_setFormat = nullptr; 221 signature_V_PBI builder_setFramesPerDataCallback = nullptr; 222 signature_V_PBI builder_setPerformanceMode = nullptr; 223 signature_V_PBI builder_setSampleRate = nullptr; 224 signature_V_PBI builder_setSharingMode = nullptr; 225 signature_V_PBU builder_setChannelMask = nullptr; 226 227 signature_V_PBI builder_setUsage = nullptr; 228 signature_V_PBI builder_setContentType = nullptr; 229 signature_V_PBI builder_setInputPreset = nullptr; 230 signature_V_PBI builder_setSessionId = nullptr; 231 232 signature_V_PBO builder_setPrivacySensitive = nullptr; 233 signature_V_PBI builder_setAllowedCapturePolicy = nullptr; 234 235 signature_V_PBCPH builder_setPackageName = nullptr; 236 signature_V_PBCPH builder_setAttributionTag = nullptr; 237 238 signature_V_PBO builder_setIsContentSpatialized = nullptr; 239 signature_V_PBI builder_setSpatializationBehavior = nullptr; 240 241 signature_V_PBPDPV builder_setDataCallback = nullptr; 242 signature_V_PBPEPV builder_setErrorCallback = nullptr; 243 signature_V_PBPRPV builder_setPresentationEndCallback = nullptr; 244 245 signature_I_PB builder_delete = nullptr; 246 247 signature_F_PS stream_getFormat = nullptr; 248 249 signature_I_PSPVIL stream_read = nullptr; 250 signature_I_PSCPVIL stream_write = nullptr; 251 252 signature_I_PSTPTL stream_waitForStateChange = nullptr; 253 254 signature_I_PSKPLPL stream_getTimestamp = nullptr; 255 256 signature_I_PS stream_release = nullptr; 257 signature_I_PS stream_close = nullptr; 258 259 signature_I_PS stream_getChannelCount = nullptr; 260 signature_I_PS stream_getDeviceId = nullptr; 261 262 signature_I_PS stream_getBufferSize = nullptr; 263 signature_I_PS stream_getBufferCapacity = nullptr; 264 signature_I_PS stream_getFramesPerBurst = nullptr; 265 signature_I_PS stream_getState = nullptr; 266 signature_I_PS stream_getPerformanceMode = nullptr; 267 signature_I_PS stream_getSampleRate = nullptr; 268 signature_I_PS stream_getSharingMode = nullptr; 269 signature_I_PS stream_getXRunCount = nullptr; 270 271 signature_I_PSI stream_setBufferSize = nullptr; 272 signature_I_PS stream_requestStart = nullptr; 273 signature_I_PS stream_requestPause = nullptr; 274 signature_I_PS stream_requestFlush = nullptr; 275 signature_I_PS stream_requestStop = nullptr; 276 277 signature_L_PS stream_getFramesRead = nullptr; 278 signature_L_PS stream_getFramesWritten = nullptr; 279 280 signature_CPH_I convertResultToText = nullptr; 281 282 signature_I_PS stream_getUsage = nullptr; 283 signature_I_PS stream_getContentType = nullptr; 284 signature_I_PS stream_getInputPreset = nullptr; 285 signature_I_PS stream_getSessionId = nullptr; 286 287 signature_O_PS stream_isPrivacySensitive = nullptr; 288 signature_I_PS stream_getAllowedCapturePolicy = nullptr; 289 290 signature_U_PS stream_getChannelMask = nullptr; 291 292 signature_O_PS stream_isContentSpatialized = nullptr; 293 signature_I_PS stream_getSpatializationBehavior = nullptr; 294 295 signature_I_PS stream_getHardwareChannelCount = nullptr; 296 signature_I_PS stream_getHardwareSampleRate = nullptr; 297 signature_F_PS stream_getHardwareFormat = nullptr; 298 299 300 signature_I_II aaudio_getPlatformMMapPolicy = nullptr; 301 signature_I_II aaudio_getPlatformMMapExclusivePolicy = nullptr; 302 signature_I_I aaudio_setMMapPolicy = nullptr; 303 signature_I aaudio_getMMapPolicy = nullptr; 304 signature_O_PS stream_isMMapUsed = nullptr; 305 306 signature_I_PSII stream_setOffloadDelayPadding = nullptr; 307 signature_I_PS stream_getOffloadDelay = nullptr; 308 signature_I_PS stream_getOffloadPadding = nullptr; 309 signature_I_PS stream_setOffloadEndOfStream = nullptr; 310 311 private: AAudioLoader()312 AAudioLoader() {} 313 ~AAudioLoader(); 314 315 // Load function pointers for specific signatures. 316 signature_I_PPB load_I_PPB(const char *name); 317 signature_CPH_I load_CPH_I(const char *name); 318 signature_V_PBI load_V_PBI(const char *name); 319 signature_V_PBCPH load_V_PBCPH(const char *name); 320 signature_V_PBPDPV load_V_PBPDPV(const char *name); 321 signature_V_PBPEPV load_V_PBPEPV(const char *name); 322 signature_I_PB load_I_PB(const char *name); 323 signature_I_PBPPS load_I_PBPPS(const char *name); 324 signature_I_PS load_I_PS(const char *name); 325 signature_L_PS load_L_PS(const char *name); 326 signature_F_PS load_F_PS(const char *name); 327 signature_O_PS load_O_PS(const char *name); 328 signature_I_PSI load_I_PSI(const char *name); 329 signature_I_PSPVIL load_I_PSPVIL(const char *name); 330 signature_I_PSCPVIL load_I_PSCPVIL(const char *name); 331 signature_I_PSTPTL load_I_PSTPTL(const char *name); 332 signature_I_PSKPLPL load_I_PSKPLPL(const char *name); 333 signature_V_PBU load_V_PBU(const char *name); 334 signature_U_PS load_U_PS(const char *name); 335 signature_V_PBO load_V_PBO(const char *name); 336 signature_I_II load_I_II(const char *name); 337 signature_I_I load_I_I(const char *name); 338 signature_I load_I(const char *name); 339 signature_V_PBPRPV load_V_PBPRPV(const char *name); 340 signature_I_PSII load_I_PSII(const char *name); 341 342 void *mLibHandle = nullptr; 343 }; 344 345 } // namespace oboe 346 347 #endif //OBOE_AAUDIO_LOADER_H_ 348