diff --git a/audio/config/linux/BUILD.gn b/audio/config/linux/BUILD.gn index cde1c9a21c344ee8f9cb02b6f33095ba33b1e032..db5f1b242dd0875dd0fb463984004f76462581b5 100644 --- a/audio/config/linux/BUILD.gn +++ b/audio/config/linux/BUILD.gn @@ -61,4 +61,13 @@ if (is_standard_system) { subsystem_name = "hdf" part_name = "drivers_peripheral_audio" } + + ohos_prebuilt_etc("hdf_audio_effect_json") { + source = "${AUDIO_JSON_PATH}/audio_effect.json" + + relative_install_dir = "hdfconfig" + install_images = [ chipset_base_dir ] + subsystem_name = "hdf" + part_name = "drivers_peripheral_audio" + } } diff --git a/audio/effect/BUILD.gn b/audio/effect/BUILD.gn index b029f84c444f92c9c0339c4fa2aa0440399c2381..875db23e39d0da79b4b261c63d229a2a8f0a1331 100644 --- a/audio/effect/BUILD.gn +++ b/audio/effect/BUILD.gn @@ -9,11 +9,12 @@ # 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. +# limitations under the License. group("effect_model") { if (!defined(ohos_lite)) { deps = [ + "../config/linux:hdf_audio_effect_json", "host:effect_model_service", "model:effect_model_service_1.0", "model/src/mock_effect:mock_effect_lib", diff --git a/audio/effect/config/include/parse_effect_config.h b/audio/effect/config/include/parse_effect_config.h new file mode 100644 index 0000000000000000000000000000000000000000..d28c5158722ae973ece97ff4bdb44c7132937c45 --- /dev/null +++ b/audio/effect/config/include/parse_effect_config.h @@ -0,0 +1,61 @@ +/* + * Copyright (c) 2023 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. + */ + +#include + +#define HDF_EFFECT_LIB_NUM_MAX 32 + +struct LibraryConfigDescriptor { + const char *libName; + const char *libPath; +}; + +struct EffectConfigDescriptor { + const char *name; + const char *library; + const char *effectId; +}; + +struct ConfigDescriptor { + struct LibraryConfigDescriptor *libCfgDescs; + uint32_t libNum; + struct EffectConfigDescriptor *effectCfgDescs; + uint32_t effectNum; +}; + +/** + * @brief Parse the effect profile and get the effect config descriptor. + * + * @param path Indicates the effect configuration file path. + * @param cfgDesc Indicates pointer of the effect config descriptor. + * + * @return Returns 0 if the process success; returns a non-zero value otherwise. + * + * @since 4.0 + * @version 1.0 + */ +int32_t AudioEffectGetConfigDescriptor(const char *path, struct ConfigDescriptor **cfgDesc); + +/** + * @brief Release the effect config descriptor. + * + * @param cfgDesc Indicates pointer of the effect config descriptor. + * + * @return Returns 0 if the process success; returns a non-zero value otherwise. + * + * @since 4.0 + * @version 1.0 + */ +void AudioEffectReleaseCfgDesc(struct ConfigDescriptor *cfgDesc); diff --git a/audio/effect/config/src/parse_effect_config.c b/audio/effect/config/src/parse_effect_config.c new file mode 100644 index 0000000000000000000000000000000000000000..db12015ec0f5fa99696f43e352adbb4b17ad53d0 --- /dev/null +++ b/audio/effect/config/src/parse_effect_config.c @@ -0,0 +1,414 @@ +/* + * Copyright (c) 2023 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. + */ + +#include +#include +#include +#include +#include +#include +#include + +#include "cJSON.h" +#include "osal_mem.h" +#include "securec.h" + +#include "hdf_log.h" +#include "parse_effect_config.h" + +#define HDF_EFFECT_NUM_MAX 32 +#define HDF_EFFECT_CONFIG_SIZE_MAX ((HDF_EFFECT_NUM_MAX) * 1024) +#define HDF_EFFECT_NAME_LEN 64 +#define HDF_LOG_TAG HDF_AUDIO_EFFECT + +static char *GetAudioEffectConfig(const char *fpath) +{ + char *pJsonStr = NULL; + if (fpath == NULL) { + HDF_LOGE("%{public}s: fpath is null!", __func__); + return NULL; + } + char pathBuf[PATH_MAX] = {'\0'}; + if (realpath(fpath, pathBuf) == NULL) { + HDF_LOGE("%{public}s: realpath is null! [%{public}d]", __func__, errno); + return NULL; + } + + FILE *fp = fopen(pathBuf, "r"); + if (fp == NULL) { + HDF_LOGE("%{public}s: can not open config file! [%{public}d]", __func__, errno); + return NULL; + } + if (fseek(fp, 0, SEEK_END) != HDF_SUCCESS) { + HDF_LOGE("%{public}s: fseek fail! [%{public}d]", __func__, errno); + (void)fclose(fp); + return NULL; + } + int32_t jsonStrSize = ftell(fp); + if (jsonStrSize <= 0) { + HDF_LOGE("%{public}s: ftell fail! [%{public}d]", __func__, errno); + (void)fclose(fp); + return NULL; + } + rewind(fp); + if (jsonStrSize > HDF_EFFECT_CONFIG_SIZE_MAX) { + HDF_LOGE("%{public}s: The configuration file is too large to load!", __func__); + (void)fclose(fp); + return NULL; + } + pJsonStr = (char *)OsalMemCalloc((uint32_t)jsonStrSize + 1); + if (pJsonStr == NULL) { + HDF_LOGE("%{public}s: alloc pJsonStr failed!", __func__); + (void)fclose(fp); + return NULL; + } + if (fread(pJsonStr, jsonStrSize, 1, fp) != 1) { + HDF_LOGE("%{public}s: read to file fail! [%{public}d]", __func__, errno); + OsalMemFree((void *)pJsonStr); + (void)fclose(fp); + return NULL; + } + (void)fclose(fp); + return pJsonStr; +} + +cJSON *GetAudioEffectConfigToJsonObj(const char *fpath) +{ + char *pJsonStr = GetAudioEffectConfig(fpath); + if (pJsonStr == NULL) { + HDF_LOGE("%{public}s: get audio effect config failed!", __func__); + return NULL; + } + cJSON *cJsonObj = cJSON_Parse(pJsonStr); + if (cJsonObj == NULL) { + HDF_LOGE("%{public}s: cJSON_Parse failed!", __func__); + OsalMemFree((void *)pJsonStr); + return NULL; + } + OsalMemFree((void *)pJsonStr); + return cJsonObj; +} + +static char *AudioEffectGetAndCheckName(const cJSON *cJSONObj, const char *name) +{ + if (cJSONObj == NULL || name == NULL) { + HDF_LOGE("%{public}s: invalid parameter!", __func__); + return NULL; + } + + cJSON *cJSONName = cJSON_GetObjectItem(cJSONObj, name); + if (cJSONName == NULL || cJSONName->valuestring == NULL) { + HDF_LOGE("%{public}s: cJSONName or cJSONName->valuestring is null!", __func__); + return NULL; + } + + char *effectValue = cJSONName->valuestring; + if (strlen(effectValue) == 0) { + HDF_LOGE("%{public}s: effectValue is null!", __func__); + return NULL; + } + + if (strcmp(name, "effectId") && !isalpha(*effectValue++)) { // Names must begin with a letter + HDF_LOGE("%{public}s: effectValue is illegal!", __func__); + return NULL; + } + + while (*effectValue != '\0') { + if (*effectValue == '_' || (strcmp(name, "effectId") == 0 && *effectValue == '-')) { + effectValue++; + continue; + } + + if (!isalnum(*effectValue++)) { + HDF_LOGE("%{public}s: effectValue is illegal!, %{public}c", __func__, *effectValue); + return NULL; + } + } + return cJSONName->valuestring; +} + +static int32_t AudioEffectParseItem(const cJSON *cJSONObj, const char *item, const char **dest) +{ + if (cJSONObj == NULL || item == NULL || dest == NULL) { + HDF_LOGE("%{public}s: invalid parameter!", __func__); + return HDF_ERR_INVALID_PARAM; + } + + const char *itemName = AudioEffectGetAndCheckName(cJSONObj, item); + if (itemName == NULL) { + HDF_LOGE("%{public}s: get %{public}s fail!", __func__, item); + return HDF_FAILURE; + } + + *dest = (char *)OsalMemCalloc(HDF_EFFECT_NAME_LEN * sizeof(char)); + if (*dest == NULL) { + HDF_LOGE("%{public}s: out of memory! Item is %{public}s", __func__, item); + return HDF_ERR_MALLOC_FAIL; + } + + if (memcpy_s((void *)(*dest), HDF_EFFECT_NAME_LEN, itemName, strlen(itemName)) != EOK) { + HDF_LOGE("%{public}s: memcpy_s effect name fail! Item is %{public}s", __func__, item); + OsalMemFree((void *)(*dest)); + *dest = NULL; + return HDF_FAILURE; + } + return HDF_SUCCESS; +} + +static int32_t AudioEffectParseEffect(const cJSON *effectObj, struct EffectConfigDescriptor *effectDesc) +{ + if (effectObj == NULL || effectDesc == NULL) { + HDF_LOGE("%{public}s: invalid parameter!", __func__); + return HDF_ERR_INVALID_PARAM; + } + + if (AudioEffectParseItem(effectObj, "name", &(effectDesc->name)) != HDF_SUCCESS) { + HDF_LOGE("%{public}s: parse item %{public}s fail!", __func__, "name"); + return HDF_FAILURE; + } + + if (AudioEffectParseItem(effectObj, "library", &(effectDesc->library)) != HDF_SUCCESS) { + HDF_LOGE("%{public}s: parse item %{public}s fail!", __func__, "library"); + OsalMemFree((void *)effectDesc->name); + return HDF_FAILURE; + } + + if (AudioEffectParseItem(effectObj, "effectId", &(effectDesc->effectId)) != HDF_SUCCESS) { + HDF_LOGE("%{public}s: parse item %{public}s fail!", __func__, "library"); + OsalMemFree((void *)effectDesc->name); + OsalMemFree((void *)effectDesc->library); + return HDF_FAILURE; + } + + return HDF_SUCCESS; +} + +static void AudioEffectReleaseDescs(struct EffectConfigDescriptor *effectDescs, int32_t effectNum) +{ + int32_t i = 0; + + if (effectDescs == NULL || effectNum <= 0 || effectNum > HDF_EFFECT_NUM_MAX) { + HDF_LOGE("%{public}s: effectDescs is null or effectNum is invalid!", __func__); + return; + } + + for (i = 0; i < effectNum; i++) { + OsalMemFree((void *)effectDescs[i].name); + OsalMemFree((void *)effectDescs[i].library); + OsalMemFree((void *)effectDescs[i].effectId); + } +} + +static int32_t AudioEffectGetEffectCfgDescs(cJSON *cJsonObj, const char *item, struct ConfigDescriptor *cfgDesc) +{ + HDF_LOGI("enter to %{public}s", __func__); + uint32_t effectNum; + uint32_t i; + cJSON *effectObj = NULL; + struct EffectConfigDescriptor *effectDescs = NULL; + int32_t ret; + + if (cJsonObj == NULL || item == NULL || cfgDesc == NULL) { + HDF_LOGE("%{public}s: invalid parameter!", __func__); + return HDF_ERR_INVALID_PARAM; + } + cJSON *effectsObj = cJSON_GetObjectItem(cJsonObj, item); + if (effectsObj == NULL) { + HDF_LOGE("%{public}s: get effects failed!", __func__); + return HDF_FAILURE; + } + + effectNum = cJSON_GetArraySize(effectsObj); + if (effectNum <= 0) { + HDF_LOGE("%{public}s: effectNum invalid, effectNum = %{public}d!", __func__, effectNum); + return HDF_FAILURE; + } + effectDescs = (struct EffectConfigDescriptor *)OsalMemCalloc(effectNum * sizeof(struct EffectConfigDescriptor)); + if (effectDescs == NULL) { + HDF_LOGE("%{public}s: alloc effectDescs failed", __func__); + return HDF_ERR_MALLOC_FAIL; + } + + for (i = 0; i < effectNum; i++) { + effectObj = cJSON_GetArrayItem(effectsObj, i); + if (effectObj == NULL) { + HDF_LOGE("%{public}s get effect item fail!", __func__); + AudioEffectReleaseDescs(effectDescs, i); + OsalMemFree((void *)effectDescs); + return HDF_FAILURE; + } + ret = AudioEffectParseEffect(effectObj, &effectDescs[i]); + if (ret != HDF_SUCCESS) { + HDF_LOGE("%{public}s: parse effect[%{public}d] failed, ret = %{public}d", __func__, i, ret); + AudioEffectReleaseDescs(effectDescs, i); + OsalMemFree((void *)effectDescs); + return HDF_FAILURE; + } + } + cfgDesc->effectNum = effectNum; + cfgDesc->effectCfgDescs = effectDescs; + HDF_LOGI("%{public}s success", __func__); + return HDF_SUCCESS; +} + +static int32_t AudioEffectParseLibrary(const cJSON *libObj, struct LibraryConfigDescriptor *libDesc) +{ + if (libObj == NULL || libDesc == NULL) { + HDF_LOGE("%{public}s: invalid parameter!", __func__); + return HDF_ERR_INVALID_PARAM; + } + + if (AudioEffectParseItem(libObj, "name", &(libDesc->libName)) != HDF_SUCCESS) { + HDF_LOGE("%{public}s: parse item %{public}s fail!", __func__, "name"); + return HDF_FAILURE; + } + + if (AudioEffectParseItem(libObj, "path", &(libDesc->libPath)) != HDF_SUCCESS) { + HDF_LOGE("%{public}s: parse item %{public}s fail!", __func__, "path"); + OsalMemFree((void *)libDesc->libName); + return HDF_FAILURE; + } + + return HDF_SUCCESS; +} + +static void AudioEffectLibraryReleaseDescs(struct LibraryConfigDescriptor *libDescs, int32_t libNum) +{ + int32_t i = 0; + + if (libDescs == NULL || libNum <= 0 || libNum > HDF_EFFECT_LIB_NUM_MAX) { + HDF_LOGE("%{public}s: libDescs is null or libNum is invalid!", __func__); + return; + } + + for (i = 0; i < libNum; i++) { + OsalMemFree((void *)libDescs[i].libName); + OsalMemFree((void *)libDescs[i].libPath); + } +} + +static int32_t AudioEffectGetLibraryCfgDescs(cJSON *cJsonObj, const char *item, struct ConfigDescriptor *cfgDesc) +{ + HDF_LOGI("enter to %{public}s", __func__); + int32_t ret; + uint32_t i; + uint32_t libNum; + cJSON *libObj = NULL; + struct LibraryConfigDescriptor *libDescs = NULL; + if (cJsonObj == NULL || item == NULL || cfgDesc == NULL) { + HDF_LOGE("%{public}s: invalid parameter!", __func__); + return HDF_ERR_INVALID_PARAM; + } + cJSON *libsObj = cJSON_GetObjectItem(cJsonObj, item); + if (libsObj == NULL) { + HDF_LOGE("%{public}s: get libs failed!", __func__); + return HDF_FAILURE; + } + + libNum = cJSON_GetArraySize(libsObj); + if (libNum <= 0) { + HDF_LOGE("%{public}s: libNum invalid, libNum = %{public}d!", __func__, libNum); + return HDF_FAILURE; + } + libDescs = (struct LibraryConfigDescriptor *)OsalMemCalloc(libNum * sizeof(struct LibraryConfigDescriptor)); + if (libDescs == NULL) { + HDF_LOGE("%{public}s: malloc libDescs failed", __func__); + return HDF_ERR_MALLOC_FAIL; + } + + for (i = 0; i < libNum; i++) { + libObj = cJSON_GetArrayItem(libsObj, i); + if (libObj == NULL) { + HDF_LOGE("%{public}s get library item fail!", __func__); + AudioEffectLibraryReleaseDescs(libDescs, i); + OsalMemFree((void *)libDescs); + return HDF_FAILURE; + } + ret = AudioEffectParseLibrary(libObj, &libDescs[i]); + if (ret != HDF_SUCCESS) { + HDF_LOGE("%{public}s: parse library[%{public}d] failed, ret = %{public}d", __func__, i, ret); + AudioEffectLibraryReleaseDescs(libDescs, i); + OsalMemFree((void *)libDescs); + return HDF_FAILURE; + } + } + cfgDesc->libNum = libNum; + cfgDesc->libCfgDescs = libDescs; + HDF_LOGI("%{public}s success", __func__); + return HDF_SUCCESS; +} + +void AudioEffectReleaseCfgDesc(struct ConfigDescriptor *cfgDesc) +{ + if (cfgDesc == NULL) { + return; + } + + if (cfgDesc->libCfgDescs != NULL) { + AudioEffectLibraryReleaseDescs(cfgDesc->libCfgDescs, cfgDesc->libNum); + OsalMemFree((void *)cfgDesc->libCfgDescs); + } + + if (cfgDesc->effectCfgDescs != NULL) { + AudioEffectReleaseDescs(cfgDesc->effectCfgDescs, cfgDesc->effectNum); + OsalMemFree((void *)cfgDesc->effectCfgDescs); + } + + OsalMemFree((void *)cfgDesc); + cfgDesc = NULL; +} + +int32_t AudioEffectGetConfigDescriptor(const char *path, struct ConfigDescriptor **cfgDesc) +{ + if (path == NULL || cfgDesc == NULL) { + HDF_LOGE("%{public}s: invalid parameter!", __func__); + return HDF_ERR_INVALID_PARAM; + } + + cJSON *cJsonObj = GetAudioEffectConfigToJsonObj(path); + if (cJsonObj == NULL) { + HDF_LOGE("%{public}s: get cJsonObj failed!", __func__); + return HDF_FAILURE; + } + + *cfgDesc = (struct ConfigDescriptor *)OsalMemCalloc(sizeof(struct ConfigDescriptor)); + if (*cfgDesc == NULL) { + HDF_LOGE("%{public}s: alloc libDescs failed", __func__); + cJSON_Delete(cJsonObj); + cJsonObj = NULL; + return HDF_ERR_MALLOC_FAIL; + } + + if (AudioEffectGetLibraryCfgDescs(cJsonObj, "libraries", *cfgDesc) != HDF_SUCCESS) { + HDF_LOGE("%{public}s: get library config failed", __func__); + AudioEffectReleaseCfgDesc(*cfgDesc); + cJSON_Delete(cJsonObj); + cJsonObj = NULL; + return HDF_FAILURE; + } + + if (AudioEffectGetEffectCfgDescs(cJsonObj, "effects", *cfgDesc) != HDF_SUCCESS) { + HDF_LOGE("%{public}s: get effect config failed", __func__); + AudioEffectReleaseCfgDesc(*cfgDesc); + cJSON_Delete(cJsonObj); + cJsonObj = NULL; + return HDF_FAILURE; + } + + cJSON_Delete(cJsonObj); + cJsonObj = NULL; + HDF_LOGE("%{public}s success", __func__); + return HDF_SUCCESS; +} diff --git a/audio/effect/host/audio_effect_driver.c b/audio/effect/host/audio_effect_driver.c index 17d042846a487cfc9b8e82e3a19cc2a10bdb7945..ab83e64faa582a3f935bdc4142e8304a0ad3ab52 100644 --- a/audio/effect/host/audio_effect_driver.c +++ b/audio/effect/host/audio_effect_driver.c @@ -67,7 +67,7 @@ static int32_t HdfEffectDriverInit(struct HdfDeviceObject *deviceObject) static int32_t HdfEffectModelDriverBind(struct HdfDeviceObject *deviceObject) { - HDF_LOGI("enter."); + HDF_LOGI("enter to %{public}s.", __func__); if (deviceObject == NULL) { HDF_LOGE("%{public}s:Param is NULL!", __func__); return HDF_ERR_INVALID_PARAM; @@ -112,7 +112,7 @@ static int32_t HdfEffectModelDriverBind(struct HdfDeviceObject *deviceObject) static void HdfEffectModelDriverRelease(struct HdfDeviceObject *deviceObject) { - HDF_LOGI("enter."); + HDF_LOGI("enter to %{public}s.", __func__); if (deviceObject == NULL) { HDF_LOGE("%{public}s:Param is NULL!", __func__); return; diff --git a/audio/effect/model/BUILD.gn b/audio/effect/model/BUILD.gn index 159c1be25ff53dbd600637343551aab0f8ba47e3..a4f2c22f8603c2ddf5698be3eb2dc65222fc8ebf 100644 --- a/audio/effect/model/BUILD.gn +++ b/audio/effect/model/BUILD.gn @@ -9,7 +9,7 @@ # 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. +# limitations under the License. import("//build/ohos.gni") import("//drivers/hdf_core/adapter/uhdf2/uhdf.gni") @@ -17,6 +17,7 @@ import("//drivers/hdf_core/adapter/uhdf2/uhdf.gni") if (!defined(ohos_lite)) { ohos_shared_library("effect_model_service_1.0") { sources = [ + "//drivers/peripheral/audio/effect/config/src/parse_effect_config.c", "src/effect_control.c", "src/effect_core.c", "src/effect_model.c", @@ -25,9 +26,12 @@ if (!defined(ohos_lite)) { include_dirs = [ "./include", "//third_party/bounds_checking_function/include", + "//drivers/peripheral/audio/effect/config/include/", + "//third_party/cJSON/", ] defines = [] + deps = [ "//third_party/cJSON:cjson" ] if (is_standard_system) { external_deps = [ diff --git a/audio/effect/model/include/effect_compatible_access.h b/audio/effect/model/include/effect_compatible_access.h index ef6582d325388fa0f3144f2110116d982a408932..977f22a5b5448f6e4086ce8bcaffa6e7fd446b84 100644 --- a/audio/effect/model/include/effect_compatible_access.h +++ b/audio/effect/model/include/effect_compatible_access.h @@ -109,6 +109,20 @@ struct EffectFactory { * @version 1.0 */ int32_t (*DestroyController)(struct EffectFactory *self, struct EffectControl *handle); + + /** + * @brief Get the effect descriptor by effectId. + * + * @param self Indicates the pointer to the effect interfaces to operate. + * @param effectId Indicates the effectId of the effect. + * @param desc Indicates the descriptor of the effect controller. + * + * @return Returns 0 if the command send success; returns a non-zero value otherwise. + * + * @since 4.0 + * @version 1.0 + */ + int32_t (*GetDescriptor)(struct EffectFactory *self, const char *effectId, struct EffectControllerDescriptor *desc); }; /* this name is going to get effect lib, it has to be realized */ diff --git a/audio/effect/model/include/effect_core.h b/audio/effect/model/include/effect_core.h index ae5a53f6502eb85187ef123c020a2752fabd4ec7..71b3b4b3140947d5ae9ede3274c7f95dab96bef0 100644 --- a/audio/effect/model/include/effect_core.h +++ b/audio/effect/model/include/effect_core.h @@ -36,7 +36,7 @@ struct ControllerManagerNode { /* declare the function here */ bool IsEffectLibExist(); int32_t RegisterEffectLibToList(void *handle, struct EffectFactory *factLib); -struct EffectFactory *GetEffectLibFromList(char *effectLibName); +struct EffectFactory *GetEffectLibFromList(const char *effectLibName); void ReleaseLibFromList(); int32_t RegisterControllerToList(struct ControllerManager *ctrlMgr); struct ControllerManager *GetControllerFromList(char *effectId); diff --git a/audio/effect/model/src/effect_control.c b/audio/effect/model/src/effect_control.c index a9e6842b76b5881d04d6f3cd0516627e665b0872..ed11010179e8496091ba62859036f1632f2b085b 100644 --- a/audio/effect/model/src/effect_control.c +++ b/audio/effect/model/src/effect_control.c @@ -16,9 +16,8 @@ #include "effect_core.h" #include "hdf_base.h" #include "hdf_log.h" -#include "osal_mem.h" -#define HDF_LOG_TAG HDF_EFFECT +#define HDF_LOG_TAG HDF_AUDIO_EFFECT int32_t EffectControlEffectProcess(struct IEffectControl *self, const struct AudioEffectBuffer *input, struct AudioEffectBuffer *output) @@ -63,10 +62,10 @@ int32_t EffectGetOwnDescriptor(struct IEffectControl *self, struct EffectControl } struct ControllerManager *manager = (struct ControllerManager *)self; - if (manager->ctrlOps == NULL || manager->ctrlOps->GetEffectDescriptor) { + if (manager->ctrlOps == NULL || manager->ctrlOps->GetEffectDescriptor == NULL) { HDF_LOGE("%{public}s: controller has no options", __func__); return HDF_FAILURE; } return manager->ctrlOps->GetEffectDescriptor(manager->ctrlOps, desc); -} \ No newline at end of file +} diff --git a/audio/effect/model/src/effect_core.c b/audio/effect/model/src/effect_core.c index ff91b4da25a18ff5b9f01a5d635caac2a87ff899..8662e3d4d11a9abc0dab280bab4d6a546991e401 100644 --- a/audio/effect/model/src/effect_core.c +++ b/audio/effect/model/src/effect_core.c @@ -74,7 +74,7 @@ void ReleaseLibFromList() } // get the lib by libname -struct EffectFactory *GetEffectLibFromList(char *effectLibName) +struct EffectFactory *GetEffectLibFromList(const char *effectLibName) { struct EffectFactoryLibListNode *tmpNode = NULL; if (effectLibName == NULL) { diff --git a/audio/effect/model/src/effect_model.c b/audio/effect/model/src/effect_model.c index 1ba41eb7063731319cc249eb9aad1d7a895eb5b8..9dc3a0bc2db0041efee0b68ad9c9019df37b6004 100644 --- a/audio/effect/model/src/effect_model.c +++ b/audio/effect/model/src/effect_model.c @@ -12,12 +12,16 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - +#include +#include #include "effect_core.h" #include "hdf_log.h" #include "osal_mem.h" +#include "parse_effect_config.h" +#define AUDIO_EFFECT_CONFIG HDF_CONFIG_DIR"/audio_effect.json" #define HDF_LOG_TAG HDF_AUDIO_EFFECT +struct ConfigDescriptor *g_cfgDescs = NULL; static int32_t EffectModelIsSupplyEffectLibs(struct IEffectModel *self, bool *supply) { @@ -33,23 +37,73 @@ static int32_t EffectModelIsSupplyEffectLibs(struct IEffectModel *self, bool *su static int32_t EffectModelGetAllEffectDescriptors(struct IEffectModel *self, struct EffectControllerDescriptor *descs, uint32_t *descsLen) { + HDF_LOGI("enter to %{public}s", __func__); + int32_t ret; + uint32_t i; + uint32_t descNum = 0; + struct EffectFactory *factLib = NULL; + if (self == NULL || descs == NULL || descsLen == NULL) { HDF_LOGE("%{public}s: invailid input params", __func__); return HDF_ERR_INVALID_PARAM; } + if (g_cfgDescs == NULL) { + HDF_LOGE("%{public}s: point is null!", __func__); + return HDF_FAILURE; + } + + for (i = 0; i < g_cfgDescs->effectNum; i++) { + factLib = GetEffectLibFromList(g_cfgDescs->effectCfgDescs[i].library); + if (factLib == NULL) { + HDF_LOGE("%{public}s: GetEffectLibFromList fail!", __func__); + continue; + } + ret = factLib->GetDescriptor(factLib, g_cfgDescs->effectCfgDescs[i].effectId, &descs[descNum]); + if (ret != HDF_SUCCESS) { + HDF_LOGE("%{public}s: GetDescriptor fail!", __func__); + continue; + } + descNum++; + } + *descsLen = descNum; + + HDF_LOGI("%{public}s success", __func__); return HDF_SUCCESS; } static int32_t EffectModelGetEffectDescriptor(struct IEffectModel *self, const char *uuid, struct EffectControllerDescriptor *desc) { + HDF_LOGI("enter to %{public}s", __func__); + uint32_t i; + struct EffectFactory *factLib = NULL; if (self == NULL || uuid == NULL || desc == NULL) { HDF_LOGE("%{public}s: invailid input params", __func__); return HDF_ERR_INVALID_PARAM; } - return HDF_SUCCESS; + for (i = 0; i < g_cfgDescs->effectNum; i++) { + if (strcmp(uuid, g_cfgDescs->effectCfgDescs[i].effectId) != 0) { + continue; + } + + factLib = GetEffectLibFromList(g_cfgDescs->effectCfgDescs[i].library); + if (factLib == NULL) { + HDF_LOGE("%{public}s: GetEffectLibFromList fail!", __func__); + return HDF_FAILURE; + } + + if (factLib->GetDescriptor(factLib, uuid, desc) != HDF_SUCCESS) { + HDF_LOGE("%{public}s: GetDescriptor fail!", __func__); + return HDF_FAILURE; + } + HDF_LOGI("%{public}s success", __func__); + return HDF_SUCCESS; + } + + HDF_LOGE("%{public}s fail!", __func__); + return HDF_FAILURE; } static int32_t EffectModelCreateEffectController(struct IEffectModel *self, const struct EffectInfo *info, @@ -176,13 +230,64 @@ static int32_t RegLibraryInstByName(char *libPath) dlclose(libHandle); return HDF_FAILURE; } + return HDF_SUCCESS; +} + +static int32_t RegLibraryInst(struct LibraryConfigDescriptor **libCfgDescs, const uint32_t libNum) +{ + int32_t ret; + uint32_t i; + char path[PATH_MAX]; + char pathBuf[PATH_MAX]; + if (libCfgDescs == NULL || libNum == 0 || libNum > HDF_EFFECT_LIB_NUM_MAX) { + HDF_LOGE("Invalid parameter!"); + return HDF_ERR_INVALID_PARAM; + } + for (i = 0; i < libNum; i++) { +#ifdef __aarch64__ +ret = snprintf_s(path, PATH_MAX, PATH_MAX, "/vendor/lib64/%s.z.so", (*libCfgDescs)[i].libPath); +#else +ret = snprintf_s(path, PATH_MAX, PATH_MAX, "/vendor/lib/%s.z.so", (*libCfgDescs)[i].libPath); +#endif + if (ret < 0) { + HDF_LOGE("%{public}s: get libPath failed", __func__); + continue; + } + + if (realpath(path, pathBuf) == NULL) { + HDF_LOGE("%{public}s: realpath is null! [%{public}d]", __func__, errno); + continue; + } + + if (RegLibraryInstByName(path) != HDF_SUCCESS) { + HDF_LOGE("%{public}s: regist library[%{private}s] failed", __func__, path); + } + } return HDF_SUCCESS; } void ModelInit() { - RegLibraryInstByName(HDF_LIBRARY_FULL_PATH("libmock_effect_lib")); + struct ConfigDescriptor *cfgDesc = NULL; + if (AudioEffectGetConfigDescriptor(AUDIO_EFFECT_CONFIG, &cfgDesc) != HDF_SUCCESS) { + HDF_LOGE("%{public}s: AudioEffectGetConfigDescriptor fail!", __func__); + return; + } + + if (cfgDesc == NULL || cfgDesc->effectCfgDescs == NULL || cfgDesc->libCfgDescs == NULL) { + HDF_LOGE("cfgDesc is null!"); + return; + } + + g_cfgDescs = cfgDesc; + if (RegLibraryInst(&(cfgDesc->libCfgDescs), cfgDesc->libNum) != HDF_SUCCESS) { + HDF_LOGE("%{public}s: RegLibraryInst failed", __func__); + AudioEffectReleaseCfgDesc(cfgDesc); + return; + } + + HDF_LOGI("%{public}s end!", __func__); } struct IEffectModel *EffectModelImplGetInstance(void) @@ -209,6 +314,7 @@ void EffectModelImplRelease(struct IEffectModel *instance) return; } + AudioEffectReleaseCfgDesc(g_cfgDescs); ReleaseLibFromList(); OsalMemFree(instance); -} \ No newline at end of file +} diff --git a/audio/effect/model/src/mock_effect/mock_effect_lib.c b/audio/effect/model/src/mock_effect/mock_effect_lib.c index 409f1d4457e8e91a899f5d22332e9e8b5941a9de..7a23b84775264ead783b54c1e4938f0bf83060c9 100644 --- a/audio/effect/model/src/mock_effect/mock_effect_lib.c +++ b/audio/effect/model/src/mock_effect/mock_effect_lib.c @@ -16,12 +16,21 @@ #include "effect_compatible_access.h" #include "hdf_log.h" #include "osal_mem.h" +#include "securec.h" +#define HDF_EFFECT_NAME_LEN 64 #define HDF_LOG_TAG HDF_AUDIO_EFFECT struct EffectHwControl { struct EffectControl impls; }; +struct EffectControllerDescriptor g_mockEffectDescriptor = { + .effectId = "aaaabbbb-8888-9999-6666-aabbccdd9966ff", + .effectName = "mock_effect", + .libName = "libmock_effect_lib", + .supplier = "mock" +}; + static int32_t MockEffectInitController(int8_t *commandData, uint32_t cmdDataLen, int8_t *replyData, uint32_t *replyDataLen) { if (commandData == NULL || replyData == NULL || replyDataLen == NULL) { @@ -147,13 +156,94 @@ static int32_t MockSendCommand(struct EffectControl *self, uint32_t cmdId, int8_ return cmdTable[cmdId].func(commandData, cmdDataLen, replyData, replyDataLen); } +static void MockEffectReleaseDesc(struct EffectControllerDescriptor *desc) +{ + if (desc == NULL) { + return; + } + + OsalMemFree((void *)desc->effectId); + desc->effectId = NULL; + + OsalMemFree((void *)desc->effectName); + desc->effectName = NULL; + + OsalMemFree((void *)desc->libName); + desc->libName = NULL; + + OsalMemFree((void *)desc->supplier); + desc->supplier = NULL; +} + +static int32_t MockCpyDesc(const char *src, char **dest) +{ + if (src == NULL || dest == NULL) { + HDF_LOGE("%{public}s: invalid parameter!", __func__); + return HDF_ERR_INVALID_PARAM; + } + + *dest = (char *)OsalMemCalloc(HDF_EFFECT_NAME_LEN * sizeof(char)); + if (*dest == NULL) { + HDF_LOGE("%{public}s: out of memory!", __func__); + return HDF_ERR_MALLOC_FAIL; + } + + if (memcpy_s((void *)(*dest), HDF_EFFECT_NAME_LEN, src, strlen(src)) != EOK) { + HDF_LOGE("%{public}s: memcpy_s effect desc fail!", __func__); + OsalMemFree((void **)dest); + return HDF_FAILURE; + } + return HDF_SUCCESS; +} + +static int32_t MockGetEffectDescriptorSub(struct EffectControllerDescriptor *desc) +{ + if (desc == NULL) { + HDF_LOGE("%{public}s: invailid input params", __func__); + return HDF_ERR_INVALID_PARAM; + } + + if (MockCpyDesc(g_mockEffectDescriptor.effectId, &(desc->effectId)) != HDF_SUCCESS) { + HDF_LOGE("%{public}s: copy item %{public}s fail!", __func__, "effectId"); + MockEffectReleaseDesc(desc); + return HDF_FAILURE; + } + + if (MockCpyDesc(g_mockEffectDescriptor.effectName, &(desc->effectName)) != HDF_SUCCESS) { + HDF_LOGE("%{public}s: copy item %{public}s fail!", __func__, "effectName"); + MockEffectReleaseDesc(desc); + return HDF_FAILURE; + } + + if (MockCpyDesc(g_mockEffectDescriptor.libName, &(desc->libName)) != HDF_SUCCESS) { + HDF_LOGE("%{public}s: copy item %{public}s fail!", __func__, "libName"); + MockEffectReleaseDesc(desc); + return HDF_FAILURE; + } + + if (MockCpyDesc(g_mockEffectDescriptor.supplier, &(desc->supplier)) != HDF_SUCCESS) { + HDF_LOGE("%{public}s: copy item %{public}s fail!", __func__, "supplier"); + MockEffectReleaseDesc(desc); + return HDF_FAILURE; + } + + return HDF_SUCCESS; +} + int32_t MockGetEffectDescriptor(struct EffectControl *self, struct EffectControllerDescriptor *desc) { + HDF_LOGI("enter to %{public}s", __func__); if (self == NULL || desc == NULL) { HDF_LOGE("%{public}s: invailid input params", __func__); return HDF_ERR_INVALID_PARAM; } + if (MockGetEffectDescriptorSub(desc) != HDF_SUCCESS) { + HDF_LOGE("%{public}s: get descriptor fail!", __func__); + return HDF_FAILURE; + } + + HDF_LOGE("%{public}s: succ", __func__); return HDF_SUCCESS; } @@ -193,15 +283,36 @@ static int32_t MockDestroyController(struct EffectFactory *self, struct EffectCo return HDF_SUCCESS; } +int32_t MockGetDescriptor(struct EffectFactory *self, const char *uuid, struct EffectControllerDescriptor *desc) +{ + HDF_LOGI("enter to %{public}s", __func__); + if (self == NULL || uuid == NULL || desc == NULL) { + HDF_LOGE("%{public}s: invailid input params", __func__); + return HDF_ERR_INVALID_PARAM; + } + + if (strcmp(uuid, g_mockEffectDescriptor.effectId) != 0) { + HDF_LOGE("%{public}s: error effectId!", __func__); + return HDF_FAILURE; + } + + if (MockGetEffectDescriptorSub(desc) != HDF_SUCCESS) { + HDF_LOGE("%{public}s: get descriptor fail!", __func__); + return HDF_FAILURE; + } + return HDF_SUCCESS; +} + struct EffectFactory g_mockFactoryLib = { .version = 1, .effectLibName = "libmock_effect_lib", .supplier = "hdf", .CreateController = MockCreateController, .DestroyController = MockDestroyController, + .GetDescriptor = MockGetDescriptor, }; struct EffectFactory *GetEffectoyFactoryLib() { return &g_mockFactoryLib; -} \ No newline at end of file +} diff --git a/audio/effect/test/BUILD.gn b/audio/effect/test/BUILD.gn index 8425fd5984fe7fcf0ecee3177f1d29da501d54cc..423483070285fe431c34950e4180082ac54117c7 100644 --- a/audio/effect/test/BUILD.gn +++ b/audio/effect/test/BUILD.gn @@ -15,12 +15,11 @@ import("//build/ohos.gni") import("//drivers/hdf_core/adapter/uhdf2/uhdf.gni") if (defined(ohos_lite)) { + group("effect_test") { + } } else { group("effect_test") { testonly = true - deps = [ - "sample:effect_demo", - "unittest:effect_hdi_ut", - ] + deps = [ "unittest:hdf_effect_hdi_ut" ] } } diff --git a/audio/effect/test/unittest/BUILD.gn b/audio/effect/test/unittest/BUILD.gn index 7264d87a4267c10501f275efc1c01c1fa6a5fd4b..224af4c28b59bed875d369edd7f9a417b2a5d862 100644 --- a/audio/effect/test/unittest/BUILD.gn +++ b/audio/effect/test/unittest/BUILD.gn @@ -14,22 +14,19 @@ import("//build/test.gni") import("//drivers/hdf_core/adapter/uhdf2/uhdf.gni") -ohos_unittest("effect_hdi_ut") { +ohos_unittest("hdf_effect_hdi_ut") { module_out_path = "hdf/audio" sources = [ + "effect_common.cpp", "effect_control_test.cpp", "effect_model_test.cpp", ] - include_dirs = [ "//third_party/googletest/googletest/include/gtest" ] - - deps = [ - "//third_party/googletest:gmock_main", - "//third_party/googletest:gtest_main", - ] + include_dirs = [ "//drivers/peripheral/audio/effect/test/unittest/" ] external_deps = [ "c_utils:utils", + "drivers_interface_audio:effect_idl_headers", "drivers_interface_audio:libeffect_proxy_1.0", "hdf_core:libhdf_utils", "hiviewdfx_hilog_native:libhilog", diff --git a/audio/effect/test/unittest/effect_common.cpp b/audio/effect/test/unittest/effect_common.cpp new file mode 100644 index 0000000000000000000000000000000000000000..93214bcebba3bbf70a3958cbc799763d8cb9607e --- /dev/null +++ b/audio/effect/test/unittest/effect_common.cpp @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2023 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. + */ + +#include "effect_common.h" +#include +#include + +void EffectControllerReleaseDesc(struct EffectControllerDescriptor *desc) +{ + if (desc == nullptr) { + return; + } + + OsalMemFree(desc->effectId); + desc->effectId = nullptr; + + OsalMemFree(desc->effectName); + desc->effectName = nullptr; + + OsalMemFree(desc->libName); + desc->libName = nullptr; + + OsalMemFree(desc->supplier); + desc->supplier = nullptr; +} + +void EffectControllerReleaseDescs(struct EffectControllerDescriptor *descs, uint32_t *descsLen) +{ + if (descs == nullptr || descsLen == nullptr || *descsLen <= 0 || *descsLen > HDF_EFFECT_NUM_MAX) { + return; + } + + for (uint32_t i = 0; i < *descsLen; i++) { + EffectControllerReleaseDesc(&descs[i]); + } +} \ No newline at end of file diff --git a/audio/effect/test/unittest/effect_common.h b/audio/effect/test/unittest/effect_common.h new file mode 100644 index 0000000000000000000000000000000000000000..6ad001cdb5d75e781495e522bea1be68c2c919da --- /dev/null +++ b/audio/effect/test/unittest/effect_common.h @@ -0,0 +1,24 @@ +/* + * Copyright (c) 2023 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_COMMON_H +#define EFFECT_COMMON_H + +#include "v1_0/effect_types.h" + +void EffectControllerReleaseDesc(struct EffectControllerDescriptor *desc); +void EffectControllerReleaseDescs(struct EffectControllerDescriptor *descs, uint32_t *descsLen); + +#endif \ No newline at end of file diff --git a/audio/effect/test/unittest/effect_control_test.cpp b/audio/effect/test/unittest/effect_control_test.cpp index ba2a07c9a4a49bde0a3f4da8724b972cf68eb91e..6d55b8502e514c9bf63dc9d262c695706693628a 100644 --- a/audio/effect/test/unittest/effect_control_test.cpp +++ b/audio/effect/test/unittest/effect_control_test.cpp @@ -13,13 +13,15 @@ * limitations under the License. */ -#include #include #include "hdf_base.h" #include "hdf_log.h" #include "v1_0/effect_types.h" #include "v1_0/ieffect_control.h" #include "v1_0/ieffect_model.h" +#include "effect_common.h" +#include "effect_core.h" +#include "osal_mem.h" using namespace std; using namespace testing::ext; @@ -28,6 +30,7 @@ constexpr bool IS_DIRECTLY_CALL = false; constexpr uint32_t SEND_COMMAND_LEN = 10; /* the output buffer len of the command */ constexpr uint32_t GET_BUFFER_LEN = 10; +# define AUDIO_EFFECT_COMMAND_INVALID_LARGE 20 namespace { class EffectControlTest : public testing::Test { @@ -43,6 +46,7 @@ public: void EffectControlTest::SetUp() { + // input testcase setup step,setup invoked before each testcases libName_ = strdup("libmock_effect_lib"); effectId_ = strdup("aaaabbbb-8888-9999-6666-aabbccdd9966ff"); struct EffectInfo info = { @@ -61,6 +65,7 @@ void EffectControlTest::SetUp() void EffectControlTest::TearDown() { + // input testcase teardown step,teardown invoked after each testcases if (libName_ != nullptr) { free(libName_); libName_ = nullptr; @@ -73,7 +78,7 @@ void EffectControlTest::TearDown() if (controller_ != nullptr) { int32_t ret = model_->DestroyEffectController(model_, &contollerId_); - ASSERT_EQ(ret, HDF_SUCCESS); + EXPECT_EQ(ret, HDF_SUCCESS); } if (model_ != nullptr) { @@ -81,15 +86,76 @@ void EffectControlTest::TearDown() } } +/** + * @tc.name: HdfAudioEffectProcess001 + * @tc.desc: Verify the EffectControlEffectProcess function when the input parameter is invalid. + * @tc.type: FUNC + * @tc.require: I6I658 + */ HWTEST_F(EffectControlTest, HdfAudioEffectProcess001, TestSize.Level1) { struct AudioEffectBuffer input = {0}; struct AudioEffectBuffer output = {0}; + EXPECT_EQ(HDF_ERR_INVALID_OBJECT, controller_->EffectProcess(nullptr, &input, &output)); + EXPECT_EQ(HDF_ERR_INVALID_PARAM, controller_->EffectProcess(controller_, nullptr, &output)); + EXPECT_EQ(HDF_ERR_INVALID_PARAM, controller_->EffectProcess(controller_, &input, nullptr)); +} + +/** + * @tc.name: HdfAudioEffectProcess002 + * @tc.desc: Verify the EffectControlEffectProcess function. + * @tc.type: FUNC + * @tc.require: I6I658 + */ +HWTEST_F(EffectControlTest, HdfAudioEffectProcess002, TestSize.Level1) +{ + struct AudioEffectBuffer input = {0}; + struct AudioEffectBuffer output = {0}; + int32_t ret = controller_->EffectProcess(controller_, &input, &output); - ASSERT_EQ(ret, HDF_SUCCESS); + EXPECT_EQ(ret, HDF_SUCCESS); } +/** + * @tc.name: HdfAudioSendCommand001 + * @tc.desc: Verify the EffectControlSendCommand function when the input parameter is invalid. + * @tc.type: FUNC + * @tc.require: I6I658 + */ +HWTEST_F(EffectControlTest, HdfAudioSendCommand001, TestSize.Level1) +{ + int8_t input[SEND_COMMAND_LEN] = {0}; + int8_t output[GET_BUFFER_LEN] = {0}; + uint32_t replyLen = GET_BUFFER_LEN; + + int32_t ret = controller_->SendCommand(nullptr, AUDIO_EFFECT_COMMAND_INIT_CONTOLLER, + input, SEND_COMMAND_LEN, output, &replyLen); + EXPECT_EQ(HDF_ERR_INVALID_OBJECT, ret); + + ret = controller_->SendCommand(controller_, AUDIO_EFFECT_COMMAND_INIT_CONTOLLER, + nullptr, SEND_COMMAND_LEN, output, &replyLen); + EXPECT_EQ(HDF_ERR_INVALID_PARAM, ret); + + ret = controller_->SendCommand(controller_, AUDIO_EFFECT_COMMAND_INIT_CONTOLLER, + input, SEND_COMMAND_LEN, nullptr, &replyLen); + EXPECT_EQ(HDF_ERR_INVALID_PARAM, ret); + + ret = controller_->SendCommand(controller_, AUDIO_EFFECT_COMMAND_INIT_CONTOLLER, + input, SEND_COMMAND_LEN, output, nullptr); + EXPECT_EQ(HDF_ERR_INVALID_PARAM, ret); + + ret = controller_->SendCommand(controller_, AUDIO_EFFECT_COMMAND_INVALID_LARGE, + input, SEND_COMMAND_LEN, nullptr, &replyLen); + EXPECT_EQ(HDF_ERR_INVALID_PARAM, ret); +} + +/** + * @tc.name: HdfAudioSendCommandInit001 + * @tc.desc: Verify the EffectControlEffectProcess function when cmdId is AUDIO_EFFECT_COMMAND_INIT_CONTOLLER. + * @tc.type: FUNC + * @tc.require: I6I658 + */ HWTEST_F(EffectControlTest, HdfAudioSendCommandInit001, TestSize.Level1) { int8_t input[SEND_COMMAND_LEN] = {0}; @@ -98,9 +164,15 @@ HWTEST_F(EffectControlTest, HdfAudioSendCommandInit001, TestSize.Level1) int32_t ret = controller_->SendCommand(controller_, AUDIO_EFFECT_COMMAND_INIT_CONTOLLER, input, SEND_COMMAND_LEN, output, &replyLen); - ASSERT_EQ(ret, HDF_SUCCESS); + EXPECT_EQ(ret, HDF_SUCCESS); } +/** + * @tc.name: HdfAudioSendCommandSetConf001 + * @tc.desc: Verify the EffectControlEffectProcess function when cmdId is AUDIO_EFFECT_COMMAND_SET_CONFIG. + * @tc.type: FUNC + * @tc.require: I6I658 + */ HWTEST_F(EffectControlTest, HdfAudioSendCommandSetConf001, TestSize.Level1) { int8_t input[SEND_COMMAND_LEN] = {0}; @@ -109,9 +181,15 @@ HWTEST_F(EffectControlTest, HdfAudioSendCommandSetConf001, TestSize.Level1) int32_t ret = controller_->SendCommand(controller_, AUDIO_EFFECT_COMMAND_SET_CONFIG, input, SEND_COMMAND_LEN, output, &replyLen); - ASSERT_EQ(ret, HDF_SUCCESS); + EXPECT_EQ(ret, HDF_SUCCESS); } +/** + * @tc.name: HdfAudioSendCommandGetConf001 + * @tc.desc: Verify the EffectControlEffectProcess function when cmdId is AUDIO_EFFECT_COMMAND_GET_CONFIG. + * @tc.type: FUNC + * @tc.require: I6I658 + */ HWTEST_F(EffectControlTest, HdfAudioSendCommandGetConf001, TestSize.Level1) { int8_t input[SEND_COMMAND_LEN] = {0}; @@ -120,9 +198,15 @@ HWTEST_F(EffectControlTest, HdfAudioSendCommandGetConf001, TestSize.Level1) int32_t ret = controller_->SendCommand(controller_, AUDIO_EFFECT_COMMAND_GET_CONFIG, input, SEND_COMMAND_LEN, output, &replyLen); - ASSERT_EQ(ret, HDF_SUCCESS); + EXPECT_EQ(ret, HDF_SUCCESS); } +/** + * @tc.name: HdfAudioSendCommandRest001 + * @tc.desc: Verify the EffectControlEffectProcess function when cmdId is AUDIO_EFFECT_COMMAND_RESET. + * @tc.type: FUNC + * @tc.require: I6I658 + */ HWTEST_F(EffectControlTest, HdfAudioSendCommandRest001, TestSize.Level1) { int8_t input[SEND_COMMAND_LEN] = {0}; @@ -131,9 +215,15 @@ HWTEST_F(EffectControlTest, HdfAudioSendCommandRest001, TestSize.Level1) int32_t ret = controller_->SendCommand(controller_, AUDIO_EFFECT_COMMAND_RESET, input, SEND_COMMAND_LEN, output, &replyLen); - ASSERT_EQ(ret, HDF_SUCCESS); + EXPECT_EQ(ret, HDF_SUCCESS); } +/** + * @tc.name: HdfAudioSendCommandEnable001 + * @tc.desc: Verify the EffectControlEffectProcess function when cmdId is AUDIO_EFFECT_COMMAND_ENABLE. + * @tc.type: FUNC + * @tc.require: I6I658 + */ HWTEST_F(EffectControlTest, HdfAudioSendCommandEnable001, TestSize.Level1) { int8_t input[SEND_COMMAND_LEN] = {0}; @@ -142,9 +232,15 @@ HWTEST_F(EffectControlTest, HdfAudioSendCommandEnable001, TestSize.Level1) int32_t ret = controller_->SendCommand(controller_, AUDIO_EFFECT_COMMAND_ENABLE, input, SEND_COMMAND_LEN, output, &replyLen); - ASSERT_EQ(ret, HDF_SUCCESS); + EXPECT_EQ(ret, HDF_SUCCESS); } +/** + * @tc.name: HdfAudioSendCommandDisable001 + * @tc.desc: Verify the EffectControlEffectProcess function when cmdId is AUDIO_EFFECT_COMMAND_DISABLE. + * @tc.type: FUNC + * @tc.require: I6I658 + */ HWTEST_F(EffectControlTest, HdfAudioSendCommandDisable001, TestSize.Level1) { int8_t input[SEND_COMMAND_LEN] = {0}; @@ -152,9 +248,15 @@ HWTEST_F(EffectControlTest, HdfAudioSendCommandDisable001, TestSize.Level1) uint32_t replyLen = GET_BUFFER_LEN; int32_t ret = controller_->SendCommand(controller_, AUDIO_EFFECT_COMMAND_DISABLE, input, SEND_COMMAND_LEN, output, &replyLen); - ASSERT_EQ(ret, HDF_SUCCESS); + EXPECT_EQ(ret, HDF_SUCCESS); } +/** + * @tc.name: HdfAudioSendCommandSetParam001 + * @tc.desc: Verify the EffectControlEffectProcess function when cmdId is AUDIO_EFFECT_COMMAND_SET_PARAM. + * @tc.type: FUNC + * @tc.require: I6I658 + */ HWTEST_F(EffectControlTest, HdfAudioSendCommandSetParam001, TestSize.Level1) { int8_t input[SEND_COMMAND_LEN] = {0}; @@ -163,9 +265,15 @@ HWTEST_F(EffectControlTest, HdfAudioSendCommandSetParam001, TestSize.Level1) int32_t ret = controller_->SendCommand(controller_, AUDIO_EFFECT_COMMAND_SET_PARAM, input, SEND_COMMAND_LEN, output, &replyLen); - ASSERT_EQ(ret, HDF_SUCCESS); + EXPECT_EQ(ret, HDF_SUCCESS); } +/** + * @tc.name: HdfAudioSendCommandGetParam001 + * @tc.desc: Verify the EffectControlEffectProcess function when cmdId is AUDIO_EFFECT_COMMAND_GET_PARAM. + * @tc.type: FUNC + * @tc.require: I6I658 + */ HWTEST_F(EffectControlTest, HdfAudioSendCommandGetParam001, TestSize.Level1) { int8_t input[SEND_COMMAND_LEN] = {0}; @@ -174,7 +282,38 @@ HWTEST_F(EffectControlTest, HdfAudioSendCommandGetParam001, TestSize.Level1) int32_t ret = controller_->SendCommand(controller_, AUDIO_EFFECT_COMMAND_GET_PARAM, input, SEND_COMMAND_LEN, output, &replyLen); - ASSERT_EQ(ret, HDF_SUCCESS); + EXPECT_EQ(ret, HDF_SUCCESS); } -} // end of namespace \ No newline at end of file +/** + * @tc.name: HdfAudioGetDescriptor001 + * @tc.desc: Verify the EffectGetOwnDescriptor function when the input parameter is invalid. + * @tc.type: FUNC + * @tc.require: I6I658 + */ +HWTEST_F(EffectControlTest, HdfAudioGetDescriptor001, TestSize.Level1) +{ + struct EffectControllerDescriptor desc; + + EXPECT_EQ(HDF_ERR_INVALID_OBJECT, controller_->GetEffectDescriptor(nullptr, &desc)); + EXPECT_EQ(HDF_ERR_INVALID_PARAM, controller_->GetEffectDescriptor(controller_, nullptr)); +} + +/** + * @tc.name: HdfAudioGetDescriptor002 + * @tc.desc: Verify the EffectGetOwnDescriptor function. + * @tc.type: FUNC + * @tc.require: I6I658 + */ +HWTEST_F(EffectControlTest, HdfAudioGetDescriptor002, TestSize.Level1) +{ + struct EffectControllerDescriptor desc; + int32_t ret = controller_->GetEffectDescriptor(controller_, &desc); + EXPECT_EQ(ret, HDF_SUCCESS); + EXPECT_STREQ(desc.effectId, effectId_); + EXPECT_STREQ(desc.effectName, "mock_effect"); + EXPECT_STREQ(desc.libName, libName_); + EXPECT_STREQ(desc.supplier, "mock"); + EffectControllerReleaseDesc(&desc); +} +} // end of namespace diff --git a/audio/effect/test/unittest/effect_model_test.cpp b/audio/effect/test/unittest/effect_model_test.cpp index 5ad7fa6b0ce3e3eaf78c639608463da6f2427de8..06514254d14e04350eb21ac005614d251d46725d 100644 --- a/audio/effect/test/unittest/effect_model_test.cpp +++ b/audio/effect/test/unittest/effect_model_test.cpp @@ -13,13 +13,14 @@ * limitations under the License. */ -#include #include #include "hdf_base.h" #include "hdf_log.h" #include "v1_0/effect_types.h" #include "v1_0/ieffect_control.h" #include "v1_0/ieffect_model.h" +#include "effect_common.h" +#include "osal_mem.h" using namespace std; using namespace testing::ext; @@ -39,6 +40,7 @@ public: void EffectModelTest::SetUp() { + // input testcase setup step,setup invoked before each testcases libName_ = strdup("libmock_effect_lib"); effectId_ = strdup("aaaabbbb-8888-9999-6666-aabbccdd9966ff"); model_ = IEffectModelGet(IS_DIRECTLY_CALL); @@ -47,6 +49,7 @@ void EffectModelTest::SetUp() void EffectModelTest::TearDown() { + // input testcase teardown step,teardown invoked after each testcases if (libName_ != nullptr) { free(libName_); libName_ = nullptr; @@ -62,17 +65,92 @@ void EffectModelTest::TearDown() } } +/** + * @tc.name: HdfAudioIsSupplyEffectLibs001 + * @tc.desc: Verify the EffectModelIsSupplyEffectLibs function when the input parameter is invalid. + * @tc.type: FUNC + * @tc.require: I6I658 + */ HWTEST_F(EffectModelTest, HdfAudioIsSupplyEffectLibs001, TestSize.Level1) +{ + bool isSupport = false; + EXPECT_EQ(HDF_ERR_INVALID_OBJECT, model_->IsSupplyEffectLibs(nullptr, &isSupport)); + EXPECT_EQ(HDF_ERR_INVALID_PARAM, model_->IsSupplyEffectLibs(model_, nullptr)); +} + +/** + * @tc.name: HdfAudioIsSupplyEffectLibs002 + * @tc.desc: Verify the EffectModelIsSupplyEffectLibs function. + * @tc.type: FUNC + * @tc.require: I6I658 + */ +HWTEST_F(EffectModelTest, HdfAudioIsSupplyEffectLibs002, TestSize.Level1) { bool isSupport = false; int32_t ret = model_->IsSupplyEffectLibs(model_, &isSupport); + EXPECT_EQ(ret, HDF_SUCCESS); +} + +/** + * @tc.name: HdfAudioGetAllEffectDescriptors001 + * @tc.desc: Verify the EffectModelGetAllEffectDescriptors function when the input parameter is invalid. + * @tc.type: FUNC + * @tc.require: I6I658 + */ +HWTEST_F(EffectModelTest, HdfAudioGetAllEffectDescriptors001, TestSize.Level1) +{ + uint32_t descsLen = MAX_DESCRIPTOR_NUM; + struct EffectControllerDescriptor descs[MAX_DESCRIPTOR_NUM]; + + EXPECT_EQ(HDF_ERR_INVALID_OBJECT, model_->GetAllEffectDescriptors(nullptr, descs, &descsLen)); + EXPECT_EQ(HDF_ERR_INVALID_PARAM, model_->GetAllEffectDescriptors(model_, nullptr, &descsLen)); + EXPECT_EQ(HDF_ERR_INVALID_PARAM, model_->GetAllEffectDescriptors(model_, descs, nullptr)); +} + +/** + * @tc.name: HdfAudioGetAllEffectDescriptors002 + * @tc.desc: Verify the EffectModelGetAllEffectDescriptors function. + * @tc.type: FUNC + * @tc.require: I6I658 + */ +HWTEST_F(EffectModelTest, HdfAudioGetAllEffectDescriptors002, TestSize.Level1) +{ + uint32_t descsLen = MAX_DESCRIPTOR_NUM; + struct EffectControllerDescriptor descs[MAX_DESCRIPTOR_NUM]; + + int32_t ret = model_->GetAllEffectDescriptors(model_, descs, &descsLen); ASSERT_EQ(ret, HDF_SUCCESS); + EXPECT_GE(MAX_DESCRIPTOR_NUM, descsLen); +} - if (isSupport == false) { - HDF_LOGE("remind that the vendor doesn't supply effect libs"); +/** + * @tc.name: HdfAudioGetAllEffectDescriptors003 + * @tc.desc: Verify the descs of EffectModelGetAllEffectDescriptors function. + * @tc.type: FUNC + * @tc.require: I6I658 + */ +HWTEST_F(EffectModelTest, HdfAudioGetAllEffectDescriptors003, TestSize.Level1) +{ + uint32_t descsLen = MAX_DESCRIPTOR_NUM; + struct EffectControllerDescriptor descs[MAX_DESCRIPTOR_NUM]; + + int32_t ret = model_->GetAllEffectDescriptors(model_, descs, &descsLen); + ASSERT_EQ(ret, HDF_SUCCESS); + EXPECT_GE(MAX_DESCRIPTOR_NUM, descsLen); + + for (uint32_t i = 0; i < descsLen; i++) { + EXPECT_NE(nullptr, descs[i].effectId); } + + EffectControllerReleaseDescs(descs, &descsLen); } +/** + * @tc.name: HdfAudioCreateDestroyController001 + * @tc.desc: Verify the EffectModelCreateEffectController and EffectModelDestroyEffectController function. + * @tc.type: FUNC + * @tc.require: I6I658 + */ HWTEST_F(EffectModelTest, HdfAudioCreateDestroyController001, TestSize.Level1) { struct EffectInfo info = { @@ -92,21 +170,37 @@ HWTEST_F(EffectModelTest, HdfAudioCreateDestroyController001, TestSize.Level1) } } -HWTEST_F(EffectModelTest, HdfAudioGetAllEffecctDescriptor001, TestSize.Level1) +/** + * @tc.name: HdfAudioGetEffectDescriptor001 + * @tc.desc: Verify the EffectModelGetEffectDescriptor function when the input parameter is invalid. + * @tc.type: FUNC + * @tc.require: I6I658 + */ +HWTEST_F(EffectModelTest, HdfAudioGetEffectDescriptor001, TestSize.Level1) { - uint32_t descsLen = MAX_DESCRIPTOR_NUM; - struct EffectControllerDescriptor descs[MAX_DESCRIPTOR_NUM]; + struct EffectControllerDescriptor desc; - int32_t ret = model_->GetAllEffectDescriptors(model_, descs, &descsLen); - ASSERT_EQ(ret, HDF_SUCCESS); + EXPECT_EQ(HDF_ERR_INVALID_OBJECT, model_->GetEffectDescriptor(nullptr, effectId_, &desc)); + EXPECT_EQ(HDF_ERR_INVALID_PARAM, model_->GetEffectDescriptor(model_, nullptr, &desc)); + EXPECT_EQ(HDF_ERR_INVALID_PARAM, model_->GetEffectDescriptor(model_, effectId_, nullptr)); } -HWTEST_F(EffectModelTest, HdfAudioGetEffectDescriptor001, TestSize.Level1) +/** + * @tc.name: HdfAudioGetEffectDescriptor002 + * @tc.desc: Verify the EffectModelGetEffectDescriptor function. + * @tc.type: FUNC + * @tc.require: I6I658 + */ +HWTEST_F(EffectModelTest, HdfAudioGetEffectDescriptor002, TestSize.Level1) { struct EffectControllerDescriptor desc; int32_t ret = model_->GetEffectDescriptor(model_, effectId_, &desc); - ASSERT_EQ(ret, HDF_SUCCESS); + EXPECT_EQ(ret, HDF_SUCCESS); + EXPECT_STREQ(desc.effectId, effectId_); + EXPECT_STREQ(desc.effectName, "mock_effect"); + EXPECT_STREQ(desc.libName, libName_); + EXPECT_STREQ(desc.supplier, "mock"); + EffectControllerReleaseDesc(&desc); } - -} // end of namespace \ No newline at end of file +} // end of namespace