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