1 /*
2  * Copyright (C) 2018 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 #define LOG_TAG "EffectFactoryHAL"
18 #include "EffectsFactory.h"
19 #include "AcousticEchoCancelerEffect.h"
20 #include "AutomaticGainControlEffect.h"
21 #include "BassBoostEffect.h"
22 #include "Conversions.h"
23 #include "DownmixEffect.h"
24 #include "Effect.h"
25 #include "EnvironmentalReverbEffect.h"
26 #include "EqualizerEffect.h"
27 #include "HidlUtils.h"
28 #include "LoudnessEnhancerEffect.h"
29 #include "NoiseSuppressionEffect.h"
30 #include "PresetReverbEffect.h"
31 #include "VirtualizerEffect.h"
32 #include "VisualizerEffect.h"
33 #include "common/all-versions/default/EffectMap.h"
34 
35 #include <android/log.h>
36 #include <media/EffectsFactoryApi.h>
37 #include <system/audio_effects/effect_aec.h>
38 #include <system/audio_effects/effect_agc.h>
39 #include <system/audio_effects/effect_bassboost.h>
40 #include <system/audio_effects/effect_downmix.h>
41 #include <system/audio_effects/effect_environmentalreverb.h>
42 #include <system/audio_effects/effect_equalizer.h>
43 #include <system/audio_effects/effect_loudnessenhancer.h>
44 #include <system/audio_effects/effect_ns.h>
45 #include <system/audio_effects/effect_presetreverb.h>
46 #include <system/audio_effects/effect_virtualizer.h>
47 #include <system/audio_effects/effect_visualizer.h>
48 
49 namespace android {
50 namespace hardware {
51 namespace audio {
52 namespace effect {
53 namespace CPP_VERSION {
54 namespace implementation {
55 
56 using ::android::hardware::audio::common::CPP_VERSION::implementation::HidlUtils;
57 
58 // static
dispatchEffectInstanceCreation(const effect_descriptor_t & halDescriptor,effect_handle_t handle)59 sp<IEffect> EffectsFactory::dispatchEffectInstanceCreation(const effect_descriptor_t& halDescriptor,
60                                                            effect_handle_t handle) {
61     const effect_uuid_t* halUuid = &halDescriptor.type;
62     if (memcmp(halUuid, FX_IID_AEC, sizeof(effect_uuid_t)) == 0) {
63         return new AcousticEchoCancelerEffect(handle);
64     } else if (memcmp(halUuid, FX_IID_AGC, sizeof(effect_uuid_t)) == 0) {
65         return new AutomaticGainControlEffect(handle);
66     } else if (memcmp(halUuid, SL_IID_BASSBOOST, sizeof(effect_uuid_t)) == 0) {
67         return new BassBoostEffect(handle);
68     } else if (memcmp(halUuid, EFFECT_UIID_DOWNMIX, sizeof(effect_uuid_t)) == 0) {
69         return new DownmixEffect(handle);
70     } else if (memcmp(halUuid, SL_IID_ENVIRONMENTALREVERB, sizeof(effect_uuid_t)) == 0) {
71         return new EnvironmentalReverbEffect(handle);
72     } else if (memcmp(halUuid, SL_IID_EQUALIZER, sizeof(effect_uuid_t)) == 0) {
73         return new EqualizerEffect(handle);
74     } else if (memcmp(halUuid, FX_IID_LOUDNESS_ENHANCER, sizeof(effect_uuid_t)) == 0) {
75         return new LoudnessEnhancerEffect(handle);
76     } else if (memcmp(halUuid, FX_IID_NS, sizeof(effect_uuid_t)) == 0) {
77         return new NoiseSuppressionEffect(handle);
78     } else if (memcmp(halUuid, SL_IID_PRESETREVERB, sizeof(effect_uuid_t)) == 0) {
79         return new PresetReverbEffect(handle);
80     } else if (memcmp(halUuid, SL_IID_VIRTUALIZER, sizeof(effect_uuid_t)) == 0) {
81         return new VirtualizerEffect(handle);
82     } else if (memcmp(halUuid, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) {
83         return new VisualizerEffect(handle);
84     }
85     return new Effect(handle);
86 }
87 
88 // Methods from ::android::hardware::audio::effect::CPP_VERSION::IEffectsFactory follow.
getAllDescriptors(getAllDescriptors_cb _hidl_cb)89 Return<void> EffectsFactory::getAllDescriptors(getAllDescriptors_cb _hidl_cb) {
90     Result retval(Result::OK);
91     hidl_vec<EffectDescriptor> result;
92     uint32_t numEffects;
93     status_t status;
94 
95 restart:
96     numEffects = 0;
97     status = EffectQueryNumberEffects(&numEffects);
98     if (status != OK) {
99         retval = Result::NOT_INITIALIZED;
100         ALOGE("Error querying number of effects: %s", strerror(-status));
101         goto exit;
102     }
103     result.resize(numEffects);
104     for (uint32_t i = 0; i < numEffects; ++i) {
105         effect_descriptor_t halDescriptor;
106         status = EffectQueryEffect(i, &halDescriptor);
107         if (status == OK) {
108             effectDescriptorFromHal(halDescriptor, &result[i]);
109         } else {
110             ALOGE("Error querying effect at position %d / %d: %s", i, numEffects,
111                   strerror(-status));
112             switch (status) {
113                 case -ENOSYS: {
114                     // Effect list has changed.
115                     goto restart;
116                 }
117                 case -ENOENT: {
118                     // No more effects available.
119                     result.resize(i);
120                     break;
121                 }
122                 default: {
123                     result.resize(0);
124                     retval = Result::NOT_INITIALIZED;
125                 }
126             }
127             break;
128         }
129     }
130 
131 exit:
132     _hidl_cb(retval, result);
133     return Void();
134 }
135 
getDescriptor(const Uuid & uuid,getDescriptor_cb _hidl_cb)136 Return<void> EffectsFactory::getDescriptor(const Uuid& uuid, getDescriptor_cb _hidl_cb) {
137     effect_uuid_t halUuid;
138     HidlUtils::uuidToHal(uuid, &halUuid);
139     effect_descriptor_t halDescriptor;
140     status_t status = EffectGetDescriptor(&halUuid, &halDescriptor);
141     EffectDescriptor descriptor;
142     effectDescriptorFromHal(halDescriptor, &descriptor);
143     Result retval(Result::OK);
144     if (status != OK) {
145         ALOGE("Error querying effect descriptor for %s: %s", uuidToString(halUuid).c_str(),
146               strerror(-status));
147         if (status == -ENOENT) {
148             retval = Result::INVALID_ARGUMENTS;
149         } else {
150             retval = Result::NOT_INITIALIZED;
151         }
152     }
153     _hidl_cb(retval, descriptor);
154     return Void();
155 }
156 
157 #if MAJOR_VERSION <= 5
createEffect(const Uuid & uuid,int32_t session,int32_t ioHandle,EffectsFactory::createEffect_cb _hidl_cb)158 Return<void> EffectsFactory::createEffect(const Uuid& uuid, int32_t session, int32_t ioHandle,
159                                           EffectsFactory::createEffect_cb _hidl_cb) {
160     return createEffectImpl(uuid, session, ioHandle, AUDIO_PORT_HANDLE_NONE, _hidl_cb);
161 }
162 #else
createEffect(const Uuid & uuid,int32_t session,int32_t ioHandle,int32_t device,EffectsFactory::createEffect_cb _hidl_cb)163 Return<void> EffectsFactory::createEffect(const Uuid& uuid, int32_t session, int32_t ioHandle,
164                                           int32_t device,
165                                           EffectsFactory::createEffect_cb _hidl_cb) {
166     return createEffectImpl(uuid, session, ioHandle, device, _hidl_cb);
167 }
168 #endif
169 
createEffectImpl(const Uuid & uuid,int32_t session,int32_t ioHandle,int32_t device,createEffect_cb _hidl_cb)170 Return<void> EffectsFactory::createEffectImpl(const Uuid& uuid, int32_t session, int32_t ioHandle,
171                                               int32_t device, createEffect_cb _hidl_cb) {
172     effect_uuid_t halUuid;
173     HidlUtils::uuidToHal(uuid, &halUuid);
174     effect_handle_t handle;
175     Result retval(Result::OK);
176     status_t status;
177     if (session == AUDIO_SESSION_DEVICE) {
178         status = EffectCreateOnDevice(&halUuid, device, ioHandle, &handle);
179     } else {
180         status = EffectCreate(&halUuid, session, ioHandle, &handle);
181     }
182     sp<IEffect> effect;
183     uint64_t effectId = EffectMap::INVALID_ID;
184     if (status == OK) {
185         effect_descriptor_t halDescriptor;
186         memset(&halDescriptor, 0, sizeof(effect_descriptor_t));
187         status = (*handle)->get_descriptor(handle, &halDescriptor);
188         if (status == OK) {
189             effect = dispatchEffectInstanceCreation(halDescriptor, handle);
190             effectId = EffectMap::getInstance().add(handle);
191         } else {
192             ALOGE("Error querying effect descriptor for %s: %s", uuidToString(halUuid).c_str(),
193                   strerror(-status));
194             EffectRelease(handle);
195         }
196     }
197     if (status != OK) {
198         ALOGE("Error creating effect %s: %s", uuidToString(halUuid).c_str(), strerror(-status));
199         if (status == -ENOENT) {
200             retval = Result::INVALID_ARGUMENTS;
201         } else {
202             retval = Result::NOT_INITIALIZED;
203         }
204     }
205     _hidl_cb(retval, effect, effectId);
206     return Void();
207 }
208 
debugDump(const hidl_handle & fd)209 Return<void> EffectsFactory::debugDump(const hidl_handle& fd) {
210     return debug(fd, {} /* options */);
211 }
212 
debug(const hidl_handle & fd,const hidl_vec<hidl_string> &)213 Return<void> EffectsFactory::debug(const hidl_handle& fd,
214                                    const hidl_vec<hidl_string>& /* options */) {
215     if (fd.getNativeHandle() != nullptr && fd->numFds == 1) {
216         EffectDumpEffects(fd->data[0]);
217     }
218     return Void();
219 }
220 
HIDL_FETCH_IEffectsFactory(const char * name)221 IEffectsFactory* HIDL_FETCH_IEffectsFactory(const char* name) {
222     return strcmp(name, "default") == 0 ? new EffectsFactory() : nullptr;
223 }
224 
225 }  // namespace implementation
226 }  // namespace CPP_VERSION
227 }  // namespace effect
228 }  // namespace audio
229 }  // namespace hardware
230 }  // namespace android
231