sles.c revision 6e7e174807fc639c49125ced8962aa369370fbf0
1/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/* OpenSL ES private and global functions not associated with an interface or class */
18
19#include "sles_allinclusive.h"
20
21
22/** \brief Return true if the specified interface exists and has been initialized for this object.
23 *  Returns false if the class does not support this kind of interface, or the class supports the
24 *  interface but this particular object has not had the interface exposed at object creation time
25 *  or by DynamicInterface::AddInterface. Note that the return value is not affected by whether
26 *  the application has requested access to the interface with Object::GetInterface. Assumes on
27 *  entry that the object is locked for either shared or exclusive access.
28 */
29
30bool IsInterfaceInitialized(IObject *thiz, unsigned MPH)
31{
32    assert(NULL != thiz);
33    assert( /* (MPH_MIN <= MPH) && */ (MPH < (unsigned) MPH_MAX));
34    const ClassTable *clazz = thiz->mClass;
35    assert(NULL != clazz);
36    int index;
37    if (0 > (index = clazz->mMPH_to_index[MPH])) {
38        return false;
39    }
40    assert(MAX_INDEX >= clazz->mInterfaceCount);
41    assert(clazz->mInterfaceCount > (unsigned) index);
42    switch (thiz->mInterfaceStates[index]) {
43    case INTERFACE_EXPOSED:
44    case INTERFACE_ADDED:
45        return true;
46    default:
47        return false;
48    }
49}
50
51
52/** \brief Map an IObject to it's "object ID" (which is really a class ID) */
53
54SLuint32 IObjectToObjectID(IObject *thiz)
55{
56    assert(NULL != thiz);
57    // Note this returns the OpenSL ES object ID in preference to the OpenMAX AL if both available
58    const ClassTable *clazz = thiz->mClass;
59    assert(NULL != clazz);
60    SLuint32 id = clazz->mSLObjectID;
61    if (!id)
62        id = clazz->mXAObjectID;
63    return id;
64}
65
66
67/** \brief Acquire a strong reference to an object.
68 *  Check that object has the specified "object ID" (which is really a class ID) and is in the
69 *  realized state.  If so, then acquire a strong reference to it and return true.
70 *  Otherwise return false.
71 */
72
73SLresult AcquireStrongRef(IObject *object, SLuint32 expectedObjectID)
74{
75    if (NULL == object) {
76        return SL_RESULT_PARAMETER_INVALID;
77    }
78    // NTH additional validity checks on address here
79    SLresult result;
80    object_lock_exclusive(object);
81    SLuint32 actualObjectID = IObjectToObjectID(object);
82    if (expectedObjectID != actualObjectID) {
83        SL_LOGE("object %p has object ID %u but expected %u", object, actualObjectID,
84            expectedObjectID);
85        result = SL_RESULT_PARAMETER_INVALID;
86    } else if (SL_OBJECT_STATE_REALIZED != object->mState) {
87        SL_LOGE("object %p with object ID %u is not realized", object, actualObjectID);
88        result = SL_RESULT_PRECONDITIONS_VIOLATED;
89    } else {
90        ++object->mStrongRefCount;
91        result = SL_RESULT_SUCCESS;
92    }
93    object_unlock_exclusive(object);
94    return result;
95}
96
97
98/** \brief Release a strong reference to an object.
99 *  Entry condition: the object is locked.
100 *  Exit condition: the object is unlocked.
101 *  Finishes the destroy if needed.
102 */
103
104void ReleaseStrongRefAndUnlockExclusive(IObject *object)
105{
106#ifdef USE_DEBUG
107    assert(pthread_equal(pthread_self(), object->mOwner));
108#endif
109    assert(0 < object->mStrongRefCount);
110    if ((0 == --object->mStrongRefCount) && (SL_OBJECT_STATE_DESTROYING == object->mState)) {
111        // FIXME do the destroy here - merge with IDestroy
112        // but can't do this until we move Destroy to the sync thread
113        // as Destroy is now a blocking operation, and to avoid a race
114    } else {
115        object_unlock_exclusive(object);
116    }
117}
118
119
120/** \brief Release a strong reference to an object.
121 *  Entry condition: the object is unlocked.
122 *  Exit condition: the object is unlocked.
123 *  Finishes the destroy if needed.
124 */
125
126void ReleaseStrongRef(IObject *object)
127{
128    assert(NULL != object);
129    object_lock_exclusive(object);
130    ReleaseStrongRefAndUnlockExclusive(object);
131}
132
133
134/** \brief Convert POSIX pthread error code to OpenSL ES result code */
135
136SLresult err_to_result(int err)
137{
138    if (EAGAIN == err || ENOMEM == err) {
139        return SL_RESULT_RESOURCE_ERROR;
140    }
141    if (0 != err) {
142        return SL_RESULT_INTERNAL_ERROR;
143    }
144    return SL_RESULT_SUCCESS;
145}
146
147
148/** \brief Check the interface IDs passed into a Create operation */
149
150SLresult checkInterfaces(const ClassTable *clazz, SLuint32 numInterfaces,
151    const SLInterfaceID *pInterfaceIds, const SLboolean *pInterfaceRequired, unsigned *pExposedMask)
152{
153    assert(NULL != clazz && NULL != pExposedMask);
154    // Initially no interfaces are exposed
155    unsigned exposedMask = 0;
156    const struct iid_vtable *interfaces = clazz->mInterfaces;
157    SLuint32 interfaceCount = clazz->mInterfaceCount;
158    SLuint32 i;
159    // Expose all implicit interfaces
160    for (i = 0; i < interfaceCount; ++i) {
161        switch (interfaces[i].mInterface) {
162        case INTERFACE_IMPLICIT:
163        case INTERFACE_IMPLICIT_PREREALIZE:
164            // there must be an initialization hook present
165            if (NULL != MPH_init_table[interfaces[i].mMPH].mInit) {
166                exposedMask |= 1 << i;
167            }
168            break;
169        case INTERFACE_EXPLICIT:
170        case INTERFACE_DYNAMIC:
171        case INTERFACE_UNAVAILABLE:
172        case INTERFACE_EXPLICIT_PREREALIZE:
173            break;
174        default:
175            assert(false);
176            break;
177        }
178    }
179    if (0 < numInterfaces) {
180        if (NULL == pInterfaceIds || NULL == pInterfaceRequired) {
181            return SL_RESULT_PARAMETER_INVALID;
182        }
183        bool anyRequiredButUnsupported = false;
184        // Loop for each requested interface
185        for (i = 0; i < numInterfaces; ++i) {
186            SLInterfaceID iid = pInterfaceIds[i];
187            if (NULL == iid) {
188                return SL_RESULT_PARAMETER_INVALID;
189            }
190            int MPH, index;
191            if ((0 > (MPH = IID_to_MPH(iid))) ||
192                    // there must be an initialization hook present
193                    (NULL == MPH_init_table[MPH].mInit) ||
194                    (0 > (index = clazz->mMPH_to_index[MPH])) ||
195                    (INTERFACE_UNAVAILABLE == interfaces[index].mInterface)) {
196                // Here if interface was not found, or is not available for this object type
197                if (pInterfaceRequired[i]) {
198                    // Application said it required the interface, so give up
199                    SL_LOGE("class %s interface %u required but unavailable MPH=%d",
200                            clazz->mName, i, MPH);
201                    anyRequiredButUnsupported = true;
202                }
203                // Application said it didn't really need the interface, so ignore with warning
204                SL_LOGW("class %s interface %u requested but unavailable MPH=%d",
205                        clazz->mName, i, MPH);
206                continue;
207            }
208            // The requested interface was both found and available, so expose it
209            exposedMask |= (1 << index);
210            // Note that we ignore duplicate requests, including equal and aliased IDs
211        }
212        if (anyRequiredButUnsupported) {
213            return SL_RESULT_FEATURE_UNSUPPORTED;
214        }
215    }
216    *pExposedMask = exposedMask;
217    return SL_RESULT_SUCCESS;
218}
219
220
221/* Interface initialization hooks */
222
223extern void
224    I3DCommit_init(void *),
225    I3DDoppler_init(void *),
226    I3DGrouping_init(void *),
227    I3DLocation_init(void *),
228    I3DMacroscopic_init(void *),
229    I3DSource_init(void *),
230    IAndroidConfiguration_init(void *),
231    IAndroidEffect_init(void *),
232    IAndroidEffectCapabilities_init(void *),
233    IAndroidEffectSend_init(void *),
234    IAndroidBufferQueue_init(void *),
235    IAudioDecoderCapabilities_init(void *),
236    IAudioEncoder_init(void *),
237    IAudioEncoderCapabilities_init(void *),
238    IAudioIODeviceCapabilities_init(void *),
239    IBassBoost_init(void *),
240    IBufferQueue_init(void *),
241    IDeviceVolume_init(void *),
242    IDynamicInterfaceManagement_init(void *),
243    IDynamicSource_init(void *),
244    IEffectSend_init(void *),
245    IEngine_init(void *),
246    IEngineCapabilities_init(void *),
247    IEnvironmentalReverb_init(void *),
248    IEqualizer_init(void *),
249    ILEDArray_init(void *),
250    IMIDIMessage_init(void *),
251    IMIDIMuteSolo_init(void *),
252    IMIDITempo_init(void *),
253    IMIDITime_init(void *),
254    IMetadataExtraction_init(void *),
255    IMetadataTraversal_init(void *),
256    IMuteSolo_init(void *),
257    IObject_init(void *),
258    IOutputMix_init(void *),
259    IOutputMixExt_init(void *),
260    IPitch_init(void *),
261    IPlay_init(void *),
262    IPlaybackRate_init(void *),
263    IPrefetchStatus_init(void *),
264    IPresetReverb_init(void *),
265    IRatePitch_init(void *),
266    IRecord_init(void *),
267    ISeek_init(void *),
268    IThreadSync_init(void *),
269    IVibra_init(void *),
270    IVirtualizer_init(void *),
271    IVisualization_init(void *),
272    IVolume_init(void *);
273
274extern void
275    I3DGrouping_deinit(void *),
276    IAndroidEffect_deinit(void *),
277    IAndroidEffectCapabilities_deinit(void *),
278    IAndroidBufferQueue_deinit(void *),
279    IBassBoost_deinit(void *),
280    IBufferQueue_deinit(void *),
281    IEngine_deinit(void *),
282    IEnvironmentalReverb_deinit(void *),
283    IEqualizer_deinit(void *),
284    IObject_deinit(void *),
285    IPresetReverb_deinit(void *),
286    IThreadSync_deinit(void *),
287    IVirtualizer_deinit(void *);
288
289extern bool
290    IAndroidEffectCapabilities_Expose(void *),
291    IBassBoost_Expose(void *),
292    IEnvironmentalReverb_Expose(void *),
293    IEqualizer_Expose(void *),
294    IPresetReverb_Expose(void *),
295    IVirtualizer_Expose(void *);
296
297extern void
298    IXAEngine_init(void *),
299    IStreamInformation_init(void*),
300    IVideoDecoderCapabilities_init(void *);
301
302extern void
303    IXAEngine_deinit(void *),
304    IStreamInformation_deinit(void *),
305    IVideoDecoderCapabilities_deinit(void *);
306
307extern bool
308    IVideoDecoderCapabilities_expose(void *);
309
310#if !(USE_PROFILES & USE_PROFILES_MUSIC)
311#define IDynamicSource_init         NULL
312#define IMetadataTraversal_init     NULL
313#define IVisualization_init         NULL
314#endif
315
316#if !(USE_PROFILES & USE_PROFILES_GAME)
317#define I3DCommit_init      NULL
318#define I3DDoppler_init     NULL
319#define I3DGrouping_init    NULL
320#define I3DLocation_init    NULL
321#define I3DMacroscopic_init NULL
322#define I3DSource_init      NULL
323#define IMIDIMessage_init   NULL
324#define IMIDIMuteSolo_init  NULL
325#define IMIDITempo_init     NULL
326#define IMIDITime_init      NULL
327#define IPitch_init         NULL
328#define IRatePitch_init     NULL
329#define I3DGrouping_deinit  NULL
330#endif
331
332#if !(USE_PROFILES & USE_PROFILES_BASE)
333#define IAudioDecoderCapabilities_init   NULL
334#define IAudioEncoderCapabilities_init   NULL
335#define IAudioEncoder_init               NULL
336#define IAudioIODeviceCapabilities_init  NULL
337#define IDeviceVolume_init               NULL
338#define IEngineCapabilities_init         NULL
339#define IThreadSync_init                 NULL
340#define IThreadSync_deinit               NULL
341#endif
342
343#if !(USE_PROFILES & USE_PROFILES_OPTIONAL)
344#define ILEDArray_init  NULL
345#define IVibra_init     NULL
346#endif
347
348#ifndef ANDROID
349#define IAndroidConfiguration_init        NULL
350#define IAndroidEffect_init               NULL
351#define IAndroidEffectCapabilities_init   NULL
352#define IAndroidEffectSend_init           NULL
353#define IAndroidEffect_deinit             NULL
354#define IAndroidEffectCapabilities_deinit NULL
355#define IAndroidEffectCapabilities_Expose NULL
356#define IAndroidBufferQueue_init          NULL
357#define IStreamInformation_init           NULL
358#define IAndroidBufferQueue_deinit        NULL
359#define IStreamInformation_deinit         NULL
360#endif
361
362#ifndef USE_OUTPUTMIXEXT
363#define IOutputMixExt_init  NULL
364#endif
365
366
367/*static*/ const struct MPH_init MPH_init_table[MPH_MAX] = {
368    { /* MPH_3DCOMMIT, */ I3DCommit_init, NULL, NULL, NULL, NULL },
369    { /* MPH_3DDOPPLER, */ I3DDoppler_init, NULL, NULL, NULL, NULL },
370    { /* MPH_3DGROUPING, */ I3DGrouping_init, NULL, I3DGrouping_deinit, NULL, NULL },
371    { /* MPH_3DLOCATION, */ I3DLocation_init, NULL, NULL, NULL, NULL },
372    { /* MPH_3DMACROSCOPIC, */ I3DMacroscopic_init, NULL, NULL, NULL, NULL },
373    { /* MPH_3DSOURCE, */ I3DSource_init, NULL, NULL, NULL, NULL },
374    { /* MPH_AUDIODECODERCAPABILITIES, */ IAudioDecoderCapabilities_init, NULL, NULL, NULL, NULL },
375    { /* MPH_AUDIOENCODER, */ IAudioEncoder_init, NULL, NULL, NULL, NULL },
376    { /* MPH_AUDIOENCODERCAPABILITIES, */ IAudioEncoderCapabilities_init, NULL, NULL, NULL, NULL },
377    { /* MPH_AUDIOIODEVICECAPABILITIES, */ IAudioIODeviceCapabilities_init, NULL, NULL, NULL,
378        NULL },
379    { /* MPH_BASSBOOST, */ IBassBoost_init, NULL, IBassBoost_deinit, IBassBoost_Expose, NULL },
380    { /* MPH_BUFFERQUEUE, */ IBufferQueue_init, NULL, IBufferQueue_deinit, NULL, NULL },
381    { /* MPH_DEVICEVOLUME, */ IDeviceVolume_init, NULL, NULL, NULL, NULL },
382    { /* MPH_DYNAMICINTERFACEMANAGEMENT, */ IDynamicInterfaceManagement_init, NULL, NULL, NULL,
383        NULL },
384    { /* MPH_DYNAMICSOURCE, */ IDynamicSource_init, NULL, NULL, NULL, NULL },
385    { /* MPH_EFFECTSEND, */ IEffectSend_init, NULL, NULL, NULL, NULL },
386    { /* MPH_ENGINE, */ IEngine_init, NULL, IEngine_deinit, NULL, NULL },
387    { /* MPH_ENGINECAPABILITIES, */ IEngineCapabilities_init, NULL, NULL, NULL, NULL },
388    { /* MPH_ENVIRONMENTALREVERB, */ IEnvironmentalReverb_init, NULL, IEnvironmentalReverb_deinit,
389        IEnvironmentalReverb_Expose, NULL },
390    { /* MPH_EQUALIZER, */ IEqualizer_init, NULL, IEqualizer_deinit, IEqualizer_Expose, NULL },
391    { /* MPH_LED, */ ILEDArray_init, NULL, NULL, NULL, NULL },
392    { /* MPH_METADATAEXTRACTION, */ IMetadataExtraction_init, NULL, NULL, NULL, NULL },
393    { /* MPH_METADATATRAVERSAL, */ IMetadataTraversal_init, NULL, NULL, NULL, NULL },
394    { /* MPH_MIDIMESSAGE, */ IMIDIMessage_init, NULL, NULL, NULL, NULL },
395    { /* MPH_MIDITIME, */ IMIDITime_init, NULL, NULL, NULL, NULL },
396    { /* MPH_MIDITEMPO, */ IMIDITempo_init, NULL, NULL, NULL, NULL },
397    { /* MPH_MIDIMUTESOLO, */ IMIDIMuteSolo_init, NULL, NULL, NULL, NULL },
398    { /* MPH_MUTESOLO, */ IMuteSolo_init, NULL, NULL, NULL, NULL },
399    { /* MPH_NULL, */ NULL, NULL, NULL, NULL, NULL },
400    { /* MPH_OBJECT, */ IObject_init, NULL, IObject_deinit, NULL, NULL },
401    { /* MPH_OUTPUTMIX, */ IOutputMix_init, NULL, NULL, NULL, NULL },
402    { /* MPH_PITCH, */ IPitch_init, NULL, NULL, NULL, NULL },
403    { /* MPH_PLAY, */ IPlay_init, NULL, NULL, NULL, NULL },
404    { /* MPH_PLAYBACKRATE, */ IPlaybackRate_init, NULL, NULL, NULL, NULL },
405    { /* MPH_PREFETCHSTATUS, */ IPrefetchStatus_init, NULL, NULL, NULL, NULL },
406    { /* MPH_PRESETREVERB, */ IPresetReverb_init, NULL, IPresetReverb_deinit,
407        IPresetReverb_Expose, NULL },
408    { /* MPH_RATEPITCH, */ IRatePitch_init, NULL, NULL, NULL, NULL },
409    { /* MPH_RECORD, */ IRecord_init, NULL, NULL, NULL, NULL },
410    { /* MPH_SEEK, */ ISeek_init, NULL, NULL, NULL, NULL },
411    { /* MPH_THREADSYNC, */ IThreadSync_init, NULL, IThreadSync_deinit, NULL, NULL },
412    { /* MPH_VIBRA, */ IVibra_init, NULL, NULL, NULL, NULL },
413    { /* MPH_VIRTUALIZER, */ IVirtualizer_init, NULL, IVirtualizer_deinit, IVirtualizer_Expose,
414        NULL },
415    { /* MPH_VISUALIZATION, */ IVisualization_init, NULL, NULL, NULL, NULL },
416    { /* MPH_VOLUME, */ IVolume_init, NULL, NULL, NULL, NULL },
417// Wilhelm desktop extended interfaces
418    { /* MPH_OUTPUTMIXEXT, */ IOutputMixExt_init, NULL, NULL, NULL, NULL },
419// Android API level 9 extended interfaces
420    { /* MPH_ANDROIDEFFECT */ IAndroidEffect_init, NULL, IAndroidEffect_deinit, NULL, NULL },
421    { /* MPH_ANDROIDEFFECTCAPABILITIES */ IAndroidEffectCapabilities_init, NULL,
422        IAndroidEffectCapabilities_deinit, IAndroidEffectCapabilities_Expose, NULL },
423    { /* MPH_ANDROIDEFFECTSEND */ IAndroidEffectSend_init, NULL, NULL, NULL, NULL },
424    { /* MPH_ANDROIDCONFIGURATION */ IAndroidConfiguration_init, NULL, NULL, NULL, NULL },
425    { /* MPH_ANDROIDSIMPLEBUFFERQUEUE */ IBufferQueue_init /* alias */, NULL, NULL, NULL, NULL },
426// Android API level 10 extended interfaces
427    { /* MPH_ANDROIDBUFFERQUEUE */ IAndroidBufferQueue_init, NULL, IAndroidBufferQueue_deinit, NULL,
428        NULL },
429// OpenMAX AL 1.0.1 interfaces
430    { /* MPH_XAAUDIODECODERCAPABILITIES */ NULL, NULL, NULL, NULL, NULL },
431    { /* MPH_XAAUDIOENCODER */ NULL, NULL, NULL, NULL, NULL },
432    { /* MPH_XAAUDIOENCODERCAPABILITIES */ NULL, NULL, NULL, NULL, NULL },
433    { /* MPH_XAAUDIOIODEVICECAPABILITIES */ NULL, NULL, NULL, NULL, NULL },
434    { /* MPH_XACAMERA */ NULL, NULL, NULL, NULL, NULL },
435    { /* MPH_XACAMERACAPABILITIES */ NULL, NULL, NULL, NULL, NULL },
436    { /* MPH_XACONFIGEXTENSION */ NULL, NULL, NULL, NULL, NULL },
437    { /* MPH_XADEVICEVOLUME */ NULL, NULL, NULL, NULL, NULL },
438    { /* MPH_XADYNAMICINTERFACEMANAGEMENT 59 */ NULL, NULL, NULL, NULL, NULL },
439    { /* MPH_XADYNAMICSOURCE */ NULL, NULL, NULL, NULL, NULL },
440    { /* MPH_XAENGINE */ IXAEngine_init, NULL, IXAEngine_deinit, NULL, NULL },
441    { /* MPH_XAEQUALIZER */ NULL, NULL, NULL, NULL, NULL },
442    { /* MPH_XAIMAGECONTROLS */ NULL, NULL, NULL, NULL, NULL },
443    { /* MPH_XAIMAGEDECODERCAPABILITIES */ NULL, NULL, NULL, NULL, NULL },
444    { /* MPH_XAIMAGEEFFECTS */ NULL, NULL, NULL, NULL, NULL },
445    { /* MPH_XAIMAGEENCODER */ NULL, NULL, NULL, NULL, NULL },
446    { /* MPH_XAIMAGEENCODERCAPABILITIES */ NULL, NULL, NULL, NULL, NULL },
447    { /* MPH_XALED */ NULL, NULL, NULL, NULL, NULL },
448    { /* MPH_XAMETADATAEXTRACTION */ NULL, NULL, NULL, NULL, NULL },
449    { /* MPH_XAMETADATAINSERTION */ NULL, NULL, NULL, NULL, NULL },
450    { /* MPH_XAMETADATATRAVERSAL */ NULL, NULL, NULL, NULL, NULL },
451//  { /* MPH_XANULL */ NULL, NULL, NULL, NULL, NULL },
452    { /* MPH_XAOBJECT */ IObject_init, NULL, IObject_deinit, NULL, NULL },
453    { /* MPH_XAOUTPUTMIX */ NULL, NULL, NULL, NULL, NULL },
454    { /* MPH_XAPLAY */ IPlay_init, NULL, NULL, NULL, NULL },
455    { /* MPH_XAPLAYBACKRATE */ NULL, NULL, NULL, NULL, NULL },
456    { /* MPH_XAPREFETCHSTATUS */ NULL, NULL, NULL, NULL, NULL },
457    { /* MPH_XARADIO */ NULL, NULL, NULL, NULL, NULL },
458    { /* MPH_XARDS */ NULL, NULL, NULL, NULL, NULL },
459    { /* MPH_XARECORD */ NULL, NULL, NULL, NULL, NULL },
460    { /* MPH_XASEEK */ ISeek_init, NULL, NULL, NULL, NULL },
461    { /* MPH_XASNAPSHOT */ NULL, NULL, NULL, NULL, NULL },
462    { /* MPH_XASTREAMINFORMATION */ IStreamInformation_init, NULL, IStreamInformation_deinit,
463        NULL, NULL },
464    { /* MPH_XATHREADSYNC */ NULL, NULL, NULL, NULL, NULL },
465    { /* MPH_XAVIBRA */ NULL, NULL, NULL, NULL, NULL },
466    { /* MPH_XAVIDEODECODERCAPABILITIES */ IVideoDecoderCapabilities_init, NULL,
467            IVideoDecoderCapabilities_deinit, IVideoDecoderCapabilities_expose, NULL },
468    { /* MPH_XAVIDEOENCODER */ NULL, NULL, NULL, NULL, NULL },
469    { /* MPH_XAVIDEOENCODERCAPABILITIES */ NULL, NULL, NULL, NULL, NULL },
470    { /* MPH_XAVIDEOPOSTPROCESSING */ NULL, NULL, NULL, NULL, NULL },
471    { /* MPH_XAVOLUME, */ IVolume_init, NULL, NULL, NULL, NULL },
472};
473
474
475/** \brief Construct a new instance of the specified class, exposing selected interfaces */
476
477IObject *construct(const ClassTable *clazz, unsigned exposedMask, SLEngineItf engine)
478{
479    IObject *thiz;
480    // Do not change this to malloc; we depend on the object being memset to zero
481    thiz = (IObject *) calloc(1, clazz->mSize);
482    if (NULL != thiz) {
483        SL_LOGV("construct %s at %p", clazz->mName, thiz);
484        unsigned lossOfControlMask = 0;
485        // a NULL engine means we are constructing the engine
486        IEngine *thisEngine = (IEngine *) engine;
487        if (NULL == thisEngine) {
488            // thisEngine = &((CEngine *) thiz)->mEngine;
489            thiz->mEngine = (CEngine *) thiz;
490        } else {
491            thiz->mEngine = (CEngine *) thisEngine->mThis;
492            interface_lock_exclusive(thisEngine);
493            if (MAX_INSTANCE <= thisEngine->mInstanceCount) {
494                SL_LOGE("Too many objects");
495                interface_unlock_exclusive(thisEngine);
496                free(thiz);
497                return NULL;
498            }
499            // pre-allocate a pending slot, but don't assign bit from mInstanceMask yet
500            ++thisEngine->mInstanceCount;
501            assert(((unsigned) ~0) != thisEngine->mInstanceMask);
502            interface_unlock_exclusive(thisEngine);
503            // const, no lock needed
504            if (thisEngine->mLossOfControlGlobal) {
505                lossOfControlMask = ~0;
506            }
507        }
508        thiz->mLossOfControlMask = lossOfControlMask;
509        thiz->mClass = clazz;
510        const struct iid_vtable *x = clazz->mInterfaces;
511        SLuint8 *interfaceStateP = thiz->mInterfaceStates;
512        SLuint32 index;
513        for (index = 0; index < clazz->mInterfaceCount; ++index, ++x, exposedMask >>= 1) {
514            SLuint8 state;
515            // initialize all interfaces with init hooks, even if not exposed
516            const struct MPH_init *mi = &MPH_init_table[x->mMPH];
517            VoidHook init = mi->mInit;
518            if (NULL != init) {
519                void *self = (char *) thiz + x->mOffset;
520                // IObject does not have an mThis, so [1] is not always defined
521                if (index) {
522                    ((IObject **) self)[1] = thiz;
523                }
524                // call the initialization hook
525                (*init)(self);
526                // IObject does not require a call to GetInterface
527                if (index) {
528                    // This trickery invalidates the v-table until GetInterface
529                    ((size_t *) self)[0] ^= ~0;
530                }
531                // if interface is exposed, also call the optional expose hook
532                BoolHook expose;
533                state = (exposedMask & 1) && ((NULL == (expose = mi->mExpose)) || (*expose)(self)) ?
534                        INTERFACE_EXPOSED : INTERFACE_INITIALIZED;
535                // FIXME log or report to application if an expose hook on a
536                // required explicit interface fails at creation time
537            } else {
538                state = INTERFACE_UNINITIALIZED;
539            }
540            *interfaceStateP++ = state;
541        }
542        // note that the new object is not yet published; creator must call IObject_Publish
543    }
544    return thiz;
545}
546