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 *this = (IAndroidEffectCapabilities *) self;
31        interface_lock_peek(this);
32
33        *pNumSupportedAudioEffects = this->mNumFx;
34        result = SL_RESULT_SUCCESS
35
36        interface_unlock_peek(this);
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 *this = (IAndroidEffectCapabilities *) self;
50    if (index > this->mNumFx) {
51        result = SL_RESULT_PARAMETER_INVALID;
52    } else {
53        interface_lock_peek(this);
54        if (NULL != pEffectType) {
55            *pEffectType = (SLInterfaceID) &this->mFxDescriptors[index].type;
56        }
57        if (NULL != pEffectImplementation) {
58            *pEffectImplementation = (SLInterfaceID) &this->mFxDescriptors[index].uuid;
59        }
60        if ((NULL != pName) && (0 < *pNameSize)) {
61            int len = strlen(this->mFxDescriptors[index].name);
62            strncpy((char*)pName, this->mFxDescriptors[index].name,
63                    *pNameSize > len ? len : *pNameSize );
64            *pNameSize = len;
65        }
66        interface_unlock_peek(this);
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 *this = (IAndroidEffectCapabilities *) self;
82    this->mItf = &IAndroidEffectCapabilities_Itf;
83
84    // This is the default initialization; fields will be updated when interface is exposed
85    this->mNumFx = 0;
86    this->mFxDescriptors = NULL;
87}
88
89bool IAndroidEffectCapabilities_Expose(void *self)
90{
91    IAndroidEffectCapabilities *this = (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 %lu", result);
96        return false;
97    }
98    this->mNumFx = numEffects;
99    SL_LOGV("Effect Capabilities has %ld effects", this->mNumFx);
100    if (this->mNumFx > 0) {
101        this->mFxDescriptors = (effect_descriptor_t*) new effect_descriptor_t[this->mNumFx];
102        for (SLuint32 i = 0 ; i < this->mNumFx ; i++) {
103            SLresult result2;
104            result2 = android_genericFx_queryEffect(i, &this->mFxDescriptors[i]);
105            if (SL_RESULT_SUCCESS != result2) {
106                SL_LOGE("Error (SLresult is %ld) querying effect %ld", 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 %ld: type=%08x-%04x-%04x-%04x-%02x%02x%02x%02x%02x%02x name=%s",
113                        i,
114                        this->mFxDescriptors[i].type.timeLow,
115                        this->mFxDescriptors[i].type.timeMid,
116                        this->mFxDescriptors[i].type.timeHiAndVersion,
117                        this->mFxDescriptors[i].type.clockSeq,
118                        this->mFxDescriptors[i].type.node[0],
119                        this->mFxDescriptors[i].type.node[1],
120                        this->mFxDescriptors[i].type.node[2],
121                        this->mFxDescriptors[i].type.node[3],
122                        this->mFxDescriptors[i].type.node[4],
123                        this->mFxDescriptors[i].type.node[5],
124                        this->mFxDescriptors[i].name);
125            }
126        }
127    }
128    return SL_RESULT_SUCCESS == result;
129}
130
131void IAndroidEffectCapabilities_deinit(void *self)
132{
133    IAndroidEffectCapabilities *this = (IAndroidEffectCapabilities *) self;
134    // free effect library data
135    if (NULL != this->mFxDescriptors) {
136        delete[] this->mFxDescriptors;
137        this->mFxDescriptors = NULL;
138    }
139}
140