blob: 572a428f4edc55d474257ac853cb0fcc8cae798c [file] [log] [blame]
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -07001/*
2 * Copyright (C) 2016 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 <media/EffectsFactoryApi.h>
19#include <system/audio_effects/effect_aec.h>
20#include <system/audio_effects/effect_agc.h>
21#include <system/audio_effects/effect_bassboost.h>
22#include <system/audio_effects/effect_downmix.h>
23#include <system/audio_effects/effect_environmentalreverb.h>
24#include <system/audio_effects/effect_equalizer.h>
25#include <system/audio_effects/effect_loudnessenhancer.h>
26#include <system/audio_effects/effect_ns.h>
27#include <system/audio_effects/effect_presetreverb.h>
28#include <system/audio_effects/effect_virtualizer.h>
29#include <system/audio_effects/effect_visualizer.h>
Yifan Hongf9d30342016-11-30 13:45:34 -080030#include <android/log.h>
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -070031
32#include "AcousticEchoCancelerEffect.h"
33#include "AutomaticGainControlEffect.h"
34#include "BassBoostEffect.h"
35#include "Conversions.h"
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -070036#include "DownmixEffect.h"
Mikhail Naganov6e81e9b2016-11-16 16:30:17 -080037#include "EffectsFactory.h"
38#include "HidlUtils.h"
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -070039#include "Effect.h"
Mikhail Naganov10548292016-10-31 10:39:47 -070040#include "EffectMap.h"
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -070041#include "EnvironmentalReverbEffect.h"
42#include "EqualizerEffect.h"
43#include "LoudnessEnhancerEffect.h"
44#include "NoiseSuppressionEffect.h"
45#include "PresetReverbEffect.h"
46#include "VirtualizerEffect.h"
47#include "VisualizerEffect.h"
48
49namespace android {
50namespace hardware {
51namespace audio {
52namespace effect {
53namespace V2_0 {
54namespace implementation {
55
56// static
57sp<IEffect> EffectsFactory::dispatchEffectInstanceCreation(
58 const effect_descriptor_t& halDescriptor, effect_handle_t handle) {
59 const effect_uuid_t *halUuid = &halDescriptor.type;
60 if (memcmp(halUuid, FX_IID_AEC, sizeof(effect_uuid_t)) == 0) {
61 return new AcousticEchoCancelerEffect(handle);
62 } else if (memcmp(halUuid, FX_IID_AGC, sizeof(effect_uuid_t)) == 0) {
63 return new AutomaticGainControlEffect(handle);
64 } else if (memcmp(halUuid, SL_IID_BASSBOOST, sizeof(effect_uuid_t)) == 0) {
65 return new BassBoostEffect(handle);
66 } else if (memcmp(halUuid, EFFECT_UIID_DOWNMIX, sizeof(effect_uuid_t)) == 0) {
67 return new DownmixEffect(handle);
68 } else if (memcmp(halUuid, SL_IID_ENVIRONMENTALREVERB, sizeof(effect_uuid_t)) == 0) {
69 return new EnvironmentalReverbEffect(handle);
70 } else if (memcmp(halUuid, SL_IID_EQUALIZER, sizeof(effect_uuid_t)) == 0) {
71 return new EqualizerEffect(handle);
72 } else if (memcmp(halUuid, FX_IID_LOUDNESS_ENHANCER, sizeof(effect_uuid_t)) == 0) {
73 return new LoudnessEnhancerEffect(handle);
74 } else if (memcmp(halUuid, FX_IID_NS, sizeof(effect_uuid_t)) == 0) {
75 return new NoiseSuppressionEffect(handle);
76 } else if (memcmp(halUuid, SL_IID_PRESETREVERB, sizeof(effect_uuid_t)) == 0) {
77 return new PresetReverbEffect(handle);
78 } else if (memcmp(halUuid, SL_IID_VIRTUALIZER, sizeof(effect_uuid_t)) == 0) {
79 return new VirtualizerEffect(handle);
80 } else if (memcmp(halUuid, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) {
81 return new VisualizerEffect(handle);
82 }
83 return new Effect(handle);
84}
85
86// Methods from ::android::hardware::audio::effect::V2_0::IEffectsFactory follow.
87Return<void> EffectsFactory::getAllDescriptors(getAllDescriptors_cb _hidl_cb) {
88 Result retval(Result::OK);
89 hidl_vec<EffectDescriptor> result;
90 uint32_t numEffects;
91 status_t status;
92
93restart:
94 numEffects = 0;
95 status = EffectQueryNumberEffects(&numEffects);
96 if (status != OK) {
97 retval = Result::NOT_INITIALIZED;
98 ALOGW("Error querying number of effects: %s", strerror(-status));
99 goto exit;
100 }
101 result.resize(numEffects);
102 for (uint32_t i = 0; i < numEffects; ++i) {
103 effect_descriptor_t halDescriptor;
104 status = EffectQueryEffect(i, &halDescriptor);
105 if (status == OK) {
106 effectDescriptorFromHal(halDescriptor, &result[i]);
107 } else {
108 ALOGW("Error querying effect at position %d / %d: %s",
109 i, numEffects, strerror(-status));
110 switch (status) {
111 case -ENOSYS: {
112 // Effect list has changed.
113 goto restart;
114 }
115 case -ENOENT: {
116 // No more effects available.
117 result.resize(i);
118 }
119 default: {
120 result.resize(0);
121 retval = Result::NOT_INITIALIZED;
122 }
123 }
124 break;
125 }
126 }
127
128exit:
129 _hidl_cb(retval, result);
130 return Void();
131}
132
133Return<void> EffectsFactory::getDescriptor(const Uuid& uid, getDescriptor_cb _hidl_cb) {
134 effect_uuid_t halUuid;
Mikhail Naganov6e81e9b2016-11-16 16:30:17 -0800135 HidlUtils::uuidToHal(uid, &halUuid);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700136 effect_descriptor_t halDescriptor;
137 status_t status = EffectGetDescriptor(&halUuid, &halDescriptor);
138 EffectDescriptor descriptor;
139 effectDescriptorFromHal(halDescriptor, &descriptor);
140 Result retval(Result::OK);
141 if (status != OK) {
142 ALOGW("Error querying effect descriptor for %s: %s",
143 uuidToString(halUuid).c_str(), strerror(-status));
144 if (status == -ENOENT) {
145 retval = Result::INVALID_ARGUMENTS;
146 } else {
147 retval = Result::NOT_INITIALIZED;
148 }
149 }
150 _hidl_cb(retval, descriptor);
151 return Void();
152}
153
154Return<void> EffectsFactory::createEffect(
155 const Uuid& uid, int32_t session, int32_t ioHandle, createEffect_cb _hidl_cb) {
156 effect_uuid_t halUuid;
Mikhail Naganov6e81e9b2016-11-16 16:30:17 -0800157 HidlUtils::uuidToHal(uid, &halUuid);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700158 effect_handle_t handle;
159 Result retval(Result::OK);
160 status_t status = EffectCreate(&halUuid, session, ioHandle, &handle);
161 sp<IEffect> effect;
Mikhail Naganov10548292016-10-31 10:39:47 -0700162 uint64_t effectId = EffectMap::INVALID_ID;
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700163 if (status == OK) {
164 effect_descriptor_t halDescriptor;
165 memset(&halDescriptor, 0, sizeof(effect_descriptor_t));
166 status = (*handle)->get_descriptor(handle, &halDescriptor);
167 if (status == OK) {
168 effect = dispatchEffectInstanceCreation(halDescriptor, handle);
Mikhail Naganov10548292016-10-31 10:39:47 -0700169 effectId = EffectMap::getInstance().add(handle);
170 } else {
171 EffectRelease(handle);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700172 }
173 }
174 if (status != OK) {
175 ALOGW("Error creating effect %s: %s", uuidToString(halUuid).c_str(), strerror(-status));
176 if (status == -ENOENT) {
177 retval = Result::INVALID_ARGUMENTS;
178 } else {
179 retval = Result::NOT_INITIALIZED;
180 }
181 }
Mikhail Naganov10548292016-10-31 10:39:47 -0700182 _hidl_cb(retval, effect, effectId);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700183 return Void();
184}
185
Martijn Coenen70b9a152016-11-18 15:29:32 +0100186Return<void> EffectsFactory::debugDump(const hidl_handle& fd) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700187 if (fd->numFds == 1) {
188 EffectDumpEffects(fd->data[0]);
189 }
190 return Void();
191}
192
193
194IEffectsFactory* HIDL_FETCH_IEffectsFactory(const char* /* name */) {
195 return new EffectsFactory();
196}
197
198} // namespace implementation
199} // namespace V2_0
200} // namespace effect
201} // namespace audio
202} // namespace hardware
203} // namespace android