Home | History | Annotate | Download | only in impl
      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 #define LOG_TAG "EffectsFactoryHalHidl"
     18 //#define LOG_NDEBUG 0
     19 
     20 #include <cutils/native_handle.h>
     21 
     22 #include "EffectsFactoryHalHidl.h"
     23 #include "ConversionHelperHidl.h"
     24 #include "EffectBufferHalHidl.h"
     25 #include "EffectHalHidl.h"
     26 #include "HidlUtils.h"
     27 
     28 using ::android::hardware::audio::common::CPP_VERSION::implementation::HidlUtils;
     29 using ::android::hardware::Return;
     30 
     31 namespace android {
     32 namespace effect {
     33 namespace CPP_VERSION {
     34 
     35 using namespace ::android::hardware::audio::common::CPP_VERSION;
     36 using namespace ::android::hardware::audio::effect::CPP_VERSION;
     37 
     38 EffectsFactoryHalHidl::EffectsFactoryHalHidl() : ConversionHelperHidl("EffectsFactory") {
     39     mEffectsFactory = IEffectsFactory::getService();
     40     if (mEffectsFactory == 0) {
     41         ALOGE("Failed to obtain IEffectsFactory service, terminating process.");
     42         exit(1);
     43     }
     44 }
     45 
     46 status_t EffectsFactoryHalHidl::queryAllDescriptors() {
     47     if (mEffectsFactory == 0) return NO_INIT;
     48     Result retval = Result::NOT_INITIALIZED;
     49     Return<void> ret = mEffectsFactory->getAllDescriptors(
     50             [&](Result r, const hidl_vec<EffectDescriptor>& result) {
     51                 retval = r;
     52                 if (retval == Result::OK) {
     53                     mLastDescriptors = result;
     54                 }
     55             });
     56     if (ret.isOk()) {
     57         return retval == Result::OK ? OK : NO_INIT;
     58     }
     59     mLastDescriptors.resize(0);
     60     return processReturn(__FUNCTION__, ret);
     61 }
     62 
     63 status_t EffectsFactoryHalHidl::queryNumberEffects(uint32_t *pNumEffects) {
     64     status_t queryResult = queryAllDescriptors();
     65     if (queryResult == OK) {
     66         *pNumEffects = mLastDescriptors.size();
     67     }
     68     return queryResult;
     69 }
     70 
     71 status_t EffectsFactoryHalHidl::getDescriptor(
     72         uint32_t index, effect_descriptor_t *pDescriptor) {
     73     // TODO: We need somehow to track the changes on the server side
     74     // or figure out how to convert everybody to query all the descriptors at once.
     75     // TODO: check for nullptr
     76     if (mLastDescriptors.size() == 0) {
     77         status_t queryResult = queryAllDescriptors();
     78         if (queryResult != OK) return queryResult;
     79     }
     80     if (index >= mLastDescriptors.size()) return NAME_NOT_FOUND;
     81     EffectHalHidl::effectDescriptorToHal(mLastDescriptors[index], pDescriptor);
     82     return OK;
     83 }
     84 
     85 status_t EffectsFactoryHalHidl::getDescriptor(
     86         const effect_uuid_t *pEffectUuid, effect_descriptor_t *pDescriptor) {
     87     // TODO: check for nullptr
     88     if (mEffectsFactory == 0) return NO_INIT;
     89     Uuid hidlUuid;
     90     HidlUtils::uuidFromHal(*pEffectUuid, &hidlUuid);
     91     Result retval = Result::NOT_INITIALIZED;
     92     Return<void> ret = mEffectsFactory->getDescriptor(hidlUuid,
     93             [&](Result r, const EffectDescriptor& result) {
     94                 retval = r;
     95                 if (retval == Result::OK) {
     96                     EffectHalHidl::effectDescriptorToHal(result, pDescriptor);
     97                 }
     98             });
     99     if (ret.isOk()) {
    100         if (retval == Result::OK) return OK;
    101         else if (retval == Result::INVALID_ARGUMENTS) return NAME_NOT_FOUND;
    102         else return NO_INIT;
    103     }
    104     return processReturn(__FUNCTION__, ret);
    105 }
    106 
    107 status_t EffectsFactoryHalHidl::createEffect(
    108         const effect_uuid_t *pEffectUuid, int32_t sessionId, int32_t ioId,
    109         sp<EffectHalInterface> *effect) {
    110     if (mEffectsFactory == 0) return NO_INIT;
    111     Uuid hidlUuid;
    112     HidlUtils::uuidFromHal(*pEffectUuid, &hidlUuid);
    113     Result retval = Result::NOT_INITIALIZED;
    114     Return<void> ret = mEffectsFactory->createEffect(
    115             hidlUuid, sessionId, ioId,
    116             [&](Result r, const sp<IEffect>& result, uint64_t effectId) {
    117                 retval = r;
    118                 if (retval == Result::OK) {
    119                     *effect = new EffectHalHidl(result, effectId);
    120                 }
    121             });
    122     if (ret.isOk()) {
    123         if (retval == Result::OK) return OK;
    124         else if (retval == Result::INVALID_ARGUMENTS) return NAME_NOT_FOUND;
    125         else return NO_INIT;
    126     }
    127     return processReturn(__FUNCTION__, ret);
    128 }
    129 
    130 status_t EffectsFactoryHalHidl::dumpEffects(int fd) {
    131     if (mEffectsFactory == 0) return NO_INIT;
    132     native_handle_t* hidlHandle = native_handle_create(1, 0);
    133     hidlHandle->data[0] = fd;
    134     Return<void> ret = mEffectsFactory->debug(hidlHandle, {} /* options */);
    135     native_handle_delete(hidlHandle);
    136     return processReturn(__FUNCTION__, ret);
    137 }
    138 
    139 status_t EffectsFactoryHalHidl::allocateBuffer(size_t size, sp<EffectBufferHalInterface>* buffer) {
    140     return EffectBufferHalHidl::allocate(size, buffer);
    141 }
    142 
    143 status_t EffectsFactoryHalHidl::mirrorBuffer(void* external, size_t size,
    144                           sp<EffectBufferHalInterface>* buffer) {
    145     return EffectBufferHalHidl::mirror(external, size, buffer);
    146 }
    147 
    148 } // namespace CPP_VERSION
    149 } // namespace effect
    150 } // namespace android
    151