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