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#ifdef ANDROID
21#include "SLES/OpenSLES_Android.h"
22#endif
23#include <stddef.h> // offsetof
24#include <stdlib.h> // malloc
25#include <string.h> // memcmp
26#include <stdio.h>  // debugging
27#include <assert.h> // debugging
28#include <pthread.h>
29#include <unistd.h> // usleep
30#include <errno.h>
31
32#ifndef __cplusplus
33typedef int bool;
34#ifndef false
35#define false 0
36#endif
37#ifndef true
38#define true 1
39#endif
40#endif
41
42// The OpenSLES.h definitions of SL_PROFILES_... have casts, so are unusable by preprocessor
43#define USE_PROFILES_PHONE    0x1   // == SL_PROFILES_PHONE
44#define USE_PROFILES_MUSIC    0x2   // == SL_PROFILES_MUSIC
45#define USE_PROFILES_GAME     0x4   // == SL_PROFILES_GAME
46// Pseudo profiles, used to decide whether to include code for incomplete or untested features
47// Features that are not in union of all profiles: audio recorder, LED, Vibra
48#define USE_PROFILES_OPTIONAL 0x8
49// Features that are in the intersection of all profiles:
50// object priorities, preemption, loss of control, device configuration
51#define USE_PROFILES_BASE     0x10
52
53#include "MPH.h"
54#include "MPH_to.h"
55#include "devices.h"
56#include "OpenSLESUT.h"
57#include "ThreadPool.h"
58
59typedef struct CAudioPlayer_struct CAudioPlayer;
60typedef struct CAudioRecorder_struct CAudioRecorder;
61typedef struct C3DGroup_struct C3DGroup;
62typedef struct COutputMix_struct COutputMix;
63
64#ifdef USE_SNDFILE
65#include <sndfile.h>
66#include "SLSndFile.h"
67#endif // USE_SNDFILE
68
69#ifdef USE_SDL
70#include <SDL/SDL_audio.h>
71#endif // USE_SDL
72
73#ifdef ANDROID
74#include <utils/Log.h>
75#include <utils/KeyedVector.h>
76#include "SLES/OpenSLES_AndroidConfiguration.h"
77#include "media/AudioSystem.h"
78#include "media/mediarecorder.h"
79#include "media/AudioRecord.h"
80#include "media/AudioTrack.h"
81#include "media/mediaplayer.h"
82#ifndef USE_BACKPORT
83#include "media/AudioEffect.h"
84#include "media/EffectApi.h"
85#include "media/EffectEqualizerApi.h"
86#include "media/EffectBassBoostApi.h"
87#include "media/EffectVirtualizerApi.h"
88#include "media/EffectPresetReverbApi.h"
89#include "media/EffectEnvironmentalReverbApi.h"
90#endif
91#include <utils/String8.h>
92#define ANDROID_SL_MILLIBEL_MAX 0
93#include <binder/ProcessState.h>
94#include "android_sles_conversions.h"
95#ifndef USE_BACKPORT
96#include "android_SfPlayer.h"
97#include "android_OutputMix.h"
98#endif
99#include "android_AudioRecorder.h"
100#endif
101
102#define STEREO_CHANNELS 2
103
104#ifdef USE_OUTPUTMIXEXT
105#include "OutputMixExt.h"
106#endif
107
108#include "sllog.h"
109
110// Hook functions
111
112typedef void (*VoidHook)(void *self);
113//typedef SLresult (*ResultHook)(void *self);
114typedef SLresult (*AsyncHook)(void *self, SLboolean async);
115typedef bool (*BoolHook)(void *self);
116
117// Describes how an interface is related to a given class, used in iid_vtable::mInterface
118
119#define INTERFACE_IMPLICIT            0 // no need for application to request prior to GetInterface
120#define INTERFACE_EXPLICIT            1 // must be requested explicitly during object creation
121#define INTERFACE_DYNAMIC             2 // can be requested after object creation
122#define INTERFACE_UNAVAILABLE         3 // this interface is not available on objects of this class
123#define INTERFACE_IMPLICIT_PREREALIZE 4 // implicit, and can call GetInterface before Realize
124#define INTERFACE_EXPLICIT_PREREALIZE 5 // explicit, and can call GetInterface before Realize
125// 6 and 7 are reserved for the meaningless DYNAMIC_PREREALIZE and UNAVAILABLE_PREREALIZE
126// note that INTERFACE_OPTIONAL is always re-mapped to one of the above
127#define INTERFACE_PREREALIZE          4 // bit-mask to test for calling GetInterface before Realize
128
129// Profile-specific interfaces
130
131#if USE_PROFILES & USE_PROFILES_BASE
132#define INTERFACE_IMPLICIT_BASE       INTERFACE_IMPLICIT
133#define INTERFACE_EXPLICIT_BASE       INTERFACE_EXPLICIT
134#else
135#define INTERFACE_IMPLICIT_BASE       INTERFACE_UNAVAILABLE
136#define INTERFACE_EXPLICIT_BASE       INTERFACE_UNAVAILABLE
137#endif
138
139#if USE_PROFILES & USE_PROFILES_GAME
140#define INTERFACE_DYNAMIC_GAME        INTERFACE_DYNAMIC
141#define INTERFACE_EXPLICIT_GAME       INTERFACE_EXPLICIT
142#else
143#define INTERFACE_DYNAMIC_GAME        INTERFACE_OPTIONAL
144#define INTERFACE_EXPLICIT_GAME       INTERFACE_OPTIONAL
145#endif
146
147#if USE_PROFILES & USE_PROFILES_MUSIC
148#define INTERFACE_DYNAMIC_MUSIC       INTERFACE_DYNAMIC
149#else
150#define INTERFACE_DYNAMIC_MUSIC       INTERFACE_OPTIONAL
151#endif
152
153#if USE_PROFILES & (USE_PROFILES_GAME | USE_PROFILES_MUSIC)
154#define INTERFACE_DYNAMIC_GAME_MUSIC  INTERFACE_DYNAMIC
155#define INTERFACE_EXPLICIT_GAME_MUSIC INTERFACE_EXPLICIT
156#else
157#define INTERFACE_DYNAMIC_GAME_MUSIC  INTERFACE_OPTIONAL
158#define INTERFACE_EXPLICIT_GAME_MUSIC INTERFACE_OPTIONAL
159#endif
160
161#if USE_PROFILES & (USE_PROFILES_GAME | USE_PROFILES_PHONE)
162#define INTERFACE_EXPLICIT_GAME_PHONE INTERFACE_EXPLICIT
163#else
164#define INTERFACE_EXPLICIT_GAME_PHONE INTERFACE_OPTIONAL
165#endif
166
167#if USE_PROFILES & USE_PROFILES_OPTIONAL
168#define INTERFACE_OPTIONAL            INTERFACE_EXPLICIT
169#define INTERFACE_DYNAMIC_OPTIONAL    INTERFACE_DYNAMIC
170#else
171#define INTERFACE_OPTIONAL            INTERFACE_UNAVAILABLE
172#define INTERFACE_DYNAMIC_OPTIONAL    INTERFACE_UNAVAILABLE
173#endif
174
175// Describes how an interface is related to a given object
176
177#define INTERFACE_UNINITIALIZED 0  ///< not available
178#define INTERFACE_INITIALIZED   1  ///< not requested at object creation time
179#define INTERFACE_EXPOSED       2  ///< requested at object creation time
180#define INTERFACE_ADDING_1      3  ///< part 1 of asynchronous AddInterface, pending
181#define INTERFACE_ADDING_2      4  ///< synchronous AddInterface, or part 2 of asynchronous
182#define INTERFACE_ADDED         5  ///< AddInterface has completed
183#define INTERFACE_REMOVING      6  ///< unlocked phase of (synchronous) RemoveInterface
184#define INTERFACE_SUSPENDING    7  ///< suspend in progress
185#define INTERFACE_SUSPENDED     8  ///< suspend has completed
186#define INTERFACE_RESUMING_1    9  ///< part 1 of asynchronous ResumeInterface, pending
187#define INTERFACE_RESUMING_2   10  ///< synchronous ResumeInterface, or part 2 of asynchronous
188#define INTERFACE_ADDING_1A    11  ///< part 1 of asynchronous AddInterface, aborted
189#define INTERFACE_RESUMING_1A  12  ///< part 1 of asynchronous ResumeInterface, aborted
190
191
192// Maps an interface ID to its offset within the class that exposes it
193
194struct iid_vtable {
195    unsigned char mMPH;         // primary MPH for this interface, does not include any aliases
196    unsigned char mInterface;   // relationship of interface to this class
197    /*size_t*/ unsigned short mOffset;
198};
199
200// Per-class const data shared by all instances of the same class
201
202typedef struct {
203    const struct iid_vtable *mInterfaces;   // maps interface index to info about that interface
204    SLuint32 mInterfaceCount;  // number of possible interfaces
205    const signed char *mMPH_to_index;
206    const char * const mName;
207    size_t mSize;
208    SLuint32 mObjectID;
209    // hooks
210    AsyncHook mRealize;
211    AsyncHook mResume;
212    VoidHook mDestroy;
213    BoolHook mPreDestroy;
214} ClassTable;
215
216// BufferHeader describes each element of a BufferQueue, other than the data
217
218typedef struct {
219    const void *mBuffer;
220    SLuint32 mSize;
221} BufferHeader;
222
223#ifdef __cplusplus
224#define this this_
225#endif
226
227#ifdef USE_SNDFILE
228
229#define SndFile_BUFSIZE 512     // in 16-bit samples
230#define SndFile_NUMBUFS 2
231
232struct SndFile {
233    // save URI also?
234    SLchar *mPathname;
235    SNDFILE *mSNDFILE;
236    SF_INFO mSfInfo;
237    pthread_mutex_t mMutex; // protects mSNDFILE only
238    SLboolean mEOF;         // sf_read returned zero sample frames
239    SLuint32 mWhich;        // which buffer to use next
240    short mBuffer[SndFile_BUFSIZE * SndFile_NUMBUFS];
241};
242
243#endif // USE_SNDFILE
244
245/* Our own merged version of SLDataSource and SLDataSink */
246
247typedef union {
248    SLuint32 mLocatorType;
249    SLDataLocator_Address mAddress;
250    SLDataLocator_BufferQueue mBufferQueue;
251    SLDataLocator_IODevice mIODevice;
252    SLDataLocator_MIDIBufferQueue mMIDIBufferQueue;
253    SLDataLocator_OutputMix mOutputMix;
254    SLDataLocator_URI mURI;
255#ifdef ANDROID
256    SLDataLocator_AndroidFD mFD;
257#endif
258} DataLocator;
259
260typedef union {
261    SLuint32 mFormatType;
262    SLDataFormat_PCM mPCM;
263    SLDataFormat_MIME mMIME;
264} DataFormat;
265
266typedef struct {
267    union {
268        SLDataSource mSource;
269        SLDataSink mSink;
270        struct {
271            DataLocator *pLocator;
272            DataFormat *pFormat;
273        } mNeutral;
274    } u;
275    DataLocator mLocator;
276    DataFormat mFormat;
277} DataLocatorFormat;
278
279/* Interface structures */
280
281typedef struct Object_interface {
282    const struct SLObjectItf_ *mItf;    // const
283    // field mThis would be redundant within an IObject, so we substitute mEngine
284    struct Engine_interface *mEngine;   // const
285    const ClassTable *mClass;       // const
286    SLuint32 mInstanceID;           // const for debugger and for RPC, 0 means unpublished
287    slObjectCallback mCallback;
288    void *mContext;
289    unsigned mGottenMask;           ///< bit-mask of interfaces exposed or added, then gotten
290    unsigned mLossOfControlMask;    // interfaces with loss of control enabled
291    unsigned mAttributesMask;       // attributes which have changed since last sync
292#if USE_PROFILES & USE_PROFILES_BASE
293    SLint32 mPriority;
294#endif
295    pthread_mutex_t mMutex;
296#ifdef USE_DEBUG
297    pthread_t mOwner;
298    const char *mFile;
299    int mLine;
300#endif
301    pthread_cond_t mCond;
302    SLuint8 mState;                 // really SLuint32, but SLuint8 to save space
303#if USE_PROFILES & USE_PROFILES_BASE
304    SLuint8 mPreemptable;           // really SLboolean, but SLuint8 to save space
305#else
306    SLuint8 mPadding;
307#endif
308    SLuint8 mStrongRefCount;        // number of strong references to this object
309    // (object cannot be destroyed as long as > 0, and referrers _prefer_ it stay in Realized state)
310    // for best alignment, do not add any fields here
311#define INTERFACES_Default 1
312    SLuint8 mInterfaceStates[INTERFACES_Default];    // state of each of interface
313    // do not add any fields here
314} IObject;
315
316#include "locks.h"
317
318typedef struct {
319    const struct SL3DCommitItf_ *mItf;
320    IObject *mThis;
321    SLboolean mDeferred;
322    SLuint32 mGeneration;   // incremented each master clock cycle
323    SLuint32 mWaiting;      // number of threads waiting in Commit
324} I3DCommit;
325
326enum CartesianSphericalActive {
327    CARTESIAN_COMPUTED_SPHERICAL_SET,
328    CARTESIAN_REQUESTED_SPHERICAL_SET,
329    CARTESIAN_UNKNOWN_SPHERICAL_SET,
330    CARTESIAN_SET_SPHERICAL_COMPUTED,   // not in 1.0.1
331    CARTESIAN_SET_SPHERICAL_REQUESTED,  // not in 1.0.1
332    CARTESIAN_SET_SPHERICAL_UNKNOWN
333};
334
335typedef struct {
336    const struct SL3DDopplerItf_ *mItf;
337    IObject *mThis;
338    // The API allows client to specify either Cartesian and spherical velocities.
339    // But an implementation will likely prefer one or the other. So for
340    // maximum portablity, we maintain both units and an indication of which
341    // unit was set most recently. In addition, we keep a flag saying whether
342    // the other unit has been derived yet. It can take significant time
343    // to compute the other unit, so this may be deferred to another thread.
344    // For this reason we also keep an indication of whether the secondary
345    // has been computed yet, and its accuracy.
346    // Though only one unit is primary at a time, a union is inappropriate:
347    // the application might read in both units (not in 1.0.1),
348    // and due to multi-threading concerns.
349    SLVec3D mVelocityCartesian;
350    struct {
351        SLmillidegree mAzimuth;
352        SLmillidegree mElevation;
353        SLmillidegree mSpeed;
354    } mVelocitySpherical;
355    enum CartesianSphericalActive mVelocityActive;
356    SLpermille mDopplerFactor;
357} I3DDoppler;
358
359typedef struct {
360    const struct SL3DGroupingItf_ *mItf;
361    IObject *mThis;
362    C3DGroup *mGroup;   // strong reference to associated group or NULL
363} I3DGrouping;
364
365enum AnglesVectorsActive {
366    ANGLES_COMPUTED_VECTORS_SET,    // not in 1.0.1
367    ANGLES_REQUESTED_VECTORS_SET,   // not in 1.0.1
368    ANGLES_UNKNOWN_VECTORS_SET,
369    ANGLES_SET_VECTORS_COMPUTED,
370    ANGLES_SET_VECTORS_REQUESTED,
371    ANGLES_SET_VECTORS_UNKNOWN
372};
373
374typedef struct {
375    const struct SL3DLocationItf_ *mItf;
376    IObject *mThis;
377    SLVec3D mLocationCartesian;
378    struct {
379        SLmillidegree mAzimuth;
380        SLmillidegree mElevation;
381        SLmillimeter mDistance;
382    } mLocationSpherical;
383    enum CartesianSphericalActive mLocationActive;
384    struct {
385        SLmillidegree mHeading;
386        SLmillidegree mPitch;
387        SLmillidegree mRoll;
388    } mOrientationAngles;
389    struct {
390        SLVec3D mFront;
391        SLVec3D mAbove;
392        SLVec3D mUp;
393    } mOrientationVectors;
394    enum AnglesVectorsActive mOrientationActive;
395    // Rotations can be slow, so are deferred.
396    SLmillidegree mTheta;
397    SLVec3D mAxis;
398    SLboolean mRotatePending;
399} I3DLocation;
400
401typedef struct {
402    const struct SL3DMacroscopicItf_ *mItf;
403    IObject *mThis;
404    struct {
405        SLmillimeter mWidth;
406        SLmillimeter mHeight;
407        SLmillimeter mDepth;
408    } mSize;
409    struct {
410        SLmillimeter mHeading;
411        SLmillimeter mPitch;
412        SLmillimeter mRoll;
413    } mOrientationAngles;
414    struct {
415        SLVec3D mFront;
416        SLVec3D mAbove;
417        SLVec3D mUp;
418    } mOrientationVectors;
419    enum AnglesVectorsActive mOrientationActive;
420    // Rotations can be slow, so are deferred.
421    SLmillidegree mTheta;
422    SLVec3D mAxis;
423    SLboolean mRotatePending;
424} I3DMacroscopic;
425
426typedef struct {
427    const struct SL3DSourceItf_ *mItf;
428    IObject *mThis;
429    SLboolean mHeadRelative;
430    SLboolean mRolloffMaxDistanceMute;
431    SLmillimeter mMaxDistance;
432    SLmillimeter mMinDistance;
433    SLmillidegree mConeInnerAngle;
434    SLmillidegree mConeOuterAngle;
435    SLmillibel mConeOuterLevel;
436    SLpermille mRolloffFactor;
437    SLpermille mRoomRolloffFactor;
438    SLuint8 mDistanceModel;
439} I3DSource;
440
441typedef struct {
442    const struct SLAudioDecoderCapabilitiesItf_ *mItf;
443    IObject *mThis;
444} IAudioDecoderCapabilities;
445
446typedef struct {
447    const struct SLAudioEncoderItf_ *mItf;
448    IObject *mThis;
449    SLAudioEncoderSettings mSettings;
450} IAudioEncoder;
451
452typedef struct {
453    const struct SLAudioEncoderCapabilitiesItf_ *mItf;
454    IObject *mThis;
455} IAudioEncoderCapabilities;
456
457typedef struct {
458    const struct SLAudioIODeviceCapabilitiesItf_ *mItf;
459    IObject *mThis;
460    slAvailableAudioInputsChangedCallback mAvailableAudioInputsChangedCallback;
461    void *mAvailableAudioInputsChangedContext;
462    slAvailableAudioOutputsChangedCallback mAvailableAudioOutputsChangedCallback;
463    void *mAvailableAudioOutputsChangedContext;
464    slDefaultDeviceIDMapChangedCallback mDefaultDeviceIDMapChangedCallback;
465    void *mDefaultDeviceIDMapChangedContext;
466} IAudioIODeviceCapabilities;
467
468typedef struct {
469    const struct SLBassBoostItf_ *mItf;
470    IObject *mThis;
471    SLboolean mEnabled;
472    SLpermille mStrength;
473#if defined(ANDROID) && !defined(USE_BACKPORT)
474    effect_descriptor_t mBassBoostDescriptor;
475    android::sp<android::AudioEffect> mBassBoostEffect;
476#endif
477} IBassBoost;
478
479typedef struct BufferQueue_interface {
480    const struct SLBufferQueueItf_ *mItf;
481    IObject *mThis;
482    SLBufferQueueState mState;
483    slBufferQueueCallback mCallback;
484    void *mContext;
485    // originally SLuint32, but range-checked down to SLuint16
486    SLuint16 mNumBuffers;
487    /*SLboolean*/ SLuint16 mClearRequested;
488    BufferHeader *mArray;
489    BufferHeader *mFront, *mRear;
490#ifdef ANDROID
491    SLuint32 mSizeConsumed;
492#endif
493    // saves a malloc in the typical case
494#define BUFFER_HEADER_TYPICAL 4
495    BufferHeader mTypical[BUFFER_HEADER_TYPICAL+1];
496} IBufferQueue;
497
498#define MAX_DEVICE 2    // hard-coded array size for default in/out
499
500typedef struct {
501    const struct SLDeviceVolumeItf_ *mItf;
502    IObject *mThis;
503    SLint32 mVolume[MAX_DEVICE];
504} IDeviceVolume;
505
506typedef struct {
507    const struct SLDynamicInterfaceManagementItf_ *mItf;
508    IObject *mThis;
509    slDynamicInterfaceManagementCallback mCallback;
510    void *mContext;
511} IDynamicInterfaceManagement;
512
513typedef struct {
514    const struct SLDynamicSourceItf_ *mItf;
515    IObject *mThis;
516    SLDataSource *mDataSource;
517} IDynamicSource;
518
519// private
520
521struct EnableLevel {
522    SLboolean mEnable;
523    SLmillibel mSendLevel;
524};
525
526// indexes into IEffectSend.mEnableLevels
527
528#define AUX_ENVIRONMENTALREVERB 0
529#define AUX_PRESETREVERB        1
530#define AUX_MAX                 2
531
532typedef struct {
533    const struct SLEffectSendItf_ *mItf;
534    IObject *mThis;
535    struct EnableLevel mEnableLevels[AUX_MAX];  // wet enable and volume per effect type
536} IEffectSend;
537
538typedef struct Engine_interface {
539    const struct SLEngineItf_ *mItf;
540    IObject *mThis;
541    SLboolean mLossOfControlGlobal;
542#ifdef USE_SDL
543    COutputMix *mOutputMix; // SDL pulls PCM from an arbitrary IOutputMixExt
544#endif
545    // Each engine is its own universe.
546    SLuint32 mInstanceCount;
547    unsigned mInstanceMask; // 1 bit per active object
548    unsigned mChangedMask;  // objects which have changed since last sync
549#define MAX_INSTANCE 32     // maximum active objects per engine, see mInstanceMask
550    IObject *mInstances[MAX_INSTANCE];
551    SLboolean mShutdown;
552    SLboolean mShutdownAck;
553    ThreadPool mThreadPool; // for asynchronous operations
554#if defined(ANDROID) && !defined(USE_BACKPORT)
555    // FIXME number of presets will only be saved in IEqualizer, preset names will not be stored
556    SLuint32 mEqNumPresets;
557    char** mEqPresetNames;
558#endif
559} IEngine;
560
561typedef struct {
562    const struct SLEngineCapabilitiesItf_ *mItf;
563    IObject *mThis;
564    SLboolean mThreadSafe;
565    // const
566    SLuint32 mMaxIndexLED;
567    SLuint32 mMaxIndexVibra;
568} IEngineCapabilities;
569
570typedef struct {
571    const struct SLEnvironmentalReverbItf_ *mItf;
572    IObject *mThis;
573    SLEnvironmentalReverbSettings mProperties;
574#if defined(ANDROID) && !defined(USE_BACKPORT)
575    effect_descriptor_t mEnvironmentalReverbDescriptor;
576    android::sp<android::AudioEffect> mEnvironmentalReverbEffect;
577#endif
578} IEnvironmentalReverb;
579
580struct EqualizerBand {
581    SLmilliHertz mMin;
582    SLmilliHertz mCenter;
583    SLmilliHertz mMax;
584};
585
586#if defined(ANDROID) && !defined(USE_BACKPORT)
587#define MAX_EQ_BANDS 0
588#else
589#define MAX_EQ_BANDS 4  // compile-time limit, runtime limit may be smaller
590#endif
591
592typedef struct {
593    const struct SLEqualizerItf_ *mItf;
594    IObject *mThis;
595    SLboolean mEnabled;
596    SLuint16 mPreset;
597#if 0 < MAX_EQ_BANDS
598    SLmillibel mLevels[MAX_EQ_BANDS];
599#endif
600    // const to end of struct
601    SLuint16 mNumPresets;
602    SLuint16 mNumBands;
603#if !defined(ANDROID) || defined(USE_BACKPORT)
604    const struct EqualizerBand *mBands;
605    const struct EqualizerPreset *mPresets;
606#endif
607    SLmillibel mBandLevelRangeMin;
608    SLmillibel mBandLevelRangeMax;
609#if defined(ANDROID) && !defined(USE_BACKPORT)
610    effect_descriptor_t mEqDescriptor;
611    android::sp<android::AudioEffect> mEqEffect;
612#endif
613} IEqualizer;
614
615#define MAX_LED_COUNT 32
616
617typedef struct {
618    const struct SLLEDArrayItf_ *mItf;
619    IObject *mThis;
620    SLuint32 mLightMask;
621    SLHSL mColors[MAX_LED_COUNT];
622    // const
623    SLuint8 mCount;
624} ILEDArray;
625
626typedef struct {
627    const struct SLMetadataExtractionItf_ *mItf;
628    IObject *mThis;
629    SLuint32 mKeySize;
630    const void *mKey;
631    SLuint32 mKeyEncoding;
632    const SLchar *mValueLangCountry;
633    SLuint32 mValueEncoding;
634    SLuint8 mFilterMask;
635    int mKeyFilter;
636} IMetadataExtraction;
637
638typedef struct {
639    const struct SLMetadataTraversalItf_ *mItf;
640    IObject *mThis;
641    SLuint32 mIndex;
642    SLuint32 mMode;
643    SLuint32 mCount;
644    SLuint32 mSize;
645} IMetadataTraversal;
646
647typedef struct {
648    const struct SLMIDIMessageItf_ *mItf;
649    IObject *mThis;
650    slMetaEventCallback mMetaEventCallback;
651    void *mMetaEventContext;
652    slMIDIMessageCallback mMessageCallback;
653    void *mMessageContext;
654    SLuint8 mMessageTypes;
655} IMIDIMessage;
656
657typedef struct {
658    const struct SLMIDIMuteSoloItf_ *mItf;
659    IObject *mThis;
660    SLuint16 mChannelMuteMask;
661    SLuint16 mChannelSoloMask;
662    SLuint32 mTrackMuteMask;
663    SLuint32 mTrackSoloMask;
664    // const
665    SLuint16 mTrackCount;
666} IMIDIMuteSolo;
667
668typedef struct {
669    const struct SLMIDITempoItf_ *mItf;
670    IObject *mThis;
671    SLuint32 mTicksPerQuarterNote;
672    SLuint32 mMicrosecondsPerQuarterNote;
673} IMIDITempo;
674
675typedef struct {
676    const struct SLMIDITimeItf_ *mItf;
677    IObject *mThis;
678    SLuint32 mDuration;
679    SLuint32 mPosition;
680    SLuint32 mStartTick;
681    SLuint32 mNumTicks;
682} IMIDITime;
683
684typedef struct {
685    const struct SLMuteSoloItf_ *mItf;
686    IObject *mThis;
687    // fields that were formerly here are now at CAudioPlayer
688} IMuteSolo;
689
690#define MAX_TRACK 32        // see mActiveMask
691
692typedef struct {
693    const struct SLOutputMixItf_ *mItf;
694    IObject *mThis;
695    slMixDeviceChangeCallback mCallback;
696    void *mContext;
697} IOutputMix;
698
699#ifdef USE_OUTPUTMIXEXT
700typedef struct {
701    const struct SLOutputMixExtItf_ *mItf;
702    IObject *mThis;
703    unsigned mActiveMask;   // 1 bit per active track
704    Track mTracks[MAX_TRACK];
705    SLboolean mDestroyRequested;    ///< Mixer to acknowledge application's call to Object::Destroy
706} IOutputMixExt;
707#endif
708
709typedef struct {
710    const struct SLPitchItf_ *mItf;
711    IObject *mThis;
712    SLpermille mPitch;
713    // const
714    SLpermille mMinPitch;
715    SLpermille mMaxPitch;
716} IPitch;
717
718typedef struct Play_interface {
719    const struct SLPlayItf_ *mItf;
720    IObject *mThis;
721    SLuint32 mState;
722    // next 2 fields are read-only to application
723    SLmillisecond mDuration;
724    SLmillisecond mPosition;
725    slPlayCallback mCallback;
726    void *mContext;
727    SLuint32 mEventFlags;
728    // the ISeek trick of using a distinct value doesn't work here because it's readable by app
729    SLmillisecond mMarkerPosition;
730    SLmillisecond mPositionUpdatePeriod; // Zero means do not do position updates (FIXME ~0)
731#ifdef USE_OUTPUTMIXEXT
732    SLuint32 mFrameUpdatePeriod;         // mPositionUpdatePeriod in frame units
733    SLmillisecond mLastSeekPosition;     // Last known accurate position, set at Seek
734    SLuint32 mFramesSinceLastSeek;       // Frames mixed since last known accurate position
735    SLuint32 mFramesSincePositionUpdate; // Frames mixed since last position update callback
736#endif
737} IPlay;
738
739typedef struct {
740    const struct SLPlaybackRateItf_ *mItf;
741    IObject *mThis;
742    SLpermille mRate;
743    SLuint32 mProperties;
744    // const
745    SLpermille mMinRate;
746    SLpermille mMaxRate;
747    SLpermille mStepSize;
748    SLuint32 mCapabilities;
749} IPlaybackRate;
750
751typedef struct {
752    const struct SLPrefetchStatusItf_ *mItf;
753    IObject *mThis;
754    SLuint32 mStatus;
755    SLpermille mLevel;
756    slPrefetchCallback mCallback;
757    void *mContext;
758    SLuint32 mCallbackEventsMask;
759    SLpermille mFillUpdatePeriod;
760} IPrefetchStatus;
761
762typedef struct {
763    const struct SLPresetReverbItf_ *mItf;
764    IObject *mThis;
765    SLuint16 mPreset;
766#if defined(ANDROID) && !defined(USE_BACKPORT)
767    effect_descriptor_t mPresetReverbDescriptor;
768    android::sp<android::AudioEffect> mPresetReverbEffect;
769#endif
770} IPresetReverb;
771
772typedef struct {
773    const struct SLRatePitchItf_ *mItf;
774    IObject *mThis;
775    SLpermille mRate;
776    // const
777    SLpermille mMinRate;
778    SLpermille mMaxRate;
779} IRatePitch;
780
781typedef struct {
782    const struct SLRecordItf_ *mItf;
783    IObject *mThis;
784    SLuint32 mState;
785    SLmillisecond mDurationLimit;
786    SLmillisecond mPosition;
787    slRecordCallback mCallback;
788    void *mContext;
789    SLuint32 mCallbackEventsMask;
790    SLmillisecond mMarkerPosition;
791    SLmillisecond mPositionUpdatePeriod;
792} IRecord;
793
794typedef struct {
795    const struct SLSeekItf_ *mItf;
796    IObject *mThis;
797    SLmillisecond mPos;     // mPos != SL_TIME_UNKNOWN means pending seek request
798    SLboolean mLoopEnabled;
799    SLmillisecond mStartPos;
800    SLmillisecond mEndPos;
801} ISeek;
802
803typedef struct {
804    const struct SLThreadSyncItf_ *mItf;
805    IObject *mThis;
806    SLboolean mInCriticalSection;
807    SLuint32 mWaiting;  // number of threads waiting
808    pthread_t mOwner;
809} IThreadSync;
810
811typedef struct {
812    const struct SLVibraItf_ *mItf;
813    IObject *mThis;
814    SLboolean mVibrate;
815    SLmilliHertz mFrequency;
816    SLpermille mIntensity;
817} IVibra;
818
819typedef struct {
820    const struct SLVirtualizerItf_ *mItf;
821    IObject *mThis;
822    SLboolean mEnabled;
823    SLpermille mStrength;
824#if defined(ANDROID) && !defined(USE_BACKPORT)
825    effect_descriptor_t mVirtualizerDescriptor;
826    android::sp<android::AudioEffect> mVirtualizerEffect;
827#endif
828} IVirtualizer;
829
830typedef struct {
831    const struct SLVisualizationItf_ *mItf;
832    IObject *mThis;
833    slVisualizationCallback mCallback;
834    void *mContext;
835    SLmilliHertz mRate;
836} IVisualization;
837
838typedef struct /*Volume_interface*/ {
839    const struct SLVolumeItf_ *mItf;
840    IObject *mThis;
841    // Values as specified by the application
842    SLmillibel mLevel;
843    SLpermille mStereoPosition;
844    SLuint8 /*SLboolean*/ mMute;
845    SLuint8 /*SLboolean*/ mEnableStereoPosition;
846} IVolume;
847
848/* Class structures */
849
850/*typedef*/ struct C3DGroup_struct {
851    IObject mObject;
852#define INTERFACES_3DGroup 6 // see MPH_to_3DGroup in MPH_to.c for list of interfaces
853    SLuint8 mInterfaceStates2[INTERFACES_3DGroup - INTERFACES_Default];
854    IDynamicInterfaceManagement mDynamicInterfaceManagement;
855    I3DLocation m3DLocation;
856    I3DDoppler m3DDoppler;
857    I3DSource m3DSource;
858    I3DMacroscopic m3DMacroscopic;
859    // remaining are per-instance private fields not associated with an interface
860    unsigned mMemberMask;   // set of member objects
861} /*C3DGroup*/;
862
863#ifdef ANDROID
864
865// FIXME Move these into the I... section above
866
867typedef struct {
868    const struct SLAndroidEffectItf_ *mItf;
869    IObject *mThis;
870    android::KeyedVector<SLuint32, android::AudioEffect* > *mEffects;
871} IAndroidEffect;
872
873typedef struct {
874    const struct SLAndroidEffectCapabilitiesItf_ *mItf;
875    IObject *mThis;
876    SLuint32 mNumFx;
877    effect_descriptor_t* mFxDescriptors;
878} IAndroidEffectCapabilities;
879
880typedef struct {
881    const struct SLAndroidEffectSendItf_ *mItf;
882    IObject *mThis;
883    // only one send per interface for now (1 bus)
884    SLboolean mEnabled;
885    SLmillibel mSendLevel; //android::KeyedVector<SLuint32, SLmillibel> mSendLevels;
886} IAndroidEffectSend;
887
888typedef struct {
889    const struct SLAndroidConfigurationItf_ *mItf;
890    IObject *mThis;
891} IAndroidConfiguration;
892
893#if defined(ANDROID) && !defined(USE_BACKPORT)
894// FIXME this include is done here so the effect structures have been defined. Messy.
895#include "android_Effect.h"
896#endif
897
898
899/*
900 * Used to define the mapping from an OpenSL ES audio player to an Android
901 * media framework object
902 */
903enum AndroidObject_type {
904    INVALID_TYPE     =-1,
905    MEDIAPLAYER      = 0,
906    AUDIOTRACK_PULL  = 1,
907    NUM_AUDIOPLAYER_MAP_TYPES
908};
909
910enum AndroidObject_state {
911    ANDROID_UNINITIALIZED = -1,
912    ANDROID_PREPARING,
913    ANDROID_READY,
914    NUM_ANDROID_STATES
915};
916
917#endif  // ANDROID
918
919
920/*typedef*/ struct CAudioPlayer_struct {
921    IObject mObject;
922#ifdef ANDROID
923#define INTERFACES_AudioPlayer 29 // see MPH_to_AudioPlayer in MPH_to.c for list of interfaces
924#else
925#define INTERFACES_AudioPlayer 26 // see MPH_to_AudioPlayer in MPH_to.c for list of interfaces
926#endif
927    SLuint8 mInterfaceStates2[INTERFACES_AudioPlayer - INTERFACES_Default];
928    IDynamicInterfaceManagement mDynamicInterfaceManagement;
929    IPlay mPlay;
930    I3DDoppler m3DDoppler;
931    I3DGrouping m3DGrouping;
932    I3DLocation m3DLocation;
933    I3DSource m3DSource;
934    IBufferQueue mBufferQueue;
935    IEffectSend mEffectSend;
936    IMetadataExtraction mMetadataExtraction;
937    IMetadataTraversal mMetadataTraversal;
938    IPrefetchStatus mPrefetchStatus;
939    IRatePitch mRatePitch;
940    ISeek mSeek;
941    IVolume mVolume;
942    IMuteSolo mMuteSolo;
943#ifdef ANDROID
944    IAndroidEffect mAndroidEffect;
945    IAndroidEffectSend mAndroidEffectSend;
946    IAndroidConfiguration mAndroidConfiguration;
947#endif
948    // optional interfaces
949    I3DMacroscopic m3DMacroscopic;
950    IBassBoost mBassBoost;
951    IDynamicSource mDynamicSource;
952    IEnvironmentalReverb mEnvironmentalReverb;
953    IEqualizer mEqualizer;
954    IPitch mPitch;
955    IPresetReverb mPresetReverb;
956    IPlaybackRate mPlaybackRate;
957    IVirtualizer mVirtualizer;
958    IVisualization mVisualization;
959    // remaining are per-instance private fields not associated with an interface
960    DataLocatorFormat mDataSource;
961    DataLocatorFormat mDataSink;
962    // cached data for this instance
963    SLuint8 /*SLboolean*/ mMute;
964    // Formerly at IMuteSolo
965    SLuint8 mMuteMask;      // Mask for which channels are muted: bit 0=left, 1=right
966    SLuint8 mSoloMask;      // Mask for which channels are soloed: bit 0=left, 1=right
967    SLuint8 mNumChannels;   // 0 means unknown, then const once it is known, range 1 <= x <= 8
968    SLuint32 mSampleRateMilliHz;// 0 means unknown, then const once it is known
969    // Formerly at IEffectSend
970    /**
971     * Dry volume modified by effect send interfaces: SLEffectSendItf and SLAndroidEffectSendItf
972     */
973    SLmillibel mDirectLevel;
974    // implementation-specific data for this instance
975#ifdef USE_OUTPUTMIXEXT
976    Track *mTrack;
977    float mGains[STEREO_CHANNELS];  ///< Computed gain based on volume, mute, solo, stereo position
978    SLboolean mDestroyRequested;    ///< Mixer to acknowledge application's call to Object::Destroy
979#endif
980#ifdef USE_SNDFILE
981    struct SndFile mSndFile;
982#endif // USE_SNDFILE
983#ifdef ANDROID
984    android::Mutex          *mpLock;
985    enum AndroidObject_type mAndroidObjType;
986    enum AndroidObject_state mAndroidObjState;
987    /** identifies which group of effects ("session") this player belongs to */
988    int mSessionId;
989    /** identifies the Android stream type playback will occur on */
990    int mStreamType;
991    /** plays the PCM data for this player */
992    android::AudioTrack *mAudioTrack;
993#ifndef USE_BACKPORT
994    android::sp<android::SfPlayer> mSfPlayer;
995    /** aux effect the AudioTrack will be attached to if aux send enabled */
996    android::sp<android::AudioEffect> mAuxEffect;
997    /** send level to aux effect, there's a single aux bus, so there's a single level */
998    SLmillibel mAuxSendLevel;
999#endif
1000    /**
1001     * Amplification (can be attenuation) factor derived for the VolumeLevel
1002     */
1003    float mAmplFromVolLevel;
1004    /**
1005     * Left/right amplification (can be attenuations) factors derived for the StereoPosition
1006     */
1007    float mAmplFromStereoPos[STEREO_CHANNELS];
1008    /**
1009     * Attenuation factor derived from direct level
1010     */
1011    float mAmplFromDirectLevel;
1012#endif
1013} /*CAudioPlayer*/;
1014
1015
1016/*typedef*/ struct CAudioRecorder_struct {
1017    // mandated interfaces
1018    IObject mObject;
1019#ifdef ANDROID
1020#define INTERFACES_AudioRecorder 11 // see MPH_to_AudioRecorder in MPH_to.c for list of interfaces
1021#else
1022#define INTERFACES_AudioRecorder 9  // see MPH_to_AudioRecorder in MPH_to.c for list of interfaces
1023#endif
1024    SLuint8 mInterfaceStates2[INTERFACES_AudioRecorder - INTERFACES_Default];
1025    IDynamicInterfaceManagement mDynamicInterfaceManagement;
1026    IRecord mRecord;
1027    IAudioEncoder mAudioEncoder;
1028    // optional interfaces
1029    IBassBoost mBassBoost;
1030    IDynamicSource mDynamicSource;
1031    IEqualizer mEqualizer;
1032    IVisualization mVisualization;
1033    IVolume mVolume;
1034#ifdef ANDROID
1035    IBufferQueue mBufferQueue;
1036    IAndroidConfiguration mAndroidConfiguration;
1037#endif
1038    // remaining are per-instance private fields not associated with an interface
1039    DataLocatorFormat mDataSource;
1040    DataLocatorFormat mDataSink;
1041    // cached data for this instance
1042    SLuint8 mNumChannels;   // 0 means unknown, then const once it is known, range 1 <= x <= 8
1043    SLuint32 mSampleRateMilliHz;// 0 means unknown, then const once it is known
1044    // implementation-specific data for this instance
1045#ifdef ANDROID
1046    android::AudioRecord *mAudioRecord;
1047    int mRecordSource;
1048#endif
1049} /*CAudioRecorder*/;
1050
1051
1052typedef struct {
1053    // mandated implicit interfaces
1054    IObject mObject;
1055#ifdef ANDROID
1056#define INTERFACES_Engine 11 // see MPH_to_Engine in MPH_to.c for list of interfaces
1057#else
1058#define INTERFACES_Engine 10 // see MPH_to_Engine in MPH_to.c for list of interfaces
1059#endif
1060    SLuint8 mInterfaceStates2[INTERFACES_Engine - INTERFACES_Default];
1061    IDynamicInterfaceManagement mDynamicInterfaceManagement;
1062    IEngine mEngine;
1063    IEngineCapabilities mEngineCapabilities;
1064    IThreadSync mThreadSync;
1065    // mandated explicit interfaces
1066    IAudioIODeviceCapabilities mAudioIODeviceCapabilities;
1067    IAudioDecoderCapabilities mAudioDecoderCapabilities;
1068    IAudioEncoderCapabilities mAudioEncoderCapabilities;
1069    I3DCommit m3DCommit;
1070#ifdef ANDROID
1071    IAndroidEffectCapabilities mAndroidEffectCapabilities;
1072#endif
1073    // optional interfaces
1074    IDeviceVolume mDeviceVolume;
1075    // remaining are per-instance private fields not associated with an interface
1076    pthread_t mSyncThread;
1077} CEngine;
1078
1079typedef struct {
1080    // mandated interfaces
1081    IObject mObject;
1082#define INTERFACES_LEDDevice 3 // see MPH_to_LEDDevice in MPH_to.c for list of interfaces
1083    SLuint8 mInterfaceStates2[INTERFACES_LEDDevice - INTERFACES_Default];
1084    IDynamicInterfaceManagement mDynamicInterfaceManagement;
1085    ILEDArray mLEDArray;
1086    // remaining are per-instance private fields not associated with an interface
1087    SLuint32 mDeviceID;
1088} CLEDDevice;
1089
1090typedef struct {
1091    // mandated interfaces
1092    IObject mObject;
1093#define INTERFACES_Listener 4 // see MPH_to_Listener in MPH_to.c for list of interfaces
1094    SLuint8 mInterfaceStates2[INTERFACES_Listener - INTERFACES_Default];
1095    IDynamicInterfaceManagement mDynamicInterfaceManagement;
1096    I3DDoppler m3DDoppler;
1097    I3DLocation m3DLocation;
1098    // remaining are per-instance private fields not associated with an interface
1099} CListener;
1100
1101typedef struct {
1102    // mandated interfaces
1103    IObject mObject;
1104#define INTERFACES_MetadataExtractor 5 // see MPH_to_MetadataExtractor in MPH_to.c for list of
1105                                       // interfaces
1106    SLuint8 mInterfaceStates2[INTERFACES_MetadataExtractor - INTERFACES_Default];
1107    IDynamicInterfaceManagement mDynamicInterfaceManagement;
1108    IDynamicSource mDynamicSource;
1109    IMetadataExtraction mMetadataExtraction;
1110    IMetadataTraversal mMetadataTraversal;
1111    // remaining are per-instance private fields not associated with an interface
1112} CMetadataExtractor;
1113
1114typedef struct {
1115    // mandated interfaces
1116    IObject mObject;
1117
1118#define INTERFACES_MidiPlayer 29 // see MPH_to_MidiPlayer in MPH_to.c for list of interfaces
1119    SLuint8 mInterfaceStates2[INTERFACES_MidiPlayer - INTERFACES_Default];
1120    IDynamicInterfaceManagement mDynamicInterfaceManagement;
1121    IPlay mPlay;
1122    I3DDoppler m3DDoppler;
1123    I3DGrouping m3DGrouping;
1124    I3DLocation m3DLocation;
1125    I3DSource m3DSource;
1126    IBufferQueue mBufferQueue;
1127    IEffectSend mEffectSend;
1128    IMetadataExtraction mMetadataExtraction;
1129    IMetadataTraversal mMetadataTraversal;
1130    IMIDIMessage mMIDIMessage;
1131    IMIDITime mMIDITime;
1132    IMIDITempo mMIDITempo;
1133    IMIDIMuteSolo mMIDIMuteSolo;
1134    IPrefetchStatus mPrefetchStatus;
1135    ISeek mSeek;
1136    IVolume mVolume;
1137    IMuteSolo mMuteSolo;
1138    // optional interfaces
1139    I3DMacroscopic m3DMacroscopic;
1140    IBassBoost mBassBoost;
1141    IDynamicSource mDynamicSource;
1142    IEnvironmentalReverb mEnvironmentalReverb;
1143    IEqualizer mEqualizer;
1144    IPitch mPitch;
1145    IPresetReverb mPresetReverb;
1146    IPlaybackRate mPlaybackRate;
1147    IVirtualizer mVirtualizer;
1148    IVisualization mVisualization;
1149    // remaining are per-instance private fields not associated with an interface
1150} CMidiPlayer;
1151
1152/*typedef*/ struct COutputMix_struct {
1153    // mandated interfaces
1154    IObject mObject;
1155#ifdef ANDROID
1156#define INTERFACES_OutputMix 12 // see MPH_to_OutputMix in MPH_to.c for list of interfaces
1157#else
1158#define INTERFACES_OutputMix 11 // see MPH_to_OutputMix in MPH_to.c for list of interfaces
1159#endif
1160    SLuint8 mInterfaceStates2[INTERFACES_OutputMix - INTERFACES_Default];
1161    IDynamicInterfaceManagement mDynamicInterfaceManagement;
1162    IOutputMix mOutputMix;
1163#ifdef USE_OUTPUTMIXEXT
1164    IOutputMixExt mOutputMixExt;
1165#endif
1166    IEnvironmentalReverb mEnvironmentalReverb;
1167    IEqualizer mEqualizer;
1168    IPresetReverb mPresetReverb;
1169    IVirtualizer mVirtualizer;
1170    IVolume mVolume;
1171    // optional interfaces
1172    IBassBoost mBassBoost;
1173    IVisualization mVisualization;
1174#ifdef ANDROID
1175    IAndroidEffect mAndroidEffect;
1176#endif
1177    // remaining are per-instance private fields not associated with an interface
1178} /*COutputMix*/;
1179
1180typedef struct {
1181    // mandated interfaces
1182    IObject mObject;
1183#define INTERFACES_VibraDevice 3 // see MPH_to_VibraDevice in MPH_to.c for list of interfaces
1184    SLuint8 mInterfaceStates2[INTERFACES_VibraDevice - INTERFACES_Default];
1185    IDynamicInterfaceManagement mDynamicInterfaceManagement;
1186    IVibra mVibra;
1187    // remaining are per-instance private fields not associated with an interface
1188    SLuint32 mDeviceID;
1189} CVibraDevice;
1190
1191struct MPH_init {
1192    VoidHook mInit;     // called first to initialize the interface, right after object is allocated
1193    // Each interface is initialized regardless whether it is exposed to application.
1194    VoidHook mResume;   // called to resume interface after suspension, not currently used
1195    VoidHook mDeinit;   // called last when object is about to be destroyed
1196    BoolHook mExpose;   // called after initialization, only if interface is exposed to application
1197    VoidHook mRemove;   // called by DynamicInterfaceManager::RemoveInterface, and prior to mDeinit
1198    // will need a suspend hook when suspend is implemented
1199};
1200
1201extern /*static*/ int IID_to_MPH(const SLInterfaceID iid);
1202extern /*static*/ const struct MPH_init MPH_init_table[MPH_MAX];
1203extern SLresult checkInterfaces(const ClassTable *class__,
1204    SLuint32 numInterfaces, const SLInterfaceID *pInterfaceIds,
1205    const SLboolean *pInterfaceRequired, unsigned *pExposedMask);
1206extern IObject *construct(const ClassTable *class__,
1207    unsigned exposedMask, SLEngineItf engine);
1208extern const ClassTable *objectIDtoClass(SLuint32 objectID);
1209extern const struct SLInterfaceID_ SL_IID_array[MPH_MAX];
1210extern SLuint32 IObjectToObjectID(IObject *object);
1211extern void IObject_Publish(IObject *this);
1212extern void IObject_Destroy(SLObjectItf self);
1213
1214// Map an interface to it's "object ID" (which is really a class ID).
1215// Note: this operation is undefined on IObject, as it lacks an mThis.
1216// If you have an IObject, then use IObjectToObjectID directly.
1217
1218#define InterfaceToObjectID(this) IObjectToObjectID((this)->mThis)
1219
1220// Map an interface to it's corresponding IObject.
1221// Note: this operation is undefined on IObject, as it lacks an mThis.
1222// If you have an IObject, then you're done -- you already have what you need.
1223
1224#define InterfaceToIObject(this) ((this)->mThis)
1225
1226#define InterfaceToCAudioPlayer(this) (((CAudioPlayer*)InterfaceToIObject(this)))
1227
1228#define InterfaceToCAudioRecorder(this) (((CAudioRecorder*)InterfaceToIObject(this)))
1229
1230#ifdef ANDROID
1231#include "android_AudioPlayer.h"
1232#endif
1233
1234extern SLresult checkDataSource(const SLDataSource *pDataSrc,
1235        DataLocatorFormat *myDataSourceLocator);
1236extern SLresult checkDataSink(const SLDataSink *pDataSink, DataLocatorFormat *myDataSinkLocator,
1237        SLuint32 objType);
1238extern SLresult checkSourceFormatVsInterfacesCompatibility(
1239        const DataLocatorFormat *pDataLocatorFormat, const ClassTable *class__,
1240        unsigned exposedMask);
1241extern void freeDataLocatorFormat(DataLocatorFormat *dlf);
1242
1243extern bool C3DGroup_PreDestroy(void *self);
1244
1245extern SLresult CAudioPlayer_Realize(void *self, SLboolean async);
1246extern SLresult CAudioPlayer_Resume(void *self, SLboolean async);
1247extern void CAudioPlayer_Destroy(void *self);
1248extern bool CAudioPlayer_PreDestroy(void *self);
1249
1250extern SLresult CAudioRecorder_Realize(void *self, SLboolean async);
1251extern SLresult CAudioRecorder_Resume(void *self, SLboolean async);
1252extern void CAudioRecorder_Destroy(void *self);
1253extern bool CAudioRecorder_PreDestroy(void *self);
1254
1255extern SLresult CEngine_Realize(void *self, SLboolean async);
1256extern SLresult CEngine_Resume(void *self, SLboolean async);
1257extern void CEngine_Destroy(void *self);
1258extern bool CEngine_PreDestroy(void *self);
1259extern void CEngine_Destroyed(CEngine *self);
1260
1261extern SLresult COutputMix_Realize(void *self, SLboolean async);
1262extern SLresult COutputMix_Resume(void *self, SLboolean async);
1263extern void COutputMix_Destroy(void *self);
1264extern bool COutputMix_PreDestroy(void *self);
1265
1266#ifdef USE_SDL
1267extern void SDL_open(IEngine *thisEngine);
1268extern void SDL_close(void);
1269#endif
1270#define SL_OBJECT_STATE_REALIZING_1  ((SLuint32) 0x4) // async realize on work queue
1271#define SL_OBJECT_STATE_REALIZING_2  ((SLuint32) 0x5) // sync realize, or async realize hook
1272#define SL_OBJECT_STATE_RESUMING_1   ((SLuint32) 0x6) // async resume on work queue
1273#define SL_OBJECT_STATE_RESUMING_2   ((SLuint32) 0x7) // sync resume, or async resume hook
1274#define SL_OBJECT_STATE_SUSPENDING   ((SLuint32) 0x8) // suspend in progress
1275#define SL_OBJECT_STATE_REALIZING_1A ((SLuint32) 0x9) // abort while async realize on work queue
1276#define SL_OBJECT_STATE_RESUMING_1A  ((SLuint32) 0xA) // abort while async resume on work queue
1277#define SL_OBJECT_STATE_DESTROYING   ((SLuint32) 0xB) // destroy object when no strong references
1278#ifndef ANDROID
1279extern void *sync_start(void *arg);
1280#endif
1281extern SLresult err_to_result(int err);
1282
1283#ifdef __GNUC__
1284#define ctz __builtin_ctz
1285#else
1286extern unsigned ctz(unsigned);
1287#endif
1288extern const char * const interface_names[MPH_MAX];
1289#include "platform.h"
1290
1291// Attributes
1292
1293#define ATTR_NONE       ((unsigned) 0x0)      // none
1294#define ATTR_GAIN       ((unsigned) 0x1 << 0) // player volume, channel mute, channel solo,
1295                                              // player stereo position, player mute
1296#define ATTR_TRANSPORT  ((unsigned) 0x1 << 1) // play state, looping
1297#define ATTR_POSITION   ((unsigned) 0x1 << 2) // requested position (a.k.a. seek position)
1298#define ATTR_ENQUEUE    ((unsigned) 0x1 << 3) // buffer queue became non-empty and in playing state
1299
1300#define SL_DATALOCATOR_NULL 0    // application specified a NULL value for pLocator
1301#define SL_DATAFORMAT_NULL 0     // application specified a NULL or undefined value for pFormat
1302
1303// Trace debugging
1304
1305// Always defined, but may be a no-op if trace support is disabled at compile-time
1306extern void slTraceSetEnabled(unsigned enabled);
1307
1308#define SL_TRACE_ENTER          0x1
1309#define SL_TRACE_LEAVE_FAILURE  0x2
1310#define SL_TRACE_LEAVE_VOID     0x4
1311#define SL_TRACE_LEAVE_SUCCESS  0x8
1312#define SL_TRACE_LEAVE          (SL_TRACE_LEAVE_FAILURE | SL_TRACE_LEAVE_VOID | \
1313                                    SL_TRACE_LEAVE_SUCCESS)
1314#define SL_TRACE_ALL            (SL_TRACE_ENTER | SL_TRACE_LEAVE)
1315#ifndef SL_TRACE_DEFAULT
1316#define SL_TRACE_DEFAULT        (SL_TRACE_LEAVE_FAILURE)
1317#endif
1318
1319#ifndef USE_TRACE
1320
1321#define SL_ENTER_GLOBAL SLresult result;
1322#define SL_LEAVE_GLOBAL return result;
1323#define SL_ENTER_INTERFACE SLresult result;
1324#define SL_LEAVE_INTERFACE return result;
1325#define SL_ENTER_INTERFACE_VOID
1326#define SL_LEAVE_INTERFACE_VOID return;
1327
1328#else
1329
1330extern void slTraceEnterGlobal(const char *function);
1331extern void slTraceLeaveGlobal(const char *function, SLresult result);
1332extern void slTraceEnterInterface(const char *function);
1333extern void slTraceLeaveInterface(const char *function, SLresult result);
1334extern void slTraceEnterInterfaceVoid(const char *function);
1335extern void slTraceLeaveInterfaceVoid(const char *function);
1336#define SL_ENTER_GLOBAL SLresult result; slTraceEnterGlobal(__FUNCTION__);
1337#define SL_LEAVE_GLOBAL slTraceLeaveGlobal(__FUNCTION__, result); return result;
1338#define SL_ENTER_INTERFACE SLresult result; slTraceEnterInterface(__FUNCTION__);
1339#define SL_LEAVE_INTERFACE slTraceLeaveInterface(__FUNCTION__, result); return result;
1340#define SL_ENTER_INTERFACE_VOID slTraceEnterInterfaceVoid(__FUNCTION__);
1341#define SL_LEAVE_INTERFACE_VOID slTraceLeaveInterfaceVoid(__FUNCTION__); return;
1342
1343#endif
1344
1345#ifdef USE_OUTPUTMIXEXT
1346
1347#define SL_PLAYSTATE_STOPPING ((SLuint32) 0x4) // Play::Stop while PLAYING
1348// If we needed it, could have PLAYING mean mixer is currently reading from front buffer,
1349// while PLAYABLE would mean application requested PLAYING, but buffer queue is empty
1350
1351#endif
1352
1353#ifdef USE_SNDFILE
1354extern void audioPlayerTransportUpdate(CAudioPlayer *audioPlayer);
1355#endif
1356
1357extern SLresult IBufferQueue_Enqueue(SLBufferQueueItf self, const void *pBuffer, SLuint32 size);
1358extern SLresult IBufferQueue_Clear(SLBufferQueueItf self);
1359extern SLresult IBufferQueue_RegisterCallback(SLBufferQueueItf self,
1360    slBufferQueueCallback callback, void *pContext);
1361
1362extern bool IsInterfaceInitialized(IObject *this, unsigned MPH);
1363extern SLresult AcquireStrongRef(IObject *object, SLuint32 expectedObjectID);
1364extern void ReleaseStrongRef(IObject *object);
1365extern void ReleaseStrongRefAndUnlockExclusive(IObject *object);
1366
1367extern COutputMix *CAudioPlayer_GetOutputMix(CAudioPlayer *audioPlayer);
1368