IEngine.c revision daf661248ff6ea2e21799e5114c78b7c1d49630e
10b167267bda99b68346045ccab14e810121d5de4Glenn Kasten/*
20b167267bda99b68346045ccab14e810121d5de4Glenn Kasten * Copyright (C) 2010 The Android Open Source Project
30b167267bda99b68346045ccab14e810121d5de4Glenn Kasten *
40b167267bda99b68346045ccab14e810121d5de4Glenn Kasten * Licensed under the Apache License, Version 2.0 (the "License");
50b167267bda99b68346045ccab14e810121d5de4Glenn Kasten * you may not use this file except in compliance with the License.
60b167267bda99b68346045ccab14e810121d5de4Glenn Kasten * You may obtain a copy of the License at
70b167267bda99b68346045ccab14e810121d5de4Glenn Kasten *
80b167267bda99b68346045ccab14e810121d5de4Glenn Kasten *      http://www.apache.org/licenses/LICENSE-2.0
90b167267bda99b68346045ccab14e810121d5de4Glenn Kasten *
100b167267bda99b68346045ccab14e810121d5de4Glenn Kasten * Unless required by applicable law or agreed to in writing, software
110b167267bda99b68346045ccab14e810121d5de4Glenn Kasten * distributed under the License is distributed on an "AS IS" BASIS,
120b167267bda99b68346045ccab14e810121d5de4Glenn Kasten * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
130b167267bda99b68346045ccab14e810121d5de4Glenn Kasten * See the License for the specific language governing permissions and
140b167267bda99b68346045ccab14e810121d5de4Glenn Kasten * limitations under the License.
150b167267bda99b68346045ccab14e810121d5de4Glenn Kasten */
160b167267bda99b68346045ccab14e810121d5de4Glenn Kasten
170b167267bda99b68346045ccab14e810121d5de4Glenn Kasten/* Engine implementation */
180b167267bda99b68346045ccab14e810121d5de4Glenn Kasten
19979a3f8743646af9999a89dff9e13b972b7efd87Glenn Kasten#include "sles_allinclusive.h"
20979a3f8743646af9999a89dff9e13b972b7efd87Glenn Kasten
21daf661248ff6ea2e21799e5114c78b7c1d49630eGlenn Kastenstatic SLresult IEngine_CreateLEDDevice(SLEngineItf self, SLObjectItf *pDevice, SLuint32 deviceID,
22daf661248ff6ea2e21799e5114c78b7c1d49630eGlenn Kasten    SLuint32 numInterfaces, const SLInterfaceID *pInterfaceIds, const SLboolean *pInterfaceRequired)
230b167267bda99b68346045ccab14e810121d5de4Glenn Kasten{
240b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    if (NULL == pDevice || SL_DEFAULTDEVICEID_LED != deviceID)
250b167267bda99b68346045ccab14e810121d5de4Glenn Kasten        return SL_RESULT_PARAMETER_INVALID;
260b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    *pDevice = NULL;
270b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    unsigned exposedMask;
28979a3f8743646af9999a89dff9e13b972b7efd87Glenn Kasten    const ClassTable *pCLEDDevice_class = objectIDtoClass(SL_OBJECTID_LEDDEVICE);
29979a3f8743646af9999a89dff9e13b972b7efd87Glenn Kasten    SLresult result = checkInterfaces(pCLEDDevice_class, numInterfaces,
300b167267bda99b68346045ccab14e810121d5de4Glenn Kasten        pInterfaceIds, pInterfaceRequired, &exposedMask);
310b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    if (SL_RESULT_SUCCESS != result)
320b167267bda99b68346045ccab14e810121d5de4Glenn Kasten        return result;
33daf661248ff6ea2e21799e5114c78b7c1d49630eGlenn Kasten    CLEDDevice *this = (CLEDDevice *) construct(pCLEDDevice_class, exposedMask, self);
340b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    if (NULL == this)
350b167267bda99b68346045ccab14e810121d5de4Glenn Kasten        return SL_RESULT_MEMORY_FAILURE;
360b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    SLHSL *color = (SLHSL *) malloc(sizeof(SLHSL) * this->mLEDArray.mCount);
370b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    assert(NULL != this->mLEDArray.mColor);
380b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    this->mLEDArray.mColor = color;
390b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    unsigned i;
400b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    for (i = 0; i < this->mLEDArray.mCount; ++i) {
410b167267bda99b68346045ccab14e810121d5de4Glenn Kasten        // per specification 1.0.1 pg. 259: "Default color is undefined."
420b167267bda99b68346045ccab14e810121d5de4Glenn Kasten        color->hue = 0;
430b167267bda99b68346045ccab14e810121d5de4Glenn Kasten        color->saturation = 1000;
440b167267bda99b68346045ccab14e810121d5de4Glenn Kasten        color->lightness = 1000;
450b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    }
460b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    this->mDeviceID = deviceID;
470b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    *pDevice = &this->mObject.mItf;
480b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    return SL_RESULT_SUCCESS;
490b167267bda99b68346045ccab14e810121d5de4Glenn Kasten}
500b167267bda99b68346045ccab14e810121d5de4Glenn Kasten
51daf661248ff6ea2e21799e5114c78b7c1d49630eGlenn Kastenstatic SLresult IEngine_CreateVibraDevice(SLEngineItf self, SLObjectItf *pDevice, SLuint32 deviceID,
52daf661248ff6ea2e21799e5114c78b7c1d49630eGlenn Kasten    SLuint32 numInterfaces, const SLInterfaceID *pInterfaceIds, const SLboolean *pInterfaceRequired)
530b167267bda99b68346045ccab14e810121d5de4Glenn Kasten{
540b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    if (NULL == pDevice || SL_DEFAULTDEVICEID_VIBRA != deviceID)
550b167267bda99b68346045ccab14e810121d5de4Glenn Kasten        return SL_RESULT_PARAMETER_INVALID;
560b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    *pDevice = NULL;
570b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    unsigned exposedMask;
58979a3f8743646af9999a89dff9e13b972b7efd87Glenn Kasten    const ClassTable *pCVibraDevice_class = objectIDtoClass(SL_OBJECTID_VIBRADEVICE);
59979a3f8743646af9999a89dff9e13b972b7efd87Glenn Kasten    SLresult result = checkInterfaces(pCVibraDevice_class, numInterfaces,
600b167267bda99b68346045ccab14e810121d5de4Glenn Kasten        pInterfaceIds, pInterfaceRequired, &exposedMask);
610b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    if (SL_RESULT_SUCCESS != result)
620b167267bda99b68346045ccab14e810121d5de4Glenn Kasten        return result;
63daf661248ff6ea2e21799e5114c78b7c1d49630eGlenn Kasten    CVibraDevice *this = (CVibraDevice *) construct(pCVibraDevice_class, exposedMask, self);
640b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    if (NULL == this)
650b167267bda99b68346045ccab14e810121d5de4Glenn Kasten        return SL_RESULT_MEMORY_FAILURE;
660b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    this->mDeviceID = deviceID;
670b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    *pDevice = &this->mObject.mItf;
680b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    return SL_RESULT_SUCCESS;
690b167267bda99b68346045ccab14e810121d5de4Glenn Kasten}
700b167267bda99b68346045ccab14e810121d5de4Glenn Kasten
710b167267bda99b68346045ccab14e810121d5de4Glenn Kastenstatic SLresult IEngine_CreateAudioPlayer(SLEngineItf self, SLObjectItf *pPlayer,
720b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    SLDataSource *pAudioSrc, SLDataSink *pAudioSnk, SLuint32 numInterfaces,
730b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    const SLInterfaceID *pInterfaceIds, const SLboolean *pInterfaceRequired)
740b167267bda99b68346045ccab14e810121d5de4Glenn Kasten{
750b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    fprintf(stderr, "entering IEngine_CreateAudioPlayer()\n");
760b167267bda99b68346045ccab14e810121d5de4Glenn Kasten
770b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    if (NULL == pPlayer)
780b167267bda99b68346045ccab14e810121d5de4Glenn Kasten        return SL_RESULT_PARAMETER_INVALID;
790b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    *pPlayer = NULL;
800b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    unsigned exposedMask;
81979a3f8743646af9999a89dff9e13b972b7efd87Glenn Kasten    const ClassTable *pCAudioPlayer_class = objectIDtoClass(SL_OBJECTID_AUDIOPLAYER);
82979a3f8743646af9999a89dff9e13b972b7efd87Glenn Kasten    SLresult result = checkInterfaces(pCAudioPlayer_class, numInterfaces,
830b167267bda99b68346045ccab14e810121d5de4Glenn Kasten        pInterfaceIds, pInterfaceRequired, &exposedMask);
840b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    if (SL_RESULT_SUCCESS != result)
850b167267bda99b68346045ccab14e810121d5de4Glenn Kasten        return result;
86daf661248ff6ea2e21799e5114c78b7c1d49630eGlenn Kasten
87daf661248ff6ea2e21799e5114c78b7c1d49630eGlenn Kasten    // Check the source and sink parameters against generic constraints,
88daf661248ff6ea2e21799e5114c78b7c1d49630eGlenn Kasten    // and make a local copy of all parameters in case other application threads
89daf661248ff6ea2e21799e5114c78b7c1d49630eGlenn Kasten    // change memory concurrently.
90daf661248ff6ea2e21799e5114c78b7c1d49630eGlenn Kasten
91daf661248ff6ea2e21799e5114c78b7c1d49630eGlenn Kasten    // DataSource checks
92daf661248ff6ea2e21799e5114c78b7c1d49630eGlenn Kasten    DataLocatorFormat myDataSource;
93daf661248ff6ea2e21799e5114c78b7c1d49630eGlenn Kasten    result = checkDataSource(pAudioSrc, &myDataSource);
94daf661248ff6ea2e21799e5114c78b7c1d49630eGlenn Kasten    if (SL_RESULT_SUCCESS != result)
95daf661248ff6ea2e21799e5114c78b7c1d49630eGlenn Kasten        return result;
96daf661248ff6ea2e21799e5114c78b7c1d49630eGlenn Kasten
97daf661248ff6ea2e21799e5114c78b7c1d49630eGlenn Kasten    // DataSink checks
98daf661248ff6ea2e21799e5114c78b7c1d49630eGlenn Kasten    DataLocatorFormat myDataSink;
99daf661248ff6ea2e21799e5114c78b7c1d49630eGlenn Kasten    result = checkDataSink(pAudioSnk, &myDataSink);
100daf661248ff6ea2e21799e5114c78b7c1d49630eGlenn Kasten    if (SL_RESULT_SUCCESS != result) {
101daf661248ff6ea2e21799e5114c78b7c1d49630eGlenn Kasten        freeDataLocatorFormat(&myDataSink);
1020b167267bda99b68346045ccab14e810121d5de4Glenn Kasten        return result;
1030b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    }
104daf661248ff6ea2e21799e5114c78b7c1d49630eGlenn Kasten
105daf661248ff6ea2e21799e5114c78b7c1d49630eGlenn Kasten    // FIXME numerous leaks are possible from here on - check each return
106daf661248ff6ea2e21799e5114c78b7c1d49630eGlenn Kasten    // freeDataLocatorFormat(&myDataSource);
107daf661248ff6ea2e21799e5114c78b7c1d49630eGlenn Kasten    // freeDataLocatorFormat(&myDataSink);
108daf661248ff6ea2e21799e5114c78b7c1d49630eGlenn Kasten
1090b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    fprintf(stderr, "\t after sles_checkSourceSink()\n");
110daf661248ff6ea2e21799e5114c78b7c1d49630eGlenn Kasten
1110b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    // check the audio source and sink parameters against platform support
112daf661248ff6ea2e21799e5114c78b7c1d49630eGlenn Kasten
113daf661248ff6ea2e21799e5114c78b7c1d49630eGlenn Kasten    SLDataSource myAudioSrc;
114daf661248ff6ea2e21799e5114c78b7c1d49630eGlenn Kasten    myAudioSrc.pLocator = &myDataSource.mLocator;
115daf661248ff6ea2e21799e5114c78b7c1d49630eGlenn Kasten    myAudioSrc.pFormat = &myDataSource.mFormat;
116daf661248ff6ea2e21799e5114c78b7c1d49630eGlenn Kasten    SLDataSink myAudioSnk;
117daf661248ff6ea2e21799e5114c78b7c1d49630eGlenn Kasten    myAudioSnk.pLocator = &myDataSink.mLocator;
118daf661248ff6ea2e21799e5114c78b7c1d49630eGlenn Kasten    myAudioSnk.pFormat = &myDataSink.mFormat;
119daf661248ff6ea2e21799e5114c78b7c1d49630eGlenn Kasten
120daf661248ff6ea2e21799e5114c78b7c1d49630eGlenn Kasten    SLuint32 numBuffers = 0;
121daf661248ff6ea2e21799e5114c78b7c1d49630eGlenn Kasten
1220b167267bda99b68346045ccab14e810121d5de4Glenn Kasten#ifdef USE_ANDROID
1230b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    result = sles_to_android_checkAudioPlayerSourceSink(pAudioSrc, pAudioSnk);
124daf661248ff6ea2e21799e5114c78b7c1d49630eGlenn Kasten    if (SL_RESULT_SUCCESS != result)
125daf661248ff6ea2e21799e5114c78b7c1d49630eGlenn Kasten        return result;
1260b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    fprintf(stderr, "\t after sles_to_android_checkAudioPlayerSourceSink()\n");
127daf661248ff6ea2e21799e5114c78b7c1d49630eGlenn Kasten    numBuffers = 4; // FIXME
1280b167267bda99b68346045ccab14e810121d5de4Glenn Kasten#endif
1290b167267bda99b68346045ccab14e810121d5de4Glenn Kasten#ifdef USE_SNDFILE
130daf661248ff6ea2e21799e5114c78b7c1d49630eGlenn Kasten    SLchar *pathname;
131daf661248ff6ea2e21799e5114c78b7c1d49630eGlenn Kasten    result = SndFile_checkAudioPlayerSourceSink(pAudioSrc, pAudioSnk, &pathname, &numBuffers);
132daf661248ff6ea2e21799e5114c78b7c1d49630eGlenn Kasten    if (SL_RESULT_SUCCESS != result)
133daf661248ff6ea2e21799e5114c78b7c1d49630eGlenn Kasten        return result;
1340b167267bda99b68346045ccab14e810121d5de4Glenn Kasten#endif
1350b167267bda99b68346045ccab14e810121d5de4Glenn Kasten
1360b167267bda99b68346045ccab14e810121d5de4Glenn Kasten#ifdef USE_OUTPUTMIXEXT
137daf661248ff6ea2e21799e5114c78b7c1d49630eGlenn Kasten    struct Track *track, *track_;
138daf661248ff6ea2e21799e5114c78b7c1d49630eGlenn Kasten    result = IOutputMixExt_checkAudioPlayerSourceSink(pAudioSrc, pAudioSnk, &track_);
139daf661248ff6ea2e21799e5114c78b7c1d49630eGlenn Kasten    if (SL_RESULT_SUCCESS != result)
140daf661248ff6ea2e21799e5114c78b7c1d49630eGlenn Kasten        return result;
141daf661248ff6ea2e21799e5114c78b7c1d49630eGlenn Kasten    track = track_;
142daf661248ff6ea2e21799e5114c78b7c1d49630eGlenn Kasten#endif
1430b167267bda99b68346045ccab14e810121d5de4Glenn Kasten
1440b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    // Construct our new AudioPlayer instance
145979a3f8743646af9999a89dff9e13b972b7efd87Glenn Kasten    CAudioPlayer *this = (CAudioPlayer *) construct(pCAudioPlayer_class, exposedMask, self);
1466a7bf7733e955d4d89204627c34fb357d542a9ecJean-Michel Trivi    if (NULL == this) {
1470b167267bda99b68346045ccab14e810121d5de4Glenn Kasten        return SL_RESULT_MEMORY_FAILURE;
1486a7bf7733e955d4d89204627c34fb357d542a9ecJean-Michel Trivi    }
1490b167267bda99b68346045ccab14e810121d5de4Glenn Kasten
150daf661248ff6ea2e21799e5114c78b7c1d49630eGlenn Kasten    // Allocate memory for buffer queue
151daf661248ff6ea2e21799e5114c78b7c1d49630eGlenn Kasten    this->mBufferQueue.mNumBuffers = numBuffers;
152daf661248ff6ea2e21799e5114c78b7c1d49630eGlenn Kasten    if (0 != numBuffers) {
1530b167267bda99b68346045ccab14e810121d5de4Glenn Kasten        // inline allocation of circular mArray, up to a typical max
154daf661248ff6ea2e21799e5114c78b7c1d49630eGlenn Kasten        if (BUFFER_HEADER_TYPICAL >= numBuffers) {
1550b167267bda99b68346045ccab14e810121d5de4Glenn Kasten            this->mBufferQueue.mArray = this->mBufferQueue.mTypical;
1560b167267bda99b68346045ccab14e810121d5de4Glenn Kasten        } else {
1570b167267bda99b68346045ccab14e810121d5de4Glenn Kasten            // FIXME integer overflow possible during multiplication
1580b167267bda99b68346045ccab14e810121d5de4Glenn Kasten            this->mBufferQueue.mArray = (struct BufferHeader *)
159daf661248ff6ea2e21799e5114c78b7c1d49630eGlenn Kasten                    malloc((numBuffers + 1) * sizeof(struct BufferHeader));
1600b167267bda99b68346045ccab14e810121d5de4Glenn Kasten            if (NULL == this->mBufferQueue.mArray) {
1610b167267bda99b68346045ccab14e810121d5de4Glenn Kasten                free(this);
1620b167267bda99b68346045ccab14e810121d5de4Glenn Kasten                return SL_RESULT_MEMORY_FAILURE;
1630b167267bda99b68346045ccab14e810121d5de4Glenn Kasten            }
1640b167267bda99b68346045ccab14e810121d5de4Glenn Kasten        }
1650b167267bda99b68346045ccab14e810121d5de4Glenn Kasten        this->mBufferQueue.mFront = this->mBufferQueue.mArray;
1660b167267bda99b68346045ccab14e810121d5de4Glenn Kasten        this->mBufferQueue.mRear = this->mBufferQueue.mArray;
1670b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    }
1680b167267bda99b68346045ccab14e810121d5de4Glenn Kasten
169daf661248ff6ea2e21799e5114c78b7c1d49630eGlenn Kasten    // used to store the data source of our audio player (FIXME - volatile)
1700b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    this->mDynamicSource.mDataSource = pAudioSrc;
171daf661248ff6ea2e21799e5114c78b7c1d49630eGlenn Kasten
172daf661248ff6ea2e21799e5114c78b7c1d49630eGlenn Kasten    // platform-specific initialization
173daf661248ff6ea2e21799e5114c78b7c1d49630eGlenn Kasten
1740b167267bda99b68346045ccab14e810121d5de4Glenn Kasten#ifdef USE_SNDFILE
1750b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    this->mSndFile.mPathname = pathname;
1760b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    this->mSndFile.mIs0 = SL_BOOLEAN_TRUE;
1770b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    this->mSndFile.mSNDFILE = NULL;
1780b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    this->mSndFile.mRetryBuffer = NULL;
1790b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    this->mSndFile.mRetrySize = 0;
1800b167267bda99b68346045ccab14e810121d5de4Glenn Kasten#endif
181daf661248ff6ea2e21799e5114c78b7c1d49630eGlenn Kasten
1820b167267bda99b68346045ccab14e810121d5de4Glenn Kasten#ifdef USE_OUTPUTMIXEXT
183daf661248ff6ea2e21799e5114c78b7c1d49630eGlenn Kasten    // link track to player
184daf661248ff6ea2e21799e5114c78b7c1d49630eGlenn Kasten    // const SLDataFormat_PCM *df_pcm = (SLDataFormat_PCM *) pAudioSrc->pFormat;
185daf661248ff6ea2e21799e5114c78b7c1d49630eGlenn Kasten    // track->mDfPcm = df_pcm;
1860b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    track->mBufferQueue = &this->mBufferQueue;
1870b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    track->mPlay = &this->mPlay;
1880b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    // next 2 fields must be initialized explicitly (not part of this)
1890b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    track->mReader = NULL;
1900b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    track->mAvail = 0;
1910b167267bda99b68346045ccab14e810121d5de4Glenn Kasten#endif
192daf661248ff6ea2e21799e5114c78b7c1d49630eGlenn Kasten
1930b167267bda99b68346045ccab14e810121d5de4Glenn Kasten#ifdef USE_ANDROID
1946a7bf7733e955d4d89204627c34fb357d542a9ecJean-Michel Trivi    sles_to_android_audioPlayerCreate(pAudioSrc, pAudioSnk, this);
1950b167267bda99b68346045ccab14e810121d5de4Glenn Kasten#endif
196daf661248ff6ea2e21799e5114c78b7c1d49630eGlenn Kasten
1970b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    // return the new audio player object
1980b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    *pPlayer = &this->mObject.mItf;
1990b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    return SL_RESULT_SUCCESS;
2000b167267bda99b68346045ccab14e810121d5de4Glenn Kasten}
2010b167267bda99b68346045ccab14e810121d5de4Glenn Kasten
2020b167267bda99b68346045ccab14e810121d5de4Glenn Kastenstatic SLresult IEngine_CreateAudioRecorder(SLEngineItf self,
2030b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    SLObjectItf *pRecorder, SLDataSource *pAudioSrc, SLDataSink *pAudioSnk,
2040b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    SLuint32 numInterfaces, const SLInterfaceID *pInterfaceIds,
2050b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    const SLboolean *pInterfaceRequired)
2060b167267bda99b68346045ccab14e810121d5de4Glenn Kasten{
2070b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    if (NULL == pRecorder)
2080b167267bda99b68346045ccab14e810121d5de4Glenn Kasten        return SL_RESULT_PARAMETER_INVALID;
2090b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    *pRecorder = NULL;
2100b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    unsigned exposedMask;
211979a3f8743646af9999a89dff9e13b972b7efd87Glenn Kasten    const ClassTable *pCAudioRecorder_class = objectIDtoClass(SL_OBJECTID_AUDIORECORDER);
212979a3f8743646af9999a89dff9e13b972b7efd87Glenn Kasten    SLresult result = checkInterfaces(pCAudioRecorder_class, numInterfaces,
2130b167267bda99b68346045ccab14e810121d5de4Glenn Kasten        pInterfaceIds, pInterfaceRequired, &exposedMask);
2140b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    if (SL_RESULT_SUCCESS != result)
2150b167267bda99b68346045ccab14e810121d5de4Glenn Kasten        return result;
2160b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    return SL_RESULT_SUCCESS;
2170b167267bda99b68346045ccab14e810121d5de4Glenn Kasten}
2180b167267bda99b68346045ccab14e810121d5de4Glenn Kasten
2190b167267bda99b68346045ccab14e810121d5de4Glenn Kastenstatic SLresult IEngine_CreateMidiPlayer(SLEngineItf self, SLObjectItf *pPlayer,
2200b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    SLDataSource *pMIDISrc, SLDataSource *pBankSrc, SLDataSink *pAudioOutput,
2210b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    SLDataSink *pVibra, SLDataSink *pLEDArray, SLuint32 numInterfaces,
2220b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    const SLInterfaceID *pInterfaceIds, const SLboolean *pInterfaceRequired)
2230b167267bda99b68346045ccab14e810121d5de4Glenn Kasten{
2240b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    if (NULL == pPlayer)
2250b167267bda99b68346045ccab14e810121d5de4Glenn Kasten        return SL_RESULT_PARAMETER_INVALID;
2260b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    *pPlayer = NULL;
2270b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    unsigned exposedMask;
228979a3f8743646af9999a89dff9e13b972b7efd87Glenn Kasten    const ClassTable *pCMidiPlayer_class = objectIDtoClass(SL_OBJECTID_MIDIPLAYER);
229979a3f8743646af9999a89dff9e13b972b7efd87Glenn Kasten    SLresult result = checkInterfaces(pCMidiPlayer_class, numInterfaces,
2300b167267bda99b68346045ccab14e810121d5de4Glenn Kasten        pInterfaceIds, pInterfaceRequired, &exposedMask);
2310b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    if (SL_RESULT_SUCCESS != result)
2320b167267bda99b68346045ccab14e810121d5de4Glenn Kasten        return result;
2330b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    if (NULL == pMIDISrc || NULL == pAudioOutput)
2340b167267bda99b68346045ccab14e810121d5de4Glenn Kasten        return SL_RESULT_PARAMETER_INVALID;
235daf661248ff6ea2e21799e5114c78b7c1d49630eGlenn Kasten    CMidiPlayer *this = (CMidiPlayer *) construct(pCMidiPlayer_class, exposedMask, self);
2360b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    if (NULL == this)
2370b167267bda99b68346045ccab14e810121d5de4Glenn Kasten        return SL_RESULT_MEMORY_FAILURE;
2380b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    // return the new MIDI player object
2390b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    *pPlayer = &this->mObject.mItf;
2400b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    return SL_RESULT_SUCCESS;
2410b167267bda99b68346045ccab14e810121d5de4Glenn Kasten}
2420b167267bda99b68346045ccab14e810121d5de4Glenn Kasten
2430b167267bda99b68346045ccab14e810121d5de4Glenn Kastenstatic SLresult IEngine_CreateListener(SLEngineItf self, SLObjectItf *pListener,
2440b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    SLuint32 numInterfaces, const SLInterfaceID *pInterfaceIds,
2450b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    const SLboolean *pInterfaceRequired)
2460b167267bda99b68346045ccab14e810121d5de4Glenn Kasten{
2470b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    if (NULL == pListener)
2480b167267bda99b68346045ccab14e810121d5de4Glenn Kasten        return SL_RESULT_PARAMETER_INVALID;
2490b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    *pListener = NULL;
2500b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    unsigned exposedMask;
251979a3f8743646af9999a89dff9e13b972b7efd87Glenn Kasten    const ClassTable *pCListener_class = objectIDtoClass(SL_OBJECTID_LISTENER);
252979a3f8743646af9999a89dff9e13b972b7efd87Glenn Kasten    SLresult result = checkInterfaces(pCListener_class, numInterfaces,
2530b167267bda99b68346045ccab14e810121d5de4Glenn Kasten        pInterfaceIds, pInterfaceRequired, &exposedMask);
2540b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    if (SL_RESULT_SUCCESS != result)
2550b167267bda99b68346045ccab14e810121d5de4Glenn Kasten        return result;
2560b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    return SL_RESULT_FEATURE_UNSUPPORTED;
2570b167267bda99b68346045ccab14e810121d5de4Glenn Kasten}
2580b167267bda99b68346045ccab14e810121d5de4Glenn Kasten
2590b167267bda99b68346045ccab14e810121d5de4Glenn Kastenstatic SLresult IEngine_Create3DGroup(SLEngineItf self, SLObjectItf *pGroup,
2600b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    SLuint32 numInterfaces, const SLInterfaceID *pInterfaceIds,
2610b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    const SLboolean *pInterfaceRequired)
2620b167267bda99b68346045ccab14e810121d5de4Glenn Kasten{
2630b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    if (NULL == pGroup)
2640b167267bda99b68346045ccab14e810121d5de4Glenn Kasten        return SL_RESULT_PARAMETER_INVALID;
2650b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    *pGroup = NULL;
2660b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    unsigned exposedMask;
267979a3f8743646af9999a89dff9e13b972b7efd87Glenn Kasten    const ClassTable *pC3DGroup_class = objectIDtoClass(SL_OBJECTID_3DGROUP);
268979a3f8743646af9999a89dff9e13b972b7efd87Glenn Kasten    SLresult result = checkInterfaces(pC3DGroup_class, numInterfaces,
2690b167267bda99b68346045ccab14e810121d5de4Glenn Kasten        pInterfaceIds, pInterfaceRequired, &exposedMask);
2700b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    if (SL_RESULT_SUCCESS != result)
2710b167267bda99b68346045ccab14e810121d5de4Glenn Kasten        return result;
2720b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    return SL_RESULT_FEATURE_UNSUPPORTED;
2730b167267bda99b68346045ccab14e810121d5de4Glenn Kasten}
2740b167267bda99b68346045ccab14e810121d5de4Glenn Kasten
2750b167267bda99b68346045ccab14e810121d5de4Glenn Kastenstatic SLresult IEngine_CreateOutputMix(SLEngineItf self, SLObjectItf *pMix,
2760b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    SLuint32 numInterfaces, const SLInterfaceID *pInterfaceIds,
2770b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    const SLboolean *pInterfaceRequired)
2780b167267bda99b68346045ccab14e810121d5de4Glenn Kasten{
2790b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    if (NULL == pMix)
2800b167267bda99b68346045ccab14e810121d5de4Glenn Kasten        return SL_RESULT_PARAMETER_INVALID;
2810b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    *pMix = NULL;
2820b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    unsigned exposedMask;
283979a3f8743646af9999a89dff9e13b972b7efd87Glenn Kasten    const ClassTable *pCOutputMix_class = objectIDtoClass(SL_OBJECTID_OUTPUTMIX);
284979a3f8743646af9999a89dff9e13b972b7efd87Glenn Kasten    SLresult result = checkInterfaces(pCOutputMix_class, numInterfaces,
2850b167267bda99b68346045ccab14e810121d5de4Glenn Kasten        pInterfaceIds, pInterfaceRequired, &exposedMask);
2860b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    if (SL_RESULT_SUCCESS != result)
2870b167267bda99b68346045ccab14e810121d5de4Glenn Kasten        return result;
288daf661248ff6ea2e21799e5114c78b7c1d49630eGlenn Kasten    COutputMix *this = (COutputMix *) construct(pCOutputMix_class, exposedMask, self);
2890b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    if (NULL == this)
2900b167267bda99b68346045ccab14e810121d5de4Glenn Kasten        return SL_RESULT_MEMORY_FAILURE;
2910b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    *pMix = &this->mObject.mItf;
2920b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    return SL_RESULT_SUCCESS;
2930b167267bda99b68346045ccab14e810121d5de4Glenn Kasten}
2940b167267bda99b68346045ccab14e810121d5de4Glenn Kasten
2950b167267bda99b68346045ccab14e810121d5de4Glenn Kastenstatic SLresult IEngine_CreateMetadataExtractor(SLEngineItf self,
2960b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    SLObjectItf *pMetadataExtractor, SLDataSource *pDataSource,
2970b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    SLuint32 numInterfaces, const SLInterfaceID *pInterfaceIds,
2980b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    const SLboolean *pInterfaceRequired)
2990b167267bda99b68346045ccab14e810121d5de4Glenn Kasten{
3000b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    if (NULL == pMetadataExtractor)
3010b167267bda99b68346045ccab14e810121d5de4Glenn Kasten        return SL_RESULT_PARAMETER_INVALID;
3020b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    *pMetadataExtractor = NULL;
3030b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    unsigned exposedMask;
304979a3f8743646af9999a89dff9e13b972b7efd87Glenn Kasten    const ClassTable *pCMetadataExtractor_class = objectIDtoClass(SL_OBJECTID_METADATAEXTRACTOR);
305979a3f8743646af9999a89dff9e13b972b7efd87Glenn Kasten    SLresult result = checkInterfaces(pCMetadataExtractor_class, numInterfaces,
3060b167267bda99b68346045ccab14e810121d5de4Glenn Kasten        pInterfaceIds, pInterfaceRequired, &exposedMask);
3070b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    if (SL_RESULT_SUCCESS != result)
3080b167267bda99b68346045ccab14e810121d5de4Glenn Kasten        return result;
3090b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    CMetadataExtractor *this = (CMetadataExtractor *)
310979a3f8743646af9999a89dff9e13b972b7efd87Glenn Kasten        construct(pCMetadataExtractor_class, exposedMask, self);
3110b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    if (NULL == this)
3120b167267bda99b68346045ccab14e810121d5de4Glenn Kasten        return SL_RESULT_MEMORY_FAILURE;
3130b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    *pMetadataExtractor = &this->mObject.mItf;
3140b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    return SL_RESULT_SUCCESS;
3150b167267bda99b68346045ccab14e810121d5de4Glenn Kasten}
3160b167267bda99b68346045ccab14e810121d5de4Glenn Kasten
3170b167267bda99b68346045ccab14e810121d5de4Glenn Kastenstatic SLresult IEngine_CreateExtensionObject(SLEngineItf self,
3180b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    SLObjectItf *pObject, void *pParameters, SLuint32 objectID,
3190b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    SLuint32 numInterfaces, const SLInterfaceID *pInterfaceIds,
3200b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    const SLboolean *pInterfaceRequired)
3210b167267bda99b68346045ccab14e810121d5de4Glenn Kasten{
3220b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    if (NULL == pObject)
3230b167267bda99b68346045ccab14e810121d5de4Glenn Kasten        return SL_RESULT_PARAMETER_INVALID;
3240b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    *pObject = NULL;
3250b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    return SL_RESULT_FEATURE_UNSUPPORTED;
3260b167267bda99b68346045ccab14e810121d5de4Glenn Kasten}
3270b167267bda99b68346045ccab14e810121d5de4Glenn Kasten
3280b167267bda99b68346045ccab14e810121d5de4Glenn Kastenstatic SLresult IEngine_QueryNumSupportedInterfaces(SLEngineItf self,
3290b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    SLuint32 objectID, SLuint32 *pNumSupportedInterfaces)
3300b167267bda99b68346045ccab14e810121d5de4Glenn Kasten{
3310b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    if (NULL == pNumSupportedInterfaces)
3320b167267bda99b68346045ccab14e810121d5de4Glenn Kasten        return SL_RESULT_PARAMETER_INVALID;
3330b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    const ClassTable *class__ = objectIDtoClass(objectID);
3340b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    if (NULL == class__)
3350b167267bda99b68346045ccab14e810121d5de4Glenn Kasten        return SL_RESULT_FEATURE_UNSUPPORTED;
3360b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    *pNumSupportedInterfaces = class__->mInterfaceCount;
3370b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    return SL_RESULT_SUCCESS;
3380b167267bda99b68346045ccab14e810121d5de4Glenn Kasten}
3390b167267bda99b68346045ccab14e810121d5de4Glenn Kasten
3400b167267bda99b68346045ccab14e810121d5de4Glenn Kastenstatic SLresult IEngine_QuerySupportedInterfaces(SLEngineItf self,
3410b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    SLuint32 objectID, SLuint32 index, SLInterfaceID *pInterfaceId)
3420b167267bda99b68346045ccab14e810121d5de4Glenn Kasten{
3430b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    if (NULL == pInterfaceId)
3440b167267bda99b68346045ccab14e810121d5de4Glenn Kasten        return SL_RESULT_PARAMETER_INVALID;
3450b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    const ClassTable *class__ = objectIDtoClass(objectID);
3460b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    if (NULL == class__)
3470b167267bda99b68346045ccab14e810121d5de4Glenn Kasten        return SL_RESULT_FEATURE_UNSUPPORTED;
3480b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    if (index >= class__->mInterfaceCount)
3490b167267bda99b68346045ccab14e810121d5de4Glenn Kasten        return SL_RESULT_PARAMETER_INVALID;
3500b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    *pInterfaceId = &SL_IID_array[class__->mInterfaces[index].mMPH];
3510b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    return SL_RESULT_SUCCESS;
3520b167267bda99b68346045ccab14e810121d5de4Glenn Kasten};
3530b167267bda99b68346045ccab14e810121d5de4Glenn Kasten
3540b167267bda99b68346045ccab14e810121d5de4Glenn Kastenstatic SLresult IEngine_QueryNumSupportedExtensions(SLEngineItf self,
3550b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    SLuint32 *pNumExtensions)
3560b167267bda99b68346045ccab14e810121d5de4Glenn Kasten{
3570b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    if (NULL == pNumExtensions)
3580b167267bda99b68346045ccab14e810121d5de4Glenn Kasten        return SL_RESULT_PARAMETER_INVALID;
3590b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    *pNumExtensions = 0;
3600b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    return SL_RESULT_SUCCESS;
3610b167267bda99b68346045ccab14e810121d5de4Glenn Kasten}
3620b167267bda99b68346045ccab14e810121d5de4Glenn Kasten
3630b167267bda99b68346045ccab14e810121d5de4Glenn Kastenstatic SLresult IEngine_QuerySupportedExtension(SLEngineItf self,
3640b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    SLuint32 index, SLchar *pExtensionName, SLint16 *pNameLength)
3650b167267bda99b68346045ccab14e810121d5de4Glenn Kasten{
3660b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    // any index >= 0 will be >= number of supported extensions
3670b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    return SL_RESULT_PARAMETER_INVALID;
3680b167267bda99b68346045ccab14e810121d5de4Glenn Kasten}
3690b167267bda99b68346045ccab14e810121d5de4Glenn Kasten
3700b167267bda99b68346045ccab14e810121d5de4Glenn Kastenstatic SLresult IEngine_IsExtensionSupported(SLEngineItf self,
3710b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    const SLchar *pExtensionName, SLboolean *pSupported)
3720b167267bda99b68346045ccab14e810121d5de4Glenn Kasten{
3730b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    if (NULL == pExtensionName || NULL == pSupported)
3740b167267bda99b68346045ccab14e810121d5de4Glenn Kasten        return SL_RESULT_PARAMETER_INVALID;
3750b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    *pSupported = SL_BOOLEAN_FALSE;
3760b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    return SL_RESULT_SUCCESS;
3770b167267bda99b68346045ccab14e810121d5de4Glenn Kasten}
3780b167267bda99b68346045ccab14e810121d5de4Glenn Kasten
3790b167267bda99b68346045ccab14e810121d5de4Glenn Kastenstatic const struct SLEngineItf_ IEngine_Itf = {
3800b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    IEngine_CreateLEDDevice,
3810b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    IEngine_CreateVibraDevice,
3820b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    IEngine_CreateAudioPlayer,
3830b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    IEngine_CreateAudioRecorder,
3840b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    IEngine_CreateMidiPlayer,
3850b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    IEngine_CreateListener,
3860b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    IEngine_Create3DGroup,
3870b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    IEngine_CreateOutputMix,
3880b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    IEngine_CreateMetadataExtractor,
3890b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    IEngine_CreateExtensionObject,
3900b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    IEngine_QueryNumSupportedInterfaces,
3910b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    IEngine_QuerySupportedInterfaces,
3920b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    IEngine_QueryNumSupportedExtensions,
3930b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    IEngine_QuerySupportedExtension,
3940b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    IEngine_IsExtensionSupported
3950b167267bda99b68346045ccab14e810121d5de4Glenn Kasten};
3960b167267bda99b68346045ccab14e810121d5de4Glenn Kasten
3970b167267bda99b68346045ccab14e810121d5de4Glenn Kastenvoid IEngine_init(void *self)
3980b167267bda99b68346045ccab14e810121d5de4Glenn Kasten{
3990b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    IEngine *this = (IEngine *) self;
4000b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    this->mItf = &IEngine_Itf;
4010b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    // mLossOfControlGlobal is initialized in CreateEngine
4020b167267bda99b68346045ccab14e810121d5de4Glenn Kasten#ifndef NDEBUG
4030b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    this->mInstanceCount = 0;
4040b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    unsigned i;
4050b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    for (i = 0; i < INSTANCE_MAX; ++i)
4060b167267bda99b68346045ccab14e810121d5de4Glenn Kasten        this->mInstances[i] = NULL;
4070b167267bda99b68346045ccab14e810121d5de4Glenn Kasten#endif
4080b167267bda99b68346045ccab14e810121d5de4Glenn Kasten}
409