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