blob: 6283e7bc13bf7c063f696eb793640fa3d1e8d3d9 [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"
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
Kevin Rocard22505e62017-12-14 18:50:12 -080035#include <android/log.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>
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -070048
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -070049namespace android {
50namespace hardware {
51namespace audio {
52namespace effect {
Kevin Rocard96d2cd92018-11-14 16:22:07 -080053namespace CPP_VERSION {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -070054namespace implementation {
55
Mikhail Naganov543bf9c2018-12-11 16:36:53 -080056using ::android::hardware::audio::common::CPP_VERSION::implementation::HidlUtils;
57
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -070058// static
Kevin Rocard22505e62017-12-14 18:50:12 -080059sp<IEffect> EffectsFactory::dispatchEffectInstanceCreation(const effect_descriptor_t& halDescriptor,
60 effect_handle_t handle) {
61 const effect_uuid_t* halUuid = &halDescriptor.type;
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -070062 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
Kevin Rocard96d2cd92018-11-14 16:22:07 -080088// Methods from ::android::hardware::audio::effect::CPP_VERSION::IEffectsFactory follow.
Kevin Rocard22505e62017-12-14 18:50:12 -080089Return<void> EffectsFactory::getAllDescriptors(getAllDescriptors_cb _hidl_cb) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -070090 Result retval(Result::OK);
91 hidl_vec<EffectDescriptor> result;
92 uint32_t numEffects;
93 status_t status;
94
95restart:
96 numEffects = 0;
97 status = EffectQueryNumberEffects(&numEffects);
98 if (status != OK) {
99 retval = Result::NOT_INITIALIZED;
Mikhail Naganov9f289042017-02-23 08:39:36 -0800100 ALOGE("Error querying number of effects: %s", strerror(-status));
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700101 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 {
Kevin Rocard22505e62017-12-14 18:50:12 -0800110 ALOGE("Error querying effect at position %d / %d: %s", i, numEffects,
111 strerror(-status));
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700112 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);
Mikhail Naganov40e36fa2018-10-10 14:40:54 -0700120 break;
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700121 }
122 default: {
123 result.resize(0);
124 retval = Result::NOT_INITIALIZED;
125 }
126 }
127 break;
128 }
129 }
130
131exit:
132 _hidl_cb(retval, result);
133 return Void();
134}
135
Kevin Rocard22505e62017-12-14 18:50:12 -0800136Return<void> EffectsFactory::getDescriptor(const Uuid& uid, getDescriptor_cb _hidl_cb) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700137 effect_uuid_t halUuid;
Mikhail Naganov6e81e9b2016-11-16 16:30:17 -0800138 HidlUtils::uuidToHal(uid, &halUuid);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700139 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) {
Kevin Rocard22505e62017-12-14 18:50:12 -0800145 ALOGE("Error querying effect descriptor for %s: %s", uuidToString(halUuid).c_str(),
146 strerror(-status));
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700147 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
Kevin Rocard22505e62017-12-14 18:50:12 -0800157Return<void> EffectsFactory::createEffect(const Uuid& uid, int32_t session, int32_t ioHandle,
158 createEffect_cb _hidl_cb) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700159 effect_uuid_t halUuid;
Mikhail Naganov6e81e9b2016-11-16 16:30:17 -0800160 HidlUtils::uuidToHal(uid, &halUuid);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700161 effect_handle_t handle;
162 Result retval(Result::OK);
163 status_t status = EffectCreate(&halUuid, session, ioHandle, &handle);
164 sp<IEffect> effect;
Mikhail Naganov10548292016-10-31 10:39:47 -0700165 uint64_t effectId = EffectMap::INVALID_ID;
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700166 if (status == OK) {
167 effect_descriptor_t halDescriptor;
168 memset(&halDescriptor, 0, sizeof(effect_descriptor_t));
169 status = (*handle)->get_descriptor(handle, &halDescriptor);
170 if (status == OK) {
171 effect = dispatchEffectInstanceCreation(halDescriptor, handle);
Mikhail Naganov10548292016-10-31 10:39:47 -0700172 effectId = EffectMap::getInstance().add(handle);
173 } else {
Kevin Rocard22505e62017-12-14 18:50:12 -0800174 ALOGE("Error querying effect descriptor for %s: %s", uuidToString(halUuid).c_str(),
175 strerror(-status));
Mikhail Naganov10548292016-10-31 10:39:47 -0700176 EffectRelease(handle);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700177 }
178 }
179 if (status != OK) {
Mikhail Naganov9f289042017-02-23 08:39:36 -0800180 ALOGE("Error creating effect %s: %s", uuidToString(halUuid).c_str(), strerror(-status));
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700181 if (status == -ENOENT) {
182 retval = Result::INVALID_ARGUMENTS;
183 } else {
184 retval = Result::NOT_INITIALIZED;
185 }
186 }
Mikhail Naganov10548292016-10-31 10:39:47 -0700187 _hidl_cb(retval, effect, effectId);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700188 return Void();
189}
190
Kevin Rocard22505e62017-12-14 18:50:12 -0800191Return<void> EffectsFactory::debugDump(const hidl_handle& fd) {
Kevin Rocard3d410272018-04-19 17:52:32 -0700192 return debug(fd, {} /* options */);
193}
194
195Return<void> EffectsFactory::debug(const hidl_handle& fd,
196 const hidl_vec<hidl_string>& /* options */) {
Mikhail Naganov7bae6a02017-04-24 10:44:08 -0700197 if (fd.getNativeHandle() != nullptr && fd->numFds == 1) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700198 EffectDumpEffects(fd->data[0]);
199 }
200 return Void();
201}
202
Mikhail Naganov6ffde442018-03-07 09:55:06 -0800203IEffectsFactory* HIDL_FETCH_IEffectsFactory(const char* name) {
204 return strcmp(name, "default") == 0 ? new EffectsFactory() : nullptr;
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700205}
206
Kevin Rocard22505e62017-12-14 18:50:12 -0800207} // namespace implementation
Kevin Rocard96d2cd92018-11-14 16:22:07 -0800208} // namespace CPP_VERSION
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700209} // namespace effect
210} // namespace audio
211} // namespace hardware
212} // namespace android