diff --git a/audio/effect/model/include/effect_model.h b/audio/effect/model/include/effect_model.h new file mode 100644 index 0000000000000000000000000000000000000000..ee09ca8cc760e5650ca6daf89f7b6ec510bb8f51 --- /dev/null +++ b/audio/effect/model/include/effect_model.h @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef EFFECT_MODEL_H +#define EFFECT_MODEL_H + +#include +#include +#include +#include + +#include "effect_host_common.h" + +struct ControllerManager +int32_t IsControllerManagerExist(struct ControllerManager *manager); +pthread_rwlock_t* GetAudioEffectLock(void); + +#endif \ No newline at end of file diff --git a/audio/effect/model/src/effect_control.c b/audio/effect/model/src/effect_control.c index c89aba08686513d92304db19b1721854e82f4dcd..0898d535cfd1fc2c022d671c15731c9126f7d44c 100644 --- a/audio/effect/model/src/effect_control.c +++ b/audio/effect/model/src/effect_control.c @@ -28,15 +28,18 @@ #define HDF_LOG_TAG HDF_AUDIO_EFFECT -int32_t EffectControlEffectProcess(struct IEffectControl *self, const struct AudioEffectBuffer *input, - struct AudioEffectBuffer *output) +static int32_t EffectControlEffectProcessLocked(struct IEffectControl *self, const struct AudioEffectBuffer *input, + struct AudioEffectBuffer *output) { - CHECK_TRUE_RETURN_RET_LOG(self == NULL || input == NULL || output == NULL, HDF_ERR_INVALID_PARAM, - "%{public}s: invailid input params", __func__); - struct ControllerManager *manager = (struct ControllerManager *)self; + if (IsControllerManagerExist(manager) != HDF_SUCCESS) { + HDF_LOGE("%{public}s: invailid manager", __func__); + return HDF_FAILURE; + } CHECK_TRUE_RETURN_RET_LOG(manager->ctrlOps == NULL || manager->ctrlOps->EffectProcess == NULL, HDF_FAILURE, "%{public}s: controller has no options", __func__); + CHECK_TRUE_RETURN_RET_LOG(manager->libName == NULL, + HDF_FAILURE, "%{public}s: controller libName is NULL", __func__); if (strcmp(manager->libName, "libmock_effect_lib") != 0) { output->frameCount = input->frameCount; output->datatag = input->datatag; @@ -74,22 +77,39 @@ int32_t EffectControlEffectProcess(struct IEffectControl *self, const struct Aud return ret; } +int32_t EffectControlEffectProcess(struct IEffectControl *self, const struct AudioEffectBuffer *input, + struct AudioEffectBuffer *output) +{ + CHECK_TRUE_RETURN_RET_LOG(self == NULL || input == NULL || output == NULL, HDF_ERR_INVALID_PARAM, + "%{public}s: invailid input params", __func__); + pthread_rwlock_rdlock(GetAudioEffectLock()); + int32_t ret = EffectControlEffectProcessLocked(self, input, output); + pthread_rwlock_unlock(GetAudioEffectLock()); +} + int32_t EffectControlSendCommand(struct IEffectControl *self, enum EffectCommandTableIndex cmdId, const int8_t *cmdData, - uint32_t cmdDataLen, int8_t *replyData, uint32_t *replyDataLen) + uint32_t cmdDataLen, int8_t *replyData, uint32_t *replyDataLen) { if (self == NULL || cmdData == NULL || replyData == NULL || replyDataLen == NULL) { HDF_LOGE("%{public}s: invailid input params", __func__); return HDF_ERR_INVALID_PARAM; } - + pthread_rwlock_rdlock(GetAudioEffectLock()); struct ControllerManager *manager = (struct ControllerManager *)self; + if (IsControllerManagerExist(manager) != HDF_SUCCESS) { + pthread_rwlock_unlock(GetAudioEffectLock()); + HDF_LOGE("%{public}s: invailid manager", __func__); + return HDF_FAILURE; + } if (manager->ctrlOps == NULL || manager->ctrlOps->SendCommand == NULL) { + pthread_rwlock_unlock(GetAudioEffectLock()); HDF_LOGE("%{public}s: controller has no options", __func__); return HDF_FAILURE; } enum EffectCommandTableIndexVdi cmdIdVdi = (enum EffectCommandTableIndexVdi)cmdId; int32_t ret = manager->ctrlOps->SendCommand(manager->ctrlOps, cmdIdVdi, (void *)cmdData, cmdDataLen, (void *)replyData, replyDataLen); + pthread_rwlock_unlock(GetAudioEffectLock()); if (ret != HDF_SUCCESS) { HDF_LOGE("SendCommand failed, ret=%{public}d", ret); return ret; @@ -101,7 +121,6 @@ int32_t EffectGetOwnDescriptor(struct IEffectControl *self, struct EffectControl { CHECK_TRUE_RETURN_RET_LOG(self == NULL || desc == NULL, HDF_ERR_INVALID_PARAM, "%{public}s: invailid input params", __func__); - struct ControllerManager *manager = (struct ControllerManager *)self; CHECK_TRUE_RETURN_RET_LOG(manager->ctrlOps == NULL || manager->ctrlOps->GetEffectDescriptor == NULL, HDF_FAILURE, "%{public}s: controller has no options", __func__); @@ -137,21 +156,28 @@ int32_t EffectGetOwnDescriptor(struct IEffectControl *self, struct EffectControl } int32_t EffectControlEffectReverse(struct IEffectControl *self, const struct AudioEffectBuffer *input, - struct AudioEffectBuffer *output) + struct AudioEffectBuffer *output) { if (self == NULL || input == NULL || output == NULL) { HDF_LOGE("%{public}s: invailid input params", __func__); return HDF_ERR_INVALID_PARAM; } - + pthread_rwlock_rdlock(GetAudioEffectLock()); struct ControllerManager *manager = (struct ControllerManager *)self; + if (IsControllerManagerExist(manager) != HDF_SUCCESS) { + pthread_rwlock_unlock(GetAudioEffectLock()); + HDF_LOGE("%{public}s: invailid manager", __func__); + return HDF_FAILURE; + } if (manager->ctrlOps == NULL || manager->ctrlOps->EffectReverse == NULL) { + pthread_rwlock_unlock(GetAudioEffectLock()); HDF_LOGE("%{public}s: controller has no options", __func__); return HDF_FAILURE; } struct AudioEffectBufferVdi *inputVdi = (struct AudioEffectBufferVdi *)input; struct AudioEffectBufferVdi *outputVdi = (struct AudioEffectBufferVdi *)output; int32_t ret = manager->ctrlOps->EffectReverse(manager->ctrlOps, inputVdi, outputVdi); + pthread_rwlock_unlock(GetAudioEffectLock()); if (ret != HDF_SUCCESS) { HDF_LOGE("EffectReverse failed, ret=%{public}d", ret); return ret; diff --git a/audio/effect/model/src/effect_model.c b/audio/effect/model/src/effect_model.c index 3c1c24d7ca1f3495c2c8227620e086b30696154e..8607881e0dbed3e388f772d5aa6ee681cf20d74c 100644 --- a/audio/effect/model/src/effect_model.c +++ b/audio/effect/model/src/effect_model.c @@ -12,6 +12,9 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + +#include "effect_model.h" + #include #include #include @@ -38,6 +41,11 @@ struct AudioEffectLibInfo { }; struct AudioEffectLibInfo *g_libInfos[AUDIO_EFFECT_NUM_MAX] = { NULL }; +pthread_rwlock_t* GetAudioEffectLock(void) +{ + return &g_rwEffectLock; +} + static struct AudioEffectLibInfo* GetEffectLibInfoByName(const char *libName) { if (libName == NULL) { @@ -208,6 +216,22 @@ static int32_t DeleteEffectLibrary(const char *libName) return HDF_SUCCESS; } +int32_t IsControllerManagerExist(ControllerManager *manager) +{ + if (manager == NULL) { + return HDF_FAILURE; + } + for (int i = 0; i < AUDIO_EFFECT_NUM_MAX; i++) { + if (g_libInfos[i] == NULL) { + continue; + } + if (g_libInfos[i]->ctrlMgr == manager) { + return HDF_SUCCESS; + } + } + return HDF_FAILURE; +} + static int32_t IsSupplyEffect(const char *libName) { if (g_cfgDescs == NULL) { @@ -283,7 +307,7 @@ static int32_t EffectModelGetAllEffectDescriptors(struct IEffectModel *self, } static int32_t EffectModelGetEffectDescriptor(struct IEffectModel *self, const char *uuid, - struct EffectControllerDescriptor *desc) + struct EffectControllerDescriptor *desc) { HDF_LOGD("enter to %{public}s", __func__); uint32_t i; @@ -374,10 +398,12 @@ static int32_t EffectModelCreateEffectController(struct IEffectModel *self, cons HDF_LOGE("%{public}s: invailid input params", __func__); return HDF_ERR_INVALID_PARAM; } + if (IsSupplyEffect(info->libName) != HDF_SUCCESS) { HDF_LOGE("%{public}s: not support effect [%{public}s]", __func__, info->libName); return HDF_FAILURE; } + struct EffectFactory *lib = NULL; struct ControllerManager *ctrlMgr = NULL; struct IEffectControlVdi *ctrlOps = NULL; @@ -531,5 +557,4 @@ void EffectModelImplRelease(struct IEffectModel *instance) } OsalMemFree(service); service = NULL; -} - +} \ No newline at end of file