10b167267bda99b68346045ccab14e810121d5de4Glenn Kasten/*
20b167267bda99b68346045ccab14e810121d5de4Glenn Kasten * Copyright (C) 2010 The Android Open Source Project
30b167267bda99b68346045ccab14e810121d5de4Glenn Kasten *
40b167267bda99b68346045ccab14e810121d5de4Glenn Kasten * Licensed under the Apache License, Version 2.0 (the "License");
50b167267bda99b68346045ccab14e810121d5de4Glenn Kasten * you may not use this file except in compliance with the License.
60b167267bda99b68346045ccab14e810121d5de4Glenn Kasten * You may obtain a copy of the License at
70b167267bda99b68346045ccab14e810121d5de4Glenn Kasten *
80b167267bda99b68346045ccab14e810121d5de4Glenn Kasten *      http://www.apache.org/licenses/LICENSE-2.0
90b167267bda99b68346045ccab14e810121d5de4Glenn Kasten *
100b167267bda99b68346045ccab14e810121d5de4Glenn Kasten * Unless required by applicable law or agreed to in writing, software
110b167267bda99b68346045ccab14e810121d5de4Glenn Kasten * distributed under the License is distributed on an "AS IS" BASIS,
120b167267bda99b68346045ccab14e810121d5de4Glenn Kasten * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
130b167267bda99b68346045ccab14e810121d5de4Glenn Kasten * See the License for the specific language governing permissions and
140b167267bda99b68346045ccab14e810121d5de4Glenn Kasten * limitations under the License.
150b167267bda99b68346045ccab14e810121d5de4Glenn Kasten */
160b167267bda99b68346045ccab14e810121d5de4Glenn Kasten
170b167267bda99b68346045ccab14e810121d5de4Glenn Kasten/* Engine implementation */
180b167267bda99b68346045ccab14e810121d5de4Glenn Kasten
19979a3f8743646af9999a89dff9e13b972b7efd87Glenn Kasten#include "sles_allinclusive.h"
20979a3f8743646af9999a89dff9e13b972b7efd87Glenn Kasten
21bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi
22bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi/* Utility functions */
23bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi
24bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivistatic SLresult initializeBufferQueueMembers(CAudioPlayer *ap) {
25bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    // inline allocation of circular mArray, up to a typical max
26bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    if (BUFFER_HEADER_TYPICAL >= ap->mBufferQueue.mNumBuffers) {
27bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi        ap->mBufferQueue.mArray = ap->mBufferQueue.mTypical;
28bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    } else {
29bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi        // Avoid possible integer overflow during multiplication; this arbitrary
30bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi        // maximum is big enough to not interfere with real applications, but
31bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi        // small enough to not overflow.
32bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi        if (ap->mBufferQueue.mNumBuffers >= 256) {
33bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi            return SL_RESULT_MEMORY_FAILURE;
34bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi        }
35bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi        ap->mBufferQueue.mArray = (BufferHeader *)
36bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi                malloc((ap->mBufferQueue.mNumBuffers + 1) * sizeof(BufferHeader));
37bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi        if (NULL == ap->mBufferQueue.mArray) {
38bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi            return SL_RESULT_MEMORY_FAILURE;
39bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi        }
40bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    }
41bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    ap->mBufferQueue.mFront = ap->mBufferQueue.mArray;
42bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    ap->mBufferQueue.mRear = ap->mBufferQueue.mArray;
43bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    return SL_RESULT_SUCCESS;
44bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi}
45bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi
46bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi#ifdef ANDROID
47bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivistatic SLresult initializeAndroidBufferQueueMembers(CAudioPlayer *ap) {
48bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    // Avoid possible integer overflow during multiplication; this arbitrary
49bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    // maximum is big enough to not interfere with real applications, but
50bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    // small enough to not overflow.
51bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    if (ap->mAndroidBufferQueue.mNumBuffers >= 256) {
52bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi        return SL_RESULT_MEMORY_FAILURE;
53bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    }
54bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    ap->mAndroidBufferQueue.mBufferArray = (AdvancedBufferHeader *)
55bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi            malloc( (ap->mAndroidBufferQueue.mNumBuffers + 1) * sizeof(AdvancedBufferHeader));
56bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    if (NULL == ap->mAndroidBufferQueue.mBufferArray) {
57bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi        return SL_RESULT_MEMORY_FAILURE;
58bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    } else {
59bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi
60bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi        // initialize ABQ buffer type
61bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi        // assert below has been checked in android_audioPlayer_checkSourceSink
62bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi        assert(SL_DATAFORMAT_MIME == ap->mDataSource.mFormat.mFormatType);
63bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi        switch(ap->mDataSource.mFormat.mMIME.containerType) {
64bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi          case SL_CONTAINERTYPE_MPEG_TS:
65bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi            ap->mAndroidBufferQueue.mBufferType = kAndroidBufferTypeMpeg2Ts;
66bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi            break;
67bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi          case SL_CONTAINERTYPE_AAC:
68bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi          case SL_CONTAINERTYPE_RAW: {
69bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi            const char* mime = (char*)ap->mDataSource.mFormat.mMIME.mimeType;
70c3b82a293ed06001ba6d50f111608160c6065ef2Glenn Kasten            if ((mime != NULL) && !(strcasecmp(mime, (const char *)SL_ANDROID_MIME_AACADTS) &&
71bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi                    strcasecmp(mime, ANDROID_MIME_AACADTS_ANDROID_FRAMEWORK))) {
72bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi                ap->mAndroidBufferQueue.mBufferType = kAndroidBufferTypeAacadts;
73bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi            } else {
74bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi                ap->mAndroidBufferQueue.mBufferType = kAndroidBufferTypeInvalid;
75bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi                SL_LOGE("CreateAudioPlayer: Invalid buffer type in Android Buffer Queue");
76bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi                return SL_RESULT_CONTENT_UNSUPPORTED;
77bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi            }
78bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi          } break;
79bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi          default:
80bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi            ap->mAndroidBufferQueue.mBufferType = kAndroidBufferTypeInvalid;
81bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi            SL_LOGE("CreateAudioPlayer: Invalid buffer type in Android Buffer Queue");
82bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi            return SL_RESULT_CONTENT_UNSUPPORTED;
83bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi        }
84bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi
85bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi        ap->mAndroidBufferQueue.mFront = ap->mAndroidBufferQueue.mBufferArray;
86bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi        ap->mAndroidBufferQueue.mRear  = ap->mAndroidBufferQueue.mBufferArray;
87bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    }
88bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi
89bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi    return SL_RESULT_SUCCESS;
90bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi}
91bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi#endif
92bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi
93bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi
94daf661248ff6ea2e21799e5114c78b7c1d49630eGlenn Kastenstatic SLresult IEngine_CreateLEDDevice(SLEngineItf self, SLObjectItf *pDevice, SLuint32 deviceID,
95daf661248ff6ea2e21799e5114c78b7c1d49630eGlenn Kasten    SLuint32 numInterfaces, const SLInterfaceID *pInterfaceIds, const SLboolean *pInterfaceRequired)
960b167267bda99b68346045ccab14e810121d5de4Glenn Kasten{
97ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten    SL_ENTER_INTERFACE
98ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten
99a3080daa505f91df51a808c85ddb37c48745bf7cGlenn Kasten#if USE_PROFILES & USE_PROFILES_OPTIONAL
100928ea4ffff40c82987cfb1ac9e3095fdc6968709Glenn Kasten    if ((NULL == pDevice) || (SL_DEFAULTDEVICEID_LED != deviceID)) {
101ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten        result = SL_RESULT_PARAMETER_INVALID;
102ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten    } else {
103ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten        *pDevice = NULL;
104ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten        unsigned exposedMask;
105ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten        const ClassTable *pCLEDDevice_class = objectIDtoClass(SL_OBJECTID_LEDDEVICE);
106c2b9d79c4b59caff965076f445f5a735a360b084Glenn Kasten        if (NULL == pCLEDDevice_class) {
107c2b9d79c4b59caff965076f445f5a735a360b084Glenn Kasten            result = SL_RESULT_FEATURE_UNSUPPORTED;
108c2b9d79c4b59caff965076f445f5a735a360b084Glenn Kasten        } else {
109c2b9d79c4b59caff965076f445f5a735a360b084Glenn Kasten            result = checkInterfaces(pCLEDDevice_class, numInterfaces, pInterfaceIds,
11005c7b2d09d54b9260ff7f3f5e491f38d0097c406Glenn Kasten                pInterfaceRequired, &exposedMask, NULL);
111c2b9d79c4b59caff965076f445f5a735a360b084Glenn Kasten        }
112ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten        if (SL_RESULT_SUCCESS == result) {
113bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten            CLEDDevice *thiz = (CLEDDevice *) construct(pCLEDDevice_class, exposedMask, self);
114bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten            if (NULL == thiz) {
115ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten                result = SL_RESULT_MEMORY_FAILURE;
116ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten            } else {
117bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten                thiz->mDeviceID = deviceID;
118bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten                IObject_Publish(&thiz->mObject);
119f51dba65751107c930759938775b75531ec1f330Glenn Kasten                // return the new LED object
120bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten                *pDevice = &thiz->mObject.mItf;
121ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten            }
122ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten        }
123ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten    }
1247a79f519d89eb0e1a5b3f4005484b16d6854d7e2Glenn Kasten#else
1257a79f519d89eb0e1a5b3f4005484b16d6854d7e2Glenn Kasten    result = SL_RESULT_FEATURE_UNSUPPORTED;
1267a79f519d89eb0e1a5b3f4005484b16d6854d7e2Glenn Kasten#endif
127ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten
128ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten    SL_LEAVE_INTERFACE
1290b167267bda99b68346045ccab14e810121d5de4Glenn Kasten}
1300b167267bda99b68346045ccab14e810121d5de4Glenn Kasten
131ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten
132daf661248ff6ea2e21799e5114c78b7c1d49630eGlenn Kastenstatic SLresult IEngine_CreateVibraDevice(SLEngineItf self, SLObjectItf *pDevice, SLuint32 deviceID,
133daf661248ff6ea2e21799e5114c78b7c1d49630eGlenn Kasten    SLuint32 numInterfaces, const SLInterfaceID *pInterfaceIds, const SLboolean *pInterfaceRequired)
1340b167267bda99b68346045ccab14e810121d5de4Glenn Kasten{
135ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten    SL_ENTER_INTERFACE
136ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten
137a3080daa505f91df51a808c85ddb37c48745bf7cGlenn Kasten#if USE_PROFILES & USE_PROFILES_OPTIONAL
138928ea4ffff40c82987cfb1ac9e3095fdc6968709Glenn Kasten    if ((NULL == pDevice) || (SL_DEFAULTDEVICEID_VIBRA != deviceID)) {
139ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten        result = SL_RESULT_PARAMETER_INVALID;
140ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten    } else {
141ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten        *pDevice = NULL;
142ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten        unsigned exposedMask;
143ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten        const ClassTable *pCVibraDevice_class = objectIDtoClass(SL_OBJECTID_VIBRADEVICE);
144c2b9d79c4b59caff965076f445f5a735a360b084Glenn Kasten        if (NULL == pCVibraDevice_class) {
145c2b9d79c4b59caff965076f445f5a735a360b084Glenn Kasten            result = SL_RESULT_FEATURE_UNSUPPORTED;
146c2b9d79c4b59caff965076f445f5a735a360b084Glenn Kasten        } else {
147c2b9d79c4b59caff965076f445f5a735a360b084Glenn Kasten            result = checkInterfaces(pCVibraDevice_class, numInterfaces,
14805c7b2d09d54b9260ff7f3f5e491f38d0097c406Glenn Kasten                pInterfaceIds, pInterfaceRequired, &exposedMask, NULL);
149c2b9d79c4b59caff965076f445f5a735a360b084Glenn Kasten        }
150ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten        if (SL_RESULT_SUCCESS == result) {
151bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten            CVibraDevice *thiz = (CVibraDevice *) construct(pCVibraDevice_class, exposedMask, self);
152bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten            if (NULL == thiz) {
153ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten                result = SL_RESULT_MEMORY_FAILURE;
154ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten            } else {
155bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten                thiz->mDeviceID = deviceID;
156bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten                IObject_Publish(&thiz->mObject);
157f51dba65751107c930759938775b75531ec1f330Glenn Kasten                // return the new vibra object
158bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten                *pDevice = &thiz->mObject.mItf;
159ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten            }
160ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten        }
161ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten    }
1627a79f519d89eb0e1a5b3f4005484b16d6854d7e2Glenn Kasten#else
1637a79f519d89eb0e1a5b3f4005484b16d6854d7e2Glenn Kasten    result = SL_RESULT_FEATURE_UNSUPPORTED;
1647a79f519d89eb0e1a5b3f4005484b16d6854d7e2Glenn Kasten#endif
165ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten
166ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten    SL_LEAVE_INTERFACE
1670b167267bda99b68346045ccab14e810121d5de4Glenn Kasten}
1680b167267bda99b68346045ccab14e810121d5de4Glenn Kasten
169ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten
1700b167267bda99b68346045ccab14e810121d5de4Glenn Kastenstatic SLresult IEngine_CreateAudioPlayer(SLEngineItf self, SLObjectItf *pPlayer,
1710b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    SLDataSource *pAudioSrc, SLDataSink *pAudioSnk, SLuint32 numInterfaces,
1720b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    const SLInterfaceID *pInterfaceIds, const SLboolean *pInterfaceRequired)
1730b167267bda99b68346045ccab14e810121d5de4Glenn Kasten{
174ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten    SL_ENTER_INTERFACE
175ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten
176ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten    if (NULL == pPlayer) {
177ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten       result = SL_RESULT_PARAMETER_INVALID;
178ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten    } else {
179ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten        *pPlayer = NULL;
18005c7b2d09d54b9260ff7f3f5e491f38d0097c406Glenn Kasten        unsigned exposedMask, requiredMask;
181ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten        const ClassTable *pCAudioPlayer_class = objectIDtoClass(SL_OBJECTID_AUDIOPLAYER);
182c2b9d79c4b59caff965076f445f5a735a360b084Glenn Kasten        assert(NULL != pCAudioPlayer_class);
183ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten        result = checkInterfaces(pCAudioPlayer_class, numInterfaces,
18405c7b2d09d54b9260ff7f3f5e491f38d0097c406Glenn Kasten            pInterfaceIds, pInterfaceRequired, &exposedMask, &requiredMask);
185ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten        if (SL_RESULT_SUCCESS == result) {
186ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten
187ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten            // Construct our new AudioPlayer instance
188bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten            CAudioPlayer *thiz = (CAudioPlayer *) construct(pCAudioPlayer_class, exposedMask, self);
189bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten            if (NULL == thiz) {
190ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten                result = SL_RESULT_MEMORY_FAILURE;
191ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten            } else {
192b91e32605ecf39e34ad39936b1ee193bb4e30225Glenn Kasten
193ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten                do {
194daf661248ff6ea2e21799e5114c78b7c1d49630eGlenn Kasten
195ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten                    // Initialize private fields not associated with an interface
196a9a70a4451545034c9263dd55b181f2912534c37Glenn Kasten
197a9a70a4451545034c9263dd55b181f2912534c37Glenn Kasten                    // Default data source in case of failure in checkDataSource
198bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten                    thiz->mDataSource.mLocator.mLocatorType = SL_DATALOCATOR_NULL;
199bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten                    thiz->mDataSource.mFormat.mFormatType = SL_DATAFORMAT_NULL;
200a9a70a4451545034c9263dd55b181f2912534c37Glenn Kasten
201a9a70a4451545034c9263dd55b181f2912534c37Glenn Kasten                    // Default data sink in case of failure in checkDataSink
202bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten                    thiz->mDataSink.mLocator.mLocatorType = SL_DATALOCATOR_NULL;
203bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten                    thiz->mDataSink.mFormat.mFormatType = SL_DATAFORMAT_NULL;
204a9a70a4451545034c9263dd55b181f2912534c37Glenn Kasten
205a9a70a4451545034c9263dd55b181f2912534c37Glenn Kasten                    // Default is no per-channel mute or solo
206bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten                    thiz->mMuteMask = 0;
207bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten                    thiz->mSoloMask = 0;
208a9a70a4451545034c9263dd55b181f2912534c37Glenn Kasten
209f6f5ceb363286d5ebef2c2e70c8a5aa135d5d1eeGlenn Kasten                    // Will be set soon for PCM buffer queues, or later by platform-specific code
210f6f5ceb363286d5ebef2c2e70c8a5aa135d5d1eeGlenn Kasten                    // during Realize or Prefetch
2111a9c2615d0933d183fcb1b9e34ec8f0da2a85153Glenn Kasten                    thiz->mNumChannels = UNKNOWN_NUMCHANNELS;
2121a9c2615d0933d183fcb1b9e34ec8f0da2a85153Glenn Kasten                    thiz->mSampleRateMilliHz = UNKNOWN_SAMPLERATE;
213daf661248ff6ea2e21799e5114c78b7c1d49630eGlenn Kasten
214a9a70a4451545034c9263dd55b181f2912534c37Glenn Kasten                    // More default values, in case destructor needs to be called early
21572042d4448cee63528c619537321ba73944c6382Glenn Kasten                    thiz->mDirectLevel = 0; // no attenuation
216a9a70a4451545034c9263dd55b181f2912534c37Glenn Kasten#ifdef USE_OUTPUTMIXEXT
217bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten                    thiz->mTrack = NULL;
218bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten                    thiz->mGains[0] = 1.0f;
219bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten                    thiz->mGains[1] = 1.0f;
220bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten                    thiz->mDestroyRequested = SL_BOOLEAN_FALSE;
221a9a70a4451545034c9263dd55b181f2912534c37Glenn Kasten#endif
222a9a70a4451545034c9263dd55b181f2912534c37Glenn Kasten#ifdef USE_SNDFILE
223bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten                    thiz->mSndFile.mPathname = NULL;
224bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten                    thiz->mSndFile.mSNDFILE = NULL;
225bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten                    memset(&thiz->mSndFile.mSfInfo, 0, sizeof(SF_INFO));
226bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten                    memset(&thiz->mSndFile.mMutex, 0, sizeof(pthread_mutex_t));
227bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten                    thiz->mSndFile.mEOF = SL_BOOLEAN_FALSE;
228bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten                    thiz->mSndFile.mWhich = 0;
229bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten                    memset(thiz->mSndFile.mBuffer, 0, sizeof(thiz->mSndFile.mBuffer));
230a9a70a4451545034c9263dd55b181f2912534c37Glenn Kasten#endif
231a9a70a4451545034c9263dd55b181f2912534c37Glenn Kasten#ifdef ANDROID
232a9a70a4451545034c9263dd55b181f2912534c37Glenn Kasten                    // placement new (explicit constructor)
23313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                    // FIXME unnecessary once those fields are encapsulated in one class, rather
23413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                    //   than a structure
235ae1a5c8dc1fccf7c121eda830f8d23bd094c0f16Glenn Kasten                    (void) new (&thiz->mAudioTrack) android::sp<android::AudioTrack>();
2366cce136651f6fd2c7aecd45bc553270152d75462Jean-Michel Trivi                    (void) new (&thiz->mCallbackProtector)
2376cce136651f6fd2c7aecd45bc553270152d75462Jean-Michel Trivi                            android::sp<android::CallbackProtector>();
238bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten                    (void) new (&thiz->mAuxEffect) android::sp<android::AudioEffect>();
23913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                    (void) new (&thiz->mAPlayer) android::sp<android::GenericPlayer>();
24072042d4448cee63528c619537321ba73944c6382Glenn Kasten                    // Android-specific POD fields are initialized in android_audioPlayer_create,
24172042d4448cee63528c619537321ba73944c6382Glenn Kasten                    // and assume calloc or memset 0 during allocation
242a9a70a4451545034c9263dd55b181f2912534c37Glenn Kasten#endif
243a9a70a4451545034c9263dd55b181f2912534c37Glenn Kasten
244ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten                    // Check the source and sink parameters against generic constraints,
245ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten                    // and make a local copy of all parameters in case other application threads
246ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten                    // change memory concurrently.
247daf661248ff6ea2e21799e5114c78b7c1d49630eGlenn Kasten
248bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten                    result = checkDataSource("pAudioSrc", pAudioSrc, &thiz->mDataSource,
24994a37e8117fb72790882dfb815f99e2365754c74Glenn Kasten                            DATALOCATOR_MASK_URI | DATALOCATOR_MASK_ADDRESS |
25094a37e8117fb72790882dfb815f99e2365754c74Glenn Kasten                            DATALOCATOR_MASK_BUFFERQUEUE
25194a37e8117fb72790882dfb815f99e2365754c74Glenn Kasten#ifdef ANDROID
25294a37e8117fb72790882dfb815f99e2365754c74Glenn Kasten                            | DATALOCATOR_MASK_ANDROIDFD | DATALOCATOR_MASK_ANDROIDSIMPLEBUFFERQUEUE
253c869defb7c0dd6f4e45ad26abb0f8ccc960152deJean-Michel Trivi                            | DATALOCATOR_MASK_ANDROIDBUFFERQUEUE
25494a37e8117fb72790882dfb815f99e2365754c74Glenn Kasten#endif
25594a37e8117fb72790882dfb815f99e2365754c74Glenn Kasten                            , DATAFORMAT_MASK_MIME | DATAFORMAT_MASK_PCM);
256c869defb7c0dd6f4e45ad26abb0f8ccc960152deJean-Michel Trivi
2573c170255cc71942f310b676d968cf73328aa5d70Jean-Michel Trivi                    if (SL_RESULT_SUCCESS != result) {
2583c170255cc71942f310b676d968cf73328aa5d70Jean-Michel Trivi                        break;
2593c170255cc71942f310b676d968cf73328aa5d70Jean-Michel Trivi                    }
2603c170255cc71942f310b676d968cf73328aa5d70Jean-Michel Trivi
261bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten                    result = checkDataSink("pAudioSnk", pAudioSnk, &thiz->mDataSink,
26213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                            DATALOCATOR_MASK_OUTPUTMIX                  // for playback
26313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi#ifdef ANDROID
26413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                            | DATALOCATOR_MASK_ANDROIDSIMPLEBUFFERQUEUE // for decode to a BQ
26513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                            | DATALOCATOR_MASK_BUFFERQUEUE              // for decode to a BQ
26613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi#endif
26713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                            , DATAFORMAT_MASK_NULL
26813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi#ifdef ANDROID
26913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                            | DATAFORMAT_MASK_PCM                       // for decode to PCM
27013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi#endif
27113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                            );
2723c170255cc71942f310b676d968cf73328aa5d70Jean-Michel Trivi                    if (SL_RESULT_SUCCESS != result) {
273ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten                        break;
2743c170255cc71942f310b676d968cf73328aa5d70Jean-Michel Trivi                    }
275daf661248ff6ea2e21799e5114c78b7c1d49630eGlenn Kasten
276f6f5ceb363286d5ebef2c2e70c8a5aa135d5d1eeGlenn Kasten                    // It would be unsafe to ever refer to the application pointers again
277f6f5ceb363286d5ebef2c2e70c8a5aa135d5d1eeGlenn Kasten                    pAudioSrc = NULL;
278f6f5ceb363286d5ebef2c2e70c8a5aa135d5d1eeGlenn Kasten                    pAudioSnk = NULL;
279f6f5ceb363286d5ebef2c2e70c8a5aa135d5d1eeGlenn Kasten
2807965455f86c21d6e1f788b284f5fc829e82ff2b5Glenn Kasten                    // Check that the requested interfaces are compatible with data source and sink
2817965455f86c21d6e1f788b284f5fc829e82ff2b5Glenn Kasten                    result = checkSourceSinkVsInterfacesCompatibility(&thiz->mDataSource,
28205c7b2d09d54b9260ff7f3f5e491f38d0097c406Glenn Kasten                            &thiz->mDataSink, pCAudioPlayer_class, requiredMask);
2833c170255cc71942f310b676d968cf73328aa5d70Jean-Michel Trivi                    if (SL_RESULT_SUCCESS != result) {
284ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten                        break;
2853c170255cc71942f310b676d968cf73328aa5d70Jean-Michel Trivi                    }
286daf661248ff6ea2e21799e5114c78b7c1d49630eGlenn Kasten
28713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                    // copy the buffer queue count from source locator (for playback) / from the
28813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                    // sink locator (for decode on ANDROID build) to the buffer queue interface
28918abcc4b70fab1f84d6fbebac3a8e34480a6c4d3Glenn Kasten                    // we have already range-checked the value down to a smaller width
29013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                    SLuint16 nbBuffers = 0;
291d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi                    bool usesAdvancedBufferHeaders = false;
292bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi                    bool usesSimpleBufferQueue = false;
293bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi                    // creating an AudioPlayer which decodes AAC ADTS buffers to a PCM buffer queue
294bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi                    //  will cause usesAdvancedBufferHeaders and usesSimpleBufferQueue to be true
295bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten                    switch (thiz->mDataSource.mLocator.mLocatorType) {
29601e9f5fa4698856f92bcfd88188ee4c8397b22dbGlenn Kasten                    case SL_DATALOCATOR_BUFFERQUEUE:
29701e9f5fa4698856f92bcfd88188ee4c8397b22dbGlenn Kasten#ifdef ANDROID
29801e9f5fa4698856f92bcfd88188ee4c8397b22dbGlenn Kasten                    case SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE:
29901e9f5fa4698856f92bcfd88188ee4c8397b22dbGlenn Kasten#endif
300bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi                        usesSimpleBufferQueue = true;
30113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                        nbBuffers = (SLuint16) thiz->mDataSource.mLocator.mBufferQueue.numBuffers;
302bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten                        assert(SL_DATAFORMAT_PCM == thiz->mDataSource.mFormat.mFormatType);
303bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten                        thiz->mNumChannels = thiz->mDataSource.mFormat.mPCM.numChannels;
304bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten                        thiz->mSampleRateMilliHz = thiz->mDataSource.mFormat.mPCM.samplesPerSec;
30501e9f5fa4698856f92bcfd88188ee4c8397b22dbGlenn Kasten                        break;
306d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi#ifdef ANDROID
307d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi                    case SL_DATALOCATOR_ANDROIDBUFFERQUEUE:
308d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi                        usesAdvancedBufferHeaders = true;
309bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi                        nbBuffers = (SLuint16) thiz->mDataSource.mLocator.mABQ.numBuffers;
310d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi                        thiz->mAndroidBufferQueue.mNumBuffers = nbBuffers;
311d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi                        break;
312d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi#endif
31301e9f5fa4698856f92bcfd88188ee4c8397b22dbGlenn Kasten                    default:
31413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                        nbBuffers = 0;
31501e9f5fa4698856f92bcfd88188ee4c8397b22dbGlenn Kasten                        break;
31601e9f5fa4698856f92bcfd88188ee4c8397b22dbGlenn Kasten                    }
31713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi#ifdef ANDROID
31813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                    switch(thiz->mDataSink.mLocator.mLocatorType) {
31913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                    case SL_DATALOCATOR_BUFFERQUEUE:
32013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                    case SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE:
321bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi                        usesSimpleBufferQueue = true;
32213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                        nbBuffers = thiz->mDataSink.mLocator.mBufferQueue.numBuffers;
32313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                        assert(SL_DATAFORMAT_PCM == thiz->mDataSink.mFormat.mFormatType);
324e878c470cf58c8654d613ab2449468b44a90d6e5Glenn Kasten                        // FIXME The values specified by the app are meaningless. We get the
325e878c470cf58c8654d613ab2449468b44a90d6e5Glenn Kasten                        // real values from the decoder.  But the data sink checks currently require
326e878c470cf58c8654d613ab2449468b44a90d6e5Glenn Kasten                        // that the app specify these useless values.  Needs doc/fix.
327e878c470cf58c8654d613ab2449468b44a90d6e5Glenn Kasten                        // Instead use the "unknown" values, as needed by prepare completion.
328e878c470cf58c8654d613ab2449468b44a90d6e5Glenn Kasten                        // thiz->mNumChannels = thiz->mDataSink.mFormat.mPCM.numChannels;
329e878c470cf58c8654d613ab2449468b44a90d6e5Glenn Kasten                        // thiz->mSampleRateMilliHz = thiz->mDataSink.mFormat.mPCM.samplesPerSec;
330e878c470cf58c8654d613ab2449468b44a90d6e5Glenn Kasten                        thiz->mNumChannels = UNKNOWN_NUMCHANNELS;
331e878c470cf58c8654d613ab2449468b44a90d6e5Glenn Kasten                        thiz->mSampleRateMilliHz = UNKNOWN_SAMPLERATE;
33213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                        break;
33313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                    default:
33413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                        // leave nbBuffers unchanged
33513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                        break;
33613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                    }
33713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi#endif
33813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                    thiz->mBufferQueue.mNumBuffers = nbBuffers;
3394b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten
340ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten                    // check the audio source and sink parameters against platform support
341ef8931ae547cd703e69df9ad350d69825da0f546Jean-Michel Trivi#ifdef ANDROID
342bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten                    result = android_audioPlayer_checkSourceSink(thiz);
343928ea4ffff40c82987cfb1ac9e3095fdc6968709Glenn Kasten                    if (SL_RESULT_SUCCESS != result) {
344ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten                        break;
345928ea4ffff40c82987cfb1ac9e3095fdc6968709Glenn Kasten                    }
3460b167267bda99b68346045ccab14e810121d5de4Glenn Kasten#endif
347acd88797a1d3b8225bab888d29036e245f275be5Glenn Kasten
3480b167267bda99b68346045ccab14e810121d5de4Glenn Kasten#ifdef USE_SNDFILE
349bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten                    result = SndFile_checkAudioPlayerSourceSink(thiz);
350928ea4ffff40c82987cfb1ac9e3095fdc6968709Glenn Kasten                    if (SL_RESULT_SUCCESS != result) {
351ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten                        break;
352928ea4ffff40c82987cfb1ac9e3095fdc6968709Glenn Kasten                    }
3530b167267bda99b68346045ccab14e810121d5de4Glenn Kasten#endif
3540b167267bda99b68346045ccab14e810121d5de4Glenn Kasten
3550b167267bda99b68346045ccab14e810121d5de4Glenn Kasten#ifdef USE_OUTPUTMIXEXT
356bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten                    result = IOutputMixExt_checkAudioPlayerSourceSink(thiz);
357928ea4ffff40c82987cfb1ac9e3095fdc6968709Glenn Kasten                    if (SL_RESULT_SUCCESS != result) {
358ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten                        break;
359928ea4ffff40c82987cfb1ac9e3095fdc6968709Glenn Kasten                    }
360daf661248ff6ea2e21799e5114c78b7c1d49630eGlenn Kasten#endif
3610b167267bda99b68346045ccab14e810121d5de4Glenn Kasten
362ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten                    // Allocate memory for buffer queue
363d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi                    if (usesAdvancedBufferHeaders) {
364f6c0c2346b2ca1ac01c2d68a2cd2a7f1d9ff3427Glenn Kasten#ifdef ANDROID
365d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi                        // locator is SL_DATALOCATOR_ANDROIDBUFFERQUEUE
366bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi                        result = initializeAndroidBufferQueueMembers(thiz);
367f6c0c2346b2ca1ac01c2d68a2cd2a7f1d9ff3427Glenn Kasten#else
368f6c0c2346b2ca1ac01c2d68a2cd2a7f1d9ff3427Glenn Kasten                        assert(false);
369f6c0c2346b2ca1ac01c2d68a2cd2a7f1d9ff3427Glenn Kasten#endif
370bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi                    }
371bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi
372bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi                    if (usesSimpleBufferQueue) {
373d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi                        // locator is SL_DATALOCATOR_BUFFERQUEUE
374d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi                        //         or SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE
375bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi                        result = initializeBufferQueueMembers(thiz);
376d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi                    }
377ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten
378d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi                    // used to store the data source of our audio player
379d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi                    thiz->mDynamicSource.mDataSource = &thiz->mDataSource.u.mSource;
380ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten
381d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi                    // platform-specific initialization
382ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten#ifdef ANDROID
383d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi                    android_audioPlayer_create(thiz);
384ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten#endif
3850b167267bda99b68346045ccab14e810121d5de4Glenn Kasten
386ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten                } while (0);
387daf661248ff6ea2e21799e5114c78b7c1d49630eGlenn Kasten
388e5d006b298ce7683d66f7ec86136403cf5fb20d6Glenn Kasten                if (SL_RESULT_SUCCESS != result) {
389bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten                    IObject_Destroy(&thiz->mObject.mItf);
390f51dba65751107c930759938775b75531ec1f330Glenn Kasten                } else {
391bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten                    IObject_Publish(&thiz->mObject);
392f51dba65751107c930759938775b75531ec1f330Glenn Kasten                    // return the new audio player object
393bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten                    *pPlayer = &thiz->mObject.mItf;
394e5d006b298ce7683d66f7ec86136403cf5fb20d6Glenn Kasten                }
395daf661248ff6ea2e21799e5114c78b7c1d49630eGlenn Kasten
396ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten            }
397ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten        }
398acd88797a1d3b8225bab888d29036e245f275be5Glenn Kasten
399ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten    }
400ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten
401ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten    SL_LEAVE_INTERFACE
4020b167267bda99b68346045ccab14e810121d5de4Glenn Kasten}
4030b167267bda99b68346045ccab14e810121d5de4Glenn Kasten
404ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten
405ccdf07b17f23b4c040dd3f62478d0965eba804e3Glenn Kastenstatic SLresult IEngine_CreateAudioRecorder(SLEngineItf self, SLObjectItf *pRecorder,
406ccdf07b17f23b4c040dd3f62478d0965eba804e3Glenn Kasten    SLDataSource *pAudioSrc, SLDataSink *pAudioSnk, SLuint32 numInterfaces,
407ccdf07b17f23b4c040dd3f62478d0965eba804e3Glenn Kasten    const SLInterfaceID *pInterfaceIds, const SLboolean *pInterfaceRequired)
4080b167267bda99b68346045ccab14e810121d5de4Glenn Kasten{
409ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten    SL_ENTER_INTERFACE
410ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten
411a3080daa505f91df51a808c85ddb37c48745bf7cGlenn Kasten#if (USE_PROFILES & USE_PROFILES_OPTIONAL) || defined(ANDROID)
412ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten    if (NULL == pRecorder) {
413ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten        result = SL_RESULT_PARAMETER_INVALID;
414ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten    } else {
415ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten        *pRecorder = NULL;
416ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten        unsigned exposedMask;
417ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten        const ClassTable *pCAudioRecorder_class = objectIDtoClass(SL_OBJECTID_AUDIORECORDER);
418c2b9d79c4b59caff965076f445f5a735a360b084Glenn Kasten        if (NULL == pCAudioRecorder_class) {
419c2b9d79c4b59caff965076f445f5a735a360b084Glenn Kasten            result = SL_RESULT_FEATURE_UNSUPPORTED;
420c2b9d79c4b59caff965076f445f5a735a360b084Glenn Kasten        } else {
421c2b9d79c4b59caff965076f445f5a735a360b084Glenn Kasten            result = checkInterfaces(pCAudioRecorder_class, numInterfaces,
42205c7b2d09d54b9260ff7f3f5e491f38d0097c406Glenn Kasten                    pInterfaceIds, pInterfaceRequired, &exposedMask, NULL);
423c2b9d79c4b59caff965076f445f5a735a360b084Glenn Kasten        }
4243af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
425ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten        if (SL_RESULT_SUCCESS == result) {
426ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten
427ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten            // Construct our new AudioRecorder instance
428bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten            CAudioRecorder *thiz = (CAudioRecorder *) construct(pCAudioRecorder_class, exposedMask,
4293af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                    self);
430bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten            if (NULL == thiz) {
431ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten                result = SL_RESULT_MEMORY_FAILURE;
432ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten            } else {
433ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten
434ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten                do {
435f6f5ceb363286d5ebef2c2e70c8a5aa135d5d1eeGlenn Kasten
436f6f5ceb363286d5ebef2c2e70c8a5aa135d5d1eeGlenn Kasten                    // Initialize fields not associated with any interface
437f6f5ceb363286d5ebef2c2e70c8a5aa135d5d1eeGlenn Kasten
438a9a70a4451545034c9263dd55b181f2912534c37Glenn Kasten                    // Default data source in case of failure in checkDataSource
439bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten                    thiz->mDataSource.mLocator.mLocatorType = SL_DATALOCATOR_NULL;
440bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten                    thiz->mDataSource.mFormat.mFormatType = SL_DATAFORMAT_NULL;
441a9a70a4451545034c9263dd55b181f2912534c37Glenn Kasten
442a9a70a4451545034c9263dd55b181f2912534c37Glenn Kasten                    // Default data sink in case of failure in checkDataSink
443bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten                    thiz->mDataSink.mLocator.mLocatorType = SL_DATALOCATOR_NULL;
444bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten                    thiz->mDataSink.mFormat.mFormatType = SL_DATAFORMAT_NULL;
445a9a70a4451545034c9263dd55b181f2912534c37Glenn Kasten
446f6f5ceb363286d5ebef2c2e70c8a5aa135d5d1eeGlenn Kasten                    // These fields are set to real values by
447f6f5ceb363286d5ebef2c2e70c8a5aa135d5d1eeGlenn Kasten                    // android_audioRecorder_checkSourceSinkSupport.  Note that the data sink is
448f6f5ceb363286d5ebef2c2e70c8a5aa135d5d1eeGlenn Kasten                    // always PCM buffer queue, so we know the channel count and sample rate early.
4491a9c2615d0933d183fcb1b9e34ec8f0da2a85153Glenn Kasten                    thiz->mNumChannels = UNKNOWN_NUMCHANNELS;
4501a9c2615d0933d183fcb1b9e34ec8f0da2a85153Glenn Kasten                    thiz->mSampleRateMilliHz = UNKNOWN_SAMPLERATE;
451f6f5ceb363286d5ebef2c2e70c8a5aa135d5d1eeGlenn Kasten#ifdef ANDROID
452bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten                    thiz->mAudioRecord = NULL;
453ca39f4b4dbeb920a5b97bd65be73f2f7cac77431Dima Zavin                    thiz->mRecordSource = AUDIO_SOURCE_DEFAULT;
454f6f5ceb363286d5ebef2c2e70c8a5aa135d5d1eeGlenn Kasten#endif
455712b490060e4164fbe47986be1d2584d1610d8ddJean-Michel Trivi
456ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten                    // Check the source and sink parameters, and make a local copy of all parameters
457bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten                    result = checkDataSource("pAudioSrc", pAudioSrc, &thiz->mDataSource,
45894a37e8117fb72790882dfb815f99e2365754c74Glenn Kasten                            DATALOCATOR_MASK_IODEVICE, DATAFORMAT_MASK_NULL);
459928ea4ffff40c82987cfb1ac9e3095fdc6968709Glenn Kasten                    if (SL_RESULT_SUCCESS != result) {
460ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten                        break;
461928ea4ffff40c82987cfb1ac9e3095fdc6968709Glenn Kasten                    }
462bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten                    result = checkDataSink("pAudioSnk", pAudioSnk, &thiz->mDataSink,
46394a37e8117fb72790882dfb815f99e2365754c74Glenn Kasten                            DATALOCATOR_MASK_URI
46494a37e8117fb72790882dfb815f99e2365754c74Glenn Kasten#ifdef ANDROID
46594a37e8117fb72790882dfb815f99e2365754c74Glenn Kasten                            | DATALOCATOR_MASK_ANDROIDSIMPLEBUFFERQUEUE
46694a37e8117fb72790882dfb815f99e2365754c74Glenn Kasten#endif
46794a37e8117fb72790882dfb815f99e2365754c74Glenn Kasten                            , DATAFORMAT_MASK_MIME | DATAFORMAT_MASK_PCM
46894a37e8117fb72790882dfb815f99e2365754c74Glenn Kasten                    );
469928ea4ffff40c82987cfb1ac9e3095fdc6968709Glenn Kasten                    if (SL_RESULT_SUCCESS != result) {
470ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten                        break;
471928ea4ffff40c82987cfb1ac9e3095fdc6968709Glenn Kasten                    }
472ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten
473f6f5ceb363286d5ebef2c2e70c8a5aa135d5d1eeGlenn Kasten                    // It would be unsafe to ever refer to the application pointers again
474f6f5ceb363286d5ebef2c2e70c8a5aa135d5d1eeGlenn Kasten                    pAudioSrc = NULL;
475f6f5ceb363286d5ebef2c2e70c8a5aa135d5d1eeGlenn Kasten                    pAudioSnk = NULL;
476f6f5ceb363286d5ebef2c2e70c8a5aa135d5d1eeGlenn Kasten
4773af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                    // check the audio source and sink parameters against platform support
4783af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi#ifdef ANDROID
479bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten                    result = android_audioRecorder_checkSourceSinkSupport(thiz);
4803af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                    if (SL_RESULT_SUCCESS != result) {
481a7b79e766ec6d95e9236168c27461c2ebaef4659Glenn Kasten                        SL_LOGE("Cannot create AudioRecorder: invalid source or sink");
4823af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                        break;
4833af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                    }
4843af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi#endif
4853af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
4864b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten#ifdef ANDROID
4879e60b0a390d780539459f41c2bf4a45a326a7b62Glenn Kasten                    // Allocate memory for buffer queue
488bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten                    SLuint32 locatorType = thiz->mDataSink.mLocator.mLocatorType;
48901e9f5fa4698856f92bcfd88188ee4c8397b22dbGlenn Kasten                    if (locatorType == SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE) {
490bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten                        thiz->mBufferQueue.mNumBuffers =
491bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten                            thiz->mDataSink.mLocator.mBufferQueue.numBuffers;
4923af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                        // inline allocation of circular Buffer Queue mArray, up to a typical max
493bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten                        if (BUFFER_HEADER_TYPICAL >= thiz->mBufferQueue.mNumBuffers) {
494bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten                            thiz->mBufferQueue.mArray = thiz->mBufferQueue.mTypical;
4953af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                        } else {
4963af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                            // Avoid possible integer overflow during multiplication; this arbitrary
4973af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                            // maximum is big enough to not interfere with real applications, but
4983af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                            // small enough to not overflow.
499bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten                            if (thiz->mBufferQueue.mNumBuffers >= 256) {
5003af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                                result = SL_RESULT_MEMORY_FAILURE;
5013af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                                break;
5023af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                            }
503bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten                            thiz->mBufferQueue.mArray = (BufferHeader *) malloc((thiz->mBufferQueue.
5043af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                                    mNumBuffers + 1) * sizeof(BufferHeader));
505bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten                            if (NULL == thiz->mBufferQueue.mArray) {
5063af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                                result = SL_RESULT_MEMORY_FAILURE;
5073af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                                break;
5083af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                            }
5093af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                        }
510bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten                        thiz->mBufferQueue.mFront = thiz->mBufferQueue.mArray;
511bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten                        thiz->mBufferQueue.mRear = thiz->mBufferQueue.mArray;
5123af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                    }
5134b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten#endif
5143af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi
5153af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi                    // platform-specific initialization
5163af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi#ifdef ANDROID
517bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten                    android_audioRecorder_create(thiz);
5183af2a8dd03f3113d5da1000dd79c143a9f0c4f36Jean-Michel Trivi#endif
519ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten
520ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten                } while (0);
521ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten
522928ea4ffff40c82987cfb1ac9e3095fdc6968709Glenn Kasten                if (SL_RESULT_SUCCESS != result) {
523bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten                    IObject_Destroy(&thiz->mObject.mItf);
524f51dba65751107c930759938775b75531ec1f330Glenn Kasten                } else {
525bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten                    IObject_Publish(&thiz->mObject);
526f51dba65751107c930759938775b75531ec1f330Glenn Kasten                    // return the new audio recorder object
527bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten                    *pRecorder = &thiz->mObject.mItf;
528928ea4ffff40c82987cfb1ac9e3095fdc6968709Glenn Kasten                }
529ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten            }
530ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten
531ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten        }
532ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten
533ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten    }
5347a79f519d89eb0e1a5b3f4005484b16d6854d7e2Glenn Kasten#else
5357a79f519d89eb0e1a5b3f4005484b16d6854d7e2Glenn Kasten    result = SL_RESULT_FEATURE_UNSUPPORTED;
5367a79f519d89eb0e1a5b3f4005484b16d6854d7e2Glenn Kasten#endif
537ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten
538ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten    SL_LEAVE_INTERFACE
5390b167267bda99b68346045ccab14e810121d5de4Glenn Kasten}
5400b167267bda99b68346045ccab14e810121d5de4Glenn Kasten
541ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten
5420b167267bda99b68346045ccab14e810121d5de4Glenn Kastenstatic SLresult IEngine_CreateMidiPlayer(SLEngineItf self, SLObjectItf *pPlayer,
5430b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    SLDataSource *pMIDISrc, SLDataSource *pBankSrc, SLDataSink *pAudioOutput,
5440b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    SLDataSink *pVibra, SLDataSink *pLEDArray, SLuint32 numInterfaces,
5450b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    const SLInterfaceID *pInterfaceIds, const SLboolean *pInterfaceRequired)
5460b167267bda99b68346045ccab14e810121d5de4Glenn Kasten{
547ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten    SL_ENTER_INTERFACE
548ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten
549a3080daa505f91df51a808c85ddb37c48745bf7cGlenn Kasten#if USE_PROFILES & (USE_PROFILES_GAME | USE_PROFILES_PHONE)
550928ea4ffff40c82987cfb1ac9e3095fdc6968709Glenn Kasten    if ((NULL == pPlayer) || (NULL == pMIDISrc) || (NULL == pAudioOutput)) {
551ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten        result = SL_RESULT_PARAMETER_INVALID;
552ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten    } else {
553ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten        *pPlayer = NULL;
554ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten        unsigned exposedMask;
555ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten        const ClassTable *pCMidiPlayer_class = objectIDtoClass(SL_OBJECTID_MIDIPLAYER);
556c2b9d79c4b59caff965076f445f5a735a360b084Glenn Kasten        if (NULL == pCMidiPlayer_class) {
557c2b9d79c4b59caff965076f445f5a735a360b084Glenn Kasten            result = SL_RESULT_FEATURE_UNSUPPORTED;
558c2b9d79c4b59caff965076f445f5a735a360b084Glenn Kasten        } else {
559c2b9d79c4b59caff965076f445f5a735a360b084Glenn Kasten            result = checkInterfaces(pCMidiPlayer_class, numInterfaces,
56005c7b2d09d54b9260ff7f3f5e491f38d0097c406Glenn Kasten                pInterfaceIds, pInterfaceRequired, &exposedMask, NULL);
561c2b9d79c4b59caff965076f445f5a735a360b084Glenn Kasten        }
562ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten        if (SL_RESULT_SUCCESS == result) {
563bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten            CMidiPlayer *thiz = (CMidiPlayer *) construct(pCMidiPlayer_class, exposedMask, self);
564bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten            if (NULL == thiz) {
565ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten                result = SL_RESULT_MEMORY_FAILURE;
566ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten            } else {
56794a37e8117fb72790882dfb815f99e2365754c74Glenn Kasten#if 0
56894a37e8117fb72790882dfb815f99e2365754c74Glenn Kasten                "pMIDISrc", pMIDISrc, URI | MIDIBUFFERQUEUE, NONE
56994a37e8117fb72790882dfb815f99e2365754c74Glenn Kasten                "pBankSrc", pBanksrc, NULL | URI | ADDRESS, NULL
57094a37e8117fb72790882dfb815f99e2365754c74Glenn Kasten                "pAudioOutput", pAudioOutput, OUTPUTMIX, NULL
57194a37e8117fb72790882dfb815f99e2365754c74Glenn Kasten                "pVibra", pVibra, NULL | IODEVICE, NULL
57294a37e8117fb72790882dfb815f99e2365754c74Glenn Kasten                "pLEDArray", pLEDArray, NULL | IODEVICE, NULL
57394a37e8117fb72790882dfb815f99e2365754c74Glenn Kasten#endif
5749e0f457f2809f5f062ac052d4a304e8996b49a80Glenn Kasten                // a fake value - why not use value from IPlay_init? what does CT check for?
575bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten                thiz->mPlay.mDuration = 0;
576bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten                IObject_Publish(&thiz->mObject);
577f51dba65751107c930759938775b75531ec1f330Glenn Kasten                // return the new MIDI player object
578bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten                *pPlayer = &thiz->mObject.mItf;
579ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten            }
580ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten        }
581ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten    }
5827a79f519d89eb0e1a5b3f4005484b16d6854d7e2Glenn Kasten#else
5837a79f519d89eb0e1a5b3f4005484b16d6854d7e2Glenn Kasten    result = SL_RESULT_FEATURE_UNSUPPORTED;
5847a79f519d89eb0e1a5b3f4005484b16d6854d7e2Glenn Kasten#endif
585ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten
586ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten    SL_LEAVE_INTERFACE
5870b167267bda99b68346045ccab14e810121d5de4Glenn Kasten}
5880b167267bda99b68346045ccab14e810121d5de4Glenn Kasten
589ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten
5900b167267bda99b68346045ccab14e810121d5de4Glenn Kastenstatic SLresult IEngine_CreateListener(SLEngineItf self, SLObjectItf *pListener,
591d2a7f0d6883a6d3835642e7b282f05ed1c54fe63Glenn Kasten    SLuint32 numInterfaces, const SLInterfaceID *pInterfaceIds, const SLboolean *pInterfaceRequired)
5920b167267bda99b68346045ccab14e810121d5de4Glenn Kasten{
593ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten    SL_ENTER_INTERFACE
594ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten
595a3080daa505f91df51a808c85ddb37c48745bf7cGlenn Kasten#if USE_PROFILES & USE_PROFILES_GAME
596ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten    if (NULL == pListener) {
597ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten        result = SL_RESULT_PARAMETER_INVALID;
598ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten    } else {
599ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten        *pListener = NULL;
600ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten        unsigned exposedMask;
601ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten        const ClassTable *pCListener_class = objectIDtoClass(SL_OBJECTID_LISTENER);
602c2b9d79c4b59caff965076f445f5a735a360b084Glenn Kasten        if (NULL == pCListener_class) {
603c2b9d79c4b59caff965076f445f5a735a360b084Glenn Kasten            result = SL_RESULT_FEATURE_UNSUPPORTED;
604c2b9d79c4b59caff965076f445f5a735a360b084Glenn Kasten        } else {
605c2b9d79c4b59caff965076f445f5a735a360b084Glenn Kasten            result = checkInterfaces(pCListener_class, numInterfaces,
60605c7b2d09d54b9260ff7f3f5e491f38d0097c406Glenn Kasten                pInterfaceIds, pInterfaceRequired, &exposedMask, NULL);
607c2b9d79c4b59caff965076f445f5a735a360b084Glenn Kasten        }
608ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten        if (SL_RESULT_SUCCESS == result) {
609bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten            CListener *thiz = (CListener *) construct(pCListener_class, exposedMask, self);
610bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten            if (NULL == thiz) {
611ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten                result = SL_RESULT_MEMORY_FAILURE;
612ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten            } else {
613bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten                IObject_Publish(&thiz->mObject);
614f51dba65751107c930759938775b75531ec1f330Glenn Kasten                // return the new 3D listener object
615bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten                *pListener = &thiz->mObject.mItf;
616ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten            }
617ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten        }
618ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten    }
6197a79f519d89eb0e1a5b3f4005484b16d6854d7e2Glenn Kasten#else
6207a79f519d89eb0e1a5b3f4005484b16d6854d7e2Glenn Kasten    result = SL_RESULT_FEATURE_UNSUPPORTED;
6217a79f519d89eb0e1a5b3f4005484b16d6854d7e2Glenn Kasten#endif
622ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten
623ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten    SL_LEAVE_INTERFACE
6240b167267bda99b68346045ccab14e810121d5de4Glenn Kasten}
6250b167267bda99b68346045ccab14e810121d5de4Glenn Kasten
626ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten
627d2a7f0d6883a6d3835642e7b282f05ed1c54fe63Glenn Kastenstatic SLresult IEngine_Create3DGroup(SLEngineItf self, SLObjectItf *pGroup, SLuint32 numInterfaces,
628d2a7f0d6883a6d3835642e7b282f05ed1c54fe63Glenn Kasten    const SLInterfaceID *pInterfaceIds, const SLboolean *pInterfaceRequired)
6290b167267bda99b68346045ccab14e810121d5de4Glenn Kasten{
630ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten    SL_ENTER_INTERFACE
631ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten
632a3080daa505f91df51a808c85ddb37c48745bf7cGlenn Kasten#if USE_PROFILES & USE_PROFILES_GAME
633ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten    if (NULL == pGroup) {
634ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten        result = SL_RESULT_PARAMETER_INVALID;
635ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten    } else {
636ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten        *pGroup = NULL;
637ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten        unsigned exposedMask;
638ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten        const ClassTable *pC3DGroup_class = objectIDtoClass(SL_OBJECTID_3DGROUP);
639c2b9d79c4b59caff965076f445f5a735a360b084Glenn Kasten        if (NULL == pC3DGroup_class) {
640c2b9d79c4b59caff965076f445f5a735a360b084Glenn Kasten            result = SL_RESULT_FEATURE_UNSUPPORTED;
641c2b9d79c4b59caff965076f445f5a735a360b084Glenn Kasten        } else {
642c2b9d79c4b59caff965076f445f5a735a360b084Glenn Kasten            result = checkInterfaces(pC3DGroup_class, numInterfaces,
64305c7b2d09d54b9260ff7f3f5e491f38d0097c406Glenn Kasten                pInterfaceIds, pInterfaceRequired, &exposedMask, NULL);
644c2b9d79c4b59caff965076f445f5a735a360b084Glenn Kasten        }
645ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten        if (SL_RESULT_SUCCESS == result) {
646bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten            C3DGroup *thiz = (C3DGroup *) construct(pC3DGroup_class, exposedMask, self);
647bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten            if (NULL == thiz) {
648ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten                result = SL_RESULT_MEMORY_FAILURE;
649ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten            } else {
650bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten                thiz->mMemberMask = 0;
651bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten                IObject_Publish(&thiz->mObject);
652f51dba65751107c930759938775b75531ec1f330Glenn Kasten                // return the new 3D group object
653bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten                *pGroup = &thiz->mObject.mItf;
654ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten            }
655ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten        }
656ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten    }
6577a79f519d89eb0e1a5b3f4005484b16d6854d7e2Glenn Kasten#else
6587a79f519d89eb0e1a5b3f4005484b16d6854d7e2Glenn Kasten    result = SL_RESULT_FEATURE_UNSUPPORTED;
6597a79f519d89eb0e1a5b3f4005484b16d6854d7e2Glenn Kasten#endif
660ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten
661ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten    SL_LEAVE_INTERFACE
6620b167267bda99b68346045ccab14e810121d5de4Glenn Kasten}
6630b167267bda99b68346045ccab14e810121d5de4Glenn Kasten
664ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten
66500d2d554e04ac369367c903dbf53b975355d1bcdGlenn Kastenstatic SLresult IEngine_CreateOutputMix(SLEngineItf self, SLObjectItf *pMix, SLuint32 numInterfaces,
66600d2d554e04ac369367c903dbf53b975355d1bcdGlenn Kasten    const SLInterfaceID *pInterfaceIds, const SLboolean *pInterfaceRequired)
6670b167267bda99b68346045ccab14e810121d5de4Glenn Kasten{
668ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten    SL_ENTER_INTERFACE
669ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten
670ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten    if (NULL == pMix) {
671ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten        result = SL_RESULT_PARAMETER_INVALID;
672ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten    } else {
673ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten        *pMix = NULL;
674ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten        unsigned exposedMask;
675ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten        const ClassTable *pCOutputMix_class = objectIDtoClass(SL_OBJECTID_OUTPUTMIX);
676c2b9d79c4b59caff965076f445f5a735a360b084Glenn Kasten        assert(NULL != pCOutputMix_class);
677ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten        result = checkInterfaces(pCOutputMix_class, numInterfaces,
67805c7b2d09d54b9260ff7f3f5e491f38d0097c406Glenn Kasten            pInterfaceIds, pInterfaceRequired, &exposedMask, NULL);
679ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten        if (SL_RESULT_SUCCESS == result) {
680bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten            COutputMix *thiz = (COutputMix *) construct(pCOutputMix_class, exposedMask, self);
681bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten            if (NULL == thiz) {
682ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten                result = SL_RESULT_MEMORY_FAILURE;
683ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten            } else {
68443e7743b5297cc5afcb246a7e2b5d825a607f8afJean-Michel Trivi#ifdef ANDROID
685bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten                android_outputMix_create(thiz);
68643e7743b5297cc5afcb246a7e2b5d825a607f8afJean-Michel Trivi#endif
687f51dba65751107c930759938775b75531ec1f330Glenn Kasten#ifdef USE_SDL
688bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten                IEngine *thisEngine = &thiz->mObject.mEngine->mEngine;
689f51dba65751107c930759938775b75531ec1f330Glenn Kasten                interface_lock_exclusive(thisEngine);
690f51dba65751107c930759938775b75531ec1f330Glenn Kasten                bool unpause = false;
691f51dba65751107c930759938775b75531ec1f330Glenn Kasten                if (NULL == thisEngine->mOutputMix) {
692bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten                    thisEngine->mOutputMix = thiz;
693f51dba65751107c930759938775b75531ec1f330Glenn Kasten                    unpause = true;
694f51dba65751107c930759938775b75531ec1f330Glenn Kasten                }
695f51dba65751107c930759938775b75531ec1f330Glenn Kasten                interface_unlock_exclusive(thisEngine);
696f51dba65751107c930759938775b75531ec1f330Glenn Kasten#endif
697bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten                IObject_Publish(&thiz->mObject);
698f51dba65751107c930759938775b75531ec1f330Glenn Kasten#ifdef USE_SDL
699f51dba65751107c930759938775b75531ec1f330Glenn Kasten                if (unpause) {
700f51dba65751107c930759938775b75531ec1f330Glenn Kasten                    // Enable SDL_callback to be called periodically by SDL's internal thread
701f51dba65751107c930759938775b75531ec1f330Glenn Kasten                    SDL_PauseAudio(0);
702f51dba65751107c930759938775b75531ec1f330Glenn Kasten                }
703f51dba65751107c930759938775b75531ec1f330Glenn Kasten#endif
704f51dba65751107c930759938775b75531ec1f330Glenn Kasten                // return the new output mix object
705bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten                *pMix = &thiz->mObject.mItf;
706ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten            }
707ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten        }
708ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten    }
709ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten
710ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten    SL_LEAVE_INTERFACE
7110b167267bda99b68346045ccab14e810121d5de4Glenn Kasten}
7120b167267bda99b68346045ccab14e810121d5de4Glenn Kasten
713ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten
714d2a7f0d6883a6d3835642e7b282f05ed1c54fe63Glenn Kastenstatic SLresult IEngine_CreateMetadataExtractor(SLEngineItf self, SLObjectItf *pMetadataExtractor,
715d2a7f0d6883a6d3835642e7b282f05ed1c54fe63Glenn Kasten    SLDataSource *pDataSource, SLuint32 numInterfaces, const SLInterfaceID *pInterfaceIds,
7160b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    const SLboolean *pInterfaceRequired)
7170b167267bda99b68346045ccab14e810121d5de4Glenn Kasten{
718ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten    SL_ENTER_INTERFACE
719ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten
720a3080daa505f91df51a808c85ddb37c48745bf7cGlenn Kasten#if USE_PROFILES & (USE_PROFILES_GAME | USE_PROFILES_MUSIC)
721ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten    if (NULL == pMetadataExtractor) {
722ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten        result = SL_RESULT_PARAMETER_INVALID;
723ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten    } else {
724ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten        *pMetadataExtractor = NULL;
725ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten        unsigned exposedMask;
726ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten        const ClassTable *pCMetadataExtractor_class =
727ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten            objectIDtoClass(SL_OBJECTID_METADATAEXTRACTOR);
728c2b9d79c4b59caff965076f445f5a735a360b084Glenn Kasten        if (NULL == pCMetadataExtractor_class) {
729c2b9d79c4b59caff965076f445f5a735a360b084Glenn Kasten            result = SL_RESULT_FEATURE_UNSUPPORTED;
730c2b9d79c4b59caff965076f445f5a735a360b084Glenn Kasten        } else {
731c2b9d79c4b59caff965076f445f5a735a360b084Glenn Kasten            result = checkInterfaces(pCMetadataExtractor_class, numInterfaces,
73205c7b2d09d54b9260ff7f3f5e491f38d0097c406Glenn Kasten                pInterfaceIds, pInterfaceRequired, &exposedMask, NULL);
733c2b9d79c4b59caff965076f445f5a735a360b084Glenn Kasten        }
734ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten        if (SL_RESULT_SUCCESS == result) {
735bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten            CMetadataExtractor *thiz = (CMetadataExtractor *)
736ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten                construct(pCMetadataExtractor_class, exposedMask, self);
737bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten            if (NULL == thiz) {
738ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten                result = SL_RESULT_MEMORY_FAILURE;
739ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten            } else {
74094a37e8117fb72790882dfb815f99e2365754c74Glenn Kasten#if 0
74194a37e8117fb72790882dfb815f99e2365754c74Glenn Kasten                "pDataSource", pDataSource, NONE, NONE
74294a37e8117fb72790882dfb815f99e2365754c74Glenn Kasten#endif
743bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten                IObject_Publish(&thiz->mObject);
744f51dba65751107c930759938775b75531ec1f330Glenn Kasten                // return the new metadata extractor object
745bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten                *pMetadataExtractor = &thiz->mObject.mItf;
746ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten                result = SL_RESULT_SUCCESS;
747ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten            }
748ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten        }
749ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten    }
7507a79f519d89eb0e1a5b3f4005484b16d6854d7e2Glenn Kasten#else
7517a79f519d89eb0e1a5b3f4005484b16d6854d7e2Glenn Kasten    result = SL_RESULT_FEATURE_UNSUPPORTED;
7527a79f519d89eb0e1a5b3f4005484b16d6854d7e2Glenn Kasten#endif
753ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten
754ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten    SL_LEAVE_INTERFACE
7550b167267bda99b68346045ccab14e810121d5de4Glenn Kasten}
7560b167267bda99b68346045ccab14e810121d5de4Glenn Kasten
757ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten
758d2a7f0d6883a6d3835642e7b282f05ed1c54fe63Glenn Kastenstatic SLresult IEngine_CreateExtensionObject(SLEngineItf self, SLObjectItf *pObject,
759d2a7f0d6883a6d3835642e7b282f05ed1c54fe63Glenn Kasten    void *pParameters, SLuint32 objectID, SLuint32 numInterfaces,
760d2a7f0d6883a6d3835642e7b282f05ed1c54fe63Glenn Kasten    const SLInterfaceID *pInterfaceIds, const SLboolean *pInterfaceRequired)
7610b167267bda99b68346045ccab14e810121d5de4Glenn Kasten{
762ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten    SL_ENTER_INTERFACE
763ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten
764ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten    if (NULL == pObject) {
765ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten        result = SL_RESULT_PARAMETER_INVALID;
766ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten    } else {
767ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten        *pObject = NULL;
768ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten        result = SL_RESULT_FEATURE_UNSUPPORTED;
769ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten    }
770ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten
771ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten    SL_LEAVE_INTERFACE
7720b167267bda99b68346045ccab14e810121d5de4Glenn Kasten}
7730b167267bda99b68346045ccab14e810121d5de4Glenn Kasten
774ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten
7750b167267bda99b68346045ccab14e810121d5de4Glenn Kastenstatic SLresult IEngine_QueryNumSupportedInterfaces(SLEngineItf self,
7760b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    SLuint32 objectID, SLuint32 *pNumSupportedInterfaces)
7770b167267bda99b68346045ccab14e810121d5de4Glenn Kasten{
778ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten    SL_ENTER_INTERFACE
779ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten
780ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten    if (NULL == pNumSupportedInterfaces) {
781ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten        result = SL_RESULT_PARAMETER_INVALID;
782ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten    } else {
783bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten        const ClassTable *clazz = objectIDtoClass(objectID);
784bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten        if (NULL == clazz) {
785ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten            result = SL_RESULT_FEATURE_UNSUPPORTED;
786ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten        } else {
787ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten            SLuint32 count = 0;
788ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten            SLuint32 i;
789bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten            for (i = 0; i < clazz->mInterfaceCount; ++i) {
790bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten                switch (clazz->mInterfaces[i].mInterface) {
791c3845706778edec308bd9ced559cec0a456608bdGlenn Kasten                case INTERFACE_IMPLICIT:
792104c000a6f36b871ca11e0394db1e5217f374cafGlenn Kasten                case INTERFACE_IMPLICIT_PREREALIZE:
793c3845706778edec308bd9ced559cec0a456608bdGlenn Kasten                case INTERFACE_EXPLICIT:
7945e3b06982dbf1eae237cc74326e66d51d3cdd664Glenn Kasten                case INTERFACE_EXPLICIT_PREREALIZE:
795c3845706778edec308bd9ced559cec0a456608bdGlenn Kasten                case INTERFACE_DYNAMIC:
796ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten                    ++count;
797c3845706778edec308bd9ced559cec0a456608bdGlenn Kasten                    break;
798c3845706778edec308bd9ced559cec0a456608bdGlenn Kasten                case INTERFACE_UNAVAILABLE:
799c3845706778edec308bd9ced559cec0a456608bdGlenn Kasten                    break;
800c3845706778edec308bd9ced559cec0a456608bdGlenn Kasten                default:
801c3845706778edec308bd9ced559cec0a456608bdGlenn Kasten                    assert(false);
802c3845706778edec308bd9ced559cec0a456608bdGlenn Kasten                    break;
803c3845706778edec308bd9ced559cec0a456608bdGlenn Kasten                }
804c3845706778edec308bd9ced559cec0a456608bdGlenn Kasten            }
805ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten            *pNumSupportedInterfaces = count;
806ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten            result = SL_RESULT_SUCCESS;
807ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten        }
808ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten    }
809ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten
810ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten    SL_LEAVE_INTERFACE;
8110b167267bda99b68346045ccab14e810121d5de4Glenn Kasten}
8120b167267bda99b68346045ccab14e810121d5de4Glenn Kasten
813ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten
8140b167267bda99b68346045ccab14e810121d5de4Glenn Kastenstatic SLresult IEngine_QuerySupportedInterfaces(SLEngineItf self,
8150b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    SLuint32 objectID, SLuint32 index, SLInterfaceID *pInterfaceId)
8160b167267bda99b68346045ccab14e810121d5de4Glenn Kasten{
817ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten    SL_ENTER_INTERFACE
818ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten
819ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten    if (NULL == pInterfaceId) {
820ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten        result = SL_RESULT_PARAMETER_INVALID;
821ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten    } else {
822ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten        *pInterfaceId = NULL;
823bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten        const ClassTable *clazz = objectIDtoClass(objectID);
824bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten        if (NULL == clazz) {
825ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten            result = SL_RESULT_FEATURE_UNSUPPORTED;
826ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten        } else {
827ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten            result = SL_RESULT_PARAMETER_INVALID; // will be reset later
828ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten            SLuint32 i;
829bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten            for (i = 0; i < clazz->mInterfaceCount; ++i) {
830bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten                switch (clazz->mInterfaces[i].mInterface) {
831c3845706778edec308bd9ced559cec0a456608bdGlenn Kasten                case INTERFACE_IMPLICIT:
832104c000a6f36b871ca11e0394db1e5217f374cafGlenn Kasten                case INTERFACE_IMPLICIT_PREREALIZE:
833c3845706778edec308bd9ced559cec0a456608bdGlenn Kasten                case INTERFACE_EXPLICIT:
8345e3b06982dbf1eae237cc74326e66d51d3cdd664Glenn Kasten                case INTERFACE_EXPLICIT_PREREALIZE:
835c3845706778edec308bd9ced559cec0a456608bdGlenn Kasten                case INTERFACE_DYNAMIC:
836c3845706778edec308bd9ced559cec0a456608bdGlenn Kasten                    break;
837c3845706778edec308bd9ced559cec0a456608bdGlenn Kasten                case INTERFACE_UNAVAILABLE:
838ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten                    continue;
839c3845706778edec308bd9ced559cec0a456608bdGlenn Kasten                default:
840c3845706778edec308bd9ced559cec0a456608bdGlenn Kasten                    assert(false);
841c3845706778edec308bd9ced559cec0a456608bdGlenn Kasten                    break;
842c3845706778edec308bd9ced559cec0a456608bdGlenn Kasten                }
843ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten                if (index == 0) {
844bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten                    *pInterfaceId = &SL_IID_array[clazz->mInterfaces[i].mMPH];
845ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten                    result = SL_RESULT_SUCCESS;
846ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten                    break;
847ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten                }
848ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten                --index;
849ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten            }
850a438eb1cf1ae602afab00336528dd230bd929206Glenn Kasten        }
851a438eb1cf1ae602afab00336528dd230bd929206Glenn Kasten    }
852ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten
853ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten    SL_LEAVE_INTERFACE
8540b167267bda99b68346045ccab14e810121d5de4Glenn Kasten};
8550b167267bda99b68346045ccab14e810121d5de4Glenn Kasten
856ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten
857b52bc98b576a9b56e82eca435849bd55e54b6bc1Glenn Kastenstatic const char * const extensionNames[] = {
858b52bc98b576a9b56e82eca435849bd55e54b6bc1Glenn Kasten#ifdef ANDROID
8594076e5009bfe70bc06a78f5aafec77af6c03201dGlenn Kasten#define _(n) #n
8604076e5009bfe70bc06a78f5aafec77af6c03201dGlenn Kasten#define __(n) _(n)
8614076e5009bfe70bc06a78f5aafec77af6c03201dGlenn Kasten    "ANDROID_SDK_LEVEL_" __(PLATFORM_SDK_VERSION),
8624076e5009bfe70bc06a78f5aafec77af6c03201dGlenn Kasten#undef _
8634076e5009bfe70bc06a78f5aafec77af6c03201dGlenn Kasten#undef __
864b52bc98b576a9b56e82eca435849bd55e54b6bc1Glenn Kasten#else
865b52bc98b576a9b56e82eca435849bd55e54b6bc1Glenn Kasten    "WILHELM_DESKTOP",
866b52bc98b576a9b56e82eca435849bd55e54b6bc1Glenn Kasten#endif
867b52bc98b576a9b56e82eca435849bd55e54b6bc1Glenn Kasten};
868b52bc98b576a9b56e82eca435849bd55e54b6bc1Glenn Kasten
869b52bc98b576a9b56e82eca435849bd55e54b6bc1Glenn Kasten
870ccdf07b17f23b4c040dd3f62478d0965eba804e3Glenn Kastenstatic SLresult IEngine_QueryNumSupportedExtensions(SLEngineItf self, SLuint32 *pNumExtensions)
8710b167267bda99b68346045ccab14e810121d5de4Glenn Kasten{
872ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten    SL_ENTER_INTERFACE
873ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten
874ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten    if (NULL == pNumExtensions) {
875ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten        result = SL_RESULT_PARAMETER_INVALID;
876ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten    } else {
877b52bc98b576a9b56e82eca435849bd55e54b6bc1Glenn Kasten        *pNumExtensions = sizeof(extensionNames) / sizeof(extensionNames[0]);
878ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten        result = SL_RESULT_SUCCESS;
879ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten    }
880ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten
881ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten    SL_LEAVE_INTERFACE
8820b167267bda99b68346045ccab14e810121d5de4Glenn Kasten}
8830b167267bda99b68346045ccab14e810121d5de4Glenn Kasten
884ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten
8850b167267bda99b68346045ccab14e810121d5de4Glenn Kastenstatic SLresult IEngine_QuerySupportedExtension(SLEngineItf self,
8860b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    SLuint32 index, SLchar *pExtensionName, SLint16 *pNameLength)
8870b167267bda99b68346045ccab14e810121d5de4Glenn Kasten{
888ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten    SL_ENTER_INTERFACE
889ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten
890b52bc98b576a9b56e82eca435849bd55e54b6bc1Glenn Kasten    if (NULL == pNameLength) {
891b52bc98b576a9b56e82eca435849bd55e54b6bc1Glenn Kasten        result = SL_RESULT_PARAMETER_INVALID;
892b52bc98b576a9b56e82eca435849bd55e54b6bc1Glenn Kasten    } else {
893b52bc98b576a9b56e82eca435849bd55e54b6bc1Glenn Kasten        size_t actualNameLength;
894b52bc98b576a9b56e82eca435849bd55e54b6bc1Glenn Kasten        unsigned numExtensions = sizeof(extensionNames) / sizeof(extensionNames[0]);
895b52bc98b576a9b56e82eca435849bd55e54b6bc1Glenn Kasten        if (index >= numExtensions) {
896b52bc98b576a9b56e82eca435849bd55e54b6bc1Glenn Kasten            actualNameLength = 0;
897b52bc98b576a9b56e82eca435849bd55e54b6bc1Glenn Kasten            result = SL_RESULT_PARAMETER_INVALID;
898b52bc98b576a9b56e82eca435849bd55e54b6bc1Glenn Kasten        } else {
899b52bc98b576a9b56e82eca435849bd55e54b6bc1Glenn Kasten            const char *extensionName = extensionNames[index];
900b52bc98b576a9b56e82eca435849bd55e54b6bc1Glenn Kasten            actualNameLength = strlen(extensionName) + 1;
901b52bc98b576a9b56e82eca435849bd55e54b6bc1Glenn Kasten            if (NULL == pExtensionName) {
902b52bc98b576a9b56e82eca435849bd55e54b6bc1Glenn Kasten                // application is querying the name length in order to allocate a buffer
903b52bc98b576a9b56e82eca435849bd55e54b6bc1Glenn Kasten                result = SL_RESULT_SUCCESS;
904b52bc98b576a9b56e82eca435849bd55e54b6bc1Glenn Kasten            } else {
905b52bc98b576a9b56e82eca435849bd55e54b6bc1Glenn Kasten                SLint16 availableNameLength = *pNameLength;
906b52bc98b576a9b56e82eca435849bd55e54b6bc1Glenn Kasten                if (0 >= availableNameLength) {
907b52bc98b576a9b56e82eca435849bd55e54b6bc1Glenn Kasten                    // there is not even room for the terminating NUL
908b52bc98b576a9b56e82eca435849bd55e54b6bc1Glenn Kasten                    result = SL_RESULT_BUFFER_INSUFFICIENT;
909b52bc98b576a9b56e82eca435849bd55e54b6bc1Glenn Kasten                } else if (actualNameLength > (size_t) availableNameLength) {
910b52bc98b576a9b56e82eca435849bd55e54b6bc1Glenn Kasten                    // "no invalid strings are written. That is, the null-terminator always exists"
911b52bc98b576a9b56e82eca435849bd55e54b6bc1Glenn Kasten                    memcpy(pExtensionName, extensionName, (size_t) availableNameLength - 1);
912b52bc98b576a9b56e82eca435849bd55e54b6bc1Glenn Kasten                    pExtensionName[(size_t) availableNameLength - 1] = '\0';
913b52bc98b576a9b56e82eca435849bd55e54b6bc1Glenn Kasten                    result = SL_RESULT_BUFFER_INSUFFICIENT;
914b52bc98b576a9b56e82eca435849bd55e54b6bc1Glenn Kasten                } else {
915b52bc98b576a9b56e82eca435849bd55e54b6bc1Glenn Kasten                    memcpy(pExtensionName, extensionName, actualNameLength);
916b52bc98b576a9b56e82eca435849bd55e54b6bc1Glenn Kasten                    result = SL_RESULT_SUCCESS;
917b52bc98b576a9b56e82eca435849bd55e54b6bc1Glenn Kasten                }
918b52bc98b576a9b56e82eca435849bd55e54b6bc1Glenn Kasten            }
919b52bc98b576a9b56e82eca435849bd55e54b6bc1Glenn Kasten        }
920b52bc98b576a9b56e82eca435849bd55e54b6bc1Glenn Kasten        *pNameLength = actualNameLength;
921b52bc98b576a9b56e82eca435849bd55e54b6bc1Glenn Kasten    }
922ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten
923ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten    SL_LEAVE_INTERFACE
9240b167267bda99b68346045ccab14e810121d5de4Glenn Kasten}
9250b167267bda99b68346045ccab14e810121d5de4Glenn Kasten
926ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten
9270b167267bda99b68346045ccab14e810121d5de4Glenn Kastenstatic SLresult IEngine_IsExtensionSupported(SLEngineItf self,
9280b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    const SLchar *pExtensionName, SLboolean *pSupported)
9290b167267bda99b68346045ccab14e810121d5de4Glenn Kasten{
930ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten    SL_ENTER_INTERFACE
931ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten
932b52bc98b576a9b56e82eca435849bd55e54b6bc1Glenn Kasten    if (NULL == pSupported) {
933ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten        result = SL_RESULT_PARAMETER_INVALID;
934ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten    } else {
935b52bc98b576a9b56e82eca435849bd55e54b6bc1Glenn Kasten        SLboolean isSupported = SL_BOOLEAN_FALSE;
936b52bc98b576a9b56e82eca435849bd55e54b6bc1Glenn Kasten        if (NULL == pExtensionName) {
937b52bc98b576a9b56e82eca435849bd55e54b6bc1Glenn Kasten            result = SL_RESULT_PARAMETER_INVALID;
938b52bc98b576a9b56e82eca435849bd55e54b6bc1Glenn Kasten        } else {
939b52bc98b576a9b56e82eca435849bd55e54b6bc1Glenn Kasten            unsigned numExtensions = sizeof(extensionNames) / sizeof(extensionNames[0]);
940b52bc98b576a9b56e82eca435849bd55e54b6bc1Glenn Kasten            unsigned i;
941b52bc98b576a9b56e82eca435849bd55e54b6bc1Glenn Kasten            for (i = 0; i < numExtensions; ++i) {
942b52bc98b576a9b56e82eca435849bd55e54b6bc1Glenn Kasten                if (!strcmp((const char *) pExtensionName, extensionNames[i])) {
943b52bc98b576a9b56e82eca435849bd55e54b6bc1Glenn Kasten                    isSupported = SL_BOOLEAN_TRUE;
944b52bc98b576a9b56e82eca435849bd55e54b6bc1Glenn Kasten                    break;
945b52bc98b576a9b56e82eca435849bd55e54b6bc1Glenn Kasten                }
946b52bc98b576a9b56e82eca435849bd55e54b6bc1Glenn Kasten            }
947b52bc98b576a9b56e82eca435849bd55e54b6bc1Glenn Kasten            result = SL_RESULT_SUCCESS;
948b52bc98b576a9b56e82eca435849bd55e54b6bc1Glenn Kasten        }
949b52bc98b576a9b56e82eca435849bd55e54b6bc1Glenn Kasten        *pSupported = isSupported;
950ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten    }
951ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten
952ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten    SL_LEAVE_INTERFACE
9530b167267bda99b68346045ccab14e810121d5de4Glenn Kasten}
9540b167267bda99b68346045ccab14e810121d5de4Glenn Kasten
955ed46c29d6a09112dbbf584c82953f63289596fd6Glenn Kasten
9560b167267bda99b68346045ccab14e810121d5de4Glenn Kastenstatic const struct SLEngineItf_ IEngine_Itf = {
9570b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    IEngine_CreateLEDDevice,
9580b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    IEngine_CreateVibraDevice,
9590b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    IEngine_CreateAudioPlayer,
9600b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    IEngine_CreateAudioRecorder,
9610b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    IEngine_CreateMidiPlayer,
9620b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    IEngine_CreateListener,
9630b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    IEngine_Create3DGroup,
9640b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    IEngine_CreateOutputMix,
9650b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    IEngine_CreateMetadataExtractor,
9660b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    IEngine_CreateExtensionObject,
9670b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    IEngine_QueryNumSupportedInterfaces,
9680b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    IEngine_QuerySupportedInterfaces,
9690b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    IEngine_QueryNumSupportedExtensions,
9700b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    IEngine_QuerySupportedExtension,
9710b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    IEngine_IsExtensionSupported
9720b167267bda99b68346045ccab14e810121d5de4Glenn Kasten};
9730b167267bda99b68346045ccab14e810121d5de4Glenn Kasten
9740b167267bda99b68346045ccab14e810121d5de4Glenn Kastenvoid IEngine_init(void *self)
9750b167267bda99b68346045ccab14e810121d5de4Glenn Kasten{
976bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten    IEngine *thiz = (IEngine *) self;
977bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten    thiz->mItf = &IEngine_Itf;
978a9a70a4451545034c9263dd55b181f2912534c37Glenn Kasten    // mLossOfControlGlobal is initialized in slCreateEngine
97900d2d554e04ac369367c903dbf53b975355d1bcdGlenn Kasten#ifdef USE_SDL
980bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten    thiz->mOutputMix = NULL;
98100d2d554e04ac369367c903dbf53b975355d1bcdGlenn Kasten#endif
982bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten    thiz->mInstanceCount = 1; // ourself
983bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten    thiz->mInstanceMask = 0;
984bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten    thiz->mChangedMask = 0;
9850b167267bda99b68346045ccab14e810121d5de4Glenn Kasten    unsigned i;
986928ea4ffff40c82987cfb1ac9e3095fdc6968709Glenn Kasten    for (i = 0; i < MAX_INSTANCE; ++i) {
987bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten        thiz->mInstances[i] = NULL;
988928ea4ffff40c82987cfb1ac9e3095fdc6968709Glenn Kasten    }
989bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten    thiz->mShutdown = SL_BOOLEAN_FALSE;
990bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten    thiz->mShutdownAck = SL_BOOLEAN_FALSE;
9910b167267bda99b68346045ccab14e810121d5de4Glenn Kasten}
992a9a70a4451545034c9263dd55b181f2912534c37Glenn Kasten
993a9a70a4451545034c9263dd55b181f2912534c37Glenn Kastenvoid IEngine_deinit(void *self)
994a9a70a4451545034c9263dd55b181f2912534c37Glenn Kasten{
995a9a70a4451545034c9263dd55b181f2912534c37Glenn Kasten}
9968db0b106cdce5f5216b1535492de7930ad738aedGlenn Kasten
9978db0b106cdce5f5216b1535492de7930ad738aedGlenn Kasten
9988db0b106cdce5f5216b1535492de7930ad738aedGlenn Kasten// OpenMAX AL Engine
9998db0b106cdce5f5216b1535492de7930ad738aedGlenn Kasten
10008db0b106cdce5f5216b1535492de7930ad738aedGlenn Kasten
10018db0b106cdce5f5216b1535492de7930ad738aedGlenn Kastenstatic XAresult IEngine_CreateCameraDevice(XAEngineItf self, XAObjectItf *pDevice,
10028db0b106cdce5f5216b1535492de7930ad738aedGlenn Kasten        XAuint32 deviceID, XAuint32 numInterfaces, const XAInterfaceID *pInterfaceIds,
10038db0b106cdce5f5216b1535492de7930ad738aedGlenn Kasten        const XAboolean *pInterfaceRequired)
10048db0b106cdce5f5216b1535492de7930ad738aedGlenn Kasten{
10058db0b106cdce5f5216b1535492de7930ad738aedGlenn Kasten    XA_ENTER_INTERFACE
10068db0b106cdce5f5216b1535492de7930ad738aedGlenn Kasten
1007bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten    //IXAEngine *thiz = (IXAEngine *) self;
10088db0b106cdce5f5216b1535492de7930ad738aedGlenn Kasten    result = SL_RESULT_FEATURE_UNSUPPORTED;
10098db0b106cdce5f5216b1535492de7930ad738aedGlenn Kasten
10108db0b106cdce5f5216b1535492de7930ad738aedGlenn Kasten    XA_LEAVE_INTERFACE
10118db0b106cdce5f5216b1535492de7930ad738aedGlenn Kasten}
10128db0b106cdce5f5216b1535492de7930ad738aedGlenn Kasten
10138db0b106cdce5f5216b1535492de7930ad738aedGlenn Kasten
10148db0b106cdce5f5216b1535492de7930ad738aedGlenn Kastenstatic XAresult IEngine_CreateRadioDevice(XAEngineItf self, XAObjectItf *pDevice,
10158db0b106cdce5f5216b1535492de7930ad738aedGlenn Kasten        XAuint32 numInterfaces, const XAInterfaceID *pInterfaceIds,
10168db0b106cdce5f5216b1535492de7930ad738aedGlenn Kasten        const XAboolean *pInterfaceRequired)
10178db0b106cdce5f5216b1535492de7930ad738aedGlenn Kasten{
10188db0b106cdce5f5216b1535492de7930ad738aedGlenn Kasten    XA_ENTER_INTERFACE
10198db0b106cdce5f5216b1535492de7930ad738aedGlenn Kasten
1020bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten    //IXAEngine *thiz = (IXAEngine *) self;
10218db0b106cdce5f5216b1535492de7930ad738aedGlenn Kasten    result = SL_RESULT_FEATURE_UNSUPPORTED;
10228db0b106cdce5f5216b1535492de7930ad738aedGlenn Kasten
10238db0b106cdce5f5216b1535492de7930ad738aedGlenn Kasten    XA_LEAVE_INTERFACE
10248db0b106cdce5f5216b1535492de7930ad738aedGlenn Kasten}
10258db0b106cdce5f5216b1535492de7930ad738aedGlenn Kasten
10268db0b106cdce5f5216b1535492de7930ad738aedGlenn Kasten
10278db0b106cdce5f5216b1535492de7930ad738aedGlenn Kastenstatic XAresult IXAEngine_CreateLEDDevice(XAEngineItf self, XAObjectItf *pDevice, XAuint32 deviceID,
10288db0b106cdce5f5216b1535492de7930ad738aedGlenn Kasten        XAuint32 numInterfaces, const XAInterfaceID *pInterfaceIds,
10298db0b106cdce5f5216b1535492de7930ad738aedGlenn Kasten        const XAboolean *pInterfaceRequired)
10308db0b106cdce5f5216b1535492de7930ad738aedGlenn Kasten{
10318db0b106cdce5f5216b1535492de7930ad738aedGlenn Kasten    // forward to OpenSL ES
10328db0b106cdce5f5216b1535492de7930ad738aedGlenn Kasten    return IEngine_CreateLEDDevice(&((CEngine *) ((IXAEngine *) self)->mThis)->mEngine.mItf,
10338db0b106cdce5f5216b1535492de7930ad738aedGlenn Kasten            (SLObjectItf *) pDevice, deviceID, numInterfaces, (const SLInterfaceID *) pInterfaceIds,
10348db0b106cdce5f5216b1535492de7930ad738aedGlenn Kasten            (const SLboolean *) pInterfaceRequired);
10358db0b106cdce5f5216b1535492de7930ad738aedGlenn Kasten}
10368db0b106cdce5f5216b1535492de7930ad738aedGlenn Kasten
10378db0b106cdce5f5216b1535492de7930ad738aedGlenn Kasten
10388db0b106cdce5f5216b1535492de7930ad738aedGlenn Kastenstatic XAresult IXAEngine_CreateVibraDevice(XAEngineItf self, XAObjectItf *pDevice,
10398db0b106cdce5f5216b1535492de7930ad738aedGlenn Kasten        XAuint32 deviceID, XAuint32 numInterfaces, const XAInterfaceID *pInterfaceIds,
10408db0b106cdce5f5216b1535492de7930ad738aedGlenn Kasten        const XAboolean *pInterfaceRequired)
10418db0b106cdce5f5216b1535492de7930ad738aedGlenn Kasten{
10428db0b106cdce5f5216b1535492de7930ad738aedGlenn Kasten    // forward to OpenSL ES
10438db0b106cdce5f5216b1535492de7930ad738aedGlenn Kasten    return IEngine_CreateVibraDevice(&((CEngine *) ((IXAEngine *) self)->mThis)->mEngine.mItf,
10448db0b106cdce5f5216b1535492de7930ad738aedGlenn Kasten            (SLObjectItf *) pDevice, deviceID, numInterfaces, (const SLInterfaceID *) pInterfaceIds,
10458db0b106cdce5f5216b1535492de7930ad738aedGlenn Kasten            (const SLboolean *) pInterfaceRequired);
10468db0b106cdce5f5216b1535492de7930ad738aedGlenn Kasten}
10478db0b106cdce5f5216b1535492de7930ad738aedGlenn Kasten
10488db0b106cdce5f5216b1535492de7930ad738aedGlenn Kasten
10498db0b106cdce5f5216b1535492de7930ad738aedGlenn Kastenstatic XAresult IEngine_CreateMediaPlayer(XAEngineItf self, XAObjectItf *pPlayer,
10508db0b106cdce5f5216b1535492de7930ad738aedGlenn Kasten        XADataSource *pDataSrc, XADataSource *pBankSrc, XADataSink *pAudioSnk,
10518db0b106cdce5f5216b1535492de7930ad738aedGlenn Kasten        XADataSink *pImageVideoSnk, XADataSink *pVibra, XADataSink *pLEDArray,
10528db0b106cdce5f5216b1535492de7930ad738aedGlenn Kasten        XAuint32 numInterfaces, const XAInterfaceID *pInterfaceIds,
10538db0b106cdce5f5216b1535492de7930ad738aedGlenn Kasten        const XAboolean *pInterfaceRequired)
10548db0b106cdce5f5216b1535492de7930ad738aedGlenn Kasten{
10558db0b106cdce5f5216b1535492de7930ad738aedGlenn Kasten    XA_ENTER_INTERFACE
10568db0b106cdce5f5216b1535492de7930ad738aedGlenn Kasten
10578b8875067dd02b79361abb00c5d65b02a8ae72b0Glenn Kasten    if (NULL == pPlayer) {
10588b8875067dd02b79361abb00c5d65b02a8ae72b0Glenn Kasten        result = XA_RESULT_PARAMETER_INVALID;
10598b8875067dd02b79361abb00c5d65b02a8ae72b0Glenn Kasten    } else {
10608b8875067dd02b79361abb00c5d65b02a8ae72b0Glenn Kasten        *pPlayer = NULL;
10618b8875067dd02b79361abb00c5d65b02a8ae72b0Glenn Kasten        unsigned exposedMask;
10628b8875067dd02b79361abb00c5d65b02a8ae72b0Glenn Kasten        const ClassTable *pCMediaPlayer_class = objectIDtoClass(XA_OBJECTID_MEDIAPLAYER);
10638b8875067dd02b79361abb00c5d65b02a8ae72b0Glenn Kasten        assert(NULL != pCMediaPlayer_class);
10648b8875067dd02b79361abb00c5d65b02a8ae72b0Glenn Kasten        result = checkInterfaces(pCMediaPlayer_class, numInterfaces,
106505c7b2d09d54b9260ff7f3f5e491f38d0097c406Glenn Kasten                (const SLInterfaceID *) pInterfaceIds, pInterfaceRequired, &exposedMask, NULL);
10668b8875067dd02b79361abb00c5d65b02a8ae72b0Glenn Kasten        if (XA_RESULT_SUCCESS == result) {
10678b8875067dd02b79361abb00c5d65b02a8ae72b0Glenn Kasten
10688b8875067dd02b79361abb00c5d65b02a8ae72b0Glenn Kasten            // Construct our new MediaPlayer instance
1069bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten            CMediaPlayer *thiz = (CMediaPlayer *) construct(pCMediaPlayer_class, exposedMask,
10708b8875067dd02b79361abb00c5d65b02a8ae72b0Glenn Kasten                    &((CEngine *) ((IXAEngine *) self)->mThis)->mEngine.mItf);
1071bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten            if (NULL == thiz) {
10728b8875067dd02b79361abb00c5d65b02a8ae72b0Glenn Kasten                result = XA_RESULT_MEMORY_FAILURE;
10738b8875067dd02b79361abb00c5d65b02a8ae72b0Glenn Kasten            } else {
10748b8875067dd02b79361abb00c5d65b02a8ae72b0Glenn Kasten
10758b8875067dd02b79361abb00c5d65b02a8ae72b0Glenn Kasten                do {
10768b8875067dd02b79361abb00c5d65b02a8ae72b0Glenn Kasten
10778b8875067dd02b79361abb00c5d65b02a8ae72b0Glenn Kasten                    // Initialize private fields not associated with an interface
10788b8875067dd02b79361abb00c5d65b02a8ae72b0Glenn Kasten
1079d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi                    // Default data source in case of failure in checkDataSource
1080d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi                    thiz->mDataSource.mLocator.mLocatorType = SL_DATALOCATOR_NULL;
1081d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi                    thiz->mDataSource.mFormat.mFormatType = XA_DATAFORMAT_NULL;
1082d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi
1083d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi                    // Default andio and image sink in case of failure in checkDataSink
1084d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi                    thiz->mAudioSink.mLocator.mLocatorType = XA_DATALOCATOR_NULL;
1085d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi                    thiz->mAudioSink.mFormat.mFormatType = XA_DATAFORMAT_NULL;
1086d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi                    thiz->mImageVideoSink.mLocator.mLocatorType = XA_DATALOCATOR_NULL;
1087d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi                    thiz->mImageVideoSink.mFormat.mFormatType = XA_DATAFORMAT_NULL;
1088d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi
1089d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi                    // More default values, in case destructor needs to be called early
109099b927751677abfb60a388d65dfeed1fed1db12cGlenn Kasten                    thiz->mNumChannels = UNKNOWN_NUMCHANNELS;
1091d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi
109213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi#ifdef ANDROID
1093485a038f9f0f898227b8ab4218e94c5d56b6ed0bGlenn Kasten                    // placement new (explicit constructor)
109413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                    // FIXME unnecessary once those fields are encapsulated in one class, rather
109513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                    //   than a structure
109668d56b8ebaf60184a3aef988e3d2b09ed8b88c05Jean-Michel Trivi                    (void) new (&thiz->mAVPlayer) android::sp<android::GenericPlayer>();
1097485a038f9f0f898227b8ab4218e94c5d56b6ed0bGlenn Kasten                    (void) new (&thiz->mCallbackProtector)
1098485a038f9f0f898227b8ab4218e94c5d56b6ed0bGlenn Kasten                            android::sp<android::CallbackProtector>();
109972042d4448cee63528c619537321ba73944c6382Glenn Kasten                    // Android-specific POD fields are initialized in android_Player_create,
110072042d4448cee63528c619537321ba73944c6382Glenn Kasten                    // and assume calloc or memset 0 during allocation
110113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi#endif
11028b8875067dd02b79361abb00c5d65b02a8ae72b0Glenn Kasten
11038b8875067dd02b79361abb00c5d65b02a8ae72b0Glenn Kasten                    // Check the source and sink parameters against generic constraints
11048b8875067dd02b79361abb00c5d65b02a8ae72b0Glenn Kasten
110594a37e8117fb72790882dfb815f99e2365754c74Glenn Kasten                    result = checkDataSource("pDataSrc", (const SLDataSource *) pDataSrc,
1106bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten                            &thiz->mDataSource, DATALOCATOR_MASK_URI
110794a37e8117fb72790882dfb815f99e2365754c74Glenn Kasten#ifdef ANDROID
110894a37e8117fb72790882dfb815f99e2365754c74Glenn Kasten                            | DATALOCATOR_MASK_ANDROIDFD
1109eae4df541ba1d46f65d37e959baf2127aa632c93Jean-Michel Trivi                            | DATALOCATOR_MASK_ANDROIDBUFFERQUEUE
111094a37e8117fb72790882dfb815f99e2365754c74Glenn Kasten#endif
111194a37e8117fb72790882dfb815f99e2365754c74Glenn Kasten                            , DATAFORMAT_MASK_MIME);
11128b8875067dd02b79361abb00c5d65b02a8ae72b0Glenn Kasten                    if (XA_RESULT_SUCCESS != result) {
11138b8875067dd02b79361abb00c5d65b02a8ae72b0Glenn Kasten                        break;
11148b8875067dd02b79361abb00c5d65b02a8ae72b0Glenn Kasten                    }
11158b8875067dd02b79361abb00c5d65b02a8ae72b0Glenn Kasten
111694a37e8117fb72790882dfb815f99e2365754c74Glenn Kasten                    result = checkDataSource("pBankSrc", (const SLDataSource *) pBankSrc,
1117bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten                            &thiz->mBankSource, DATALOCATOR_MASK_NULL | DATALOCATOR_MASK_URI |
111894a37e8117fb72790882dfb815f99e2365754c74Glenn Kasten                            DATALOCATOR_MASK_ADDRESS, DATAFORMAT_MASK_NULL);
11198b8875067dd02b79361abb00c5d65b02a8ae72b0Glenn Kasten                    if (XA_RESULT_SUCCESS != result) {
11208b8875067dd02b79361abb00c5d65b02a8ae72b0Glenn Kasten                        break;
11218b8875067dd02b79361abb00c5d65b02a8ae72b0Glenn Kasten                    }
11228b8875067dd02b79361abb00c5d65b02a8ae72b0Glenn Kasten
112394a37e8117fb72790882dfb815f99e2365754c74Glenn Kasten                    result = checkDataSink("pAudioSnk", (const SLDataSink *) pAudioSnk,
1124bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten                            &thiz->mAudioSink, DATALOCATOR_MASK_OUTPUTMIX, DATAFORMAT_MASK_NULL);
11258b8875067dd02b79361abb00c5d65b02a8ae72b0Glenn Kasten                    if (XA_RESULT_SUCCESS != result) {
11268b8875067dd02b79361abb00c5d65b02a8ae72b0Glenn Kasten                        break;
11278b8875067dd02b79361abb00c5d65b02a8ae72b0Glenn Kasten                    }
11288b8875067dd02b79361abb00c5d65b02a8ae72b0Glenn Kasten
112994a37e8117fb72790882dfb815f99e2365754c74Glenn Kasten                    result = checkDataSink("pImageVideoSnk", (const SLDataSink *) pImageVideoSnk,
11302bb3547351a40ad394b09babec4d2fcc233bfe2eGlenn Kasten                            &thiz->mImageVideoSink,
11312bb3547351a40ad394b09babec4d2fcc233bfe2eGlenn Kasten                            DATALOCATOR_MASK_NULL | DATALOCATOR_MASK_NATIVEDISPLAY,
113294a37e8117fb72790882dfb815f99e2365754c74Glenn Kasten                            DATAFORMAT_MASK_NULL);
11338b8875067dd02b79361abb00c5d65b02a8ae72b0Glenn Kasten                    if (XA_RESULT_SUCCESS != result) {
11348b8875067dd02b79361abb00c5d65b02a8ae72b0Glenn Kasten                        break;
11358b8875067dd02b79361abb00c5d65b02a8ae72b0Glenn Kasten                    }
11368b8875067dd02b79361abb00c5d65b02a8ae72b0Glenn Kasten
1137bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten                    result = checkDataSink("pVibra", (const SLDataSink *) pVibra, &thiz->mVibraSink,
113894a37e8117fb72790882dfb815f99e2365754c74Glenn Kasten                            DATALOCATOR_MASK_NULL | DATALOCATOR_MASK_IODEVICE,
113994a37e8117fb72790882dfb815f99e2365754c74Glenn Kasten                            DATAFORMAT_MASK_NULL);
11408b8875067dd02b79361abb00c5d65b02a8ae72b0Glenn Kasten                    if (XA_RESULT_SUCCESS != result) {
11418b8875067dd02b79361abb00c5d65b02a8ae72b0Glenn Kasten                        break;
11428b8875067dd02b79361abb00c5d65b02a8ae72b0Glenn Kasten                    }
11438b8875067dd02b79361abb00c5d65b02a8ae72b0Glenn Kasten
114494a37e8117fb72790882dfb815f99e2365754c74Glenn Kasten                    result = checkDataSink("pLEDArray", (const SLDataSink *) pLEDArray,
1145bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten                            &thiz->mLEDArraySink, DATALOCATOR_MASK_NULL | DATALOCATOR_MASK_IODEVICE,
114694a37e8117fb72790882dfb815f99e2365754c74Glenn Kasten                            DATAFORMAT_MASK_NULL);
11478b8875067dd02b79361abb00c5d65b02a8ae72b0Glenn Kasten                    if (XA_RESULT_SUCCESS != result) {
11488b8875067dd02b79361abb00c5d65b02a8ae72b0Glenn Kasten                        break;
11498b8875067dd02b79361abb00c5d65b02a8ae72b0Glenn Kasten                    }
11508b8875067dd02b79361abb00c5d65b02a8ae72b0Glenn Kasten
11518b8875067dd02b79361abb00c5d65b02a8ae72b0Glenn Kasten                    // Unsafe to ever refer to application pointers again
11528b8875067dd02b79361abb00c5d65b02a8ae72b0Glenn Kasten                    pDataSrc = NULL;
11538b8875067dd02b79361abb00c5d65b02a8ae72b0Glenn Kasten                    pBankSrc = NULL;
11548b8875067dd02b79361abb00c5d65b02a8ae72b0Glenn Kasten                    pAudioSnk = NULL;
11558b8875067dd02b79361abb00c5d65b02a8ae72b0Glenn Kasten                    pImageVideoSnk = NULL;
11568b8875067dd02b79361abb00c5d65b02a8ae72b0Glenn Kasten                    pVibra = NULL;
11578b8875067dd02b79361abb00c5d65b02a8ae72b0Glenn Kasten                    pLEDArray = NULL;
11588b8875067dd02b79361abb00c5d65b02a8ae72b0Glenn Kasten
11598b8875067dd02b79361abb00c5d65b02a8ae72b0Glenn Kasten                    // Check that the requested interfaces are compatible with the data source
1160d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi                    // FIXME implement
11618b8875067dd02b79361abb00c5d65b02a8ae72b0Glenn Kasten
11628b8875067dd02b79361abb00c5d65b02a8ae72b0Glenn Kasten                    // check the source and sink parameters against platform support
11638b8875067dd02b79361abb00c5d65b02a8ae72b0Glenn Kasten#ifdef ANDROID
1164d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi                    result = android_Player_checkSourceSink(thiz);
11658b8875067dd02b79361abb00c5d65b02a8ae72b0Glenn Kasten                    if (XA_RESULT_SUCCESS != result) {
11668b8875067dd02b79361abb00c5d65b02a8ae72b0Glenn Kasten                        break;
11678b8875067dd02b79361abb00c5d65b02a8ae72b0Glenn Kasten                    }
11688b8875067dd02b79361abb00c5d65b02a8ae72b0Glenn Kasten#endif
11698b8875067dd02b79361abb00c5d65b02a8ae72b0Glenn Kasten
1170f6c0c2346b2ca1ac01c2d68a2cd2a7f1d9ff3427Glenn Kasten#ifdef ANDROID
1171d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi                    // AndroidBufferQueue-specific initialization
1172d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi                    if (XA_DATALOCATOR_ANDROIDBUFFERQUEUE ==
1173d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi                            thiz->mDataSource.mLocator.mLocatorType) {
1174d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi                        XAuint16 nbBuffers = (XAuint16) thiz->mDataSource.mLocator.mABQ.numBuffers;
1175d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi
1176d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi                        // Avoid possible integer overflow during multiplication; this arbitrary
1177d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi                        // maximum is big enough to not interfere with real applications, but
1178d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi                        // small enough to not overflow.
1179d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi                        if (nbBuffers >= 256) {
1180d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi                            result = SL_RESULT_MEMORY_FAILURE;
1181d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi                            break;
1182d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi                        }
118370c49ae2867094072a4365423417ea452bf82231Jean-Michel Trivi
118470c49ae2867094072a4365423417ea452bf82231Jean-Michel Trivi                        // initialize ABQ buffer type
118570c49ae2867094072a4365423417ea452bf82231Jean-Michel Trivi                        // assert below has been checked in android_audioPlayer_checkSourceSink
118670c49ae2867094072a4365423417ea452bf82231Jean-Michel Trivi                        assert(XA_DATAFORMAT_MIME == thiz->mDataSource.mFormat.mFormatType);
118770c49ae2867094072a4365423417ea452bf82231Jean-Michel Trivi                        if (XA_CONTAINERTYPE_MPEG_TS ==
118870c49ae2867094072a4365423417ea452bf82231Jean-Michel Trivi                                thiz->mDataSource.mFormat.mMIME.containerType) {
118970c49ae2867094072a4365423417ea452bf82231Jean-Michel Trivi                            thiz->mAndroidBufferQueue.mBufferType = kAndroidBufferTypeMpeg2Ts;
119037dc2fccf3f122b79ebd554de209d0a3c94ae161Jean-Michel Trivi
119137dc2fccf3f122b79ebd554de209d0a3c94ae161Jean-Michel Trivi                            // Set the container type for the StreamInformation interface
119237dc2fccf3f122b79ebd554de209d0a3c94ae161Jean-Michel Trivi                            XAMediaContainerInformation *containerInfo =
119337dc2fccf3f122b79ebd554de209d0a3c94ae161Jean-Michel Trivi                                    (XAMediaContainerInformation*)
119437dc2fccf3f122b79ebd554de209d0a3c94ae161Jean-Michel Trivi                                        // always storing container info at index 0, as per spec
119537dc2fccf3f122b79ebd554de209d0a3c94ae161Jean-Michel Trivi                                        &(thiz->mStreamInfo.mStreamInfoTable.itemAt(0).
119637dc2fccf3f122b79ebd554de209d0a3c94ae161Jean-Michel Trivi                                                containerInfo);
119737dc2fccf3f122b79ebd554de209d0a3c94ae161Jean-Michel Trivi                            containerInfo->containerType = XA_CONTAINERTYPE_MPEG_TS;
119837dc2fccf3f122b79ebd554de209d0a3c94ae161Jean-Michel Trivi                            // there are no streams at this stage
119937dc2fccf3f122b79ebd554de209d0a3c94ae161Jean-Michel Trivi                            containerInfo->numStreams = 0;
120037dc2fccf3f122b79ebd554de209d0a3c94ae161Jean-Michel Trivi
120170c49ae2867094072a4365423417ea452bf82231Jean-Michel Trivi                        } else {
120270c49ae2867094072a4365423417ea452bf82231Jean-Michel Trivi                            thiz->mAndroidBufferQueue.mBufferType = kAndroidBufferTypeInvalid;
120370c49ae2867094072a4365423417ea452bf82231Jean-Michel Trivi                            SL_LOGE("Invalid buffer type in Android Buffer Queue");
120470c49ae2867094072a4365423417ea452bf82231Jean-Michel Trivi                            result = SL_RESULT_CONTENT_UNSUPPORTED;
120570c49ae2867094072a4365423417ea452bf82231Jean-Michel Trivi                        }
120670c49ae2867094072a4365423417ea452bf82231Jean-Michel Trivi
120770c49ae2867094072a4365423417ea452bf82231Jean-Michel Trivi                        // initialize ABQ memory
1208d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi                        thiz->mAndroidBufferQueue.mBufferArray = (AdvancedBufferHeader *)
1209d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi                                    malloc( (nbBuffers + 1) * sizeof(AdvancedBufferHeader));
1210d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi                        if (NULL == thiz->mAndroidBufferQueue.mBufferArray) {
1211d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi                            result = SL_RESULT_MEMORY_FAILURE;
1212d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi                            break;
1213d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi                        } else {
1214d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi                            thiz->mAndroidBufferQueue.mFront =
1215d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi                                    thiz->mAndroidBufferQueue.mBufferArray;
1216d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi                            thiz->mAndroidBufferQueue.mRear =
1217d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi                                    thiz->mAndroidBufferQueue.mBufferArray;
1218d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi                        }
1219d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi
1220d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi                        thiz->mAndroidBufferQueue.mNumBuffers = nbBuffers;
1221d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi
1222d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi                    }
1223f6c0c2346b2ca1ac01c2d68a2cd2a7f1d9ff3427Glenn Kasten#endif
1224d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi
1225d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi                    // used to store the data source of our audio player
1226d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi                    thiz->mDynamicSource.mDataSource = &thiz->mDataSource.u.mSource;
1227d158d31a6bbb06426b71c3d097b7768bc3fb79a3Jean-Michel Trivi
12288b8875067dd02b79361abb00c5d65b02a8ae72b0Glenn Kasten                    // platform-specific initialization
12298b8875067dd02b79361abb00c5d65b02a8ae72b0Glenn Kasten#ifdef ANDROID
1230bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten                    android_Player_create(thiz);
12318b8875067dd02b79361abb00c5d65b02a8ae72b0Glenn Kasten#endif
12328b8875067dd02b79361abb00c5d65b02a8ae72b0Glenn Kasten
12338b8875067dd02b79361abb00c5d65b02a8ae72b0Glenn Kasten                } while (0);
12348b8875067dd02b79361abb00c5d65b02a8ae72b0Glenn Kasten
12358b8875067dd02b79361abb00c5d65b02a8ae72b0Glenn Kasten                if (XA_RESULT_SUCCESS != result) {
1236bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten                    IObject_Destroy(&thiz->mObject.mItf);
12378b8875067dd02b79361abb00c5d65b02a8ae72b0Glenn Kasten                } else {
1238bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten                    IObject_Publish(&thiz->mObject);
12398b8875067dd02b79361abb00c5d65b02a8ae72b0Glenn Kasten                    // return the new media player object
1240bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten                    *pPlayer = (XAObjectItf) &thiz->mObject.mItf;
12418b8875067dd02b79361abb00c5d65b02a8ae72b0Glenn Kasten                }
12428b8875067dd02b79361abb00c5d65b02a8ae72b0Glenn Kasten
12438b8875067dd02b79361abb00c5d65b02a8ae72b0Glenn Kasten            }
12448b8875067dd02b79361abb00c5d65b02a8ae72b0Glenn Kasten        }
12458b8875067dd02b79361abb00c5d65b02a8ae72b0Glenn Kasten
12468b8875067dd02b79361abb00c5d65b02a8ae72b0Glenn Kasten    }
12478db0b106cdce5f5216b1535492de7930ad738aedGlenn Kasten
12488db0b106cdce5f5216b1535492de7930ad738aedGlenn Kasten    XA_LEAVE_INTERFACE
12498db0b106cdce5f5216b1535492de7930ad738aedGlenn Kasten}
12508db0b106cdce5f5216b1535492de7930ad738aedGlenn Kasten
12518db0b106cdce5f5216b1535492de7930ad738aedGlenn Kasten
12528db0b106cdce5f5216b1535492de7930ad738aedGlenn Kastenstatic XAresult IEngine_CreateMediaRecorder(XAEngineItf self, XAObjectItf *pRecorder,
12538db0b106cdce5f5216b1535492de7930ad738aedGlenn Kasten        XADataSource *pAudioSrc, XADataSource *pImageVideoSrc,
12548db0b106cdce5f5216b1535492de7930ad738aedGlenn Kasten        XADataSink *pDataSnk, XAuint32 numInterfaces, const XAInterfaceID *pInterfaceIds,
12558db0b106cdce5f5216b1535492de7930ad738aedGlenn Kasten        const XAboolean *pInterfaceRequired)
12568db0b106cdce5f5216b1535492de7930ad738aedGlenn Kasten{
12578db0b106cdce5f5216b1535492de7930ad738aedGlenn Kasten    XA_ENTER_INTERFACE
12588db0b106cdce5f5216b1535492de7930ad738aedGlenn Kasten
1259bcc5c7225e3b7a1dbf2e9e830987f69167acf06fGlenn Kasten    //IXAEngine *thiz = (IXAEngine *) self;
12608db0b106cdce5f5216b1535492de7930ad738aedGlenn Kasten    result = SL_RESULT_FEATURE_UNSUPPORTED;
12618db0b106cdce5f5216b1535492de7930ad738aedGlenn Kasten
126294a37e8117fb72790882dfb815f99e2365754c74Glenn Kasten#if 0
126394a37e8117fb72790882dfb815f99e2365754c74Glenn Kasten    "pAudioSrc", pAudioSrc,
126494a37e8117fb72790882dfb815f99e2365754c74Glenn Kasten    "pImageVideoSrc", pImageVideoSrc,
126594a37e8117fb72790882dfb815f99e2365754c74Glenn Kasten    "pDataSink", pDataSnk,
126694a37e8117fb72790882dfb815f99e2365754c74Glenn Kasten#endif
126794a37e8117fb72790882dfb815f99e2365754c74Glenn Kasten
12688db0b106cdce5f5216b1535492de7930ad738aedGlenn Kas