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