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 <aaudio/AAudio.h> 21 22 /** 23 * The AAudio API was not available in early versions of Android. 24 * To avoid linker errors, we dynamically link with the functions by name using dlsym(). 25 * On older versions this linkage will safely fail. 26 */ 27 class AAudioLoader { 28 public: 29 // Use signatures for common functions. 30 typedef const char * (*signature_PC_I)(int32_t); 31 typedef int32_t (*signature_I_I)(int32_t); 32 typedef int32_t (*signature_I_II)(int32_t, int32_t); 33 typedef int32_t (*signature_I_IPI)(int32_t, int32_t *); 34 typedef int32_t (*signature_I_IIPI)(int32_t, int32_t, int32_t *); 35 36 typedef int32_t (*signature_I_PB)(AAudioStreamBuilder *); // AAudioStreamBuilder_delete() 37 // AAudioStreamBuilder_setSampleRate() 38 typedef void (*signature_V_PBI)(AAudioStreamBuilder *, int32_t); 39 40 typedef int32_t (*signature_I_PS)(AAudioStream *); // AAudioStream_getSampleRate() 41 typedef int64_t (*signature_L_PS)(AAudioStream *); // AAudioStream_getFramesRead() 42 // AAudioStream_setBufferSizeInFrames() 43 typedef int32_t (*signature_I_PSI)(AAudioStream *, int32_t); 44 45 static AAudioLoader* getInstance(); // singleton 46 47 /** 48 * Open the AAudio shared library and load the function pointers. 49 * This can be called multiple times. 50 * It should only be called from one thread. 51 * 52 * @return 0 if successful or negative error. 53 */ 54 int open(); 55 56 /** 57 * Close the AAudio shared library. 58 * This can be called multiple times. 59 * It should only be called from one thread. 60 * 61 * The open() and close() do not nest. Calling close() once will always close the library. 62 * The destructor will call close() so you don't need to. 63 * 64 * @return 0 if successful or negative error. 65 */ 66 int close(); 67 68 // Function pointers into the AAudio shared library. 69 aaudio_result_t (*createStreamBuilder)(AAudioStreamBuilder **builder); 70 71 aaudio_result_t (*builder_openStream)(AAudioStreamBuilder *builder, 72 AAudioStream **stream); 73 74 signature_V_PBI builder_setBufferCapacityInFrames; 75 signature_V_PBI builder_setChannelCount; 76 signature_V_PBI builder_setDeviceId; 77 signature_V_PBI builder_setDirection; 78 signature_V_PBI builder_setFormat; 79 signature_V_PBI builder_setFramesPerDataCallback; 80 signature_V_PBI builder_setPerformanceMode; 81 signature_V_PBI builder_setSampleRate; 82 signature_V_PBI builder_setSharingMode; 83 84 void (*builder_setDataCallback)(AAudioStreamBuilder *builder, 85 AAudioStream_dataCallback callback, 86 void *userData); 87 88 void (*builder_setErrorCallback)(AAudioStreamBuilder *builder, 89 AAudioStream_errorCallback callback, 90 void *userData); 91 92 signature_I_PB builder_delete; 93 94 aaudio_format_t (*stream_getFormat)(AAudioStream *stream); 95 96 aaudio_result_t (*stream_read)(AAudioStream* stream, 97 void *buffer, 98 int32_t numFrames, 99 int64_t timeoutNanoseconds); 100 101 aaudio_result_t (*stream_write)(AAudioStream *stream, 102 const void *buffer, 103 int32_t numFrames, 104 int64_t timeoutNanoseconds); 105 106 aaudio_result_t (*stream_waitForStateChange)(AAudioStream *stream, 107 aaudio_stream_state_t inputState, 108 aaudio_stream_state_t *nextState, 109 int64_t timeoutNanoseconds); 110 111 aaudio_result_t (*stream_getTimestamp)(AAudioStream *stream, 112 clockid_t clockid, 113 int64_t *framePosition, 114 int64_t *timeNanoseconds); 115 116 signature_I_PS stream_close; 117 118 signature_I_PS stream_getChannelCount; 119 signature_I_PS stream_getDeviceId; 120 signature_I_PS stream_getDirection; 121 signature_I_PS stream_getBufferSize; 122 signature_I_PS stream_getBufferCapacity; 123 signature_I_PS stream_getFramesPerBurst; 124 signature_I_PS stream_getState; 125 signature_I_PS stream_getPerformanceMode; 126 signature_I_PS stream_getSampleRate; 127 signature_I_PS stream_getSharingMode; 128 signature_I_PS stream_getXRunCount; 129 130 signature_I_PSI stream_setBufferSize; 131 signature_I_PS stream_requestStart; 132 signature_I_PS stream_requestPause; 133 signature_I_PS stream_requestFlush; 134 signature_I_PS stream_requestStop; 135 136 signature_L_PS stream_getFramesRead; 137 signature_L_PS stream_getFramesWritten; 138 139 signature_PC_I convertResultToText; 140 signature_PC_I convertStreamStateToText; 141 142 // TODO add any missing AAudio functions. 143 144 private: AAudioLoader()145 AAudioLoader() {} 146 ~AAudioLoader(); 147 148 // Load function pointers for specific signatures. 149 signature_PC_I load_PC_I(const char *name); 150 151 signature_V_PBI load_V_PBI(const char *name); 152 signature_I_PB load_I_PB(const char *name); 153 signature_I_PS load_I_PS(const char *name); 154 signature_L_PS load_L_PS(const char *name); 155 signature_I_PSI load_I_PSI(const char *name); 156 157 void *mLibHandle = nullptr; 158 }; 159 160 #endif //OBOE_AAUDIO_LOADER_H_ 161