1 /*
2  * Copyright (C) 2010 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 /** \file sles_allinclusive.h Everything including the kitchen sink */
18 
19 #include <SLES/OpenSLES.h>
20 #include <OMXAL/OpenMAXAL.h>
21 #ifdef ANDROID
22 #include <SLES/OpenSLES_Android.h>
23 #include <OMXAL/OpenMAXAL_Android.h>
24 #endif
25 #include <stddef.h> // offsetof
26 #include <stdlib.h> // malloc
27 #include <string.h> // memcmp
28 #include <strings.h>
29 #include <stdio.h>  // debugging
30 #include <assert.h> // debugging
31 #include <pthread.h>
32 #include <unistd.h> // usleep
33 #include <errno.h>
34 
35 #ifndef __cplusplus
36 typedef int bool;
37 #ifndef false
38 #define false 0
39 #endif
40 #ifndef true
41 #define true 1
42 #endif
43 #endif
44 
45 // The OpenSLES.h definitions of SL_PROFILES_... have casts, so are unusable by preprocessor
46 #define USE_PROFILES_PHONE    0x1   // == SL_PROFILES_PHONE
47 #define USE_PROFILES_MUSIC    0x2   // == SL_PROFILES_MUSIC
48 #define USE_PROFILES_GAME     0x4   // == SL_PROFILES_GAME
49 // Pseudo profiles, used to decide whether to include code for incomplete or untested features
50 // Features that are not in union of all profiles: audio recorder, LED, Vibra
51 #define USE_PROFILES_OPTIONAL 0x8
52 // Features that are in the intersection of all profiles:
53 // object priorities, preemption, loss of control, device configuration
54 #define USE_PROFILES_BASE     0x10
55 
56 #include "MPH.h"
57 #include "MPH_to.h"
58 #include "devices.h"
59 #include "ut/OpenSLESUT.h"
60 #include "ThreadPool.h"
61 
62 typedef struct CEngine_struct CEngine;
63 typedef struct CAudioPlayer_struct CAudioPlayer;
64 typedef struct CAudioRecorder_struct CAudioRecorder;
65 typedef struct C3DGroup_struct C3DGroup;
66 typedef struct COutputMix_struct COutputMix;
67 
68 #ifdef USE_SNDFILE
69 #include <sndfile.h>
70 #include "desktop/SLSndFile.h"
71 #endif // USE_SNDFILE
72 
73 #ifdef USE_SDL
74 #include <SDL/SDL_audio.h>
75 #endif // USE_SDL
76 
77 #define STEREO_CHANNELS 2
78 
79 /**
80  * Constants to define unknown property values
81  */
82 #define UNKNOWN_NUMCHANNELS 0
83 #define UNKNOWN_SAMPLERATE  0
84 
85 #ifdef ANDROID
86 #include <utils/Log.h>
87 #include <utils/KeyedVector.h>
88 #include "media/AudioSystem.h"
89 #include "media/mediarecorder.h"
90 #include "media/AudioRecord.h"
91 #include "media/AudioTrack.h"
92 #include "media/mediaplayer.h"
93 #include <utils/String8.h>
94 #define ANDROID_SL_MILLIBEL_MAX 0
95 #include "android/android_sles_conversions.h"
96 #include "android/android_defs.h"
97 #endif
98 
99 #ifdef USE_OUTPUTMIXEXT
100 #include "desktop/OutputMixExt.h"
101 #endif
102 
103 #include "sllog.h"
104 
105 typedef enum {
106     predestroy_error,   // Application should not be calling destroy now
107     predestroy_ok,      // OK to destroy object now
108     predestroy_again    // Application did nothing wrong, but should destroy again to be effective
109 } predestroy_t;
110 
111 // Hook functions
112 
113 typedef void (*VoidHook)(void *self);
114 //typedef SLresult (*ResultHook)(void *self);
115 typedef SLresult (*AsyncHook)(void *self, SLboolean async);
116 typedef bool (*BoolHook)(void *self);
117 typedef predestroy_t (*PreDestroyHook)(void *self);
118 
119 // Describes how an interface is related to a given class, used in iid_vtable::mInterface
120 
121 #define INTERFACE_IMPLICIT            0 // no need for application to request prior to GetInterface
122 #define INTERFACE_EXPLICIT            1 // must be requested explicitly during object creation
123 #define INTERFACE_DYNAMIC             2 // can be requested after object creation
124 #define INTERFACE_UNAVAILABLE         3 // this interface is not available on objects of this class
125 #define INTERFACE_IMPLICIT_PREREALIZE 4 // implicit, and can call GetInterface before Realize
126 #define INTERFACE_EXPLICIT_PREREALIZE 5 // explicit, and can call GetInterface before Realize
127 // 6 and 7 are reserved for the meaningless DYNAMIC_PREREALIZE and UNAVAILABLE_PREREALIZE
128 // note that INTERFACE_OPTIONAL is always re-mapped to one of the above
129 #define INTERFACE_PREREALIZE          4 // bit-mask to test for calling GetInterface before Realize
130 
131 // Profile-specific interfaces
132 
133 #if USE_PROFILES & USE_PROFILES_BASE
134 #define INTERFACE_IMPLICIT_BASE       INTERFACE_IMPLICIT
135 #define INTERFACE_EXPLICIT_BASE       INTERFACE_EXPLICIT
136 #else
137 #define INTERFACE_IMPLICIT_BASE       INTERFACE_UNAVAILABLE
138 #define INTERFACE_EXPLICIT_BASE       INTERFACE_UNAVAILABLE
139 #endif
140 
141 #if USE_PROFILES & USE_PROFILES_GAME
142 #define INTERFACE_DYNAMIC_GAME        INTERFACE_DYNAMIC
143 #define INTERFACE_EXPLICIT_GAME       INTERFACE_EXPLICIT
144 #else
145 #define INTERFACE_DYNAMIC_GAME        INTERFACE_OPTIONAL
146 #define INTERFACE_EXPLICIT_GAME       INTERFACE_OPTIONAL
147 #endif
148 
149 #if USE_PROFILES & USE_PROFILES_MUSIC
150 #define INTERFACE_DYNAMIC_MUSIC       INTERFACE_DYNAMIC
151 #else
152 #define INTERFACE_DYNAMIC_MUSIC       INTERFACE_OPTIONAL
153 #endif
154 
155 #if USE_PROFILES & (USE_PROFILES_GAME | USE_PROFILES_MUSIC)
156 #define INTERFACE_DYNAMIC_GAME_MUSIC  INTERFACE_DYNAMIC
157 #define INTERFACE_EXPLICIT_GAME_MUSIC INTERFACE_EXPLICIT
158 #else
159 #define INTERFACE_DYNAMIC_GAME_MUSIC  INTERFACE_OPTIONAL
160 #define INTERFACE_EXPLICIT_GAME_MUSIC INTERFACE_OPTIONAL
161 #endif
162 
163 #if USE_PROFILES & (USE_PROFILES_GAME | USE_PROFILES_PHONE)
164 #define INTERFACE_EXPLICIT_GAME_PHONE INTERFACE_EXPLICIT
165 #else
166 #define INTERFACE_EXPLICIT_GAME_PHONE INTERFACE_OPTIONAL
167 #endif
168 
169 #if USE_PROFILES & USE_PROFILES_OPTIONAL
170 #define INTERFACE_OPTIONAL            INTERFACE_EXPLICIT
171 #define INTERFACE_DYNAMIC_OPTIONAL    INTERFACE_DYNAMIC
172 #else
173 #define INTERFACE_OPTIONAL            INTERFACE_UNAVAILABLE
174 #define INTERFACE_DYNAMIC_OPTIONAL    INTERFACE_UNAVAILABLE
175 #endif
176 
177 // Describes how an interface is related to a given object
178 
179 #define INTERFACE_UNINITIALIZED 0  ///< not available
180 #define INTERFACE_INITIALIZED   1  ///< not requested at object creation time
181 #define INTERFACE_EXPOSED       2  ///< requested at object creation time
182 #define INTERFACE_ADDING_1      3  ///< part 1 of asynchronous AddInterface, pending
183 #define INTERFACE_ADDING_2      4  ///< synchronous AddInterface, or part 2 of asynchronous
184 #define INTERFACE_ADDED         5  ///< AddInterface has completed
185 #define INTERFACE_REMOVING      6  ///< unlocked phase of (synchronous) RemoveInterface
186 #define INTERFACE_SUSPENDING    7  ///< suspend in progress
187 #define INTERFACE_SUSPENDED     8  ///< suspend has completed
188 #define INTERFACE_RESUMING_1    9  ///< part 1 of asynchronous ResumeInterface, pending
189 #define INTERFACE_RESUMING_2   10  ///< synchronous ResumeInterface, or part 2 of asynchronous
190 #define INTERFACE_ADDING_1A    11  ///< part 1 of asynchronous AddInterface, aborted
191 #define INTERFACE_RESUMING_1A  12  ///< part 1 of asynchronous ResumeInterface, aborted
192 
193 
194 // Maps an interface ID to its offset within the class that exposes it
195 
196 struct iid_vtable {
197     unsigned char mMPH;         // primary MPH for this interface, does not include any aliases
198     unsigned char mInterface;   // relationship of interface to this class
199     /*size_t*/ unsigned short mOffset;
200 };
201 
202 // Per-class const data shared by all instances of the same class
203 
204 typedef struct {
205     const struct iid_vtable *mInterfaces;   // maps interface index to info about that interface
206     SLuint32 mInterfaceCount;  // number of possible interfaces
207     const signed char *mMPH_to_index;
208     const char * const mName;
209     size_t mSize;
210     // OpenSL ES and OpenMAX AL object IDs come from different ranges, and some objects such as
211     // Engine, Output Mix, LED, and Vibra belong to both APIs, so we keep both object IDs
212     SLuint16 mSLObjectID;   // OpenSL ES object ID
213     XAuint16 mXAObjectID;   // OpenMAX AL object ID
214     // hooks
215     AsyncHook mRealize;     // called with mutex locked; can temporarily unlock mutex (for async)
216     AsyncHook mResume;      // called with mutex locked; can temporarily unlock mutex (for async)
217     VoidHook mDestroy;      // called with mutex locked and must keep mutex locked throughout
218     PreDestroyHook mPreDestroy; // called with mutex locked; can temporarily unlock mutex (for wait)
219 } ClassTable;
220 
221 // BufferHeader describes each element of a BufferQueue, other than the data
222 typedef struct {
223     const void *mBuffer;
224     SLuint32 mSize;
225 } BufferHeader;
226 
227 #ifdef ANDROID
228 // Holds information about all commands that can be passed alongside an MPEG-2 TS buffer
229 // Is used with buffers of type kAndroidBufferTypeMpeg2Ts
230 typedef struct {
231     SLuint32 mTsCmdCode;
232     SLAuint64 mPts;
233 } Mpeg2TsCommands;
234 
235 // Holds information about all commands that can be passed alongside an AAC ADTS buffer
236 // Is used with buffers of type kAndroidBufferTypeAacadts
237 typedef struct {
238     SLuint32 mAdtsCmdCode;
239 } AdtsCommands;
240 
241 // Union of the different structures to hold items stored in an AdvancedBufferHeader
242 //   when an item comes from an AndroidBufferQueue as the data source, it's a command
243 //   when an item is output to an AndroidBufferQueue as the data sink, it's a message (or metadata)
244 typedef union {
245     Mpeg2TsCommands mTsCmdData;
246     AdtsCommands    mAdtsCmdData;
247 } AdvancedBufferItems;
248 
249 // AdvancedBufferHeader describes each element of an AndroidBufferQueue, other than the data
250 //  and associated messages
251 typedef struct {
252     const void *mDataBuffer;
253     SLuint32 mDataSize;
254     SLuint32 mDataSizeConsumed;
255     AdvancedBufferItems mItems;
256     const void *mBufferContext;
257     // mBufferState will be used for the other ABQ events we'll support in the future
258     // SLuint32 mBufferState;
259 } AdvancedBufferHeader;
260 #endif
261 
262 #ifdef USE_SNDFILE
263 
264 #define SndFile_BUFSIZE 512     // in 16-bit samples
265 #define SndFile_NUMBUFS 2
266 
267 struct SndFile {
268     // save URI also?
269     SLchar *mPathname;
270     SNDFILE *mSNDFILE;
271     SF_INFO mSfInfo;
272     pthread_mutex_t mMutex; // protects mSNDFILE only
273     SLboolean mEOF;         // sf_read returned zero sample frames
274     SLuint32 mWhich;        // which buffer to use next
275     short mBuffer[SndFile_BUFSIZE * SndFile_NUMBUFS];
276 };
277 
278 #endif // USE_SNDFILE
279 
280 #include "data.h"
281 #include "itfstruct.h"
282 #include "classes.h"
283 
284 struct MPH_init {
285     VoidHook mInit;     // called first to initialize the interface, right after object is allocated
286     // Each interface is initialized regardless whether it is exposed to application.
287     VoidHook mResume;   // called to resume interface after suspension, not currently used
288     VoidHook mDeinit;   // called last when object is about to be destroyed
289     BoolHook mExpose;   // called after initialization, only if interface is exposed to application
290     VoidHook mRemove;   // called by DynamicInterfaceManager::RemoveInterface, and prior to mDeinit
291     // will need a suspend hook when suspend is implemented
292 };
293 
294 extern /*static*/ int IID_to_MPH(const SLInterfaceID iid);
295 extern /*static*/ const struct MPH_init MPH_init_table[MPH_MAX];
296 extern SLresult checkInterfaces(const ClassTable *clazz,
297     SLuint32 numInterfaces, const SLInterfaceID *pInterfaceIds,
298     const SLboolean *pInterfaceRequired, unsigned *pExposedMask, unsigned *pRequiredMask);
299 extern IObject *construct(const ClassTable *clazz,
300     unsigned exposedMask, SLEngineItf engine);
301 extern const ClassTable *objectIDtoClass(SLuint32 objectID);
302 extern const struct SLInterfaceID_ SL_IID_array[MPH_MAX];
303 extern SLuint32 IObjectToObjectID(IObject *object);
304 extern void IObject_Publish(IObject *thiz);
305 extern void IObject_Destroy(SLObjectItf self);
306 
307 // Map an interface to it's "object ID" (which is really a class ID).
308 // Note: this operation is undefined on IObject, as it lacks an mThis.
309 // If you have an IObject, then use IObjectToObjectID directly.
310 
311 #define InterfaceToObjectID(thiz) IObjectToObjectID((thiz)->mThis)
312 
313 // Map an interface to it's corresponding IObject.
314 // Note: this operation is undefined on IObject, as it lacks an mThis.
315 // If you have an IObject, then you're done -- you already have what you need.
316 
317 #define InterfaceToIObject(thiz) ((thiz)->mThis)
318 
319 #define InterfaceToCAudioPlayer(thiz) (((CAudioPlayer*)InterfaceToIObject(thiz)))
320 
321 #define InterfaceToCAudioRecorder(thiz) (((CAudioRecorder*)InterfaceToIObject(thiz)))
322 
323 #define InterfaceToCAudioRecorder(thiz) (((CAudioRecorder*)InterfaceToIObject(thiz)))
324 
325 #define InterfaceToCMediaPlayer(thiz) (((CMediaPlayer*)InterfaceToIObject(thiz)))
326 
327 #ifdef ANDROID
328 #include "android/MediaPlayer_to_android.h"
329 #include "android/OutputMix_to_android.h"
330 #include "android/AudioPlayer_to_android.h"
331 #include "android/AudioRecorder_to_android.h"
332 #endif
333 
334 extern predestroy_t C3DGroup_PreDestroy(void *self);
335 
336 extern SLresult CAudioPlayer_Realize(void *self, SLboolean async);
337 extern SLresult CAudioPlayer_Resume(void *self, SLboolean async);
338 extern void CAudioPlayer_Destroy(void *self);
339 extern predestroy_t CAudioPlayer_PreDestroy(void *self);
340 
341 extern SLresult CAudioRecorder_Realize(void *self, SLboolean async);
342 extern SLresult CAudioRecorder_Resume(void *self, SLboolean async);
343 extern void CAudioRecorder_Destroy(void *self);
344 extern predestroy_t CAudioRecorder_PreDestroy(void *self);
345 
346 extern SLresult CEngine_Realize(void *self, SLboolean async);
347 extern SLresult CEngine_Resume(void *self, SLboolean async);
348 extern void CEngine_Destroy(void *self);
349 extern predestroy_t CEngine_PreDestroy(void *self);
350 extern void CEngine_Destroyed(CEngine *self);
351 
352 extern SLresult COutputMix_Realize(void *self, SLboolean async);
353 extern SLresult COutputMix_Resume(void *self, SLboolean async);
354 extern void COutputMix_Destroy(void *self);
355 extern predestroy_t COutputMix_PreDestroy(void *self);
356 
357 extern SLresult CMediaPlayer_Realize(void *self, SLboolean async);
358 extern SLresult CMediaPlayer_Resume(void *self, SLboolean async);
359 extern void CMediaPlayer_Destroy(void *self);
360 extern predestroy_t CMediaPlayer_PreDestroy(void *self);
361 
362 #ifdef USE_SDL
363 extern void SDL_open(IEngine *thisEngine);
364 extern void SDL_close(void);
365 #endif
366 
367 #define SL_OBJECT_STATE_REALIZING_1  ((SLuint32) 0x4) // async realize on work queue
368 #define SL_OBJECT_STATE_REALIZING_2  ((SLuint32) 0x5) // sync realize, or async realize hook
369 #define SL_OBJECT_STATE_RESUMING_1   ((SLuint32) 0x6) // async resume on work queue
370 #define SL_OBJECT_STATE_RESUMING_2   ((SLuint32) 0x7) // sync resume, or async resume hook
371 #define SL_OBJECT_STATE_SUSPENDING   ((SLuint32) 0x8) // suspend in progress
372 #define SL_OBJECT_STATE_REALIZING_1A ((SLuint32) 0x9) // abort while async realize on work queue
373 #define SL_OBJECT_STATE_RESUMING_1A  ((SLuint32) 0xA) // abort while async resume on work queue
374 #define SL_OBJECT_STATE_DESTROYING   ((SLuint32) 0xB) // destroy object when no strong references
375 
376 #ifdef USE_OUTPUTMIXEXT
377 #define SL_PLAYSTATE_STOPPING ((SLuint32) 0x4) // Play::Stop while PLAYING
378 // If we needed it, could have PLAYING mean mixer is currently reading from front buffer,
379 // while PLAYABLE would mean application requested PLAYING, but buffer queue is empty
380 #endif
381 
382 #ifndef ANDROID
383 extern void *sync_start(void *arg);
384 #endif
385 extern SLresult err_to_result(int err);
386 
387 #ifdef __GNUC__
388 #define ctz __builtin_ctz
389 #else
390 extern unsigned ctz(unsigned);
391 #endif
392 extern const char * const interface_names[MPH_MAX];
393 #include "platform.h"
394 #include "attr.h"
395 #include "handlers.h"
396 #include "trace.h"
397 
398 #ifdef USE_SNDFILE
399 extern void audioPlayerTransportUpdate(CAudioPlayer *audioPlayer);
400 #endif
401 
402 extern SLresult IBufferQueue_Enqueue(SLBufferQueueItf self, const void *pBuffer, SLuint32 size);
403 extern SLresult IBufferQueue_Clear(SLBufferQueueItf self);
404 extern SLresult IBufferQueue_RegisterCallback(SLBufferQueueItf self,
405     slBufferQueueCallback callback, void *pContext);
406 
407 extern bool IsInterfaceInitialized(IObject *thiz, unsigned MPH);
408 extern SLresult AcquireStrongRef(IObject *object, SLuint32 expectedObjectID);
409 extern void ReleaseStrongRef(IObject *object);
410 extern void ReleaseStrongRefAndUnlockExclusive(IObject *object);
411 
412 extern COutputMix *CAudioPlayer_GetOutputMix(CAudioPlayer *audioPlayer);
413 extern SLresult IEngineCapabilities_QueryLEDCapabilities(SLEngineCapabilitiesItf self,
414     SLuint32 *pIndex, SLuint32 *pLEDDeviceID, SLLEDDescriptor *pDescriptor);
415 extern SLresult IEngineCapabilities_QueryVibraCapabilities(SLEngineCapabilitiesItf self,
416     SLuint32 *pIndex, SLuint32 *pVibraDeviceID, SLVibraDescriptor *pDescriptor);
417 
418 extern CEngine *theOneTrueEngine;
419 extern pthread_mutex_t theOneTrueMutex;
420 extern unsigned theOneTrueRefCount;
421 
422 extern LI_API SLresult liCreateEngine(SLObjectItf *pEngine, SLuint32 numOptions,
423     const SLEngineOption *pEngineOptions, SLuint32 numInterfaces,
424     const SLInterfaceID *pInterfaceIds, const SLboolean *pInterfaceRequired,
425     const ClassTable *pCEngine_class);
426 extern LI_API SLresult liQueryNumSupportedInterfaces(SLuint32 *pNumSupportedInterfaces,
427         const ClassTable *clazz);
428 extern LI_API SLresult liQuerySupportedInterfaces(SLuint32 index, SLInterfaceID *pInterfaceId,
429         const ClassTable *clazz);
430 
431 // The EnqueueAsyncCallback macros provide a safe way to asynchronously call an application-level
432 // callback handler that is permitted to do almost anything, including block.  This is intended
433 // primarily for "notification" callbacks such as play head progress.  Do not use for callbacks
434 // which must be synchronous, such as buffer queue completions.  The enqueue may fail if
435 // the callback queue is full.  This almost always indicates an application error such as blocking
436 // for an excessive time within a callback handler or requesting too frequent callbacks.  The
437 // recommended recovery is to either retry later, or log a warning or error as appropriate.
438 // If the callback absolutely must be called, then you should be calling it directly instead.
439 // Example usage:
440 //  CAudioPlayer *ap;
441 //  SLresult result = EnqueueAsyncCallback_ppi(ap, playCallback, &ap->mPlay.mItf, playContext,
442 //       SL_PLAYEVENT_HEADATEND);
443 //  if (SL_RESULT_SUCCESS != result) {
444 //    ALOGW("Callback %p(%p, %p, SL_PLAYEVENT_HEADATEND) dropped", playCallback, &ap->mPlay.mItf,
445 //        playContext);
446 //  }
447 // which replaces:
448 //  (*playCallback)(&ap->mPlay.mItf, playContext, SL_PLAYEVENT_HEADATEND);
449 #define EnqueueAsyncCallback_ppi(object, handler, p1, p2, i1) \
450         ThreadPool_add_ppi(&(object)->mObject.mEngine->mThreadPool, \
451             (ClosureHandler_ppi) (handler), (p1), (p2), (i1))
452 #define EnqueueAsyncCallback_ppii(object, handler, p1, p2, i1, i2) \
453         ThreadPool_add_ppii(&(object)->mObject.mEngine->mThreadPool, \
454             (ClosureHandler_ppii) (handler), (p1), (p2), (i1), (i2))
455 #define EnqueueAsyncCallback_piipp(object, handler, p1, i1, i2, p2, p3) \
456         ThreadPool_add_piipp(&(object)->mObject.mEngine->mThreadPool, \
457             (ClosureHandler_piipp) (handler), (p1), (i1), (i2), (p2), (p3))
458 
459 #define SL_PREFETCHEVENT_NONE ((SLuint32) 0)    // placeholder for non-existent SL_PREFETCHEVENT_*
460