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