Mikhail Naganov | 7cbf2f1 | 2016-10-27 20:05:35 -0700 | [diff] [blame] | 1 | /* |
Kevin Rocard | 96d2cd9 | 2018-11-14 16:22:07 -0800 | [diff] [blame] | 2 | * Copyright (C) 2018 The Android Open Source Project |
Mikhail Naganov | 7cbf2f1 | 2016-10-27 20:05:35 -0700 | [diff] [blame] | 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 | |
Kevin Rocard | 96d2cd9 | 2018-11-14 16:22:07 -0800 | [diff] [blame] | 17 | #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 | |
| 35 | using ::android::hardware::audio::common::CPP_VERSION::HidlUtils; |
Kevin Rocard | 62588b6 | 2017-12-20 11:07:12 -0800 | [diff] [blame] | 36 | |
Kevin Rocard | 22505e6 | 2017-12-14 18:50:12 -0800 | [diff] [blame] | 37 | #include <android/log.h> |
Mikhail Naganov | 7cbf2f1 | 2016-10-27 20:05:35 -0700 | [diff] [blame] | 38 | #include <media/EffectsFactoryApi.h> |
| 39 | #include <system/audio_effects/effect_aec.h> |
| 40 | #include <system/audio_effects/effect_agc.h> |
| 41 | #include <system/audio_effects/effect_bassboost.h> |
| 42 | #include <system/audio_effects/effect_downmix.h> |
| 43 | #include <system/audio_effects/effect_environmentalreverb.h> |
| 44 | #include <system/audio_effects/effect_equalizer.h> |
| 45 | #include <system/audio_effects/effect_loudnessenhancer.h> |
| 46 | #include <system/audio_effects/effect_ns.h> |
| 47 | #include <system/audio_effects/effect_presetreverb.h> |
| 48 | #include <system/audio_effects/effect_virtualizer.h> |
| 49 | #include <system/audio_effects/effect_visualizer.h> |
Mikhail Naganov | 7cbf2f1 | 2016-10-27 20:05:35 -0700 | [diff] [blame] | 50 | |
Kevin Rocard | 96d2cd9 | 2018-11-14 16:22:07 -0800 | [diff] [blame] | 51 | using ::android::hardware::audio::common::CPP_VERSION::HidlUtils; |
Mikhail Naganov | 7cbf2f1 | 2016-10-27 20:05:35 -0700 | [diff] [blame] | 52 | |
| 53 | namespace android { |
| 54 | namespace hardware { |
| 55 | namespace audio { |
| 56 | namespace effect { |
Kevin Rocard | 96d2cd9 | 2018-11-14 16:22:07 -0800 | [diff] [blame] | 57 | namespace CPP_VERSION { |
Mikhail Naganov | 7cbf2f1 | 2016-10-27 20:05:35 -0700 | [diff] [blame] | 58 | namespace implementation { |
| 59 | |
| 60 | // static |
Kevin Rocard | 22505e6 | 2017-12-14 18:50:12 -0800 | [diff] [blame] | 61 | sp<IEffect> EffectsFactory::dispatchEffectInstanceCreation(const effect_descriptor_t& halDescriptor, |
| 62 | effect_handle_t handle) { |
| 63 | const effect_uuid_t* halUuid = &halDescriptor.type; |
Mikhail Naganov | 7cbf2f1 | 2016-10-27 20:05:35 -0700 | [diff] [blame] | 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 | return new Effect(handle); |
| 88 | } |
| 89 | |
Kevin Rocard | 96d2cd9 | 2018-11-14 16:22:07 -0800 | [diff] [blame] | 90 | // Methods from ::android::hardware::audio::effect::CPP_VERSION::IEffectsFactory follow. |
Kevin Rocard | 22505e6 | 2017-12-14 18:50:12 -0800 | [diff] [blame] | 91 | Return<void> EffectsFactory::getAllDescriptors(getAllDescriptors_cb _hidl_cb) { |
Mikhail Naganov | 7cbf2f1 | 2016-10-27 20:05:35 -0700 | [diff] [blame] | 92 | Result retval(Result::OK); |
| 93 | hidl_vec<EffectDescriptor> result; |
| 94 | uint32_t numEffects; |
| 95 | status_t status; |
| 96 | |
| 97 | restart: |
| 98 | numEffects = 0; |
| 99 | status = EffectQueryNumberEffects(&numEffects); |
| 100 | if (status != OK) { |
| 101 | retval = Result::NOT_INITIALIZED; |
Mikhail Naganov | 9f28904 | 2017-02-23 08:39:36 -0800 | [diff] [blame] | 102 | ALOGE("Error querying number of effects: %s", strerror(-status)); |
Mikhail Naganov | 7cbf2f1 | 2016-10-27 20:05:35 -0700 | [diff] [blame] | 103 | goto exit; |
| 104 | } |
| 105 | result.resize(numEffects); |
| 106 | for (uint32_t i = 0; i < numEffects; ++i) { |
| 107 | effect_descriptor_t halDescriptor; |
| 108 | status = EffectQueryEffect(i, &halDescriptor); |
| 109 | if (status == OK) { |
| 110 | effectDescriptorFromHal(halDescriptor, &result[i]); |
| 111 | } else { |
Kevin Rocard | 22505e6 | 2017-12-14 18:50:12 -0800 | [diff] [blame] | 112 | ALOGE("Error querying effect at position %d / %d: %s", i, numEffects, |
| 113 | strerror(-status)); |
Mikhail Naganov | 7cbf2f1 | 2016-10-27 20:05:35 -0700 | [diff] [blame] | 114 | switch (status) { |
| 115 | case -ENOSYS: { |
| 116 | // Effect list has changed. |
| 117 | goto restart; |
| 118 | } |
| 119 | case -ENOENT: { |
| 120 | // No more effects available. |
| 121 | result.resize(i); |
Mikhail Naganov | 40e36fa | 2018-10-10 14:40:54 -0700 | [diff] [blame] | 122 | break; |
Mikhail Naganov | 7cbf2f1 | 2016-10-27 20:05:35 -0700 | [diff] [blame] | 123 | } |
| 124 | default: { |
| 125 | result.resize(0); |
| 126 | retval = Result::NOT_INITIALIZED; |
| 127 | } |
| 128 | } |
| 129 | break; |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | exit: |
| 134 | _hidl_cb(retval, result); |
| 135 | return Void(); |
| 136 | } |
| 137 | |
Kevin Rocard | 22505e6 | 2017-12-14 18:50:12 -0800 | [diff] [blame] | 138 | Return<void> EffectsFactory::getDescriptor(const Uuid& uid, getDescriptor_cb _hidl_cb) { |
Mikhail Naganov | 7cbf2f1 | 2016-10-27 20:05:35 -0700 | [diff] [blame] | 139 | effect_uuid_t halUuid; |
Mikhail Naganov | 6e81e9b | 2016-11-16 16:30:17 -0800 | [diff] [blame] | 140 | HidlUtils::uuidToHal(uid, &halUuid); |
Mikhail Naganov | 7cbf2f1 | 2016-10-27 20:05:35 -0700 | [diff] [blame] | 141 | effect_descriptor_t halDescriptor; |
| 142 | status_t status = EffectGetDescriptor(&halUuid, &halDescriptor); |
| 143 | EffectDescriptor descriptor; |
| 144 | effectDescriptorFromHal(halDescriptor, &descriptor); |
| 145 | Result retval(Result::OK); |
| 146 | if (status != OK) { |
Kevin Rocard | 22505e6 | 2017-12-14 18:50:12 -0800 | [diff] [blame] | 147 | ALOGE("Error querying effect descriptor for %s: %s", uuidToString(halUuid).c_str(), |
| 148 | strerror(-status)); |
Mikhail Naganov | 7cbf2f1 | 2016-10-27 20:05:35 -0700 | [diff] [blame] | 149 | if (status == -ENOENT) { |
| 150 | retval = Result::INVALID_ARGUMENTS; |
| 151 | } else { |
| 152 | retval = Result::NOT_INITIALIZED; |
| 153 | } |
| 154 | } |
| 155 | _hidl_cb(retval, descriptor); |
| 156 | return Void(); |
| 157 | } |
| 158 | |
Kevin Rocard | 22505e6 | 2017-12-14 18:50:12 -0800 | [diff] [blame] | 159 | Return<void> EffectsFactory::createEffect(const Uuid& uid, int32_t session, int32_t ioHandle, |
| 160 | createEffect_cb _hidl_cb) { |
Mikhail Naganov | 7cbf2f1 | 2016-10-27 20:05:35 -0700 | [diff] [blame] | 161 | effect_uuid_t halUuid; |
Mikhail Naganov | 6e81e9b | 2016-11-16 16:30:17 -0800 | [diff] [blame] | 162 | HidlUtils::uuidToHal(uid, &halUuid); |
Mikhail Naganov | 7cbf2f1 | 2016-10-27 20:05:35 -0700 | [diff] [blame] | 163 | effect_handle_t handle; |
| 164 | Result retval(Result::OK); |
| 165 | status_t status = EffectCreate(&halUuid, session, ioHandle, &handle); |
| 166 | sp<IEffect> effect; |
Mikhail Naganov | 1054829 | 2016-10-31 10:39:47 -0700 | [diff] [blame] | 167 | uint64_t effectId = EffectMap::INVALID_ID; |
Mikhail Naganov | 7cbf2f1 | 2016-10-27 20:05:35 -0700 | [diff] [blame] | 168 | if (status == OK) { |
| 169 | effect_descriptor_t halDescriptor; |
| 170 | memset(&halDescriptor, 0, sizeof(effect_descriptor_t)); |
| 171 | status = (*handle)->get_descriptor(handle, &halDescriptor); |
| 172 | if (status == OK) { |
| 173 | effect = dispatchEffectInstanceCreation(halDescriptor, handle); |
Mikhail Naganov | 1054829 | 2016-10-31 10:39:47 -0700 | [diff] [blame] | 174 | effectId = EffectMap::getInstance().add(handle); |
| 175 | } else { |
Kevin Rocard | 22505e6 | 2017-12-14 18:50:12 -0800 | [diff] [blame] | 176 | ALOGE("Error querying effect descriptor for %s: %s", uuidToString(halUuid).c_str(), |
| 177 | strerror(-status)); |
Mikhail Naganov | 1054829 | 2016-10-31 10:39:47 -0700 | [diff] [blame] | 178 | EffectRelease(handle); |
Mikhail Naganov | 7cbf2f1 | 2016-10-27 20:05:35 -0700 | [diff] [blame] | 179 | } |
| 180 | } |
| 181 | if (status != OK) { |
Mikhail Naganov | 9f28904 | 2017-02-23 08:39:36 -0800 | [diff] [blame] | 182 | ALOGE("Error creating effect %s: %s", uuidToString(halUuid).c_str(), strerror(-status)); |
Mikhail Naganov | 7cbf2f1 | 2016-10-27 20:05:35 -0700 | [diff] [blame] | 183 | if (status == -ENOENT) { |
| 184 | retval = Result::INVALID_ARGUMENTS; |
| 185 | } else { |
| 186 | retval = Result::NOT_INITIALIZED; |
| 187 | } |
| 188 | } |
Mikhail Naganov | 1054829 | 2016-10-31 10:39:47 -0700 | [diff] [blame] | 189 | _hidl_cb(retval, effect, effectId); |
Mikhail Naganov | 7cbf2f1 | 2016-10-27 20:05:35 -0700 | [diff] [blame] | 190 | return Void(); |
| 191 | } |
| 192 | |
Kevin Rocard | 22505e6 | 2017-12-14 18:50:12 -0800 | [diff] [blame] | 193 | Return<void> EffectsFactory::debugDump(const hidl_handle& fd) { |
Kevin Rocard | 3d41027 | 2018-04-19 17:52:32 -0700 | [diff] [blame] | 194 | return debug(fd, {} /* options */); |
| 195 | } |
| 196 | |
| 197 | Return<void> EffectsFactory::debug(const hidl_handle& fd, |
| 198 | const hidl_vec<hidl_string>& /* options */) { |
Mikhail Naganov | 7bae6a0 | 2017-04-24 10:44:08 -0700 | [diff] [blame] | 199 | if (fd.getNativeHandle() != nullptr && fd->numFds == 1) { |
Mikhail Naganov | 7cbf2f1 | 2016-10-27 20:05:35 -0700 | [diff] [blame] | 200 | EffectDumpEffects(fd->data[0]); |
| 201 | } |
| 202 | return Void(); |
| 203 | } |
| 204 | |
Mikhail Naganov | 6ffde44 | 2018-03-07 09:55:06 -0800 | [diff] [blame] | 205 | IEffectsFactory* HIDL_FETCH_IEffectsFactory(const char* name) { |
| 206 | return strcmp(name, "default") == 0 ? new EffectsFactory() : nullptr; |
Mikhail Naganov | 7cbf2f1 | 2016-10-27 20:05:35 -0700 | [diff] [blame] | 207 | } |
| 208 | |
Kevin Rocard | 22505e6 | 2017-12-14 18:50:12 -0800 | [diff] [blame] | 209 | } // namespace implementation |
Kevin Rocard | 96d2cd9 | 2018-11-14 16:22:07 -0800 | [diff] [blame] | 210 | } // namespace CPP_VERSION |
Mikhail Naganov | 7cbf2f1 | 2016-10-27 20:05:35 -0700 | [diff] [blame] | 211 | } // namespace effect |
| 212 | } // namespace audio |
| 213 | } // namespace hardware |
| 214 | } // namespace android |