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 
16 #include "effect_host_common.h"
17 
18 #include "hdf_base.h"
19 #include "v1_0/effect_types.h"
20 #include "v1_0/effect_types_vdi.h"
21 #include "v1_0/ieffect_control_vdi.h"
22 #include "audio_uhdf_log.h"
23 #include "osal_mem.h"
24 
25 #define HDF_LOG_TAG HDF_AUDIO_EFFECT
26 
EffectControlEffectProcess(struct IEffectControl * self,const struct AudioEffectBuffer * input,struct AudioEffectBuffer * output)27 int32_t EffectControlEffectProcess(struct IEffectControl *self, const struct AudioEffectBuffer *input,
28     struct AudioEffectBuffer *output)
29 {
30     if (self == NULL || input == NULL || output == NULL) {
31         HDF_LOGE("%{public}s: invailid input params", __func__);
32         return HDF_ERR_INVALID_PARAM;
33     }
34 
35     struct ControllerManager *manager = (struct ControllerManager *)self;
36     if (manager->ctrlOps == NULL || manager->ctrlOps->EffectProcess == NULL) {
37         HDF_LOGE("%{public}s: controller has no options", __func__);
38         return HDF_FAILURE;
39     }
40     if (strcmp(manager->libName, "libmock_effect_lib") != 0) {
41         output->frameCount = input->frameCount;
42         output->datatag = input->datatag;
43         output->rawDataLen = input->rawDataLen;
44         output->rawData = (int8_t *)OsalMemCalloc(sizeof(int8_t) * output->rawDataLen);
45         if (output->rawData == NULL) {
46             HDF_LOGE("%{public}s: OsalMemCalloc fail", __func__);
47             return HDF_FAILURE;
48         }
49     }
50     struct AudioEffectBufferVdi *inputVdi = (struct AudioEffectBufferVdi *)input;
51     struct AudioEffectBufferVdi *outputVdi = (struct AudioEffectBufferVdi *)output;
52     int32_t ret = manager->ctrlOps->EffectProcess(manager->ctrlOps, inputVdi, outputVdi);
53     if (ret != HDF_SUCCESS) {
54         HDF_LOGE("AudioEffectProcess failed, ret=%{public}d", ret);
55         return ret;
56     }
57 
58     output = (struct AudioEffectBuffer *)outputVdi;
59     return ret;
60 }
61 
EffectControlSendCommand(struct IEffectControl * self,enum EffectCommandTableIndex cmdId,const int8_t * cmdData,uint32_t cmdDataLen,int8_t * replyData,uint32_t * replyDataLen)62 int32_t EffectControlSendCommand(struct IEffectControl *self, enum EffectCommandTableIndex cmdId, const int8_t *cmdData,
63     uint32_t cmdDataLen, int8_t *replyData, uint32_t *replyDataLen)
64 {
65     if (self == NULL || cmdData == NULL || replyData == NULL || replyDataLen == NULL) {
66         HDF_LOGE("%{public}s: invailid input params", __func__);
67         return HDF_ERR_INVALID_PARAM;
68     }
69 
70     struct ControllerManager *manager = (struct ControllerManager *)self;
71     if (manager->ctrlOps == NULL || manager->ctrlOps->SendCommand == NULL) {
72         HDF_LOGE("%{public}s: controller has no options", __func__);
73         return HDF_FAILURE;
74     }
75     enum EffectCommandTableIndexVdi cmdIdVdi = (enum EffectCommandTableIndexVdi)cmdId;
76     int32_t ret = manager->ctrlOps->SendCommand(manager->ctrlOps, cmdIdVdi, (void *)cmdData, cmdDataLen,
77                                          (void *)replyData, replyDataLen);
78     if (ret != HDF_SUCCESS) {
79         HDF_LOGE("SendCommand failed, ret=%{public}d", ret);
80         return ret;
81     }
82     return ret;
83 }
84 
EffectGetOwnDescriptor(struct IEffectControl * self,struct EffectControllerDescriptor * desc)85 int32_t EffectGetOwnDescriptor(struct IEffectControl *self, struct EffectControllerDescriptor *desc)
86 {
87     if (self == NULL || desc == NULL) {
88         HDF_LOGE("%{public}s: invailid input params", __func__);
89         return HDF_ERR_INVALID_PARAM;
90     }
91 
92     struct ControllerManager *manager = (struct ControllerManager *)self;
93     if (manager->ctrlOps == NULL || manager->ctrlOps->GetEffectDescriptor == NULL) {
94         HDF_LOGE("%{public}s: controller has no options", __func__);
95         return HDF_FAILURE;
96     }
97     struct EffectControllerDescriptorVdi *descVdi = (struct EffectControllerDescriptorVdi *)desc;
98     int32_t ret = manager->ctrlOps->GetEffectDescriptor(manager->ctrlOps, descVdi);
99     if (ret != HDF_SUCCESS) {
100         HDF_LOGE("EffectGetOwnDescriptor failed, ret=%{public}d", ret);
101         return ret;
102     }
103 
104     desc = (struct EffectControllerDescriptor *)descVdi;
105     return ret;
106 }
107 
EffectControlEffectReverse(struct IEffectControl * self,const struct AudioEffectBuffer * input,struct AudioEffectBuffer * output)108 int32_t EffectControlEffectReverse(struct IEffectControl *self, const struct AudioEffectBuffer *input,
109     struct AudioEffectBuffer *output)
110 {
111     if (self == NULL || input == NULL || output == NULL) {
112         HDF_LOGE("%{public}s: invailid input params", __func__);
113         return HDF_ERR_INVALID_PARAM;
114     }
115 
116     struct ControllerManager *manager = (struct ControllerManager *)self;
117     if (manager->ctrlOps == NULL || manager->ctrlOps->EffectReverse == NULL) {
118         HDF_LOGE("%{public}s: controller has no options", __func__);
119         return HDF_FAILURE;
120     }
121     struct AudioEffectBufferVdi *inputVdi = (struct AudioEffectBufferVdi *)input;
122     struct AudioEffectBufferVdi *outputVdi = (struct AudioEffectBufferVdi *)output;
123     int32_t ret = manager->ctrlOps->EffectReverse(manager->ctrlOps, inputVdi, outputVdi);
124     if (ret != HDF_SUCCESS) {
125         HDF_LOGE("EffectReverse failed, ret=%{public}d", ret);
126         return ret;
127     }
128     output = (struct AudioEffectBuffer *)outputVdi;
129     return ret;
130 }
131