1 /*
2  * Copyright (c) 2024 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 
16 #ifndef LOG_TAG
17 #define LOG_TAG "AudioEnhanceChainAdapter"
18 #endif
19 
20 #include "audio_enhance_chain_adapter.h"
21 #include <map>
22 
23 #include "securec.h"
24 #include "audio_effect_log.h"
25 #include "audio_effect.h"
26 #include "audio_errors.h"
27 #include "audio_enhance_chain_manager.h"
28 
29 using namespace OHOS::AudioStandard;
30 
EnhanceChainManagerProcess(const char * sceneType,EnhanceBufferAttr * enhanceBufferAttr)31 int32_t EnhanceChainManagerProcess(const char *sceneType, EnhanceBufferAttr *enhanceBufferAttr)
32 {
33     AudioEnhanceChainManager *audioEnhanceChainMananger = AudioEnhanceChainManager::GetInstance();
34     CHECK_AND_RETURN_RET_LOG(audioEnhanceChainMananger != nullptr,
35         ERR_INVALID_HANDLE, "null audioEnhanceChainManager");
36     std::string sceneTypeString = "";
37     if (sceneType) {
38         sceneTypeString = sceneType;
39     }
40     if (audioEnhanceChainMananger->ApplyAudioEnhanceChain(sceneTypeString, enhanceBufferAttr) != SUCCESS) {
41         AUDIO_ERR_LOG("%{public}s process failed", sceneType);
42         return ERROR;
43     }
44     AUDIO_DEBUG_LOG("%{public}s process success", sceneType);
45     return SUCCESS;
46 }
47 
EnhanceChainManagerCreateCb(const char * sceneType,const char * enhanceMode,const char * upAndDownDevice)48 int32_t EnhanceChainManagerCreateCb(const char *sceneType, const char *enhanceMode, const char *upAndDownDevice)
49 {
50     AudioEnhanceChainManager *audioEnhanceChainMananger = AudioEnhanceChainManager::GetInstance();
51     CHECK_AND_RETURN_RET_LOG(audioEnhanceChainMananger != nullptr,
52         ERR_INVALID_HANDLE, "null audioEnhanceChainManager");
53     std::string sceneTypeString = "";
54     std::string enhanceModeString = "";
55     std::string upAndDownDeviceString = "";
56     if (sceneType) {
57         sceneTypeString = sceneType;
58     }
59     if (enhanceMode) {
60         enhanceModeString = enhanceMode;
61     }
62     if (upAndDownDevice) {
63         upAndDownDeviceString = upAndDownDevice;
64     }
65     if (audioEnhanceChainMananger->CreateAudioEnhanceChainDynamic(sceneTypeString,
66         enhanceModeString, upAndDownDeviceString) != SUCCESS) {
67         return ERROR;
68     }
69     return SUCCESS;
70 }
71 
EnhanceChainManagerReleaseCb(const char * sceneType,const char * enhanceMode,const char * upAndDownDevice)72 int32_t EnhanceChainManagerReleaseCb(const char *sceneType, const char *enhanceMode, const char *upAndDownDevice)
73 {
74     AudioEnhanceChainManager *audioEnhanceChainMananger = AudioEnhanceChainManager::GetInstance();
75     CHECK_AND_RETURN_RET_LOG(audioEnhanceChainMananger != nullptr,
76         ERR_INVALID_HANDLE, "null audioEnhanceChainManager");
77     std::string sceneTypeString = "";
78     std::string upAndDownDeviceString = "";
79     if (sceneType) {
80         sceneTypeString = sceneType;
81     }
82     if (upAndDownDevice) {
83         upAndDownDeviceString = upAndDownDevice;
84     }
85     if (audioEnhanceChainMananger->ReleaseAudioEnhanceChainDynamic(sceneTypeString,
86         upAndDownDeviceString) != SUCCESS) {
87         return ERROR;
88     }
89     return SUCCESS;
90 }
91 
EnhanceChainManagerExist(const char * sceneType)92 bool EnhanceChainManagerExist(const char *sceneType)
93 {
94     AudioEnhanceChainManager *audioEnhanceChainMananger = AudioEnhanceChainManager::GetInstance();
95     CHECK_AND_RETURN_RET_LOG(audioEnhanceChainMananger != nullptr,
96         ERR_INVALID_HANDLE, "null audioEnhanceChainManager");
97     std::string sceneTypeString = "";
98     if (sceneType) {
99         sceneTypeString = sceneType;
100     }
101     return audioEnhanceChainMananger->ExistAudioEnhanceChain(sceneTypeString);
102 }
103