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