blob: 9bf309c2249af7102b5018d15f3c24901897d9b1 [file] [log] [blame]
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -07001/*
Kevin Rocard96d2cd92018-11-14 16:22:07 -08002 * Copyright (C) 2018 The Android Open Source Project
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -07003 *
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
Kevin Rocard96d2cd92018-11-14 16:22:07 -080017#define LOG_TAG "EffectFactoryHAL"
18#include "EffectsFactory.h"
19#include "AcousticEchoCancelerEffect.h"
20#include "AutomaticGainControlEffect.h"
21#include "BassBoostEffect.h"
Kevin Rocard96d2cd92018-11-14 16:22:07 -080022#include "DownmixEffect.h"
23#include "Effect.h"
24#include "EnvironmentalReverbEffect.h"
25#include "EqualizerEffect.h"
Kevin Rocard96d2cd92018-11-14 16:22:07 -080026#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
Mikhail Naganova9ac8892021-01-15 19:05:04 +000033#include <UuidUtils.h>
Kevin Rocard22505e62017-12-14 18:50:12 -080034#include <android/log.h>
Andy Hung2d990242022-12-05 13:55:31 -080035#include <hidl/HidlTransportSupport.h>
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -070036#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>
Andy Hung2d990242022-12-05 13:55:31 -080048#include <system/thread_defs.h>
Mikhail Naganova9ac8892021-01-15 19:05:04 +000049#include <util/EffectUtils.h>
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -070050
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -070051namespace android {
52namespace hardware {
53namespace audio {
54namespace effect {
Kevin Rocard96d2cd92018-11-14 16:22:07 -080055namespace CPP_VERSION {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -070056namespace implementation {
57
Mikhail Naganov8140f562022-01-15 01:15:12 +000058using ::android::hardware::audio::common::COMMON_TYPES_CPP_VERSION::implementation::UuidUtils;
Mikhail Naganov543bf9c2018-12-11 16:36:53 -080059
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -070060// static
Kevin Rocard22505e62017-12-14 18:50:12 -080061sp<IEffect> EffectsFactory::dispatchEffectInstanceCreation(const effect_descriptor_t& halDescriptor,
62 effect_handle_t handle) {
63 const effect_uuid_t* halUuid = &halDescriptor.type;
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -070064 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 }
Mikhail Naganovf363ed42020-12-10 18:47:51 -080087 const bool isInput =
88 (halDescriptor.flags & EFFECT_FLAG_TYPE_PRE_PROC) == EFFECT_FLAG_TYPE_PRE_PROC;
89 return new Effect(isInput, handle);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -070090}
91
Kevin Rocard96d2cd92018-11-14 16:22:07 -080092// Methods from ::android::hardware::audio::effect::CPP_VERSION::IEffectsFactory follow.
Kevin Rocard22505e62017-12-14 18:50:12 -080093Return<void> EffectsFactory::getAllDescriptors(getAllDescriptors_cb _hidl_cb) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -070094 Result retval(Result::OK);
95 hidl_vec<EffectDescriptor> result;
96 uint32_t numEffects;
97 status_t status;
98
99restart:
100 numEffects = 0;
101 status = EffectQueryNumberEffects(&numEffects);
102 if (status != OK) {
103 retval = Result::NOT_INITIALIZED;
Mikhail Naganov9f289042017-02-23 08:39:36 -0800104 ALOGE("Error querying number of effects: %s", strerror(-status));
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700105 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) {
Mikhail Naganova9ac8892021-01-15 19:05:04 +0000112 EffectUtils::effectDescriptorFromHal(halDescriptor, &result[i]);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700113 } else {
Kevin Rocard22505e62017-12-14 18:50:12 -0800114 ALOGE("Error querying effect at position %d / %d: %s", i, numEffects,
115 strerror(-status));
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700116 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);
Mikhail Naganov40e36fa2018-10-10 14:40:54 -0700124 break;
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700125 }
126 default: {
127 result.resize(0);
128 retval = Result::NOT_INITIALIZED;
129 }
130 }
131 break;
132 }
133 }
134
135exit:
136 _hidl_cb(retval, result);
137 return Void();
138}
139
Eric Laurentd33e6932019-11-13 13:47:50 -0800140Return<void> EffectsFactory::getDescriptor(const Uuid& uuid, getDescriptor_cb _hidl_cb) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700141 effect_uuid_t halUuid;
Mikhail Naganov4122f632020-10-29 12:37:00 -0700142 UuidUtils::uuidToHal(uuid, &halUuid);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700143 effect_descriptor_t halDescriptor;
144 status_t status = EffectGetDescriptor(&halUuid, &halDescriptor);
145 EffectDescriptor descriptor;
Mikhail Naganova9ac8892021-01-15 19:05:04 +0000146 EffectUtils::effectDescriptorFromHal(halDescriptor, &descriptor);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700147 Result retval(Result::OK);
148 if (status != OK) {
Mikhail Naganova9ac8892021-01-15 19:05:04 +0000149 ALOGE("Error querying effect descriptor for %s: %s",
150 UuidUtils::uuidToString(halUuid).c_str(), strerror(-status));
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700151 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
Eric Laurentd33e6932019-11-13 13:47:50 -0800161#if MAJOR_VERSION <= 5
162Return<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
167Return<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
174Return<void> EffectsFactory::createEffectImpl(const Uuid& uuid, int32_t session, int32_t ioHandle,
175 int32_t device, createEffect_cb _hidl_cb) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700176 effect_uuid_t halUuid;
Mikhail Naganov4122f632020-10-29 12:37:00 -0700177 UuidUtils::uuidToHal(uuid, &halUuid);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700178 effect_handle_t handle;
179 Result retval(Result::OK);
Eric Laurentd33e6932019-11-13 13:47:50 -0800180 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 }
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700186 sp<IEffect> effect;
Mikhail Naganov10548292016-10-31 10:39:47 -0700187 uint64_t effectId = EffectMap::INVALID_ID;
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700188 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);
Andy Hung2d990242022-12-05 13:55:31 -0800194 android::hardware::setMinSchedulerPolicy(effect, SCHED_NORMAL, ANDROID_PRIORITY_AUDIO);
Mikhail Naganov10548292016-10-31 10:39:47 -0700195 effectId = EffectMap::getInstance().add(handle);
196 } else {
Mikhail Naganova9ac8892021-01-15 19:05:04 +0000197 ALOGE("Error querying effect descriptor for %s: %s",
198 UuidUtils::uuidToString(halUuid).c_str(), strerror(-status));
Mikhail Naganov10548292016-10-31 10:39:47 -0700199 EffectRelease(handle);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700200 }
201 }
202 if (status != OK) {
Mikhail Naganova9ac8892021-01-15 19:05:04 +0000203 ALOGE("Error creating effect %s: %s", UuidUtils::uuidToString(halUuid).c_str(),
204 strerror(-status));
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700205 if (status == -ENOENT) {
206 retval = Result::INVALID_ARGUMENTS;
207 } else {
208 retval = Result::NOT_INITIALIZED;
209 }
210 }
Mikhail Naganov10548292016-10-31 10:39:47 -0700211 _hidl_cb(retval, effect, effectId);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700212 return Void();
213}
214
Kevin Rocard22505e62017-12-14 18:50:12 -0800215Return<void> EffectsFactory::debugDump(const hidl_handle& fd) {
Kevin Rocard3d410272018-04-19 17:52:32 -0700216 return debug(fd, {} /* options */);
217}
218
219Return<void> EffectsFactory::debug(const hidl_handle& fd,
220 const hidl_vec<hidl_string>& /* options */) {
Mikhail Naganov7bae6a02017-04-24 10:44:08 -0700221 if (fd.getNativeHandle() != nullptr && fd->numFds == 1) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700222 EffectDumpEffects(fd->data[0]);
223 }
224 return Void();
225}
226
Mikhail Naganov6ffde442018-03-07 09:55:06 -0800227IEffectsFactory* HIDL_FETCH_IEffectsFactory(const char* name) {
228 return strcmp(name, "default") == 0 ? new EffectsFactory() : nullptr;
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700229}
230
Kevin Rocard22505e62017-12-14 18:50:12 -0800231} // namespace implementation
Kevin Rocard96d2cd92018-11-14 16:22:07 -0800232} // namespace CPP_VERSION
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700233} // namespace effect
234} // namespace audio
235} // namespace hardware
236} // namespace android