• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     // Issue 360: thread_local variables with non-trivial destructors
28     // will cause segfaults if the containing library is dlclose()ed on
29     // devices running M or newer, or devices before M when using a static STL.
30     // The simple workaround is to not call dlclose.
31     // https://github.com/android/ndk/wiki/Changelog-r22#known-issues
32     //
33     // The libaaudio and libaaudioclient do not use thread_local.
34     // But, to be safe, we should avoid dlclose() if possible.
35     // Because AAudioLoader is a static Singleton, we can safely skip
36     // calling dlclose() without causing a resource leak.
37     LOGI("%s() dlclose(%s) not called, OK", __func__, LIB_AAUDIO_NAME);
38 }
39 
getInstance()40 AAudioLoader* AAudioLoader::getInstance() {
41     static AAudioLoader instance;
42     return &instance;
43 }
44 
open()45 int AAudioLoader::open() {
46     if (mLibHandle != nullptr) {
47         return 0;
48     }
49 
50     // Use RTLD_NOW to avoid the unpredictable behavior that RTLD_LAZY can cause.
51     // Also resolving all the links now will prevent a run-time penalty later.
52     mLibHandle = dlopen(LIB_AAUDIO_NAME, RTLD_NOW);
53     if (mLibHandle == nullptr) {
54         LOGI("AAudioLoader::open() could not find " LIB_AAUDIO_NAME);
55         return -1; // TODO review return code
56     } else {
57         LOGD("AAudioLoader():  dlopen(%s) returned %p", LIB_AAUDIO_NAME, mLibHandle);
58     }
59 
60     // Load all the function pointers.
61     createStreamBuilder        = load_I_PPB("AAudio_createStreamBuilder");
62     builder_openStream         = load_I_PBPPS("AAudioStreamBuilder_openStream");
63 
64     builder_setChannelCount    = load_V_PBI("AAudioStreamBuilder_setChannelCount");
65     if (builder_setChannelCount == nullptr) {
66         // Use old deprecated alias if needed.
67         builder_setChannelCount = load_V_PBI("AAudioStreamBuilder_setSamplesPerFrame");
68     }
69 
70     builder_setBufferCapacityInFrames = load_V_PBI("AAudioStreamBuilder_setBufferCapacityInFrames");
71     builder_setDeviceId        = load_V_PBI("AAudioStreamBuilder_setDeviceId");
72     builder_setDirection       = load_V_PBI("AAudioStreamBuilder_setDirection");
73     builder_setFormat          = load_V_PBI("AAudioStreamBuilder_setFormat");
74     builder_setFramesPerDataCallback = load_V_PBI("AAudioStreamBuilder_setFramesPerDataCallback");
75     builder_setSharingMode     = load_V_PBI("AAudioStreamBuilder_setSharingMode");
76     builder_setPerformanceMode = load_V_PBI("AAudioStreamBuilder_setPerformanceMode");
77     builder_setSampleRate      = load_V_PBI("AAudioStreamBuilder_setSampleRate");
78 
79     if (getSdkVersion() >= __ANDROID_API_P__){
80         builder_setUsage       = load_V_PBI("AAudioStreamBuilder_setUsage");
81         builder_setContentType = load_V_PBI("AAudioStreamBuilder_setContentType");
82         builder_setInputPreset = load_V_PBI("AAudioStreamBuilder_setInputPreset");
83         builder_setSessionId   = load_V_PBI("AAudioStreamBuilder_setSessionId");
84     }
85 
86     if (getSdkVersion() >= __ANDROID_API_Q__){
87         builder_setAllowedCapturePolicy = load_V_PBI("AAudioStreamBuilder_setAllowedCapturePolicy");
88     }
89 
90     if (getSdkVersion() >= __ANDROID_API_R__){
91         builder_setPrivacySensitive  = load_V_PBO("AAudioStreamBuilder_setPrivacySensitive");
92     }
93 
94     if (getSdkVersion() >= __ANDROID_API_S__){
95         builder_setPackageName       = load_V_PBCPH("AAudioStreamBuilder_setPackageName");
96         builder_setAttributionTag    = load_V_PBCPH("AAudioStreamBuilder_setAttributionTag");
97     }
98 
99     if (getSdkVersion() >= __ANDROID_API_S_V2__) {
100         builder_setChannelMask = load_V_PBU("AAudioStreamBuilder_setChannelMask");
101         builder_setIsContentSpatialized = load_V_PBO("AAudioStreamBuilder_setIsContentSpatialized");
102         builder_setSpatializationBehavior = load_V_PBI("AAudioStreamBuilder_setSpatializationBehavior");
103     }
104 
105     if (getSdkVersion() >= __ANDROID_API_B__) {
106         builder_setPresentationEndCallback = load_V_PBPRPV("AAudioStreamBuilder_setPresentationEndCallback");
107     }
108 
109     builder_delete             = load_I_PB("AAudioStreamBuilder_delete");
110 
111 
112     builder_setDataCallback    = load_V_PBPDPV("AAudioStreamBuilder_setDataCallback");
113     builder_setErrorCallback   = load_V_PBPEPV("AAudioStreamBuilder_setErrorCallback");
114 
115     stream_read                = load_I_PSPVIL("AAudioStream_read");
116 
117     stream_write               = load_I_PSCPVIL("AAudioStream_write");
118 
119     stream_waitForStateChange  = load_I_PSTPTL("AAudioStream_waitForStateChange");
120 
121     stream_getTimestamp        = load_I_PSKPLPL("AAudioStream_getTimestamp");
122 
123     stream_getChannelCount     = load_I_PS("AAudioStream_getChannelCount");
124     if (stream_getChannelCount == nullptr) {
125         // Use old alias if needed.
126         stream_getChannelCount    = load_I_PS("AAudioStream_getSamplesPerFrame");
127     }
128 
129     if (getSdkVersion() >= __ANDROID_API_R__) {
130         stream_release         = load_I_PS("AAudioStream_release");
131     }
132 
133     stream_close               = load_I_PS("AAudioStream_close");
134 
135     stream_getBufferSize       = load_I_PS("AAudioStream_getBufferSizeInFrames");
136     stream_getDeviceId         = load_I_PS("AAudioStream_getDeviceId");
137     stream_getBufferCapacity   = load_I_PS("AAudioStream_getBufferCapacityInFrames");
138     stream_getFormat           = load_F_PS("AAudioStream_getFormat");
139     stream_getFramesPerBurst   = load_I_PS("AAudioStream_getFramesPerBurst");
140     stream_getFramesRead       = load_L_PS("AAudioStream_getFramesRead");
141     stream_getFramesWritten    = load_L_PS("AAudioStream_getFramesWritten");
142     stream_getPerformanceMode  = load_I_PS("AAudioStream_getPerformanceMode");
143     stream_getSampleRate       = load_I_PS("AAudioStream_getSampleRate");
144     stream_getSharingMode      = load_I_PS("AAudioStream_getSharingMode");
145     stream_getState            = load_I_PS("AAudioStream_getState");
146     stream_getXRunCount        = load_I_PS("AAudioStream_getXRunCount");
147 
148     stream_requestStart        = load_I_PS("AAudioStream_requestStart");
149     stream_requestPause        = load_I_PS("AAudioStream_requestPause");
150     stream_requestFlush        = load_I_PS("AAudioStream_requestFlush");
151     stream_requestStop         = load_I_PS("AAudioStream_requestStop");
152 
153     stream_setBufferSize       = load_I_PSI("AAudioStream_setBufferSizeInFrames");
154 
155     convertResultToText        = load_CPH_I("AAudio_convertResultToText");
156 
157     if (getSdkVersion() >= __ANDROID_API_P__){
158         stream_getUsage        = load_I_PS("AAudioStream_getUsage");
159         stream_getContentType  = load_I_PS("AAudioStream_getContentType");
160         stream_getInputPreset  = load_I_PS("AAudioStream_getInputPreset");
161         stream_getSessionId    = load_I_PS("AAudioStream_getSessionId");
162     }
163 
164     if (getSdkVersion() >= __ANDROID_API_Q__){
165         stream_getAllowedCapturePolicy    = load_I_PS("AAudioStream_getAllowedCapturePolicy");
166     }
167 
168     if (getSdkVersion() >= __ANDROID_API_R__){
169         stream_isPrivacySensitive  = load_O_PS("AAudioStream_isPrivacySensitive");
170     }
171 
172     if (getSdkVersion() >= __ANDROID_API_S_V2__) {
173         stream_getChannelMask = load_U_PS("AAudioStream_getChannelMask");
174         stream_isContentSpatialized = load_O_PS("AAudioStream_isContentSpatialized");
175         stream_getSpatializationBehavior = load_I_PS("AAudioStream_getSpatializationBehavior");
176     }
177 
178     if (getSdkVersion() >= __ANDROID_API_U__) {
179         stream_getHardwareChannelCount = load_I_PS("AAudioStream_getHardwareChannelCount");
180         stream_getHardwareSampleRate = load_I_PS("AAudioStream_getHardwareSampleRate");
181         stream_getHardwareFormat = load_F_PS("AAudioStream_getHardwareFormat");
182     }
183 
184     if (getSdkVersion() >= __ANDROID_API_B__) {
185         aaudio_getPlatformMMapPolicy = load_I_II("AAudio_getPlatformMMapPolicy");
186         aaudio_getPlatformMMapExclusivePolicy = load_I_II("AAudio_getPlatformMMapExclusivePolicy");
187         aaudio_setMMapPolicy = load_I_I("AAudio_setMMapPolicy");
188         aaudio_getMMapPolicy = load_I("AAudio_getMMapPolicy");
189         stream_isMMapUsed = load_O_PS("AAudioStream_isMMapUsed");
190 
191         stream_setOffloadDelayPadding = load_I_PSII("AAudioStream_setOffloadDelayPadding");
192         stream_getOffloadDelay = load_I_PS("AAudioStream_getOffloadDelay");
193         stream_getOffloadPadding = load_I_PS("AAudioStream_getOffloadPadding");
194         stream_setOffloadEndOfStream = load_I_PS("AAudioStream_setOffloadEndOfStream");
195     }
196 
197     return 0;
198 }
199 
AAudioLoader_check(void * proc,const char * functionName)200 static void AAudioLoader_check(void *proc, const char *functionName) {
201     if (proc == nullptr) {
202         LOGW("AAudioLoader could not find %s", functionName);
203     }
204 }
205 
load_I_PPB(const char * functionName)206 AAudioLoader::signature_I_PPB AAudioLoader::load_I_PPB(const char *functionName) {
207     void *proc = dlsym(mLibHandle, functionName);
208     AAudioLoader_check(proc, functionName);
209     return reinterpret_cast<signature_I_PPB>(proc);
210 }
211 
load_CPH_I(const char * functionName)212 AAudioLoader::signature_CPH_I AAudioLoader::load_CPH_I(const char *functionName) {
213     void *proc = dlsym(mLibHandle, functionName);
214     AAudioLoader_check(proc, functionName);
215     return reinterpret_cast<signature_CPH_I>(proc);
216 }
217 
load_V_PBI(const char * functionName)218 AAudioLoader::signature_V_PBI AAudioLoader::load_V_PBI(const char *functionName) {
219     void *proc = dlsym(mLibHandle, functionName);
220     AAudioLoader_check(proc, functionName);
221     return reinterpret_cast<signature_V_PBI>(proc);
222 }
223 
load_V_PBCPH(const char * functionName)224 AAudioLoader::signature_V_PBCPH AAudioLoader::load_V_PBCPH(const char *functionName) {
225     void *proc = dlsym(mLibHandle, functionName);
226     AAudioLoader_check(proc, functionName);
227     return reinterpret_cast<signature_V_PBCPH>(proc);
228 }
229 
load_V_PBPDPV(const char * functionName)230 AAudioLoader::signature_V_PBPDPV AAudioLoader::load_V_PBPDPV(const char *functionName) {
231     void *proc = dlsym(mLibHandle, functionName);
232     AAudioLoader_check(proc, functionName);
233     return reinterpret_cast<signature_V_PBPDPV>(proc);
234 }
235 
load_V_PBPEPV(const char * functionName)236 AAudioLoader::signature_V_PBPEPV AAudioLoader::load_V_PBPEPV(const char *functionName) {
237     void *proc = dlsym(mLibHandle, functionName);
238     AAudioLoader_check(proc, functionName);
239     return reinterpret_cast<signature_V_PBPEPV>(proc);
240 }
241 
load_I_PSI(const char * functionName)242 AAudioLoader::signature_I_PSI AAudioLoader::load_I_PSI(const char *functionName) {
243     void *proc = dlsym(mLibHandle, functionName);
244     AAudioLoader_check(proc, functionName);
245     return reinterpret_cast<signature_I_PSI>(proc);
246 }
247 
load_I_PS(const char * functionName)248 AAudioLoader::signature_I_PS AAudioLoader::load_I_PS(const char *functionName) {
249     void *proc = dlsym(mLibHandle, functionName);
250     AAudioLoader_check(proc, functionName);
251     return reinterpret_cast<signature_I_PS>(proc);
252 }
253 
load_L_PS(const char * functionName)254 AAudioLoader::signature_L_PS AAudioLoader::load_L_PS(const char *functionName) {
255     void *proc = dlsym(mLibHandle, functionName);
256     AAudioLoader_check(proc, functionName);
257     return reinterpret_cast<signature_L_PS>(proc);
258 }
259 
load_F_PS(const char * functionName)260 AAudioLoader::signature_F_PS AAudioLoader::load_F_PS(const char *functionName) {
261     void *proc = dlsym(mLibHandle, functionName);
262     AAudioLoader_check(proc, functionName);
263     return reinterpret_cast<signature_F_PS>(proc);
264 }
265 
load_O_PS(const char * functionName)266 AAudioLoader::signature_O_PS AAudioLoader::load_O_PS(const char *functionName) {
267     void *proc = dlsym(mLibHandle, functionName);
268     AAudioLoader_check(proc, functionName);
269     return reinterpret_cast<signature_O_PS>(proc);
270 }
271 
load_I_PB(const char * functionName)272 AAudioLoader::signature_I_PB AAudioLoader::load_I_PB(const char *functionName) {
273     void *proc = dlsym(mLibHandle, functionName);
274     AAudioLoader_check(proc, functionName);
275     return reinterpret_cast<signature_I_PB>(proc);
276 }
277 
load_I_PBPPS(const char * functionName)278 AAudioLoader::signature_I_PBPPS AAudioLoader::load_I_PBPPS(const char *functionName) {
279     void *proc = dlsym(mLibHandle, functionName);
280     AAudioLoader_check(proc, functionName);
281     return reinterpret_cast<signature_I_PBPPS>(proc);
282 }
283 
load_I_PSCPVIL(const char * functionName)284 AAudioLoader::signature_I_PSCPVIL AAudioLoader::load_I_PSCPVIL(const char *functionName) {
285     void *proc = dlsym(mLibHandle, functionName);
286     AAudioLoader_check(proc, functionName);
287     return reinterpret_cast<signature_I_PSCPVIL>(proc);
288 }
289 
load_I_PSPVIL(const char * functionName)290 AAudioLoader::signature_I_PSPVIL AAudioLoader::load_I_PSPVIL(const char *functionName) {
291     void *proc = dlsym(mLibHandle, functionName);
292     AAudioLoader_check(proc, functionName);
293     return reinterpret_cast<signature_I_PSPVIL>(proc);
294 }
295 
load_I_PSTPTL(const char * functionName)296 AAudioLoader::signature_I_PSTPTL AAudioLoader::load_I_PSTPTL(const char *functionName) {
297     void *proc = dlsym(mLibHandle, functionName);
298     AAudioLoader_check(proc, functionName);
299     return reinterpret_cast<signature_I_PSTPTL>(proc);
300 }
301 
load_I_PSKPLPL(const char * functionName)302 AAudioLoader::signature_I_PSKPLPL AAudioLoader::load_I_PSKPLPL(const char *functionName) {
303     void *proc = dlsym(mLibHandle, functionName);
304     AAudioLoader_check(proc, functionName);
305     return reinterpret_cast<signature_I_PSKPLPL>(proc);
306 }
307 
load_V_PBU(const char * functionName)308 AAudioLoader::signature_V_PBU AAudioLoader::load_V_PBU(const char *functionName) {
309     void *proc = dlsym(mLibHandle, functionName);
310     AAudioLoader_check(proc, functionName);
311     return reinterpret_cast<signature_V_PBU>(proc);
312 }
313 
load_U_PS(const char * functionName)314 AAudioLoader::signature_U_PS AAudioLoader::load_U_PS(const char *functionName) {
315     void *proc = dlsym(mLibHandle, functionName);
316     AAudioLoader_check(proc, functionName);
317     return reinterpret_cast<signature_U_PS>(proc);
318 }
319 
load_V_PBO(const char * functionName)320 AAudioLoader::signature_V_PBO AAudioLoader::load_V_PBO(const char *functionName) {
321     void *proc = dlsym(mLibHandle, functionName);
322     AAudioLoader_check(proc, functionName);
323     return reinterpret_cast<signature_V_PBO>(proc);
324 }
325 
load_I_II(const char * functionName)326 AAudioLoader::signature_I_II AAudioLoader::load_I_II(const char *functionName) {
327     void *proc = dlsym(mLibHandle, functionName);
328     AAudioLoader_check(proc, functionName);
329     return reinterpret_cast<signature_I_II>(proc);
330 }
331 
load_I_I(const char * functionName)332 AAudioLoader::signature_I_I AAudioLoader::load_I_I(const char *functionName) {
333     void *proc = dlsym(mLibHandle, functionName);
334     AAudioLoader_check(proc, functionName);
335     return reinterpret_cast<signature_I_I>(proc);
336 }
337 
load_I(const char * functionName)338 AAudioLoader::signature_I AAudioLoader::load_I(const char *functionName) {
339     void *proc = dlsym(mLibHandle, functionName);
340     AAudioLoader_check(proc, functionName);
341     return reinterpret_cast<signature_I>(proc);
342 }
343 
load_V_PBPRPV(const char * functionName)344 AAudioLoader::signature_V_PBPRPV AAudioLoader::load_V_PBPRPV(const char *functionName) {
345     void *proc = dlsym(mLibHandle, functionName);
346     AAudioLoader_check(proc, functionName);
347     return reinterpret_cast<signature_V_PBPRPV>(proc);
348 }
349 
load_I_PSII(const char * functionName)350 AAudioLoader::signature_I_PSII AAudioLoader::load_I_PSII(const char *functionName) {
351     void *proc = dlsym(mLibHandle, functionName);
352     AAudioLoader_check(proc, functionName);
353     return reinterpret_cast<signature_I_PSII>(proc);
354 }
355 
356 // Ensure that all AAudio primitive data types are int32_t
357 #define ASSERT_INT32(type) static_assert(std::is_same<int32_t, type>::value, \
358 #type" must be int32_t")
359 
360 // Ensure that all AAudio primitive data types are uint32_t
361 #define ASSERT_UINT32(type) static_assert(std::is_same<uint32_t, type>::value, \
362 #type" must be uint32_t")
363 
364 #define ERRMSG "Oboe constants must match AAudio constants."
365 
366 // These asserts help verify that the Oboe definitions match the equivalent AAudio definitions.
367 // This code is in this .cpp file so it only gets tested once.
368 #ifdef AAUDIO_AAUDIO_H
369 
370     ASSERT_INT32(aaudio_stream_state_t);
371     ASSERT_INT32(aaudio_direction_t);
372     ASSERT_INT32(aaudio_format_t);
373     ASSERT_INT32(aaudio_data_callback_result_t);
374     ASSERT_INT32(aaudio_result_t);
375     ASSERT_INT32(aaudio_sharing_mode_t);
376     ASSERT_INT32(aaudio_performance_mode_t);
377 
378     static_assert((int32_t)StreamState::Uninitialized == AAUDIO_STREAM_STATE_UNINITIALIZED, ERRMSG);
379     static_assert((int32_t)StreamState::Unknown == AAUDIO_STREAM_STATE_UNKNOWN, ERRMSG);
380     static_assert((int32_t)StreamState::Open == AAUDIO_STREAM_STATE_OPEN, ERRMSG);
381     static_assert((int32_t)StreamState::Starting == AAUDIO_STREAM_STATE_STARTING, ERRMSG);
382     static_assert((int32_t)StreamState::Started == AAUDIO_STREAM_STATE_STARTED, ERRMSG);
383     static_assert((int32_t)StreamState::Pausing == AAUDIO_STREAM_STATE_PAUSING, ERRMSG);
384     static_assert((int32_t)StreamState::Paused == AAUDIO_STREAM_STATE_PAUSED, ERRMSG);
385     static_assert((int32_t)StreamState::Flushing == AAUDIO_STREAM_STATE_FLUSHING, ERRMSG);
386     static_assert((int32_t)StreamState::Flushed == AAUDIO_STREAM_STATE_FLUSHED, ERRMSG);
387     static_assert((int32_t)StreamState::Stopping == AAUDIO_STREAM_STATE_STOPPING, ERRMSG);
388     static_assert((int32_t)StreamState::Stopped == AAUDIO_STREAM_STATE_STOPPED, ERRMSG);
389     static_assert((int32_t)StreamState::Closing == AAUDIO_STREAM_STATE_CLOSING, ERRMSG);
390     static_assert((int32_t)StreamState::Closed == AAUDIO_STREAM_STATE_CLOSED, ERRMSG);
391     static_assert((int32_t)StreamState::Disconnected == AAUDIO_STREAM_STATE_DISCONNECTED, ERRMSG);
392 
393     static_assert((int32_t)Direction::Output == AAUDIO_DIRECTION_OUTPUT, ERRMSG);
394     static_assert((int32_t)Direction::Input == AAUDIO_DIRECTION_INPUT, ERRMSG);
395 
396     static_assert((int32_t)AudioFormat::Invalid == AAUDIO_FORMAT_INVALID, ERRMSG);
397     static_assert((int32_t)AudioFormat::Unspecified == AAUDIO_FORMAT_UNSPECIFIED, ERRMSG);
398     static_assert((int32_t)AudioFormat::I16 == AAUDIO_FORMAT_PCM_I16, ERRMSG);
399     static_assert((int32_t)AudioFormat::Float == AAUDIO_FORMAT_PCM_FLOAT, ERRMSG);
400 
401     static_assert((int32_t)DataCallbackResult::Continue == AAUDIO_CALLBACK_RESULT_CONTINUE, ERRMSG);
402     static_assert((int32_t)DataCallbackResult::Stop == AAUDIO_CALLBACK_RESULT_STOP, ERRMSG);
403 
404     static_assert((int32_t)Result::OK == AAUDIO_OK, ERRMSG);
405     static_assert((int32_t)Result::ErrorBase == AAUDIO_ERROR_BASE, ERRMSG);
406     static_assert((int32_t)Result::ErrorDisconnected == AAUDIO_ERROR_DISCONNECTED, ERRMSG);
407     static_assert((int32_t)Result::ErrorIllegalArgument == AAUDIO_ERROR_ILLEGAL_ARGUMENT, ERRMSG);
408     static_assert((int32_t)Result::ErrorInternal == AAUDIO_ERROR_INTERNAL, ERRMSG);
409     static_assert((int32_t)Result::ErrorInvalidState == AAUDIO_ERROR_INVALID_STATE, ERRMSG);
410     static_assert((int32_t)Result::ErrorInvalidHandle == AAUDIO_ERROR_INVALID_HANDLE, ERRMSG);
411     static_assert((int32_t)Result::ErrorUnimplemented == AAUDIO_ERROR_UNIMPLEMENTED, ERRMSG);
412     static_assert((int32_t)Result::ErrorUnavailable == AAUDIO_ERROR_UNAVAILABLE, ERRMSG);
413     static_assert((int32_t)Result::ErrorNoFreeHandles == AAUDIO_ERROR_NO_FREE_HANDLES, ERRMSG);
414     static_assert((int32_t)Result::ErrorNoMemory == AAUDIO_ERROR_NO_MEMORY, ERRMSG);
415     static_assert((int32_t)Result::ErrorNull == AAUDIO_ERROR_NULL, ERRMSG);
416     static_assert((int32_t)Result::ErrorTimeout == AAUDIO_ERROR_TIMEOUT, ERRMSG);
417     static_assert((int32_t)Result::ErrorWouldBlock == AAUDIO_ERROR_WOULD_BLOCK, ERRMSG);
418     static_assert((int32_t)Result::ErrorInvalidFormat == AAUDIO_ERROR_INVALID_FORMAT, ERRMSG);
419     static_assert((int32_t)Result::ErrorOutOfRange == AAUDIO_ERROR_OUT_OF_RANGE, ERRMSG);
420     static_assert((int32_t)Result::ErrorNoService == AAUDIO_ERROR_NO_SERVICE, ERRMSG);
421     static_assert((int32_t)Result::ErrorInvalidRate == AAUDIO_ERROR_INVALID_RATE, ERRMSG);
422 
423     static_assert((int32_t)SharingMode::Exclusive == AAUDIO_SHARING_MODE_EXCLUSIVE, ERRMSG);
424     static_assert((int32_t)SharingMode::Shared == AAUDIO_SHARING_MODE_SHARED, ERRMSG);
425 
426     static_assert((int32_t)PerformanceMode::None == AAUDIO_PERFORMANCE_MODE_NONE, ERRMSG);
427     static_assert((int32_t)PerformanceMode::PowerSaving
428             == AAUDIO_PERFORMANCE_MODE_POWER_SAVING, ERRMSG);
429     static_assert((int32_t)PerformanceMode::LowLatency
430             == AAUDIO_PERFORMANCE_MODE_LOW_LATENCY, ERRMSG);
431 
432 // The aaudio_ usage, content and input_preset types were added in NDK 17,
433 // which is the first version to support Android Pie (API 28).
434 #if __NDK_MAJOR__ >= 17
435 
436     ASSERT_INT32(aaudio_usage_t);
437     ASSERT_INT32(aaudio_content_type_t);
438     ASSERT_INT32(aaudio_input_preset_t);
439 
440     static_assert((int32_t)Usage::Media == AAUDIO_USAGE_MEDIA, ERRMSG);
441     static_assert((int32_t)Usage::VoiceCommunication == AAUDIO_USAGE_VOICE_COMMUNICATION, ERRMSG);
442     static_assert((int32_t)Usage::VoiceCommunicationSignalling
443             == AAUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING, ERRMSG);
444     static_assert((int32_t)Usage::Alarm == AAUDIO_USAGE_ALARM, ERRMSG);
445     static_assert((int32_t)Usage::Notification == AAUDIO_USAGE_NOTIFICATION, ERRMSG);
446     static_assert((int32_t)Usage::NotificationRingtone == AAUDIO_USAGE_NOTIFICATION_RINGTONE, ERRMSG);
447     static_assert((int32_t)Usage::NotificationEvent == AAUDIO_USAGE_NOTIFICATION_EVENT, ERRMSG);
448     static_assert((int32_t)Usage::AssistanceAccessibility == AAUDIO_USAGE_ASSISTANCE_ACCESSIBILITY, ERRMSG);
449     static_assert((int32_t)Usage::AssistanceNavigationGuidance
450             == AAUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE, ERRMSG);
451     static_assert((int32_t)Usage::AssistanceSonification == AAUDIO_USAGE_ASSISTANCE_SONIFICATION, ERRMSG);
452     static_assert((int32_t)Usage::Game == AAUDIO_USAGE_GAME, ERRMSG);
453     static_assert((int32_t)Usage::Assistant == AAUDIO_USAGE_ASSISTANT, ERRMSG);
454 
455     static_assert((int32_t)ContentType::Speech == AAUDIO_CONTENT_TYPE_SPEECH, ERRMSG);
456     static_assert((int32_t)ContentType::Music == AAUDIO_CONTENT_TYPE_MUSIC, ERRMSG);
457     static_assert((int32_t)ContentType::Movie == AAUDIO_CONTENT_TYPE_MOVIE, ERRMSG);
458     static_assert((int32_t)ContentType::Sonification == AAUDIO_CONTENT_TYPE_SONIFICATION, ERRMSG);
459 
460     static_assert((int32_t)InputPreset::Generic == AAUDIO_INPUT_PRESET_GENERIC, ERRMSG);
461     static_assert((int32_t)InputPreset::Camcorder == AAUDIO_INPUT_PRESET_CAMCORDER, ERRMSG);
462     static_assert((int32_t)InputPreset::VoiceRecognition == AAUDIO_INPUT_PRESET_VOICE_RECOGNITION, ERRMSG);
463     static_assert((int32_t)InputPreset::VoiceCommunication
464             == AAUDIO_INPUT_PRESET_VOICE_COMMUNICATION, ERRMSG);
465     static_assert((int32_t)InputPreset::Unprocessed == AAUDIO_INPUT_PRESET_UNPROCESSED, ERRMSG);
466 
467     static_assert((int32_t)SessionId::None == AAUDIO_SESSION_ID_NONE, ERRMSG);
468     static_assert((int32_t)SessionId::Allocate == AAUDIO_SESSION_ID_ALLOCATE, ERRMSG);
469 
470 #endif // __NDK_MAJOR__ >= 17
471 
472 // aaudio_allowed_capture_policy_t was added in NDK 20,
473 // which is the first version to support Android Q (API 29).
474 #if __NDK_MAJOR__ >= 20
475 
476     ASSERT_INT32(aaudio_allowed_capture_policy_t);
477 
478     static_assert((int32_t)AllowedCapturePolicy::Unspecified == AAUDIO_UNSPECIFIED, ERRMSG);
479     static_assert((int32_t)AllowedCapturePolicy::All == AAUDIO_ALLOW_CAPTURE_BY_ALL, ERRMSG);
480     static_assert((int32_t)AllowedCapturePolicy::System == AAUDIO_ALLOW_CAPTURE_BY_SYSTEM, ERRMSG);
481     static_assert((int32_t)AllowedCapturePolicy::None == AAUDIO_ALLOW_CAPTURE_BY_NONE, ERRMSG);
482 
483 #endif // __NDK_MAJOR__ >= 20
484 
485 // The aaudio channel masks and spatialization behavior were added in NDK 24,
486 // which is the first version to support Android SC_V2 (API 32).
487 #if __NDK_MAJOR__ >= 24
488 
489     ASSERT_UINT32(aaudio_channel_mask_t);
490 
491     static_assert((uint32_t)ChannelMask::FrontLeft == AAUDIO_CHANNEL_FRONT_LEFT, ERRMSG);
492     static_assert((uint32_t)ChannelMask::FrontRight == AAUDIO_CHANNEL_FRONT_RIGHT, ERRMSG);
493     static_assert((uint32_t)ChannelMask::FrontCenter == AAUDIO_CHANNEL_FRONT_CENTER, ERRMSG);
494     static_assert((uint32_t)ChannelMask::LowFrequency == AAUDIO_CHANNEL_LOW_FREQUENCY, ERRMSG);
495     static_assert((uint32_t)ChannelMask::BackLeft == AAUDIO_CHANNEL_BACK_LEFT, ERRMSG);
496     static_assert((uint32_t)ChannelMask::BackRight == AAUDIO_CHANNEL_BACK_RIGHT, ERRMSG);
497     static_assert((uint32_t)ChannelMask::FrontLeftOfCenter == AAUDIO_CHANNEL_FRONT_LEFT_OF_CENTER, ERRMSG);
498     static_assert((uint32_t)ChannelMask::FrontRightOfCenter == AAUDIO_CHANNEL_FRONT_RIGHT_OF_CENTER, ERRMSG);
499     static_assert((uint32_t)ChannelMask::BackCenter == AAUDIO_CHANNEL_BACK_CENTER, ERRMSG);
500     static_assert((uint32_t)ChannelMask::SideLeft == AAUDIO_CHANNEL_SIDE_LEFT, ERRMSG);
501     static_assert((uint32_t)ChannelMask::SideRight == AAUDIO_CHANNEL_SIDE_RIGHT, ERRMSG);
502     static_assert((uint32_t)ChannelMask::TopCenter == AAUDIO_CHANNEL_TOP_CENTER, ERRMSG);
503     static_assert((uint32_t)ChannelMask::TopFrontLeft == AAUDIO_CHANNEL_TOP_FRONT_LEFT, ERRMSG);
504     static_assert((uint32_t)ChannelMask::TopFrontCenter == AAUDIO_CHANNEL_TOP_FRONT_CENTER, ERRMSG);
505     static_assert((uint32_t)ChannelMask::TopFrontRight == AAUDIO_CHANNEL_TOP_FRONT_RIGHT, ERRMSG);
506     static_assert((uint32_t)ChannelMask::TopBackLeft == AAUDIO_CHANNEL_TOP_BACK_LEFT, ERRMSG);
507     static_assert((uint32_t)ChannelMask::TopBackCenter == AAUDIO_CHANNEL_TOP_BACK_CENTER, ERRMSG);
508     static_assert((uint32_t)ChannelMask::TopBackRight == AAUDIO_CHANNEL_TOP_BACK_RIGHT, ERRMSG);
509     static_assert((uint32_t)ChannelMask::TopSideLeft == AAUDIO_CHANNEL_TOP_SIDE_LEFT, ERRMSG);
510     static_assert((uint32_t)ChannelMask::TopSideRight == AAUDIO_CHANNEL_TOP_SIDE_RIGHT, ERRMSG);
511     static_assert((uint32_t)ChannelMask::BottomFrontLeft == AAUDIO_CHANNEL_BOTTOM_FRONT_LEFT, ERRMSG);
512     static_assert((uint32_t)ChannelMask::BottomFrontCenter == AAUDIO_CHANNEL_BOTTOM_FRONT_CENTER, ERRMSG);
513     static_assert((uint32_t)ChannelMask::BottomFrontRight == AAUDIO_CHANNEL_BOTTOM_FRONT_RIGHT, ERRMSG);
514     static_assert((uint32_t)ChannelMask::LowFrequency2 == AAUDIO_CHANNEL_LOW_FREQUENCY_2, ERRMSG);
515     static_assert((uint32_t)ChannelMask::FrontWideLeft == AAUDIO_CHANNEL_FRONT_WIDE_LEFT, ERRMSG);
516     static_assert((uint32_t)ChannelMask::FrontWideRight == AAUDIO_CHANNEL_FRONT_WIDE_RIGHT, ERRMSG);
517     static_assert((uint32_t)ChannelMask::Mono == AAUDIO_CHANNEL_MONO, ERRMSG);
518     static_assert((uint32_t)ChannelMask::Stereo == AAUDIO_CHANNEL_STEREO, ERRMSG);
519     static_assert((uint32_t)ChannelMask::CM2Point1 == AAUDIO_CHANNEL_2POINT1, ERRMSG);
520     static_assert((uint32_t)ChannelMask::Tri == AAUDIO_CHANNEL_TRI, ERRMSG);
521     static_assert((uint32_t)ChannelMask::TriBack == AAUDIO_CHANNEL_TRI_BACK, ERRMSG);
522     static_assert((uint32_t)ChannelMask::CM3Point1 == AAUDIO_CHANNEL_3POINT1, ERRMSG);
523     static_assert((uint32_t)ChannelMask::CM2Point0Point2 == AAUDIO_CHANNEL_2POINT0POINT2, ERRMSG);
524     static_assert((uint32_t)ChannelMask::CM2Point1Point2 == AAUDIO_CHANNEL_2POINT1POINT2, ERRMSG);
525     static_assert((uint32_t)ChannelMask::CM3Point0Point2 == AAUDIO_CHANNEL_3POINT0POINT2, ERRMSG);
526     static_assert((uint32_t)ChannelMask::CM3Point1Point2 == AAUDIO_CHANNEL_3POINT1POINT2, ERRMSG);
527     static_assert((uint32_t)ChannelMask::Quad == AAUDIO_CHANNEL_QUAD, ERRMSG);
528     static_assert((uint32_t)ChannelMask::QuadSide == AAUDIO_CHANNEL_QUAD_SIDE, ERRMSG);
529     static_assert((uint32_t)ChannelMask::Surround == AAUDIO_CHANNEL_SURROUND, ERRMSG);
530     static_assert((uint32_t)ChannelMask::Penta == AAUDIO_CHANNEL_PENTA, ERRMSG);
531     static_assert((uint32_t)ChannelMask::CM5Point1 == AAUDIO_CHANNEL_5POINT1, ERRMSG);
532     static_assert((uint32_t)ChannelMask::CM5Point1Side == AAUDIO_CHANNEL_5POINT1_SIDE, ERRMSG);
533     static_assert((uint32_t)ChannelMask::CM6Point1 == AAUDIO_CHANNEL_6POINT1, ERRMSG);
534     static_assert((uint32_t)ChannelMask::CM7Point1 == AAUDIO_CHANNEL_7POINT1, ERRMSG);
535     static_assert((uint32_t)ChannelMask::CM5Point1Point2 == AAUDIO_CHANNEL_5POINT1POINT2, ERRMSG);
536     static_assert((uint32_t)ChannelMask::CM5Point1Point4 == AAUDIO_CHANNEL_5POINT1POINT4, ERRMSG);
537     static_assert((uint32_t)ChannelMask::CM7Point1Point2 == AAUDIO_CHANNEL_7POINT1POINT2, ERRMSG);
538     static_assert((uint32_t)ChannelMask::CM7Point1Point4 == AAUDIO_CHANNEL_7POINT1POINT4, ERRMSG);
539     static_assert((uint32_t)ChannelMask::CM9Point1Point4 == AAUDIO_CHANNEL_9POINT1POINT4, ERRMSG);
540     static_assert((uint32_t)ChannelMask::CM9Point1Point6 == AAUDIO_CHANNEL_9POINT1POINT6, ERRMSG);
541     static_assert((uint32_t)ChannelMask::FrontBack == AAUDIO_CHANNEL_FRONT_BACK, ERRMSG);
542 
543     ASSERT_INT32(aaudio_spatialization_behavior_t);
544 
545     static_assert((int32_t)SpatializationBehavior::Unspecified == AAUDIO_UNSPECIFIED, ERRMSG);
546     static_assert((int32_t)SpatializationBehavior::Auto == AAUDIO_SPATIALIZATION_BEHAVIOR_AUTO, ERRMSG);
547     static_assert((int32_t)SpatializationBehavior::Never == AAUDIO_SPATIALIZATION_BEHAVIOR_NEVER, ERRMSG);
548 
549 #endif
550 
551 // The aaudio device type and aaudio policy were added in NDK 28,
552 // which is the first version to support Android W (API 36).
553 #if __NDK_MAJOR__ >= 29
554 
555     ASSERT_INT32(AAudio_DeviceType);
556     static_assert((int32_t)DeviceType::BuiltinEarpiece == AAUDIO_DEVICE_BUILTIN_EARPIECE, ERRMSG);
557     static_assert((int32_t)DeviceType::BuiltinSpeaker == AAUDIO_DEVICE_BUILTIN_SPEAKER, ERRMSG);
558     static_assert((int32_t)DeviceType::WiredHeadset == AAUDIO_DEVICE_WIRED_HEADSET, ERRMSG);
559     static_assert((int32_t)DeviceType::WiredHeadphones == AAUDIO_DEVICE_WIRED_HEADPHONES, ERRMSG);
560     static_assert((int32_t)DeviceType::LineAnalog == AAUDIO_DEVICE_LINE_ANALOG, ERRMSG);
561     static_assert((int32_t)DeviceType::LineDigital == AAUDIO_DEVICE_LINE_DIGITAL, ERRMSG);
562     static_assert((int32_t)DeviceType::BluetoothSco == AAUDIO_DEVICE_BLUETOOTH_SCO, ERRMSG);
563     static_assert((int32_t)DeviceType::BluetoothA2dp == AAUDIO_DEVICE_BLUETOOTH_A2DP, ERRMSG);
564     static_assert((int32_t)DeviceType::Hdmi == AAUDIO_DEVICE_HDMI, ERRMSG);
565     static_assert((int32_t)DeviceType::HdmiArc == AAUDIO_DEVICE_HDMI_ARC, ERRMSG);
566     static_assert((int32_t)DeviceType::UsbDevice == AAUDIO_DEVICE_USB_DEVICE, ERRMSG);
567     static_assert((int32_t)DeviceType::UsbAccessory == AAUDIO_DEVICE_USB_ACCESSORY, ERRMSG);
568     static_assert((int32_t)DeviceType::Dock == AAUDIO_DEVICE_DOCK, ERRMSG);
569     static_assert((int32_t)DeviceType::FM == AAUDIO_DEVICE_FM, ERRMSG);
570     static_assert((int32_t)DeviceType::BuiltinMic == AAUDIO_DEVICE_BUILTIN_MIC, ERRMSG);
571     static_assert((int32_t)DeviceType::FMTuner == AAUDIO_DEVICE_FM_TUNER, ERRMSG);
572     static_assert((int32_t)DeviceType::TVTuner == AAUDIO_DEVICE_TV_TUNER, ERRMSG);
573     static_assert((int32_t)DeviceType::Telephony == AAUDIO_DEVICE_TELEPHONY, ERRMSG);
574     static_assert((int32_t)DeviceType::AuxLine == AAUDIO_DEVICE_AUX_LINE, ERRMSG);
575     static_assert((int32_t)DeviceType::IP == AAUDIO_DEVICE_IP, ERRMSG);
576     static_assert((int32_t)DeviceType::Bus == AAUDIO_DEVICE_BUS, ERRMSG);
577     static_assert((int32_t)DeviceType::UsbHeadset == AAUDIO_DEVICE_USB_HEADSET, ERRMSG);
578     static_assert((int32_t)DeviceType::HearingAid == AAUDIO_DEVICE_HEARING_AID, ERRMSG);
579     static_assert((int32_t)DeviceType::BuiltinSpeakerSafe == AAUDIO_DEVICE_BUILTIN_SPEAKER_SAFE, ERRMSG);
580     static_assert((int32_t)DeviceType::RemoteSubmix == AAUDIO_DEVICE_REMOTE_SUBMIX, ERRMSG);
581     static_assert((int32_t)DeviceType::BleHeadset == AAUDIO_DEVICE_BLE_HEADSET, ERRMSG);
582     static_assert((int32_t)DeviceType::BleSpeaker == AAUDIO_DEVICE_BLE_SPEAKER, ERRMSG);
583     static_assert((int32_t)DeviceType::HdmiEarc == AAUDIO_DEVICE_HDMI_EARC, ERRMSG);
584     static_assert((int32_t)DeviceType::BleBroadcast == AAUDIO_DEVICE_BLE_BROADCAST, ERRMSG);
585     static_assert((int32_t)DeviceType::DockAnalog == AAUDIO_DEVICE_DOCK_ANALOG, ERRMSG);
586 
587     ASSERT_INT32(aaudio_policy_t);
588     static_assert((int32_t)MMapPolicy::Unspecified == AAUDIO_UNSPECIFIED, ERRMSG);
589     static_assert((int32_t)MMapPolicy::Never == AAUDIO_POLICY_NEVER, ERRMSG);
590     static_assert((int32_t)MMapPolicy::Auto == AAUDIO_POLICY_AUTO, ERRMSG);
591     static_assert((int32_t)MMapPolicy::Always == AAUDIO_POLICY_ALWAYS, ERRMSG);
592 
593 #endif // __NDK_MAJOR__ >= 28
594 
595 #endif // AAUDIO_AAUDIO_H
596 
597 } // namespace oboe
598