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/* Android Effect Capabilities implementation */
18
19#include "sles_allinclusive.h"
20
21
22static SLresult IAndroidEffectCapabilities_QueryNumEffects(SLAndroidEffectCapabilitiesItf self,
23        SLuint32 * pNumSupportedAudioEffects) {
24
25    SL_ENTER_INTERFACE
26
27    if (NULL == pNumSupportedAudioEffects) {
28        result = SL_RESULT_PARAMETER_INVALID;
29    } else {
30        IAndroidEffectCapabilities *thiz = (IAndroidEffectCapabilities *) self;
31        interface_lock_shared(thiz);
32
33        *pNumSupportedAudioEffects = thiz->mNumFx;
34        result = SL_RESULT_SUCCESS;
35
36        interface_unlock_shared(thiz);
37    }
38
39    SL_LEAVE_INTERFACE
40}
41
42
43static SLresult IAndroidEffectCapabilities_QueryEffect(SLAndroidEffectCapabilitiesItf self,
44        SLuint32 index, SLInterfaceID *pEffectType, SLInterfaceID *pEffectImplementation,
45        SLchar * pName, SLuint16 *pNameSize) {
46
47    SL_ENTER_INTERFACE
48
49    IAndroidEffectCapabilities *thiz = (IAndroidEffectCapabilities *) self;
50    if (index > thiz->mNumFx) {
51        result = SL_RESULT_PARAMETER_INVALID;
52    } else {
53        interface_lock_shared(thiz);
54        if (NULL != pEffectType) {
55            *pEffectType = (SLInterfaceID) &thiz->mFxDescriptors[index].type;
56        }
57        if (NULL != pEffectImplementation) {
58            *pEffectImplementation = (SLInterfaceID) &thiz->mFxDescriptors[index].uuid;
59        }
60        if ((NULL != pName) && (0 < *pNameSize)) {
61            int len = strlen(thiz->mFxDescriptors[index].name);
62            strncpy((char*)pName, thiz->mFxDescriptors[index].name,
63                    *pNameSize > len ? len : *pNameSize );
64            *pNameSize = len;
65        }
66        interface_unlock_shared(thiz);
67        result = SL_RESULT_SUCCESS;
68    }
69
70    SL_LEAVE_INTERFACE
71}
72
73
74static const struct SLAndroidEffectCapabilitiesItf_ IAndroidEffectCapabilities_Itf = {
75        IAndroidEffectCapabilities_QueryNumEffects,
76        IAndroidEffectCapabilities_QueryEffect
77};
78
79void IAndroidEffectCapabilities_init(void *self)
80{
81    IAndroidEffectCapabilities *thiz = (IAndroidEffectCapabilities *) self;
82    thiz->mItf = &IAndroidEffectCapabilities_Itf;
83
84    // This is the default initialization; fields will be updated when interface is exposed
85    thiz->mNumFx = 0;
86    thiz->mFxDescriptors = NULL;
87}
88
89bool IAndroidEffectCapabilities_Expose(void *self)
90{
91    IAndroidEffectCapabilities *thiz = (IAndroidEffectCapabilities *) self;
92    SLuint32 numEffects = 0;
93    SLresult result = android_genericFx_queryNumEffects(&numEffects);
94    if (SL_RESULT_SUCCESS != result) {
95        SL_LOGE("android_genericFx_queryNumEffects %u", result);
96        return false;
97    }
98    thiz->mNumFx = numEffects;
99    SL_LOGV("Effect Capabilities has %d effects", thiz->mNumFx);
100    if (thiz->mNumFx > 0) {
101        thiz->mFxDescriptors = (effect_descriptor_t*) new effect_descriptor_t[thiz->mNumFx];
102        for (SLuint32 i = 0 ; i < thiz->mNumFx ; i++) {
103            SLresult result2;
104            result2 = android_genericFx_queryEffect(i, &thiz->mFxDescriptors[i]);
105            if (SL_RESULT_SUCCESS != result2) {
106                SL_LOGE("Error (SLresult is %d) querying effect %d", result2, i);
107                // Remember the first failing result code, but keep going
108                if (SL_RESULT_SUCCESS == result) {
109                    result = result2;
110                }
111            } else {
112                SL_LOGV("effect %d: type=%08x-%04x-%04x-%04x-%02x%02x%02x%02x%02x%02x name=%s",
113                        i,
114                        thiz->mFxDescriptors[i].type.timeLow,
115                        thiz->mFxDescriptors[i].type.timeMid,
116                        thiz->mFxDescriptors[i].type.timeHiAndVersion,
117                        thiz->mFxDescriptors[i].type.clockSeq,
118                        thiz->mFxDescriptors[i].type.node[0],
119                        thiz->mFxDescriptors[i].type.node[1],
120                        thiz->mFxDescriptors[i].type.node[2],
121                        thiz->mFxDescriptors[i].type.node[3],
122                        thiz->mFxDescriptors[i].type.node[4],
123                        thiz->mFxDescriptors[i].type.node[5],
124                        thiz->mFxDescriptors[i].name);
125            }
126        }
127    }
128    return SL_RESULT_SUCCESS == result;
129}
130
131void IAndroidEffectCapabilities_deinit(void *self)
132{
133    IAndroidEffectCapabilities *thiz = (IAndroidEffectCapabilities *) self;
134    // free effect library data
135    if (NULL != thiz->mFxDescriptors) {
136        delete[] thiz->mFxDescriptors;
137        thiz->mFxDescriptors = NULL;
138    }
139}
140