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 <pthread.h>
18 #include "effect_core.h"
19 #include "effect_host_common.h"
20 #include "v1_0/effect_types_vdi.h"
21 #include "v1_0/effect_factory.h"
22 #include "osal_mem.h"
23 #include "parse_effect_config.h"
24 #include "audio_uhdf_log.h"
25
26 #define AUDIO_EFFECT_PLAFORM_CONFIG HDF_CONFIG_DIR"/audio_effect.json"
27 #define AUDIO_EFFECT_PRODUCT_CONFIG HDF_CHIP_PROD_CONFIG_DIR"/audio_effect.json"
28 #define HDF_LOG_TAG HDF_AUDIO_EFFECT
29 #define AUDIO_EFFECT_NUM_MAX 10
30 pthread_rwlock_t g_rwEffectLock = PTHREAD_RWLOCK_INITIALIZER;
31 struct ConfigDescriptor *g_cfgDescs = NULL;
32 struct AudioEffectLibInfo {
33 char *libName;
34 uint8_t *libHandle;
35 struct EffectFactory *libEffect;
36 struct ControllerManager *ctrlMgr;
37 int32_t effectCnt;
38 };
39 struct AudioEffectLibInfo *g_libInfos[AUDIO_EFFECT_NUM_MAX] = { NULL };
40
GetEffectLibInfoByName(const char * libName)41 static struct AudioEffectLibInfo* GetEffectLibInfoByName(const char *libName)
42 {
43 if (libName == NULL) {
44 HDF_LOGE("%{public}s: invailid input params", __func__);
45 return NULL;
46 }
47 struct AudioEffectLibInfo *libInfo = NULL;
48 for (int i = 0; i <= AUDIO_EFFECT_NUM_MAX; i++) {
49 if (i == AUDIO_EFFECT_NUM_MAX) {
50 HDF_LOGE("%{public}s: can not find %{public}s", __func__, libName);
51 return NULL;
52 }
53 if (g_libInfos[i] == NULL || strcmp(g_libInfos[i]->libName, libName) != 0) {
54 continue;
55 }
56 libInfo = g_libInfos[i];
57 break;
58 }
59 return libInfo;
60 }
61
LoadLibraryByName(const char * libName,uint8_t ** libHandle,struct EffectFactory ** factLib)62 static int32_t LoadLibraryByName(const char *libName, uint8_t **libHandle, struct EffectFactory **factLib)
63 {
64 if (libName == NULL || factLib == NULL || libHandle == NULL) {
65 HDF_LOGE("%{public}s: invailid input params", __func__);
66 return HDF_ERR_INVALID_PARAM;
67 }
68 int32_t ret = 0;
69 struct EffectFactory *(*getFactoryLib)(void);
70 char path[PATH_MAX];
71 char pathBuf[PATH_MAX];
72 #if (defined(__aarch64__) || defined(__x86_64__))
73 ret = snprintf_s(path, PATH_MAX, PATH_MAX, "/vendor/lib64/%s.z.so", libName);
74 #else
75 ret = snprintf_s(path, PATH_MAX, PATH_MAX, "/vendor/lib/%s.z.so", libName);
76 #endif
77 if (ret < 0) {
78 HDF_LOGE("%{public}s: get libPath failed", __func__);
79 return HDF_FAILURE;
80 }
81 if (realpath(path, pathBuf) == NULL) {
82 HDF_LOGE("%{public}s: realpath is null! [%{public}d]", __func__, errno);
83 return HDF_FAILURE;
84 }
85 if (strncmp(HDF_LIBRARY_DIR, pathBuf, strlen(HDF_LIBRARY_DIR)) != 0) {
86 HDF_LOGE("%{public}s: The file path is incorrect", __func__);
87 return HDF_FAILURE;
88 }
89
90 void *handle = dlopen(pathBuf, RTLD_LAZY);
91 if (handle == NULL) {
92 HDF_LOGE("%{public}s: open so failed, reason:%{public}s", __func__, dlerror());
93 return HDF_FAILURE;
94 }
95
96 getFactoryLib = dlsym(handle, "GetEffectoyFactoryLib");
97 if (getFactoryLib == NULL) {
98 HDF_LOGE("%{public}s: dlsym failed %{public}s", __func__, dlerror());
99 dlclose(handle);
100 return HDF_FAILURE;
101 }
102 *factLib = getFactoryLib();
103 if (*factLib == NULL) {
104 HDF_LOGE("%{public}s: get fact lib failed %{public}s", __func__, dlerror());
105 dlclose(handle);
106 return HDF_FAILURE;
107 }
108 *libHandle = handle;
109 return HDF_SUCCESS;
110 }
111
AddEffectLibInfo(const char * libName,uint8_t * libHandle,struct EffectFactory * factLib)112 static struct AudioEffectLibInfo* AddEffectLibInfo(const char *libName, uint8_t *libHandle,
113 struct EffectFactory *factLib)
114 {
115 if (libName == NULL || factLib == NULL || libHandle == NULL) {
116 HDF_LOGE("%{public}s: invailid input params", __func__);
117 return NULL;
118 }
119 struct AudioEffectLibInfo *libInfo = NULL;
120 libInfo = (struct AudioEffectLibInfo *)OsalMemCalloc(sizeof(struct AudioEffectLibInfo));
121 if (libInfo == NULL) {
122 HDF_LOGE("%{public}s: OsalMemCalloc fail", __func__);
123 return NULL;
124 }
125 libInfo->libName = strdup(libName);
126 if (libInfo->libName == NULL) {
127 HDF_LOGE("%{public}s: strdup fail", __func__);
128 OsalMemFree(libInfo);
129 libInfo = NULL;
130 return NULL;
131 }
132 libInfo->libHandle = libHandle;
133 libInfo->libEffect = factLib;
134 libInfo->ctrlMgr = NULL;
135 libInfo->effectCnt = 1;
136 return libInfo;
137 }
138
LoadEffectLibrary(const char * libName,struct EffectFactory ** factLib,struct ControllerManager ** ctrlMgr)139 static int32_t LoadEffectLibrary(const char *libName, struct EffectFactory **factLib,
140 struct ControllerManager** ctrlMgr)
141 {
142 if (libName == NULL || factLib == NULL || ctrlMgr == NULL) {
143 HDF_LOGE("%{public}s: invailid input params", __func__);
144 return HDF_ERR_INVALID_PARAM;
145 }
146 uint8_t *libHandle = NULL;
147 int32_t index = 0;
148 for (int i = 0; i <= AUDIO_EFFECT_NUM_MAX; i++) {
149 if (i == AUDIO_EFFECT_NUM_MAX) {
150 HDF_LOGE("%{public}s: over effect max num", __func__);
151 return HDF_FAILURE;
152 }
153 if (g_libInfos[i] == NULL) {
154 index = i;
155 break;
156 }
157 if (strcmp(g_libInfos[i]->libName, libName) != 0) {
158 continue;
159 }
160 g_libInfos[i]->effectCnt++;
161 *factLib = g_libInfos[i]->libEffect;
162 *ctrlMgr = g_libInfos[i]->ctrlMgr;
163 HDF_LOGI("%{public}s: %{public}s increase, cnt=[%{public}d]", __func__, libName, g_libInfos[i]->effectCnt);
164 return HDF_SUCCESS;
165 }
166 int32_t ret = LoadLibraryByName(libName, &libHandle, factLib);
167 if (ret != HDF_SUCCESS || libHandle == NULL || *factLib == NULL) {
168 HDF_LOGE("%{public}s: load lib fail, libName:[%{public}s]", __func__, libName);
169 return HDF_FAILURE;
170 }
171 g_libInfos[index] = AddEffectLibInfo(libName, libHandle, *factLib);
172 if (g_libInfos[index] == NULL) {
173 HDF_LOGE("%{public}s: AddEffectLibInfo fail", __func__);
174 dlclose((void *)libHandle);
175 return HDF_FAILURE;
176 }
177 HDF_LOGI("%{public}s: %{public}s create, cnt=[%{public}d]", __func__, libName, g_libInfos[index]->effectCnt);
178 return HDF_SUCCESS;
179 }
180
DeleteEffectLibrary(const char * libName)181 static int32_t DeleteEffectLibrary(const char *libName)
182 {
183 if (libName == NULL) {
184 HDF_LOGE("%{public}s: invailid input params", __func__);
185 return HDF_ERR_INVALID_PARAM;
186 }
187 for (int i = 0; i <= AUDIO_EFFECT_NUM_MAX; i++) {
188 if (i == AUDIO_EFFECT_NUM_MAX) {
189 HDF_LOGE("%{public}s: fail to destroy effect, can not find %{public}s", __func__, libName);
190 return HDF_FAILURE;
191 }
192 if (g_libInfos[i] == NULL || strcmp(g_libInfos[i]->libName, libName) != 0) {
193 continue;
194 }
195 if (g_libInfos[i]->effectCnt > 1) {
196 g_libInfos[i]->effectCnt--;
197 HDF_LOGI("%{public}s: %{public}s decrease, cnt=[%{public}d]", __func__, libName, g_libInfos[i]->effectCnt);
198 return HDF_SUCCESS;
199 }
200 dlclose((void*)g_libInfos[i]->libHandle);
201 OsalMemFree(g_libInfos[i]->libName);
202 OsalMemFree(g_libInfos[i]->ctrlMgr);
203 OsalMemFree(g_libInfos[i]);
204 g_libInfos[i] = NULL;
205 break;
206 }
207 HDF_LOGI("%{public}s: %{public}s delete", __func__, libName);
208 return HDF_SUCCESS;
209 }
210
IsSupplyEffect(const char * libName)211 static int32_t IsSupplyEffect(const char *libName)
212 {
213 if (g_cfgDescs == NULL) {
214 HDF_LOGE("%{public}s: point is null!", __func__);
215 return HDF_FAILURE;
216 }
217 for (uint32_t i = 0; i < g_cfgDescs->effectNum; i++) {
218 if (strcmp(g_cfgDescs->effectCfgDescs[i].library, libName) == 0) {
219 return HDF_SUCCESS;
220 }
221 }
222 return HDF_FAILURE;
223 }
224
EffectModelIsSupplyEffectLibs(struct IEffectModel * self,bool * supply)225 static int32_t EffectModelIsSupplyEffectLibs(struct IEffectModel *self, bool *supply)
226 {
227 if (self == NULL || supply == NULL) {
228 HDF_LOGE("%{public}s: invailid input params", __func__);
229 return HDF_ERR_INVALID_PARAM;
230 }
231
232 *supply = IsEffectLibExist();
233 return HDF_SUCCESS;
234 }
235
EffectModelGetAllEffectDescriptors(struct IEffectModel * self,struct EffectControllerDescriptor * descs,uint32_t * descsLen)236 static int32_t EffectModelGetAllEffectDescriptors(struct IEffectModel *self,
237 struct EffectControllerDescriptor *descs, uint32_t *descsLen)
238 {
239 HDF_LOGD("enter to %{public}s", __func__);
240 int32_t ret;
241 uint32_t i;
242 uint32_t descNum = 0;
243 struct EffectFactory *factLib = NULL;
244 struct ControllerManager *ctrlMgr = NULL;
245
246 if (self == NULL || descs == NULL || descsLen == NULL) {
247 HDF_LOGE("%{public}s: invailid input params", __func__);
248 return HDF_ERR_INVALID_PARAM;
249 }
250
251 if (g_cfgDescs == NULL) {
252 HDF_LOGE("%{public}s: point is null!", __func__);
253 return HDF_FAILURE;
254 }
255 struct EffectControllerDescriptorVdi *descsVdi = (struct EffectControllerDescriptorVdi *)descs;
256 pthread_rwlock_wrlock(&g_rwEffectLock);
257 for (i = 0; i < g_cfgDescs->effectNum; i++) {
258 ret = LoadEffectLibrary(g_cfgDescs->effectCfgDescs[i].library, &factLib, &ctrlMgr);
259 if (ret != HDF_SUCCESS || factLib == NULL) {
260 HDF_LOGE("%{public}s: GetEffectLibFromList fail!", __func__);
261 continue;
262 }
263 if (ConstructDescriptor(&descsVdi[descNum]) != HDF_SUCCESS) {
264 HDF_LOGE("%{public}s: ConstructDescriptor fail!", __func__);
265 continue;
266 }
267 ret = factLib->GetDescriptor(factLib, g_cfgDescs->effectCfgDescs[i].effectId, &descsVdi[descNum]);
268 if (ret != HDF_SUCCESS) {
269 descNum++;
270 DeleteEffectLibrary(g_cfgDescs->effectCfgDescs[i].library);
271 HDF_LOGE("%{public}s: GetDescriptor fail!", __func__);
272 continue;
273 }
274 DeleteEffectLibrary(g_cfgDescs->effectCfgDescs[i].library);
275 factLib = NULL;
276 descNum++;
277 }
278 *descsLen = descNum;
279 descs = (struct EffectControllerDescriptor *)descsVdi;
280 pthread_rwlock_unlock(&g_rwEffectLock);
281 HDF_LOGD("%{public}s success", __func__);
282 return HDF_SUCCESS;
283 }
284
EffectModelGetEffectDescriptor(struct IEffectModel * self,const char * uuid,struct EffectControllerDescriptor * desc)285 static int32_t EffectModelGetEffectDescriptor(struct IEffectModel *self, const char *uuid,
286 struct EffectControllerDescriptor *desc)
287 {
288 HDF_LOGD("enter to %{public}s", __func__);
289 uint32_t i;
290 struct EffectFactory *factLib = NULL;
291 struct ControllerManager *ctrlMgr = NULL;
292 if (self == NULL || uuid == NULL || desc == NULL) {
293 HDF_LOGE("%{public}s: invailid input params", __func__);
294 return HDF_ERR_INVALID_PARAM;
295 }
296 struct EffectControllerDescriptorVdi *descVdi = (struct EffectControllerDescriptorVdi *)desc;
297 for (i = 0; i < g_cfgDescs->effectNum; i++) {
298 if (strcmp(uuid, g_cfgDescs->effectCfgDescs[i].effectId) != 0) {
299 continue;
300 }
301 pthread_rwlock_wrlock(&g_rwEffectLock);
302 LoadEffectLibrary(g_cfgDescs->effectCfgDescs[i].library, &factLib, &ctrlMgr);
303 if (factLib == NULL) {
304 HDF_LOGE("%{public}s: GetEffectLibFromList fail!", __func__);
305 pthread_rwlock_unlock(&g_rwEffectLock);
306 return HDF_FAILURE;
307 }
308 if (ConstructDescriptor(descVdi) != HDF_SUCCESS) {
309 HDF_LOGE("%{public}s: ConstructDescriptor fail!", __func__);
310 pthread_rwlock_unlock(&g_rwEffectLock);
311 return HDF_FAILURE;
312 }
313 if (factLib->GetDescriptor(factLib, uuid, descVdi) != HDF_SUCCESS) {
314 DeleteEffectLibrary(g_cfgDescs->effectCfgDescs[i].library);
315 HDF_LOGE("%{public}s: GetDescriptor fail!", __func__);
316 pthread_rwlock_unlock(&g_rwEffectLock);
317 return HDF_FAILURE;
318 }
319 DeleteEffectLibrary(g_cfgDescs->effectCfgDescs[i].library);
320 HDF_LOGD("%{public}s success", __func__);
321 pthread_rwlock_unlock(&g_rwEffectLock);
322 return HDF_SUCCESS;
323 }
324 desc = (struct EffectControllerDescriptor *)descVdi;
325 HDF_LOGE("%{public}s fail!", __func__);
326 return HDF_FAILURE;
327 }
328
CreateEffectController(const struct EffectInfo * info,struct IEffectControl ** contoller,struct ControllerId * contollerId,struct IEffectControlVdi * ctrlOps)329 static int32_t CreateEffectController(const struct EffectInfo *info, struct IEffectControl **contoller,
330 struct ControllerId *contollerId, struct IEffectControlVdi *ctrlOps)
331 {
332 if (info == NULL || contoller == NULL || contollerId == NULL) {
333 HDF_LOGE("%{public}s: invailid input params", __func__);
334 return HDF_ERR_INVALID_PARAM;
335 }
336 struct ControllerManager *ctrlMgr = (struct ControllerManager *)OsalMemCalloc(sizeof(struct ControllerManager));
337 CHECK_NULL_PTR_RETURN_VALUE(ctrlMgr, HDF_FAILURE);
338 struct AudioEffectLibInfo *libInfo = GetEffectLibInfoByName(info->libName);
339 if (libInfo == NULL) {
340 HDF_LOGE("%{public}s: GetEffectLibInfoByName failed", __func__);
341 OsalMemFree(ctrlMgr);
342 return HDF_FAILURE;
343 }
344 libInfo->ctrlMgr = ctrlMgr;
345 ctrlMgr->ctrlOps = ctrlOps;
346 ctrlMgr->libName = strdup(info->libName);
347 if (ctrlMgr->libName == NULL) {
348 HDF_LOGE("%{public}s: strdup failed, info->effectId = %{public}s", __func__, info->effectId);
349 OsalMemFree(ctrlMgr);
350 return HDF_FAILURE;
351 }
352 ctrlMgr->ctrlImpls.EffectProcess = EffectControlEffectProcess;
353 ctrlMgr->ctrlImpls.SendCommand = EffectControlSendCommand;
354 ctrlMgr->ctrlImpls.GetEffectDescriptor = EffectGetOwnDescriptor;
355 ctrlMgr->ctrlImpls.EffectReverse = EffectControlEffectReverse;
356 *contoller = &ctrlMgr->ctrlImpls;
357 // free after send reply
358 contollerId->libName = strdup(info->libName);
359 contollerId->effectId = strdup(info->effectId);
360 if (contollerId->libName == NULL || contollerId->effectId == NULL) {
361 HDF_LOGE("%{public}s: strdup failed, info->libName = %{public}s", __func__, info->libName);
362 OsalMemFree(ctrlMgr->libName);
363 OsalMemFree(ctrlMgr);
364 *contoller = NULL;
365 return HDF_FAILURE;
366 }
367 return HDF_SUCCESS;
368 }
369
EffectModelCreateEffectController(struct IEffectModel * self,const struct EffectInfo * info,struct IEffectControl ** contoller,struct ControllerId * contollerId)370 static int32_t EffectModelCreateEffectController(struct IEffectModel *self, const struct EffectInfo *info,
371 struct IEffectControl **contoller, struct ControllerId *contollerId)
372 {
373 if (self == NULL || info == NULL || contoller == NULL || contollerId == NULL) {
374 HDF_LOGE("%{public}s: invailid input params", __func__);
375 return HDF_ERR_INVALID_PARAM;
376 }
377 if (IsSupplyEffect(info->libName) != HDF_SUCCESS) {
378 HDF_LOGE("%{public}s: not support effect [%{public}s]", __func__, info->libName);
379 return HDF_FAILURE;
380 }
381 struct EffectFactory *lib = NULL;
382 struct ControllerManager *ctrlMgr = NULL;
383 struct IEffectControlVdi *ctrlOps = NULL;
384 pthread_rwlock_wrlock(&g_rwEffectLock);
385 int32_t ret = LoadEffectLibrary(info->libName, &lib, &ctrlMgr);
386 if (ret != HDF_SUCCESS) {
387 HDF_LOGE("%{public}s: LoadEffectLibrary fail", __func__);
388 goto EXIT;
389 }
390 if (ctrlMgr != NULL) {
391 contollerId->libName = strdup(info->libName);
392 contollerId->effectId = strdup(info->effectId);
393 if (contollerId->libName == NULL || contollerId->effectId == NULL) {
394 HDF_LOGE("%{public}s: strdup failed", __func__);
395 goto EXIT;
396 }
397 *contoller = &ctrlMgr->ctrlImpls;
398 pthread_rwlock_unlock(&g_rwEffectLock);
399 return HDF_SUCCESS;
400 }
401 if (lib == NULL || lib->CreateController == NULL) {
402 HDF_LOGE("%{public}s: lib or lib->CreateController is null", __func__);
403 goto EXIT;
404 }
405
406 struct EffectInfoVdi *infoVdi = (struct EffectInfoVdi *)info;
407 lib->CreateController(lib, infoVdi, &ctrlOps);
408 if (ctrlOps == NULL) {
409 HDF_LOGE("%{public}s: ctrlOps is null", __func__);
410 goto EXIT;
411 }
412
413 if (CreateEffectController(info, contoller, contollerId, ctrlOps) != HDF_SUCCESS) {
414 DeleteEffectLibrary(info->libName);
415 goto EXIT;
416 }
417
418 pthread_rwlock_unlock(&g_rwEffectLock);
419 HDF_LOGI("%{public}s: create effect succeed, libName = %{public}s", __func__, info->libName);
420 return HDF_SUCCESS;
421 EXIT:
422 pthread_rwlock_unlock(&g_rwEffectLock);
423 return HDF_FAILURE;
424 }
425
EffectModelDestroyEffectController(struct IEffectModel * self,const struct ControllerId * contollerId)426 int32_t EffectModelDestroyEffectController(struct IEffectModel *self, const struct ControllerId *contollerId)
427 {
428 if (self == NULL || contollerId == NULL) {
429 HDF_LOGE("%{public}s: invailid input params", __func__);
430 return HDF_ERR_INVALID_PARAM;
431 }
432 pthread_rwlock_wrlock(&g_rwEffectLock);
433 struct AudioEffectLibInfo *libInfo = GetEffectLibInfoByName(contollerId->libName);
434 if (libInfo == NULL) {
435 HDF_LOGE("%{public}s: GetEffectLibInfoByName failed", __func__);
436 pthread_rwlock_unlock(&g_rwEffectLock);
437 return HDF_FAILURE;
438 }
439 if (libInfo->effectCnt > 1) {
440 libInfo->effectCnt--;
441 pthread_rwlock_unlock(&g_rwEffectLock);
442 return HDF_SUCCESS;
443 }
444 struct EffectFactory *lib = libInfo->libEffect;
445 if (lib == NULL) {
446 HDF_LOGE("%{public}s: lib is null", __func__);
447 pthread_rwlock_unlock(&g_rwEffectLock);
448 return HDF_FAILURE;
449 }
450 struct ControllerManager *ctrlMgr = libInfo->ctrlMgr;
451 if (ctrlMgr == NULL) {
452 HDF_LOGE("%{public}s: ctrlMgr is null", __func__);
453 pthread_rwlock_unlock(&g_rwEffectLock);
454 return HDF_FAILURE;
455 }
456
457 if (ctrlMgr->libName != NULL) {
458 OsalMemFree(ctrlMgr->libName);
459 ctrlMgr->libName = NULL;
460 }
461
462 if (ctrlMgr->ctrlOps == NULL) {
463 HDF_LOGE("%{public}s: controller has no options", __func__);
464 pthread_rwlock_unlock(&g_rwEffectLock);
465 return HDF_FAILURE;
466 }
467
468 /* call the lib destroy method,then free controller manager */
469 lib->DestroyController(lib, ctrlMgr->ctrlOps);
470 DeleteEffectLibrary(contollerId->libName);
471 pthread_rwlock_unlock(&g_rwEffectLock);
472 HDF_LOGI("%{public}s: destroy effect succeed, libName = %{public}s", __func__, contollerId->libName);
473 return HDF_SUCCESS;
474 }
475
ModelInit(void)476 void ModelInit(void)
477 {
478 FILE *file;
479 struct ConfigDescriptor *cfgDesc = NULL;
480 int32_t ret;
481 file = fopen(AUDIO_EFFECT_PRODUCT_CONFIG, "r");
482 if (file == NULL) {
483 ret = AudioEffectGetConfigDescriptor(AUDIO_EFFECT_PLAFORM_CONFIG, &cfgDesc);
484 HDF_LOGI("%{public}s: %{public}s!", __func__, AUDIO_EFFECT_PLAFORM_CONFIG);
485 } else {
486 ret = AudioEffectGetConfigDescriptor(AUDIO_EFFECT_PRODUCT_CONFIG, &cfgDesc);
487 HDF_LOGI("%{public}s: %{public}s!", __func__, AUDIO_EFFECT_PRODUCT_CONFIG);
488 (void)fclose(file);
489 }
490 if (ret != HDF_SUCCESS) {
491 HDF_LOGE("%{public}s: AudioEffectGetConfigDescriptor fail!", __func__);
492 return;
493 }
494
495 if (cfgDesc == NULL || cfgDesc->effectCfgDescs == NULL || cfgDesc->libCfgDescs == NULL) {
496 HDF_LOGE("cfgDesc is null!");
497 return;
498 }
499 g_cfgDescs = cfgDesc;
500 HDF_LOGD("%{public}s end!", __func__);
501 }
502
EffectModelImplGetInstance(void)503 struct IEffectModel *EffectModelImplGetInstance(void)
504 {
505 struct EffectModelService *service = (struct EffectModelService *)OsalMemCalloc(sizeof(struct EffectModelService));
506 if (service == NULL) {
507 HDF_LOGE("%{public}s: malloc EffectModelService obj failed!", __func__);
508 return NULL;
509 }
510
511 ModelInit();
512 service->interface.IsSupplyEffectLibs = EffectModelIsSupplyEffectLibs;
513 service->interface.GetAllEffectDescriptors = EffectModelGetAllEffectDescriptors;
514 service->interface.CreateEffectController = EffectModelCreateEffectController;
515 service->interface.DestroyEffectController = EffectModelDestroyEffectController;
516 service->interface.GetEffectDescriptor = EffectModelGetEffectDescriptor;
517
518 return &service->interface;
519 }
520
EffectModelImplRelease(struct IEffectModel * instance)521 void EffectModelImplRelease(struct IEffectModel *instance)
522 {
523 if (instance == NULL) {
524 return;
525 }
526
527 AudioEffectReleaseCfgDesc(g_cfgDescs);
528 struct EffectModelService *service = CONTAINER_OF(instance, struct EffectModelService, interface);
529 if (service == NULL) {
530 return;
531 }
532 OsalMemFree(service);
533 service = NULL;
534 }
535
536