• 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 #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 #ifndef __NDK_MAJOR__
66 #define __NDK_MAJOR__ 0
67 #endif
68 
69 namespace oboe {
70 
71 /**
72  * The AAudio API was not available in early versions of Android.
73  * To avoid linker errors, we dynamically link with the functions by name using dlsym().
74  * On older versions this linkage will safely fail.
75  */
76 class AAudioLoader {
77   public:
78     // Use signatures for common functions.
79     // Key to letter abbreviations.
80     // S = Stream
81     // B = Builder
82     // I = int32_t
83     // L = int64_t
84     // T = sTate
85     // K = clocKid_t
86     // P = Pointer to following data type
87     // C = Const prefix
88     // H = cHar
89     typedef int32_t  (*signature_I_PPB)(AAudioStreamBuilder **builder);
90 
91     typedef const char * (*signature_CPH_I)(int32_t);
92 
93     typedef int32_t (*signature_I_PBPPS)(AAudioStreamBuilder *,
94                                       AAudioStream **stream);  // AAudioStreamBuilder_open()
95 
96     typedef int32_t (*signature_I_PB)(AAudioStreamBuilder *);  // AAudioStreamBuilder_delete()
97     // AAudioStreamBuilder_setSampleRate()
98     typedef void    (*signature_V_PBI)(AAudioStreamBuilder *, int32_t);
99 
100     typedef int32_t (*signature_I_PS)(AAudioStream *);  // AAudioStream_getSampleRate()
101     typedef int64_t (*signature_L_PS)(AAudioStream *);  // AAudioStream_getFramesRead()
102     // AAudioStream_setBufferSizeInFrames()
103     typedef int32_t (*signature_I_PSI)(AAudioStream *, int32_t);
104 
105     typedef void    (*signature_V_PBPDPV)(AAudioStreamBuilder *,
106                                           AAudioStream_dataCallback,
107                                           void *);
108 
109     typedef void    (*signature_V_PBPEPV)(AAudioStreamBuilder *,
110                                           AAudioStream_errorCallback,
111                                           void *);
112 
113     typedef aaudio_format_t (*signature_F_PS)(AAudioStream *stream);
114 
115     typedef int32_t (*signature_I_PSPVIL)(AAudioStream *, void *, int32_t, int64_t);
116     typedef int32_t (*signature_I_PSCPVIL)(AAudioStream *, const void *, int32_t, int64_t);
117 
118     typedef int32_t (*signature_I_PSTPTL)(AAudioStream *,
119                                           aaudio_stream_state_t,
120                                           aaudio_stream_state_t *,
121                                           int64_t);
122 
123     typedef int32_t (*signature_I_PSKPLPL)(AAudioStream *, clockid_t, int64_t *, int64_t *);
124 
125     typedef bool    (*signature_B_PS)(AAudioStream *);
126 
127     static AAudioLoader* getInstance(); // singleton
128 
129     /**
130      * Open the AAudio shared library and load the function pointers.
131      * This can be called multiple times.
132      * It should only be called from one thread.
133      *
134      * The destructor will clean up after the open.
135      *
136      * @return 0 if successful or negative error.
137      */
138     int open();
139 
getLibHandle()140     void *getLibHandle() const { return mLibHandle; }
141 
142     // Function pointers into the AAudio shared library.
143     signature_I_PPB   createStreamBuilder = nullptr;
144 
145     signature_I_PBPPS builder_openStream = nullptr;
146 
147     signature_V_PBI builder_setBufferCapacityInFrames = nullptr;
148     signature_V_PBI builder_setChannelCount = nullptr;
149     signature_V_PBI builder_setDeviceId = nullptr;
150     signature_V_PBI builder_setDirection = nullptr;
151     signature_V_PBI builder_setFormat = nullptr;
152     signature_V_PBI builder_setFramesPerDataCallback = nullptr;
153     signature_V_PBI builder_setPerformanceMode = nullptr;
154     signature_V_PBI builder_setSampleRate = nullptr;
155     signature_V_PBI builder_setSharingMode = nullptr;
156 
157     signature_V_PBI builder_setUsage = nullptr;
158     signature_V_PBI builder_setContentType = nullptr;
159     signature_V_PBI builder_setInputPreset = nullptr;
160     signature_V_PBI builder_setSessionId = nullptr;
161 
162     signature_V_PBPDPV  builder_setDataCallback = nullptr;
163     signature_V_PBPEPV  builder_setErrorCallback = nullptr;
164 
165     signature_I_PB      builder_delete = nullptr;
166 
167     signature_F_PS      stream_getFormat = nullptr;
168 
169     signature_I_PSPVIL  stream_read = nullptr;
170     signature_I_PSCPVIL stream_write = nullptr;
171 
172     signature_I_PSTPTL  stream_waitForStateChange = nullptr;
173 
174     signature_I_PSKPLPL stream_getTimestamp = nullptr;
175 
176     signature_I_PS   stream_close = nullptr;
177 
178     signature_I_PS   stream_getChannelCount = nullptr;
179     signature_I_PS   stream_getDeviceId = nullptr;
180 
181     signature_I_PS   stream_getBufferSize = nullptr;
182     signature_I_PS   stream_getBufferCapacity = nullptr;
183     signature_I_PS   stream_getFramesPerBurst = nullptr;
184     signature_I_PS   stream_getState = nullptr;
185     signature_I_PS   stream_getPerformanceMode = nullptr;
186     signature_I_PS   stream_getSampleRate = nullptr;
187     signature_I_PS   stream_getSharingMode = nullptr;
188     signature_I_PS   stream_getXRunCount = nullptr;
189 
190     signature_I_PSI  stream_setBufferSize = nullptr;
191     signature_I_PS   stream_requestStart = nullptr;
192     signature_I_PS   stream_requestPause = nullptr;
193     signature_I_PS   stream_requestFlush = nullptr;
194     signature_I_PS   stream_requestStop = nullptr;
195 
196     signature_L_PS   stream_getFramesRead = nullptr;
197     signature_L_PS   stream_getFramesWritten = nullptr;
198 
199     signature_CPH_I  convertResultToText = nullptr;
200 
201     signature_I_PS   stream_getUsage = nullptr;
202     signature_I_PS   stream_getContentType = nullptr;
203     signature_I_PS   stream_getInputPreset = nullptr;
204     signature_I_PS   stream_getSessionId = nullptr;
205 
206   private:
AAudioLoader()207     AAudioLoader() {}
208     ~AAudioLoader();
209 
210     // Load function pointers for specific signatures.
211     signature_I_PPB     load_I_PPB(const char *name);
212     signature_CPH_I     load_CPH_I(const char *name);
213     signature_V_PBI     load_V_PBI(const char *name);
214     signature_V_PBPDPV  load_V_PBPDPV(const char *name);
215     signature_V_PBPEPV  load_V_PBPEPV(const char *name);
216     signature_I_PB      load_I_PB(const char *name);
217     signature_I_PBPPS   load_I_PBPPS(const char *name);
218     signature_I_PS      load_I_PS(const char *name);
219     signature_L_PS      load_L_PS(const char *name);
220     signature_F_PS      load_F_PS(const char *name);
221     signature_B_PS      load_B_PS(const char *name);
222     signature_I_PSI     load_I_PSI(const char *name);
223     signature_I_PSPVIL  load_I_PSPVIL(const char *name);
224     signature_I_PSCPVIL load_I_PSCPVIL(const char *name);
225     signature_I_PSTPTL  load_I_PSTPTL(const char *name);
226     signature_I_PSKPLPL load_I_PSKPLPL(const char *name);
227 
228     void *mLibHandle = nullptr;
229 };
230 
231 } // namespace oboe
232 
233 #endif //OBOE_AAUDIO_LOADER_H_
234