1 /*
2 * Copyright (c) 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
16 #ifndef LOG_TAG
17 #define LOG_TAG "CallbackWrapper"
18 #endif
19
20 #include "util/callback_wrapper.h"
21 #include "audio_hdi_log.h"
22 #include "audio_errors.h"
23
24 namespace OHOS {
25 namespace AudioStandard {
RegistCallback(uint32_t type,std::shared_ptr<IAudioSinkCallback> cb)26 void SinkCallbackWrapper::RegistCallback(uint32_t type, std::shared_ptr<IAudioSinkCallback> cb)
27 {
28 CHECK_AND_RETURN_LOG(type < HDI_CB_TYPE_NUM, "invalid type %{public}u", type);
29 CHECK_AND_RETURN_LOG(cb != nullptr, "callback is nullptr");
30
31 std::lock_guard<std::mutex> lock(cbMtx_);
32 cbs_[type] = cb;
33 }
34
RegistCallback(uint32_t type,IAudioSinkCallback * cb)35 void SinkCallbackWrapper::RegistCallback(uint32_t type, IAudioSinkCallback *cb)
36 {
37 CHECK_AND_RETURN_LOG(type < HDI_CB_TYPE_NUM, "invalid type %{public}u", type);
38 CHECK_AND_RETURN_LOG(cb != nullptr, "callback is nullptr");
39
40 std::lock_guard<std::mutex> lock(rawCbMtx_);
41 rawCbs_[type] = cb;
42 }
43
RegistCallbackGenerator(uint32_t type,std::function<std::shared_ptr<IAudioSinkCallback> (uint32_t)> cbGenerator)44 void SinkCallbackWrapper::RegistCallbackGenerator(uint32_t type,
45 std::function<std::shared_ptr<IAudioSinkCallback>(uint32_t)> cbGenerator)
46 {
47 CHECK_AND_RETURN_LOG(type < HDI_CB_TYPE_NUM, "invalid type %{public}u", type);
48 CHECK_AND_RETURN_LOG(cbGenerator, "callback generator is nullptr");
49 std::lock_guard<std::mutex> lock(cbGeneratorMtx_);
50 cbGenerators_[type] = cbGenerator;
51 }
52
GetCallback(uint32_t type,uint32_t renderId)53 std::shared_ptr<IAudioSinkCallback> SinkCallbackWrapper::GetCallback(uint32_t type, uint32_t renderId)
54 {
55 CHECK_AND_RETURN_RET_LOG(type < HDI_CB_TYPE_NUM, nullptr, "invalid type %{public}u", type);
56 std::lock_guard<std::mutex> cbLock(cbMtx_);
57 if (cbs_.count(type)) {
58 return cbs_[type];
59 }
60 std::lock_guard<std::mutex> cbGeneratorLock(cbGeneratorMtx_);
61 if (cbGenerators_.count(type) && cbGenerators_[type]) {
62 return cbGenerators_[type](renderId);
63 }
64 return nullptr;
65 }
66
GetRawCallback(uint32_t type)67 IAudioSinkCallback *SinkCallbackWrapper::GetRawCallback(uint32_t type)
68 {
69 CHECK_AND_RETURN_RET_LOG(type < HDI_CB_TYPE_NUM, nullptr, "invalid type %{public}u", type);
70 CHECK_AND_RETURN_RET(rawCbs_.count(type), nullptr);
71 std::lock_guard<std::mutex> lock(rawCbMtx_);
72 return rawCbs_[type];
73 }
74
OnRenderSinkParamChange(const std::string & networkId,const AudioParamKey key,const std::string & condition,const std::string & value)75 void SinkCallbackWrapper::OnRenderSinkParamChange(const std::string &networkId, const AudioParamKey key,
76 const std::string &condition, const std::string &value)
77 {
78 for (auto &cb : cbs_) {
79 if (cb.second == nullptr) {
80 continue;
81 }
82 cb.second->OnRenderSinkParamChange(networkId, key, condition, value);
83 }
84 for (auto &cb : rawCbs_) {
85 if (cb.second == nullptr) {
86 continue;
87 }
88 cb.second->OnRenderSinkParamChange(networkId, key, condition, value);
89 }
90 }
91
OnRenderSinkStateChange(uint32_t uniqueId,bool started)92 void SinkCallbackWrapper::OnRenderSinkStateChange(uint32_t uniqueId, bool started)
93 {
94 for (auto &cb : cbs_) {
95 if (cb.second == nullptr) {
96 continue;
97 }
98 cb.second->OnRenderSinkStateChange(uniqueId, started);
99 }
100 for (auto &cb : rawCbs_) {
101 if (cb.second == nullptr) {
102 continue;
103 }
104 cb.second->OnRenderSinkStateChange(uniqueId, started);
105 }
106 }
107
RegistCallback(uint32_t type,std::shared_ptr<IAudioSourceCallback> cb)108 void SourceCallbackWrapper::RegistCallback(uint32_t type, std::shared_ptr<IAudioSourceCallback> cb)
109 {
110 CHECK_AND_RETURN_LOG(type < HDI_CB_TYPE_NUM, "invalid type %{public}u", type);
111 CHECK_AND_RETURN_LOG(cb != nullptr, "callback is nullptr");
112 std::lock_guard<std::mutex> lock(cbMtx_);
113 cbs_[type] = cb;
114 }
115
RegistCallback(uint32_t type,IAudioSourceCallback * cb)116 void SourceCallbackWrapper::RegistCallback(uint32_t type, IAudioSourceCallback *cb)
117 {
118 CHECK_AND_RETURN_LOG(type < HDI_CB_TYPE_NUM, "invalid type %{public}u", type);
119 CHECK_AND_RETURN_LOG(cb != nullptr, "callback is nullptr");
120 std::lock_guard<std::mutex> lock(rawCbMtx_);
121 rawCbs_[type] = cb;
122 }
123
RegistCallbackGenerator(uint32_t type,std::function<std::shared_ptr<IAudioSourceCallback> (uint32_t)> cbGenerator)124 void SourceCallbackWrapper::RegistCallbackGenerator(uint32_t type,
125 std::function<std::shared_ptr<IAudioSourceCallback>(uint32_t)> cbGenerator)
126 {
127 CHECK_AND_RETURN_LOG(type < HDI_CB_TYPE_NUM, "invalid type %{public}u", type);
128 CHECK_AND_RETURN_LOG(cbGenerator, "callback generator is nullptr");
129 std::lock_guard<std::mutex> lock(cbGeneratorMtx_);
130 cbGenerators_[type] = cbGenerator;
131 }
132
GetCallback(uint32_t type,uint32_t captureId)133 std::shared_ptr<IAudioSourceCallback> SourceCallbackWrapper::GetCallback(uint32_t type, uint32_t captureId)
134 {
135 CHECK_AND_RETURN_RET_LOG(type < HDI_CB_TYPE_NUM, nullptr, "invalid type %{public}u", type);
136 std::lock_guard<std::mutex> cbLock(cbMtx_);
137 if (cbs_.count(type)) {
138 return cbs_[type];
139 }
140 std::lock_guard<std::mutex> cbGeneratorLock(cbGeneratorMtx_);
141 if (cbGenerators_.count(type) && cbGenerators_[type]) {
142 return cbGenerators_[type](captureId);
143 }
144 return nullptr;
145 }
146
GetRawCallback(uint32_t type)147 IAudioSourceCallback *SourceCallbackWrapper::GetRawCallback(uint32_t type)
148 {
149 CHECK_AND_RETURN_RET_LOG(type < HDI_CB_TYPE_NUM, nullptr, "invalid type %{public}u", type);
150 CHECK_AND_RETURN_RET(rawCbs_.count(type), nullptr);
151 std::lock_guard<std::mutex> lock(rawCbMtx_);
152 return rawCbs_[type];
153 }
154
OnCaptureSourceParamChange(const std::string & networkId,const AudioParamKey key,const std::string & condition,const std::string & value)155 void SourceCallbackWrapper::OnCaptureSourceParamChange(const std::string &networkId, const AudioParamKey key,
156 const std::string &condition, const std::string &value)
157 {
158 for (auto &cb : cbs_) {
159 if (cb.second == nullptr) {
160 continue;
161 }
162 cb.second->OnCaptureSourceParamChange(networkId, key, condition, value);
163 }
164 for (auto &cb : rawCbs_) {
165 if (cb.second == nullptr) {
166 continue;
167 }
168 cb.second->OnCaptureSourceParamChange(networkId, key, condition, value);
169 }
170 }
171
OnCaptureState(bool isActive)172 void SourceCallbackWrapper::OnCaptureState(bool isActive)
173 {
174 for (auto &cb : cbs_) {
175 if (cb.second == nullptr) {
176 continue;
177 }
178 cb.second->OnCaptureState(isActive);
179 }
180 for (auto &cb : rawCbs_) {
181 if (cb.second == nullptr) {
182 continue;
183 }
184 cb.second->OnCaptureState(isActive);
185 }
186 }
187
OnWakeupClose(void)188 void SourceCallbackWrapper::OnWakeupClose(void)
189 {
190 for (auto &cb : cbs_) {
191 if (cb.second == nullptr) {
192 continue;
193 }
194 cb.second->OnWakeupClose();
195 }
196 for (auto &cb : rawCbs_) {
197 if (cb.second == nullptr) {
198 continue;
199 }
200 cb.second->OnWakeupClose();
201 }
202 }
203
204 } // namespace AudioStandard
205 } // namespace OHOS
206