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 if (pDescriptor == nullptr) {
77 return BAD_VALUE;
78 }
79 if (mLastDescriptors.size() == 0) {
80 status_t queryResult = queryAllDescriptors();
81 if (queryResult != OK) return queryResult;
82 }
83 if (index >= mLastDescriptors.size()) return NAME_NOT_FOUND;
84 EffectUtils::effectDescriptorToHal(mLastDescriptors[index], pDescriptor);
85 return OK;
86 }
87
getDescriptor(const effect_uuid_t * pEffectUuid,effect_descriptor_t * pDescriptor)88 status_t EffectsFactoryHalHidl::getDescriptor(
89 const effect_uuid_t *pEffectUuid, effect_descriptor_t *pDescriptor) {
90 if (pDescriptor == nullptr || pEffectUuid == nullptr) {
91 return BAD_VALUE;
92 }
93 if (mEffectsFactory == 0) return NO_INIT;
94 Uuid hidlUuid;
95 UuidUtils::uuidFromHal(*pEffectUuid, &hidlUuid);
96 Result retval = Result::NOT_INITIALIZED;
97 Return<void> ret = mEffectsFactory->getDescriptor(hidlUuid,
98 [&](Result r, const EffectDescriptor& result) {
99 retval = r;
100 if (retval == Result::OK) {
101 EffectUtils::effectDescriptorToHal(result, pDescriptor);
102 }
103 });
104 if (ret.isOk()) {
105 if (retval == Result::OK) return OK;
106 else if (retval == Result::INVALID_ARGUMENTS) return NAME_NOT_FOUND;
107 else return NO_INIT;
108 }
109 return processReturn(__FUNCTION__, ret);
110 }
111
getDescriptors(const effect_uuid_t * pEffectType,std::vector<effect_descriptor_t> * descriptors)112 status_t EffectsFactoryHalHidl::getDescriptors(const effect_uuid_t *pEffectType,
113 std::vector<effect_descriptor_t> *descriptors) {
114 if (pEffectType == nullptr || descriptors == nullptr) {
115 return BAD_VALUE;
116 }
117
118 uint32_t numEffects = 0;
119 status_t status = queryNumberEffects(&numEffects);
120 if (status != NO_ERROR) {
121 ALOGW("%s error %d from FactoryHal queryNumberEffects", __func__, status);
122 return status;
123 }
124
125 for (uint32_t i = 0; i < numEffects; i++) {
126 effect_descriptor_t descriptor;
127 status = getDescriptor(i, &descriptor);
128 if (status != NO_ERROR) {
129 ALOGW("%s error %d from FactoryHal getDescriptor", __func__, status);
130 continue;
131 }
132 if (memcmp(&descriptor.type, pEffectType, sizeof(effect_uuid_t)) == 0) {
133 descriptors->push_back(descriptor);
134 }
135 }
136 return descriptors->empty() ? NAME_NOT_FOUND : NO_ERROR;
137 }
138
createEffect(const effect_uuid_t * pEffectUuid,int32_t sessionId,int32_t ioId,int32_t deviceId __unused,sp<EffectHalInterface> * effect)139 status_t EffectsFactoryHalHidl::createEffect(
140 const effect_uuid_t *pEffectUuid, int32_t sessionId, int32_t ioId,
141 int32_t deviceId __unused, sp<EffectHalInterface> *effect) {
142 if (mEffectsFactory == 0) return NO_INIT;
143 Uuid hidlUuid;
144 UuidUtils::uuidFromHal(*pEffectUuid, &hidlUuid);
145 Result retval = Result::NOT_INITIALIZED;
146 Return<void> ret;
147 #if MAJOR_VERSION >= 6
148 ret = mEffectsFactory->createEffect(
149 hidlUuid, sessionId, ioId, deviceId,
150 [&](Result r, const sp<IEffect>& result, uint64_t effectId) {
151 retval = r;
152 if (retval == Result::OK) {
153 *effect = new EffectHalHidl(result, effectId);
154 }
155 });
156 #else
157 if (sessionId == AUDIO_SESSION_DEVICE && ioId == AUDIO_IO_HANDLE_NONE) {
158 return INVALID_OPERATION;
159 }
160 ret = mEffectsFactory->createEffect(
161 hidlUuid, sessionId, ioId,
162 [&](Result r, const sp<IEffect>& result, uint64_t effectId) {
163 retval = r;
164 if (retval == Result::OK) {
165 *effect = new EffectHalHidl(result, effectId);
166 }
167 });
168 #endif
169 if (ret.isOk()) {
170 if (retval == Result::OK) return OK;
171 else if (retval == Result::INVALID_ARGUMENTS) return NAME_NOT_FOUND;
172 else return NO_INIT;
173 }
174 return processReturn(__FUNCTION__, ret);
175 }
176
dumpEffects(int fd)177 status_t EffectsFactoryHalHidl::dumpEffects(int fd) {
178 if (mEffectsFactory == 0) return NO_INIT;
179 native_handle_t* hidlHandle = native_handle_create(1, 0);
180 hidlHandle->data[0] = fd;
181 Return<void> ret = mEffectsFactory->debug(hidlHandle, {} /* options */);
182 native_handle_delete(hidlHandle);
183
184 // TODO(b/111997867, b/177271958) Workaround - remove when fixed.
185 // A Binder transmitted fd may not close immediately due to a race condition b/111997867
186 // when the remote binder thread removes the last refcount to the fd blocks in the
187 // kernel for binder activity. We send a Binder ping() command to unblock the thread
188 // and complete the fd close / release.
189 //
190 // See DeviceHalHidl::dump(), EffectHalHidl::dump(), StreamHalHidl::dump(),
191 // EffectsFactoryHalHidl::dumpEffects().
192
193 (void)mEffectsFactory->ping(); // synchronous Binder call
194
195 return processReturn(__FUNCTION__, ret);
196 }
197
allocateBuffer(size_t size,sp<EffectBufferHalInterface> * buffer)198 status_t EffectsFactoryHalHidl::allocateBuffer(size_t size, sp<EffectBufferHalInterface>* buffer) {
199 return EffectBufferHalHidl::allocate(size, buffer);
200 }
201
mirrorBuffer(void * external,size_t size,sp<EffectBufferHalInterface> * buffer)202 status_t EffectsFactoryHalHidl::mirrorBuffer(void* external, size_t size,
203 sp<EffectBufferHalInterface>* buffer) {
204 return EffectBufferHalHidl::mirror(external, size, buffer);
205 }
206
207 } // namespace CPP_VERSION
208 } // namespace effect
209
createIEffectsFactory()210 extern "C" __attribute__((visibility("default"))) void* createIEffectsFactory() {
211 auto service = hardware::audio::effect::CPP_VERSION::IEffectsFactory::getService();
212 return service ? new effect::CPP_VERSION::EffectsFactoryHalHidl(service) : nullptr;
213 }
214
215 } // namespace android
216