sles_allinclusive.h revision 1fec6cc920db52e63c67eafd2034e52b8eb5780d
19066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project/*
29066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Copyright (C) 2010 The Android Open Source Project
39066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *
49066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Licensed under the Apache License, Version 2.0 (the "License");
59066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * you may not use this file except in compliance with the License.
69066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * You may obtain a copy of the License at
79066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *
89066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *      http://www.apache.org/licenses/LICENSE-2.0
99066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *
109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Unless required by applicable law or agreed to in writing, software
119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * distributed under the License is distributed on an "AS IS" BASIS,
129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * See the License for the specific language governing permissions and
149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * limitations under the License.
159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */
169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include "OpenSLES.h"
189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include <stddef.h> // offsetof
199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include <stdlib.h> // malloc
209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include <string.h> // memcmp
219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include <stdio.h>  // debugging
229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include <assert.h> // debugging
239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include <pthread.h>
249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include <unistd.h> // usleep
259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include <errno.h>
269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include "MPH.h"
289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include "MPH_to.h"
299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include "devices.h"
309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include "ThreadPool.h"
319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projecttypedef struct CAudioPlayer_struct CAudioPlayer;
339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projecttypedef struct C3DGroup_struct C3DGroup;
349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#ifdef USE_SNDFILE
369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include <sndfile.h>
379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include "SLSndFile.h"
389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#endif // USE_SNDFILE
399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#ifdef USE_SDL
419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include <SDL/SDL_audio.h>
429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#endif // USE_SDL
439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#ifdef ANDROID
459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include "media/AudioSystem.h"
469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include "media/AudioTrack.h"
479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include "media/mediaplayer.h"
489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include <utils/String8.h>
499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#define ANDROID_SL_MILLIBEL_MAX 0
509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include <binder/ProcessState.h>
519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include "android_SfPlayer.h"
529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#endif
539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#ifdef USE_OUTPUTMIXEXT
559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include "OutputMixExt.h"
569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#endif
579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// Hook functions
599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projecttypedef void (*VoidHook)(void *self);
619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projecttypedef SLresult (*StatusHook)(void *self);
629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projecttypedef SLresult (*AsyncHook)(void *self, SLboolean async);
639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// Describes how an interface is related to a given class
659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#define INTERFACE_IMPLICIT           0
679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#define INTERFACE_EXPLICIT           1
689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#define INTERFACE_OPTIONAL           2
699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#define INTERFACE_DYNAMIC            3
709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#define INTERFACE_UNAVAILABLE        4
719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#define INTERFACE_DYNAMIC_GAME       INTERFACE_DYNAMIC
729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#define INTERFACE_DYNAMIC_MUSIC      INTERFACE_DYNAMIC
739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#define INTERFACE_DYNAMIC_MUSIC_GAME INTERFACE_DYNAMIC
749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#define INTERFACE_EXPLICIT_GAME      INTERFACE_EXPLICIT
759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#define INTERFACE_GAME               INTERFACE_OPTIONAL
769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#define INTERFACE_GAME_MUSIC         INTERFACE_OPTIONAL
779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#define INTERFACE_MUSIC_GAME         INTERFACE_OPTIONAL
789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#define INTERFACE_OPTIONAL_DYNAMIC   INTERFACE_DYNAMIC
799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#define INTERFACE_PHONE_GAME         INTERFACE_OPTIONAL
809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#define INTERFACE_TBD                INTERFACE_IMPLICIT
819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// Describes how an interface is related to a given object
839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#define INTERFACE_UNINITIALIZED 1  // not requested at object creation time
859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#define INTERFACE_EXPOSED       2  // requested at object creation time
869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#define INTERFACE_ADDING_1      3  // part 1 of asynchronous AddInterface, pending
879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#define INTERFACE_ADDING_2      4  // synchronous AddInterface, or part 2 of asynchronous
889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#define INTERFACE_ADDED         5  // AddInterface has completed
899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#define INTERFACE_REMOVING      6  // unlocked phase of (synchronous) RemoveInterface
909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#define INTERFACE_SUSPENDING    7  // suspend in progress
91#define INTERFACE_SUSPENDED     8  // suspend has completed
92#define INTERFACE_RESUMING_1    9  // part 1 of asynchronous ResumeInterface, pending
93#define INTERFACE_RESUMING_2   10  // synchronous ResumeInterface, or part 2 of asynchronous
94#define INTERFACE_ADDING_1A    11  // part 1 of asynchronous AddInterface, aborted
95#define INTERFACE_RESUMING_1A  12  // part 1 of asynchronous ResumeInterface, aborted
96
97// Maps an interface ID to its offset within the class that exposes it
98
99struct iid_vtable {
100    unsigned char mMPH;
101    unsigned char mInterface;   // relationship
102    /*size_t*/ unsigned short mOffset;
103};
104
105// Per-class const data shared by all instances of the same class
106
107typedef struct {
108    const struct iid_vtable *mInterfaces;
109    SLuint32 mInterfaceCount;  // number of possible interfaces
110    const signed char *mMPH_to_index;
111    // FIXME not yet used
112    const char * const mName;
113    size_t mSize;
114    SLuint32 mObjectID;
115    // hooks
116    AsyncHook mRealize;
117    AsyncHook mResume;
118    VoidHook mDestroy;
119} ClassTable;
120
121// BufferHeader describes each element of a BufferQueue, other than the data
122
123typedef struct {
124    const void *mBuffer;
125    SLuint32 mSize;
126} BufferHeader;
127
128#ifdef USE_OUTPUTMIXEXT
129
130// stereo is a frame consisting of a pair of 16-bit PCM samples
131
132typedef struct {
133    short left;
134    short right;
135} stereo;
136
137#endif
138
139#ifdef __cplusplus
140#define this this_
141#endif
142
143#ifdef USE_SNDFILE
144
145#define SndFile_BUFSIZE 512     // in 16-bit samples
146#define SndFile_NUMBUFS 2
147
148struct SndFile {
149    // save URI also?
150    SLchar *mPathname;
151    SNDFILE *mSNDFILE;
152    // These are used when Enqueue returns SL_RESULT_BUFFER_INSUFFICIENT
153    const void *mRetryBuffer;
154    SLuint32 mRetrySize;
155    SLuint32 mWhich;    // which buffer to use next
156    short mBuffer[SndFile_BUFSIZE * SndFile_NUMBUFS];
157};
158
159#endif // USE_SNDFILE
160
161/* Our own merged version of SLDataSource and SLDataSink */
162
163typedef union {
164    SLuint32 mLocatorType;
165    SLDataLocator_Address mAddress;
166    SLDataLocator_BufferQueue mBufferQueue;
167    SLDataLocator_IODevice mIODevice;
168    SLDataLocator_MIDIBufferQueue mMIDIBufferQueue;
169    SLDataLocator_OutputMix mOutputMix;
170    SLDataLocator_URI mURI;
171} DataLocator;
172
173typedef union {
174    SLuint32 mFormatType;
175    SLDataFormat_PCM mPCM;
176    SLDataFormat_MIME mMIME;
177} DataFormat;
178
179typedef struct {
180    union {
181        SLDataSource mSource;
182        SLDataSink mSink;
183        struct {
184            DataLocator *pLocator;
185            DataFormat *pFormat;
186        } mNeutral;
187    } u;
188    DataLocator mLocator;
189    DataFormat mFormat;
190} DataLocatorFormat;
191
192/* Interface structures */
193
194typedef struct Object_interface {
195    const struct SLObjectItf_ *mItf;
196    // field mThis would be redundant within an IObject, so we substitute mEngine
197    struct Engine_interface *mEngine;
198    const ClassTable *mClass;
199    SLuint32 mInstanceID; // for debugger and for RPC
200    slObjectCallback mCallback;
201    void *mContext;
202    unsigned mGottenMask;           // interfaces which are exposed or added, and then gotten
203    unsigned mLossOfControlMask;    // interfaces with loss of control enabled
204    SLint32 mPriority;
205    pthread_mutex_t mMutex;
206    pthread_cond_t mCond;
207    SLuint8 mState;                 // really SLuint32, but SLuint8 to save space
208    SLuint8 mPreemptable;           // really SLboolean, but SLuint8 to save space
209    // for best alignment, do not add any fields here
210#define INTERFACES_Default 2
211    SLuint8 mInterfaceStates[INTERFACES_Default];    // state of each of interface
212} IObject;
213
214#include "locks.h"
215
216typedef struct {
217    const struct SL3DCommitItf_ *mItf;
218    IObject *mThis;
219    SLboolean mDeferred;
220    SLuint32 mGeneration;   // incremented each master clock cycle
221} I3DCommit;
222
223enum CartesianSphericalActive {
224    CARTESIAN_COMPUTED_SPHERICAL_SET,
225    CARTESIAN_REQUESTED_SPHERICAL_SET,
226    CARTESIAN_UNKNOWN_SPHERICAL_SET,
227    CARTESIAN_SET_SPHERICAL_COMPUTED,   // not in 1.0.1
228    CARTESIAN_SET_SPHERICAL_REQUESTED,  // not in 1.0.1
229    CARTESIAN_SET_SPHERICAL_UNKNOWN
230};
231
232typedef struct {
233    const struct SL3DDopplerItf_ *mItf;
234    IObject *mThis;
235    // The API allows client to specify either Cartesian and spherical velocities.
236    // But an implementation will likely prefer one or the other. So for
237    // maximum portablity, we maintain both units and an indication of which
238    // unit was set most recently. In addition, we keep a flag saying whether
239    // the other unit has been derived yet. It can take significant time
240    // to compute the other unit, so this may be deferred to another thread.
241    // For this reason we also keep an indication of whether the secondary
242    // has been computed yet, and its accuracy.
243    // Though only one unit is primary at a time, a union is inappropriate:
244    // the application might read in both units (not in 1.0.1),
245    // and due to multi-threading concerns.
246    SLVec3D mVelocityCartesian;
247    struct {
248        SLmillidegree mAzimuth;
249        SLmillidegree mElevation;
250        SLmillidegree mSpeed;
251    } mVelocitySpherical;
252    enum CartesianSphericalActive mVelocityActive;
253    SLpermille mDopplerFactor;
254} I3DDoppler;
255
256typedef struct {
257    const struct SL3DGroupingItf_ *mItf;
258    IObject *mThis;
259    C3DGroup *mGroup;   // link to associated group or NULL
260} I3DGrouping;
261
262enum AnglesVectorsActive {
263    ANGLES_COMPUTED_VECTORS_SET,    // not in 1.0.1
264    ANGLES_REQUESTED_VECTORS_SET,   // not in 1.0.1
265    ANGLES_UNKNOWN_VECTORS_SET,
266    ANGLES_SET_VECTORS_COMPUTED,
267    ANGLES_SET_VECTORS_REQUESTED,
268    ANGLES_SET_VECTORS_UNKNOWN
269};
270
271typedef struct {
272    const struct SL3DLocationItf_ *mItf;
273    IObject *mThis;
274    SLVec3D mLocationCartesian;
275    struct {
276        SLmillidegree mAzimuth;
277        SLmillidegree mElevation;
278        SLmillimeter mDistance;
279    } mLocationSpherical;
280    enum CartesianSphericalActive mLocationActive;
281    struct {
282        SLmillidegree mHeading;
283        SLmillidegree mPitch;
284        SLmillidegree mRoll;
285    } mOrientationAngles;
286    struct {
287        SLVec3D mFront;
288        SLVec3D mAbove;
289        SLVec3D mUp;
290    } mOrientationVectors;
291    enum AnglesVectorsActive mOrientationActive;
292    // Rotations can be slow, so are deferred.
293    SLmillidegree mTheta;
294    SLVec3D mAxis;
295    SLboolean mRotatePending;
296} I3DLocation;
297
298typedef struct {
299    const struct SL3DMacroscopicItf_ *mItf;
300    IObject *mThis;
301    struct {
302        SLmillimeter mWidth;
303        SLmillimeter mHeight;
304        SLmillimeter mDepth;
305    } mSize;
306    struct {
307        SLmillimeter mHeading;
308        SLmillimeter mPitch;
309        SLmillimeter mRoll;
310    } mOrientationAngles;
311    struct {
312        SLVec3D mFront;
313        SLVec3D mUp;
314    } mOrientationVectors;
315    enum AnglesVectorsActive mOrientationActive;
316    // Rotations can be slow, so are deferred.
317    SLmillidegree mTheta;
318    SLVec3D mAxis;
319    SLboolean mRotatePending;
320} I3DMacroscopic;
321
322typedef struct {
323    const struct SL3DSourceItf_ *mItf;
324    IObject *mThis;
325    SLboolean mHeadRelative;
326    SLboolean mRolloffMaxDistanceMute;
327    SLmillimeter mMaxDistance;
328    SLmillimeter mMinDistance;
329    SLmillidegree mConeInnerAngle;
330    SLmillidegree mConeOuterAngle;
331    SLmillibel mConeOuterLevel;
332    SLpermille mRolloffFactor;
333    SLpermille mRoomRolloffFactor;
334    SLuint8 mDistanceModel;
335} I3DSource;
336
337typedef struct {
338    const struct SLAudioDecoderCapabilitiesItf_ *mItf;
339    IObject *mThis;
340} IAudioDecoderCapabilities;
341
342typedef struct {
343    const struct SLAudioEncoderItf_ *mItf;
344    IObject *mThis;
345    SLAudioEncoderSettings mSettings;
346} IAudioEncoder;
347
348typedef struct {
349    const struct SLAudioEncoderCapabilitiesItf_ *mItf;
350    IObject *mThis;
351} IAudioEncoderCapabilities;
352
353typedef struct {
354    const struct SLAudioIODeviceCapabilitiesItf_ *mItf;
355    IObject *mThis;
356    slAvailableAudioInputsChangedCallback mAvailableAudioInputsChangedCallback;
357    void *mAvailableAudioInputsChangedContext;
358    slAvailableAudioOutputsChangedCallback mAvailableAudioOutputsChangedCallback;
359    void *mAvailableAudioOutputsChangedContext;
360    slDefaultDeviceIDMapChangedCallback mDefaultDeviceIDMapChangedCallback;
361    void *mDefaultDeviceIDMapChangedContext;
362} IAudioIODeviceCapabilities;
363
364typedef struct {
365    const struct SLBassBoostItf_ *mItf;
366    IObject *mThis;
367    SLboolean mEnabled;
368    SLpermille mStrength;
369} IBassBoost;
370
371typedef struct BufferQueue_interface {
372    const struct SLBufferQueueItf_ *mItf;
373    IObject *mThis;
374    SLBufferQueueState mState;
375    slBufferQueueCallback mCallback;
376    void *mContext;
377    SLuint32 mNumBuffers;
378    BufferHeader *mArray;
379    BufferHeader *mFront, *mRear;
380    SLuint32 mSizeConsumed;
381    // saves a malloc in the typical case
382#define BUFFER_HEADER_TYPICAL 4
383    BufferHeader mTypical[BUFFER_HEADER_TYPICAL+1];
384} IBufferQueue;
385
386#define MAX_DEVICE 2
387
388typedef struct {
389    const struct SLDeviceVolumeItf_ *mItf;
390    IObject *mThis;
391    SLint32 mVolume[MAX_DEVICE]; // FIXME Hard-coded for default in/out
392} IDeviceVolume;
393
394typedef struct {
395    const struct SLDynamicInterfaceManagementItf_ *mItf;
396    IObject *mThis;
397    slDynamicInterfaceManagementCallback mCallback;
398    void *mContext;
399} IDynamicInterfaceManagement;
400
401typedef struct {
402    const struct SLDynamicSourceItf_ *mItf;
403    IObject *mThis;
404    SLDataSource *mDataSource;
405} IDynamicSource;
406
407// private
408
409struct EnableLevel {
410    SLboolean mEnable;
411    SLmillibel mSendLevel;
412};
413
414// indexes into IEffectSend.mEnableLevels
415
416#define AUX_ENVIRONMENTALREVERB 0
417#define AUX_PRESETREVERB        1
418#define AUX_MAX                 2
419
420typedef struct {
421    const struct SLEffectSendItf_ *mItf;
422    IObject *mThis;
423    struct OutputMix_class *mOutputMix;
424    SLmillibel mDirectLevel;
425    struct EnableLevel mEnableLevels[AUX_MAX];
426} IEffectSend;
427
428// private
429
430typedef struct Engine_interface {
431    const struct SLEngineItf_ *mItf;
432    IObject *mThis;
433    SLboolean mLossOfControlGlobal;
434#ifdef USE_SDL
435    struct OutputMix_class *mOutputMix; // SDL pulls PCM from an arbitrary OutputMixExt
436#endif
437    // Each engine is its own universe.
438    SLuint32 mInstanceCount;
439    unsigned mInstanceMask; // 1 bit per active object
440#define MAX_INSTANCE 32     // see mInstanceMask
441    IObject *mInstances[MAX_INSTANCE];
442    SLboolean mShutdown;
443    ThreadPool mThreadPool; // for asynchronous operations
444} IEngine;
445
446typedef struct {
447    const struct SLEngineCapabilitiesItf_ *mItf;
448    IObject *mThis;
449    SLboolean mThreadSafe;
450    // const
451    SLuint32 mMaxIndexLED;
452    SLuint32 mMaxIndexVibra;
453} IEngineCapabilities;
454
455typedef struct {
456    const struct SLEnvironmentalReverbItf_ *mItf;
457    IObject *mThis;
458    SLEnvironmentalReverbSettings mProperties;
459} IEnvironmentalReverb;
460
461struct EqualizerBand {
462    SLmilliHertz mMin;
463    SLmilliHertz mCenter;
464    SLmilliHertz mMax;
465};
466
467#define MAX_EQ_BANDS 4  // compile-time limit, runtime limit may be smaller
468
469typedef struct {
470    const struct SLEqualizerItf_ *mItf;
471    IObject *mThis;
472    SLboolean mEnabled;
473    SLuint16 mPreset;
474    SLmillibel mLevels[MAX_EQ_BANDS];
475    // const to end of struct
476    SLuint16 mNumPresets;
477    SLuint16 mNumBands;
478    const struct EqualizerBand *mBands;
479    const struct EqualizerPreset *mPresets;
480    SLmillibel mBandLevelRangeMin;
481    SLmillibel mBandLevelRangeMax;
482} IEqualizer;
483
484#define MAX_LED_COUNT 32
485
486typedef struct {
487    const struct SLLEDArrayItf_ *mItf;
488    IObject *mThis;
489    SLuint32 mLightMask;
490    SLHSL mColors[MAX_LED_COUNT];
491    // const
492    SLuint8 mCount;
493} ILEDArray;
494
495typedef struct {
496    const struct SLMetadataExtractionItf_ *mItf;
497    IObject *mThis;
498    SLuint32 mKeySize;
499    const void *mKey;
500    SLuint32 mKeyEncoding;
501    const SLchar *mValueLangCountry;
502    SLuint32 mValueEncoding;
503    SLuint8 mFilterMask;
504    int mKeyFilter;
505} IMetadataExtraction;
506
507typedef struct {
508    const struct SLMetadataTraversalItf_ *mItf;
509    IObject *mThis;
510    SLuint32 mIndex;
511    SLuint32 mMode;
512    SLuint32 mCount;
513    SLuint32 mSize;
514} IMetadataTraversal;
515
516typedef struct {
517    const struct SLMIDIMessageItf_ *mItf;
518    IObject *mThis;
519    slMetaEventCallback mMetaEventCallback;
520    void *mMetaEventContext;
521    slMIDIMessageCallback mMessageCallback;
522    void *mMessageContext;
523    SLuint8 mMessageTypes;
524} IMIDIMessage;
525
526typedef struct {
527    const struct SLMIDIMuteSoloItf_ *mItf;
528    IObject *mThis;
529    SLuint16 mChannelMuteMask;
530    SLuint16 mChannelSoloMask;
531    SLuint32 mTrackMuteMask;
532    SLuint32 mTrackSoloMask;
533    // const
534    SLuint16 mTrackCount;
535} IMIDIMuteSolo;
536
537typedef struct {
538    const struct SLMIDITempoItf_ *mItf;
539    IObject *mThis;
540    SLuint32 mTicksPerQuarterNote;
541    SLuint32 mMicrosecondsPerQuarterNote;
542} IMIDITempo;
543
544typedef struct {
545    const struct SLMIDITimeItf_ *mItf;
546    IObject *mThis;
547    SLuint32 mDuration;
548    SLuint32 mPosition;
549    SLuint32 mStartTick;
550    SLuint32 mNumTicks;
551} IMIDITime;
552
553typedef struct {
554    const struct SLMuteSoloItf_ *mItf;
555    IObject *mThis;
556} IMuteSolo;
557
558#define MAX_TRACK 32        // see mActiveMask
559
560typedef struct {
561    const struct SLOutputMixItf_ *mItf;
562    IObject *mThis;
563    slMixDeviceChangeCallback mCallback;
564    void *mContext;
565#ifdef USE_OUTPUTMIXEXT
566    unsigned mActiveMask;   // 1 bit per active track
567    struct Track mTracks[MAX_TRACK];
568#endif
569} IOutputMix;
570
571#ifdef USE_OUTPUTMIXEXT
572typedef struct {
573    const struct SLOutputMixExtItf_ *mItf;
574    IObject *mThis;
575} IOutputMixExt;
576#endif
577
578typedef struct {
579    const struct SLPitchItf_ *mItf;
580    IObject *mThis;
581    SLpermille mPitch;
582    // const
583    SLpermille mMinPitch;
584    SLpermille mMaxPitch;
585} IPitch;
586
587typedef struct Play_interface {
588    const struct SLPlayItf_ *mItf;
589    IObject *mThis;
590    SLuint32 mState;
591    SLmillisecond mDuration;
592    SLmillisecond mPosition;
593    // unsigned mPositionSamples;  // position in sample units
594    slPlayCallback mCallback;
595    void *mContext;
596    SLuint32 mEventFlags;
597    SLmillisecond mMarkerPosition;
598    SLmillisecond mPositionUpdatePeriod;
599} IPlay;
600
601typedef struct {
602    const struct SLPlaybackRateItf_ *mItf;
603    IObject *mThis;
604    SLpermille mRate;
605    SLuint32 mProperties;
606    // const
607    SLpermille mMinRate;
608    SLpermille mMaxRate;
609    SLpermille mStepSize;
610    SLuint32 mCapabilities;
611} IPlaybackRate;
612
613typedef struct {
614    const struct SLPrefetchStatusItf_ *mItf;
615    IObject *mThis;
616    SLuint32 mStatus;
617    SLpermille mLevel;
618    slPrefetchCallback mCallback;
619    void *mContext;
620    SLuint32 mCallbackEventsMask;
621    SLpermille mFillUpdatePeriod;
622} IPrefetchStatus;
623
624typedef struct {
625    const struct SLPresetReverbItf_ *mItf;
626    IObject *mThis;
627    SLuint16 mPreset;
628} IPresetReverb;
629
630typedef struct {
631    const struct SLRatePitchItf_ *mItf;
632    IObject *mThis;
633    SLpermille mRate;
634    // const
635    SLpermille mMinRate;
636    SLpermille mMaxRate;
637} IRatePitch;
638
639typedef struct {
640    const struct SLRecordItf_ *mItf;
641    IObject *mThis;
642    SLuint32 mState;
643    SLmillisecond mDurationLimit;
644    SLmillisecond mPosition;
645    slRecordCallback mCallback;
646    void *mContext;
647    SLuint32 mCallbackEventsMask;
648    SLmillisecond mMarkerPosition;
649    SLmillisecond mPositionUpdatePeriod;
650} IRecord;
651
652typedef struct {
653    const struct SLSeekItf_ *mItf;
654    IObject *mThis;
655    SLmillisecond mPos;
656    SLboolean mLoopEnabled;
657    SLmillisecond mStartPos;
658    SLmillisecond mEndPos;
659} ISeek;
660
661typedef struct {
662    const struct SLThreadSyncItf_ *mItf;
663    IObject *mThis;
664    SLboolean mInCriticalSection;
665    SLboolean mWaiting;
666    pthread_t mOwner;
667} IThreadSync;
668
669typedef struct {
670    const struct SLVibraItf_ *mItf;
671    IObject *mThis;
672    SLboolean mVibrate;
673    SLmilliHertz mFrequency;
674    SLpermille mIntensity;
675} IVibra;
676
677typedef struct {
678    const struct SLVirtualizerItf_ *mItf;
679    IObject *mThis;
680    SLboolean mEnabled;
681    SLpermille mStrength;
682} IVirtualizer;
683
684typedef struct {
685    const struct SLVisualizationItf_ *mItf;
686    IObject *mThis;
687    slVisualizationCallback mCallback;
688    void *mContext;
689    SLmilliHertz mRate;
690} IVisualization;
691
692typedef struct {
693    const struct SLVolumeItf_ *mItf;
694    IObject *mThis;
695    SLmillibel mLevel;
696    SLboolean mMute;          // FIXME to be moved inside each object of that supports the interface
697    SLboolean mEnableStereoPosition;
698    SLpermille mStereoPosition;
699#ifdef ANDROID
700    /**
701     * Amplification (can be attenuation) factor derived for the VolumeLevel
702     */
703    float mAmplFromVolLevel;
704    /**
705     * Left/right amplification (can be attenuations) factors derived for the StereoPosition
706     */
707    float mAmplFromStereoPos[2];
708    /**
709     * Channel mask for which channels are muted
710     */
711    int mChannelMutes;
712    /**
713     * Channel mask for which channels are solo'ed
714     */
715    int mChannelSolos;
716#endif
717} IVolume;
718
719/* Class structures */
720
721/*typedef*/ struct C3DGroup_struct {
722    IObject mObject;
723#define INTERFACES_3DGroup 6 // see MPH_to_3DGroup in MPH_to.c for list of interfaces
724    SLuint8 mInterfaceStates2[INTERFACES_3DGroup - INTERFACES_Default];
725    IDynamicInterfaceManagement mDynamicInterfaceManagement;
726    I3DLocation m3DLocation;
727    I3DDoppler m3DDoppler;
728    I3DSource m3DSource;
729    I3DMacroscopic m3DMacroscopic;
730    unsigned mMemberMask;   // set of member objects
731} /*C3DGroup*/;
732
733#ifdef ANDROID
734/*
735 * Used to define the mapping from an OpenSL ES audio player to an Android
736 * media framework object
737 */
738enum AndroidObject_type {
739    INVALID_TYPE     =-1,
740    MEDIAPLAYER      = 0,
741    AUDIOTRACK_PUSH  = 1,
742    AUDIOTRACK_PULL  = 2,
743    NUM_AUDIOPLAYER_MAP_TYPES
744};
745
746enum AndroidObject_state {
747    ANDROID_UNINITIALIZED = -1,
748    ANDROID_PREPARING,
749    ANDROID_PREPARED,
750    ANDROID_PREFETCHING,
751    ANDROID_READY,
752    NUM_ANDROID_STATES
753};
754#endif
755
756/*typedef*/ struct CAudioPlayer_struct {
757    IObject mObject;
758#define INTERFACES_AudioPlayer 26 // see MPH_to_AudioPlayer in MPH_to.c for list of interfaces
759    SLuint8 mInterfaceStates2[INTERFACES_AudioPlayer - INTERFACES_Default];
760    IDynamicInterfaceManagement mDynamicInterfaceManagement;
761    IPlay mPlay;
762    I3DDoppler m3DDoppler;
763    I3DGrouping m3DGrouping;
764    I3DLocation m3DLocation;
765    I3DSource m3DSource;
766    IBufferQueue mBufferQueue;
767    IEffectSend mEffectSend;
768    IMuteSolo mMuteSolo;
769    IMetadataExtraction mMetadataExtraction;
770    IMetadataTraversal mMetadataTraversal;
771    IPrefetchStatus mPrefetchStatus;
772    IRatePitch mRatePitch;
773    ISeek mSeek;
774    IVolume mVolume;
775    // optional interfaces
776    I3DMacroscopic m3DMacroscopic;
777    IBassBoost mBassBoost;
778    IDynamicSource mDynamicSource;
779    IEnvironmentalReverb mEnvironmentalReverb;
780    IEqualizer mEqualizer;
781    IPitch mPitch;
782    IPresetReverb mPresetReverb;
783    IPlaybackRate mPlaybackRate;
784    IVirtualizer mVirtualizer;
785    IVisualization mVisualization;
786    // rest of fields are not related to the interfaces
787    DataLocatorFormat mDataSource;
788    DataLocatorFormat mDataSink;
789    // cached data for this instance
790    SLuint8 mNumChannels;
791    SLboolean mMute;
792    SLuint32 mMuteMask;
793    SLuint32 mSoloMask;
794    // implementation-specific data for this instance
795#ifdef USE_SNDFILE
796    struct SndFile mSndFile;
797#endif // USE_SNDFILE
798#ifdef ANDROID
799    android::Mutex          *mpLock;
800    enum AndroidObject_type mAndroidObjType;
801    enum AndroidObject_state mAndroidObjState;
802    android::AudioTrack *mAudioTrack;
803    android::sp<android::SfPlayer> mSfPlayer;
804    android::sp<android::ALooper>  mRenderLooper;
805#endif
806} /*CAudioPlayer*/;
807
808typedef struct {
809    // mandated interfaces
810    IObject mObject;
811#define INTERFACES_AudioRecorder 9 // see MPH_to_AudioRecorder in MPH_to.c for list of interfaces
812    SLuint8 mInterfaceContinued[INTERFACES_AudioRecorder - INTERFACES_Default];
813    IDynamicInterfaceManagement mDynamicInterfaceManagement;
814    IRecord mRecord;
815    IAudioEncoder mAudioEncoder;
816    // optional interfaces
817    IBassBoost mBassBoost;
818    IDynamicSource mDynamicSource;
819    IEqualizer mEqualizer;
820    IVisualization mVisualization;
821    IVolume mVolume;
822    // rest of fields are not related to the interfaces
823    DataLocatorFormat mDataSource;
824    DataLocatorFormat mDataSink;
825} CAudioRecorder;
826
827typedef struct {
828    // mandated implicit interfaces
829    IObject mObject;
830#define INTERFACES_Engine 10 // see MPH_to_Engine in MPH_to.c for list of interfaces
831    SLuint8 mInterfaceStates2[INTERFACES_Engine - INTERFACES_Default];
832    IDynamicInterfaceManagement mDynamicInterfaceManagement;
833    IEngine mEngine;
834    IEngineCapabilities mEngineCapabilities;
835    IThreadSync mThreadSync;
836    // mandated explicit interfaces
837    IAudioIODeviceCapabilities mAudioIODeviceCapabilities;
838    IAudioDecoderCapabilities mAudioDecoderCapabilities;
839    IAudioEncoderCapabilities mAudioEncoderCapabilities;
840    I3DCommit m3DCommit;
841    // optional interfaces
842    IDeviceVolume mDeviceVolume;
843    pthread_t mSyncThread;
844} CEngine;
845
846typedef struct {
847    // mandated interfaces
848    IObject mObject;
849#define INTERFACES_LEDDevice 3 // see MPH_to_LEDDevice in MPH_to.c for list of interfaces
850    SLuint8 mInterfaceStates2[INTERFACES_LEDDevice - INTERFACES_Default];
851    IDynamicInterfaceManagement mDynamicInterfaceManagement;
852    ILEDArray mLEDArray;
853    SLuint32 mDeviceID;
854} CLEDDevice;
855
856typedef struct {
857    // mandated interfaces
858    IObject mObject;
859#define INTERFACES_Listener 4 // see MPH_to_Listener in MPH_to.c for list of interfaces
860    SLuint8 mInterfaceStates2[INTERFACES_Listener - INTERFACES_Default];
861    IDynamicInterfaceManagement mDynamicInterfaceManagement;
862    I3DDoppler m3DDoppler;
863    I3DLocation m3DLocation;
864} CListener;
865
866typedef struct {
867    // mandated interfaces
868    IObject mObject;
869#define INTERFACES_MetadataExtractor 5 // see MPH_to_MetadataExtractor in MPH_to.c for list of interfaces
870    SLuint8 mInterfaceStates2[INTERFACES_MetadataExtractor - INTERFACES_Default];
871    IDynamicInterfaceManagement mDynamicInterfaceManagement;
872    IDynamicSource mDynamicSource;
873    IMetadataExtraction mMetadataExtraction;
874    IMetadataTraversal mMetadataTraversal;
875} CMetadataExtractor;
876
877typedef struct {
878    // mandated interfaces
879    IObject mObject;
880#define INTERFACES_MidiPlayer 29 // see MPH_to_MidiPlayer in MPH_to.c for list of interfaces
881    SLuint8 mInterfaceStates2[INTERFACES_MidiPlayer - INTERFACES_Default];
882    IDynamicInterfaceManagement mDynamicInterfaceManagement;
883    IPlay mPlay;
884    I3DDoppler m3DDoppler;
885    I3DGrouping m3DGrouping;
886    I3DLocation m3DLocation;
887    I3DSource m3DSource;
888    IBufferQueue mBufferQueue;
889    IEffectSend mEffectSend;
890    IMuteSolo mMuteSolo;
891    IMetadataExtraction mMetadataExtraction;
892    IMetadataTraversal mMetadataTraversal;
893    IMIDIMessage mMIDIMessage;
894    IMIDITime mMIDITime;
895    IMIDITempo mMIDITempo;
896    IMIDIMuteSolo mMIDIMuteSolo;
897    IPrefetchStatus mPrefetchStatus;
898    ISeek mSeek;
899    IVolume mVolume;
900    // optional interfaces
901    I3DMacroscopic m3DMacroscopic;
902    IBassBoost mBassBoost;
903    IDynamicSource mDynamicSource;
904    IEnvironmentalReverb mEnvironmentalReverb;
905    IEqualizer mEqualizer;
906    IPitch mPitch;
907    IPresetReverb mPresetReverb;
908    IPlaybackRate mPlaybackRate;
909    IVirtualizer mVirtualizer;
910    IVisualization mVisualization;
911} CMidiPlayer;
912
913typedef struct OutputMix_class {
914    // mandated interfaces
915    IObject mObject;
916#define INTERFACES_OutputMix 11 // see MPH_to_OutputMix in MPH_to.c for list of interfaces
917    SLuint8 mInterfaceStates2[INTERFACES_OutputMix - INTERFACES_Default];
918    IDynamicInterfaceManagement mDynamicInterfaceManagement;
919    IOutputMix mOutputMix;
920#ifdef USE_OUTPUTMIXEXT
921    IOutputMixExt mOutputMixExt;
922#endif
923    IEnvironmentalReverb mEnvironmentalReverb;
924    IEqualizer mEqualizer;
925    IPresetReverb mPresetReverb;
926    IVirtualizer mVirtualizer;
927    IVolume mVolume;
928    // optional interfaces
929    IBassBoost mBassBoost;
930    IVisualization mVisualization;
931} COutputMix;
932
933typedef struct {
934    // mandated interfaces
935    IObject mObject;
936#define INTERFACES_VibraDevice 3 // see MPH_to_VibraDevice in MPH_to.c for list of interfaces
937    SLuint8 mInterfaceStates2[INTERFACES_VibraDevice - INTERFACES_Default];
938    IDynamicInterfaceManagement mDynamicInterfaceManagement;
939    IVibra mVibra;
940    //
941    SLuint32 mDeviceID;
942} CVibraDevice;
943
944struct MPH_init {
945    // unsigned char mMPH;
946    VoidHook mInit;
947    VoidHook mResume;
948    VoidHook mDeinit;
949};
950
951extern /*static*/ int IID_to_MPH(const SLInterfaceID iid);
952extern /*static*/ const struct MPH_init MPH_init_table[MPH_MAX];
953extern SLresult checkInterfaces(const ClassTable *class__,
954    SLuint32 numInterfaces, const SLInterfaceID *pInterfaceIds,
955    const SLboolean *pInterfaceRequired, unsigned *pExposedMask);
956extern IObject *construct(const ClassTable *class__,
957    unsigned exposedMask, SLEngineItf engine);
958extern const ClassTable *objectIDtoClass(SLuint32 objectID);
959extern const struct SLInterfaceID_ SL_IID_array[MPH_MAX];
960extern SLuint32 IObjectToObjectID(IObject *object);
961
962// Map an interface to it's "object ID" (which is really a class ID).
963// Note: this operation is undefined on IObject, as it lacks an mThis.
964// If you have an IObject, then use IObjectToObjectID directly.
965
966#define InterfaceToObjectID(this) IObjectToObjectID((this)->mThis)
967
968// Map an interface to it's corresponding IObject.
969// Note: this operation is undefined on IObject, as it lacks an mThis.
970// If you have an IObject, then you're done -- you already have what you need.
971
972#define InterfaceToIObject(this) ((this)->mThis)
973
974#define InterfaceToCAudioPlayer(this) (((CAudioPlayer*)InterfaceToIObject(this)))
975
976#ifdef ANDROID
977#include "sles_to_android.h"
978#endif
979
980extern SLresult checkDataSource(const SLDataSource *pDataSrc, DataLocatorFormat *myDataSourceLocator);
981extern SLresult checkDataSink(const SLDataSink *pDataSink, DataLocatorFormat *myDataSinkLocator);
982extern void freeDataLocatorFormat(DataLocatorFormat *dlf);
983extern SLresult CAudioPlayer_Realize(void *self, SLboolean async);
984extern void CAudioPlayer_Destroy(void *self);
985extern SLresult CEngine_Realize(void *self, SLboolean async);
986extern void CEngine_Destroy(void *self);
987#ifdef USE_SDL
988extern void SDL_start(IEngine *thisEngine);
989#endif
990#define SL_OBJECT_STATE_REALIZING_1  ((SLuint32) 0x4) // async realize on work queue
991#define SL_OBJECT_STATE_REALIZING_2  ((SLuint32) 0x5) // sync realize, or async realize hook
992#define SL_OBJECT_STATE_RESUMING_1   ((SLuint32) 0x6) // async resume on work queue
993#define SL_OBJECT_STATE_RESUMING_2   ((SLuint32) 0x7) // sync resume, or async resume hook
994#define SL_OBJECT_STATE_SUSPENDING   ((SLuint32) 0x8) // suspend in progress
995#define SL_OBJECT_STATE_REALIZING_1A ((SLuint32) 0x9) // abort while async realize on work queue
996#define SL_OBJECT_STATE_RESUMING_1A  ((SLuint32) 0xA) // abort while async resume on work queue
997extern void *sync_start(void *arg);
998extern SLresult err_to_result(int err);
999
1000#ifdef __GNUC__
1001#define ctz __builtin_ctz
1002#else
1003extern unsigned ctz(unsigned);
1004#endif
1005extern const char * const interface_names[MPH_MAX];
1006