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