sles_allinclusive.h revision 7b726bdcd996f1cab3a584c04ce1afc07bc8fbe7
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 36typedef 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 62typedef struct CEngine_struct CEngine; 63typedef struct CAudioPlayer_struct CAudioPlayer; 64typedef struct CAudioRecorder_struct CAudioRecorder; 65typedef struct C3DGroup_struct C3DGroup; 66typedef 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#ifdef ANDROID 80#include <utils/Log.h> 81#include <utils/KeyedVector.h> 82#include "SLES/OpenSLES_AndroidConfiguration.h" 83#include "media/AudioSystem.h" 84#include "media/mediarecorder.h" 85#include "media/AudioRecord.h" 86#include "media/AudioTrack.h" 87#include "media/mediaplayer.h" 88#include <utils/String8.h> 89#define ANDROID_SL_MILLIBEL_MAX 0 90#include "android/android_sles_conversions.h" 91#include "android/android_defs.h" 92#endif 93 94#ifdef USE_OUTPUTMIXEXT 95#include "desktop/OutputMixExt.h" 96#endif 97 98#include "sllog.h" 99 100typedef enum { 101 predestroy_error, // Application should not be calling destroy now 102 predestroy_ok, // OK to destroy object now 103 predestroy_again // Application did nothing wrong, but should destroy again to be effective 104} predestroy_t; 105 106// Hook functions 107 108typedef void (*VoidHook)(void *self); 109//typedef SLresult (*ResultHook)(void *self); 110typedef SLresult (*AsyncHook)(void *self, SLboolean async); 111typedef bool (*BoolHook)(void *self); 112typedef predestroy_t (*PreDestroyHook)(void *self); 113 114// Describes how an interface is related to a given class, used in iid_vtable::mInterface 115 116#define INTERFACE_IMPLICIT 0 // no need for application to request prior to GetInterface 117#define INTERFACE_EXPLICIT 1 // must be requested explicitly during object creation 118#define INTERFACE_DYNAMIC 2 // can be requested after object creation 119#define INTERFACE_UNAVAILABLE 3 // this interface is not available on objects of this class 120#define INTERFACE_IMPLICIT_PREREALIZE 4 // implicit, and can call GetInterface before Realize 121#define INTERFACE_EXPLICIT_PREREALIZE 5 // explicit, and can call GetInterface before Realize 122// 6 and 7 are reserved for the meaningless DYNAMIC_PREREALIZE and UNAVAILABLE_PREREALIZE 123// note that INTERFACE_OPTIONAL is always re-mapped to one of the above 124#define INTERFACE_PREREALIZE 4 // bit-mask to test for calling GetInterface before Realize 125 126// Profile-specific interfaces 127 128#if USE_PROFILES & USE_PROFILES_BASE 129#define INTERFACE_IMPLICIT_BASE INTERFACE_IMPLICIT 130#define INTERFACE_EXPLICIT_BASE INTERFACE_EXPLICIT 131#else 132#define INTERFACE_IMPLICIT_BASE INTERFACE_UNAVAILABLE 133#define INTERFACE_EXPLICIT_BASE INTERFACE_UNAVAILABLE 134#endif 135 136#if USE_PROFILES & USE_PROFILES_GAME 137#define INTERFACE_DYNAMIC_GAME INTERFACE_DYNAMIC 138#define INTERFACE_EXPLICIT_GAME INTERFACE_EXPLICIT 139#else 140#define INTERFACE_DYNAMIC_GAME INTERFACE_OPTIONAL 141#define INTERFACE_EXPLICIT_GAME INTERFACE_OPTIONAL 142#endif 143 144#if USE_PROFILES & USE_PROFILES_MUSIC 145#define INTERFACE_DYNAMIC_MUSIC INTERFACE_DYNAMIC 146#else 147#define INTERFACE_DYNAMIC_MUSIC INTERFACE_OPTIONAL 148#endif 149 150#if USE_PROFILES & (USE_PROFILES_GAME | USE_PROFILES_MUSIC) 151#define INTERFACE_DYNAMIC_GAME_MUSIC INTERFACE_DYNAMIC 152#define INTERFACE_EXPLICIT_GAME_MUSIC INTERFACE_EXPLICIT 153#else 154#define INTERFACE_DYNAMIC_GAME_MUSIC INTERFACE_OPTIONAL 155#define INTERFACE_EXPLICIT_GAME_MUSIC INTERFACE_OPTIONAL 156#endif 157 158#if USE_PROFILES & (USE_PROFILES_GAME | USE_PROFILES_PHONE) 159#define INTERFACE_EXPLICIT_GAME_PHONE INTERFACE_EXPLICIT 160#else 161#define INTERFACE_EXPLICIT_GAME_PHONE INTERFACE_OPTIONAL 162#endif 163 164#if USE_PROFILES & USE_PROFILES_OPTIONAL 165#define INTERFACE_OPTIONAL INTERFACE_EXPLICIT 166#define INTERFACE_DYNAMIC_OPTIONAL INTERFACE_DYNAMIC 167#else 168#define INTERFACE_OPTIONAL INTERFACE_UNAVAILABLE 169#define INTERFACE_DYNAMIC_OPTIONAL INTERFACE_UNAVAILABLE 170#endif 171 172// Describes how an interface is related to a given object 173 174#define INTERFACE_UNINITIALIZED 0 ///< not available 175#define INTERFACE_INITIALIZED 1 ///< not requested at object creation time 176#define INTERFACE_EXPOSED 2 ///< requested at object creation time 177#define INTERFACE_ADDING_1 3 ///< part 1 of asynchronous AddInterface, pending 178#define INTERFACE_ADDING_2 4 ///< synchronous AddInterface, or part 2 of asynchronous 179#define INTERFACE_ADDED 5 ///< AddInterface has completed 180#define INTERFACE_REMOVING 6 ///< unlocked phase of (synchronous) RemoveInterface 181#define INTERFACE_SUSPENDING 7 ///< suspend in progress 182#define INTERFACE_SUSPENDED 8 ///< suspend has completed 183#define INTERFACE_RESUMING_1 9 ///< part 1 of asynchronous ResumeInterface, pending 184#define INTERFACE_RESUMING_2 10 ///< synchronous ResumeInterface, or part 2 of asynchronous 185#define INTERFACE_ADDING_1A 11 ///< part 1 of asynchronous AddInterface, aborted 186#define INTERFACE_RESUMING_1A 12 ///< part 1 of asynchronous ResumeInterface, aborted 187 188 189// Maps an interface ID to its offset within the class that exposes it 190 191struct iid_vtable { 192 unsigned char mMPH; // primary MPH for this interface, does not include any aliases 193 unsigned char mInterface; // relationship of interface to this class 194 /*size_t*/ unsigned short mOffset; 195}; 196 197// Per-class const data shared by all instances of the same class 198 199typedef struct { 200 const struct iid_vtable *mInterfaces; // maps interface index to info about that interface 201 SLuint32 mInterfaceCount; // number of possible interfaces 202 const signed char *mMPH_to_index; 203 const char * const mName; 204 size_t mSize; 205 // OpenSL ES and OpenMAX AL object IDs come from different ranges, and some objects such as 206 // Engine, Output Mix, LED, and Vibra belong to both APIs, so we keep both object IDs 207 SLuint16 mSLObjectID; // OpenSL ES object ID 208 XAuint16 mXAObjectID; // OpenMAX AL object ID 209 // hooks 210 AsyncHook mRealize; 211 AsyncHook mResume; 212 VoidHook mDestroy; 213 PreDestroyHook mPreDestroy; 214} ClassTable; 215 216// BufferHeader describes each element of a BufferQueue, other than the data 217typedef struct { 218 const void *mBuffer; 219 SLuint32 mSize; 220} BufferHeader; 221 222#ifdef ANDROID 223// Holds information about all commands that can be passed alongside an MPEG-2 TS buffer 224// Is used with buffers of type kAndroidBufferTypeMpeg2Ts 225typedef struct { 226 SLuint32 mTsCmdCode; 227 SLAuint64 mPts; 228} Mpeg2TsCommands; 229 230// Union of the different structures to hold items stored in an AdvancedBufferHeader 231// when an item comes from an AndroidBufferQueue as the data source, it's a command 232// when an item is output to an AndroidBufferQueue as the data sink, it's a message (or metadata) 233typedef union { 234 Mpeg2TsCommands mTsCmdData; 235} AdvancedBufferItems; 236 237// AdvancedBufferHeader describes each element of an AndroidBufferQueue, other than the data 238// and associated messages 239typedef struct { 240 const void *mDataBuffer; 241 SLuint32 mDataSize; 242 SLuint32 mDataSizeConsumed; 243 AdvancedBufferItems mItems; 244 const void *mBufferContext; 245 SLuint32 mBufferState; 246} AdvancedBufferHeader; 247#endif 248 249#ifdef USE_SNDFILE 250 251#define SndFile_BUFSIZE 512 // in 16-bit samples 252#define SndFile_NUMBUFS 2 253 254struct SndFile { 255 // save URI also? 256 SLchar *mPathname; 257 SNDFILE *mSNDFILE; 258 SF_INFO mSfInfo; 259 pthread_mutex_t mMutex; // protects mSNDFILE only 260 SLboolean mEOF; // sf_read returned zero sample frames 261 SLuint32 mWhich; // which buffer to use next 262 short mBuffer[SndFile_BUFSIZE * SndFile_NUMBUFS]; 263}; 264 265#endif // USE_SNDFILE 266 267#include "data.h" 268#include "itfstruct.h" 269#include "classes.h" 270 271struct MPH_init { 272 VoidHook mInit; // called first to initialize the interface, right after object is allocated 273 // Each interface is initialized regardless whether it is exposed to application. 274 VoidHook mResume; // called to resume interface after suspension, not currently used 275 VoidHook mDeinit; // called last when object is about to be destroyed 276 BoolHook mExpose; // called after initialization, only if interface is exposed to application 277 VoidHook mRemove; // called by DynamicInterfaceManager::RemoveInterface, and prior to mDeinit 278 // will need a suspend hook when suspend is implemented 279}; 280 281extern /*static*/ int IID_to_MPH(const SLInterfaceID iid); 282extern /*static*/ const struct MPH_init MPH_init_table[MPH_MAX]; 283extern SLresult checkInterfaces(const ClassTable *clazz, 284 SLuint32 numInterfaces, const SLInterfaceID *pInterfaceIds, 285 const SLboolean *pInterfaceRequired, unsigned *pExposedMask); 286extern IObject *construct(const ClassTable *clazz, 287 unsigned exposedMask, SLEngineItf engine); 288extern const ClassTable *objectIDtoClass(SLuint32 objectID); 289extern const struct SLInterfaceID_ SL_IID_array[MPH_MAX]; 290extern SLuint32 IObjectToObjectID(IObject *object); 291extern void IObject_Publish(IObject *thiz); 292extern void IObject_Destroy(SLObjectItf self); 293 294// Map an interface to it's "object ID" (which is really a class ID). 295// Note: this operation is undefined on IObject, as it lacks an mThis. 296// If you have an IObject, then use IObjectToObjectID directly. 297 298#define InterfaceToObjectID(thiz) IObjectToObjectID((thiz)->mThis) 299 300// Map an interface to it's corresponding IObject. 301// Note: this operation is undefined on IObject, as it lacks an mThis. 302// If you have an IObject, then you're done -- you already have what you need. 303 304#define InterfaceToIObject(thiz) ((thiz)->mThis) 305 306#define InterfaceToCAudioPlayer(thiz) (((CAudioPlayer*)InterfaceToIObject(thiz))) 307 308#define InterfaceToCAudioRecorder(thiz) (((CAudioRecorder*)InterfaceToIObject(thiz))) 309 310#define InterfaceToCAudioRecorder(thiz) (((CAudioRecorder*)InterfaceToIObject(thiz))) 311 312#define InterfaceToCMediaPlayer(thiz) (((CMediaPlayer*)InterfaceToIObject(thiz))) 313 314#ifdef ANDROID 315#include "android/MediaPlayer_to_android.h" 316#include "android/OutputMix_to_android.h" 317#include "android/AudioPlayer_to_android.h" 318#include "android/AudioRecorder_to_android.h" 319#endif 320 321extern predestroy_t C3DGroup_PreDestroy(void *self); 322 323extern SLresult CAudioPlayer_Realize(void *self, SLboolean async); 324extern SLresult CAudioPlayer_Resume(void *self, SLboolean async); 325extern void CAudioPlayer_Destroy(void *self); 326extern predestroy_t CAudioPlayer_PreDestroy(void *self); 327 328extern SLresult CAudioRecorder_Realize(void *self, SLboolean async); 329extern SLresult CAudioRecorder_Resume(void *self, SLboolean async); 330extern void CAudioRecorder_Destroy(void *self); 331extern predestroy_t CAudioRecorder_PreDestroy(void *self); 332 333extern SLresult CEngine_Realize(void *self, SLboolean async); 334extern SLresult CEngine_Resume(void *self, SLboolean async); 335extern void CEngine_Destroy(void *self); 336extern predestroy_t CEngine_PreDestroy(void *self); 337extern void CEngine_Destroyed(CEngine *self); 338 339extern SLresult COutputMix_Realize(void *self, SLboolean async); 340extern SLresult COutputMix_Resume(void *self, SLboolean async); 341extern void COutputMix_Destroy(void *self); 342extern predestroy_t COutputMix_PreDestroy(void *self); 343 344extern SLresult CMediaPlayer_Realize(void *self, SLboolean async); 345extern SLresult CMediaPlayer_Resume(void *self, SLboolean async); 346extern void CMediaPlayer_Destroy(void *self); 347extern predestroy_t CMediaPlayer_PreDestroy(void *self); 348 349#ifdef USE_SDL 350extern void SDL_open(IEngine *thisEngine); 351extern void SDL_close(void); 352#endif 353#define SL_OBJECT_STATE_REALIZING_1 ((SLuint32) 0x4) // async realize on work queue 354#define SL_OBJECT_STATE_REALIZING_2 ((SLuint32) 0x5) // sync realize, or async realize hook 355#define SL_OBJECT_STATE_RESUMING_1 ((SLuint32) 0x6) // async resume on work queue 356#define SL_OBJECT_STATE_RESUMING_2 ((SLuint32) 0x7) // sync resume, or async resume hook 357#define SL_OBJECT_STATE_SUSPENDING ((SLuint32) 0x8) // suspend in progress 358#define SL_OBJECT_STATE_REALIZING_1A ((SLuint32) 0x9) // abort while async realize on work queue 359#define SL_OBJECT_STATE_RESUMING_1A ((SLuint32) 0xA) // abort while async resume on work queue 360#define SL_OBJECT_STATE_DESTROYING ((SLuint32) 0xB) // destroy object when no strong references 361#ifndef ANDROID 362extern void *sync_start(void *arg); 363#endif 364extern SLresult err_to_result(int err); 365 366#ifdef __GNUC__ 367#define ctz __builtin_ctz 368#else 369extern unsigned ctz(unsigned); 370#endif 371extern const char * const interface_names[MPH_MAX]; 372#include "platform.h" 373#include "attr.h" 374#include "handlers.h" 375#include "trace.h" 376 377#ifdef USE_SNDFILE 378extern void audioPlayerTransportUpdate(CAudioPlayer *audioPlayer); 379#endif 380 381extern SLresult IBufferQueue_Enqueue(SLBufferQueueItf self, const void *pBuffer, SLuint32 size); 382extern SLresult IBufferQueue_Clear(SLBufferQueueItf self); 383extern SLresult IBufferQueue_RegisterCallback(SLBufferQueueItf self, 384 slBufferQueueCallback callback, void *pContext); 385 386extern bool IsInterfaceInitialized(IObject *thiz, unsigned MPH); 387extern SLresult AcquireStrongRef(IObject *object, SLuint32 expectedObjectID); 388extern void ReleaseStrongRef(IObject *object); 389extern void ReleaseStrongRefAndUnlockExclusive(IObject *object); 390 391extern COutputMix *CAudioPlayer_GetOutputMix(CAudioPlayer *audioPlayer); 392extern SLresult IEngineCapabilities_QueryLEDCapabilities(SLEngineCapabilitiesItf self, 393 SLuint32 *pIndex, SLuint32 *pLEDDeviceID, SLLEDDescriptor *pDescriptor); 394extern SLresult IEngineCapabilities_QueryVibraCapabilities(SLEngineCapabilitiesItf self, 395 SLuint32 *pIndex, SLuint32 *pVibraDeviceID, SLVibraDescriptor *pDescriptor); 396 397extern CEngine *theOneTrueEngine; 398extern pthread_mutex_t theOneTrueMutex; 399extern unsigned theOneTrueRefCount; 400 401extern LI_API SLresult liCreateEngine(SLObjectItf *pEngine, SLuint32 numOptions, 402 const SLEngineOption *pEngineOptions, SLuint32 numInterfaces, 403 const SLInterfaceID *pInterfaceIds, const SLboolean *pInterfaceRequired, 404 const ClassTable *pCEngine_class); 405extern LI_API SLresult liQueryNumSupportedInterfaces(SLuint32 *pNumSupportedInterfaces, 406 const ClassTable *clazz); 407extern LI_API SLresult liQuerySupportedInterfaces(SLuint32 index, SLInterfaceID *pInterfaceId, 408 const ClassTable *clazz); 409