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 #define UNKNOWN_CHANNELMASK 0 85 86 #ifdef ANDROID 87 #include <utils/Log.h> 88 #include <utils/KeyedVector.h> 89 #include "media/AudioSystem.h" 90 #include "media/mediarecorder.h" 91 #include "media/AudioRecord.h" 92 #include "media/AudioTrack.h" 93 #include "media/mediaplayer.h" 94 #include <utils/String8.h> 95 #define ANDROID_SL_MILLIBEL_MAX 0 96 #include "android/android_sles_conversions.h" 97 #include "android/android_defs.h" 98 #endif 99 100 #ifdef USE_OUTPUTMIXEXT 101 #include "desktop/OutputMixExt.h" 102 #endif 103 104 #include "sllog.h" 105 106 typedef enum { 107 predestroy_error, // Application should not be calling destroy now 108 predestroy_ok, // OK to destroy object now 109 predestroy_again // Application did nothing wrong, but should destroy again to be effective 110 } predestroy_t; 111 112 // Hook functions 113 114 typedef void (*VoidHook)(void *self); 115 //typedef SLresult (*ResultHook)(void *self); 116 typedef SLresult (*AsyncHook)(void *self, SLboolean async); 117 typedef bool (*BoolHook)(void *self); 118 typedef predestroy_t (*PreDestroyHook)(void *self); 119 120 // Describes how an interface is related to a given class, used in iid_vtable::mInterface 121 122 #define INTERFACE_IMPLICIT 0 // no need for application to request prior to GetInterface 123 #define INTERFACE_EXPLICIT 1 // must be requested explicitly during object creation 124 #define INTERFACE_DYNAMIC 2 // can be requested after object creation 125 #define INTERFACE_UNAVAILABLE 3 // this interface is not available on objects of this class 126 #define INTERFACE_IMPLICIT_PREREALIZE 4 // implicit, and can call GetInterface before Realize 127 #define INTERFACE_EXPLICIT_PREREALIZE 5 // explicit, and can call GetInterface before Realize 128 // 6 and 7 are reserved for the meaningless DYNAMIC_PREREALIZE and UNAVAILABLE_PREREALIZE 129 // note that INTERFACE_OPTIONAL is always re-mapped to one of the above 130 #define INTERFACE_PREREALIZE 4 // bit-mask to test for calling GetInterface before Realize 131 132 // Profile-specific interfaces 133 134 #if USE_PROFILES & USE_PROFILES_BASE 135 #define INTERFACE_IMPLICIT_BASE INTERFACE_IMPLICIT 136 #define INTERFACE_EXPLICIT_BASE INTERFACE_EXPLICIT 137 #else 138 #define INTERFACE_IMPLICIT_BASE INTERFACE_UNAVAILABLE 139 #define INTERFACE_EXPLICIT_BASE INTERFACE_UNAVAILABLE 140 #endif 141 142 #if USE_PROFILES & USE_PROFILES_GAME 143 #define INTERFACE_DYNAMIC_GAME INTERFACE_DYNAMIC 144 #define INTERFACE_EXPLICIT_GAME INTERFACE_EXPLICIT 145 #else 146 #define INTERFACE_DYNAMIC_GAME INTERFACE_OPTIONAL 147 #define INTERFACE_EXPLICIT_GAME INTERFACE_OPTIONAL 148 #endif 149 150 #if USE_PROFILES & USE_PROFILES_MUSIC 151 #define INTERFACE_DYNAMIC_MUSIC INTERFACE_DYNAMIC 152 #else 153 #define INTERFACE_DYNAMIC_MUSIC INTERFACE_OPTIONAL 154 #endif 155 156 #if USE_PROFILES & (USE_PROFILES_GAME | USE_PROFILES_MUSIC) 157 #define INTERFACE_DYNAMIC_GAME_MUSIC INTERFACE_DYNAMIC 158 #define INTERFACE_EXPLICIT_GAME_MUSIC INTERFACE_EXPLICIT 159 #else 160 #define INTERFACE_DYNAMIC_GAME_MUSIC INTERFACE_OPTIONAL 161 #define INTERFACE_EXPLICIT_GAME_MUSIC INTERFACE_OPTIONAL 162 #endif 163 164 #if USE_PROFILES & (USE_PROFILES_GAME | USE_PROFILES_PHONE) 165 #define INTERFACE_EXPLICIT_GAME_PHONE INTERFACE_EXPLICIT 166 #else 167 #define INTERFACE_EXPLICIT_GAME_PHONE INTERFACE_OPTIONAL 168 #endif 169 170 #if USE_PROFILES & USE_PROFILES_OPTIONAL 171 #define INTERFACE_OPTIONAL INTERFACE_EXPLICIT 172 #define INTERFACE_DYNAMIC_OPTIONAL INTERFACE_DYNAMIC 173 #else 174 #define INTERFACE_OPTIONAL INTERFACE_UNAVAILABLE 175 #define INTERFACE_DYNAMIC_OPTIONAL INTERFACE_UNAVAILABLE 176 #endif 177 178 // Describes how an interface is related to a given object 179 180 #define INTERFACE_UNINITIALIZED 0 ///< not available 181 #define INTERFACE_INITIALIZED 1 ///< not requested at object creation time 182 #define INTERFACE_EXPOSED 2 ///< requested at object creation time 183 #define INTERFACE_ADDING_1 3 ///< part 1 of asynchronous AddInterface, pending 184 #define INTERFACE_ADDING_2 4 ///< synchronous AddInterface, or part 2 of asynchronous 185 #define INTERFACE_ADDED 5 ///< AddInterface has completed 186 #define INTERFACE_REMOVING 6 ///< unlocked phase of (synchronous) RemoveInterface 187 #define INTERFACE_SUSPENDING 7 ///< suspend in progress 188 #define INTERFACE_SUSPENDED 8 ///< suspend has completed 189 #define INTERFACE_RESUMING_1 9 ///< part 1 of asynchronous ResumeInterface, pending 190 #define INTERFACE_RESUMING_2 10 ///< synchronous ResumeInterface, or part 2 of asynchronous 191 #define INTERFACE_ADDING_1A 11 ///< part 1 of asynchronous AddInterface, aborted 192 #define INTERFACE_RESUMING_1A 12 ///< part 1 of asynchronous ResumeInterface, aborted 193 194 195 // Maps an interface ID to its offset within the class that exposes it 196 197 struct iid_vtable { 198 unsigned char mMPH; // primary MPH for this interface, does not include any aliases 199 unsigned char mInterface; // relationship of interface to this class 200 /*size_t*/ unsigned short mOffset; 201 }; 202 203 // Per-class const data shared by all instances of the same class 204 205 typedef struct { 206 const struct iid_vtable *mInterfaces; // maps interface index to info about that interface 207 SLuint32 mInterfaceCount; // number of possible interfaces 208 const signed char *mMPH_to_index; 209 const char * const mName; 210 size_t mSize; 211 // OpenSL ES and OpenMAX AL object IDs come from different ranges, and some objects such as 212 // Engine, Output Mix, LED, and Vibra belong to both APIs, so we keep both object IDs 213 SLuint16 mSLObjectID; // OpenSL ES object ID 214 XAuint16 mXAObjectID; // OpenMAX AL object ID 215 // hooks 216 AsyncHook mRealize; // called with mutex locked; can temporarily unlock mutex (for async) 217 AsyncHook mResume; // called with mutex locked; can temporarily unlock mutex (for async) 218 VoidHook mDestroy; // called with mutex locked and must keep mutex locked throughout 219 PreDestroyHook mPreDestroy; // called with mutex locked; can temporarily unlock mutex (for wait) 220 } ClassTable; 221 222 // BufferHeader describes each element of a BufferQueue, other than the data 223 typedef struct { 224 const void *mBuffer; 225 SLuint32 mSize; 226 } BufferHeader; 227 228 #ifdef ANDROID 229 // Holds information about all commands that can be passed alongside an MPEG-2 TS buffer 230 // Is used with buffers of type kAndroidBufferTypeMpeg2Ts 231 typedef struct { 232 SLuint32 mTsCmdCode; 233 SLAuint64 mPts; 234 } Mpeg2TsCommands; 235 236 // Holds information about all commands that can be passed alongside an AAC ADTS buffer 237 // Is used with buffers of type kAndroidBufferTypeAacadts 238 typedef struct { 239 SLuint32 mAdtsCmdCode; 240 } AdtsCommands; 241 242 // Union of the different structures to hold items stored in an AdvancedBufferHeader 243 // when an item comes from an AndroidBufferQueue as the data source, it's a command 244 // when an item is output to an AndroidBufferQueue as the data sink, it's a message (or metadata) 245 typedef union { 246 Mpeg2TsCommands mTsCmdData; 247 AdtsCommands mAdtsCmdData; 248 } AdvancedBufferItems; 249 250 // AdvancedBufferHeader describes each element of an AndroidBufferQueue, other than the data 251 // and associated messages 252 typedef struct { 253 const void *mDataBuffer; 254 SLuint32 mDataSize; 255 SLuint32 mDataSizeConsumed; 256 AdvancedBufferItems mItems; 257 const void *mBufferContext; 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 // LOGW("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