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