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