blob: f7c5d628b0275196417e2538bd6fb3497be09b67 [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>
30#include <utils/Log.h>
31
32#include "AcousticEchoCancelerEffect.h"
33#include "AutomaticGainControlEffect.h"
34#include "BassBoostEffect.h"
35#include "Conversions.h"
36#include "EffectsFactory.h"
37#include "DownmixEffect.h"
38#include "Effect.h"
39#include "EnvironmentalReverbEffect.h"
40#include "EqualizerEffect.h"
41#include "LoudnessEnhancerEffect.h"
42#include "NoiseSuppressionEffect.h"
43#include "PresetReverbEffect.h"
44#include "VirtualizerEffect.h"
45#include "VisualizerEffect.h"
46
47namespace android {
48namespace hardware {
49namespace audio {
50namespace effect {
51namespace V2_0 {
52namespace implementation {
53
54// static
55sp<IEffect> EffectsFactory::dispatchEffectInstanceCreation(
56 const effect_descriptor_t& halDescriptor, effect_handle_t handle) {
57 const effect_uuid_t *halUuid = &halDescriptor.type;
58 if (memcmp(halUuid, FX_IID_AEC, sizeof(effect_uuid_t)) == 0) {
59 return new AcousticEchoCancelerEffect(handle);
60 } else if (memcmp(halUuid, FX_IID_AGC, sizeof(effect_uuid_t)) == 0) {
61 return new AutomaticGainControlEffect(handle);
62 } else if (memcmp(halUuid, SL_IID_BASSBOOST, sizeof(effect_uuid_t)) == 0) {
63 return new BassBoostEffect(handle);
64 } else if (memcmp(halUuid, EFFECT_UIID_DOWNMIX, sizeof(effect_uuid_t)) == 0) {
65 return new DownmixEffect(handle);
66 } else if (memcmp(halUuid, SL_IID_ENVIRONMENTALREVERB, sizeof(effect_uuid_t)) == 0) {
67 return new EnvironmentalReverbEffect(handle);
68 } else if (memcmp(halUuid, SL_IID_EQUALIZER, sizeof(effect_uuid_t)) == 0) {
69 return new EqualizerEffect(handle);
70 } else if (memcmp(halUuid, FX_IID_LOUDNESS_ENHANCER, sizeof(effect_uuid_t)) == 0) {
71 return new LoudnessEnhancerEffect(handle);
72 } else if (memcmp(halUuid, FX_IID_NS, sizeof(effect_uuid_t)) == 0) {
73 return new NoiseSuppressionEffect(handle);
74 } else if (memcmp(halUuid, SL_IID_PRESETREVERB, sizeof(effect_uuid_t)) == 0) {
75 return new PresetReverbEffect(handle);
76 } else if (memcmp(halUuid, SL_IID_VIRTUALIZER, sizeof(effect_uuid_t)) == 0) {
77 return new VirtualizerEffect(handle);
78 } else if (memcmp(halUuid, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) {
79 return new VisualizerEffect(handle);
80 }
81 return new Effect(handle);
82}
83
84// Methods from ::android::hardware::audio::effect::V2_0::IEffectsFactory follow.
85Return<void> EffectsFactory::getAllDescriptors(getAllDescriptors_cb _hidl_cb) {
86 Result retval(Result::OK);
87 hidl_vec<EffectDescriptor> result;
88 uint32_t numEffects;
89 status_t status;
90
91restart:
92 numEffects = 0;
93 status = EffectQueryNumberEffects(&numEffects);
94 if (status != OK) {
95 retval = Result::NOT_INITIALIZED;
96 ALOGW("Error querying number of effects: %s", strerror(-status));
97 goto exit;
98 }
99 result.resize(numEffects);
100 for (uint32_t i = 0; i < numEffects; ++i) {
101 effect_descriptor_t halDescriptor;
102 status = EffectQueryEffect(i, &halDescriptor);
103 if (status == OK) {
104 effectDescriptorFromHal(halDescriptor, &result[i]);
105 } else {
106 ALOGW("Error querying effect at position %d / %d: %s",
107 i, numEffects, strerror(-status));
108 switch (status) {
109 case -ENOSYS: {
110 // Effect list has changed.
111 goto restart;
112 }
113 case -ENOENT: {
114 // No more effects available.
115 result.resize(i);
116 }
117 default: {
118 result.resize(0);
119 retval = Result::NOT_INITIALIZED;
120 }
121 }
122 break;
123 }
124 }
125
126exit:
127 _hidl_cb(retval, result);
128 return Void();
129}
130
131Return<void> EffectsFactory::getDescriptor(const Uuid& uid, getDescriptor_cb _hidl_cb) {
132 effect_uuid_t halUuid;
133 uuidToHal(uid, &halUuid);
134 effect_descriptor_t halDescriptor;
135 status_t status = EffectGetDescriptor(&halUuid, &halDescriptor);
136 EffectDescriptor descriptor;
137 effectDescriptorFromHal(halDescriptor, &descriptor);
138 Result retval(Result::OK);
139 if (status != OK) {
140 ALOGW("Error querying effect descriptor for %s: %s",
141 uuidToString(halUuid).c_str(), strerror(-status));
142 if (status == -ENOENT) {
143 retval = Result::INVALID_ARGUMENTS;
144 } else {
145 retval = Result::NOT_INITIALIZED;
146 }
147 }
148 _hidl_cb(retval, descriptor);
149 return Void();
150}
151
152Return<void> EffectsFactory::createEffect(
153 const Uuid& uid, int32_t session, int32_t ioHandle, createEffect_cb _hidl_cb) {
154 effect_uuid_t halUuid;
155 uuidToHal(uid, &halUuid);
156 effect_handle_t handle;
157 Result retval(Result::OK);
158 status_t status = EffectCreate(&halUuid, session, ioHandle, &handle);
159 sp<IEffect> effect;
160 if (status == OK) {
161 effect_descriptor_t halDescriptor;
162 memset(&halDescriptor, 0, sizeof(effect_descriptor_t));
163 status = (*handle)->get_descriptor(handle, &halDescriptor);
164 if (status == OK) {
165 effect = dispatchEffectInstanceCreation(halDescriptor, handle);
166 }
167 }
168 if (status != OK) {
169 ALOGW("Error creating effect %s: %s", uuidToString(halUuid).c_str(), strerror(-status));
170 if (status == -ENOENT) {
171 retval = Result::INVALID_ARGUMENTS;
172 } else {
173 retval = Result::NOT_INITIALIZED;
174 }
175 }
176 _hidl_cb(retval, effect);
177 return Void();
178}
179
180Return<void> EffectsFactory::debugDump(const native_handle_t* fd) {
181 if (fd->numFds == 1) {
182 EffectDumpEffects(fd->data[0]);
183 }
184 return Void();
185}
186
187
188IEffectsFactory* HIDL_FETCH_IEffectsFactory(const char* /* name */) {
189 return new EffectsFactory();
190}
191
192} // namespace implementation
193} // namespace V2_0
194} // namespace effect
195} // namespace audio
196} // namespace hardware
197} // namespace android