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