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