1 /*
2 * Copyright (c) 2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 #include <securec.h>
16 #include <string.h>
17 #include "effect_core.h"
18 #include "effect_host_common.h"
19 #include "v1_0/effect_types_vdi.h"
20 #include "v1_0/effect_factory.h"
21 #include "osal_mem.h"
22 #include "parse_effect_config.h"
23 #include "audio_uhdf_log.h"
24
25 #define AUDIO_EFFECT_CONFIG HDF_CONFIG_DIR"/audio_effect.json"
26 #define HDF_LOG_TAG HDF_AUDIO_EFFECT
27 struct ConfigDescriptor *g_cfgDescs = NULL;
28
EffectModelIsSupplyEffectLibs(struct IEffectModel * self,bool * supply)29 static int32_t EffectModelIsSupplyEffectLibs(struct IEffectModel *self, bool *supply)
30 {
31 if (self == NULL || supply == NULL) {
32 HDF_LOGE("%{public}s: invailid input params", __func__);
33 return HDF_ERR_INVALID_PARAM;
34 }
35
36 *supply = IsEffectLibExist();
37 return HDF_SUCCESS;
38 }
39
EffectModelGetAllEffectDescriptors(struct IEffectModel * self,struct EffectControllerDescriptor * descs,uint32_t * descsLen)40 static int32_t EffectModelGetAllEffectDescriptors(struct IEffectModel *self,
41 struct EffectControllerDescriptor *descs, uint32_t *descsLen)
42 {
43 HDF_LOGD("enter to %{public}s", __func__);
44 int32_t ret;
45 uint32_t i;
46 uint32_t descNum = 0;
47 struct EffectFactory *factLib = NULL;
48
49 if (self == NULL || descs == NULL || descsLen == NULL) {
50 HDF_LOGE("%{public}s: invailid input params", __func__);
51 return HDF_ERR_INVALID_PARAM;
52 }
53
54 if (g_cfgDescs == NULL) {
55 HDF_LOGE("%{public}s: point is null!", __func__);
56 return HDF_FAILURE;
57 }
58 struct EffectControllerDescriptorVdi *descsVdi = (struct EffectControllerDescriptorVdi *)descs;
59 for (i = 0; i < g_cfgDescs->effectNum; i++) {
60 factLib = GetEffectLibFromList(g_cfgDescs->effectCfgDescs[i].library);
61 if (factLib == NULL) {
62 HDF_LOGE("%{public}s: GetEffectLibFromList fail!", __func__);
63 continue;
64 }
65 ret = factLib->GetDescriptor(factLib, g_cfgDescs->effectCfgDescs[i].effectId, &descsVdi[descNum]);
66 if (ret != HDF_SUCCESS) {
67 HDF_LOGE("%{public}s: GetDescriptor fail!", __func__);
68 continue;
69 }
70 descNum++;
71 }
72 *descsLen = descNum;
73 descs = (struct EffectControllerDescriptor *)descsVdi;
74 HDF_LOGD("%{public}s success", __func__);
75 return HDF_SUCCESS;
76 }
77
EffectModelGetEffectDescriptor(struct IEffectModel * self,const char * uuid,struct EffectControllerDescriptor * desc)78 static int32_t EffectModelGetEffectDescriptor(struct IEffectModel *self, const char *uuid,
79 struct EffectControllerDescriptor *desc)
80 {
81 HDF_LOGD("enter to %{public}s", __func__);
82 uint32_t i;
83 struct EffectFactory *factLib = NULL;
84 if (self == NULL || uuid == NULL || desc == NULL) {
85 HDF_LOGE("%{public}s: invailid input params", __func__);
86 return HDF_ERR_INVALID_PARAM;
87 }
88 struct EffectControllerDescriptorVdi *descVdi = (struct EffectControllerDescriptorVdi *)desc;
89 for (i = 0; i < g_cfgDescs->effectNum; i++) {
90 if (strcmp(uuid, g_cfgDescs->effectCfgDescs[i].effectId) != 0) {
91 continue;
92 }
93
94 factLib = GetEffectLibFromList(g_cfgDescs->effectCfgDescs[i].library);
95 if (factLib == NULL) {
96 HDF_LOGE("%{public}s: GetEffectLibFromList fail!", __func__);
97 return HDF_FAILURE;
98 }
99
100 if (factLib->GetDescriptor(factLib, uuid, descVdi) != HDF_SUCCESS) {
101 HDF_LOGE("%{public}s: GetDescriptor fail!", __func__);
102 return HDF_FAILURE;
103 }
104 HDF_LOGD("%{public}s success", __func__);
105 return HDF_SUCCESS;
106 }
107 desc = (struct EffectControllerDescriptor *)descVdi;
108 HDF_LOGE("%{public}s fail!", __func__);
109 return HDF_FAILURE;
110 }
111
EffectModelCreateEffectController(struct IEffectModel * self,const struct EffectInfo * info,struct IEffectControl ** contoller,struct ControllerId * contollerId)112 static int32_t EffectModelCreateEffectController(struct IEffectModel *self, const struct EffectInfo *info,
113 struct IEffectControl **contoller, struct ControllerId *contollerId)
114 {
115 if (self == NULL || info == NULL || contoller == NULL || contollerId == NULL) {
116 HDF_LOGE("%{public}s: invailid input params", __func__);
117 return HDF_ERR_INVALID_PARAM;
118 }
119
120 struct EffectFactory *lib = NULL;
121 struct ControllerManager *ctrlMgr = NULL;
122 struct IEffectControlVdi *ctrlOps = NULL;
123
124 lib = GetEffectLibFromList(info->libName);
125 if (lib == NULL) {
126 HDF_LOGE("%{public}s: not match any lib", __func__);
127 return HDF_FAILURE;
128 }
129
130 if (lib->CreateController == NULL) {
131 HDF_LOGE("%{public}s: lib has no create method", __func__);
132 return HDF_FAILURE;
133 }
134
135 struct EffectInfoVdi *infoVdi = (struct EffectInfoVdi *)info;
136 lib->CreateController(lib, infoVdi, &ctrlOps);
137 if (ctrlOps == NULL) {
138 HDF_LOGE("%{public}s: lib create controller failed.", __func__);
139 return HDF_FAILURE;
140 }
141
142 /* ctrlMgr mark it and using it in release process */
143 ctrlMgr = (struct ControllerManager *)OsalMemCalloc(sizeof(struct ControllerManager));
144 if (ctrlMgr == NULL) {
145 HDF_LOGE("%{public}s: malloc ControllerManager obj failed!", __func__);
146 return HDF_FAILURE;
147 }
148
149 ctrlMgr->ctrlOps = ctrlOps;
150 ctrlMgr->effectId = strdup(info->effectId);
151 ctrlMgr->ctrlImpls.EffectProcess = EffectControlEffectProcess;
152 ctrlMgr->ctrlImpls.SendCommand = EffectControlSendCommand;
153 ctrlMgr->ctrlImpls.GetEffectDescriptor = EffectGetOwnDescriptor;
154 ctrlMgr->ctrlImpls.EffectReverse = EffectControlEffectReverse;
155 *contoller = &ctrlMgr->ctrlImpls;
156 if (RegisterControllerToList(ctrlMgr) != HDF_SUCCESS) {
157 HDF_LOGE("%{public}s: register ctroller to list failed.", __func__);
158 OsalMemFree(ctrlMgr);
159 return HDF_FAILURE;
160 }
161
162 // free after send reply
163 contollerId->libName = strdup(info->libName);
164 contollerId->effectId = strdup(info->effectId);
165 return HDF_SUCCESS;
166 }
167
EffectModelDestroyEffectController(struct IEffectModel * self,const struct ControllerId * contollerId)168 int32_t EffectModelDestroyEffectController(struct IEffectModel *self, const struct ControllerId *contollerId)
169 {
170 if (self == NULL || contollerId == NULL) {
171 HDF_LOGE("%{public}s: invailid input params", __func__);
172 return HDF_ERR_INVALID_PARAM;
173 }
174 struct EffectFactory *lib = NULL;
175 struct ControllerManager *ctrlMgr = NULL;
176
177 lib = GetEffectLibFromList(contollerId->libName);
178 if (lib == NULL) {
179 HDF_LOGE("%{public}s: not match any lib", __func__);
180 return HDF_FAILURE;
181 }
182
183 ctrlMgr = GetControllerFromList(contollerId->effectId);
184 if (ctrlMgr == NULL) {
185 HDF_LOGE("%{public}s: controller manager not found", __func__);
186 return HDF_FAILURE;
187 }
188
189 if (ctrlMgr->ctrlOps == NULL) {
190 HDF_LOGE("%{public}s: controller has no options", __func__);
191 OsalMemFree(ctrlMgr);
192 ctrlMgr = NULL;
193 return HDF_FAILURE;
194 }
195
196 if (ctrlMgr->effectId != NULL) {
197 OsalMemFree(ctrlMgr->effectId);
198 ctrlMgr->effectId = NULL;
199 }
200
201 /* call the lib destroy method,then free controller manager */
202 lib->DestroyController(lib, ctrlMgr->ctrlOps);
203 OsalMemFree(ctrlMgr);
204 ctrlMgr = NULL;
205
206 return HDF_SUCCESS;
207 }
208
RegLibraryInstByName(char * libPath)209 static int32_t RegLibraryInstByName(char *libPath)
210 {
211 struct EffectFactory *factLib = NULL;
212 struct EffectFactory *(*GetFactoryLib)(void);
213 void *libHandle = NULL;
214 if (libPath == NULL) {
215 HDF_LOGE("%{public}s: invalid input param", __func__);
216 return HDF_FAILURE;
217 }
218
219 char pathBuf[PATH_MAX] = {'\0'};
220 if (realpath(libPath, pathBuf) == NULL) {
221 HDF_LOGE("%{public}s: path conversion failed", __func__);
222 return HDF_FAILURE;
223 }
224
225 if (strncmp(HDF_LIBRARY_DIR, pathBuf, strlen(HDF_LIBRARY_DIR)) != 0) {
226 HDF_LOGE("%{public}s: The file path is incorrect", __func__);
227 return HDF_FAILURE;
228 }
229
230 libHandle = dlopen(pathBuf, RTLD_LAZY);
231 if (libHandle == NULL) {
232 HDF_LOGE("%{public}s: open so failed, reason:%{public}s", __func__, dlerror());
233 return HDF_FAILURE;
234 }
235
236 GetFactoryLib = dlsym(libHandle, "GetEffectoyFactoryLib");
237 factLib = GetFactoryLib();
238 if (factLib == NULL) {
239 HDF_LOGE("%{public}s: get fact lib failed %{public}s", __func__, dlerror());
240 dlclose(libHandle);
241 return HDF_FAILURE;
242 }
243
244 if (RegisterEffectLibToList(libHandle, factLib) != HDF_SUCCESS) {
245 HDF_LOGE("%{public}s: register lib to list failed", __func__);
246 dlclose(libHandle);
247 return HDF_FAILURE;
248 }
249 return HDF_SUCCESS;
250 }
251
RegLibraryInst(struct LibraryConfigDescriptor ** libCfgDescs,const uint32_t libNum)252 static int32_t RegLibraryInst(struct LibraryConfigDescriptor **libCfgDescs, const uint32_t libNum)
253 {
254 int32_t ret;
255 uint32_t i;
256 char path[PATH_MAX];
257 char pathBuf[PATH_MAX];
258 if (libCfgDescs == NULL || libNum == 0 || libNum > HDF_EFFECT_LIB_NUM_MAX) {
259 HDF_LOGE("Invalid parameter!");
260 return HDF_ERR_INVALID_PARAM;
261 }
262
263 for (i = 0; i < libNum; i++) {
264 #if (defined(__aarch64__) || defined(__x86_64__))
265 ret = snprintf_s(path, PATH_MAX, PATH_MAX, "/vendor/lib64/%s.z.so", (*libCfgDescs)[i].libPath);
266 #else
267 ret = snprintf_s(path, PATH_MAX, PATH_MAX, "/vendor/lib/%s.z.so", (*libCfgDescs)[i].libPath);
268 #endif
269 if (ret < 0) {
270 HDF_LOGE("%{public}s: get libPath failed", __func__);
271 continue;
272 }
273
274 if (realpath(path, pathBuf) == NULL) {
275 HDF_LOGE("%{public}s: realpath is null! [%{public}d]", __func__, errno);
276 continue;
277 }
278
279 if (RegLibraryInstByName(path) != HDF_SUCCESS) {
280 HDF_LOGE("%{public}s: regist library[%{private}s] failed", __func__, path);
281 }
282 }
283 return HDF_SUCCESS;
284 }
285
ModelInit(void)286 void ModelInit(void)
287 {
288 struct ConfigDescriptor *cfgDesc = NULL;
289 if (AudioEffectGetConfigDescriptor(AUDIO_EFFECT_CONFIG, &cfgDesc) != HDF_SUCCESS) {
290 HDF_LOGE("%{public}s: AudioEffectGetConfigDescriptor fail!", __func__);
291 return;
292 }
293
294 if (cfgDesc == NULL || cfgDesc->effectCfgDescs == NULL || cfgDesc->libCfgDescs == NULL) {
295 HDF_LOGE("cfgDesc is null!");
296 return;
297 }
298
299 g_cfgDescs = cfgDesc;
300 if (RegLibraryInst(&(cfgDesc->libCfgDescs), cfgDesc->libNum) != HDF_SUCCESS) {
301 HDF_LOGE("%{public}s: RegLibraryInst failed", __func__);
302 AudioEffectReleaseCfgDesc(cfgDesc);
303 return;
304 }
305
306 HDF_LOGD("%{public}s end!", __func__);
307 }
308
EffectModelImplGetInstance(void)309 struct IEffectModel *EffectModelImplGetInstance(void)
310 {
311 struct EffectModelService *service = (struct EffectModelService *)OsalMemCalloc(sizeof(struct EffectModelService));
312 if (service == NULL) {
313 HDF_LOGE("%{public}s: malloc EffectModelService obj failed!", __func__);
314 return NULL;
315 }
316
317 ModelInit();
318 service->interface.IsSupplyEffectLibs = EffectModelIsSupplyEffectLibs;
319 service->interface.GetAllEffectDescriptors = EffectModelGetAllEffectDescriptors;
320 service->interface.CreateEffectController = EffectModelCreateEffectController;
321 service->interface.DestroyEffectController = EffectModelDestroyEffectController;
322 service->interface.GetEffectDescriptor = EffectModelGetEffectDescriptor;
323
324 return &service->interface;
325 }
326
EffectModelImplRelease(struct IEffectModel * instance)327 void EffectModelImplRelease(struct IEffectModel *instance)
328 {
329 if (instance == NULL) {
330 return;
331 }
332
333 AudioEffectReleaseCfgDesc(g_cfgDescs);
334 ReleaseLibFromList();
335 OsalMemFree(instance);
336 }
337