sles_allinclusive.h revision 262059f71a68edc5e510427c63f5f1623d3672a8
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 <stdio.h> // debugging 29#include <assert.h> // debugging 30#include <pthread.h> 31#include <unistd.h> // usleep 32#include <errno.h> 33 34#ifndef __cplusplus 35typedef int bool; 36#ifndef false 37#define false 0 38#endif 39#ifndef true 40#define true 1 41#endif 42#endif 43 44// The OpenSLES.h definitions of SL_PROFILES_... have casts, so are unusable by preprocessor 45#define USE_PROFILES_PHONE 0x1 // == SL_PROFILES_PHONE 46#define USE_PROFILES_MUSIC 0x2 // == SL_PROFILES_MUSIC 47#define USE_PROFILES_GAME 0x4 // == SL_PROFILES_GAME 48// Pseudo profiles, used to decide whether to include code for incomplete or untested features 49// Features that are not in union of all profiles: audio recorder, LED, Vibra 50#define USE_PROFILES_OPTIONAL 0x8 51// Features that are in the intersection of all profiles: 52// object priorities, preemption, loss of control, device configuration 53#define USE_PROFILES_BASE 0x10 54 55#include "MPH.h" 56#include "MPH_to.h" 57#include "devices.h" 58#include "ut/OpenSLESUT.h" 59#include "ThreadPool.h" 60 61typedef struct CEngine_struct CEngine; 62typedef struct CAudioPlayer_struct CAudioPlayer; 63typedef struct CAudioRecorder_struct CAudioRecorder; 64typedef struct C3DGroup_struct C3DGroup; 65typedef struct COutputMix_struct COutputMix; 66 67#ifdef USE_SNDFILE 68#include <sndfile.h> 69#include "desktop/SLSndFile.h" 70#endif // USE_SNDFILE 71 72#ifdef USE_SDL 73#include <SDL/SDL_audio.h> 74#endif // USE_SDL 75 76#define STEREO_CHANNELS 2 77 78#ifdef ANDROID 79#include <utils/Log.h> 80#include <utils/KeyedVector.h> 81#include "SLES/OpenSLES_AndroidConfiguration.h" 82#include "media/AudioSystem.h" 83#include "media/mediarecorder.h" 84#include "media/AudioRecord.h" 85#include "media/AudioTrack.h" 86#include "media/mediaplayer.h" 87#include <media/IStreamSource.h> 88#include "media/AudioEffect.h" 89#include "media/EffectApi.h" 90#include "media/EffectEqualizerApi.h" 91#include "media/EffectBassBoostApi.h" 92#include "media/EffectVirtualizerApi.h" 93#include "media/EffectPresetReverbApi.h" 94#include "media/EffectEnvironmentalReverbApi.h" 95#include <utils/String8.h> 96#define ANDROID_SL_MILLIBEL_MAX 0 97#include <binder/ProcessState.h> 98#include "android/android_sles_conversions.h" 99#include "android/android_OutputMix.h" 100#include "android/android_defs.h" 101#include "android/android_SfPlayer.h" 102#include "android/android_AudioRecorder.h" 103#endif 104 105#ifdef USE_OUTPUTMIXEXT 106#include "desktop/OutputMixExt.h" 107#endif 108 109#include "sllog.h" 110 111typedef enum { 112 predestroy_error, // Application should not be calling destroy now 113 predestroy_ok, // OK to destroy object now 114 predestroy_again // Application did nothing wrong, but should destroy again to be effective 115} predestroy_t; 116 117// Hook functions 118 119typedef void (*VoidHook)(void *self); 120//typedef SLresult (*ResultHook)(void *self); 121typedef SLresult (*AsyncHook)(void *self, SLboolean async); 122typedef bool (*BoolHook)(void *self); 123typedef predestroy_t (*PreDestroyHook)(void *self); 124 125// Describes how an interface is related to a given class, used in iid_vtable::mInterface 126 127#define INTERFACE_IMPLICIT 0 // no need for application to request prior to GetInterface 128#define INTERFACE_EXPLICIT 1 // must be requested explicitly during object creation 129#define INTERFACE_DYNAMIC 2 // can be requested after object creation 130#define INTERFACE_UNAVAILABLE 3 // this interface is not available on objects of this class 131#define INTERFACE_IMPLICIT_PREREALIZE 4 // implicit, and can call GetInterface before Realize 132#define INTERFACE_EXPLICIT_PREREALIZE 5 // explicit, and can call GetInterface before Realize 133// 6 and 7 are reserved for the meaningless DYNAMIC_PREREALIZE and UNAVAILABLE_PREREALIZE 134// note that INTERFACE_OPTIONAL is always re-mapped to one of the above 135#define INTERFACE_PREREALIZE 4 // bit-mask to test for calling GetInterface before Realize 136 137// Profile-specific interfaces 138 139#if USE_PROFILES & USE_PROFILES_BASE 140#define INTERFACE_IMPLICIT_BASE INTERFACE_IMPLICIT 141#define INTERFACE_EXPLICIT_BASE INTERFACE_EXPLICIT 142#else 143#define INTERFACE_IMPLICIT_BASE INTERFACE_UNAVAILABLE 144#define INTERFACE_EXPLICIT_BASE INTERFACE_UNAVAILABLE 145#endif 146 147#if USE_PROFILES & USE_PROFILES_GAME 148#define INTERFACE_DYNAMIC_GAME INTERFACE_DYNAMIC 149#define INTERFACE_EXPLICIT_GAME INTERFACE_EXPLICIT 150#else 151#define INTERFACE_DYNAMIC_GAME INTERFACE_OPTIONAL 152#define INTERFACE_EXPLICIT_GAME INTERFACE_OPTIONAL 153#endif 154 155#if USE_PROFILES & USE_PROFILES_MUSIC 156#define INTERFACE_DYNAMIC_MUSIC INTERFACE_DYNAMIC 157#else 158#define INTERFACE_DYNAMIC_MUSIC INTERFACE_OPTIONAL 159#endif 160 161#if USE_PROFILES & (USE_PROFILES_GAME | USE_PROFILES_MUSIC) 162#define INTERFACE_DYNAMIC_GAME_MUSIC INTERFACE_DYNAMIC 163#define INTERFACE_EXPLICIT_GAME_MUSIC INTERFACE_EXPLICIT 164#else 165#define INTERFACE_DYNAMIC_GAME_MUSIC INTERFACE_OPTIONAL 166#define INTERFACE_EXPLICIT_GAME_MUSIC INTERFACE_OPTIONAL 167#endif 168 169#if USE_PROFILES & (USE_PROFILES_GAME | USE_PROFILES_PHONE) 170#define INTERFACE_EXPLICIT_GAME_PHONE INTERFACE_EXPLICIT 171#else 172#define INTERFACE_EXPLICIT_GAME_PHONE INTERFACE_OPTIONAL 173#endif 174 175#if USE_PROFILES & USE_PROFILES_OPTIONAL 176#define INTERFACE_OPTIONAL INTERFACE_EXPLICIT 177#define INTERFACE_DYNAMIC_OPTIONAL INTERFACE_DYNAMIC 178#else 179#define INTERFACE_OPTIONAL INTERFACE_UNAVAILABLE 180#define INTERFACE_DYNAMIC_OPTIONAL INTERFACE_UNAVAILABLE 181#endif 182 183// Describes how an interface is related to a given object 184 185#define INTERFACE_UNINITIALIZED 0 ///< not available 186#define INTERFACE_INITIALIZED 1 ///< not requested at object creation time 187#define INTERFACE_EXPOSED 2 ///< requested at object creation time 188#define INTERFACE_ADDING_1 3 ///< part 1 of asynchronous AddInterface, pending 189#define INTERFACE_ADDING_2 4 ///< synchronous AddInterface, or part 2 of asynchronous 190#define INTERFACE_ADDED 5 ///< AddInterface has completed 191#define INTERFACE_REMOVING 6 ///< unlocked phase of (synchronous) RemoveInterface 192#define INTERFACE_SUSPENDING 7 ///< suspend in progress 193#define INTERFACE_SUSPENDED 8 ///< suspend has completed 194#define INTERFACE_RESUMING_1 9 ///< part 1 of asynchronous ResumeInterface, pending 195#define INTERFACE_RESUMING_2 10 ///< synchronous ResumeInterface, or part 2 of asynchronous 196#define INTERFACE_ADDING_1A 11 ///< part 1 of asynchronous AddInterface, aborted 197#define INTERFACE_RESUMING_1A 12 ///< part 1 of asynchronous ResumeInterface, aborted 198 199 200// Maps an interface ID to its offset within the class that exposes it 201 202struct iid_vtable { 203 unsigned char mMPH; // primary MPH for this interface, does not include any aliases 204 unsigned char mInterface; // relationship of interface to this class 205 /*size_t*/ unsigned short mOffset; 206}; 207 208// Per-class const data shared by all instances of the same class 209 210typedef struct { 211 const struct iid_vtable *mInterfaces; // maps interface index to info about that interface 212 SLuint32 mInterfaceCount; // number of possible interfaces 213 const signed char *mMPH_to_index; 214 const char * const mName; 215 size_t mSize; 216 // OpenSL ES and OpenMAX AL object IDs come from different ranges, and some objects such as 217 // Engine, Output Mix, LED, and Vibra belong to both APIs, so we keep both object IDs 218 SLuint16 mSLObjectID; // OpenSL ES object ID 219 XAuint16 mXAObjectID; // OpenMAX AL object ID 220 // hooks 221 AsyncHook mRealize; 222 AsyncHook mResume; 223 VoidHook mDestroy; 224 PreDestroyHook mPreDestroy; 225} ClassTable; 226 227// BufferHeader describes each element of a BufferQueue, other than the data 228 229typedef struct { 230 const void *mBuffer; 231 SLuint32 mSize; 232} BufferHeader; 233 234#ifdef __cplusplus 235#define this this_ 236#endif 237 238#ifdef USE_SNDFILE 239 240#define SndFile_BUFSIZE 512 // in 16-bit samples 241#define SndFile_NUMBUFS 2 242 243struct SndFile { 244 // save URI also? 245 SLchar *mPathname; 246 SNDFILE *mSNDFILE; 247 SF_INFO mSfInfo; 248 pthread_mutex_t mMutex; // protects mSNDFILE only 249 SLboolean mEOF; // sf_read returned zero sample frames 250 SLuint32 mWhich; // which buffer to use next 251 short mBuffer[SndFile_BUFSIZE * SndFile_NUMBUFS]; 252}; 253 254#endif // USE_SNDFILE 255 256#include "data.h" 257#include "itfstruct.h" 258 259#ifdef ANDROID 260 261#ifdef ANDROID 262// FIXME this include is done here so the effect structures and enums have been defined. Messy. 263#include "android/android_Effect.h" 264#include "android/android_AVPlayer.h" 265#include "android/android_StreamPlayer.h" 266#include "android/android_LocAVPlayer.h" 267#endif 268 269#endif // ANDROID 270 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 *class__, 286 SLuint32 numInterfaces, const SLInterfaceID *pInterfaceIds, 287 const SLboolean *pInterfaceRequired, unsigned *pExposedMask); 288extern IObject *construct(const ClassTable *class__, 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 *this); 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(this) IObjectToObjectID((this)->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(this) ((this)->mThis) 307 308#define InterfaceToCAudioPlayer(this) (((CAudioPlayer*)InterfaceToIObject(this))) 309 310#define InterfaceToCAudioRecorder(this) (((CAudioRecorder*)InterfaceToIObject(this))) 311 312#ifdef ANDROID 313#include "android/android_Player.h" 314#include "android/android_AudioPlayer.h" 315#endif 316 317extern predestroy_t C3DGroup_PreDestroy(void *self); 318 319extern SLresult CAudioPlayer_Realize(void *self, SLboolean async); 320extern SLresult CAudioPlayer_Resume(void *self, SLboolean async); 321extern void CAudioPlayer_Destroy(void *self); 322extern predestroy_t CAudioPlayer_PreDestroy(void *self); 323 324extern SLresult CAudioRecorder_Realize(void *self, SLboolean async); 325extern SLresult CAudioRecorder_Resume(void *self, SLboolean async); 326extern void CAudioRecorder_Destroy(void *self); 327extern predestroy_t CAudioRecorder_PreDestroy(void *self); 328 329extern SLresult CEngine_Realize(void *self, SLboolean async); 330extern SLresult CEngine_Resume(void *self, SLboolean async); 331extern void CEngine_Destroy(void *self); 332extern predestroy_t CEngine_PreDestroy(void *self); 333extern void CEngine_Destroyed(CEngine *self); 334 335extern SLresult COutputMix_Realize(void *self, SLboolean async); 336extern SLresult COutputMix_Resume(void *self, SLboolean async); 337extern void COutputMix_Destroy(void *self); 338extern predestroy_t COutputMix_PreDestroy(void *self); 339 340extern SLresult CMediaPlayer_Realize(void *self, SLboolean async); 341extern SLresult CMediaPlayer_Resume(void *self, SLboolean async); 342extern void CMediaPlayer_Destroy(void *self); 343extern predestroy_t CMediaPlayer_PreDestroy(void *self); 344 345#ifdef USE_SDL 346extern void SDL_open(IEngine *thisEngine); 347extern void SDL_close(void); 348#endif 349#define SL_OBJECT_STATE_REALIZING_1 ((SLuint32) 0x4) // async realize on work queue 350#define SL_OBJECT_STATE_REALIZING_2 ((SLuint32) 0x5) // sync realize, or async realize hook 351#define SL_OBJECT_STATE_RESUMING_1 ((SLuint32) 0x6) // async resume on work queue 352#define SL_OBJECT_STATE_RESUMING_2 ((SLuint32) 0x7) // sync resume, or async resume hook 353#define SL_OBJECT_STATE_SUSPENDING ((SLuint32) 0x8) // suspend in progress 354#define SL_OBJECT_STATE_REALIZING_1A ((SLuint32) 0x9) // abort while async realize on work queue 355#define SL_OBJECT_STATE_RESUMING_1A ((SLuint32) 0xA) // abort while async resume on work queue 356#define SL_OBJECT_STATE_DESTROYING ((SLuint32) 0xB) // destroy object when no strong references 357#ifndef ANDROID 358extern void *sync_start(void *arg); 359#endif 360extern SLresult err_to_result(int err); 361 362#ifdef __GNUC__ 363#define ctz __builtin_ctz 364#else 365extern unsigned ctz(unsigned); 366#endif 367extern const char * const interface_names[MPH_MAX]; 368#include "platform.h" 369 370// Attributes 371 372#define ATTR_NONE ((unsigned) 0x0) // none 373#define ATTR_GAIN ((unsigned) 0x1 << 0) // player volume, channel mute, channel solo, 374 // player stereo position, player mute 375#define ATTR_TRANSPORT ((unsigned) 0x1 << 1) // play state, looping 376#define ATTR_POSITION ((unsigned) 0x1 << 2) // requested position (a.k.a. seek position) 377#define ATTR_ENQUEUE ((unsigned) 0x1 << 3) // buffer queue became non-empty and in playing state 378 379#include "trace.h" 380 381#ifdef USE_SNDFILE 382extern void audioPlayerTransportUpdate(CAudioPlayer *audioPlayer); 383#endif 384 385extern SLresult IBufferQueue_Enqueue(SLBufferQueueItf self, const void *pBuffer, SLuint32 size); 386extern SLresult IBufferQueue_Clear(SLBufferQueueItf self); 387extern SLresult IBufferQueue_RegisterCallback(SLBufferQueueItf self, 388 slBufferQueueCallback callback, void *pContext); 389 390extern bool IsInterfaceInitialized(IObject *this, unsigned MPH); 391extern SLresult AcquireStrongRef(IObject *object, SLuint32 expectedObjectID); 392extern void ReleaseStrongRef(IObject *object); 393extern void ReleaseStrongRefAndUnlockExclusive(IObject *object); 394 395extern COutputMix *CAudioPlayer_GetOutputMix(CAudioPlayer *audioPlayer); 396extern SLresult IEngineCapabilities_QueryLEDCapabilities(SLEngineCapabilitiesItf self, 397 SLuint32 *pIndex, SLuint32 *pLEDDeviceID, SLLEDDescriptor *pDescriptor); 398extern SLresult IEngineCapabilities_QueryVibraCapabilities(SLEngineCapabilitiesItf self, 399 SLuint32 *pIndex, SLuint32 *pVibraDeviceID, SLVibraDescriptor *pDescriptor); 400 401extern CEngine *theOneTrueEngine; 402extern pthread_mutex_t theOneTrueMutex; 403extern unsigned theOneTrueRefCount; 404