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