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