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
19a80a6ff9a1f80792478c9d43578afa24a07eb2f0Glenn Kasten#include <endian.h>
20979a3f8743646af9999a89dff9e13b972b7efd87Glenn Kasten#include "sles_allinclusive.h"
21979a3f8743646af9999a89dff9e13b972b7efd87Glenn Kasten
22bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi
23bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi/* Utility functions */
24bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi
25bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivistatic SLresult initializeBufferQueueMembers(CAudioPlayer *ap) {
26bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    // inline allocation of circular mArray, up to a typical max
27bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    if (BUFFER_HEADER_TYPICAL >= ap->mBufferQueue.mNumBuffers) {
28bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi        ap->mBufferQueue.mArray = ap->mBufferQueue.mTypical;
29bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    } else {
30bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi        // Avoid possible integer overflow during multiplication; this arbitrary
31bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi        // maximum is big enough to not interfere with real applications, but
32bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi        // small enough to not overflow.
33bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi        if (ap->mBufferQueue.mNumBuffers >= 256) {
34bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi            return SL_RESULT_MEMORY_FAILURE;
35bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi        }
36bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi        ap->mBufferQueue.mArray = (BufferHeader *)
37bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi                malloc((ap->mBufferQueue.mNumBuffers + 1) * sizeof(BufferHeader));
38bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi        if (NULL == ap->mBufferQueue.mArray) {
39bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi            return SL_RESULT_MEMORY_FAILURE;
40bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi        }
41bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    }
42bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    ap->mBufferQueue.mFront = ap->mBufferQueue.mArray;
43bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    ap->mBufferQueue.mRear = ap->mBufferQueue.mArray;
44bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    return SL_RESULT_SUCCESS;
45bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi}
46bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi
47bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi#ifdef ANDROID
48bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivistatic SLresult initializeAndroidBufferQueueMembers(CAudioPlayer *ap) {
49bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    // Avoid possible integer overflow during multiplication; this arbitrary
50bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    // maximum is big enough to not interfere with real applications, but
51bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    // small enough to not overflow.
52bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    if (ap->mAndroidBufferQueue.mNumBuffers >= 256) {
53bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi        return SL_RESULT_MEMORY_FAILURE;
54bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    }
55bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    ap->mAndroidBufferQueue.mBufferArray = (AdvancedBufferHeader *)
56bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi            malloc( (ap->mAndroidBufferQueue.mNumBuffers + 1) * sizeof(AdvancedBufferHeader));
57bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    if (NULL == ap->mAndroidBufferQueue.mBufferArray) {
58bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi        return SL_RESULT_MEMORY_FAILURE;
59bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    } else {
60bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi
61bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi        // initialize ABQ buffer type
62bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi        // assert below has been checked in android_audioPlayer_checkSourceSink
63bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi        assert(SL_DATAFORMAT_MIME == ap->mDataSource.mFormat.mFormatType);
64ac28eca1df49f581d952ffbda5d3019f7e3b7be6Glenn Kasten        switch (ap->mDataSource.mFormat.mMIME.containerType) {
65bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi          case SL_CONTAINERTYPE_MPEG_TS:
66bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi            ap->mAndroidBufferQueue.mBufferType = kAndroidBufferTypeMpeg2Ts;
67bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi            break;
68bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi          case SL_CONTAINERTYPE_AAC:
69bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi          case SL_CONTAINERTYPE_RAW: {
70bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi            const char* mime = (char*)ap->mDataSource.mFormat.mMIME.mimeType;
71c3b82a293ed06001ba6d50f111608160c6065ef2Glenn Kasten            if ((mime != NULL) && !(strcasecmp(mime, (const char *)SL_ANDROID_MIME_AACADTS) &&
72bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi                    strcasecmp(mime, ANDROID_MIME_AACADTS_ANDROID_FRAMEWORK))) {
73bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi                ap->mAndroidBufferQueue.mBufferType = kAndroidBufferTypeAacadts;
74bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi            } else {
75bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi                ap->mAndroidBufferQueue.mBufferType = kAndroidBufferTypeInvalid;
76bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi                SL_LOGE("CreateAudioPlayer: Invalid buffer type in Android Buffer Queue");
77bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi                return SL_RESULT_CONTENT_UNSUPPORTED;
78bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi            }
79bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi          } break;
80bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi          default:
81bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi            ap->mAndroidBufferQueue.mBufferType = kAndroidBufferTypeInvalid;
82bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi            SL_LOGE("CreateAudioPlayer: Invalid buffer type in Android Buffer Queue");
83bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi            return SL_RESULT_CONTENT_UNSUPPORTED;
84bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi        }
85bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi
86bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi        ap->mAndroidBufferQueue.mFront = ap->mAndroidBufferQueue.mBufferArray;
87bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi        ap->mAndroidBufferQueue.mRear  = ap->mAndroidBufferQueue.mBufferArray;
88bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    }
89bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi
90bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    return SL_RESULT_SUCCESS;
91bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi}
92bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi#endif
93bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi
94bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi
95daf661248ff6ea2e21799e5114c78b7c1d49630eGlenn Kastenstatic SLresult IEngine_CreateLEDDevice(SLEngineItf self, SLObjectItf *pDevice, SLuint32 deviceID,
96daf661248ff6ea2e21799e5114c78b7c1d49630eGlenn Kasten    SLuint32 numInterfaces, const SLInterfaceID *pInterfaceIds, const SLboolean *pInterfaceRequired)
970b167267bda99b68346045ccab14e810121d5de4Glenn Kasten{
98ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten    SL_ENTER_INTERFACE
99ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten
100a3080daa505f91df51a808c85ddb37c48745bf7cGlenn Kasten#if USE_PROFILES & USE_PROFILES_OPTIONAL
101928ea4ffff40c82987cfb1ac9e3095fdc6968709Glenn Kasten    if ((NULL == pDevice) || (SL_DEFAULTDEVICEID_LED != deviceID)) {
102ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten        result = SL_RESULT_PARAMETER_INVALID;
103ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten    } else {
104ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten        *pDevice = NULL;
105ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten        unsigned exposedMask;
106ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten        const ClassTable *pCLEDDevice_class = objectIDtoClass(SL_OBJECTID_LEDDEVICE);
107c2b9d79c4b59caff965076f445f5a735a360b084Glenn Kasten        if (NULL == pCLEDDevice_class) {
108c2b9d79c4b59caff965076f445f5a735a360b084Glenn Kasten            result = SL_RESULT_FEATURE_UNSUPPORTED;
109c2b9d79c4b59caff965076f445f5a735a360b084Glenn Kasten        } else {
110c2b9d79c4b59caff965076f445f5a735a360b084Glenn Kasten            result = checkInterfaces(pCLEDDevice_class, numInterfaces, pInterfaceIds,
11105c7b2d09d54b9260ff7f3f5e491f38d0097c406Glenn Kasten                pInterfaceRequired, &exposedMask, NULL);
112c2b9d79c4b59caff965076f445f5a735a360b084Glenn Kasten        }
113ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten        if (SL_RESULT_SUCCESS == result) {
114bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten            CLEDDevice *thiz = (CLEDDevice *) construct(pCLEDDevice_class, exposedMask, self);
115bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten            if (NULL == thiz) {
116ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten                result = SL_RESULT_MEMORY_FAILURE;
117ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten            } else {
118bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten                thiz->mDeviceID = deviceID;
119bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten                IObject_Publish(&thiz->mObject);
120f51dba65751107c930759938775b75531ec1f330Glenn Kasten                // return the new LED object
121bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten                *pDevice = &thiz->mObject.mItf;
122ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten            }
123ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten        }
124ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten    }
1257a79f519d89eb0e1a5b3f4005484b16d6854d7e2Glenn Kasten#else
1267a79f519d89eb0e1a5b3f4005484b16d6854d7e2Glenn Kasten    result = SL_RESULT_FEATURE_UNSUPPORTED;
1277a79f519d89eb0e1a5b3f4005484b16d6854d7e2Glenn Kasten#endif
128ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten
129ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten    SL_LEAVE_INTERFACE
1300b167267bda99b68346045ccab14e810121d5de4Glenn Kasten}
1310b167267bda99b68346045ccab14e810121d5de4Glenn Kasten
132ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten
133daf661248ff6ea2e21799e5114c78b7c1d49630eGlenn Kastenstatic SLresult IEngine_CreateVibraDevice(SLEngineItf self, SLObjectItf *pDevice, SLuint32 deviceID,
134daf661248ff6ea2e21799e5114c78b7c1d49630eGlenn Kasten    SLuint32 numInterfaces, const SLInterfaceID *pInterfaceIds, const SLboolean *pInterfaceRequired)
1350b167267bda99b68346045ccab14e810121d5de4Glenn Kasten{
136ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten    SL_ENTER_INTERFACE
137ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten
138a3080daa505f91df51a808c85ddb37c48745bf7cGlenn Kasten#if USE_PROFILES & USE_PROFILES_OPTIONAL
139928ea4ffff40c82987cfb1ac9e3095fdc6968709Glenn Kasten    if ((NULL == pDevice) || (SL_DEFAULTDEVICEID_VIBRA != deviceID)) {
140ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten        result = SL_RESULT_PARAMETER_INVALID;
141ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten    } else {
142ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten        *pDevice = NULL;
143ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten        unsigned exposedMask;
144ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten        const ClassTable *pCVibraDevice_class = objectIDtoClass(SL_OBJECTID_VIBRADEVICE);
145c2b9d79c4b59caff965076f445f5a735a360b084Glenn Kasten        if (NULL == pCVibraDevice_class) {
146c2b9d79c4b59caff965076f445f5a735a360b084Glenn Kasten            result = SL_RESULT_FEATURE_UNSUPPORTED;
147c2b9d79c4b59caff965076f445f5a735a360b084Glenn Kasten        } else {
148c2b9d79c4b59caff965076f445f5a735a360b084Glenn Kasten            result = checkInterfaces(pCVibraDevice_class, numInterfaces,
14905c7b2d09d54b9260ff7f3f5e491f38d0097c406Glenn Kasten                pInterfaceIds, pInterfaceRequired, &exposedMask, NULL);
150c2b9d79c4b59caff965076f445f5a735a360b084Glenn Kasten        }
151ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten        if (SL_RESULT_SUCCESS == result) {
152bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten            CVibraDevice *thiz = (CVibraDevice *) construct(pCVibraDevice_class, exposedMask, self);
153bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten            if (NULL == thiz) {
154ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten                result = SL_RESULT_MEMORY_FAILURE;
155ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten            } else {
156bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten                thiz->mDeviceID = deviceID;
157bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten                IObject_Publish(&thiz->mObject);
158f51dba65751107c930759938775b75531ec1f330Glenn Kasten                // return the new vibra object
159bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten                *pDevice = &thiz->mObject.mItf;
160ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten            }
161ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten        }
162ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten    }
1637a79f519d89eb0e1a5b3f4005484b16d6854d7e2Glenn Kasten#else
1647a79f519d89eb0e1a5b3f4005484b16d6854d7e2Glenn Kasten    result = SL_RESULT_FEATURE_UNSUPPORTED;
1657a79f519d89eb0e1a5b3f4005484b16d6854d7e2Glenn Kasten#endif
166ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten
167ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten    SL_LEAVE_INTERFACE
1680b167267bda99b68346045ccab14e810121d5de4Glenn Kasten}
1690b167267bda99b68346045ccab14e810121d5de4Glenn Kasten
170ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten
1710b167267bda99b68346045ccab14e810121d5de4Glenn Kastenstatic SLresult IEngine_CreateAudioPlayer(SLEngineItf self, SLObjectItf *pPlayer,
1720b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    SLDataSource *pAudioSrc, SLDataSink *pAudioSnk, SLuint32 numInterfaces,
1730b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    const SLInterfaceID *pInterfaceIds, const SLboolean *pInterfaceRequired)
1740b167267bda99b68346045ccab14e810121d5de4Glenn Kasten{
175ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten    SL_ENTER_INTERFACE
176ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten
177ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten    if (NULL == pPlayer) {
178ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten       result = SL_RESULT_PARAMETER_INVALID;
179ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten    } else {
180ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten        *pPlayer = NULL;
18105c7b2d09d54b9260ff7f3f5e491f38d0097c406Glenn Kasten        unsigned exposedMask, requiredMask;
182ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten        const ClassTable *pCAudioPlayer_class = objectIDtoClass(SL_OBJECTID_AUDIOPLAYER);
183c2b9d79c4b59caff965076f445f5a735a360b084Glenn Kasten        assert(NULL != pCAudioPlayer_class);
184ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten        result = checkInterfaces(pCAudioPlayer_class, numInterfaces,
18505c7b2d09d54b9260ff7f3f5e491f38d0097c406Glenn Kasten            pInterfaceIds, pInterfaceRequired, &exposedMask, &requiredMask);
186ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten        if (SL_RESULT_SUCCESS == result) {
187ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten
188ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten            // Construct our new AudioPlayer instance
189bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten            CAudioPlayer *thiz = (CAudioPlayer *) construct(pCAudioPlayer_class, exposedMask, self);
190bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten            if (NULL == thiz) {
191ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten                result = SL_RESULT_MEMORY_FAILURE;
192ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten            } else {
193b91e32605ecf39e34ad39936b1ee193bb4e30225Glenn Kasten
194ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten                do {
195daf661248ff6ea2e21799e5114c78b7c1d49630eGlenn Kasten
196ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten                    // Initialize private fields not associated with an interface
197a9a70a4451545034c9263dd55b181f2912534c37Glenn Kasten
198a9a70a4451545034c9263dd55b181f2912534c37Glenn Kasten                    // Default data source in case of failure in checkDataSource
199bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten                    thiz->mDataSource.mLocator.mLocatorType = SL_DATALOCATOR_NULL;
200bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten                    thiz->mDataSource.mFormat.mFormatType = SL_DATAFORMAT_NULL;
201a9a70a4451545034c9263dd55b181f2912534c37Glenn Kasten
202a9a70a4451545034c9263dd55b181f2912534c37Glenn Kasten                    // Default data sink in case of failure in checkDataSink
203bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten                    thiz->mDataSink.mLocator.mLocatorType = SL_DATALOCATOR_NULL;
204bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten                    thiz->mDataSink.mFormat.mFormatType = SL_DATAFORMAT_NULL;
205a9a70a4451545034c9263dd55b181f2912534c37Glenn Kasten
206a9a70a4451545034c9263dd55b181f2912534c37Glenn Kasten                    // Default is no per-channel mute or solo
207bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten                    thiz->mMuteMask = 0;
208bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten                    thiz->mSoloMask = 0;
209a9a70a4451545034c9263dd55b181f2912534c37Glenn Kasten
210f6f5ceb363286d5ebef2c2e70c8a5aa135d5d1eeGlenn Kasten                    // Will be set soon for PCM buffer queues, or later by platform-specific code
211f6f5ceb363286d5ebef2c2e70c8a5aa135d5d1eeGlenn Kasten                    // during Realize or Prefetch
2121a9c2615d0933d183fcb1b9e34ec8f0da2a85153Glenn Kasten                    thiz->mNumChannels = UNKNOWN_NUMCHANNELS;
2131a9c2615d0933d183fcb1b9e34ec8f0da2a85153Glenn Kasten                    thiz->mSampleRateMilliHz = UNKNOWN_SAMPLERATE;
214daf661248ff6ea2e21799e5114c78b7c1d49630eGlenn Kasten
215a9a70a4451545034c9263dd55b181f2912534c37Glenn Kasten                    // More default values, in case destructor needs to be called early
21672042d4448cee63528c619537321ba73944c6382Glenn Kasten                    thiz->mDirectLevel = 0; // no attenuation
217a9a70a4451545034c9263dd55b181f2912534c37Glenn Kasten#ifdef USE_OUTPUTMIXEXT
218bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten                    thiz->mTrack = NULL;
219bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten                    thiz->mGains[0] = 1.0f;
220bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten                    thiz->mGains[1] = 1.0f;
221bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten                    thiz->mDestroyRequested = SL_BOOLEAN_FALSE;
222a9a70a4451545034c9263dd55b181f2912534c37Glenn Kasten#endif
223a9a70a4451545034c9263dd55b181f2912534c37Glenn Kasten#ifdef USE_SNDFILE
224bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten                    thiz->mSndFile.mPathname = NULL;
225bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten                    thiz->mSndFile.mSNDFILE = NULL;
226bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten                    memset(&thiz->mSndFile.mSfInfo, 0, sizeof(SF_INFO));
227bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten                    memset(&thiz->mSndFile.mMutex, 0, sizeof(pthread_mutex_t));
228bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten                    thiz->mSndFile.mEOF = SL_BOOLEAN_FALSE;
229bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten                    thiz->mSndFile.mWhich = 0;
230bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten                    memset(thiz->mSndFile.mBuffer, 0, sizeof(thiz->mSndFile.mBuffer));
231a9a70a4451545034c9263dd55b181f2912534c37Glenn Kasten#endif
232a9a70a4451545034c9263dd55b181f2912534c37Glenn Kasten#ifdef ANDROID
233a9a70a4451545034c9263dd55b181f2912534c37Glenn Kasten                    // placement new (explicit constructor)
23413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                    // FIXME unnecessary once those fields are encapsulated in one class, rather
23513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                    //   than a structure
236ae1a5c8dc1fccf7c121eda830f8d23bd094c0f16Glenn Kasten                    (void) new (&thiz->mAudioTrack) android::sp<android::AudioTrack>();
2376cce136651f6fd2c7aecd45bc553270152d75462Jean-Michel Trivi                    (void) new (&thiz->mCallbackProtector)
2386cce136651f6fd2c7aecd45bc553270152d75462Jean-Michel Trivi                            android::sp<android::CallbackProtector>();
239bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten                    (void) new (&thiz->mAuxEffect) android::sp<android::AudioEffect>();
24013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                    (void) new (&thiz->mAPlayer) android::sp<android::GenericPlayer>();
24172042d4448cee63528c619537321ba73944c6382Glenn Kasten                    // Android-specific POD fields are initialized in android_audioPlayer_create,
24272042d4448cee63528c619537321ba73944c6382Glenn Kasten                    // and assume calloc or memset 0 during allocation
243a9a70a4451545034c9263dd55b181f2912534c37Glenn Kasten#endif
244a9a70a4451545034c9263dd55b181f2912534c37Glenn Kasten
245ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten                    // Check the source and sink parameters against generic constraints,
246ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten                    // and make a local copy of all parameters in case other application threads
247ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten                    // change memory concurrently.
248daf661248ff6ea2e21799e5114c78b7c1d49630eGlenn Kasten
249bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten                    result = checkDataSource("pAudioSrc", pAudioSrc, &thiz->mDataSource,
25094a37e8117fb72790882dfb815f99e2365754c74Glenn Kasten                            DATALOCATOR_MASK_URI | DATALOCATOR_MASK_ADDRESS |
25194a37e8117fb72790882dfb815f99e2365754c74Glenn Kasten                            DATALOCATOR_MASK_BUFFERQUEUE
25294a37e8117fb72790882dfb815f99e2365754c74Glenn Kasten#ifdef ANDROID
25394a37e8117fb72790882dfb815f99e2365754c74Glenn Kasten                            | DATALOCATOR_MASK_ANDROIDFD | DATALOCATOR_MASK_ANDROIDSIMPLEBUFFERQUEUE
254c869defb7c0dd6f4e45ad26abb0f8ccc960152deJean-Michel Trivi                            | DATALOCATOR_MASK_ANDROIDBUFFERQUEUE
25594a37e8117fb72790882dfb815f99e2365754c74Glenn Kasten#endif
256e57c13397185f9ad0f162855e9a8ebeb0c94bfc4Andy Hung                            , DATAFORMAT_MASK_MIME | DATAFORMAT_MASK_PCM | DATAFORMAT_MASK_PCM_EX);
257c869defb7c0dd6f4e45ad26abb0f8ccc960152deJean-Michel Trivi
2583c170255cc71942f310b676d968cf73328aa5d70Jean-Michel Trivi                    if (SL_RESULT_SUCCESS != result) {
2593c170255cc71942f310b676d968cf73328aa5d70Jean-Michel Trivi                        break;
2603c170255cc71942f310b676d968cf73328aa5d70Jean-Michel Trivi                    }
2613c170255cc71942f310b676d968cf73328aa5d70Jean-Michel Trivi
262bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten                    result = checkDataSink("pAudioSnk", pAudioSnk, &thiz->mDataSink,
26313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                            DATALOCATOR_MASK_OUTPUTMIX                  // for playback
26413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi#ifdef ANDROID
26513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                            | DATALOCATOR_MASK_ANDROIDSIMPLEBUFFERQUEUE // for decode to a BQ
26613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                            | DATALOCATOR_MASK_BUFFERQUEUE              // for decode to a BQ
26713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi#endif
26813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                            , DATAFORMAT_MASK_NULL
26913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi#ifdef ANDROID
270e57c13397185f9ad0f162855e9a8ebeb0c94bfc4Andy Hung                            | DATAFORMAT_MASK_PCM | DATAFORMAT_MASK_PCM_EX  // for decode to PCM
27113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi#endif
27213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                            );
2733c170255cc71942f310b676d968cf73328aa5d70Jean-Michel Trivi                    if (SL_RESULT_SUCCESS != result) {
274ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten                        break;
2753c170255cc71942f310b676d968cf73328aa5d70Jean-Michel Trivi                    }
276daf661248ff6ea2e21799e5114c78b7c1d49630eGlenn Kasten
277f6f5ceb363286d5ebef2c2e70c8a5aa135d5d1eeGlenn Kasten                    // It would be unsafe to ever refer to the application pointers again
278f6f5ceb363286d5ebef2c2e70c8a5aa135d5d1eeGlenn Kasten                    pAudioSrc = NULL;
279f6f5ceb363286d5ebef2c2e70c8a5aa135d5d1eeGlenn Kasten                    pAudioSnk = NULL;
280f6f5ceb363286d5ebef2c2e70c8a5aa135d5d1eeGlenn Kasten
2817965455f86c21d6e1f788b284f5fc829e82ff2b5Glenn Kasten                    // Check that the requested interfaces are compatible with data source and sink
2827965455f86c21d6e1f788b284f5fc829e82ff2b5Glenn Kasten                    result = checkSourceSinkVsInterfacesCompatibility(&thiz->mDataSource,
28305c7b2d09d54b9260ff7f3f5e491f38d0097c406Glenn Kasten                            &thiz->mDataSink, pCAudioPlayer_class, requiredMask);
2843c170255cc71942f310b676d968cf73328aa5d70Jean-Michel Trivi                    if (SL_RESULT_SUCCESS != result) {
285ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten                        break;
2863c170255cc71942f310b676d968cf73328aa5d70Jean-Michel Trivi                    }
287daf661248ff6ea2e21799e5114c78b7c1d49630eGlenn Kasten
28813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                    // copy the buffer queue count from source locator (for playback) / from the
28913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                    // sink locator (for decode on ANDROID build) to the buffer queue interface
29018abcc4b70fab1f84d6fbebac3a8e34480a6c4d3Glenn Kasten                    // we have already range-checked the value down to a smaller width
29113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                    SLuint16 nbBuffers = 0;
292d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi                    bool usesAdvancedBufferHeaders = false;
293bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi                    bool usesSimpleBufferQueue = false;
294bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi                    // creating an AudioPlayer which decodes AAC ADTS buffers to a PCM buffer queue
295bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi                    //  will cause usesAdvancedBufferHeaders and usesSimpleBufferQueue to be true
296bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten                    switch (thiz->mDataSource.mLocator.mLocatorType) {
29701e9f5fa4698856f92bcfd88188ee4c8397b22dbGlenn Kasten                    case SL_DATALOCATOR_BUFFERQUEUE:
29801e9f5fa4698856f92bcfd88188ee4c8397b22dbGlenn Kasten#ifdef ANDROID
29901e9f5fa4698856f92bcfd88188ee4c8397b22dbGlenn Kasten                    case SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE:
30001e9f5fa4698856f92bcfd88188ee4c8397b22dbGlenn Kasten#endif
301bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi                        usesSimpleBufferQueue = true;
30213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                        nbBuffers = (SLuint16) thiz->mDataSource.mLocator.mBufferQueue.numBuffers;
303e57c13397185f9ad0f162855e9a8ebeb0c94bfc4Andy Hung                        assert(SL_DATAFORMAT_PCM == thiz->mDataSource.mFormat.mFormatType
304e57c13397185f9ad0f162855e9a8ebeb0c94bfc4Andy Hung                                || SL_ANDROID_DATAFORMAT_PCM_EX
305e57c13397185f9ad0f162855e9a8ebeb0c94bfc4Andy Hung                                    == thiz->mDataSource.mFormat.mFormatType);
306bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten                        thiz->mNumChannels = thiz->mDataSource.mFormat.mPCM.numChannels;
307bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten                        thiz->mSampleRateMilliHz = thiz->mDataSource.mFormat.mPCM.samplesPerSec;
30801e9f5fa4698856f92bcfd88188ee4c8397b22dbGlenn Kasten                        break;
309d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi#ifdef ANDROID
310d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi                    case SL_DATALOCATOR_ANDROIDBUFFERQUEUE:
311d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi                        usesAdvancedBufferHeaders = true;
312bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi                        nbBuffers = (SLuint16) thiz->mDataSource.mLocator.mABQ.numBuffers;
313d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi                        thiz->mAndroidBufferQueue.mNumBuffers = nbBuffers;
314d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi                        break;
315d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi#endif
31601e9f5fa4698856f92bcfd88188ee4c8397b22dbGlenn Kasten                    default:
31713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                        nbBuffers = 0;
31801e9f5fa4698856f92bcfd88188ee4c8397b22dbGlenn Kasten                        break;
31901e9f5fa4698856f92bcfd88188ee4c8397b22dbGlenn Kasten                    }
32013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi#ifdef ANDROID
321ac28eca1df49f581d952ffbda5d3019f7e3b7be6Glenn Kasten                    switch (thiz->mDataSink.mLocator.mLocatorType) {
32213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                    case SL_DATALOCATOR_BUFFERQUEUE:
32313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                    case SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE:
324bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi                        usesSimpleBufferQueue = true;
32513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                        nbBuffers = thiz->mDataSink.mLocator.mBufferQueue.numBuffers;
326e57c13397185f9ad0f162855e9a8ebeb0c94bfc4Andy Hung                        assert(SL_DATAFORMAT_PCM == thiz->mDataSink.mFormat.mFormatType
327e57c13397185f9ad0f162855e9a8ebeb0c94bfc4Andy Hung                                || SL_ANDROID_DATAFORMAT_PCM_EX
328e57c13397185f9ad0f162855e9a8ebeb0c94bfc4Andy Hung                                    == thiz->mDataSink.mFormat.mFormatType);
329e878c470cf58c8654d613ab2449468b44a90d6e5Glenn Kasten                        // FIXME The values specified by the app are meaningless. We get the
330e878c470cf58c8654d613ab2449468b44a90d6e5Glenn Kasten                        // real values from the decoder.  But the data sink checks currently require
331e878c470cf58c8654d613ab2449468b44a90d6e5Glenn Kasten                        // that the app specify these useless values.  Needs doc/fix.
332e878c470cf58c8654d613ab2449468b44a90d6e5Glenn Kasten                        // Instead use the "unknown" values, as needed by prepare completion.
333e878c470cf58c8654d613ab2449468b44a90d6e5Glenn Kasten                        // thiz->mNumChannels = thiz->mDataSink.mFormat.mPCM.numChannels;
334e878c470cf58c8654d613ab2449468b44a90d6e5Glenn Kasten                        // thiz->mSampleRateMilliHz = thiz->mDataSink.mFormat.mPCM.samplesPerSec;
335e878c470cf58c8654d613ab2449468b44a90d6e5Glenn Kasten                        thiz->mNumChannels = UNKNOWN_NUMCHANNELS;
336e878c470cf58c8654d613ab2449468b44a90d6e5Glenn Kasten                        thiz->mSampleRateMilliHz = UNKNOWN_SAMPLERATE;
33713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                        break;
33813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                    default:
33913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                        // leave nbBuffers unchanged
34013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                        break;
34113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                    }
34213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi#endif
34313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                    thiz->mBufferQueue.mNumBuffers = nbBuffers;
3444b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten
345ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten                    // check the audio source and sink parameters against platform support
346ef8931ae547cd703e69df9ad350d69825da0f546Jean-Michel Trivi#ifdef ANDROID
347bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten                    result = android_audioPlayer_checkSourceSink(thiz);
348928ea4ffff40c82987cfb1ac9e3095fdc6968709Glenn Kasten                    if (SL_RESULT_SUCCESS != result) {
349ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten                        break;
350928ea4ffff40c82987cfb1ac9e3095fdc6968709Glenn Kasten                    }
3510b167267bda99b68346045ccab14e810121d5de4Glenn Kasten#endif
352acd88797a1d3b8225bab888d29036e245f275be5Glenn Kasten
3530b167267bda99b68346045ccab14e810121d5de4Glenn Kasten#ifdef USE_SNDFILE
354bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten                    result = SndFile_checkAudioPlayerSourceSink(thiz);
355928ea4ffff40c82987cfb1ac9e3095fdc6968709Glenn Kasten                    if (SL_RESULT_SUCCESS != result) {
356ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten                        break;
357928ea4ffff40c82987cfb1ac9e3095fdc6968709Glenn Kasten                    }
3580b167267bda99b68346045ccab14e810121d5de4Glenn Kasten#endif
3590b167267bda99b68346045ccab14e810121d5de4Glenn Kasten
3600b167267bda99b68346045ccab14e810121d5de4Glenn Kasten#ifdef USE_OUTPUTMIXEXT
361bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten                    result = IOutputMixExt_checkAudioPlayerSourceSink(thiz);
362928ea4ffff40c82987cfb1ac9e3095fdc6968709Glenn Kasten                    if (SL_RESULT_SUCCESS != result) {
363ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten                        break;
364928ea4ffff40c82987cfb1ac9e3095fdc6968709Glenn Kasten                    }
365daf661248ff6ea2e21799e5114c78b7c1d49630eGlenn Kasten#endif
3660b167267bda99b68346045ccab14e810121d5de4Glenn Kasten
367ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten                    // Allocate memory for buffer queue
368d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi                    if (usesAdvancedBufferHeaders) {
369f6c0c2346b2ca1ac01c2d68a2cd2a7f1d9ff3427Glenn Kasten#ifdef ANDROID
370d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi                        // locator is SL_DATALOCATOR_ANDROIDBUFFERQUEUE
371bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi                        result = initializeAndroidBufferQueueMembers(thiz);
372f6c0c2346b2ca1ac01c2d68a2cd2a7f1d9ff3427Glenn Kasten#else
373f6c0c2346b2ca1ac01c2d68a2cd2a7f1d9ff3427Glenn Kasten                        assert(false);
374f6c0c2346b2ca1ac01c2d68a2cd2a7f1d9ff3427Glenn Kasten#endif
375bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi                    }
376bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi
377bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi                    if (usesSimpleBufferQueue) {
378d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi                        // locator is SL_DATALOCATOR_BUFFERQUEUE
379d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi                        //         or SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE
380bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi                        result = initializeBufferQueueMembers(thiz);
381d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi                    }
382ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten
383d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi                    // used to store the data source of our audio player
384d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi                    thiz->mDynamicSource.mDataSource = &thiz->mDataSource.u.mSource;
385ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten
386d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi                    // platform-specific initialization
387ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten#ifdef ANDROID
388d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi                    android_audioPlayer_create(thiz);
389ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten#endif
3900b167267bda99b68346045ccab14e810121d5de4Glenn Kasten
391ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten                } while (0);
392daf661248ff6ea2e21799e5114c78b7c1d49630eGlenn Kasten
393e5d006b298ce7683d66f7ec86136403cf5fb20d6Glenn Kasten                if (SL_RESULT_SUCCESS != result) {
394bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten                    IObject_Destroy(&thiz->mObject.mItf);
395f51dba65751107c930759938775b75531ec1f330Glenn Kasten                } else {
396bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten                    IObject_Publish(&thiz->mObject);
397f51dba65751107c930759938775b75531ec1f330Glenn Kasten                    // return the new audio player object
398bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten                    *pPlayer = &thiz->mObject.mItf;
399e5d006b298ce7683d66f7ec86136403cf5fb20d6Glenn Kasten                }
400daf661248ff6ea2e21799e5114c78b7c1d49630eGlenn Kasten
401ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten            }
402ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten        }
403acd88797a1d3b8225bab888d29036e245f275be5Glenn Kasten
404ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten    }
405ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten
406ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten    SL_LEAVE_INTERFACE
4070b167267bda99b68346045ccab14e810121d5de4Glenn Kasten}
4080b167267bda99b68346045ccab14e810121d5de4Glenn Kasten
409ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten
410ccdf07b17f23b4c040dd3f62478d0965eba804e3Glenn Kastenstatic SLresult IEngine_CreateAudioRecorder(SLEngineItf self, SLObjectItf *pRecorder,
411ccdf07b17f23b4c040dd3f62478d0965eba804e3Glenn Kasten    SLDataSource *pAudioSrc, SLDataSink *pAudioSnk, SLuint32 numInterfaces,
412ccdf07b17f23b4c040dd3f62478d0965eba804e3Glenn Kasten    const SLInterfaceID *pInterfaceIds, const SLboolean *pInterfaceRequired)
4130b167267bda99b68346045ccab14e810121d5de4Glenn Kasten{
414ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten    SL_ENTER_INTERFACE
415ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten
416a3080daa505f91df51a808c85ddb37c48745bf7cGlenn Kasten#if (USE_PROFILES & USE_PROFILES_OPTIONAL) || defined(ANDROID)
417ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten    if (NULL == pRecorder) {
418ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten        result = SL_RESULT_PARAMETER_INVALID;
419ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten    } else {
420ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten        *pRecorder = NULL;
421ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten        unsigned exposedMask;
422ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten        const ClassTable *pCAudioRecorder_class = objectIDtoClass(SL_OBJECTID_AUDIORECORDER);
423c2b9d79c4b59caff965076f445f5a735a360b084Glenn Kasten        if (NULL == pCAudioRecorder_class) {
424c2b9d79c4b59caff965076f445f5a735a360b084Glenn Kasten            result = SL_RESULT_FEATURE_UNSUPPORTED;
425c2b9d79c4b59caff965076f445f5a735a360b084Glenn Kasten        } else {
426c2b9d79c4b59caff965076f445f5a735a360b084Glenn Kasten            result = checkInterfaces(pCAudioRecorder_class, numInterfaces,
42705c7b2d09d54b9260ff7f3f5e491f38d0097c406Glenn Kasten                    pInterfaceIds, pInterfaceRequired, &exposedMask, NULL);
428c2b9d79c4b59caff965076f445f5a735a360b084Glenn Kasten        }
4293af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
430ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten        if (SL_RESULT_SUCCESS == result) {
431ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten
432ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten            // Construct our new AudioRecorder instance
433bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten            CAudioRecorder *thiz = (CAudioRecorder *) construct(pCAudioRecorder_class, exposedMask,
4343af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                    self);
435bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten            if (NULL == thiz) {
436ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten                result = SL_RESULT_MEMORY_FAILURE;
437ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten            } else {
438ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten
439ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten                do {
440f6f5ceb363286d5ebef2c2e70c8a5aa135d5d1eeGlenn Kasten
441f6f5ceb363286d5ebef2c2e70c8a5aa135d5d1eeGlenn Kasten                    // Initialize fields not associated with any interface
442f6f5ceb363286d5ebef2c2e70c8a5aa135d5d1eeGlenn Kasten
443a9a70a4451545034c9263dd55b181f2912534c37Glenn Kasten                    // Default data source in case of failure in checkDataSource
444bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten                    thiz->mDataSource.mLocator.mLocatorType = SL_DATALOCATOR_NULL;
445bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten                    thiz->mDataSource.mFormat.mFormatType = SL_DATAFORMAT_NULL;
446a9a70a4451545034c9263dd55b181f2912534c37Glenn Kasten
447a9a70a4451545034c9263dd55b181f2912534c37Glenn Kasten                    // Default data sink in case of failure in checkDataSink
448bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten                    thiz->mDataSink.mLocator.mLocatorType = SL_DATALOCATOR_NULL;
449bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten                    thiz->mDataSink.mFormat.mFormatType = SL_DATAFORMAT_NULL;
450a9a70a4451545034c9263dd55b181f2912534c37Glenn Kasten
451f6f5ceb363286d5ebef2c2e70c8a5aa135d5d1eeGlenn Kasten                    // These fields are set to real values by
45200a66a027199238a93b2aa9056af126efc09fe38Glenn Kasten                    // android_audioRecorder_checkSourceSink.  Note that the data sink is
453f6f5ceb363286d5ebef2c2e70c8a5aa135d5d1eeGlenn Kasten                    // always PCM buffer queue, so we know the channel count and sample rate early.
4541a9c2615d0933d183fcb1b9e34ec8f0da2a85153Glenn Kasten                    thiz->mNumChannels = UNKNOWN_NUMCHANNELS;
4551a9c2615d0933d183fcb1b9e34ec8f0da2a85153Glenn Kasten                    thiz->mSampleRateMilliHz = UNKNOWN_SAMPLERATE;
456f6f5ceb363286d5ebef2c2e70c8a5aa135d5d1eeGlenn Kasten#ifdef ANDROID
457e5ede1a139fcedbf075675179d919fbe731898f0Glenn Kasten                    // placement new (explicit constructor)
458e5ede1a139fcedbf075675179d919fbe731898f0Glenn Kasten                    // FIXME unnecessary once those fields are encapsulated in one class, rather
459e5ede1a139fcedbf075675179d919fbe731898f0Glenn Kasten                    //   than a structure
460e5ede1a139fcedbf075675179d919fbe731898f0Glenn Kasten                    (void) new (&thiz->mAudioRecord) android::sp<android::AudioRecord>();
461460bdad43aaec3c6ffe7f259719e00807742ad6dGlenn Kasten                    (void) new (&thiz->mCallbackProtector)
462460bdad43aaec3c6ffe7f259719e00807742ad6dGlenn Kasten                            android::sp<android::CallbackProtector>();
463ca39f4b4dbeb920a5b97bd65be73f2f7cac77431Dima Zavin                    thiz->mRecordSource = AUDIO_SOURCE_DEFAULT;
464f6f5ceb363286d5ebef2c2e70c8a5aa135d5d1eeGlenn Kasten#endif
465712b490060e4164fbe47986be1d2584d1610d8ddJean-Michel Trivi
466ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten                    // Check the source and sink parameters, and make a local copy of all parameters
467bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten                    result = checkDataSource("pAudioSrc", pAudioSrc, &thiz->mDataSource,
46894a37e8117fb72790882dfb815f99e2365754c74Glenn Kasten                            DATALOCATOR_MASK_IODEVICE, DATAFORMAT_MASK_NULL);
469928ea4ffff40c82987cfb1ac9e3095fdc6968709Glenn Kasten                    if (SL_RESULT_SUCCESS != result) {
470ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten                        break;
471928ea4ffff40c82987cfb1ac9e3095fdc6968709Glenn Kasten                    }
472bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten                    result = checkDataSink("pAudioSnk", pAudioSnk, &thiz->mDataSink,
47394a37e8117fb72790882dfb815f99e2365754c74Glenn Kasten                            DATALOCATOR_MASK_URI
47494a37e8117fb72790882dfb815f99e2365754c74Glenn Kasten#ifdef ANDROID
47594a37e8117fb72790882dfb815f99e2365754c74Glenn Kasten                            | DATALOCATOR_MASK_ANDROIDSIMPLEBUFFERQUEUE
47694a37e8117fb72790882dfb815f99e2365754c74Glenn Kasten#endif
477e57c13397185f9ad0f162855e9a8ebeb0c94bfc4Andy Hung                            , DATAFORMAT_MASK_MIME | DATAFORMAT_MASK_PCM | DATAFORMAT_MASK_PCM_EX
47894a37e8117fb72790882dfb815f99e2365754c74Glenn Kasten                    );
479928ea4ffff40c82987cfb1ac9e3095fdc6968709Glenn Kasten                    if (SL_RESULT_SUCCESS != result) {
480ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten                        break;
481928ea4ffff40c82987cfb1ac9e3095fdc6968709Glenn Kasten                    }
482ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten
483f6f5ceb363286d5ebef2c2e70c8a5aa135d5d1eeGlenn Kasten                    // It would be unsafe to ever refer to the application pointers again
484f6f5ceb363286d5ebef2c2e70c8a5aa135d5d1eeGlenn Kasten                    pAudioSrc = NULL;
485f6f5ceb363286d5ebef2c2e70c8a5aa135d5d1eeGlenn Kasten                    pAudioSnk = NULL;
486f6f5ceb363286d5ebef2c2e70c8a5aa135d5d1eeGlenn Kasten
4873af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                    // check the audio source and sink parameters against platform support
4883af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi#ifdef ANDROID
48900a66a027199238a93b2aa9056af126efc09fe38Glenn Kasten                    result = android_audioRecorder_checkSourceSink(thiz);
4903af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                    if (SL_RESULT_SUCCESS != result) {
491a7b79e766ec6d95e9236168c27461c2ebaef4659Glenn Kasten                        SL_LOGE("Cannot create AudioRecorder: invalid source or sink");
4923af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                        break;
4933af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                    }
4943af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi#endif
4953af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
4964b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten#ifdef ANDROID
4979e60b0a390d780539459f41c2bf4a45a326a7b62Glenn Kasten                    // Allocate memory for buffer queue
498bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten                    SLuint32 locatorType = thiz->mDataSink.mLocator.mLocatorType;
49901e9f5fa4698856f92bcfd88188ee4c8397b22dbGlenn Kasten                    if (locatorType == SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE) {
500bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten                        thiz->mBufferQueue.mNumBuffers =
501bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten                            thiz->mDataSink.mLocator.mBufferQueue.numBuffers;
5023af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                        // inline allocation of circular Buffer Queue mArray, up to a typical max
503bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten                        if (BUFFER_HEADER_TYPICAL >= thiz->mBufferQueue.mNumBuffers) {
504bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten                            thiz->mBufferQueue.mArray = thiz->mBufferQueue.mTypical;
5053af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                        } else {
5063af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                            // Avoid possible integer overflow during multiplication; this arbitrary
5073af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                            // maximum is big enough to not interfere with real applications, but
5083af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                            // small enough to not overflow.
509bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten                            if (thiz->mBufferQueue.mNumBuffers >= 256) {
5103af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                                result = SL_RESULT_MEMORY_FAILURE;
5113af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                                break;
5123af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                            }
513bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten                            thiz->mBufferQueue.mArray = (BufferHeader *) malloc((thiz->mBufferQueue.
5143af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                                    mNumBuffers + 1) * sizeof(BufferHeader));
515bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten                            if (NULL == thiz->mBufferQueue.mArray) {
5163af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                                result = SL_RESULT_MEMORY_FAILURE;
5173af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                                break;
5183af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                            }
5193af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                        }
520bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten                        thiz->mBufferQueue.mFront = thiz->mBufferQueue.mArray;
521bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten                        thiz->mBufferQueue.mRear = thiz->mBufferQueue.mArray;
5223af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                    }
5234b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten#endif
5243af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
5253af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                    // platform-specific initialization
5263af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi#ifdef ANDROID
527bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten                    android_audioRecorder_create(thiz);
5283af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi#endif
529ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten
530ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten                } while (0);
531ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten
532928ea4ffff40c82987cfb1ac9e3095fdc6968709Glenn Kasten                if (SL_RESULT_SUCCESS != result) {
533bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten                    IObject_Destroy(&thiz->mObject.mItf);
534f51dba65751107c930759938775b75531ec1f330Glenn Kasten                } else {
535bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten                    IObject_Publish(&thiz->mObject);
536f51dba65751107c930759938775b75531ec1f330Glenn Kasten                    // return the new audio recorder object
537bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten                    *pRecorder = &thiz->mObject.mItf;
538928ea4ffff40c82987cfb1ac9e3095fdc6968709Glenn Kasten                }
539ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten            }
540ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten
541ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten        }
542ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten
543ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten    }
5447a79f519d89eb0e1a5b3f4005484b16d6854d7e2Glenn Kasten#else
5457a79f519d89eb0e1a5b3f4005484b16d6854d7e2Glenn Kasten    result = SL_RESULT_FEATURE_UNSUPPORTED;
5467a79f519d89eb0e1a5b3f4005484b16d6854d7e2Glenn Kasten#endif
547ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten
548ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten    SL_LEAVE_INTERFACE
5490b167267bda99b68346045ccab14e810121d5de4Glenn Kasten}
5500b167267bda99b68346045ccab14e810121d5de4Glenn Kasten
551ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten
5520b167267bda99b68346045ccab14e810121d5de4Glenn Kastenstatic SLresult IEngine_CreateMidiPlayer(SLEngineItf self, SLObjectItf *pPlayer,
5530b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    SLDataSource *pMIDISrc, SLDataSource *pBankSrc, SLDataSink *pAudioOutput,
5540b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    SLDataSink *pVibra, SLDataSink *pLEDArray, SLuint32 numInterfaces,
5550b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    const SLInterfaceID *pInterfaceIds, const SLboolean *pInterfaceRequired)
5560b167267bda99b68346045ccab14e810121d5de4Glenn Kasten{
557ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten    SL_ENTER_INTERFACE
558ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten
559a3080daa505f91df51a808c85ddb37c48745bf7cGlenn Kasten#if USE_PROFILES & (USE_PROFILES_GAME | USE_PROFILES_PHONE)
560928ea4ffff40c82987cfb1ac9e3095fdc6968709Glenn Kasten    if ((NULL == pPlayer) || (NULL == pMIDISrc) || (NULL == pAudioOutput)) {
561ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten        result = SL_RESULT_PARAMETER_INVALID;
562ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten    } else {
563ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten        *pPlayer = NULL;
564ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten        unsigned exposedMask;
565ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten        const ClassTable *pCMidiPlayer_class = objectIDtoClass(SL_OBJECTID_MIDIPLAYER);
566c2b9d79c4b59caff965076f445f5a735a360b084Glenn Kasten        if (NULL == pCMidiPlayer_class) {
567c2b9d79c4b59caff965076f445f5a735a360b084Glenn Kasten            result = SL_RESULT_FEATURE_UNSUPPORTED;
568c2b9d79c4b59caff965076f445f5a735a360b084Glenn Kasten        } else {
569c2b9d79c4b59caff965076f445f5a735a360b084Glenn Kasten            result = checkInterfaces(pCMidiPlayer_class, numInterfaces,
57005c7b2d09d54b9260ff7f3f5e491f38d0097c406Glenn Kasten                pInterfaceIds, pInterfaceRequired, &exposedMask, NULL);
571c2b9d79c4b59caff965076f445f5a735a360b084Glenn Kasten        }
572ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten        if (SL_RESULT_SUCCESS == result) {
573bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten            CMidiPlayer *thiz = (CMidiPlayer *) construct(pCMidiPlayer_class, exposedMask, self);
574bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten            if (NULL == thiz) {
575ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten                result = SL_RESULT_MEMORY_FAILURE;
576ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten            } else {
57794a37e8117fb72790882dfb815f99e2365754c74Glenn Kasten#if 0
57894a37e8117fb72790882dfb815f99e2365754c74Glenn Kasten                "pMIDISrc", pMIDISrc, URI | MIDIBUFFERQUEUE, NONE
57994a37e8117fb72790882dfb815f99e2365754c74Glenn Kasten                "pBankSrc", pBanksrc, NULL | URI | ADDRESS, NULL
58094a37e8117fb72790882dfb815f99e2365754c74Glenn Kasten                "pAudioOutput", pAudioOutput, OUTPUTMIX, NULL
58194a37e8117fb72790882dfb815f99e2365754c74Glenn Kasten                "pVibra", pVibra, NULL | IODEVICE, NULL
58294a37e8117fb72790882dfb815f99e2365754c74Glenn Kasten                "pLEDArray", pLEDArray, NULL | IODEVICE, NULL
58394a37e8117fb72790882dfb815f99e2365754c74Glenn Kasten#endif
5849e0f457f2809f5f062ac052d4a304e8996b49a80Glenn Kasten                // a fake value - why not use value from IPlay_init? what does CT check for?
585bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten                thiz->mPlay.mDuration = 0;
586bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten                IObject_Publish(&thiz->mObject);
587f51dba65751107c930759938775b75531ec1f330Glenn Kasten                // return the new MIDI player object
588bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten                *pPlayer = &thiz->mObject.mItf;
589ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten            }
590ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten        }
591ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten    }
5927a79f519d89eb0e1a5b3f4005484b16d6854d7e2Glenn Kasten#else
5937a79f519d89eb0e1a5b3f4005484b16d6854d7e2Glenn Kasten    result = SL_RESULT_FEATURE_UNSUPPORTED;
5947a79f519d89eb0e1a5b3f4005484b16d6854d7e2Glenn Kasten#endif
595ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten
596ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten    SL_LEAVE_INTERFACE
5970b167267bda99b68346045ccab14e810121d5de4Glenn Kasten}
5980b167267bda99b68346045ccab14e810121d5de4Glenn Kasten
599ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten
6000b167267bda99b68346045ccab14e810121d5de4Glenn Kastenstatic SLresult IEngine_CreateListener(SLEngineItf self, SLObjectItf *pListener,
601d2a7f0d6883a6d3835642e7b282f05ed1c54fe63Glenn Kasten    SLuint32 numInterfaces, const SLInterfaceID *pInterfaceIds, const SLboolean *pInterfaceRequired)
6020b167267bda99b68346045ccab14e810121d5de4Glenn Kasten{
603ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten    SL_ENTER_INTERFACE
604ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten
605a3080daa505f91df51a808c85ddb37c48745bf7cGlenn Kasten#if USE_PROFILES & USE_PROFILES_GAME
606ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten    if (NULL == pListener) {
607ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten        result = SL_RESULT_PARAMETER_INVALID;
608ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten    } else {
609ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten        *pListener = NULL;
610ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten        unsigned exposedMask;
611ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten        const ClassTable *pCListener_class = objectIDtoClass(SL_OBJECTID_LISTENER);
612c2b9d79c4b59caff965076f445f5a735a360b084Glenn Kasten        if (NULL == pCListener_class) {
613c2b9d79c4b59caff965076f445f5a735a360b084Glenn Kasten            result = SL_RESULT_FEATURE_UNSUPPORTED;
614c2b9d79c4b59caff965076f445f5a735a360b084Glenn Kasten        } else {
615c2b9d79c4b59caff965076f445f5a735a360b084Glenn Kasten            result = checkInterfaces(pCListener_class, numInterfaces,
61605c7b2d09d54b9260ff7f3f5e491f38d0097c406Glenn Kasten                pInterfaceIds, pInterfaceRequired, &exposedMask, NULL);
617c2b9d79c4b59caff965076f445f5a735a360b084Glenn Kasten        }
618ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten        if (SL_RESULT_SUCCESS == result) {
619bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten            CListener *thiz = (CListener *) construct(pCListener_class, exposedMask, self);
620bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten            if (NULL == thiz) {
621ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten                result = SL_RESULT_MEMORY_FAILURE;
622ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten            } else {
623bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten                IObject_Publish(&thiz->mObject);
624f51dba65751107c930759938775b75531ec1f330Glenn Kasten                // return the new 3D listener object
625bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten                *pListener = &thiz->mObject.mItf;
626ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten            }
627ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten        }
628ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten    }
6297a79f519d89eb0e1a5b3f4005484b16d6854d7e2Glenn Kasten#else
6307a79f519d89eb0e1a5b3f4005484b16d6854d7e2Glenn Kasten    result = SL_RESULT_FEATURE_UNSUPPORTED;
6317a79f519d89eb0e1a5b3f4005484b16d6854d7e2Glenn Kasten#endif
632ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten
633ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten    SL_LEAVE_INTERFACE
6340b167267bda99b68346045ccab14e810121d5de4Glenn Kasten}
6350b167267bda99b68346045ccab14e810121d5de4Glenn Kasten
636ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten
637d2a7f0d6883a6d3835642e7b282f05ed1c54fe63Glenn Kastenstatic SLresult IEngine_Create3DGroup(SLEngineItf self, SLObjectItf *pGroup, SLuint32 numInterfaces,
638d2a7f0d6883a6d3835642e7b282f05ed1c54fe63Glenn Kasten    const SLInterfaceID *pInterfaceIds, const SLboolean *pInterfaceRequired)
6390b167267bda99b68346045ccab14e810121d5de4Glenn Kasten{
640ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten    SL_ENTER_INTERFACE
641ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten
642a3080daa505f91df51a808c85ddb37c48745bf7cGlenn Kasten#if USE_PROFILES & USE_PROFILES_GAME
643ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten    if (NULL == pGroup) {
644ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten        result = SL_RESULT_PARAMETER_INVALID;
645ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten    } else {
646ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten        *pGroup = NULL;
647ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten        unsigned exposedMask;
648ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten        const ClassTable *pC3DGroup_class = objectIDtoClass(SL_OBJECTID_3DGROUP);
649c2b9d79c4b59caff965076f445f5a735a360b084Glenn Kasten        if (NULL == pC3DGroup_class) {
650c2b9d79c4b59caff965076f445f5a735a360b084Glenn Kasten            result = SL_RESULT_FEATURE_UNSUPPORTED;
651c2b9d79c4b59caff965076f445f5a735a360b084Glenn Kasten        } else {
652c2b9d79c4b59caff965076f445f5a735a360b084Glenn Kasten            result = checkInterfaces(pC3DGroup_class, numInterfaces,
65305c7b2d09d54b9260ff7f3f5e491f38d0097c406Glenn Kasten                pInterfaceIds, pInterfaceRequired, &exposedMask, NULL);
654c2b9d79c4b59caff965076f445f5a735a360b084Glenn Kasten        }
655ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten        if (SL_RESULT_SUCCESS == result) {
656bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten            C3DGroup *thiz = (C3DGroup *) construct(pC3DGroup_class, exposedMask, self);
657bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten            if (NULL == thiz) {
658ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten                result = SL_RESULT_MEMORY_FAILURE;
659ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten            } else {
660bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten                thiz->mMemberMask = 0;
661bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten                IObject_Publish(&thiz->mObject);
662f51dba65751107c930759938775b75531ec1f330Glenn Kasten                // return the new 3D group object
663bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten                *pGroup = &thiz->mObject.mItf;
664ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten            }
665ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten        }
666ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten    }
6677a79f519d89eb0e1a5b3f4005484b16d6854d7e2Glenn Kasten#else
6687a79f519d89eb0e1a5b3f4005484b16d6854d7e2Glenn Kasten    result = SL_RESULT_FEATURE_UNSUPPORTED;
6697a79f519d89eb0e1a5b3f4005484b16d6854d7e2Glenn Kasten#endif
670ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten
671ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten    SL_LEAVE_INTERFACE
6720b167267bda99b68346045ccab14e810121d5de4Glenn Kasten}
6730b167267bda99b68346045ccab14e810121d5de4Glenn Kasten
674ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten
67500d2d554e04ac369367c903dbf53b975355d1bcdGlenn Kastenstatic SLresult IEngine_CreateOutputMix(SLEngineItf self, SLObjectItf *pMix, SLuint32 numInterfaces,
67600d2d554e04ac369367c903dbf53b975355d1bcdGlenn Kasten    const SLInterfaceID *pInterfaceIds, const SLboolean *pInterfaceRequired)
6770b167267bda99b68346045ccab14e810121d5de4Glenn Kasten{
678ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten    SL_ENTER_INTERFACE
679ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten
680ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten    if (NULL == pMix) {
681ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten        result = SL_RESULT_PARAMETER_INVALID;
682ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten    } else {
683ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten        *pMix = NULL;
684ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten        unsigned exposedMask;
685ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten        const ClassTable *pCOutputMix_class = objectIDtoClass(SL_OBJECTID_OUTPUTMIX);
686c2b9d79c4b59caff965076f445f5a735a360b084Glenn Kasten        assert(NULL != pCOutputMix_class);
687ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten        result = checkInterfaces(pCOutputMix_class, numInterfaces,
68805c7b2d09d54b9260ff7f3f5e491f38d0097c406Glenn Kasten            pInterfaceIds, pInterfaceRequired, &exposedMask, NULL);
689ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten        if (SL_RESULT_SUCCESS == result) {
690bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten            COutputMix *thiz = (COutputMix *) construct(pCOutputMix_class, exposedMask, self);
691bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten            if (NULL == thiz) {
692ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten                result = SL_RESULT_MEMORY_FAILURE;
693ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten            } else {
69443e7743b5297cc5afcb246a7e2b5d825a607f8afJean-Michel Trivi#ifdef ANDROID
695bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten                android_outputMix_create(thiz);
69643e7743b5297cc5afcb246a7e2b5d825a607f8afJean-Michel Trivi#endif
697f51dba65751107c930759938775b75531ec1f330Glenn Kasten#ifdef USE_SDL
698bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten                IEngine *thisEngine = &thiz->mObject.mEngine->mEngine;
699f51dba65751107c930759938775b75531ec1f330Glenn Kasten                interface_lock_exclusive(thisEngine);
700f51dba65751107c930759938775b75531ec1f330Glenn Kasten                bool unpause = false;
701f51dba65751107c930759938775b75531ec1f330Glenn Kasten                if (NULL == thisEngine->mOutputMix) {
702bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten                    thisEngine->mOutputMix = thiz;
703f51dba65751107c930759938775b75531ec1f330Glenn Kasten                    unpause = true;
704f51dba65751107c930759938775b75531ec1f330Glenn Kasten                }
705f51dba65751107c930759938775b75531ec1f330Glenn Kasten                interface_unlock_exclusive(thisEngine);
706f51dba65751107c930759938775b75531ec1f330Glenn Kasten#endif
707bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten                IObject_Publish(&thiz->mObject);
708f51dba65751107c930759938775b75531ec1f330Glenn Kasten#ifdef USE_SDL
709f51dba65751107c930759938775b75531ec1f330Glenn Kasten                if (unpause) {
710f51dba65751107c930759938775b75531ec1f330Glenn Kasten                    // Enable SDL_callback to be called periodically by SDL's internal thread
711f51dba65751107c930759938775b75531ec1f330Glenn Kasten                    SDL_PauseAudio(0);
712f51dba65751107c930759938775b75531ec1f330Glenn Kasten                }
713f51dba65751107c930759938775b75531ec1f330Glenn Kasten#endif
714f51dba65751107c930759938775b75531ec1f330Glenn Kasten                // return the new output mix object
715bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten                *pMix = &thiz->mObject.mItf;
716ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten            }
717ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten        }
718ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten    }
719ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten
720ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten    SL_LEAVE_INTERFACE
7210b167267bda99b68346045ccab14e810121d5de4Glenn Kasten}
7220b167267bda99b68346045ccab14e810121d5de4Glenn Kasten
723ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten
724d2a7f0d6883a6d3835642e7b282f05ed1c54fe63Glenn Kastenstatic SLresult IEngine_CreateMetadataExtractor(SLEngineItf self, SLObjectItf *pMetadataExtractor,
725d2a7f0d6883a6d3835642e7b282f05ed1c54fe63Glenn Kasten    SLDataSource *pDataSource, SLuint32 numInterfaces, const SLInterfaceID *pInterfaceIds,
7260b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    const SLboolean *pInterfaceRequired)
7270b167267bda99b68346045ccab14e810121d5de4Glenn Kasten{
728ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten    SL_ENTER_INTERFACE
729ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten
730a3080daa505f91df51a808c85ddb37c48745bf7cGlenn Kasten#if USE_PROFILES & (USE_PROFILES_GAME | USE_PROFILES_MUSIC)
731ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten    if (NULL == pMetadataExtractor) {
732ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten        result = SL_RESULT_PARAMETER_INVALID;
733ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten    } else {
734ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten        *pMetadataExtractor = NULL;
735ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten        unsigned exposedMask;
736ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten        const ClassTable *pCMetadataExtractor_class =
737ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten            objectIDtoClass(SL_OBJECTID_METADATAEXTRACTOR);
738c2b9d79c4b59caff965076f445f5a735a360b084Glenn Kasten        if (NULL == pCMetadataExtractor_class) {
739c2b9d79c4b59caff965076f445f5a735a360b084Glenn Kasten            result = SL_RESULT_FEATURE_UNSUPPORTED;
740c2b9d79c4b59caff965076f445f5a735a360b084Glenn Kasten        } else {
741c2b9d79c4b59caff965076f445f5a735a360b084Glenn Kasten            result = checkInterfaces(pCMetadataExtractor_class, numInterfaces,
74205c7b2d09d54b9260ff7f3f5e491f38d0097c406Glenn Kasten                pInterfaceIds, pInterfaceRequired, &exposedMask, NULL);
743c2b9d79c4b59caff965076f445f5a735a360b084Glenn Kasten        }
744ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten        if (SL_RESULT_SUCCESS == result) {
745bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten            CMetadataExtractor *thiz = (CMetadataExtractor *)
746ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten                construct(pCMetadataExtractor_class, exposedMask, self);
747bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten            if (NULL == thiz) {
748ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten                result = SL_RESULT_MEMORY_FAILURE;
749ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten            } else {
75094a37e8117fb72790882dfb815f99e2365754c74Glenn Kasten#if 0
75194a37e8117fb72790882dfb815f99e2365754c74Glenn Kasten                "pDataSource", pDataSource, NONE, NONE
75294a37e8117fb72790882dfb815f99e2365754c74Glenn Kasten#endif
753bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten                IObject_Publish(&thiz->mObject);
754f51dba65751107c930759938775b75531ec1f330Glenn Kasten                // return the new metadata extractor object
755bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten                *pMetadataExtractor = &thiz->mObject.mItf;
756ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten                result = SL_RESULT_SUCCESS;
757ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten            }
758ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten        }
759ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten    }
7607a79f519d89eb0e1a5b3f4005484b16d6854d7e2Glenn Kasten#else
7617a79f519d89eb0e1a5b3f4005484b16d6854d7e2Glenn Kasten    result = SL_RESULT_FEATURE_UNSUPPORTED;
7627a79f519d89eb0e1a5b3f4005484b16d6854d7e2Glenn Kasten#endif
763ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten
764ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten    SL_LEAVE_INTERFACE
7650b167267bda99b68346045ccab14e810121d5de4Glenn Kasten}
7660b167267bda99b68346045ccab14e810121d5de4Glenn Kasten
767ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten
768d2a7f0d6883a6d3835642e7b282f05ed1c54fe63Glenn Kastenstatic SLresult IEngine_CreateExtensionObject(SLEngineItf self, SLObjectItf *pObject,
769d2a7f0d6883a6d3835642e7b282f05ed1c54fe63Glenn Kasten    void *pParameters, SLuint32 objectID, SLuint32 numInterfaces,
770d2a7f0d6883a6d3835642e7b282f05ed1c54fe63Glenn Kasten    const SLInterfaceID *pInterfaceIds, const SLboolean *pInterfaceRequired)
7710b167267bda99b68346045ccab14e810121d5de4Glenn Kasten{
772ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten    SL_ENTER_INTERFACE
773ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten
774ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten    if (NULL == pObject) {
775ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten        result = SL_RESULT_PARAMETER_INVALID;
776ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten    } else {
777ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten        *pObject = NULL;
778ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten        result = SL_RESULT_FEATURE_UNSUPPORTED;
779ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten    }
780ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten
781ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten    SL_LEAVE_INTERFACE
7820b167267bda99b68346045ccab14e810121d5de4Glenn Kasten}
7830b167267bda99b68346045ccab14e810121d5de4Glenn Kasten
784ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten
7850b167267bda99b68346045ccab14e810121d5de4Glenn Kastenstatic SLresult IEngine_QueryNumSupportedInterfaces(SLEngineItf self,
7860b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    SLuint32 objectID, SLuint32 *pNumSupportedInterfaces)
7870b167267bda99b68346045ccab14e810121d5de4Glenn Kasten{
788ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten    SL_ENTER_INTERFACE
789ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten
790ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten    if (NULL == pNumSupportedInterfaces) {
791ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten        result = SL_RESULT_PARAMETER_INVALID;
792ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten    } else {
793bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten        const ClassTable *clazz = objectIDtoClass(objectID);
794bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten        if (NULL == clazz) {
795ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten            result = SL_RESULT_FEATURE_UNSUPPORTED;
796ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten        } else {
797ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten            SLuint32 count = 0;
798ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten            SLuint32 i;
799bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten            for (i = 0; i < clazz->mInterfaceCount; ++i) {
800bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten                switch (clazz->mInterfaces[i].mInterface) {
801c3845706778edec308bd9ced559cec0a456608bdGlenn Kasten                case INTERFACE_IMPLICIT:
802104c000a6f36b871ca11e0394db1e5217f374cafGlenn Kasten                case INTERFACE_IMPLICIT_PREREALIZE:
803c3845706778edec308bd9ced559cec0a456608bdGlenn Kasten                case INTERFACE_EXPLICIT:
8045e3b06982dbf1eae237cc74326e66d51d3cdd664Glenn Kasten                case INTERFACE_EXPLICIT_PREREALIZE:
805c3845706778edec308bd9ced559cec0a456608bdGlenn Kasten                case INTERFACE_DYNAMIC:
806ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten                    ++count;
807c3845706778edec308bd9ced559cec0a456608bdGlenn Kasten                    break;
808c3845706778edec308bd9ced559cec0a456608bdGlenn Kasten                case INTERFACE_UNAVAILABLE:
809c3845706778edec308bd9ced559cec0a456608bdGlenn Kasten                    break;
810c3845706778edec308bd9ced559cec0a456608bdGlenn Kasten                default:
811c3845706778edec308bd9ced559cec0a456608bdGlenn Kasten                    assert(false);
812c3845706778edec308bd9ced559cec0a456608bdGlenn Kasten                    break;
813c3845706778edec308bd9ced559cec0a456608bdGlenn Kasten                }
814c3845706778edec308bd9ced559cec0a456608bdGlenn Kasten            }
815ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten            *pNumSupportedInterfaces = count;
816ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten            result = SL_RESULT_SUCCESS;
817ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten        }
818ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten    }
819ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten
820ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten    SL_LEAVE_INTERFACE;
8210b167267bda99b68346045ccab14e810121d5de4Glenn Kasten}
8220b167267bda99b68346045ccab14e810121d5de4Glenn Kasten
823ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten
8240b167267bda99b68346045ccab14e810121d5de4Glenn Kastenstatic SLresult IEngine_QuerySupportedInterfaces(SLEngineItf self,
8250b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    SLuint32 objectID, SLuint32 index, SLInterfaceID *pInterfaceId)
8260b167267bda99b68346045ccab14e810121d5de4Glenn Kasten{
827ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten    SL_ENTER_INTERFACE
828ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten
829ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten    if (NULL == pInterfaceId) {
830ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten        result = SL_RESULT_PARAMETER_INVALID;
831ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten    } else {
832ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten        *pInterfaceId = NULL;
833bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten        const ClassTable *clazz = objectIDtoClass(objectID);
834bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten        if (NULL == clazz) {
835ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten            result = SL_RESULT_FEATURE_UNSUPPORTED;
836ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten        } else {
837ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten            result = SL_RESULT_PARAMETER_INVALID; // will be reset later
838ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten            SLuint32 i;
839bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten            for (i = 0; i < clazz->mInterfaceCount; ++i) {
840bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten                switch (clazz->mInterfaces[i].mInterface) {
841c3845706778edec308bd9ced559cec0a456608bdGlenn Kasten                case INTERFACE_IMPLICIT:
842104c000a6f36b871ca11e0394db1e5217f374cafGlenn Kasten                case INTERFACE_IMPLICIT_PREREALIZE:
843c3845706778edec308bd9ced559cec0a456608bdGlenn Kasten                case INTERFACE_EXPLICIT:
8445e3b06982dbf1eae237cc74326e66d51d3cdd664Glenn Kasten                case INTERFACE_EXPLICIT_PREREALIZE:
845c3845706778edec308bd9ced559cec0a456608bdGlenn Kasten                case INTERFACE_DYNAMIC:
846c3845706778edec308bd9ced559cec0a456608bdGlenn Kasten                    break;
847c3845706778edec308bd9ced559cec0a456608bdGlenn Kasten                case INTERFACE_UNAVAILABLE:
848ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten                    continue;
849c3845706778edec308bd9ced559cec0a456608bdGlenn Kasten                default:
850c3845706778edec308bd9ced559cec0a456608bdGlenn Kasten                    assert(false);
851c3845706778edec308bd9ced559cec0a456608bdGlenn Kasten                    break;
852c3845706778edec308bd9ced559cec0a456608bdGlenn Kasten                }
853ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten                if (index == 0) {
854bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten                    *pInterfaceId = &SL_IID_array[clazz->mInterfaces[i].mMPH];
855ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten                    result = SL_RESULT_SUCCESS;
856ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten                    break;
857ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten                }
858ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten                --index;
859ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten            }
860a438eb1cf1ae602afab00336528dd230bd929206Glenn Kasten        }
861a438eb1cf1ae602afab00336528dd230bd929206Glenn Kasten    }
862ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten
863ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten    SL_LEAVE_INTERFACE
8640b167267bda99b68346045ccab14e810121d5de4Glenn Kasten};
8650b167267bda99b68346045ccab14e810121d5de4Glenn Kasten
866ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten
867b52bc98b576a9b56e82eca435849bd55e54b6bc1Glenn Kastenstatic const char * const extensionNames[] = {
868b52bc98b576a9b56e82eca435849bd55e54b6bc1Glenn Kasten#ifdef ANDROID
8694076e5009bfe70bc06a78f5aafec77af6c03201dGlenn Kasten#define _(n) #n
8704076e5009bfe70bc06a78f5aafec77af6c03201dGlenn Kasten#define __(n) _(n)
8714076e5009bfe70bc06a78f5aafec77af6c03201dGlenn Kasten    "ANDROID_SDK_LEVEL_" __(PLATFORM_SDK_VERSION),
8724076e5009bfe70bc06a78f5aafec77af6c03201dGlenn Kasten#undef _
8734076e5009bfe70bc06a78f5aafec77af6c03201dGlenn Kasten#undef __
874b52bc98b576a9b56e82eca435849bd55e54b6bc1Glenn Kasten#else
875b52bc98b576a9b56e82eca435849bd55e54b6bc1Glenn Kasten    "WILHELM_DESKTOP",
876b52bc98b576a9b56e82eca435849bd55e54b6bc1Glenn Kasten#endif
877b52bc98b576a9b56e82eca435849bd55e54b6bc1Glenn Kasten};
878b52bc98b576a9b56e82eca435849bd55e54b6bc1Glenn Kasten
879b52bc98b576a9b56e82eca435849bd55e54b6bc1Glenn Kasten
880ccdf07b17f23b4c040dd3f62478d0965eba804e3Glenn Kastenstatic SLresult IEngine_QueryNumSupportedExtensions(SLEngineItf self, SLuint32 *pNumExtensions)
8810b167267bda99b68346045ccab14e810121d5de4Glenn Kasten{
882ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten    SL_ENTER_INTERFACE
883ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten
884ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten    if (NULL == pNumExtensions) {
885ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten        result = SL_RESULT_PARAMETER_INVALID;
886ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten    } else {
887b52bc98b576a9b56e82eca435849bd55e54b6bc1Glenn Kasten        *pNumExtensions = sizeof(extensionNames) / sizeof(extensionNames[0]);
888ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten        result = SL_RESULT_SUCCESS;
889ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten    }
890ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten
891ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten    SL_LEAVE_INTERFACE
8920b167267bda99b68346045ccab14e810121d5de4Glenn Kasten}
8930b167267bda99b68346045ccab14e810121d5de4Glenn Kasten
894ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten
8950b167267bda99b68346045ccab14e810121d5de4Glenn Kastenstatic SLresult IEngine_QuerySupportedExtension(SLEngineItf self,
8960b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    SLuint32 index, SLchar *pExtensionName, SLint16 *pNameLength)
8970b167267bda99b68346045ccab14e810121d5de4Glenn Kasten{
898ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten    SL_ENTER_INTERFACE
899ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten
900b52bc98b576a9b56e82eca435849bd55e54b6bc1Glenn Kasten    if (NULL == pNameLength) {
901b52bc98b576a9b56e82eca435849bd55e54b6bc1Glenn Kasten        result = SL_RESULT_PARAMETER_INVALID;
902b52bc98b576a9b56e82eca435849bd55e54b6bc1Glenn Kasten    } else {
903b52bc98b576a9b56e82eca435849bd55e54b6bc1Glenn Kasten        size_t actualNameLength;
904b52bc98b576a9b56e82eca435849bd55e54b6bc1Glenn Kasten        unsigned numExtensions = sizeof(extensionNames) / sizeof(extensionNames[0]);
905b52bc98b576a9b56e82eca435849bd55e54b6bc1Glenn Kasten        if (index >= numExtensions) {
906b52bc98b576a9b56e82eca435849bd55e54b6bc1Glenn Kasten            actualNameLength = 0;
907b52bc98b576a9b56e82eca435849bd55e54b6bc1Glenn Kasten            result = SL_RESULT_PARAMETER_INVALID;
908b52bc98b576a9b56e82eca435849bd55e54b6bc1Glenn Kasten        } else {
909b52bc98b576a9b56e82eca435849bd55e54b6bc1Glenn Kasten            const char *extensionName = extensionNames[index];
910b52bc98b576a9b56e82eca435849bd55e54b6bc1Glenn Kasten            actualNameLength = strlen(extensionName) + 1;
911b52bc98b576a9b56e82eca435849bd55e54b6bc1Glenn Kasten            if (NULL == pExtensionName) {
912b52bc98b576a9b56e82eca435849bd55e54b6bc1Glenn Kasten                // application is querying the name length in order to allocate a buffer
913b52bc98b576a9b56e82eca435849bd55e54b6bc1Glenn Kasten                result = SL_RESULT_SUCCESS;
914b52bc98b576a9b56e82eca435849bd55e54b6bc1Glenn Kasten            } else {
915b52bc98b576a9b56e82eca435849bd55e54b6bc1Glenn Kasten                SLint16 availableNameLength = *pNameLength;
916b52bc98b576a9b56e82eca435849bd55e54b6bc1Glenn Kasten                if (0 >= availableNameLength) {
917b52bc98b576a9b56e82eca435849bd55e54b6bc1Glenn Kasten                    // there is not even room for the terminating NUL
918b52bc98b576a9b56e82eca435849bd55e54b6bc1Glenn Kasten                    result = SL_RESULT_BUFFER_INSUFFICIENT;
919b52bc98b576a9b56e82eca435849bd55e54b6bc1Glenn Kasten                } else if (actualNameLength > (size_t) availableNameLength) {
920b52bc98b576a9b56e82eca435849bd55e54b6bc1Glenn Kasten                    // "no invalid strings are written. That is, the null-terminator always exists"
921b52bc98b576a9b56e82eca435849bd55e54b6bc1Glenn Kasten                    memcpy(pExtensionName, extensionName, (size_t) availableNameLength - 1);
922b52bc98b576a9b56e82eca435849bd55e54b6bc1Glenn Kasten                    pExtensionName[(size_t) availableNameLength - 1] = '\0';
923b52bc98b576a9b56e82eca435849bd55e54b6bc1Glenn Kasten                    result = SL_RESULT_BUFFER_INSUFFICIENT;
924b52bc98b576a9b56e82eca435849bd55e54b6bc1Glenn Kasten                } else {
925b52bc98b576a9b56e82eca435849bd55e54b6bc1Glenn Kasten                    memcpy(pExtensionName, extensionName, actualNameLength);
926b52bc98b576a9b56e82eca435849bd55e54b6bc1Glenn Kasten                    result = SL_RESULT_SUCCESS;
927b52bc98b576a9b56e82eca435849bd55e54b6bc1Glenn Kasten                }
928b52bc98b576a9b56e82eca435849bd55e54b6bc1Glenn Kasten            }
929b52bc98b576a9b56e82eca435849bd55e54b6bc1Glenn Kasten        }
930b52bc98b576a9b56e82eca435849bd55e54b6bc1Glenn Kasten        *pNameLength = actualNameLength;
931b52bc98b576a9b56e82eca435849bd55e54b6bc1Glenn Kasten    }
932ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten
933ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten    SL_LEAVE_INTERFACE
9340b167267bda99b68346045ccab14e810121d5de4Glenn Kasten}
9350b167267bda99b68346045ccab14e810121d5de4Glenn Kasten
936ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten
9370b167267bda99b68346045ccab14e810121d5de4Glenn Kastenstatic SLresult IEngine_IsExtensionSupported(SLEngineItf self,
9380b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    const SLchar *pExtensionName, SLboolean *pSupported)
9390b167267bda99b68346045ccab14e810121d5de4Glenn Kasten{
940ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten    SL_ENTER_INTERFACE
941ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten
942b52bc98b576a9b56e82eca435849bd55e54b6bc1Glenn Kasten    if (NULL == pSupported) {
943ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten        result = SL_RESULT_PARAMETER_INVALID;
944ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten    } else {
945b52bc98b576a9b56e82eca435849bd55e54b6bc1Glenn Kasten        SLboolean isSupported = SL_BOOLEAN_FALSE;
946b52bc98b576a9b56e82eca435849bd55e54b6bc1Glenn Kasten        if (NULL == pExtensionName) {
947b52bc98b576a9b56e82eca435849bd55e54b6bc1Glenn Kasten            result = SL_RESULT_PARAMETER_INVALID;
948b52bc98b576a9b56e82eca435849bd55e54b6bc1Glenn Kasten        } else {
949b52bc98b576a9b56e82eca435849bd55e54b6bc1Glenn Kasten            unsigned numExtensions = sizeof(extensionNames) / sizeof(extensionNames[0]);
950b52bc98b576a9b56e82eca435849bd55e54b6bc1Glenn Kasten            unsigned i;
951b52bc98b576a9b56e82eca435849bd55e54b6bc1Glenn Kasten            for (i = 0; i < numExtensions; ++i) {
952b52bc98b576a9b56e82eca435849bd55e54b6bc1Glenn Kasten                if (!strcmp((const char *) pExtensionName, extensionNames[i])) {
953b52bc98b576a9b56e82eca435849bd55e54b6bc1Glenn Kasten                    isSupported = SL_BOOLEAN_TRUE;
954b52bc98b576a9b56e82eca435849bd55e54b6bc1Glenn Kasten                    break;
955b52bc98b576a9b56e82eca435849bd55e54b6bc1Glenn Kasten                }
956b52bc98b576a9b56e82eca435849bd55e54b6bc1Glenn Kasten            }
957b52bc98b576a9b56e82eca435849bd55e54b6bc1Glenn Kasten            result = SL_RESULT_SUCCESS;
958b52bc98b576a9b56e82eca435849bd55e54b6bc1Glenn Kasten        }
959b52bc98b576a9b56e82eca435849bd55e54b6bc1Glenn Kasten        *pSupported = isSupported;
960ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten    }
961ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten
962ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten    SL_LEAVE_INTERFACE
9630b167267bda99b68346045ccab14e810121d5de4Glenn Kasten}
9640b167267bda99b68346045ccab14e810121d5de4Glenn Kasten
965ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten
9660b167267bda99b68346045ccab14e810121d5de4Glenn Kastenstatic const struct SLEngineItf_ IEngine_Itf = {
9670b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    IEngine_CreateLEDDevice,
9680b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    IEngine_CreateVibraDevice,
9690b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    IEngine_CreateAudioPlayer,
9700b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    IEngine_CreateAudioRecorder,
9710b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    IEngine_CreateMidiPlayer,
9720b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    IEngine_CreateListener,
9730b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    IEngine_Create3DGroup,
9740b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    IEngine_CreateOutputMix,
9750b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    IEngine_CreateMetadataExtractor,
9760b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    IEngine_CreateExtensionObject,
9770b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    IEngine_QueryNumSupportedInterfaces,
9780b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    IEngine_QuerySupportedInterfaces,
9790b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    IEngine_QueryNumSupportedExtensions,
9800b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    IEngine_QuerySupportedExtension,
9810b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    IEngine_IsExtensionSupported
9820b167267bda99b68346045ccab14e810121d5de4Glenn Kasten};
9830b167267bda99b68346045ccab14e810121d5de4Glenn Kasten
9840b167267bda99b68346045ccab14e810121d5de4Glenn Kastenvoid IEngine_init(void *self)
9850b167267bda99b68346045ccab14e810121d5de4Glenn Kasten{
986bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten    IEngine *thiz = (IEngine *) self;
987bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten    thiz->mItf = &IEngine_Itf;
988a9a70a4451545034c9263dd55b181f2912534c37Glenn Kasten    // mLossOfControlGlobal is initialized in slCreateEngine
98900d2d554e04ac369367c903dbf53b975355d1bcdGlenn Kasten#ifdef USE_SDL
990bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten    thiz->mOutputMix = NULL;
99100d2d554e04ac369367c903dbf53b975355d1bcdGlenn Kasten#endif
992bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten    thiz->mInstanceCount = 1; // ourself
993bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten    thiz->mInstanceMask = 0;
994bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten    thiz->mChangedMask = 0;
9950b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    unsigned i;
996928ea4ffff40c82987cfb1ac9e3095fdc6968709Glenn Kasten    for (i = 0; i < MAX_INSTANCE; ++i) {
997bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten        thiz->mInstances[i] = NULL;
998928ea4ffff40c82987cfb1ac9e3095fdc6968709Glenn Kasten    }
999bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten    thiz->mShutdown = SL_BOOLEAN_FALSE;
1000bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten    thiz->mShutdownAck = SL_BOOLEAN_FALSE;
1001a80a6ff9a1f80792478c9d43578afa24a07eb2f0Glenn Kasten#if _BYTE_ORDER == _BIG_ENDIAN
1002a80a6ff9a1f80792478c9d43578afa24a07eb2f0Glenn Kasten    thiz->mNativeEndianness = SL_BYTEORDER_BIGENDIAN;
1003a80a6ff9a1f80792478c9d43578afa24a07eb2f0Glenn Kasten#else
1004a80a6ff9a1f80792478c9d43578afa24a07eb2f0Glenn Kasten    thiz->mNativeEndianness = SL_BYTEORDER_LITTLEENDIAN;
1005a80a6ff9a1f80792478c9d43578afa24a07eb2f0Glenn Kasten#endif
10060b167267bda99b68346045ccab14e810121d5de4Glenn Kasten}
1007a9a70a4451545034c9263dd55b181f2912534c37Glenn Kasten
1008a9a70a4451545034c9263dd55b181f2912534c37Glenn Kastenvoid IEngine_deinit(void *self)
1009a9a70a4451545034c9263dd55b181f2912534c37Glenn Kasten{
1010a9a70a4451545034c9263dd55b181f2912534c37Glenn Kasten}
10118db0b106cdce5f5216b1535492de7930ad738aedGlenn Kasten
10128db0b106cdce5f5216b1535492de7930ad738aedGlenn Kasten
10138db0b106cdce5f5216b1535492de7930ad738aedGlenn Kasten// OpenMAX AL Engine
10148db0b106cdce5f5216b1535492de7930ad738aedGlenn Kasten
10158db0b106cdce5f5216b1535492de7930ad738aedGlenn Kasten
10168db0b106cdce5f5216b1535492de7930ad738aedGlenn Kastenstatic XAresult IEngine_CreateCameraDevice(XAEngineItf self, XAObjectItf *pDevice,
10178db0b106cdce5f5216b1535492de7930ad738aedGlenn Kasten        XAuint32 deviceID, XAuint32 numInterfaces, const XAInterfaceID *pInterfaceIds,
10188db0b106cdce5f5216b1535492de7930ad738aedGlenn Kasten        const XAboolean *pInterfaceRequired)
10198db0b106cdce5f5216b1535492de7930ad738aedGlenn Kasten{
10208db0b106cdce5f5216b1535492de7930ad738aedGlenn Kasten    XA_ENTER_INTERFACE
10218db0b106cdce5f5216b1535492de7930ad738aedGlenn Kasten
1022bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten    //IXAEngine *thiz = (IXAEngine *) self;
10238db0b106cdce5f5216b1535492de7930ad738aedGlenn Kasten    result = SL_RESULT_FEATURE_UNSUPPORTED;
10248db0b106cdce5f5216b1535492de7930ad738aedGlenn Kasten
10258db0b106cdce5f5216b1535492de7930ad738aedGlenn Kasten    XA_LEAVE_INTERFACE
10268db0b106cdce5f5216b1535492de7930ad738aedGlenn Kasten}
10278db0b106cdce5f5216b1535492de7930ad738aedGlenn Kasten
10288db0b106cdce5f5216b1535492de7930ad738aedGlenn Kasten
10298db0b106cdce5f5216b1535492de7930ad738aedGlenn Kastenstatic XAresult IEngine_CreateRadioDevice(XAEngineItf self, XAObjectItf *pDevice,
10308db0b106cdce5f5216b1535492de7930ad738aedGlenn Kasten        XAuint32 numInterfaces, const XAInterfaceID *pInterfaceIds,
10318db0b106cdce5f5216b1535492de7930ad738aedGlenn Kasten        const XAboolean *pInterfaceRequired)
10328db0b106cdce5f5216b1535492de7930ad738aedGlenn Kasten{
10338db0b106cdce5f5216b1535492de7930ad738aedGlenn Kasten    XA_ENTER_INTERFACE
10348db0b106cdce5f5216b1535492de7930ad738aedGlenn Kasten
1035bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten    //IXAEngine *thiz = (IXAEngine *) self;
10368db0b106cdce5f5216b1535492de7930ad738aedGlenn Kasten    result = SL_RESULT_FEATURE_UNSUPPORTED;
10378db0b106cdce5f5216b1535492de7930ad738aedGlenn Kasten
10388db0b106cdce5f5216b1535492de7930ad738aedGlenn Kasten    XA_LEAVE_INTERFACE
10398db0b106cdce5f5216b1535492de7930ad738aedGlenn Kasten}
10408db0b106cdce5f5216b1535492de7930ad738aedGlenn Kasten
10418db0b106cdce5f5216b1535492de7930ad738aedGlenn Kasten
10428db0b106cdce5f5216b1535492de7930ad738aedGlenn Kastenstatic XAresult IXAEngine_CreateLEDDevice(XAEngineItf self, XAObjectItf *pDevice, XAuint32 deviceID,
10438db0b106cdce5f5216b1535492de7930ad738aedGlenn Kasten        XAuint32 numInterfaces, const XAInterfaceID *pInterfaceIds,
10448db0b106cdce5f5216b1535492de7930ad738aedGlenn Kasten        const XAboolean *pInterfaceRequired)
10458db0b106cdce5f5216b1535492de7930ad738aedGlenn Kasten{
10468db0b106cdce5f5216b1535492de7930ad738aedGlenn Kasten    // forward to OpenSL ES
10478db0b106cdce5f5216b1535492de7930ad738aedGlenn Kasten    return IEngine_CreateLEDDevice(&((CEngine *) ((IXAEngine *) self)->mThis)->mEngine.mItf,
10488db0b106cdce5f5216b1535492de7930ad738aedGlenn Kasten            (SLObjectItf *) pDevice, deviceID, numInterfaces, (const SLInterfaceID *) pInterfaceIds,
10498db0b106cdce5f5216b1535492de7930ad738aedGlenn Kasten            (const SLboolean *) pInterfaceRequired);
10508db0b106cdce5f5216b1535492de7930ad738aedGlenn Kasten}
10518db0b106cdce5f5216b1535492de7930ad738aedGlenn Kasten
10528db0b106cdce5f5216b1535492de7930ad738aedGlenn Kasten
10538db0b106cdce5f5216b1535492de7930ad738aedGlenn Kastenstatic XAresult IXAEngine_CreateVibraDevice(XAEngineItf self, XAObjectItf *pDevice,
10548db0b106cdce5f5216b1535492de7930ad738aedGlenn Kasten        XAuint32 deviceID, XAuint32 numInterfaces, const XAInterfaceID *pInterfaceIds,
10558db0b106cdce5f5216b1535492de7930ad738aedGlenn Kasten        const XAboolean *pInterfaceRequired)
10568db0b106cdce5f5216b1535492de7930ad738aedGlenn Kasten{
10578db0b106cdce5f5216b1535492de7930ad738aedGlenn Kasten    // forward to OpenSL ES
10588db0b106cdce5f5216b1535492de7930ad738aedGlenn Kasten    return IEngine_CreateVibraDevice(&((CEngine *) ((IXAEngine *) self)->mThis)->mEngine.mItf,
10598db0b106cdce5f5216b1535492de7930ad738aedGlenn Kasten            (SLObjectItf *) pDevice, deviceID, numInterfaces, (const SLInterfaceID *) pInterfaceIds,
10608db0b106cdce5f5216b1535492de7930ad738aedGlenn Kasten            (const SLboolean *) pInterfaceRequired);
10618db0b106cdce5f5216b1535492de7930ad738aedGlenn Kasten}
10628db0b106cdce5f5216b1535492de7930ad738aedGlenn Kasten
10638db0b106cdce5f5216b1535492de7930ad738aedGlenn Kasten
10648db0b106cdce5f5216b1535492de7930ad738aedGlenn Kastenstatic XAresult IEngine_CreateMediaPlayer(XAEngineItf self, XAObjectItf *pPlayer,
10658db0b106cdce5f5216b1535492de7930ad738aedGlenn Kasten        XADataSource *pDataSrc, XADataSource *pBankSrc, XADataSink *pAudioSnk,
10668db0b106cdce5f5216b1535492de7930ad738aedGlenn Kasten        XADataSink *pImageVideoSnk, XADataSink *pVibra, XADataSink *pLEDArray,
10678db0b106cdce5f5216b1535492de7930ad738aedGlenn Kasten        XAuint32 numInterfaces, const XAInterfaceID *pInterfaceIds,
10688db0b106cdce5f5216b1535492de7930ad738aedGlenn Kasten        const XAboolean *pInterfaceRequired)
10698db0b106cdce5f5216b1535492de7930ad738aedGlenn Kasten{
10708db0b106cdce5f5216b1535492de7930ad738aedGlenn Kasten    XA_ENTER_INTERFACE
10718db0b106cdce5f5216b1535492de7930ad738aedGlenn Kasten
10728b8875067dd02b79361abb00c5d65b02a8ae72b0Glenn Kasten    if (NULL == pPlayer) {
10738b8875067dd02b79361abb00c5d65b02a8ae72b0Glenn Kasten        result = XA_RESULT_PARAMETER_INVALID;
10748b8875067dd02b79361abb00c5d65b02a8ae72b0Glenn Kasten    } else {
10758b8875067dd02b79361abb00c5d65b02a8ae72b0Glenn Kasten        *pPlayer = NULL;
10768b8875067dd02b79361abb00c5d65b02a8ae72b0Glenn Kasten        unsigned exposedMask;
10778b8875067dd02b79361abb00c5d65b02a8ae72b0Glenn Kasten        const ClassTable *pCMediaPlayer_class = objectIDtoClass(XA_OBJECTID_MEDIAPLAYER);
10788b8875067dd02b79361abb00c5d65b02a8ae72b0Glenn Kasten        assert(NULL != pCMediaPlayer_class);
10798b8875067dd02b79361abb00c5d65b02a8ae72b0Glenn Kasten        result = checkInterfaces(pCMediaPlayer_class, numInterfaces,
108005c7b2d09d54b9260ff7f3f5e491f38d0097c406Glenn Kasten                (const SLInterfaceID *) pInterfaceIds, pInterfaceRequired, &exposedMask, NULL);
10818b8875067dd02b79361abb00c5d65b02a8ae72b0Glenn Kasten        if (XA_RESULT_SUCCESS == result) {
10828b8875067dd02b79361abb00c5d65b02a8ae72b0Glenn Kasten
10838b8875067dd02b79361abb00c5d65b02a8ae72b0Glenn Kasten            // Construct our new MediaPlayer instance
1084bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten            CMediaPlayer *thiz = (CMediaPlayer *) construct(pCMediaPlayer_class, exposedMask,
10858b8875067dd02b79361abb00c5d65b02a8ae72b0Glenn Kasten                    &((CEngine *) ((IXAEngine *) self)->mThis)->mEngine.mItf);
1086bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten            if (NULL == thiz) {
10878b8875067dd02b79361abb00c5d65b02a8ae72b0Glenn Kasten                result = XA_RESULT_MEMORY_FAILURE;
10888b8875067dd02b79361abb00c5d65b02a8ae72b0Glenn Kasten            } else {
10898b8875067dd02b79361abb00c5d65b02a8ae72b0Glenn Kasten
10908b8875067dd02b79361abb00c5d65b02a8ae72b0Glenn Kasten                do {
10918b8875067dd02b79361abb00c5d65b02a8ae72b0Glenn Kasten
10928b8875067dd02b79361abb00c5d65b02a8ae72b0Glenn Kasten                    // Initialize private fields not associated with an interface
10938b8875067dd02b79361abb00c5d65b02a8ae72b0Glenn Kasten
1094d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi                    // Default data source in case of failure in checkDataSource
1095d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi                    thiz->mDataSource.mLocator.mLocatorType = SL_DATALOCATOR_NULL;
1096d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi                    thiz->mDataSource.mFormat.mFormatType = XA_DATAFORMAT_NULL;
1097d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi
1098d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi                    // Default andio and image sink in case of failure in checkDataSink
1099d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi                    thiz->mAudioSink.mLocator.mLocatorType = XA_DATALOCATOR_NULL;
1100d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi                    thiz->mAudioSink.mFormat.mFormatType = XA_DATAFORMAT_NULL;
1101d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi                    thiz->mImageVideoSink.mLocator.mLocatorType = XA_DATALOCATOR_NULL;
1102d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi                    thiz->mImageVideoSink.mFormat.mFormatType = XA_DATAFORMAT_NULL;
1103d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi
1104d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi                    // More default values, in case destructor needs to be called early
110599b927751677abfb60a388d65dfeed1fed1db12cGlenn Kasten                    thiz->mNumChannels = UNKNOWN_NUMCHANNELS;
1106d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi
110713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi#ifdef ANDROID
1108485a038f9f0f898227b8ab4218e94c5d56b6ed0bGlenn Kasten                    // placement new (explicit constructor)
110913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                    // FIXME unnecessary once those fields are encapsulated in one class, rather
111013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                    //   than a structure
111168d56b8ebaf60184a3aef988e3d2b09ed8b88c05Jean-Michel Trivi                    (void) new (&thiz->mAVPlayer) android::sp<android::GenericPlayer>();
1112485a038f9f0f898227b8ab4218e94c5d56b6ed0bGlenn Kasten                    (void) new (&thiz->mCallbackProtector)
1113485a038f9f0f898227b8ab4218e94c5d56b6ed0bGlenn Kasten                            android::sp<android::CallbackProtector>();
111472042d4448cee63528c619537321ba73944c6382Glenn Kasten                    // Android-specific POD fields are initialized in android_Player_create,
111572042d4448cee63528c619537321ba73944c6382Glenn Kasten                    // and assume calloc or memset 0 during allocation
111613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi#endif
11178b8875067dd02b79361abb00c5d65b02a8ae72b0Glenn Kasten
11188b8875067dd02b79361abb00c5d65b02a8ae72b0Glenn Kasten                    // Check the source and sink parameters against generic constraints
11198b8875067dd02b79361abb00c5d65b02a8ae72b0Glenn Kasten
112094a37e8117fb72790882dfb815f99e2365754c74Glenn Kasten                    result = checkDataSource("pDataSrc", (const SLDataSource *) pDataSrc,
1121bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten                            &thiz->mDataSource, DATALOCATOR_MASK_URI
112294a37e8117fb72790882dfb815f99e2365754c74Glenn Kasten#ifdef ANDROID
112394a37e8117fb72790882dfb815f99e2365754c74Glenn Kasten                            | DATALOCATOR_MASK_ANDROIDFD
1124eae4df541ba1d46f65d37e959baf2127aa632c93Jean-Michel Trivi                            | DATALOCATOR_MASK_ANDROIDBUFFERQUEUE
112594a37e8117fb72790882dfb815f99e2365754c74Glenn Kasten#endif
112694a37e8117fb72790882dfb815f99e2365754c74Glenn Kasten                            , DATAFORMAT_MASK_MIME);
11278b8875067dd02b79361abb00c5d65b02a8ae72b0Glenn Kasten                    if (XA_RESULT_SUCCESS != result) {
11288b8875067dd02b79361abb00c5d65b02a8ae72b0Glenn Kasten                        break;
11298b8875067dd02b79361abb00c5d65b02a8ae72b0Glenn Kasten                    }
11308b8875067dd02b79361abb00c5d65b02a8ae72b0Glenn Kasten
113194a37e8117fb72790882dfb815f99e2365754c74Glenn Kasten                    result = checkDataSource("pBankSrc", (const SLDataSource *) pBankSrc,
1132bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten                            &thiz->mBankSource, DATALOCATOR_MASK_NULL | DATALOCATOR_MASK_URI |
113394a37e8117fb72790882dfb815f99e2365754c74Glenn Kasten                            DATALOCATOR_MASK_ADDRESS, DATAFORMAT_MASK_NULL);
11348b8875067dd02b79361abb00c5d65b02a8ae72b0Glenn Kasten                    if (XA_RESULT_SUCCESS != result) {
11358b8875067dd02b79361abb00c5d65b02a8ae72b0Glenn Kasten                        break;
11368b8875067dd02b79361abb00c5d65b02a8ae72b0Glenn Kasten                    }
11378b8875067dd02b79361abb00c5d65b02a8ae72b0Glenn Kasten
113894a37e8117fb72790882dfb815f99e2365754c74Glenn Kasten                    result = checkDataSink("pAudioSnk", (const SLDataSink *) pAudioSnk,
1139bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten                            &thiz->mAudioSink, DATALOCATOR_MASK_OUTPUTMIX, DATAFORMAT_MASK_NULL);
11408b8875067dd02b79361abb00c5d65b02a8ae72b0Glenn Kasten                    if (XA_RESULT_SUCCESS != result) {
11418b8875067dd02b79361abb00c5d65b02a8ae72b0Glenn Kasten                        break;
11428b8875067dd02b79361abb00c5d65b02a8ae72b0Glenn Kasten                    }
11438b8875067dd02b79361abb00c5d65b02a8ae72b0Glenn Kasten
114494a37e8117fb72790882dfb815f99e2365754c74Glenn Kasten                    result = checkDataSink("pImageVideoSnk", (const SLDataSink *) pImageVideoSnk,
11452bb3547351a40ad394b09babec4d2fcc233bfe2eGlenn Kasten                            &thiz->mImageVideoSink,
11462bb3547351a40ad394b09babec4d2fcc233bfe2eGlenn Kasten                            DATALOCATOR_MASK_NULL | DATALOCATOR_MASK_NATIVEDISPLAY,
114794a37e8117fb72790882dfb815f99e2365754c74Glenn Kasten                            DATAFORMAT_MASK_NULL);
11488b8875067dd02b79361abb00c5d65b02a8ae72b0Glenn Kasten                    if (XA_RESULT_SUCCESS != result) {
11498b8875067dd02b79361abb00c5d65b02a8ae72b0Glenn Kasten                        break;
11508b8875067dd02b79361abb00c5d65b02a8ae72b0Glenn Kasten                    }
11518b8875067dd02b79361abb00c5d65b02a8ae72b0Glenn Kasten
1152bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten                    result = checkDataSink("pVibra", (const SLDataSink *) pVibra, &thiz->mVibraSink,
115394a37e8117fb72790882dfb815f99e2365754c74Glenn Kasten                            DATALOCATOR_MASK_NULL | DATALOCATOR_MASK_IODEVICE,
115494a37e8117fb72790882dfb815f99e2365754c74Glenn Kasten                            DATAFORMAT_MASK_NULL);
11558b8875067dd02b79361abb00c5d65b02a8ae72b0Glenn Kasten                    if (XA_RESULT_SUCCESS != result) {
11568b8875067dd02b79361abb00c5d65b02a8ae72b0Glenn Kasten                        break;
11578b8875067dd02b79361abb00c5d65b02a8ae72b0Glenn Kasten                    }
11588b8875067dd02b79361abb00c5d65b02a8ae72b0Glenn Kasten
115994a37e8117fb72790882dfb815f99e2365754c74Glenn Kasten                    result = checkDataSink("pLEDArray", (const SLDataSink *) pLEDArray,
1160bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten                            &thiz->mLEDArraySink, DATALOCATOR_MASK_NULL | DATALOCATOR_MASK_IODEVICE,
116194a37e8117fb72790882dfb815f99e2365754c74Glenn Kasten                            DATAFORMAT_MASK_NULL);
11628b8875067dd02b79361abb00c5d65b02a8ae72b0Glenn Kasten                    if (XA_RESULT_SUCCESS != result) {
11638b8875067dd02b79361abb00c5d65b02a8ae72b0Glenn Kasten                        break;
11648b8875067dd02b79361abb00c5d65b02a8ae72b0Glenn Kasten                    }
11658b8875067dd02b79361abb00c5d65b02a8ae72b0Glenn Kasten
11668b8875067dd02b79361abb00c5d65b02a8ae72b0Glenn Kasten                    // Unsafe to ever refer to application pointers again
11678b8875067dd02b79361abb00c5d65b02a8ae72b0Glenn Kasten                    pDataSrc = NULL;
11688b8875067dd02b79361abb00c5d65b02a8ae72b0Glenn Kasten                    pBankSrc = NULL;
11698b8875067dd02b79361abb00c5d65b02a8ae72b0Glenn Kasten                    pAudioSnk = NULL;
11708b8875067dd02b79361abb00c5d65b02a8ae72b0Glenn Kasten                    pImageVideoSnk = NULL;
11718b8875067dd02b79361abb00c5d65b02a8ae72b0Glenn Kasten                    pVibra = NULL;
11728b8875067dd02b79361abb00c5d65b02a8ae72b0Glenn Kasten                    pLEDArray = NULL;
11738b8875067dd02b79361abb00c5d65b02a8ae72b0Glenn Kasten
11748b8875067dd02b79361abb00c5d65b02a8ae72b0Glenn Kasten                    // Check that the requested interfaces are compatible with the data source
1175d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi                    // FIXME implement
11768b8875067dd02b79361abb00c5d65b02a8ae72b0Glenn Kasten
11778b8875067dd02b79361abb00c5d65b02a8ae72b0Glenn Kasten                    // check the source and sink parameters against platform support
11788b8875067dd02b79361abb00c5d65b02a8ae72b0Glenn Kasten#ifdef ANDROID
1179d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi                    result = android_Player_checkSourceSink(thiz);
11808b8875067dd02b79361abb00c5d65b02a8ae72b0Glenn Kasten                    if (XA_RESULT_SUCCESS != result) {
11818b8875067dd02b79361abb00c5d65b02a8ae72b0Glenn Kasten                        break;
11828b8875067dd02b79361abb00c5d65b02a8ae72b0Glenn Kasten                    }
11838b8875067dd02b79361abb00c5d65b02a8ae72b0Glenn Kasten#endif
11848b8875067dd02b79361abb00c5d65b02a8ae72b0Glenn Kasten
1185f6c0c2346b2ca1ac01c2d68a2cd2a7f1d9ff3427Glenn Kasten#ifdef ANDROID
1186d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi                    // AndroidBufferQueue-specific initialization
1187d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi                    if (XA_DATALOCATOR_ANDROIDBUFFERQUEUE ==
1188d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi                            thiz->mDataSource.mLocator.mLocatorType) {
1189d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi                        XAuint16 nbBuffers = (XAuint16) thiz->mDataSource.mLocator.mABQ.numBuffers;
1190d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi
1191d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi                        // Avoid possible integer overflow during multiplication; this arbitrary
1192d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi                        // maximum is big enough to not interfere with real applications, but
1193d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi                        // small enough to not overflow.
1194d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi                        if (nbBuffers >= 256) {
1195d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi                            result = SL_RESULT_MEMORY_FAILURE;
1196d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi                            break;
1197d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi                        }
119870c49ae2867094072a4365423417ea452bf82231Jean-Michel Trivi
119970c49ae2867094072a4365423417ea452bf82231Jean-Michel Trivi                        // initialize ABQ buffer type
120070c49ae2867094072a4365423417ea452bf82231Jean-Michel Trivi                        // assert below has been checked in android_audioPlayer_checkSourceSink
120170c49ae2867094072a4365423417ea452bf82231Jean-Michel Trivi                        assert(XA_DATAFORMAT_MIME == thiz->mDataSource.mFormat.mFormatType);
120270c49ae2867094072a4365423417ea452bf82231Jean-Michel Trivi                        if (XA_CONTAINERTYPE_MPEG_TS ==
120370c49ae2867094072a4365423417ea452bf82231Jean-Michel Trivi                                thiz->mDataSource.mFormat.mMIME.containerType) {
120470c49ae2867094072a4365423417ea452bf82231Jean-Michel Trivi                            thiz->mAndroidBufferQueue.mBufferType = kAndroidBufferTypeMpeg2Ts;
120537dc2fccf3f122b79ebd554de209d0a3c94ae161Jean-Michel Trivi
120637dc2fccf3f122b79ebd554de209d0a3c94ae161Jean-Michel Trivi                            // Set the container type for the StreamInformation interface
120737dc2fccf3f122b79ebd554de209d0a3c94ae161Jean-Michel Trivi                            XAMediaContainerInformation *containerInfo =
120837dc2fccf3f122b79ebd554de209d0a3c94ae161Jean-Michel Trivi                                    (XAMediaContainerInformation*)
120937dc2fccf3f122b79ebd554de209d0a3c94ae161Jean-Michel Trivi                                        // always storing container info at index 0, as per spec
121064c3fe7bd86951eeac27adca2219ce16eabff58cGlenn Kasten                                        &thiz->mStreamInfo.mStreamInfoTable.itemAt(0).containerInfo;
121137dc2fccf3f122b79ebd554de209d0a3c94ae161Jean-Michel Trivi                            containerInfo->containerType = XA_CONTAINERTYPE_MPEG_TS;
121237dc2fccf3f122b79ebd554de209d0a3c94ae161Jean-Michel Trivi                            // there are no streams at this stage
121337dc2fccf3f122b79ebd554de209d0a3c94ae161Jean-Michel Trivi                            containerInfo->numStreams = 0;
121437dc2fccf3f122b79ebd554de209d0a3c94ae161Jean-Michel Trivi
121570c49ae2867094072a4365423417ea452bf82231Jean-Michel Trivi                        } else {
121670c49ae2867094072a4365423417ea452bf82231Jean-Michel Trivi                            thiz->mAndroidBufferQueue.mBufferType = kAndroidBufferTypeInvalid;
121770c49ae2867094072a4365423417ea452bf82231Jean-Michel Trivi                            SL_LOGE("Invalid buffer type in Android Buffer Queue");
121870c49ae2867094072a4365423417ea452bf82231Jean-Michel Trivi                            result = SL_RESULT_CONTENT_UNSUPPORTED;
121970c49ae2867094072a4365423417ea452bf82231Jean-Michel Trivi                        }
122070c49ae2867094072a4365423417ea452bf82231Jean-Michel Trivi
122170c49ae2867094072a4365423417ea452bf82231Jean-Michel Trivi                        // initialize ABQ memory
1222d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi                        thiz->mAndroidBufferQueue.mBufferArray = (AdvancedBufferHeader *)
1223d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi                                    malloc( (nbBuffers + 1) * sizeof(AdvancedBufferHeader));
1224d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi                        if (NULL == thiz->mAndroidBufferQueue.mBufferArray) {
1225d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi                            result = SL_RESULT_MEMORY_FAILURE;
1226d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi                            break;
1227d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi                        } else {
1228d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi                            thiz->mAndroidBufferQueue.mFront =
1229d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi                                    thiz->mAndroidBufferQueue.mBufferArray;
1230d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi                            thiz->mAndroidBufferQueue.mRear =
1231d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi                                    thiz->mAndroidBufferQueue.mBufferArray;
1232d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi                        }
1233d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi
1234d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi                        thiz->mAndroidBufferQueue.mNumBuffers = nbBuffers;
1235d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi
1236d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi                    }
1237f6c0c2346b2ca1ac01c2d68a2cd2a7f1d9ff3427Glenn Kasten#endif
1238d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi
1239d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi                    // used to store the data source of our audio player
1240d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi                    thiz->mDynamicSource.mDataSource = &thiz->mDataSource.u.mSource;
1241d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi
12428b8875067dd02b79361abb00c5d65b02a8ae72b0Glenn Kasten                    // platform-specific initialization
12438b8875067dd02b79361abb00c5d65b02a8ae72b0Glenn Kasten#ifdef ANDROID
1244bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten                    android_Player_create(thiz);
12458b8875067dd02b79361abb00c5d65b02a8ae72b0Glenn Kasten#endif
12468b8875067dd02b79361abb00c5d65b02a8ae72b0Glenn Kasten
12478b8875067dd02b79361abb00c5d65b02a8ae72b0Glenn Kasten                } while (0);
12488b8875067dd02b79361abb00c5d65b02a8ae72b0Glenn Kasten
12498b8875067dd02b79361abb00c5d65b02a8ae72b0Glenn Kasten                if (XA_RESULT_SUCCESS != result) {
1250bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten                    IObject_Destroy(&thiz->mObject.mItf);
12518b8875067dd02b79361abb00c5d65b02a8ae72b0Glenn Kasten                } else {
1252