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
GetCallback(uint32_t type)44 std::shared_ptr<IAudioSinkCallback> SinkCallbackWrapper::GetCallback(uint32_t type)
45 {
46 CHECK_AND_RETURN_RET_LOG(type < HDI_CB_TYPE_NUM, nullptr, "invalid type %{public}u", type);
47 CHECK_AND_RETURN_RET(cbs_.count(type), nullptr);
48 std::lock_guard<std::mutex> lock(cbMtx_);
49 return cbs_[type];
50 }
51
GetRawCallback(uint32_t type)52 IAudioSinkCallback *SinkCallbackWrapper::GetRawCallback(uint32_t type)
53 {
54 CHECK_AND_RETURN_RET_LOG(type < HDI_CB_TYPE_NUM, nullptr, "invalid type %{public}u", type);
55 CHECK_AND_RETURN_RET(rawCbs_.count(type), nullptr);
56 std::lock_guard<std::mutex> lock(rawCbMtx_);
57 return rawCbs_[type];
58 }
59
OnRenderSinkParamChange(const std::string & networkId,const AudioParamKey key,const std::string & condition,const std::string & value)60 void SinkCallbackWrapper::OnRenderSinkParamChange(const std::string &networkId, const AudioParamKey key,
61 const std::string &condition, const std::string &value)
62 {
63 for (auto &cb : cbs_) {
64 if (cb.second == nullptr) {
65 continue;
66 }
67 cb.second->OnRenderSinkParamChange(networkId, key, condition, value);
68 }
69 for (auto &cb : rawCbs_) {
70 if (cb.second == nullptr) {
71 continue;
72 }
73 cb.second->OnRenderSinkParamChange(networkId, key, condition, value);
74 }
75 }
76
OnRenderSinkStateChange(uint32_t uniqueId,bool started)77 void SinkCallbackWrapper::OnRenderSinkStateChange(uint32_t uniqueId, bool started)
78 {
79 for (auto &cb : cbs_) {
80 if (cb.second == nullptr) {
81 continue;
82 }
83 cb.second->OnRenderSinkStateChange(uniqueId, started);
84 }
85 for (auto &cb : rawCbs_) {
86 if (cb.second == nullptr) {
87 continue;
88 }
89 cb.second->OnRenderSinkStateChange(uniqueId, started);
90 }
91 }
92
RegistCallback(uint32_t type,std::shared_ptr<IAudioSourceCallback> cb)93 void SourceCallbackWrapper::RegistCallback(uint32_t type, std::shared_ptr<IAudioSourceCallback> cb)
94 {
95 CHECK_AND_RETURN_LOG(type < HDI_CB_TYPE_NUM, "invalid type %{public}u", type);
96 CHECK_AND_RETURN_LOG(cb != nullptr, "callback is nullptr");
97 std::lock_guard<std::mutex> lock(cbMtx_);
98 cbs_[type] = cb;
99 }
100
RegistCallback(uint32_t type,IAudioSourceCallback * cb)101 void SourceCallbackWrapper::RegistCallback(uint32_t type, IAudioSourceCallback *cb)
102 {
103 CHECK_AND_RETURN_LOG(type < HDI_CB_TYPE_NUM, "invalid type %{public}u", type);
104 CHECK_AND_RETURN_LOG(cb != nullptr, "callback is nullptr");
105 std::lock_guard<std::mutex> lock(rawCbMtx_);
106 rawCbs_[type] = cb;
107 }
108
GetCallback(uint32_t type)109 std::shared_ptr<IAudioSourceCallback> SourceCallbackWrapper::GetCallback(uint32_t type)
110 {
111 CHECK_AND_RETURN_RET_LOG(type < HDI_CB_TYPE_NUM, nullptr, "invalid type %{public}u", type);
112 CHECK_AND_RETURN_RET(cbs_.count(type), nullptr);
113 std::lock_guard<std::mutex> lock(cbMtx_);
114 return cbs_[type];
115 }
116
GetRawCallback(uint32_t type)117 IAudioSourceCallback *SourceCallbackWrapper::GetRawCallback(uint32_t type)
118 {
119 CHECK_AND_RETURN_RET_LOG(type < HDI_CB_TYPE_NUM, nullptr, "invalid type %{public}u", type);
120 CHECK_AND_RETURN_RET(rawCbs_.count(type), nullptr);
121 std::lock_guard<std::mutex> lock(rawCbMtx_);
122 return rawCbs_[type];
123 }
124
OnCaptureSourceParamChange(const std::string & networkId,const AudioParamKey key,const std::string & condition,const std::string & value)125 void SourceCallbackWrapper::OnCaptureSourceParamChange(const std::string &networkId, const AudioParamKey key,
126 const std::string &condition, const std::string &value)
127 {
128 for (auto &cb : cbs_) {
129 if (cb.second == nullptr) {
130 continue;
131 }
132 cb.second->OnCaptureSourceParamChange(networkId, key, condition, value);
133 }
134 for (auto &cb : rawCbs_) {
135 if (cb.second == nullptr) {
136 continue;
137 }
138 cb.second->OnCaptureSourceParamChange(networkId, key, condition, value);
139 }
140 }
141
OnCaptureState(bool isActive)142 void SourceCallbackWrapper::OnCaptureState(bool isActive)
143 {
144 for (auto &cb : cbs_) {
145 if (cb.second == nullptr) {
146 continue;
147 }
148 cb.second->OnCaptureState(isActive);
149 }
150 for (auto &cb : rawCbs_) {
151 if (cb.second == nullptr) {
152 continue;
153 }
154 cb.second->OnCaptureState(isActive);
155 }
156 }
157
OnWakeupClose(void)158 void SourceCallbackWrapper::OnWakeupClose(void)
159 {
160 for (auto &cb : cbs_) {
161 if (cb.second == nullptr) {
162 continue;
163 }
164 cb.second->OnWakeupClose();
165 }
166 for (auto &cb : rawCbs_) {
167 if (cb.second == nullptr) {
168 continue;
169 }
170 cb.second->OnWakeupClose();
171 }
172 }
173
174 } // namespace AudioStandard
175 } // namespace OHOS
176