xref: /aosp_15_r20/external/oboe/src/aaudio/AAudioLoader.cpp (revision 05767d913155b055644481607e6fa1e35e2fe72c)
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     builder_delete             = load_I_PB("AAudioStreamBuilder_delete");
106 
107 
108     builder_setDataCallback    = load_V_PBPDPV("AAudioStreamBuilder_setDataCallback");
109     builder_setErrorCallback   = load_V_PBPEPV("AAudioStreamBuilder_setErrorCallback");
110 
111     stream_read                = load_I_PSPVIL("AAudioStream_read");
112 
113     stream_write               = load_I_PSCPVIL("AAudioStream_write");
114 
115     stream_waitForStateChange  = load_I_PSTPTL("AAudioStream_waitForStateChange");
116 
117     stream_getTimestamp        = load_I_PSKPLPL("AAudioStream_getTimestamp");
118 
119     stream_getChannelCount     = load_I_PS("AAudioStream_getChannelCount");
120     if (stream_getChannelCount == nullptr) {
121         // Use old alias if needed.
122         stream_getChannelCount    = load_I_PS("AAudioStream_getSamplesPerFrame");
123     }
124 
125     if (getSdkVersion() >= __ANDROID_API_R__) {
126         stream_release         = load_I_PS("AAudioStream_release");
127     }
128 
129     stream_close               = load_I_PS("AAudioStream_close");
130 
131     stream_getBufferSize       = load_I_PS("AAudioStream_getBufferSizeInFrames");
132     stream_getDeviceId         = load_I_PS("AAudioStream_getDeviceId");
133     stream_getBufferCapacity   = load_I_PS("AAudioStream_getBufferCapacityInFrames");
134     stream_getFormat           = load_F_PS("AAudioStream_getFormat");
135     stream_getFramesPerBurst   = load_I_PS("AAudioStream_getFramesPerBurst");
136     stream_getFramesRead       = load_L_PS("AAudioStream_getFramesRead");
137     stream_getFramesWritten    = load_L_PS("AAudioStream_getFramesWritten");
138     stream_getPerformanceMode  = load_I_PS("AAudioStream_getPerformanceMode");
139     stream_getSampleRate       = load_I_PS("AAudioStream_getSampleRate");
140     stream_getSharingMode      = load_I_PS("AAudioStream_getSharingMode");
141     stream_getState            = load_I_PS("AAudioStream_getState");
142     stream_getXRunCount        = load_I_PS("AAudioStream_getXRunCount");
143 
144     stream_requestStart        = load_I_PS("AAudioStream_requestStart");
145     stream_requestPause        = load_I_PS("AAudioStream_requestPause");
146     stream_requestFlush        = load_I_PS("AAudioStream_requestFlush");
147     stream_requestStop         = load_I_PS("AAudioStream_requestStop");
148 
149     stream_setBufferSize       = load_I_PSI("AAudioStream_setBufferSizeInFrames");
150 
151     convertResultToText        = load_CPH_I("AAudio_convertResultToText");
152 
153     if (getSdkVersion() >= __ANDROID_API_P__){
154         stream_getUsage        = load_I_PS("AAudioStream_getUsage");
155         stream_getContentType  = load_I_PS("AAudioStream_getContentType");
156         stream_getInputPreset  = load_I_PS("AAudioStream_getInputPreset");
157         stream_getSessionId    = load_I_PS("AAudioStream_getSessionId");
158     }
159 
160     if (getSdkVersion() >= __ANDROID_API_Q__){
161         stream_getAllowedCapturePolicy    = load_I_PS("AAudioStream_getAllowedCapturePolicy");
162     }
163 
164     if (getSdkVersion() >= __ANDROID_API_R__){
165         stream_isPrivacySensitive  = load_O_PS("AAudioStream_isPrivacySensitive");
166     }
167 
168     if (getSdkVersion() >= __ANDROID_API_S_V2__) {
169         stream_getChannelMask = load_U_PS("AAudioStream_getChannelMask");
170         stream_isContentSpatialized = load_O_PS("AAudioStream_isContentSpatialized");
171         stream_getSpatializationBehavior = load_I_PS("AAudioStream_getSpatializationBehavior");
172     }
173 
174     if (getSdkVersion() >= __ANDROID_API_U__) {
175         stream_getHardwareChannelCount = load_I_PS("AAudioStream_getHardwareChannelCount");
176         stream_getHardwareSampleRate = load_I_PS("AAudioStream_getHardwareSampleRate");
177         stream_getHardwareFormat = load_F_PS("AAudioStream_getHardwareFormat");
178     }
179 
180     return 0;
181 }
182 
AAudioLoader_check(void * proc,const char * functionName)183 static void AAudioLoader_check(void *proc, const char *functionName) {
184     if (proc == nullptr) {
185         LOGW("AAudioLoader could not find %s", functionName);
186     }
187 }
188 
load_I_PPB(const char * functionName)189 AAudioLoader::signature_I_PPB AAudioLoader::load_I_PPB(const char *functionName) {
190     void *proc = dlsym(mLibHandle, functionName);
191     AAudioLoader_check(proc, functionName);
192     return reinterpret_cast<signature_I_PPB>(proc);
193 }
194 
load_CPH_I(const char * functionName)195 AAudioLoader::signature_CPH_I AAudioLoader::load_CPH_I(const char *functionName) {
196     void *proc = dlsym(mLibHandle, functionName);
197     AAudioLoader_check(proc, functionName);
198     return reinterpret_cast<signature_CPH_I>(proc);
199 }
200 
load_V_PBI(const char * functionName)201 AAudioLoader::signature_V_PBI AAudioLoader::load_V_PBI(const char *functionName) {
202     void *proc = dlsym(mLibHandle, functionName);
203     AAudioLoader_check(proc, functionName);
204     return reinterpret_cast<signature_V_PBI>(proc);
205 }
206 
load_V_PBCPH(const char * functionName)207 AAudioLoader::signature_V_PBCPH AAudioLoader::load_V_PBCPH(const char *functionName) {
208     void *proc = dlsym(mLibHandle, functionName);
209     AAudioLoader_check(proc, functionName);
210     return reinterpret_cast<signature_V_PBCPH>(proc);
211 }
212 
load_V_PBPDPV(const char * functionName)213 AAudioLoader::signature_V_PBPDPV AAudioLoader::load_V_PBPDPV(const char *functionName) {
214     void *proc = dlsym(mLibHandle, functionName);
215     AAudioLoader_check(proc, functionName);
216     return reinterpret_cast<signature_V_PBPDPV>(proc);
217 }
218 
load_V_PBPEPV(const char * functionName)219 AAudioLoader::signature_V_PBPEPV AAudioLoader::load_V_PBPEPV(const char *functionName) {
220     void *proc = dlsym(mLibHandle, functionName);
221     AAudioLoader_check(proc, functionName);
222     return reinterpret_cast<signature_V_PBPEPV>(proc);
223 }
224 
load_I_PSI(const char * functionName)225 AAudioLoader::signature_I_PSI AAudioLoader::load_I_PSI(const char *functionName) {
226     void *proc = dlsym(mLibHandle, functionName);
227     AAudioLoader_check(proc, functionName);
228     return reinterpret_cast<signature_I_PSI>(proc);
229 }
230 
load_I_PS(const char * functionName)231 AAudioLoader::signature_I_PS AAudioLoader::load_I_PS(const char *functionName) {
232     void *proc = dlsym(mLibHandle, functionName);
233     AAudioLoader_check(proc, functionName);
234     return reinterpret_cast<signature_I_PS>(proc);
235 }
236 
load_L_PS(const char * functionName)237 AAudioLoader::signature_L_PS AAudioLoader::load_L_PS(const char *functionName) {
238     void *proc = dlsym(mLibHandle, functionName);
239     AAudioLoader_check(proc, functionName);
240     return reinterpret_cast<signature_L_PS>(proc);
241 }
242 
load_F_PS(const char * functionName)243 AAudioLoader::signature_F_PS AAudioLoader::load_F_PS(const char *functionName) {
244     void *proc = dlsym(mLibHandle, functionName);
245     AAudioLoader_check(proc, functionName);
246     return reinterpret_cast<signature_F_PS>(proc);
247 }
248 
load_O_PS(const char * functionName)249 AAudioLoader::signature_O_PS AAudioLoader::load_O_PS(const char *functionName) {
250     void *proc = dlsym(mLibHandle, functionName);
251     AAudioLoader_check(proc, functionName);
252     return reinterpret_cast<signature_O_PS>(proc);
253 }
254 
load_I_PB(const char * functionName)255 AAudioLoader::signature_I_PB AAudioLoader::load_I_PB(const char *functionName) {
256     void *proc = dlsym(mLibHandle, functionName);
257     AAudioLoader_check(proc, functionName);
258     return reinterpret_cast<signature_I_PB>(proc);
259 }
260 
load_I_PBPPS(const char * functionName)261 AAudioLoader::signature_I_PBPPS AAudioLoader::load_I_PBPPS(const char *functionName) {
262     void *proc = dlsym(mLibHandle, functionName);
263     AAudioLoader_check(proc, functionName);
264     return reinterpret_cast<signature_I_PBPPS>(proc);
265 }
266 
load_I_PSCPVIL(const char * functionName)267 AAudioLoader::signature_I_PSCPVIL AAudioLoader::load_I_PSCPVIL(const char *functionName) {
268     void *proc = dlsym(mLibHandle, functionName);
269     AAudioLoader_check(proc, functionName);
270     return reinterpret_cast<signature_I_PSCPVIL>(proc);
271 }
272 
load_I_PSPVIL(const char * functionName)273 AAudioLoader::signature_I_PSPVIL AAudioLoader::load_I_PSPVIL(const char *functionName) {
274     void *proc = dlsym(mLibHandle, functionName);
275     AAudioLoader_check(proc, functionName);
276     return reinterpret_cast<signature_I_PSPVIL>(proc);
277 }
278 
load_I_PSTPTL(const char * functionName)279 AAudioLoader::signature_I_PSTPTL AAudioLoader::load_I_PSTPTL(const char *functionName) {
280     void *proc = dlsym(mLibHandle, functionName);
281     AAudioLoader_check(proc, functionName);
282     return reinterpret_cast<signature_I_PSTPTL>(proc);
283 }
284 
load_I_PSKPLPL(const char * functionName)285 AAudioLoader::signature_I_PSKPLPL AAudioLoader::load_I_PSKPLPL(const char *functionName) {
286     void *proc = dlsym(mLibHandle, functionName);
287     AAudioLoader_check(proc, functionName);
288     return reinterpret_cast<signature_I_PSKPLPL>(proc);
289 }
290 
load_V_PBU(const char * functionName)291 AAudioLoader::signature_V_PBU AAudioLoader::load_V_PBU(const char *functionName) {
292     void *proc = dlsym(mLibHandle, functionName);
293     AAudioLoader_check(proc, functionName);
294     return reinterpret_cast<signature_V_PBU>(proc);
295 }
296 
load_U_PS(const char * functionName)297 AAudioLoader::signature_U_PS AAudioLoader::load_U_PS(const char *functionName) {
298     void *proc = dlsym(mLibHandle, functionName);
299     AAudioLoader_check(proc, functionName);
300     return reinterpret_cast<signature_U_PS>(proc);
301 }
302 
load_V_PBO(const char * functionName)303 AAudioLoader::signature_V_PBO AAudioLoader::load_V_PBO(const char *functionName) {
304     void *proc = dlsym(mLibHandle, functionName);
305     AAudioLoader_check(proc, functionName);
306     return reinterpret_cast<signature_V_PBO>(proc);
307 }
308 
309 // Ensure that all AAudio primitive data types are int32_t
310 #define ASSERT_INT32(type) static_assert(std::is_same<int32_t, type>::value, \
311 #type" must be int32_t")
312 
313 // Ensure that all AAudio primitive data types are uint32_t
314 #define ASSERT_UINT32(type) static_assert(std::is_same<uint32_t, type>::value, \
315 #type" must be uint32_t")
316 
317 #define ERRMSG "Oboe constants must match AAudio constants."
318 
319 // These asserts help verify that the Oboe definitions match the equivalent AAudio definitions.
320 // This code is in this .cpp file so it only gets tested once.
321 #ifdef AAUDIO_AAUDIO_H
322 
323     ASSERT_INT32(aaudio_stream_state_t);
324     ASSERT_INT32(aaudio_direction_t);
325     ASSERT_INT32(aaudio_format_t);
326     ASSERT_INT32(aaudio_data_callback_result_t);
327     ASSERT_INT32(aaudio_result_t);
328     ASSERT_INT32(aaudio_sharing_mode_t);
329     ASSERT_INT32(aaudio_performance_mode_t);
330 
331     static_assert((int32_t)StreamState::Uninitialized == AAUDIO_STREAM_STATE_UNINITIALIZED, ERRMSG);
332     static_assert((int32_t)StreamState::Unknown == AAUDIO_STREAM_STATE_UNKNOWN, ERRMSG);
333     static_assert((int32_t)StreamState::Open == AAUDIO_STREAM_STATE_OPEN, ERRMSG);
334     static_assert((int32_t)StreamState::Starting == AAUDIO_STREAM_STATE_STARTING, ERRMSG);
335     static_assert((int32_t)StreamState::Started == AAUDIO_STREAM_STATE_STARTED, ERRMSG);
336     static_assert((int32_t)StreamState::Pausing == AAUDIO_STREAM_STATE_PAUSING, ERRMSG);
337     static_assert((int32_t)StreamState::Paused == AAUDIO_STREAM_STATE_PAUSED, ERRMSG);
338     static_assert((int32_t)StreamState::Flushing == AAUDIO_STREAM_STATE_FLUSHING, ERRMSG);
339     static_assert((int32_t)StreamState::Flushed == AAUDIO_STREAM_STATE_FLUSHED, ERRMSG);
340     static_assert((int32_t)StreamState::Stopping == AAUDIO_STREAM_STATE_STOPPING, ERRMSG);
341     static_assert((int32_t)StreamState::Stopped == AAUDIO_STREAM_STATE_STOPPED, ERRMSG);
342     static_assert((int32_t)StreamState::Closing == AAUDIO_STREAM_STATE_CLOSING, ERRMSG);
343     static_assert((int32_t)StreamState::Closed == AAUDIO_STREAM_STATE_CLOSED, ERRMSG);
344     static_assert((int32_t)StreamState::Disconnected == AAUDIO_STREAM_STATE_DISCONNECTED, ERRMSG);
345 
346     static_assert((int32_t)Direction::Output == AAUDIO_DIRECTION_OUTPUT, ERRMSG);
347     static_assert((int32_t)Direction::Input == AAUDIO_DIRECTION_INPUT, ERRMSG);
348 
349     static_assert((int32_t)AudioFormat::Invalid == AAUDIO_FORMAT_INVALID, ERRMSG);
350     static_assert((int32_t)AudioFormat::Unspecified == AAUDIO_FORMAT_UNSPECIFIED, ERRMSG);
351     static_assert((int32_t)AudioFormat::I16 == AAUDIO_FORMAT_PCM_I16, ERRMSG);
352     static_assert((int32_t)AudioFormat::Float == AAUDIO_FORMAT_PCM_FLOAT, ERRMSG);
353 
354     static_assert((int32_t)DataCallbackResult::Continue == AAUDIO_CALLBACK_RESULT_CONTINUE, ERRMSG);
355     static_assert((int32_t)DataCallbackResult::Stop == AAUDIO_CALLBACK_RESULT_STOP, ERRMSG);
356 
357     static_assert((int32_t)Result::OK == AAUDIO_OK, ERRMSG);
358     static_assert((int32_t)Result::ErrorBase == AAUDIO_ERROR_BASE, ERRMSG);
359     static_assert((int32_t)Result::ErrorDisconnected == AAUDIO_ERROR_DISCONNECTED, ERRMSG);
360     static_assert((int32_t)Result::ErrorIllegalArgument == AAUDIO_ERROR_ILLEGAL_ARGUMENT, ERRMSG);
361     static_assert((int32_t)Result::ErrorInternal == AAUDIO_ERROR_INTERNAL, ERRMSG);
362     static_assert((int32_t)Result::ErrorInvalidState == AAUDIO_ERROR_INVALID_STATE, ERRMSG);
363     static_assert((int32_t)Result::ErrorInvalidHandle == AAUDIO_ERROR_INVALID_HANDLE, ERRMSG);
364     static_assert((int32_t)Result::ErrorUnimplemented == AAUDIO_ERROR_UNIMPLEMENTED, ERRMSG);
365     static_assert((int32_t)Result::ErrorUnavailable == AAUDIO_ERROR_UNAVAILABLE, ERRMSG);
366     static_assert((int32_t)Result::ErrorNoFreeHandles == AAUDIO_ERROR_NO_FREE_HANDLES, ERRMSG);
367     static_assert((int32_t)Result::ErrorNoMemory == AAUDIO_ERROR_NO_MEMORY, ERRMSG);
368     static_assert((int32_t)Result::ErrorNull == AAUDIO_ERROR_NULL, ERRMSG);
369     static_assert((int32_t)Result::ErrorTimeout == AAUDIO_ERROR_TIMEOUT, ERRMSG);
370     static_assert((int32_t)Result::ErrorWouldBlock == AAUDIO_ERROR_WOULD_BLOCK, ERRMSG);
371     static_assert((int32_t)Result::ErrorInvalidFormat == AAUDIO_ERROR_INVALID_FORMAT, ERRMSG);
372     static_assert((int32_t)Result::ErrorOutOfRange == AAUDIO_ERROR_OUT_OF_RANGE, ERRMSG);
373     static_assert((int32_t)Result::ErrorNoService == AAUDIO_ERROR_NO_SERVICE, ERRMSG);
374     static_assert((int32_t)Result::ErrorInvalidRate == AAUDIO_ERROR_INVALID_RATE, ERRMSG);
375 
376     static_assert((int32_t)SharingMode::Exclusive == AAUDIO_SHARING_MODE_EXCLUSIVE, ERRMSG);
377     static_assert((int32_t)SharingMode::Shared == AAUDIO_SHARING_MODE_SHARED, ERRMSG);
378 
379     static_assert((int32_t)PerformanceMode::None == AAUDIO_PERFORMANCE_MODE_NONE, ERRMSG);
380     static_assert((int32_t)PerformanceMode::PowerSaving
381             == AAUDIO_PERFORMANCE_MODE_POWER_SAVING, ERRMSG);
382     static_assert((int32_t)PerformanceMode::LowLatency
383             == AAUDIO_PERFORMANCE_MODE_LOW_LATENCY, ERRMSG);
384 
385 // The aaudio_ usage, content and input_preset types were added in NDK 17,
386 // which is the first version to support Android Pie (API 28).
387 #if __NDK_MAJOR__ >= 17
388 
389     ASSERT_INT32(aaudio_usage_t);
390     ASSERT_INT32(aaudio_content_type_t);
391     ASSERT_INT32(aaudio_input_preset_t);
392 
393     static_assert((int32_t)Usage::Media == AAUDIO_USAGE_MEDIA, ERRMSG);
394     static_assert((int32_t)Usage::VoiceCommunication == AAUDIO_USAGE_VOICE_COMMUNICATION, ERRMSG);
395     static_assert((int32_t)Usage::VoiceCommunicationSignalling
396             == AAUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING, ERRMSG);
397     static_assert((int32_t)Usage::Alarm == AAUDIO_USAGE_ALARM, ERRMSG);
398     static_assert((int32_t)Usage::Notification == AAUDIO_USAGE_NOTIFICATION, ERRMSG);
399     static_assert((int32_t)Usage::NotificationRingtone == AAUDIO_USAGE_NOTIFICATION_RINGTONE, ERRMSG);
400     static_assert((int32_t)Usage::NotificationEvent == AAUDIO_USAGE_NOTIFICATION_EVENT, ERRMSG);
401     static_assert((int32_t)Usage::AssistanceAccessibility == AAUDIO_USAGE_ASSISTANCE_ACCESSIBILITY, ERRMSG);
402     static_assert((int32_t)Usage::AssistanceNavigationGuidance
403             == AAUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE, ERRMSG);
404     static_assert((int32_t)Usage::AssistanceSonification == AAUDIO_USAGE_ASSISTANCE_SONIFICATION, ERRMSG);
405     static_assert((int32_t)Usage::Game == AAUDIO_USAGE_GAME, ERRMSG);
406     static_assert((int32_t)Usage::Assistant == AAUDIO_USAGE_ASSISTANT, ERRMSG);
407 
408     static_assert((int32_t)ContentType::Speech == AAUDIO_CONTENT_TYPE_SPEECH, ERRMSG);
409     static_assert((int32_t)ContentType::Music == AAUDIO_CONTENT_TYPE_MUSIC, ERRMSG);
410     static_assert((int32_t)ContentType::Movie == AAUDIO_CONTENT_TYPE_MOVIE, ERRMSG);
411     static_assert((int32_t)ContentType::Sonification == AAUDIO_CONTENT_TYPE_SONIFICATION, ERRMSG);
412 
413     static_assert((int32_t)InputPreset::Generic == AAUDIO_INPUT_PRESET_GENERIC, ERRMSG);
414     static_assert((int32_t)InputPreset::Camcorder == AAUDIO_INPUT_PRESET_CAMCORDER, ERRMSG);
415     static_assert((int32_t)InputPreset::VoiceRecognition == AAUDIO_INPUT_PRESET_VOICE_RECOGNITION, ERRMSG);
416     static_assert((int32_t)InputPreset::VoiceCommunication
417             == AAUDIO_INPUT_PRESET_VOICE_COMMUNICATION, ERRMSG);
418     static_assert((int32_t)InputPreset::Unprocessed == AAUDIO_INPUT_PRESET_UNPROCESSED, ERRMSG);
419 
420     static_assert((int32_t)SessionId::None == AAUDIO_SESSION_ID_NONE, ERRMSG);
421     static_assert((int32_t)SessionId::Allocate == AAUDIO_SESSION_ID_ALLOCATE, ERRMSG);
422 
423 #endif // __NDK_MAJOR__ >= 17
424 
425 // aaudio_allowed_capture_policy_t was added in NDK 20,
426 // which is the first version to support Android Q (API 29).
427 #if __NDK_MAJOR__ >= 20
428 
429     ASSERT_INT32(aaudio_allowed_capture_policy_t);
430 
431     static_assert((int32_t)AllowedCapturePolicy::Unspecified == AAUDIO_UNSPECIFIED, ERRMSG);
432     static_assert((int32_t)AllowedCapturePolicy::All == AAUDIO_ALLOW_CAPTURE_BY_ALL, ERRMSG);
433     static_assert((int32_t)AllowedCapturePolicy::System == AAUDIO_ALLOW_CAPTURE_BY_SYSTEM, ERRMSG);
434     static_assert((int32_t)AllowedCapturePolicy::None == AAUDIO_ALLOW_CAPTURE_BY_NONE, ERRMSG);
435 
436 #endif // __NDK_MAJOR__ >= 20
437 
438 // The aaudio channel masks and spatialization behavior were added in NDK 24,
439 // which is the first version to support Android SC_V2 (API 32).
440 #if __NDK_MAJOR__ >= 24
441 
442     ASSERT_UINT32(aaudio_channel_mask_t);
443 
444     static_assert((uint32_t)ChannelMask::FrontLeft == AAUDIO_CHANNEL_FRONT_LEFT, ERRMSG);
445     static_assert((uint32_t)ChannelMask::FrontRight == AAUDIO_CHANNEL_FRONT_RIGHT, ERRMSG);
446     static_assert((uint32_t)ChannelMask::FrontCenter == AAUDIO_CHANNEL_FRONT_CENTER, ERRMSG);
447     static_assert((uint32_t)ChannelMask::LowFrequency == AAUDIO_CHANNEL_LOW_FREQUENCY, ERRMSG);
448     static_assert((uint32_t)ChannelMask::BackLeft == AAUDIO_CHANNEL_BACK_LEFT, ERRMSG);
449     static_assert((uint32_t)ChannelMask::BackRight == AAUDIO_CHANNEL_BACK_RIGHT, ERRMSG);
450     static_assert((uint32_t)ChannelMask::FrontLeftOfCenter == AAUDIO_CHANNEL_FRONT_LEFT_OF_CENTER, ERRMSG);
451     static_assert((uint32_t)ChannelMask::FrontRightOfCenter == AAUDIO_CHANNEL_FRONT_RIGHT_OF_CENTER, ERRMSG);
452     static_assert((uint32_t)ChannelMask::BackCenter == AAUDIO_CHANNEL_BACK_CENTER, ERRMSG);
453     static_assert((uint32_t)ChannelMask::SideLeft == AAUDIO_CHANNEL_SIDE_LEFT, ERRMSG);
454     static_assert((uint32_t)ChannelMask::SideRight == AAUDIO_CHANNEL_SIDE_RIGHT, ERRMSG);
455     static_assert((uint32_t)ChannelMask::TopCenter == AAUDIO_CHANNEL_TOP_CENTER, ERRMSG);
456     static_assert((uint32_t)ChannelMask::TopFrontLeft == AAUDIO_CHANNEL_TOP_FRONT_LEFT, ERRMSG);
457     static_assert((uint32_t)ChannelMask::TopFrontCenter == AAUDIO_CHANNEL_TOP_FRONT_CENTER, ERRMSG);
458     static_assert((uint32_t)ChannelMask::TopFrontRight == AAUDIO_CHANNEL_TOP_FRONT_RIGHT, ERRMSG);
459     static_assert((uint32_t)ChannelMask::TopBackLeft == AAUDIO_CHANNEL_TOP_BACK_LEFT, ERRMSG);
460     static_assert((uint32_t)ChannelMask::TopBackCenter == AAUDIO_CHANNEL_TOP_BACK_CENTER, ERRMSG);
461     static_assert((uint32_t)ChannelMask::TopBackRight == AAUDIO_CHANNEL_TOP_BACK_RIGHT, ERRMSG);
462     static_assert((uint32_t)ChannelMask::TopSideLeft == AAUDIO_CHANNEL_TOP_SIDE_LEFT, ERRMSG);
463     static_assert((uint32_t)ChannelMask::TopSideRight == AAUDIO_CHANNEL_TOP_SIDE_RIGHT, ERRMSG);
464     static_assert((uint32_t)ChannelMask::BottomFrontLeft == AAUDIO_CHANNEL_BOTTOM_FRONT_LEFT, ERRMSG);
465     static_assert((uint32_t)ChannelMask::BottomFrontCenter == AAUDIO_CHANNEL_BOTTOM_FRONT_CENTER, ERRMSG);
466     static_assert((uint32_t)ChannelMask::BottomFrontRight == AAUDIO_CHANNEL_BOTTOM_FRONT_RIGHT, ERRMSG);
467     static_assert((uint32_t)ChannelMask::LowFrequency2 == AAUDIO_CHANNEL_LOW_FREQUENCY_2, ERRMSG);
468     static_assert((uint32_t)ChannelMask::FrontWideLeft == AAUDIO_CHANNEL_FRONT_WIDE_LEFT, ERRMSG);
469     static_assert((uint32_t)ChannelMask::FrontWideRight == AAUDIO_CHANNEL_FRONT_WIDE_RIGHT, ERRMSG);
470     static_assert((uint32_t)ChannelMask::Mono == AAUDIO_CHANNEL_MONO, ERRMSG);
471     static_assert((uint32_t)ChannelMask::Stereo == AAUDIO_CHANNEL_STEREO, ERRMSG);
472     static_assert((uint32_t)ChannelMask::CM2Point1 == AAUDIO_CHANNEL_2POINT1, ERRMSG);
473     static_assert((uint32_t)ChannelMask::Tri == AAUDIO_CHANNEL_TRI, ERRMSG);
474     static_assert((uint32_t)ChannelMask::TriBack == AAUDIO_CHANNEL_TRI_BACK, ERRMSG);
475     static_assert((uint32_t)ChannelMask::CM3Point1 == AAUDIO_CHANNEL_3POINT1, ERRMSG);
476     static_assert((uint32_t)ChannelMask::CM2Point0Point2 == AAUDIO_CHANNEL_2POINT0POINT2, ERRMSG);
477     static_assert((uint32_t)ChannelMask::CM2Point1Point2 == AAUDIO_CHANNEL_2POINT1POINT2, ERRMSG);
478     static_assert((uint32_t)ChannelMask::CM3Point0Point2 == AAUDIO_CHANNEL_3POINT0POINT2, ERRMSG);
479     static_assert((uint32_t)ChannelMask::CM3Point1Point2 == AAUDIO_CHANNEL_3POINT1POINT2, ERRMSG);
480     static_assert((uint32_t)ChannelMask::Quad == AAUDIO_CHANNEL_QUAD, ERRMSG);
481     static_assert((uint32_t)ChannelMask::QuadSide == AAUDIO_CHANNEL_QUAD_SIDE, ERRMSG);
482     static_assert((uint32_t)ChannelMask::Surround == AAUDIO_CHANNEL_SURROUND, ERRMSG);
483     static_assert((uint32_t)ChannelMask::Penta == AAUDIO_CHANNEL_PENTA, ERRMSG);
484     static_assert((uint32_t)ChannelMask::CM5Point1 == AAUDIO_CHANNEL_5POINT1, ERRMSG);
485     static_assert((uint32_t)ChannelMask::CM5Point1Side == AAUDIO_CHANNEL_5POINT1_SIDE, ERRMSG);
486     static_assert((uint32_t)ChannelMask::CM6Point1 == AAUDIO_CHANNEL_6POINT1, ERRMSG);
487     static_assert((uint32_t)ChannelMask::CM7Point1 == AAUDIO_CHANNEL_7POINT1, ERRMSG);
488     static_assert((uint32_t)ChannelMask::CM5Point1Point2 == AAUDIO_CHANNEL_5POINT1POINT2, ERRMSG);
489     static_assert((uint32_t)ChannelMask::CM5Point1Point4 == AAUDIO_CHANNEL_5POINT1POINT4, ERRMSG);
490     static_assert((uint32_t)ChannelMask::CM7Point1Point2 == AAUDIO_CHANNEL_7POINT1POINT2, ERRMSG);
491     static_assert((uint32_t)ChannelMask::CM7Point1Point4 == AAUDIO_CHANNEL_7POINT1POINT4, ERRMSG);
492     static_assert((uint32_t)ChannelMask::CM9Point1Point4 == AAUDIO_CHANNEL_9POINT1POINT4, ERRMSG);
493     static_assert((uint32_t)ChannelMask::CM9Point1Point6 == AAUDIO_CHANNEL_9POINT1POINT6, ERRMSG);
494     static_assert((uint32_t)ChannelMask::FrontBack == AAUDIO_CHANNEL_FRONT_BACK, ERRMSG);
495 
496     ASSERT_INT32(aaudio_spatialization_behavior_t);
497 
498     static_assert((int32_t)SpatializationBehavior::Unspecified == AAUDIO_UNSPECIFIED, ERRMSG);
499     static_assert((int32_t)SpatializationBehavior::Auto == AAUDIO_SPATIALIZATION_BEHAVIOR_AUTO, ERRMSG);
500     static_assert((int32_t)SpatializationBehavior::Never == AAUDIO_SPATIALIZATION_BEHAVIOR_NEVER, ERRMSG);
501 
502 #endif
503 
504 #endif // AAUDIO_AAUDIO_H
505 
506 } // namespace oboe
507