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