1 /*
2 * Copyright (c) 2023-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 "AudioPolicyClientStubImpl"
17 #endif
18
19 #include "audio_policy_client_stub_impl.h"
20 #include "audio_errors.h"
21 #include "audio_policy_log.h"
22 #include "audio_utils.h"
23
24 namespace OHOS {
25 namespace AudioStandard {
26 constexpr int32_t RSS_UID = 1096;
AddVolumeKeyEventCallback(const std::shared_ptr<VolumeKeyEventCallback> & cb)27 int32_t AudioPolicyClientStubImpl::AddVolumeKeyEventCallback(const std::shared_ptr<VolumeKeyEventCallback> &cb)
28 {
29 std::lock_guard<std::mutex> lockCbMap(volumeKeyEventMutex_);
30 volumeKeyEventCallbackList_.push_back(cb);
31 return SUCCESS;
32 }
33
RemoveVolumeKeyEventCallback(const std::shared_ptr<VolumeKeyEventCallback> & cb)34 int32_t AudioPolicyClientStubImpl::RemoveVolumeKeyEventCallback(const std::shared_ptr<VolumeKeyEventCallback> &cb)
35 {
36 std::lock_guard<std::mutex> lockCbMap(volumeKeyEventMutex_);
37 if (cb == nullptr) {
38 volumeKeyEventCallbackList_.clear();
39 return SUCCESS;
40 }
41 auto it = find_if(volumeKeyEventCallbackList_.begin(), volumeKeyEventCallbackList_.end(),
42 [&cb](const std::weak_ptr<VolumeKeyEventCallback>& elem) {
43 return elem.lock() == cb;
44 });
45 if (it != volumeKeyEventCallbackList_.end()) {
46 volumeKeyEventCallbackList_.erase(it);
47 }
48 return SUCCESS;
49 }
50
GetVolumeKeyEventCallbackSize() const51 size_t AudioPolicyClientStubImpl::GetVolumeKeyEventCallbackSize() const
52 {
53 std::lock_guard<std::mutex> lockCbMap(volumeKeyEventMutex_);
54 return volumeKeyEventCallbackList_.size();
55 }
56
OnVolumeKeyEvent(VolumeEvent volumeEvent)57 void AudioPolicyClientStubImpl::OnVolumeKeyEvent(VolumeEvent volumeEvent)
58 {
59 std::lock_guard<std::mutex> lockCbMap(volumeKeyEventMutex_);
60 for (auto it = volumeKeyEventCallbackList_.begin(); it != volumeKeyEventCallbackList_.end(); ++it) {
61 std::shared_ptr<VolumeKeyEventCallback> volumeKeyEventCallback = (*it).lock();
62 if (volumeKeyEventCallback != nullptr) {
63 volumeKeyEventCallback->OnVolumeKeyEvent(volumeEvent);
64 }
65 }
66 }
67
AddFocusInfoChangeCallback(const std::shared_ptr<AudioFocusInfoChangeCallback> & cb)68 int32_t AudioPolicyClientStubImpl::AddFocusInfoChangeCallback(const std::shared_ptr<AudioFocusInfoChangeCallback> &cb)
69 {
70 std::lock_guard<std::mutex> lockCbMap(focusInfoChangeMutex_);
71 focusInfoChangeCallbackList_.push_back(cb);
72 return SUCCESS;
73 }
74
RemoveFocusInfoChangeCallback()75 int32_t AudioPolicyClientStubImpl::RemoveFocusInfoChangeCallback()
76 {
77 std::lock_guard<std::mutex> lockCbMap(focusInfoChangeMutex_);
78 focusInfoChangeCallbackList_.clear();
79 return SUCCESS;
80 }
81
OnAudioFocusInfoChange(const std::list<std::pair<AudioInterrupt,AudioFocuState>> & focusInfoList)82 void AudioPolicyClientStubImpl::OnAudioFocusInfoChange(
83 const std::list<std::pair<AudioInterrupt, AudioFocuState>> &focusInfoList)
84 {
85 std::lock_guard<std::mutex> lockCbMap(focusInfoChangeMutex_);
86 for (auto it = focusInfoChangeCallbackList_.begin(); it != focusInfoChangeCallbackList_.end(); ++it) {
87 (*it)->OnAudioFocusInfoChange(focusInfoList);
88 }
89 }
90
OnAudioFocusRequested(const AudioInterrupt & requestFocus)91 void AudioPolicyClientStubImpl::OnAudioFocusRequested(const AudioInterrupt &requestFocus)
92 {
93 std::lock_guard<std::mutex> lockCbMap(focusInfoChangeMutex_);
94 for (auto it = focusInfoChangeCallbackList_.begin(); it != focusInfoChangeCallbackList_.end(); ++it) {
95 (*it)->OnAudioFocusRequested(requestFocus);
96 }
97 }
98
OnAudioFocusAbandoned(const AudioInterrupt & abandonFocus)99 void AudioPolicyClientStubImpl::OnAudioFocusAbandoned(const AudioInterrupt &abandonFocus)
100 {
101 std::lock_guard<std::mutex> lockCbMap(focusInfoChangeMutex_);
102 for (auto it = focusInfoChangeCallbackList_.begin(); it != focusInfoChangeCallbackList_.end(); ++it) {
103 (*it)->OnAudioFocusAbandoned(abandonFocus);
104 }
105 }
106
GetFocusInfoChangeCallbackSize() const107 size_t AudioPolicyClientStubImpl::GetFocusInfoChangeCallbackSize() const
108 {
109 std::lock_guard<std::mutex> lockCbMap(focusInfoChangeMutex_);
110 return focusInfoChangeCallbackList_.size();
111 }
112
DeviceFilterByFlag(DeviceFlag flag,const std::vector<std::shared_ptr<AudioDeviceDescriptor>> & desc)113 std::vector<std::shared_ptr<AudioDeviceDescriptor>> AudioPolicyClientStubImpl::DeviceFilterByFlag(DeviceFlag flag,
114 const std::vector<std::shared_ptr<AudioDeviceDescriptor>>& desc)
115 {
116 std::vector<std::shared_ptr<AudioDeviceDescriptor>> descRet;
117 DeviceRole role = DEVICE_ROLE_NONE;
118 switch (flag) {
119 case DeviceFlag::ALL_DEVICES_FLAG:
120 for (std::shared_ptr<AudioDeviceDescriptor> var : desc) {
121 if (var->networkId_ == LOCAL_NETWORK_ID) {
122 descRet.insert(descRet.end(), var);
123 }
124 }
125 break;
126 case DeviceFlag::ALL_DISTRIBUTED_DEVICES_FLAG:
127 for (std::shared_ptr<AudioDeviceDescriptor> var : desc) {
128 if (var->networkId_ != LOCAL_NETWORK_ID) {
129 descRet.insert(descRet.end(), var);
130 }
131 }
132 break;
133 case DeviceFlag::ALL_L_D_DEVICES_FLAG:
134 descRet = desc;
135 break;
136 case DeviceFlag::OUTPUT_DEVICES_FLAG:
137 case DeviceFlag::INPUT_DEVICES_FLAG:
138 role = flag == INPUT_DEVICES_FLAG ? INPUT_DEVICE : OUTPUT_DEVICE;
139 for (std::shared_ptr<AudioDeviceDescriptor> var : desc) {
140 if (var->networkId_ == LOCAL_NETWORK_ID && var->deviceRole_ == role) {
141 descRet.insert(descRet.end(), var);
142 }
143 }
144 break;
145 case DeviceFlag::DISTRIBUTED_OUTPUT_DEVICES_FLAG:
146 case DeviceFlag::DISTRIBUTED_INPUT_DEVICES_FLAG:
147 role = flag == DISTRIBUTED_INPUT_DEVICES_FLAG ? INPUT_DEVICE : OUTPUT_DEVICE;
148 for (std::shared_ptr<AudioDeviceDescriptor> var : desc) {
149 if (var->networkId_ != LOCAL_NETWORK_ID && var->deviceRole_ == role) {
150 descRet.insert(descRet.end(), var);
151 }
152 }
153 break;
154 default:
155 break;
156 }
157 return descRet;
158 }
159
AddDeviceChangeCallback(const DeviceFlag & flag,const std::shared_ptr<AudioManagerDeviceChangeCallback> & cb)160 int32_t AudioPolicyClientStubImpl::AddDeviceChangeCallback(const DeviceFlag &flag,
161 const std::shared_ptr<AudioManagerDeviceChangeCallback> &cb)
162 {
163 std::lock_guard<std::mutex> lockCbMap(deviceChangeMutex_);
164 deviceChangeCallbackList_.push_back(std::make_pair(flag, cb));
165 return SUCCESS;
166 }
167
RemoveDeviceChangeCallback(DeviceFlag flag,std::shared_ptr<AudioManagerDeviceChangeCallback> & cb)168 int32_t AudioPolicyClientStubImpl::RemoveDeviceChangeCallback(DeviceFlag flag,
169 std::shared_ptr<AudioManagerDeviceChangeCallback> &cb)
170 {
171 std::lock_guard<std::mutex> lockCbMap(deviceChangeMutex_);
172 auto iter = deviceChangeCallbackList_.begin();
173 while (iter != deviceChangeCallbackList_.end()) {
174 if ((iter->first & flag) && (iter->second == cb || cb == nullptr)) {
175 AUDIO_INFO_LOG("remove device change cb flag:%{public}d", flag);
176 iter = deviceChangeCallbackList_.erase(iter);
177 } else {
178 iter++;
179 }
180 }
181 return SUCCESS;
182 }
183
GetDeviceChangeCallbackSize() const184 size_t AudioPolicyClientStubImpl::GetDeviceChangeCallbackSize() const
185 {
186 std::lock_guard<std::mutex> lockCbMap(deviceChangeMutex_);
187 return deviceChangeCallbackList_.size();
188 }
189
OnDeviceChange(const DeviceChangeAction & dca)190 void AudioPolicyClientStubImpl::OnDeviceChange(const DeviceChangeAction &dca)
191 {
192 std::lock_guard<std::mutex> lockCbMap(deviceChangeMutex_);
193 DeviceChangeAction deviceChangeAction;
194 deviceChangeAction.type = dca.type;
195 for (auto it = deviceChangeCallbackList_.begin(); it != deviceChangeCallbackList_.end(); ++it) {
196 deviceChangeAction.flag = it->first;
197 deviceChangeAction.deviceDescriptors = DeviceFilterByFlag(it->first, dca.deviceDescriptors);
198 if (it->second && deviceChangeAction.deviceDescriptors.size() > 0) {
199 it->second->OnDeviceChange(deviceChangeAction);
200 }
201 }
202 }
203
OnDistribuitedOutputChange(const AudioDeviceDescriptor & deviceDesc,bool isRemote)204 void AudioPolicyClientStubImpl::OnDistribuitedOutputChange(const AudioDeviceDescriptor &deviceDesc, bool isRemote)
205 {
206 for (auto &item : distribuitedOutputChangeCallback_) {
207 item->OnDistribuitedOutputChange(deviceDesc, isRemote);
208 }
209 }
210
OnMicrophoneBlocked(const MicrophoneBlockedInfo & blockedInfo)211 void AudioPolicyClientStubImpl::OnMicrophoneBlocked(const MicrophoneBlockedInfo &blockedInfo)
212 {
213 std::lock_guard<std::mutex> lockCbMap(microphoneBlockedMutex_);
214 MicrophoneBlockedInfo microphoneBlockedInfo;
215 microphoneBlockedInfo.blockStatus = blockedInfo.blockStatus;
216 for (auto it = microphoneBlockedCallbackList_.begin(); it != microphoneBlockedCallbackList_.end(); ++it) {
217 microphoneBlockedInfo.devices= blockedInfo.devices;
218 if (it->second && microphoneBlockedInfo.devices.size() > 0) {
219 it->second->OnMicrophoneBlocked(microphoneBlockedInfo);
220 }
221 }
222 }
223
AddMicrophoneBlockedCallback(const int32_t clientId,const std::shared_ptr<AudioManagerMicrophoneBlockedCallback> & cb)224 int32_t AudioPolicyClientStubImpl::AddMicrophoneBlockedCallback(const int32_t clientId,
225 const std::shared_ptr<AudioManagerMicrophoneBlockedCallback> &cb)
226 {
227 std::lock_guard<std::mutex> lockCbMap(microphoneBlockedMutex_);
228 microphoneBlockedCallbackList_.push_back(std::make_pair(clientId, cb));
229 AUDIO_INFO_LOG("add mic blocked cb clientId:%{public}d", clientId);
230 return SUCCESS;
231 }
232
RemoveMicrophoneBlockedCallback(const int32_t clientId,const std::shared_ptr<AudioManagerMicrophoneBlockedCallback> & cb)233 int32_t AudioPolicyClientStubImpl::RemoveMicrophoneBlockedCallback(const int32_t clientId,
234 const std::shared_ptr<AudioManagerMicrophoneBlockedCallback> &cb)
235 {
236 std::lock_guard<std::mutex> lockCbMap(microphoneBlockedMutex_);
237 auto iter = microphoneBlockedCallbackList_.begin();
238 while (iter != microphoneBlockedCallbackList_.end()) {
239 if ((iter->first == clientId) && (iter->second == cb || cb == nullptr)) {
240 AUDIO_INFO_LOG("remove mic blocked cb flag:%{public}d", clientId);
241 iter = microphoneBlockedCallbackList_.erase(iter);
242 } else {
243 iter++;
244 }
245 }
246 return SUCCESS;
247 }
248
GetMicrophoneBlockedCallbackSize() const249 size_t AudioPolicyClientStubImpl::GetMicrophoneBlockedCallbackSize() const
250 {
251 std::lock_guard<std::mutex> lockCbMap(microphoneBlockedMutex_);
252 return microphoneBlockedCallbackList_.size();
253 }
254
AddAudioSceneChangedCallback(const int32_t clientId,const std::shared_ptr<AudioManagerAudioSceneChangedCallback> & cb)255 int32_t AudioPolicyClientStubImpl::AddAudioSceneChangedCallback(const int32_t clientId,
256 const std::shared_ptr<AudioManagerAudioSceneChangedCallback> &cb)
257 {
258 std::lock_guard<std::mutex> lockCbMap(audioSceneChangedMutex_);
259 audioSceneChangedCallbackList_.push_back(cb);
260 AUDIO_INFO_LOG("add audio scene change clientId:%{public}d", clientId);
261 return SUCCESS;
262 }
263
RemoveAudioSceneChangedCallback(const std::shared_ptr<AudioManagerAudioSceneChangedCallback> & cb)264 int32_t AudioPolicyClientStubImpl::RemoveAudioSceneChangedCallback(
265 const std::shared_ptr<AudioManagerAudioSceneChangedCallback> &cb)
266 {
267 std::lock_guard<std::mutex> lockCbMap(audioSceneChangedMutex_);
268 auto iter = audioSceneChangedCallbackList_.begin();
269 while (iter != audioSceneChangedCallbackList_.end()) {
270 if (*iter == cb) {
271 iter = audioSceneChangedCallbackList_.erase(iter);
272 } else {
273 iter++;
274 }
275 }
276 return SUCCESS;
277 }
278
GetAudioSceneChangedCallbackSize() const279 size_t AudioPolicyClientStubImpl::GetAudioSceneChangedCallbackSize() const
280 {
281 std::lock_guard<std::mutex> lockCbMap(audioSceneChangedMutex_);
282 return audioSceneChangedCallbackList_.size();
283 }
284
OnAudioSceneChange(const AudioScene & audioScene)285 void AudioPolicyClientStubImpl::OnAudioSceneChange(const AudioScene &audioScene)
286 {
287 std::lock_guard<std::mutex> lockCbMap(audioSceneChangedMutex_);
288 for (const auto &callback : audioSceneChangedCallbackList_) {
289 callback->OnAudioSceneChange(audioScene);
290 }
291 }
292
AddSelfAppVolumeChangeCallback(int32_t appUid,const std::shared_ptr<AudioManagerAppVolumeChangeCallback> & cb)293 int32_t AudioPolicyClientStubImpl::AddSelfAppVolumeChangeCallback(int32_t appUid,
294 const std::shared_ptr<AudioManagerAppVolumeChangeCallback> &cb)
295 {
296 std::lock_guard<std::mutex> lockCbMap(selfAppVolumeChangeMutex_);
297 for (auto iter : selfAppVolumeChangeCallback_) {
298 if (iter.first == appUid && iter.second.get() == cb.get()) {
299 selfAppVolumeChangeCallbackNum_[appUid]++;
300 AUDIO_INFO_LOG("selfAppVolumeChangeCallback_ No need pushback");
301 return SUCCESS;
302 }
303 }
304 selfAppVolumeChangeCallbackNum_[appUid]++;
305 selfAppVolumeChangeCallback_.push_back({appUid, cb});
306 AUDIO_INFO_LOG("Add selfAppVolumeChangeCallback appUid : %{public}d", appUid);
307 return SUCCESS;
308 }
309
RemoveAllSelfAppVolumeChangeCallback(int32_t appUid)310 int32_t AudioPolicyClientStubImpl::RemoveAllSelfAppVolumeChangeCallback(int32_t appUid)
311 {
312 std::lock_guard<std::mutex> lockCbMap(selfAppVolumeChangeMutex_);
313 if (selfAppVolumeChangeCallbackNum_[appUid] != 0) {
314 selfAppVolumeChangeCallbackNum_[appUid] = 0;
315 auto iter = selfAppVolumeChangeCallback_.begin();
316 while (iter != selfAppVolumeChangeCallback_.end()) {
317 if (iter->first == appUid) {
318 iter = selfAppVolumeChangeCallback_.erase(iter);
319 } else {
320 iter++;
321 }
322 }
323 }
324 return SUCCESS;
325 }
326
RemoveSelfAppVolumeChangeCallback(int32_t appUid,const std::shared_ptr<AudioManagerAppVolumeChangeCallback> & cb)327 int32_t AudioPolicyClientStubImpl::RemoveSelfAppVolumeChangeCallback(int32_t appUid,
328 const std::shared_ptr<AudioManagerAppVolumeChangeCallback> &cb)
329 {
330 std::lock_guard<std::mutex> lockCbMap(selfAppVolumeChangeMutex_);
331 auto iter = selfAppVolumeChangeCallback_.begin();
332 while (iter != selfAppVolumeChangeCallback_.end()) {
333 if (iter->first != appUid || iter->second.get() != cb.get()) {
334 iter++;
335 continue;
336 }
337 selfAppVolumeChangeCallbackNum_[appUid]--;
338 if (selfAppVolumeChangeCallbackNum_[appUid] == 0) {
339 iter = selfAppVolumeChangeCallback_.erase(iter);
340 } else {
341 iter++;
342 }
343 }
344 return SUCCESS;
345 }
346
RemoveAllAppVolumeChangeForUidCallback()347 int32_t AudioPolicyClientStubImpl::RemoveAllAppVolumeChangeForUidCallback()
348 {
349 std::lock_guard<std::mutex> lockCbMap(appVolumeChangeForUidMutex_);
350 appVolumeChangeForUidCallback_.clear();
351 return SUCCESS;
352 }
353
RemoveAppVolumeChangeForUidCallback(const std::shared_ptr<AudioManagerAppVolumeChangeCallback> & cb)354 int32_t AudioPolicyClientStubImpl::RemoveAppVolumeChangeForUidCallback(
355 const std::shared_ptr<AudioManagerAppVolumeChangeCallback> &cb)
356 {
357 std::lock_guard<std::mutex> lockCbMap(appVolumeChangeForUidMutex_);
358 auto iter = appVolumeChangeForUidCallback_.begin();
359 while (iter != appVolumeChangeForUidCallback_.end()) {
360 if (iter->second.get() != cb.get()) {
361 iter++;
362 continue;
363 }
364 iter = appVolumeChangeForUidCallback_.erase(iter);
365 }
366 return SUCCESS;
367 }
368
AddAppVolumeChangeForUidCallback(const int32_t appUid,const std::shared_ptr<AudioManagerAppVolumeChangeCallback> & cb)369 int32_t AudioPolicyClientStubImpl::AddAppVolumeChangeForUidCallback(const int32_t appUid,
370 const std::shared_ptr<AudioManagerAppVolumeChangeCallback> &cb)
371 {
372 std::lock_guard<std::mutex> lockCbMap(appVolumeChangeForUidMutex_);
373 for (auto iter : appVolumeChangeForUidCallback_) {
374 if (iter.first == appUid && iter.second.get() == cb.get()) {
375 appVolumeChangeForUidCallbackNum[appUid]++;
376 AUDIO_INFO_LOG("appVolumeChangeForUidCallback_ No need pushback");
377 return SUCCESS;
378 }
379 }
380 appVolumeChangeForUidCallbackNum[appUid]++;
381 appVolumeChangeForUidCallback_.push_back({appUid, cb});
382 return SUCCESS;
383 }
384
AddRingerModeCallback(const std::shared_ptr<AudioRingerModeCallback> & cb)385 int32_t AudioPolicyClientStubImpl::AddRingerModeCallback(const std::shared_ptr<AudioRingerModeCallback> &cb)
386 {
387 std::lock_guard<std::mutex> lockCbMap(ringerModeMutex_);
388 ringerModeCallbackList_.push_back(cb);
389 return SUCCESS;
390 }
391
RemoveRingerModeCallback()392 int32_t AudioPolicyClientStubImpl::RemoveRingerModeCallback()
393 {
394 std::lock_guard<std::mutex> lockCbMap(ringerModeMutex_);
395 ringerModeCallbackList_.clear();
396 return SUCCESS;
397 }
398
RemoveRingerModeCallback(const std::shared_ptr<AudioRingerModeCallback> & cb)399 int32_t AudioPolicyClientStubImpl::RemoveRingerModeCallback(const std::shared_ptr<AudioRingerModeCallback> &cb)
400 {
401 std::lock_guard<std::mutex> lockCbMap(ringerModeMutex_);
402 auto iter = ringerModeCallbackList_.begin();
403 while (iter != ringerModeCallbackList_.end()) {
404 if (*iter == cb) {
405 iter = ringerModeCallbackList_.erase(iter);
406 } else {
407 iter++;
408 }
409 }
410 return SUCCESS;
411 }
412
GetRingerModeCallbackSize() const413 size_t AudioPolicyClientStubImpl::GetRingerModeCallbackSize() const
414 {
415 std::lock_guard<std::mutex> lockCbMap(ringerModeMutex_);
416 return ringerModeCallbackList_.size();
417 }
418
GetAppVolumeChangeCallbackForUidSize() const419 size_t AudioPolicyClientStubImpl::GetAppVolumeChangeCallbackForUidSize() const
420 {
421 std::lock_guard<std::mutex> lockCbMap(appVolumeChangeForUidMutex_);
422 return appVolumeChangeForUidCallback_.size();
423 }
424
GetSelfAppVolumeChangeCallbackSize() const425 size_t AudioPolicyClientStubImpl::GetSelfAppVolumeChangeCallbackSize() const
426 {
427 std::lock_guard<std::mutex> lockCbMap(selfAppVolumeChangeMutex_);
428 return selfAppVolumeChangeCallback_.size();
429 }
430
OnRingerModeUpdated(const AudioRingerMode & ringerMode)431 void AudioPolicyClientStubImpl::OnRingerModeUpdated(const AudioRingerMode &ringerMode)
432 {
433 std::lock_guard<std::mutex> lockCbMap(ringerModeMutex_);
434 for (auto it = ringerModeCallbackList_.begin(); it != ringerModeCallbackList_.end(); ++it) {
435 (*it)->OnRingerModeUpdated(ringerMode);
436 }
437 }
438
OnAppVolumeChanged(int32_t appUid,const VolumeEvent & volumeEvent)439 void AudioPolicyClientStubImpl::OnAppVolumeChanged(int32_t appUid, const VolumeEvent& volumeEvent)
440 {
441 {
442 std::lock_guard<std::mutex> lockCbMap(appVolumeChangeForUidMutex_);
443 for (auto iter : appVolumeChangeForUidCallback_) {
444 if (iter.first != appUid) {
445 continue;
446 }
447 iter.second->OnAppVolumeChangedForUid(appUid, volumeEvent);
448 }
449 }
450 {
451 std::lock_guard<std::mutex> lockCbMap(selfAppVolumeChangeMutex_);
452 for (auto iter : selfAppVolumeChangeCallback_) {
453 if (iter.first != appUid) {
454 continue;
455 }
456 iter.second->OnSelfAppVolumeChanged(volumeEvent);
457 }
458 }
459 }
460
AddAudioSessionCallback(const std::shared_ptr<AudioSessionCallback> & cb)461 int32_t AudioPolicyClientStubImpl::AddAudioSessionCallback(const std::shared_ptr<AudioSessionCallback> &cb)
462 {
463 AUDIO_INFO_LOG("AddAudioSessionCallback in");
464 std::lock_guard<std::mutex> lockCbMap(audioSessionMutex_);
465 audioSessionCallbackList_.push_back(cb);
466 return SUCCESS;
467 }
468
SetDistribuitedOutputChangeCallback(const std::shared_ptr<AudioDistribuitedOutputChangeCallback> & cb)469 int32_t AudioPolicyClientStubImpl::SetDistribuitedOutputChangeCallback(
470 const std::shared_ptr<AudioDistribuitedOutputChangeCallback> &cb)
471 {
472 distribuitedOutputChangeCallback_.clear();
473 distribuitedOutputChangeCallback_.push_back(cb);
474 return SUCCESS;
475 }
476
RemoveAudioSessionCallback()477 int32_t AudioPolicyClientStubImpl::RemoveAudioSessionCallback()
478 {
479 AUDIO_INFO_LOG("RemoveAudioSessionCallback all in");
480 std::lock_guard<std::mutex> lockCbMap(audioSessionMutex_);
481 audioSessionCallbackList_.clear();
482 return SUCCESS;
483 }
484
RemoveAudioSessionCallback(const std::shared_ptr<AudioSessionCallback> & cb)485 int32_t AudioPolicyClientStubImpl::RemoveAudioSessionCallback(const std::shared_ptr<AudioSessionCallback> &cb)
486 {
487 AUDIO_INFO_LOG("RemoveAudioSessionCallback one in");
488 std::lock_guard<std::mutex> lockCbMap(audioSessionMutex_);
489 auto iter = audioSessionCallbackList_.begin();
490 while (iter != audioSessionCallbackList_.end()) {
491 if (*iter == cb) {
492 iter = audioSessionCallbackList_.erase(iter);
493 } else {
494 iter++;
495 }
496 }
497 return SUCCESS;
498 }
499
GetAudioSessionCallbackSize() const500 size_t AudioPolicyClientStubImpl::GetAudioSessionCallbackSize() const
501 {
502 std::lock_guard<std::mutex> lockCbMap(audioSessionMutex_);
503 return audioSessionCallbackList_.size();
504 }
505
OnAudioSessionDeactive(const AudioSessionDeactiveEvent & deactiveEvent)506 void AudioPolicyClientStubImpl::OnAudioSessionDeactive(const AudioSessionDeactiveEvent &deactiveEvent)
507 {
508 AUDIO_INFO_LOG("OnAudioSessionDeactive in");
509 std::lock_guard<std::mutex> lockCbMap(audioSessionMutex_);
510 for (auto it = audioSessionCallbackList_.begin(); it != audioSessionCallbackList_.end(); ++it) {
511 (*it)->OnAudioSessionDeactive(deactiveEvent);
512 }
513 }
514
AddMicStateChangeCallback(const std::shared_ptr<AudioManagerMicStateChangeCallback> & cb)515 int32_t AudioPolicyClientStubImpl::AddMicStateChangeCallback(
516 const std::shared_ptr<AudioManagerMicStateChangeCallback> &cb)
517 {
518 std::lock_guard<std::mutex> lockCbMap(micStateChangeMutex_);
519 micStateChangeCallbackList_.push_back(cb);
520 return SUCCESS;
521 }
RemoveMicStateChangeCallback()522 int32_t AudioPolicyClientStubImpl::RemoveMicStateChangeCallback()
523 {
524 std::lock_guard<std::mutex> lockCbMap(micStateChangeMutex_);
525 micStateChangeCallbackList_.clear();
526 return SUCCESS;
527 }
528
GetMicStateChangeCallbackSize() const529 size_t AudioPolicyClientStubImpl::GetMicStateChangeCallbackSize() const
530 {
531 std::lock_guard<std::mutex> lockCbMap(micStateChangeMutex_);
532 return micStateChangeCallbackList_.size();
533 }
534
HasMicStateChangeCallback()535 bool AudioPolicyClientStubImpl::HasMicStateChangeCallback()
536 {
537 std::lock_guard<std::mutex> lockCbMap(micStateChangeMutex_);
538 if (micStateChangeCallbackList_.empty()) {
539 AUDIO_INFO_LOG("MicStateChangeCallback list is empty.");
540 return false;
541 }
542 return true;
543 }
544
OnMicStateUpdated(const MicStateChangeEvent & micStateChangeEvent)545 void AudioPolicyClientStubImpl::OnMicStateUpdated(const MicStateChangeEvent &micStateChangeEvent)
546 {
547 std::lock_guard<std::mutex> lockCbMap(micStateChangeMutex_);
548 for (auto it = micStateChangeCallbackList_.begin(); it != micStateChangeCallbackList_.end(); ++it) {
549 (*it)->OnMicStateUpdated(micStateChangeEvent);
550 }
551 }
552
AddPreferredOutputDeviceChangeCallback(const AudioRendererInfo & rendererInfo,const std::shared_ptr<AudioPreferredOutputDeviceChangeCallback> & cb)553 int32_t AudioPolicyClientStubImpl::AddPreferredOutputDeviceChangeCallback(const AudioRendererInfo &rendererInfo,
554 const std::shared_ptr<AudioPreferredOutputDeviceChangeCallback> &cb)
555 {
556 std::lock_guard<std::mutex> lockCbMap(pOutputDeviceChangeMutex_);
557 preferredOutputDeviceCallbackMap_[rendererInfo.streamUsage].push_back(cb);
558 return SUCCESS;
559 }
560
RemovePreferredOutputDeviceChangeCallback(const std::shared_ptr<AudioPreferredOutputDeviceChangeCallback> & cb)561 int32_t AudioPolicyClientStubImpl::RemovePreferredOutputDeviceChangeCallback(
562 const std::shared_ptr<AudioPreferredOutputDeviceChangeCallback> &cb)
563 {
564 std::lock_guard<std::mutex> lockCbMap(pOutputDeviceChangeMutex_);
565 if (cb == nullptr) {
566 preferredOutputDeviceCallbackMap_.clear();
567 return SUCCESS;
568 }
569 for (auto &it : preferredOutputDeviceCallbackMap_) {
570 auto iter = find(it.second.begin(), it.second.end(), cb);
571 if (iter != it.second.end()) {
572 it.second.erase(iter);
573 }
574 }
575 return SUCCESS;
576 }
577
GetPreferredOutputDeviceChangeCallbackSize() const578 size_t AudioPolicyClientStubImpl::GetPreferredOutputDeviceChangeCallbackSize() const
579 {
580 std::lock_guard<std::mutex> lockCbMap(pOutputDeviceChangeMutex_);
581 return preferredOutputDeviceCallbackMap_.size();
582 }
583
OnPreferredOutputDeviceUpdated(const AudioRendererInfo & rendererInfo,const std::vector<std::shared_ptr<AudioDeviceDescriptor>> & desc)584 void AudioPolicyClientStubImpl::OnPreferredOutputDeviceUpdated(const AudioRendererInfo &rendererInfo,
585 const std::vector<std::shared_ptr<AudioDeviceDescriptor>> &desc)
586 {
587 std::lock_guard<std::mutex> lockCbMap(pOutputDeviceChangeMutex_);
588 auto it = preferredOutputDeviceCallbackMap_.find(rendererInfo.streamUsage);
589 CHECK_AND_RETURN_LOG(it != preferredOutputDeviceCallbackMap_.end(), "streamUsage not found");
590 for (auto iter = it->second.begin(); iter != it->second.end(); ++iter) {
591 CHECK_AND_CONTINUE_LOG(iter != it->second.end() && (*iter) != nullptr, "iter is null");
592 (*iter)->OnPreferredOutputDeviceUpdated(desc);
593 }
594 }
595
AddPreferredInputDeviceChangeCallback(const AudioCapturerInfo & capturerInfo,const std::shared_ptr<AudioPreferredInputDeviceChangeCallback> & cb)596 int32_t AudioPolicyClientStubImpl::AddPreferredInputDeviceChangeCallback(const AudioCapturerInfo &capturerInfo,
597 const std::shared_ptr<AudioPreferredInputDeviceChangeCallback> &cb)
598 {
599 std::lock_guard<std::mutex> lockCbMap(pInputDeviceChangeMutex_);
600 preferredInputDeviceCallbackMap_[capturerInfo.sourceType].push_back(cb);
601 return SUCCESS;
602 }
603
RemovePreferredInputDeviceChangeCallback(const std::shared_ptr<AudioPreferredInputDeviceChangeCallback> & cb)604 int32_t AudioPolicyClientStubImpl::RemovePreferredInputDeviceChangeCallback(
605 const std::shared_ptr<AudioPreferredInputDeviceChangeCallback> &cb)
606 {
607 std::lock_guard<std::mutex> lockCbMap(pInputDeviceChangeMutex_);
608 if (cb == nullptr) {
609 preferredInputDeviceCallbackMap_.clear();
610 return SUCCESS;
611 }
612 for (auto &it : preferredInputDeviceCallbackMap_) {
613 auto iter = find(it.second.begin(), it.second.end(), cb);
614 if (iter != it.second.end()) {
615 it.second.erase(iter);
616 }
617 }
618 return SUCCESS;
619 }
620
GetPreferredInputDeviceChangeCallbackSize() const621 size_t AudioPolicyClientStubImpl::GetPreferredInputDeviceChangeCallbackSize() const
622 {
623 std::lock_guard<std::mutex> lockCbMap(pInputDeviceChangeMutex_);
624 return preferredInputDeviceCallbackMap_.size();
625 }
626
OnPreferredInputDeviceUpdated(const AudioCapturerInfo & capturerInfo,const std::vector<std::shared_ptr<AudioDeviceDescriptor>> & desc)627 void AudioPolicyClientStubImpl::OnPreferredInputDeviceUpdated(const AudioCapturerInfo &capturerInfo,
628 const std::vector<std::shared_ptr<AudioDeviceDescriptor>> &desc)
629 {
630 std::lock_guard<std::mutex> lockCbMap(pInputDeviceChangeMutex_);
631 auto it = preferredInputDeviceCallbackMap_.find(capturerInfo.sourceType);
632 CHECK_AND_RETURN_LOG(it != preferredInputDeviceCallbackMap_.end(), "sourceType not found");
633 for (auto iter = it->second.begin(); iter != it->second.end(); ++iter) {
634 CHECK_AND_CONTINUE_LOG(iter != it->second.end() && (*iter) != nullptr, "iter is null");
635 (*iter)->OnPreferredInputDeviceUpdated(desc);
636 }
637 }
638
AddRendererStateChangeCallback(const std::shared_ptr<AudioRendererStateChangeCallback> & cb)639 int32_t AudioPolicyClientStubImpl::AddRendererStateChangeCallback(
640 const std::shared_ptr<AudioRendererStateChangeCallback> &cb)
641 {
642 CHECK_AND_RETURN_RET_LOG(cb, ERR_INVALID_PARAM, "cb is null");
643
644 std::lock_guard<std::mutex> lockCbMap(rendererStateChangeMutex_);
645 rendererStateChangeCallbackList_.push_back(cb);
646 return SUCCESS;
647 }
648
RemoveRendererStateChangeCallback(const std::vector<std::shared_ptr<AudioRendererStateChangeCallback>> & callbacks)649 int32_t AudioPolicyClientStubImpl::RemoveRendererStateChangeCallback(
650 const std::vector<std::shared_ptr<AudioRendererStateChangeCallback>> &callbacks)
651 {
652 std::lock_guard<std::mutex> lockCbMap(rendererStateChangeMutex_);
653 for (const auto &cb : callbacks) {
654 rendererStateChangeCallbackList_.erase(
655 std::remove(rendererStateChangeCallbackList_.begin(),
656 rendererStateChangeCallbackList_.end(), cb), rendererStateChangeCallbackList_.end());
657 }
658
659 return SUCCESS;
660 }
661
RemoveRendererStateChangeCallback(const std::shared_ptr<AudioRendererStateChangeCallback> & callback)662 int32_t AudioPolicyClientStubImpl::RemoveRendererStateChangeCallback(
663 const std::shared_ptr<AudioRendererStateChangeCallback> &callback)
664 {
665 std::lock_guard<std::mutex> lockCbMap(rendererStateChangeMutex_);
666 rendererStateChangeCallbackList_.erase(
667 std::remove(rendererStateChangeCallbackList_.begin(),
668 rendererStateChangeCallbackList_.end(), callback), rendererStateChangeCallbackList_.end());
669
670 return SUCCESS;
671 }
672
GetRendererStateChangeCallbackSize() const673 size_t AudioPolicyClientStubImpl::GetRendererStateChangeCallbackSize() const
674 {
675 std::lock_guard<std::mutex> lockCbMap(rendererStateChangeMutex_);
676 return rendererStateChangeCallbackList_.size();
677 }
678
AddDeviceChangeWithInfoCallback(const uint32_t sessionId,const std::weak_ptr<DeviceChangeWithInfoCallback> & cb)679 int32_t AudioPolicyClientStubImpl::AddDeviceChangeWithInfoCallback(
680 const uint32_t sessionId, const std::weak_ptr<DeviceChangeWithInfoCallback> &cb)
681 {
682 std::lock_guard<std::mutex> lockCbMap(deviceChangeWithInfoCallbackMutex_);
683 deviceChangeWithInfoCallbackMap_[sessionId] = cb;
684 return SUCCESS;
685 }
686
RemoveDeviceChangeWithInfoCallback(const uint32_t sessionId)687 int32_t AudioPolicyClientStubImpl::RemoveDeviceChangeWithInfoCallback(const uint32_t sessionId)
688 {
689 std::lock_guard<std::mutex> lockCbMap(deviceChangeWithInfoCallbackMutex_);
690 deviceChangeWithInfoCallbackMap_.erase(sessionId);
691 return SUCCESS;
692 }
693
GetDeviceChangeWithInfoCallbackkSize() const694 size_t AudioPolicyClientStubImpl::GetDeviceChangeWithInfoCallbackkSize() const
695 {
696 std::lock_guard<std::mutex> lockCbMap(deviceChangeWithInfoCallbackMutex_);
697 return deviceChangeWithInfoCallbackMap_.size();
698 }
699
OnRendererDeviceChange(const uint32_t sessionId,const AudioDeviceDescriptor & deviceInfo,const AudioStreamDeviceChangeReasonExt reason)700 void AudioPolicyClientStubImpl::OnRendererDeviceChange(const uint32_t sessionId,
701 const AudioDeviceDescriptor &deviceInfo, const AudioStreamDeviceChangeReasonExt reason)
702 {
703 Trace trace("AudioPolicyClientStubImpl::OnRendererDeviceChange");
704 std::shared_ptr<DeviceChangeWithInfoCallback> callback = nullptr;
705 {
706 std::lock_guard<std::mutex> lockCbMap(deviceChangeWithInfoCallbackMutex_);
707 if (deviceChangeWithInfoCallbackMap_.count(sessionId) == 0) {
708 return;
709 }
710 callback = deviceChangeWithInfoCallbackMap_.at(sessionId).lock();
711 if (callback == nullptr) {
712 deviceChangeWithInfoCallbackMap_.erase(sessionId);
713 return;
714 }
715 }
716 if (callback != nullptr) {
717 Trace traceCallback("callback->OnDeviceChangeWithInfo sessionid:" + std::to_string(sessionId)
718 + " reason:" + std::to_string(static_cast<int>(reason)));
719 callback->OnDeviceChangeWithInfo(sessionId, deviceInfo, reason);
720 }
721 }
722
OnRendererStateChange(std::vector<std::shared_ptr<AudioRendererChangeInfo>> & audioRendererChangeInfos)723 void AudioPolicyClientStubImpl::OnRendererStateChange(
724 std::vector<std::shared_ptr<AudioRendererChangeInfo>> &audioRendererChangeInfos)
725 {
726 std::vector<std::shared_ptr<AudioRendererStateChangeCallback>> callbacks;
727 {
728 std::lock_guard<std::mutex> lockCbMap(rendererStateChangeMutex_);
729 callbacks = rendererStateChangeCallbackList_;
730 }
731 size_t cBSize = callbacks.size();
732 size_t infosSize = audioRendererChangeInfos.size();
733 AUDIO_DEBUG_LOG("cbSize: %{public}zu infoSize: %{public}zu", cBSize, infosSize);
734
735 if (getuid() == RSS_UID) {
736 AUDIO_INFO_LOG("cbSize: %{public}zu infoSize: %{public}zu", cBSize, infosSize);
737 }
738
739 Trace trace("AudioPolicyClientStubImpl::OnRendererStateChange");
740 for (auto &cb : callbacks) {
741 Trace traceCallback("OnRendererStateChange");
742 cb->OnRendererStateChange(audioRendererChangeInfos);
743 }
744 }
745
OnRecreateRendererStreamEvent(const uint32_t sessionId,const int32_t streamFlag,const AudioStreamDeviceChangeReasonExt reason)746 void AudioPolicyClientStubImpl::OnRecreateRendererStreamEvent(const uint32_t sessionId, const int32_t streamFlag,
747 const AudioStreamDeviceChangeReasonExt reason)
748 {
749 AUDIO_INFO_LOG("Enter");
750 std::shared_ptr<DeviceChangeWithInfoCallback> callback = nullptr;
751 {
752 std::lock_guard<std::mutex> lockCbMap(deviceChangeWithInfoCallbackMutex_);
753 if (deviceChangeWithInfoCallbackMap_.count(sessionId) == 0) {
754 AUDIO_ERR_LOG("No session id %{public}d", sessionId);
755 return;
756 }
757 callback = deviceChangeWithInfoCallbackMap_.at(sessionId).lock();
758 }
759 if (callback != nullptr) {
760 callback->OnRecreateStreamEvent(sessionId, streamFlag, reason);
761 }
762 }
763
OnRecreateCapturerStreamEvent(const uint32_t sessionId,const int32_t streamFlag,const AudioStreamDeviceChangeReasonExt reason)764 void AudioPolicyClientStubImpl::OnRecreateCapturerStreamEvent(const uint32_t sessionId, const int32_t streamFlag,
765 const AudioStreamDeviceChangeReasonExt reason)
766 {
767 AUDIO_INFO_LOG("Enter");
768 std::shared_ptr<DeviceChangeWithInfoCallback> callback = nullptr;
769 {
770 std::lock_guard<std::mutex> lockCbMap(deviceChangeWithInfoCallbackMutex_);
771 if (deviceChangeWithInfoCallbackMap_.count(sessionId) == 0) {
772 AUDIO_ERR_LOG("No session id %{public}d", sessionId);
773 return;
774 }
775 callback = deviceChangeWithInfoCallbackMap_.at(sessionId).lock();
776 }
777 if (callback != nullptr) {
778 callback->OnRecreateStreamEvent(sessionId, streamFlag, reason);
779 }
780 }
781
AddCapturerStateChangeCallback(const std::shared_ptr<AudioCapturerStateChangeCallback> & cb)782 int32_t AudioPolicyClientStubImpl::AddCapturerStateChangeCallback(
783 const std::shared_ptr<AudioCapturerStateChangeCallback> &cb)
784 {
785 std::lock_guard<std::mutex> lockCbMap(capturerStateChangeMutex_);
786 capturerStateChangeCallbackList_.push_back(cb);
787 return SUCCESS;
788 }
789
RemoveCapturerStateChangeCallback()790 int32_t AudioPolicyClientStubImpl::RemoveCapturerStateChangeCallback()
791 {
792 std::lock_guard<std::mutex> lockCbMap(capturerStateChangeMutex_);
793 capturerStateChangeCallbackList_.clear();
794 return SUCCESS;
795 }
796
GetCapturerStateChangeCallbackSize() const797 size_t AudioPolicyClientStubImpl::GetCapturerStateChangeCallbackSize() const
798 {
799 std::lock_guard<std::mutex> lockCbMap(capturerStateChangeMutex_);
800 return capturerStateChangeCallbackList_.size();
801 }
802
OnCapturerStateChange(std::vector<std::shared_ptr<AudioCapturerChangeInfo>> & audioCapturerChangeInfos)803 void AudioPolicyClientStubImpl::OnCapturerStateChange(
804 std::vector<std::shared_ptr<AudioCapturerChangeInfo>> &audioCapturerChangeInfos)
805 {
806 std::vector<std::shared_ptr<AudioCapturerStateChangeCallback>> tmpCallbackList;
807 {
808 std::lock_guard<std::mutex> lockCbMap(capturerStateChangeMutex_);
809 for (auto it = capturerStateChangeCallbackList_.begin(); it != capturerStateChangeCallbackList_.end(); ++it) {
810 std::shared_ptr<AudioCapturerStateChangeCallback> capturerStateChangeCallback = (*it).lock();
811 if (capturerStateChangeCallback != nullptr) {
812 tmpCallbackList.emplace_back(capturerStateChangeCallback);
813 }
814 }
815 }
816 for (auto it = tmpCallbackList.begin(); it != tmpCallbackList.end(); ++it) {
817 if (*it == nullptr) {
818 AUDIO_WARNING_LOG("tmpCallbackList is nullptr");
819 continue;
820 }
821 (*it)->OnCapturerStateChange(audioCapturerChangeInfos);
822 }
823 }
824
AddHeadTrackingDataRequestedChangeCallback(const std::string & macAddress,const std::shared_ptr<HeadTrackingDataRequestedChangeCallback> & cb)825 int32_t AudioPolicyClientStubImpl::AddHeadTrackingDataRequestedChangeCallback(const std::string &macAddress,
826 const std::shared_ptr<HeadTrackingDataRequestedChangeCallback> &cb)
827 {
828 std::lock_guard<std::mutex> lockCbMap(headTrackingDataRequestedChangeMutex_);
829 if (!headTrackingDataRequestedChangeCallbackMap_.count(macAddress)) {
830 AUDIO_INFO_LOG("First registeration for the specified device");
831 headTrackingDataRequestedChangeCallbackMap_.insert(std::make_pair(macAddress, cb));
832 } else {
833 AUDIO_INFO_LOG("Repeated registeration for the specified device, replaced by the new one");
834 headTrackingDataRequestedChangeCallbackMap_[macAddress] = cb;
835 }
836 return SUCCESS;
837 }
838
RemoveHeadTrackingDataRequestedChangeCallback(const std::string & macAddress)839 int32_t AudioPolicyClientStubImpl::RemoveHeadTrackingDataRequestedChangeCallback(const std::string &macAddress)
840 {
841 std::lock_guard<std::mutex> lockCbMap(headTrackingDataRequestedChangeMutex_);
842 headTrackingDataRequestedChangeCallbackMap_.erase(macAddress);
843 return SUCCESS;
844 }
845
GetHeadTrackingDataRequestedChangeCallbackSize() const846 size_t AudioPolicyClientStubImpl::GetHeadTrackingDataRequestedChangeCallbackSize() const
847 {
848 std::lock_guard<std::mutex> lockCbMap(headTrackingDataRequestedChangeMutex_);
849 return headTrackingDataRequestedChangeCallbackMap_.size();
850 }
851
OnHeadTrackingDeviceChange(const std::unordered_map<std::string,bool> & changeInfo)852 void AudioPolicyClientStubImpl::OnHeadTrackingDeviceChange(const std::unordered_map<std::string, bool> &changeInfo)
853 {
854 std::lock_guard<std::mutex> lockCbMap(headTrackingDataRequestedChangeMutex_);
855 if (headTrackingDataRequestedChangeCallbackMap_.size() == 0) {
856 return;
857 }
858 for (const auto &pair : changeInfo) {
859 if (!headTrackingDataRequestedChangeCallbackMap_.count(pair.first)) {
860 AUDIO_WARNING_LOG("the specified device has not been registered");
861 continue;
862 }
863 std::shared_ptr<HeadTrackingDataRequestedChangeCallback> headTrackingDataRequestedChangeCallback =
864 headTrackingDataRequestedChangeCallbackMap_[pair.first];
865 if (headTrackingDataRequestedChangeCallback != nullptr) {
866 AUDIO_DEBUG_LOG("head tracking data requested change event of the specified device has been notified");
867 headTrackingDataRequestedChangeCallback->OnHeadTrackingDataRequestedChange(pair.second);
868 }
869 }
870 }
871
AddSpatializationEnabledChangeCallback(const std::shared_ptr<AudioSpatializationEnabledChangeCallback> & cb)872 int32_t AudioPolicyClientStubImpl::AddSpatializationEnabledChangeCallback(
873 const std::shared_ptr<AudioSpatializationEnabledChangeCallback> &cb)
874 {
875 std::lock_guard<std::mutex> lockCbMap(spatializationEnabledChangeMutex_);
876 spatializationEnabledChangeCallbackList_.push_back(cb);
877 return SUCCESS;
878 }
879
RemoveSpatializationEnabledChangeCallback()880 int32_t AudioPolicyClientStubImpl::RemoveSpatializationEnabledChangeCallback()
881 {
882 std::lock_guard<std::mutex> lockCbMap(spatializationEnabledChangeMutex_);
883 spatializationEnabledChangeCallbackList_.clear();
884 return SUCCESS;
885 }
886
GetSpatializationEnabledChangeCallbackSize() const887 size_t AudioPolicyClientStubImpl::GetSpatializationEnabledChangeCallbackSize() const
888 {
889 std::lock_guard<std::mutex> lockCbMap(spatializationEnabledChangeMutex_);
890 return spatializationEnabledChangeCallbackList_.size();
891 }
892
OnSpatializationEnabledChange(const bool & enabled)893 void AudioPolicyClientStubImpl::OnSpatializationEnabledChange(const bool &enabled)
894 {
895 std::lock_guard<std::mutex> lockCbMap(spatializationEnabledChangeMutex_);
896 for (const auto &callback : spatializationEnabledChangeCallbackList_) {
897 callback->OnSpatializationEnabledChange(enabled);
898 }
899 }
900
OnSpatializationEnabledChangeForAnyDevice(const std::shared_ptr<AudioDeviceDescriptor> & deviceDescriptor,const bool & enabled)901 void AudioPolicyClientStubImpl::OnSpatializationEnabledChangeForAnyDevice(
902 const std::shared_ptr<AudioDeviceDescriptor> &deviceDescriptor, const bool &enabled)
903 {
904 std::lock_guard<std::mutex> lockCbMap(spatializationEnabledChangeMutex_);
905 for (const auto &callback : spatializationEnabledChangeCallbackList_) {
906 callback->OnSpatializationEnabledChangeForAnyDevice(deviceDescriptor, enabled);
907 }
908 }
909
AddSpatializationEnabledChangeForCurrentDeviceCallback(const std::shared_ptr<AudioSpatializationEnabledChangeForCurrentDeviceCallback> & cb)910 int32_t AudioPolicyClientStubImpl::AddSpatializationEnabledChangeForCurrentDeviceCallback(
911 const std::shared_ptr<AudioSpatializationEnabledChangeForCurrentDeviceCallback> &cb)
912 {
913 std::lock_guard<std::mutex> lockCbMap(spatializationEnabledChangeForCurrentDeviceMutex_);
914 spatializationEnabledChangeForCurrentDeviceCallbackList_.push_back(cb);
915 return SUCCESS;
916 }
917
RemoveSpatializationEnabledChangeForCurrentDeviceCallback()918 int32_t AudioPolicyClientStubImpl::RemoveSpatializationEnabledChangeForCurrentDeviceCallback()
919 {
920 std::lock_guard<std::mutex> lockCbMap(spatializationEnabledChangeForCurrentDeviceMutex_);
921 spatializationEnabledChangeForCurrentDeviceCallbackList_.clear();
922 return SUCCESS;
923 }
924
GetSpatializationEnabledChangeForCurrentDeviceCallbackSize() const925 size_t AudioPolicyClientStubImpl::GetSpatializationEnabledChangeForCurrentDeviceCallbackSize() const
926 {
927 std::lock_guard<std::mutex> lockCbMap(spatializationEnabledChangeForCurrentDeviceMutex_);
928 return spatializationEnabledChangeForCurrentDeviceCallbackList_.size();
929 }
930
OnSpatializationEnabledChangeForCurrentDevice(const bool & enabled)931 void AudioPolicyClientStubImpl::OnSpatializationEnabledChangeForCurrentDevice(const bool &enabled)
932 {
933 std::lock_guard<std::mutex> lockCbMap(spatializationEnabledChangeForCurrentDeviceMutex_);
934 for (const auto &callback : spatializationEnabledChangeForCurrentDeviceCallbackList_) {
935 callback->OnSpatializationEnabledChangeForCurrentDevice(enabled);
936 }
937 }
938
AddHeadTrackingEnabledChangeCallback(const std::shared_ptr<AudioHeadTrackingEnabledChangeCallback> & cb)939 int32_t AudioPolicyClientStubImpl::AddHeadTrackingEnabledChangeCallback(
940 const std::shared_ptr<AudioHeadTrackingEnabledChangeCallback> &cb)
941 {
942 std::lock_guard<std::mutex> lockCbMap(headTrackingEnabledChangeMutex_);
943 headTrackingEnabledChangeCallbackList_.push_back(cb);
944 return SUCCESS;
945 }
946
RemoveHeadTrackingEnabledChangeCallback()947 int32_t AudioPolicyClientStubImpl::RemoveHeadTrackingEnabledChangeCallback()
948 {
949 std::lock_guard<std::mutex> lockCbMap(headTrackingEnabledChangeMutex_);
950 headTrackingEnabledChangeCallbackList_.clear();
951 return SUCCESS;
952 }
953
GetHeadTrackingEnabledChangeCallbacSize() const954 size_t AudioPolicyClientStubImpl::GetHeadTrackingEnabledChangeCallbacSize() const
955 {
956 std::lock_guard<std::mutex> lockCbMap(headTrackingEnabledChangeMutex_);
957 return headTrackingEnabledChangeCallbackList_.size();
958 }
959
OnHeadTrackingEnabledChange(const bool & enabled)960 void AudioPolicyClientStubImpl::OnHeadTrackingEnabledChange(const bool &enabled)
961 {
962 std::lock_guard<std::mutex> lockCbMap(headTrackingEnabledChangeMutex_);
963 for (const auto &callback : headTrackingEnabledChangeCallbackList_) {
964 callback->OnHeadTrackingEnabledChange(enabled);
965 }
966 }
967
OnHeadTrackingEnabledChangeForAnyDevice(const std::shared_ptr<AudioDeviceDescriptor> & deviceDescriptor,const bool & enabled)968 void AudioPolicyClientStubImpl::OnHeadTrackingEnabledChangeForAnyDevice(
969 const std::shared_ptr<AudioDeviceDescriptor> &deviceDescriptor, const bool &enabled)
970 {
971 std::lock_guard<std::mutex> lockCbMap(headTrackingEnabledChangeMutex_);
972 for (const auto &callback : headTrackingEnabledChangeCallbackList_) {
973 callback->OnHeadTrackingEnabledChangeForAnyDevice(deviceDescriptor, enabled);
974 }
975 }
976
AddNnStateChangeCallback(const std::shared_ptr<AudioNnStateChangeCallback> & cb)977 int32_t AudioPolicyClientStubImpl::AddNnStateChangeCallback(const std::shared_ptr<AudioNnStateChangeCallback> &cb)
978 {
979 std::lock_guard<std::mutex> lockCbMap(nnStateChangeMutex_);
980 nnStateChangeCallbackList_.push_back(cb);
981 return SUCCESS;
982 }
983
RemoveNnStateChangeCallback()984 int32_t AudioPolicyClientStubImpl::RemoveNnStateChangeCallback()
985 {
986 std::lock_guard<std::mutex> lockCbMap(nnStateChangeMutex_);
987 nnStateChangeCallbackList_.clear();
988 return SUCCESS;
989 }
990
GetNnStateChangeCallbackSize() const991 size_t AudioPolicyClientStubImpl::GetNnStateChangeCallbackSize() const
992 {
993 std::lock_guard<std::mutex> lockCbMap(nnStateChangeMutex_);
994 return nnStateChangeCallbackList_.size();
995 }
996
OnNnStateChange(const int32_t & nnState)997 void AudioPolicyClientStubImpl::OnNnStateChange(const int32_t &nnState)
998 {
999 std::lock_guard<std::mutex> lockCbMap(nnStateChangeMutex_);
1000 for (const auto &callback : nnStateChangeCallbackList_) {
1001 callback->OnNnStateChange(nnState);
1002 }
1003 }
1004 } // namespace AudioStandard
1005 } // namespace OHOS