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 #include <dlfcn.h>
18 #include <oboe/Utilities.h>
19 #include "common/OboeDebug.h"
20 #include "AAudioLoader.h"
21
22 #define LIB_AAUDIO_NAME "libaaudio.so"
23
24 namespace oboe {
25
~AAudioLoader()26 AAudioLoader::~AAudioLoader() {
27 if (mLibHandle != nullptr) {
28 dlclose(mLibHandle);
29 mLibHandle = nullptr;
30 }
31 }
32
getInstance()33 AAudioLoader* AAudioLoader::getInstance() {
34 static AAudioLoader instance;
35 return &instance;
36 }
37
open()38 int AAudioLoader::open() {
39 if (mLibHandle != nullptr) {
40 return 0;
41 }
42
43 // Use RTLD_NOW to avoid the unpredictable behavior that RTLD_LAZY can cause.
44 // Also resolving all the links now will prevent a run-time penalty later.
45 mLibHandle = dlopen(LIB_AAUDIO_NAME, RTLD_NOW);
46 if (mLibHandle == nullptr) {
47 LOGI("AAudioLoader::open() could not find " LIB_AAUDIO_NAME);
48 return -1; // TODO review return code
49 } else {
50 LOGD("AAudioLoader(): dlopen(%s) returned %p", LIB_AAUDIO_NAME, mLibHandle);
51 }
52
53 // Load all the function pointers.
54 createStreamBuilder = load_I_PPB("AAudio_createStreamBuilder");
55 builder_openStream = load_I_PBPPS("AAudioStreamBuilder_openStream");
56
57 builder_setChannelCount = load_V_PBI("AAudioStreamBuilder_setChannelCount");
58 if (builder_setChannelCount == nullptr) {
59 // Use old deprecated alias if needed.
60 builder_setChannelCount = load_V_PBI("AAudioStreamBuilder_setSamplesPerFrame");
61 }
62
63 builder_setBufferCapacityInFrames = load_V_PBI("AAudioStreamBuilder_setBufferCapacityInFrames");
64 builder_setDeviceId = load_V_PBI("AAudioStreamBuilder_setDeviceId");
65 builder_setDirection = load_V_PBI("AAudioStreamBuilder_setDirection");
66 builder_setFormat = load_V_PBI("AAudioStreamBuilder_setFormat");
67 builder_setFramesPerDataCallback = load_V_PBI("AAudioStreamBuilder_setFramesPerDataCallback");
68 builder_setSharingMode = load_V_PBI("AAudioStreamBuilder_setSharingMode");
69 builder_setPerformanceMode = load_V_PBI("AAudioStreamBuilder_setPerformanceMode");
70 builder_setSampleRate = load_V_PBI("AAudioStreamBuilder_setSampleRate");
71
72 if (getSdkVersion() >= __ANDROID_API_P__){
73 builder_setUsage = load_V_PBI("AAudioStreamBuilder_setUsage");
74 builder_setContentType = load_V_PBI("AAudioStreamBuilder_setContentType");
75 builder_setInputPreset = load_V_PBI("AAudioStreamBuilder_setInputPreset");
76 builder_setSessionId = load_V_PBI("AAudioStreamBuilder_setSessionId");
77 }
78
79 builder_delete = load_I_PB("AAudioStreamBuilder_delete");
80
81
82 builder_setDataCallback = load_V_PBPDPV("AAudioStreamBuilder_setDataCallback");
83 builder_setErrorCallback = load_V_PBPEPV("AAudioStreamBuilder_setErrorCallback");
84
85 stream_read = load_I_PSPVIL("AAudioStream_read");
86
87 stream_write = load_I_PSCPVIL("AAudioStream_write");
88
89 stream_waitForStateChange = load_I_PSTPTL("AAudioStream_waitForStateChange");
90
91 stream_getTimestamp = load_I_PSKPLPL("AAudioStream_getTimestamp");
92
93 stream_isMMapUsed = load_B_PS("AAudioStream_isMMapUsed");
94
95 stream_getChannelCount = load_I_PS("AAudioStream_getChannelCount");
96 if (stream_getChannelCount == nullptr) {
97 // Use old alias if needed.
98 stream_getChannelCount = load_I_PS("AAudioStream_getSamplesPerFrame");
99 }
100
101 stream_close = load_I_PS("AAudioStream_close");
102
103 stream_getBufferSize = load_I_PS("AAudioStream_getBufferSizeInFrames");
104 stream_getDeviceId = load_I_PS("AAudioStream_getDeviceId");
105 stream_getBufferCapacity = load_I_PS("AAudioStream_getBufferCapacityInFrames");
106 stream_getFormat = load_F_PS("AAudioStream_getFormat");
107 stream_getFramesPerBurst = load_I_PS("AAudioStream_getFramesPerBurst");
108 stream_getFramesRead = load_L_PS("AAudioStream_getFramesRead");
109 stream_getFramesWritten = load_L_PS("AAudioStream_getFramesWritten");
110 stream_getPerformanceMode = load_I_PS("AAudioStream_getPerformanceMode");
111 stream_getSampleRate = load_I_PS("AAudioStream_getSampleRate");
112 stream_getSharingMode = load_I_PS("AAudioStream_getSharingMode");
113 stream_getState = load_I_PS("AAudioStream_getState");
114 stream_getXRunCount = load_I_PS("AAudioStream_getXRunCount");
115
116 stream_requestStart = load_I_PS("AAudioStream_requestStart");
117 stream_requestPause = load_I_PS("AAudioStream_requestPause");
118 stream_requestFlush = load_I_PS("AAudioStream_requestFlush");
119 stream_requestStop = load_I_PS("AAudioStream_requestStop");
120
121 stream_setBufferSize = load_I_PSI("AAudioStream_setBufferSizeInFrames");
122
123 convertResultToText = load_CPH_I("AAudio_convertResultToText");
124
125 if (getSdkVersion() >= __ANDROID_API_P__){
126 stream_getUsage = load_I_PS("AAudioStream_getUsage");
127 stream_getContentType = load_I_PS("AAudioStream_getContentType");
128 stream_getInputPreset = load_I_PS("AAudioStream_getInputPreset");
129 stream_getSessionId = load_I_PS("AAudioStream_getSessionId");
130 }
131 return 0;
132 }
133
AAudioLoader_check(void * proc,const char * functionName)134 static void AAudioLoader_check(void *proc, const char *functionName) {
135 if (proc == nullptr) {
136 LOGW("AAudioLoader could not find %s", functionName);
137 }
138 }
139
load_I_PPB(const char * functionName)140 AAudioLoader::signature_I_PPB AAudioLoader::load_I_PPB(const char *functionName) {
141 void *proc = dlsym(mLibHandle, functionName);
142 AAudioLoader_check(proc, functionName);
143 return reinterpret_cast<signature_I_PPB>(proc);
144 }
145
load_CPH_I(const char * functionName)146 AAudioLoader::signature_CPH_I AAudioLoader::load_CPH_I(const char *functionName) {
147 void *proc = dlsym(mLibHandle, functionName);
148 AAudioLoader_check(proc, functionName);
149 return reinterpret_cast<signature_CPH_I>(proc);
150 }
151
load_V_PBI(const char * functionName)152 AAudioLoader::signature_V_PBI AAudioLoader::load_V_PBI(const char *functionName) {
153 void *proc = dlsym(mLibHandle, functionName);
154 AAudioLoader_check(proc, functionName);
155 return reinterpret_cast<signature_V_PBI>(proc);
156 }
157
load_V_PBPDPV(const char * functionName)158 AAudioLoader::signature_V_PBPDPV AAudioLoader::load_V_PBPDPV(const char *functionName) {
159 void *proc = dlsym(mLibHandle, functionName);
160 AAudioLoader_check(proc, functionName);
161 return reinterpret_cast<signature_V_PBPDPV>(proc);
162 }
163
load_V_PBPEPV(const char * functionName)164 AAudioLoader::signature_V_PBPEPV AAudioLoader::load_V_PBPEPV(const char *functionName) {
165 void *proc = dlsym(mLibHandle, functionName);
166 AAudioLoader_check(proc, functionName);
167 return reinterpret_cast<signature_V_PBPEPV>(proc);
168 }
169
load_I_PSI(const char * functionName)170 AAudioLoader::signature_I_PSI AAudioLoader::load_I_PSI(const char *functionName) {
171 void *proc = dlsym(mLibHandle, functionName);
172 AAudioLoader_check(proc, functionName);
173 return reinterpret_cast<signature_I_PSI>(proc);
174 }
175
load_I_PS(const char * functionName)176 AAudioLoader::signature_I_PS AAudioLoader::load_I_PS(const char *functionName) {
177 void *proc = dlsym(mLibHandle, functionName);
178 AAudioLoader_check(proc, functionName);
179 return reinterpret_cast<signature_I_PS>(proc);
180 }
181
load_L_PS(const char * functionName)182 AAudioLoader::signature_L_PS AAudioLoader::load_L_PS(const char *functionName) {
183 void *proc = dlsym(mLibHandle, functionName);
184 AAudioLoader_check(proc, functionName);
185 return reinterpret_cast<signature_L_PS>(proc);
186 }
187
load_F_PS(const char * functionName)188 AAudioLoader::signature_F_PS AAudioLoader::load_F_PS(const char *functionName) {
189 void *proc = dlsym(mLibHandle, functionName);
190 AAudioLoader_check(proc, functionName);
191 return reinterpret_cast<signature_F_PS>(proc);
192 }
193
load_B_PS(const char * functionName)194 AAudioLoader::signature_B_PS AAudioLoader::load_B_PS(const char *functionName) {
195 void *proc = dlsym(mLibHandle, functionName);
196 AAudioLoader_check(proc, functionName);
197 return reinterpret_cast<signature_B_PS>(proc);
198 }
199
load_I_PB(const char * functionName)200 AAudioLoader::signature_I_PB AAudioLoader::load_I_PB(const char *functionName) {
201 void *proc = dlsym(mLibHandle, functionName);
202 AAudioLoader_check(proc, functionName);
203 return reinterpret_cast<signature_I_PB>(proc);
204 }
205
load_I_PBPPS(const char * functionName)206 AAudioLoader::signature_I_PBPPS AAudioLoader::load_I_PBPPS(const char *functionName) {
207 void *proc = dlsym(mLibHandle, functionName);
208 AAudioLoader_check(proc, functionName);
209 return reinterpret_cast<signature_I_PBPPS>(proc);
210 }
211
load_I_PSCPVIL(const char * functionName)212 AAudioLoader::signature_I_PSCPVIL AAudioLoader::load_I_PSCPVIL(const char *functionName) {
213 void *proc = dlsym(mLibHandle, functionName);
214 AAudioLoader_check(proc, functionName);
215 return reinterpret_cast<signature_I_PSCPVIL>(proc);
216 }
217
load_I_PSPVIL(const char * functionName)218 AAudioLoader::signature_I_PSPVIL AAudioLoader::load_I_PSPVIL(const char *functionName) {
219 void *proc = dlsym(mLibHandle, functionName);
220 AAudioLoader_check(proc, functionName);
221 return reinterpret_cast<signature_I_PSPVIL>(proc);
222 }
223
load_I_PSTPTL(const char * functionName)224 AAudioLoader::signature_I_PSTPTL AAudioLoader::load_I_PSTPTL(const char *functionName) {
225 void *proc = dlsym(mLibHandle, functionName);
226 AAudioLoader_check(proc, functionName);
227 return reinterpret_cast<signature_I_PSTPTL>(proc);
228 }
229
load_I_PSKPLPL(const char * functionName)230 AAudioLoader::signature_I_PSKPLPL AAudioLoader::load_I_PSKPLPL(const char *functionName) {
231 void *proc = dlsym(mLibHandle, functionName);
232 AAudioLoader_check(proc, functionName);
233 return reinterpret_cast<signature_I_PSKPLPL>(proc);
234 }
235
236 // Ensure that all AAudio primitive data types are int32_t
237 #define ASSERT_INT32(type) static_assert(std::is_same<int32_t, type>::value, \
238 #type" must be int32_t")
239
240 #define ERRMSG "Oboe constants must match AAudio constants."
241
242 // These asserts help verify that the Oboe definitions match the equivalent AAudio definitions.
243 // This code is in this .cpp file so it only gets tested once.
244 #ifdef AAUDIO_AAUDIO_H
245
246 ASSERT_INT32(aaudio_stream_state_t);
247 ASSERT_INT32(aaudio_direction_t);
248 ASSERT_INT32(aaudio_format_t);
249 ASSERT_INT32(aaudio_data_callback_result_t);
250 ASSERT_INT32(aaudio_result_t);
251 ASSERT_INT32(aaudio_sharing_mode_t);
252 ASSERT_INT32(aaudio_performance_mode_t);
253
254 static_assert((int32_t)StreamState::Uninitialized == AAUDIO_STREAM_STATE_UNINITIALIZED, ERRMSG);
255 static_assert((int32_t)StreamState::Unknown == AAUDIO_STREAM_STATE_UNKNOWN, ERRMSG);
256 static_assert((int32_t)StreamState::Open == AAUDIO_STREAM_STATE_OPEN, ERRMSG);
257 static_assert((int32_t)StreamState::Starting == AAUDIO_STREAM_STATE_STARTING, ERRMSG);
258 static_assert((int32_t)StreamState::Started == AAUDIO_STREAM_STATE_STARTED, ERRMSG);
259 static_assert((int32_t)StreamState::Pausing == AAUDIO_STREAM_STATE_PAUSING, ERRMSG);
260 static_assert((int32_t)StreamState::Paused == AAUDIO_STREAM_STATE_PAUSED, ERRMSG);
261 static_assert((int32_t)StreamState::Flushing == AAUDIO_STREAM_STATE_FLUSHING, ERRMSG);
262 static_assert((int32_t)StreamState::Flushed == AAUDIO_STREAM_STATE_FLUSHED, ERRMSG);
263 static_assert((int32_t)StreamState::Stopping == AAUDIO_STREAM_STATE_STOPPING, ERRMSG);
264 static_assert((int32_t)StreamState::Stopped == AAUDIO_STREAM_STATE_STOPPED, ERRMSG);
265 static_assert((int32_t)StreamState::Closing == AAUDIO_STREAM_STATE_CLOSING, ERRMSG);
266 static_assert((int32_t)StreamState::Closed == AAUDIO_STREAM_STATE_CLOSED, ERRMSG);
267 static_assert((int32_t)StreamState::Disconnected == AAUDIO_STREAM_STATE_DISCONNECTED, ERRMSG);
268
269 static_assert((int32_t)Direction::Output == AAUDIO_DIRECTION_OUTPUT, ERRMSG);
270 static_assert((int32_t)Direction::Input == AAUDIO_DIRECTION_INPUT, ERRMSG);
271
272 static_assert((int32_t)AudioFormat::Invalid == AAUDIO_FORMAT_INVALID, ERRMSG);
273 static_assert((int32_t)AudioFormat::Unspecified == AAUDIO_FORMAT_UNSPECIFIED, ERRMSG);
274 static_assert((int32_t)AudioFormat::I16 == AAUDIO_FORMAT_PCM_I16, ERRMSG);
275 static_assert((int32_t)AudioFormat::Float == AAUDIO_FORMAT_PCM_FLOAT, ERRMSG);
276
277 static_assert((int32_t)DataCallbackResult::Continue == AAUDIO_CALLBACK_RESULT_CONTINUE, ERRMSG);
278 static_assert((int32_t)DataCallbackResult::Stop == AAUDIO_CALLBACK_RESULT_STOP, ERRMSG);
279
280 static_assert((int32_t)Result::OK == AAUDIO_OK, ERRMSG);
281 static_assert((int32_t)Result::ErrorBase == AAUDIO_ERROR_BASE, ERRMSG);
282 static_assert((int32_t)Result::ErrorDisconnected == AAUDIO_ERROR_DISCONNECTED, ERRMSG);
283 static_assert((int32_t)Result::ErrorIllegalArgument == AAUDIO_ERROR_ILLEGAL_ARGUMENT, ERRMSG);
284 static_assert((int32_t)Result::ErrorInternal == AAUDIO_ERROR_INTERNAL, ERRMSG);
285 static_assert((int32_t)Result::ErrorInvalidState == AAUDIO_ERROR_INVALID_STATE, ERRMSG);
286 static_assert((int32_t)Result::ErrorInvalidHandle == AAUDIO_ERROR_INVALID_HANDLE, ERRMSG);
287 static_assert((int32_t)Result::ErrorUnimplemented == AAUDIO_ERROR_UNIMPLEMENTED, ERRMSG);
288 static_assert((int32_t)Result::ErrorUnavailable == AAUDIO_ERROR_UNAVAILABLE, ERRMSG);
289 static_assert((int32_t)Result::ErrorNoFreeHandles == AAUDIO_ERROR_NO_FREE_HANDLES, ERRMSG);
290 static_assert((int32_t)Result::ErrorNoMemory == AAUDIO_ERROR_NO_MEMORY, ERRMSG);
291 static_assert((int32_t)Result::ErrorNull == AAUDIO_ERROR_NULL, ERRMSG);
292 static_assert((int32_t)Result::ErrorTimeout == AAUDIO_ERROR_TIMEOUT, ERRMSG);
293 static_assert((int32_t)Result::ErrorWouldBlock == AAUDIO_ERROR_WOULD_BLOCK, ERRMSG);
294 static_assert((int32_t)Result::ErrorInvalidFormat == AAUDIO_ERROR_INVALID_FORMAT, ERRMSG);
295 static_assert((int32_t)Result::ErrorOutOfRange == AAUDIO_ERROR_OUT_OF_RANGE, ERRMSG);
296 static_assert((int32_t)Result::ErrorNoService == AAUDIO_ERROR_NO_SERVICE, ERRMSG);
297 static_assert((int32_t)Result::ErrorInvalidRate == AAUDIO_ERROR_INVALID_RATE, ERRMSG);
298
299 static_assert((int32_t)SharingMode::Exclusive == AAUDIO_SHARING_MODE_EXCLUSIVE, ERRMSG);
300 static_assert((int32_t)SharingMode::Shared == AAUDIO_SHARING_MODE_SHARED, ERRMSG);
301
302 static_assert((int32_t)PerformanceMode::None == AAUDIO_PERFORMANCE_MODE_NONE, ERRMSG);
303 static_assert((int32_t)PerformanceMode::PowerSaving
304 == AAUDIO_PERFORMANCE_MODE_POWER_SAVING, ERRMSG);
305 static_assert((int32_t)PerformanceMode::LowLatency
306 == AAUDIO_PERFORMANCE_MODE_LOW_LATENCY, ERRMSG);
307 #endif
308
309 // The aaudio_ usage, content and input_preset types were added in NDK 17,
310 // which is the first version to support Android Pie (API 28).
311 #if __NDK_MAJOR__ >= 17
312
313 ASSERT_INT32(aaudio_usage_t);
314 ASSERT_INT32(aaudio_content_type_t);
315 ASSERT_INT32(aaudio_input_preset_t);
316
317 static_assert((int32_t)Usage::Media == AAUDIO_USAGE_MEDIA, ERRMSG);
318 static_assert((int32_t)Usage::VoiceCommunication == AAUDIO_USAGE_VOICE_COMMUNICATION, ERRMSG);
319 static_assert((int32_t)Usage::VoiceCommunicationSignalling
320 == AAUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING, ERRMSG);
321 static_assert((int32_t)Usage::Alarm == AAUDIO_USAGE_ALARM, ERRMSG);
322 static_assert((int32_t)Usage::Notification == AAUDIO_USAGE_NOTIFICATION, ERRMSG);
323 static_assert((int32_t)Usage::NotificationRingtone == AAUDIO_USAGE_NOTIFICATION_RINGTONE, ERRMSG);
324 static_assert((int32_t)Usage::NotificationEvent == AAUDIO_USAGE_NOTIFICATION_EVENT, ERRMSG);
325 static_assert((int32_t)Usage::AssistanceAccessibility == AAUDIO_USAGE_ASSISTANCE_ACCESSIBILITY, ERRMSG);
326 static_assert((int32_t)Usage::AssistanceNavigationGuidance
327 == AAUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE, ERRMSG);
328 static_assert((int32_t)Usage::AssistanceSonification == AAUDIO_USAGE_ASSISTANCE_SONIFICATION, ERRMSG);
329 static_assert((int32_t)Usage::Game == AAUDIO_USAGE_GAME, ERRMSG);
330 static_assert((int32_t)Usage::Assistant == AAUDIO_USAGE_ASSISTANT, ERRMSG);
331
332 static_assert((int32_t)ContentType::Speech == AAUDIO_CONTENT_TYPE_SPEECH, ERRMSG);
333 static_assert((int32_t)ContentType::Music == AAUDIO_CONTENT_TYPE_MUSIC, ERRMSG);
334 static_assert((int32_t)ContentType::Movie == AAUDIO_CONTENT_TYPE_MOVIE, ERRMSG);
335 static_assert((int32_t)ContentType::Sonification == AAUDIO_CONTENT_TYPE_SONIFICATION, ERRMSG);
336
337 static_assert((int32_t)InputPreset::Generic == AAUDIO_INPUT_PRESET_GENERIC, ERRMSG);
338 static_assert((int32_t)InputPreset::Camcorder == AAUDIO_INPUT_PRESET_CAMCORDER, ERRMSG);
339 static_assert((int32_t)InputPreset::VoiceRecognition == AAUDIO_INPUT_PRESET_VOICE_RECOGNITION, ERRMSG);
340 static_assert((int32_t)InputPreset::VoiceCommunication
341 == AAUDIO_INPUT_PRESET_VOICE_COMMUNICATION, ERRMSG);
342 static_assert((int32_t)InputPreset::Unprocessed == AAUDIO_INPUT_PRESET_UNPROCESSED, ERRMSG);
343
344 static_assert((int32_t)SessionId::None == AAUDIO_SESSION_ID_NONE, ERRMSG);
345 static_assert((int32_t)SessionId::Allocate == AAUDIO_SESSION_ID_ALLOCATE, ERRMSG);
346 #endif
347
348 } // namespace oboe
349