• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "osal_time.h"
25 #include "effect_core.h"
26 #include "audio_dfx.h"
27 #include <inttypes.h>
28 
29 #define HDF_LOG_TAG HDF_AUDIO_EFFECT
30 
EffectControlEffectProcess(struct IEffectControl * self,const struct AudioEffectBuffer * input,struct AudioEffectBuffer * output)31 int32_t EffectControlEffectProcess(struct IEffectControl *self, const struct AudioEffectBuffer *input,
32     struct AudioEffectBuffer *output)
33 {
34     CHECK_TRUE_RETURN_RET_LOG(self == NULL || input == NULL || output == NULL, HDF_ERR_INVALID_PARAM,
35         "%{public}s: invailid input params", __func__);
36 
37     struct ControllerManager *manager = (struct ControllerManager *)self;
38     CHECK_TRUE_RETURN_RET_LOG(manager->ctrlOps == NULL || manager->ctrlOps->EffectProcess == NULL,
39         HDF_FAILURE, "%{public}s: controller has no options", __func__);
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         CHECK_TRUE_RETURN_RET_LOG(output->rawData == NULL, HDF_FAILURE,
46             "%{public}s: OsalMemCalloc fail", __func__);
47     }
48     struct AudioEffectBufferVdi *inputVdi = (struct AudioEffectBufferVdi *)input;
49     struct AudioEffectBufferVdi *outputVdi = (struct AudioEffectBufferVdi *)output;
50 
51     OsalTimespec start;
52     OsalTimespec end;
53     OsalTimespec diff;
54     int res = OsalGetTime(&start);
55     CHECK_TRUE_RETURN_RET_LOG(HDF_SUCCESS != res, HDF_FAILURE, "OsalGetTime start failed.");
56 
57     HdfAudioStartTrace("Hdi:Audio:EffectProcess", 0);
58     int32_t ret = manager->ctrlOps->EffectProcess(manager->ctrlOps, inputVdi, outputVdi);
59     HdfAudioFinishTrace();
60 
61     res = OsalGetTime(&end);
62     CHECK_TRUE_RETURN_RET_LOG(HDF_SUCCESS != res, HDF_FAILURE, "OsalGetTime end failed.");
63 
64     res = OsalDiffTime(&start, &end, &diff);
65     CHECK_TRUE_RETURN_RET_LOG(HDF_SUCCESS != res, HDF_FAILURE, "OsalDiffTime failed.");
66 
67     HDF_LOGI("EffectProcess cost time %{public}" PRIu64 " us",
68         (HDF_KILO_UNIT * HDF_KILO_UNIT) * diff.sec + diff.usec);
69 
70     CHECK_TRUE_RETURN_RET_LOG(ret != HDF_SUCCESS, ret,
71         "AudioEffectProcess failed, ret=%{public}d", ret);
72 
73     output = (struct AudioEffectBuffer *)outputVdi;
74     return ret;
75 }
76 
EffectControlSendCommand(struct IEffectControl * self,enum EffectCommandTableIndex cmdId,const int8_t * cmdData,uint32_t cmdDataLen,int8_t * replyData,uint32_t * replyDataLen)77 int32_t EffectControlSendCommand(struct IEffectControl *self, enum EffectCommandTableIndex cmdId, const int8_t *cmdData,
78     uint32_t cmdDataLen, int8_t *replyData, uint32_t *replyDataLen)
79 {
80     if (self == NULL || cmdData == NULL || replyData == NULL || replyDataLen == NULL) {
81         HDF_LOGE("%{public}s: invailid input params", __func__);
82         return HDF_ERR_INVALID_PARAM;
83     }
84 
85     struct ControllerManager *manager = (struct ControllerManager *)self;
86     if (manager->ctrlOps == NULL || manager->ctrlOps->SendCommand == NULL) {
87         HDF_LOGE("%{public}s: controller has no options", __func__);
88         return HDF_FAILURE;
89     }
90     enum EffectCommandTableIndexVdi cmdIdVdi = (enum EffectCommandTableIndexVdi)cmdId;
91     int32_t ret = manager->ctrlOps->SendCommand(manager->ctrlOps, cmdIdVdi, (void *)cmdData, cmdDataLen,
92                                          (void *)replyData, replyDataLen);
93     if (ret != HDF_SUCCESS) {
94         HDF_LOGE("SendCommand failed, ret=%{public}d", ret);
95         return ret;
96     }
97     return ret;
98 }
99 
EffectGetOwnDescriptor(struct IEffectControl * self,struct EffectControllerDescriptor * desc)100 int32_t EffectGetOwnDescriptor(struct IEffectControl *self, struct EffectControllerDescriptor *desc)
101 {
102     CHECK_TRUE_RETURN_RET_LOG(self == NULL || desc == NULL, HDF_ERR_INVALID_PARAM,
103         "%{public}s: invailid input params", __func__);
104 
105     struct ControllerManager *manager = (struct ControllerManager *)self;
106     CHECK_TRUE_RETURN_RET_LOG(manager->ctrlOps == NULL || manager->ctrlOps->GetEffectDescriptor == NULL,
107         HDF_FAILURE, "%{public}s: controller has no options", __func__);
108 
109     struct EffectControllerDescriptorVdi *descVdi = (struct EffectControllerDescriptorVdi *)desc;
110     CHECK_TRUE_RETURN_RET_LOG(ConstructDescriptor(descVdi) != HDF_SUCCESS, HDF_FAILURE,
111         "%{public}s: ConstructDescriptor fail!", __func__);
112 
113     OsalTimespec start;
114     OsalTimespec end;
115     OsalTimespec diff;
116     int res = OsalGetTime(&start);
117     CHECK_TRUE_RETURN_RET_LOG(HDF_SUCCESS != res, HDF_FAILURE, "OsalGetTime start failed.");
118 
119     HdfAudioStartTrace("Hdi:Audio:GetEffectDescriptor", 0);
120     int32_t ret = manager->ctrlOps->GetEffectDescriptor(manager->ctrlOps, descVdi);
121     HdfAudioFinishTrace();
122 
123     res = OsalGetTime(&end);
124     CHECK_TRUE_RETURN_RET_LOG(HDF_SUCCESS != res, HDF_FAILURE, "OsalGetTime end failed.");
125 
126     res = OsalDiffTime(&start, &end, &diff);
127     CHECK_TRUE_RETURN_RET_LOG(HDF_SUCCESS != res, HDF_FAILURE, "OsalDiffTime failed.");
128 
129     HDF_LOGI("GetEffectDescriptor cost time %{public}" PRIu64 " us",
130         (HDF_KILO_UNIT * HDF_KILO_UNIT) * diff.sec + diff.usec);
131 
132     CHECK_TRUE_RETURN_RET_LOG(ret != HDF_SUCCESS, ret,
133         "EffectGetOwnDescriptor failed, ret=%{public}d", ret);
134 
135     desc = (struct EffectControllerDescriptor *)descVdi;
136     return ret;
137 }
138 
EffectControlEffectReverse(struct IEffectControl * self,const struct AudioEffectBuffer * input,struct AudioEffectBuffer * output)139 int32_t EffectControlEffectReverse(struct IEffectControl *self, const struct AudioEffectBuffer *input,
140     struct AudioEffectBuffer *output)
141 {
142     if (self == NULL || input == NULL || output == NULL) {
143         HDF_LOGE("%{public}s: invailid input params", __func__);
144         return HDF_ERR_INVALID_PARAM;
145     }
146 
147     struct ControllerManager *manager = (struct ControllerManager *)self;
148     if (manager->ctrlOps == NULL || manager->ctrlOps->EffectReverse == NULL) {
149         HDF_LOGE("%{public}s: controller has no options", __func__);
150         return HDF_FAILURE;
151     }
152     struct AudioEffectBufferVdi *inputVdi = (struct AudioEffectBufferVdi *)input;
153     struct AudioEffectBufferVdi *outputVdi = (struct AudioEffectBufferVdi *)output;
154     int32_t ret = manager->ctrlOps->EffectReverse(manager->ctrlOps, inputVdi, outputVdi);
155     if (ret != HDF_SUCCESS) {
156         HDF_LOGE("EffectReverse failed, ret=%{public}d", ret);
157         return ret;
158     }
159     output = (struct AudioEffectBuffer *)outputVdi;
160     return ret;
161 }
162