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 #ifndef LOG_TAG
16 #define LOG_TAG "AudioEffectHdiParam"
17 #endif
18 
19 #include "audio_effect.h"
20 #include "audio_effect_hdi_param.h"
21 #include "audio_errors.h"
22 #include "audio_effect_log.h"
23 #include "securec.h"
24 
25 namespace OHOS {
26 namespace AudioStandard {
AudioEffectHdiParam()27 AudioEffectHdiParam::AudioEffectHdiParam()
28 {
29     AUDIO_DEBUG_LOG("constructor.");
30     DeviceTypeToHdiControlMap_.clear();
31     int32_t ret = memset_s(static_cast<void *>(input_), sizeof(input_), 0, sizeof(input_));
32     if (ret != SUCCESS) {
33         AUDIO_ERR_LOG("hdi constructor memset input failed");
34     }
35     ret = memset_s(static_cast<void *>(output_), sizeof(output_), 0, sizeof(output_));
36     if (ret != SUCCESS) {
37         AUDIO_ERR_LOG("hdi constructor memset output failed");
38     }
39     replyLen_ = GET_HDI_BUFFER_LEN;
40     hdiModel_ = nullptr;
41 }
42 
~AudioEffectHdiParam()43 AudioEffectHdiParam::~AudioEffectHdiParam()
44 {
45     AUDIO_DEBUG_LOG("destructor!");
46 }
47 
CreateHdiControl()48 void AudioEffectHdiParam::CreateHdiControl()
49 {
50     for (const auto &item : HDI_EFFECT_LIB_MAP) {
51         libName_ = item.second[0];
52         effectId_ = item.second[1];
53         EffectInfo info = {
54             .libName = &libName_[0],
55             .effectId = &effectId_[0],
56             .ioDirection = 1,
57         };
58         ControllerId controllerId;
59         IEffectControl *hdiControl = nullptr;
60         int32_t ret = hdiModel_->CreateEffectController(hdiModel_, &info, &hdiControl, &controllerId);
61         if ((ret != SUCCESS) || (hdiControl == nullptr)) {
62             AUDIO_WARNING_LOG("hdi init failed");
63         } else {
64             DeviceTypeToHdiControlMap_.emplace(item.first, hdiControl);
65         }
66     }
67     return;
68 }
69 
InitHdi()70 void AudioEffectHdiParam::InitHdi()
71 {
72     hdiModel_ = IEffectModelGet(false);
73     if (hdiModel_ == nullptr) {
74         AUDIO_ERR_LOG("IEffectModelGet failed");
75         return;
76     }
77 
78     CreateHdiControl();
79 }
80 
SetHdiCommand(IEffectControl * hdiControl,int8_t * effectHdiInput)81 int32_t AudioEffectHdiParam::SetHdiCommand(IEffectControl *hdiControl, int8_t *effectHdiInput)
82 {
83     int32_t ret = memcpy_s(static_cast<void *>(input_), sizeof(input_),
84         static_cast<void *>(effectHdiInput), sizeof(input_));
85     if (ret != SUCCESS) {
86         AUDIO_WARNING_LOG("hdi memcpy failed");
87     }
88     uint32_t replyLen = GET_HDI_BUFFER_LEN;
89     ret = hdiControl->SendCommand(hdiControl, HDI_SET_PATAM, input_, SEND_HDI_COMMAND_LEN,
90         output_, &replyLen);
91     return ret;
92 }
93 
UpdateHdiState(int8_t * effectHdiInput)94 int32_t AudioEffectHdiParam::UpdateHdiState(int8_t *effectHdiInput)
95 {
96     if (hdiModel_ == nullptr) {
97         return ERROR;
98     }
99     int32_t ret = ERROR;
100     for (const auto &item : DeviceTypeToHdiControlMap_) {
101         IEffectControl *hdiControl = item.second;
102         if (hdiControl == nullptr) {
103             AUDIO_WARNING_LOG("hdiControl is nullptr.");
104             continue;
105         }
106         ret = SetHdiCommand(hdiControl, effectHdiInput);
107         CHECK_AND_CONTINUE_LOG(ret == 0, "hdi send command failed");
108     }
109     return ret;
110 }
111 
UpdateHdiState(int8_t * effectHdiInput,DeviceType deviceType)112 int32_t AudioEffectHdiParam::UpdateHdiState(int8_t *effectHdiInput, DeviceType deviceType)
113 {
114     if (hdiModel_ == nullptr) {
115         return ERROR;
116     }
117     IEffectControl *hdiControl = DeviceTypeToHdiControlMap_[deviceType];
118     if (hdiControl == nullptr) {
119         AUDIO_WARNING_LOG("hdiControl is nullptr.");
120         return ERROR;
121     }
122     int32_t ret = SetHdiCommand(hdiControl, effectHdiInput);
123     CHECK_AND_RETURN_RET_LOG(ret == 0, ret, "hdi send command failed");
124     return ret;
125 }
126 }  // namespace AudioStandard
127 }  // namespace OHOS