• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <memory>
20 
21 #include "audio_policy_client_stub_impl.h"
22 #include "audio_errors.h"
23 #include "audio_policy_log.h"
24 #include "audio_utils.h"
25 
26 namespace OHOS {
27 namespace AudioStandard {
28 constexpr int32_t RSS_UID = 1096;
29 static const int32_t FOCUS_INFO_VALID_SIZE = 128;
30 static const int32_t DEVICE_CHANGE_VALID_SIZE = 128;
31 static const int32_t PREFERRED_DEVICE_VALID_SIZE = 128;
32 static const int32_t STATE_VALID_SIZE = 1024;
33 static const int32_t MIC_BLOCKED_VALID_SIZE = 128;
34 
AddVolumeKeyEventCallback(const std::shared_ptr<VolumeKeyEventCallback> & cb)35 int32_t AudioPolicyClientStubImpl::AddVolumeKeyEventCallback(const std::shared_ptr<VolumeKeyEventCallback> &cb)
36 {
37     std::lock_guard<std::mutex> lockCbMap(volumeKeyEventMutex_);
38     volumeKeyEventCallbackList_.push_back(cb);
39     return SUCCESS;
40 }
41 
RemoveVolumeKeyEventCallback(const std::shared_ptr<VolumeKeyEventCallback> & cb)42 int32_t AudioPolicyClientStubImpl::RemoveVolumeKeyEventCallback(const std::shared_ptr<VolumeKeyEventCallback> &cb)
43 {
44     std::lock_guard<std::mutex> lockCbMap(volumeKeyEventMutex_);
45     if (cb == nullptr) {
46         volumeKeyEventCallbackList_.clear();
47         return SUCCESS;
48     }
49     auto it = find_if(volumeKeyEventCallbackList_.begin(), volumeKeyEventCallbackList_.end(),
50         [&cb](const std::weak_ptr<VolumeKeyEventCallback>& elem) {
51             return elem.lock() == cb;
52         });
53     if (it != volumeKeyEventCallbackList_.end()) {
54         volumeKeyEventCallbackList_.erase(it);
55     }
56     return SUCCESS;
57 }
58 
GetVolumeKeyEventCallbackSize() const59 size_t AudioPolicyClientStubImpl::GetVolumeKeyEventCallbackSize() const
60 {
61     std::lock_guard<std::mutex> lockCbMap(volumeKeyEventMutex_);
62     return volumeKeyEventCallbackList_.size();
63 }
64 
OnVolumeKeyEvent(const VolumeEvent & volumeEvent)65 int32_t AudioPolicyClientStubImpl::OnVolumeKeyEvent(const VolumeEvent &volumeEvent)
66 {
67     std::lock_guard<std::mutex> lockCbMap(volumeKeyEventMutex_);
68     for (auto it = volumeKeyEventCallbackList_.begin(); it != volumeKeyEventCallbackList_.end(); ++it) {
69         std::shared_ptr<VolumeKeyEventCallback> volumeKeyEventCallback = (*it).lock();
70         if (volumeKeyEventCallback != nullptr) {
71             volumeKeyEventCallback->OnVolumeKeyEvent(volumeEvent);
72         }
73     }
74     return SUCCESS;
75 }
76 
AddVolumeDegreeCallback(const std::shared_ptr<VolumeKeyEventCallback> & cb)77 int32_t AudioPolicyClientStubImpl::AddVolumeDegreeCallback(const std::shared_ptr<VolumeKeyEventCallback> &cb)
78 {
79     std::lock_guard<std::mutex> lockCbMap(volumeDegreeEventMutex_);
80     volumeDegreeCallbackList_.push_back(cb);
81     return SUCCESS;
82 }
83 
RemoveVolumeDegreeCallback(const std::shared_ptr<VolumeKeyEventCallback> & cb)84 int32_t AudioPolicyClientStubImpl::RemoveVolumeDegreeCallback(const std::shared_ptr<VolumeKeyEventCallback> &cb)
85 {
86     std::lock_guard<std::mutex> lockCbMap(volumeDegreeEventMutex_);
87     if (cb == nullptr) {
88         volumeDegreeCallbackList_.clear();
89         return SUCCESS;
90     }
91     auto it = find_if(volumeDegreeCallbackList_.begin(), volumeDegreeCallbackList_.end(),
92         [&cb](const std::weak_ptr<VolumeKeyEventCallback>& elem) {
93             return elem.lock() == cb;
94         });
95     if (it != volumeDegreeCallbackList_.end()) {
96         volumeDegreeCallbackList_.erase(it);
97     }
98     return SUCCESS;
99 }
100 
GetVolumeDegreeCallbackSize() const101 size_t AudioPolicyClientStubImpl::GetVolumeDegreeCallbackSize() const
102 {
103     std::lock_guard<std::mutex> lockCbMap(volumeDegreeEventMutex_);
104     return volumeDegreeCallbackList_.size();
105 }
106 
OnVolumeDegreeEvent(const VolumeEvent & volumeEvent)107 int32_t AudioPolicyClientStubImpl::OnVolumeDegreeEvent(const VolumeEvent &volumeEvent)
108 {
109     std::lock_guard<std::mutex> lockCbMap(volumeDegreeEventMutex_);
110     for (auto it = volumeDegreeCallbackList_.begin(); it != volumeDegreeCallbackList_.end(); ++it) {
111         std::shared_ptr<VolumeKeyEventCallback> volumeKeyEventCallback = (*it).lock();
112         if (volumeKeyEventCallback != nullptr) {
113             volumeKeyEventCallback->OnVolumeDegreeEvent(volumeEvent);
114         }
115     }
116     return SUCCESS;
117 }
118 
AddSystemVolumeChangeCallback(const std::shared_ptr<SystemVolumeChangeCallback> & cb)119 int32_t AudioPolicyClientStubImpl::AddSystemVolumeChangeCallback(const std::shared_ptr<SystemVolumeChangeCallback> &cb)
120 {
121     std::lock_guard<std::mutex> lockCbMap(systemVolumeChangeMutex_);
122     systemVolumeChangeCallbackList_.push_back(cb);
123     return SUCCESS;
124 }
125 
RemoveSystemVolumeChangeCallback(const std::shared_ptr<SystemVolumeChangeCallback> & cb)126 int32_t AudioPolicyClientStubImpl::RemoveSystemVolumeChangeCallback(
127     const std::shared_ptr<SystemVolumeChangeCallback> &cb)
128 {
129     std::lock_guard<std::mutex> lockCbMap(systemVolumeChangeMutex_);
130     if (cb == nullptr) {
131         systemVolumeChangeCallbackList_.clear();
132         return SUCCESS;
133     }
134     auto it = find_if(systemVolumeChangeCallbackList_.begin(), systemVolumeChangeCallbackList_.end(),
135         [&cb](const std::weak_ptr<SystemVolumeChangeCallback>& elem) {
136             return elem.lock() == cb;
137         });
138     if (it != systemVolumeChangeCallbackList_.end()) {
139         systemVolumeChangeCallbackList_.erase(it);
140     }
141     return SUCCESS;
142 }
143 
GetSystemVolumeChangeCallbackSize() const144 size_t AudioPolicyClientStubImpl::GetSystemVolumeChangeCallbackSize() const
145 {
146     std::lock_guard<std::mutex> lockCbMap(systemVolumeChangeMutex_);
147     return systemVolumeChangeCallbackList_.size();
148 }
149 
OnSystemVolumeChange(const VolumeEvent & volumeEvent)150 int32_t AudioPolicyClientStubImpl::OnSystemVolumeChange(const VolumeEvent &volumeEvent)
151 {
152     std::lock_guard<std::mutex> lockCbMap(systemVolumeChangeMutex_);
153     for (auto it = systemVolumeChangeCallbackList_.begin(); it != systemVolumeChangeCallbackList_.end(); ++it) {
154         std::shared_ptr<SystemVolumeChangeCallback> systemVolumeChangeCallback = (*it).lock();
155         if (systemVolumeChangeCallback != nullptr) {
156             systemVolumeChangeCallback->OnSystemVolumeChange(volumeEvent);
157         }
158     }
159     return SUCCESS;
160 }
161 
AddFocusInfoChangeCallback(const std::shared_ptr<AudioFocusInfoChangeCallback> & cb)162 int32_t AudioPolicyClientStubImpl::AddFocusInfoChangeCallback(const std::shared_ptr<AudioFocusInfoChangeCallback> &cb)
163 {
164     std::lock_guard<std::mutex> lockCbMap(focusInfoChangeMutex_);
165     focusInfoChangeCallbackList_.push_back(cb);
166     return SUCCESS;
167 }
168 
RemoveFocusInfoChangeCallback()169 int32_t AudioPolicyClientStubImpl::RemoveFocusInfoChangeCallback()
170 {
171     std::lock_guard<std::mutex> lockCbMap(focusInfoChangeMutex_);
172     focusInfoChangeCallbackList_.clear();
173     return SUCCESS;
174 }
175 
OnAudioFocusInfoChange(const std::vector<std::map<AudioInterrupt,int32_t>> & focusInfoList)176 int32_t AudioPolicyClientStubImpl::OnAudioFocusInfoChange(
177     const std::vector<std::map<AudioInterrupt, int32_t>> &focusInfoList)
178 {
179     int32_t size = static_cast<int32_t>(focusInfoList.size());
180     CHECK_AND_RETURN_RET_LOG(size < FOCUS_INFO_VALID_SIZE, ERR_INVALID_PARAM, "get invalid size : %{public}d", size);
181     std::lock_guard<std::mutex> lockCbMap(focusInfoChangeMutex_);
182 
183     std::list<std::pair<AudioInterrupt, AudioFocuState>> newFocusInfoList;
184     for (const auto& map : focusInfoList) {
185         for (const auto& [key, value] : map) {
186             newFocusInfoList.emplace_back(key, static_cast<AudioFocuState>(value));
187         }
188     }
189 
190     for (auto it = focusInfoChangeCallbackList_.begin(); it != focusInfoChangeCallbackList_.end(); ++it) {
191         (*it)->OnAudioFocusInfoChange(newFocusInfoList);
192     }
193     return SUCCESS;
194 }
195 
OnAudioFocusRequested(const AudioInterrupt & requestFocus)196 int32_t AudioPolicyClientStubImpl::OnAudioFocusRequested(const AudioInterrupt &requestFocus)
197 {
198     std::lock_guard<std::mutex> lockCbMap(focusInfoChangeMutex_);
199     for (auto it = focusInfoChangeCallbackList_.begin(); it != focusInfoChangeCallbackList_.end(); ++it) {
200         (*it)->OnAudioFocusRequested(requestFocus);
201     }
202     return SUCCESS;
203 }
204 
OnAudioFocusAbandoned(const AudioInterrupt & abandonFocus)205 int32_t AudioPolicyClientStubImpl::OnAudioFocusAbandoned(const AudioInterrupt &abandonFocus)
206 {
207     std::lock_guard<std::mutex> lockCbMap(focusInfoChangeMutex_);
208     for (auto it = focusInfoChangeCallbackList_.begin(); it != focusInfoChangeCallbackList_.end(); ++it) {
209         (*it)->OnAudioFocusAbandoned(abandonFocus);
210     }
211     return SUCCESS;
212 }
213 
GetFocusInfoChangeCallbackSize() const214 size_t AudioPolicyClientStubImpl::GetFocusInfoChangeCallbackSize() const
215 {
216     std::lock_guard<std::mutex> lockCbMap(focusInfoChangeMutex_);
217     return focusInfoChangeCallbackList_.size();
218 }
219 
DeviceFilterByFlag(DeviceFlag flag,const std::vector<std::shared_ptr<AudioDeviceDescriptor>> & desc)220 std::vector<std::shared_ptr<AudioDeviceDescriptor>> AudioPolicyClientStubImpl::DeviceFilterByFlag(DeviceFlag flag,
221     const std::vector<std::shared_ptr<AudioDeviceDescriptor>>& desc)
222 {
223     std::vector<std::shared_ptr<AudioDeviceDescriptor>> descRet;
224     DeviceRole role = DEVICE_ROLE_NONE;
225     switch (flag) {
226         case DeviceFlag::ALL_DEVICES_FLAG:
227             for (std::shared_ptr<AudioDeviceDescriptor> var : desc) {
228                 if (var->networkId_ == LOCAL_NETWORK_ID) {
229                     descRet.insert(descRet.end(), var);
230                 }
231             }
232             break;
233         case DeviceFlag::ALL_DISTRIBUTED_DEVICES_FLAG:
234             for (std::shared_ptr<AudioDeviceDescriptor> var : desc) {
235                 if (var->networkId_ != LOCAL_NETWORK_ID) {
236                     descRet.insert(descRet.end(), var);
237                 }
238             }
239             break;
240         case DeviceFlag::ALL_L_D_DEVICES_FLAG:
241             descRet = desc;
242             break;
243         case DeviceFlag::OUTPUT_DEVICES_FLAG:
244         case DeviceFlag::INPUT_DEVICES_FLAG:
245             role = flag == INPUT_DEVICES_FLAG ? INPUT_DEVICE : OUTPUT_DEVICE;
246             for (std::shared_ptr<AudioDeviceDescriptor> var : desc) {
247                 if (var->networkId_ == LOCAL_NETWORK_ID && var->deviceRole_ == role) {
248                     descRet.insert(descRet.end(), var);
249                 }
250             }
251             break;
252         case DeviceFlag::DISTRIBUTED_OUTPUT_DEVICES_FLAG:
253         case DeviceFlag::DISTRIBUTED_INPUT_DEVICES_FLAG:
254             role = flag == DISTRIBUTED_INPUT_DEVICES_FLAG ? INPUT_DEVICE : OUTPUT_DEVICE;
255             for (std::shared_ptr<AudioDeviceDescriptor> var : desc) {
256                 if (var->networkId_ != LOCAL_NETWORK_ID && var->deviceRole_ == role) {
257                     descRet.insert(descRet.end(), var);
258                 }
259             }
260             break;
261         default:
262             break;
263     }
264     return descRet;
265 }
266 
AddDeviceChangeCallback(const DeviceFlag & flag,const std::shared_ptr<AudioManagerDeviceChangeCallback> & cb)267 int32_t AudioPolicyClientStubImpl::AddDeviceChangeCallback(const DeviceFlag &flag,
268     const std::shared_ptr<AudioManagerDeviceChangeCallback> &cb)
269 {
270     std::lock_guard<std::mutex> lockCbMap(deviceChangeMutex_);
271     deviceChangeCallbackList_.push_back(std::make_pair(flag, cb));
272     return SUCCESS;
273 }
274 
RemoveDeviceChangeCallback(DeviceFlag flag,std::shared_ptr<AudioManagerDeviceChangeCallback> & cb)275 int32_t AudioPolicyClientStubImpl::RemoveDeviceChangeCallback(DeviceFlag flag,
276     std::shared_ptr<AudioManagerDeviceChangeCallback> &cb)
277 {
278     std::lock_guard<std::mutex> lockCbMap(deviceChangeMutex_);
279     auto iter = deviceChangeCallbackList_.begin();
280     while (iter != deviceChangeCallbackList_.end()) {
281         if ((iter->first & flag) && (iter->second == cb || cb == nullptr)) {
282             AUDIO_INFO_LOG("remove device change cb flag:%{public}d", flag);
283             iter = deviceChangeCallbackList_.erase(iter);
284         } else {
285             iter++;
286         }
287     }
288     return SUCCESS;
289 }
290 
GetDeviceChangeCallbackSize() const291 size_t AudioPolicyClientStubImpl::GetDeviceChangeCallbackSize() const
292 {
293     std::lock_guard<std::mutex> lockCbMap(deviceChangeMutex_);
294     return deviceChangeCallbackList_.size();
295 }
296 
OnDeviceChange(const DeviceChangeAction & dca)297 int32_t AudioPolicyClientStubImpl::OnDeviceChange(const DeviceChangeAction &dca)
298 {
299     int32_t size = static_cast<int32_t>(dca.deviceDescriptors.size());
300     CHECK_AND_RETURN_RET_LOG(size < DEVICE_CHANGE_VALID_SIZE, ERR_INVALID_PARAM,
301         "get invalid size : %{public}d", size);
302     std::lock_guard<std::mutex> lockCbMap(deviceChangeMutex_);
303     DeviceChangeAction deviceChangeAction;
304     deviceChangeAction.type = dca.type;
305     for (auto it = deviceChangeCallbackList_.begin(); it != deviceChangeCallbackList_.end(); ++it) {
306         deviceChangeAction.flag = it->first;
307         deviceChangeAction.deviceDescriptors = DeviceFilterByFlag(it->first, dca.deviceDescriptors);
308         if (it->second && deviceChangeAction.deviceDescriptors.size() > 0) {
309             it->second->OnDeviceChange(deviceChangeAction);
310         }
311     }
312     return SUCCESS;
313 }
314 
OnMicrophoneBlocked(const MicrophoneBlockedInfo & blockedInfo)315 int32_t AudioPolicyClientStubImpl::OnMicrophoneBlocked(const MicrophoneBlockedInfo &blockedInfo)
316 {
317     int32_t size = static_cast<int32_t>(blockedInfo.devices.size());
318     CHECK_AND_RETURN_RET_LOG(size < MIC_BLOCKED_VALID_SIZE, ERR_INVALID_PARAM,
319         "get invalid size : %{public}d", size);
320     std::lock_guard<std::mutex> lockCbMap(microphoneBlockedMutex_);
321     MicrophoneBlockedInfo microphoneBlockedInfo;
322     microphoneBlockedInfo.blockStatus = blockedInfo.blockStatus;
323     for (auto it = microphoneBlockedCallbackList_.begin(); it != microphoneBlockedCallbackList_.end(); ++it) {
324         microphoneBlockedInfo.devices = blockedInfo.devices;
325         if (it->second && microphoneBlockedInfo.devices.size() > 0) {
326             it->second->OnMicrophoneBlocked(microphoneBlockedInfo);
327         }
328     }
329     return SUCCESS;
330 }
331 
AddMicrophoneBlockedCallback(const int32_t clientId,const std::shared_ptr<AudioManagerMicrophoneBlockedCallback> & cb)332 int32_t AudioPolicyClientStubImpl::AddMicrophoneBlockedCallback(const int32_t clientId,
333     const std::shared_ptr<AudioManagerMicrophoneBlockedCallback> &cb)
334 {
335     std::lock_guard<std::mutex> lockCbMap(microphoneBlockedMutex_);
336     microphoneBlockedCallbackList_.push_back(std::make_pair(clientId, cb));
337     AUDIO_INFO_LOG("add mic blocked cb clientId:%{public}d", clientId);
338     return SUCCESS;
339 }
340 
RemoveMicrophoneBlockedCallback(const int32_t clientId,const std::shared_ptr<AudioManagerMicrophoneBlockedCallback> & cb)341 int32_t AudioPolicyClientStubImpl::RemoveMicrophoneBlockedCallback(const int32_t clientId,
342     const std::shared_ptr<AudioManagerMicrophoneBlockedCallback> &cb)
343 {
344     std::lock_guard<std::mutex> lockCbMap(microphoneBlockedMutex_);
345     auto iter = microphoneBlockedCallbackList_.begin();
346     while (iter != microphoneBlockedCallbackList_.end()) {
347         if ((iter->first == clientId) && (iter->second == cb || cb == nullptr)) {
348             AUDIO_INFO_LOG("remove mic blocked cb flag:%{public}d", clientId);
349             iter = microphoneBlockedCallbackList_.erase(iter);
350         } else {
351             iter++;
352         }
353     }
354     return SUCCESS;
355 }
356 
GetMicrophoneBlockedCallbackSize() const357 size_t AudioPolicyClientStubImpl::GetMicrophoneBlockedCallbackSize() const
358 {
359     std::lock_guard<std::mutex> lockCbMap(microphoneBlockedMutex_);
360     return microphoneBlockedCallbackList_.size();
361 }
362 
AddAudioSceneChangedCallback(const int32_t clientId,const std::shared_ptr<AudioManagerAudioSceneChangedCallback> & cb)363 int32_t AudioPolicyClientStubImpl::AddAudioSceneChangedCallback(const int32_t clientId,
364     const std::shared_ptr<AudioManagerAudioSceneChangedCallback> &cb)
365 {
366     std::lock_guard<std::mutex> lockCbMap(audioSceneChangedMutex_);
367     audioSceneChangedCallbackList_.push_back(cb);
368     AUDIO_INFO_LOG("add audio scene change clientId:%{public}d", clientId);
369     return SUCCESS;
370 }
371 
RemoveAudioSceneChangedCallback(const std::shared_ptr<AudioManagerAudioSceneChangedCallback> & cb)372 int32_t AudioPolicyClientStubImpl::RemoveAudioSceneChangedCallback(
373     const std::shared_ptr<AudioManagerAudioSceneChangedCallback> &cb)
374 {
375     std::lock_guard<std::mutex> lockCbMap(audioSceneChangedMutex_);
376     auto iter = audioSceneChangedCallbackList_.begin();
377     while (iter != audioSceneChangedCallbackList_.end()) {
378         if (*iter == cb) {
379             iter = audioSceneChangedCallbackList_.erase(iter);
380         } else {
381             iter++;
382         }
383     }
384     return SUCCESS;
385 }
386 
GetAudioSceneChangedCallbackSize() const387 size_t AudioPolicyClientStubImpl::GetAudioSceneChangedCallbackSize() const
388 {
389     std::lock_guard<std::mutex> lockCbMap(audioSceneChangedMutex_);
390     return audioSceneChangedCallbackList_.size();
391 }
392 
OnAudioSceneChange(int32_t audioScene)393 int32_t AudioPolicyClientStubImpl::OnAudioSceneChange(int32_t audioScene)
394 {
395     CHECK_AND_RETURN_RET_LOG(audioScene < AUDIO_SCENE_MAX && audioScene > AUDIO_SCENE_INVALID,
396         ERR_INVALID_PARAM, "get invalid audioScene : %{public}d", audioScene);
397 
398     std::lock_guard<std::mutex> lockCbMap(audioSceneChangedMutex_);
399     for (const auto &callback : audioSceneChangedCallbackList_) {
400         CHECK_AND_CONTINUE(callback != nullptr);
401         callback->OnAudioSceneChange(static_cast<AudioScene>(audioScene));
402     }
403     return SUCCESS;
404 }
405 
RemoveAllActiveVolumeTypeChangeCallback()406 int32_t AudioPolicyClientStubImpl::RemoveAllActiveVolumeTypeChangeCallback()
407 {
408     std::lock_guard<std::mutex> lockCbMap(activeVolumeTypeChangeMutex_);
409     activeVolumeTypeChangeCallbackList_.clear();
410     return SUCCESS;
411 }
412 
RemoveActiveVolumeTypeChangeCallback(const std::shared_ptr<AudioManagerActiveVolumeTypeChangeCallback> & cb)413 int32_t AudioPolicyClientStubImpl::RemoveActiveVolumeTypeChangeCallback(
414     const std::shared_ptr<AudioManagerActiveVolumeTypeChangeCallback> &cb)
415 {
416     std::lock_guard<std::mutex> lockCbMap(activeVolumeTypeChangeMutex_);
417     if (cb == nullptr) {
418         activeVolumeTypeChangeCallbackList_.clear();
419         return SUCCESS;
420     }
421     auto it = find_if(activeVolumeTypeChangeCallbackList_.begin(), activeVolumeTypeChangeCallbackList_.end(),
422         [&cb](const std::weak_ptr<AudioManagerActiveVolumeTypeChangeCallback>& elem) {
423             return elem.lock() == cb;
424         });
425     if (it != activeVolumeTypeChangeCallbackList_.end()) {
426         activeVolumeTypeChangeCallbackList_.erase(it);
427     }
428     return SUCCESS;
429 }
430 
AddActiveVolumeTypeChangeCallback(const std::shared_ptr<AudioManagerActiveVolumeTypeChangeCallback> & cb)431 int32_t AudioPolicyClientStubImpl::AddActiveVolumeTypeChangeCallback(
432     const std::shared_ptr<AudioManagerActiveVolumeTypeChangeCallback> &cb)
433 {
434     std::lock_guard<std::mutex> lockCbMap(activeVolumeTypeChangeMutex_);
435     activeVolumeTypeChangeCallbackList_.push_back(cb);
436     AUDIO_INFO_LOG("Add activeVolumeTypeChangeCallback P : %{private}p", cb.get());
437     return SUCCESS;
438 }
439 
AddSelfAppVolumeChangeCallback(int32_t appUid,const std::shared_ptr<AudioManagerAppVolumeChangeCallback> & cb)440 int32_t AudioPolicyClientStubImpl::AddSelfAppVolumeChangeCallback(int32_t appUid,
441     const std::shared_ptr<AudioManagerAppVolumeChangeCallback> &cb)
442 {
443     std::lock_guard<std::mutex> lockCbMap(selfAppVolumeChangeMutex_);
444     for (auto iter : selfAppVolumeChangeCallback_) {
445         if (iter.first == appUid && iter.second.get() == cb.get()) {
446             selfAppVolumeChangeCallbackNum_[appUid]++;
447             AUDIO_INFO_LOG("selfAppVolumeChangeCallback_ No need pushback");
448             return SUCCESS;
449         }
450     }
451     selfAppVolumeChangeCallbackNum_[appUid]++;
452     selfAppVolumeChangeCallback_.push_back({appUid, cb});
453     AUDIO_INFO_LOG("Add selfAppVolumeChangeCallback appUid : %{public}d ", appUid);
454     return SUCCESS;
455 }
456 
RemoveAllSelfAppVolumeChangeCallback(int32_t appUid)457 int32_t AudioPolicyClientStubImpl::RemoveAllSelfAppVolumeChangeCallback(int32_t appUid)
458 {
459     std::lock_guard<std::mutex> lockCbMap(selfAppVolumeChangeMutex_);
460     if (selfAppVolumeChangeCallbackNum_[appUid] != 0) {
461         selfAppVolumeChangeCallbackNum_[appUid] = 0;
462         auto iter = selfAppVolumeChangeCallback_.begin();
463         while (iter != selfAppVolumeChangeCallback_.end()) {
464             if (iter->first == appUid) {
465                 iter = selfAppVolumeChangeCallback_.erase(iter);
466             } else {
467                 iter++;
468             }
469         }
470     }
471     return SUCCESS;
472 }
473 
RemoveSelfAppVolumeChangeCallback(int32_t appUid,const std::shared_ptr<AudioManagerAppVolumeChangeCallback> & cb)474 int32_t AudioPolicyClientStubImpl::RemoveSelfAppVolumeChangeCallback(int32_t appUid,
475     const std::shared_ptr<AudioManagerAppVolumeChangeCallback> &cb)
476 {
477     std::lock_guard<std::mutex> lockCbMap(selfAppVolumeChangeMutex_);
478     auto iter = selfAppVolumeChangeCallback_.begin();
479     while (iter != selfAppVolumeChangeCallback_.end()) {
480         if (iter->first != appUid || iter->second.get() != cb.get()) {
481             iter++;
482             continue;
483         }
484         selfAppVolumeChangeCallbackNum_[appUid]--;
485         if (selfAppVolumeChangeCallbackNum_[appUid] == 0) {
486             iter = selfAppVolumeChangeCallback_.erase(iter);
487         } else {
488             iter++;
489         }
490     }
491     return SUCCESS;
492 }
493 
RemoveAllAppVolumeChangeForUidCallback()494 int32_t AudioPolicyClientStubImpl::RemoveAllAppVolumeChangeForUidCallback()
495 {
496     std::lock_guard<std::mutex> lockCbMap(appVolumeChangeForUidMutex_);
497     appVolumeChangeForUidCallback_.clear();
498     return SUCCESS;
499 }
500 
RemoveAppVolumeChangeForUidCallback(const std::shared_ptr<AudioManagerAppVolumeChangeCallback> & cb)501 int32_t AudioPolicyClientStubImpl::RemoveAppVolumeChangeForUidCallback(
502     const std::shared_ptr<AudioManagerAppVolumeChangeCallback> &cb)
503 {
504     std::lock_guard<std::mutex> lockCbMap(appVolumeChangeForUidMutex_);
505     auto iter = appVolumeChangeForUidCallback_.begin();
506     while (iter != appVolumeChangeForUidCallback_.end()) {
507         if (iter->second.get() != cb.get()) {
508             iter++;
509             continue;
510         }
511         iter = appVolumeChangeForUidCallback_.erase(iter);
512     }
513     return SUCCESS;
514 }
515 
AddAppVolumeChangeForUidCallback(const int32_t appUid,const std::shared_ptr<AudioManagerAppVolumeChangeCallback> & cb)516 int32_t AudioPolicyClientStubImpl::AddAppVolumeChangeForUidCallback(const int32_t appUid,
517     const std::shared_ptr<AudioManagerAppVolumeChangeCallback> &cb)
518 {
519     std::lock_guard<std::mutex> lockCbMap(appVolumeChangeForUidMutex_);
520     for (auto iter : appVolumeChangeForUidCallback_) {
521         if (iter.first == appUid && iter.second.get() == cb.get()) {
522             appVolumeChangeForUidCallbackNum[appUid]++;
523             AUDIO_INFO_LOG("appVolumeChangeForUidCallback_ No need pushback");
524             return SUCCESS;
525         }
526     }
527     appVolumeChangeForUidCallbackNum[appUid]++;
528     appVolumeChangeForUidCallback_.push_back({appUid, cb});
529     return SUCCESS;
530 }
531 
AddRingerModeCallback(const std::shared_ptr<AudioRingerModeCallback> & cb)532 int32_t AudioPolicyClientStubImpl::AddRingerModeCallback(const std::shared_ptr<AudioRingerModeCallback> &cb)
533 {
534     std::lock_guard<std::mutex> lockCbMap(ringerModeMutex_);
535     ringerModeCallbackList_.push_back(cb);
536     return SUCCESS;
537 }
538 
RemoveRingerModeCallback()539 int32_t AudioPolicyClientStubImpl::RemoveRingerModeCallback()
540 {
541     std::lock_guard<std::mutex> lockCbMap(ringerModeMutex_);
542     ringerModeCallbackList_.clear();
543     return SUCCESS;
544 }
545 
RemoveRingerModeCallback(const std::shared_ptr<AudioRingerModeCallback> & cb)546 int32_t AudioPolicyClientStubImpl::RemoveRingerModeCallback(const std::shared_ptr<AudioRingerModeCallback> &cb)
547 {
548     std::lock_guard<std::mutex> lockCbMap(ringerModeMutex_);
549     auto iter = ringerModeCallbackList_.begin();
550     while (iter != ringerModeCallbackList_.end()) {
551         if (*iter == cb) {
552             iter = ringerModeCallbackList_.erase(iter);
553         } else {
554             iter++;
555         }
556     }
557     return SUCCESS;
558 }
559 
GetActiveVolumeTypeChangeCallbackSize() const560 size_t AudioPolicyClientStubImpl::GetActiveVolumeTypeChangeCallbackSize() const
561 {
562     std::lock_guard<std::mutex> lockCbMap(activeVolumeTypeChangeMutex_);
563     return activeVolumeTypeChangeCallbackList_.size();
564 }
565 
GetRingerModeCallbackSize() const566 size_t AudioPolicyClientStubImpl::GetRingerModeCallbackSize() const
567 {
568     std::lock_guard<std::mutex> lockCbMap(ringerModeMutex_);
569     return ringerModeCallbackList_.size();
570 }
571 
GetAppVolumeChangeCallbackForUidSize() const572 size_t AudioPolicyClientStubImpl::GetAppVolumeChangeCallbackForUidSize() const
573 {
574     std::lock_guard<std::mutex> lockCbMap(appVolumeChangeForUidMutex_);
575     return appVolumeChangeForUidCallback_.size();
576 }
577 
GetSelfAppVolumeChangeCallbackSize() const578 size_t AudioPolicyClientStubImpl::GetSelfAppVolumeChangeCallbackSize() const
579 {
580     std::lock_guard<std::mutex> lockCbMap(selfAppVolumeChangeMutex_);
581     return selfAppVolumeChangeCallback_.size();
582 }
583 
OnRingerModeUpdated(int32_t ringerMode)584 int32_t AudioPolicyClientStubImpl::OnRingerModeUpdated(int32_t ringerMode)
585 {
586     std::lock_guard<std::mutex> lockCbMap(ringerModeMutex_);
587     for (auto it = ringerModeCallbackList_.begin(); it != ringerModeCallbackList_.end(); ++it) {
588         (*it)->OnRingerModeUpdated(static_cast<AudioRingerMode>(ringerMode));
589     }
590     return SUCCESS;
591 }
592 
OnActiveVolumeTypeChanged(int32_t volumeType)593 int32_t AudioPolicyClientStubImpl::OnActiveVolumeTypeChanged(int32_t volumeType)
594 {
595     std::lock_guard<std::mutex> lockCbMap(activeVolumeTypeChangeMutex_);
596     for (auto it = activeVolumeTypeChangeCallbackList_.begin(); it != activeVolumeTypeChangeCallbackList_.end(); ++it) {
597         std::shared_ptr<AudioManagerActiveVolumeTypeChangeCallback> activeVolumeTypeCallback = (*it).lock();
598         if (activeVolumeTypeCallback != nullptr) {
599             activeVolumeTypeCallback->OnActiveVolumeTypeChanged(static_cast<AudioVolumeType>(volumeType));
600         }
601     }
602     return SUCCESS;
603 }
604 
OnAppVolumeChanged(int32_t appUid,const VolumeEvent & volumeEvent)605 int32_t AudioPolicyClientStubImpl::OnAppVolumeChanged(int32_t appUid, const VolumeEvent& volumeEvent)
606 {
607     {
608         std::lock_guard<std::mutex> lockCbMap(appVolumeChangeForUidMutex_);
609         for (auto iter : appVolumeChangeForUidCallback_) {
610             if (iter.first != appUid) {
611                 continue;
612             }
613             iter.second->OnAppVolumeChangedForUid(appUid, volumeEvent);
614         }
615     }
616     {
617         std::lock_guard<std::mutex> lockCbMap(selfAppVolumeChangeMutex_);
618         for (auto iter : selfAppVolumeChangeCallback_) {
619             if (iter.first != appUid) {
620                 continue;
621             }
622             iter.second->OnSelfAppVolumeChanged(volumeEvent);
623         }
624     }
625     return SUCCESS;
626 }
627 
AddAudioSessionCallback(const std::shared_ptr<AudioSessionCallback> & cb)628 int32_t AudioPolicyClientStubImpl::AddAudioSessionCallback(const std::shared_ptr<AudioSessionCallback> &cb)
629 {
630     AUDIO_INFO_LOG("AddAudioSessionCallback in");
631     std::lock_guard<std::mutex> lockCbMap(audioSessionMutex_);
632     audioSessionCallbackList_.push_back(cb);
633     return SUCCESS;
634 }
635 
RemoveAudioSessionCallback()636 int32_t AudioPolicyClientStubImpl::RemoveAudioSessionCallback()
637 {
638     AUDIO_INFO_LOG("RemoveAudioSessionCallback all in");
639     std::lock_guard<std::mutex> lockCbMap(audioSessionMutex_);
640     audioSessionCallbackList_.clear();
641     return SUCCESS;
642 }
643 
RemoveAudioSessionCallback(const std::shared_ptr<AudioSessionCallback> & cb)644 int32_t AudioPolicyClientStubImpl::RemoveAudioSessionCallback(const std::shared_ptr<AudioSessionCallback> &cb)
645 {
646     AUDIO_INFO_LOG("RemoveAudioSessionCallback one in");
647     std::lock_guard<std::mutex> lockCbMap(audioSessionMutex_);
648     auto iter = audioSessionCallbackList_.begin();
649     while (iter != audioSessionCallbackList_.end()) {
650         if (*iter == cb) {
651             iter = audioSessionCallbackList_.erase(iter);
652         } else {
653             iter++;
654         }
655     }
656     return SUCCESS;
657 }
658 
GetAudioSessionCallbackSize() const659 size_t AudioPolicyClientStubImpl::GetAudioSessionCallbackSize() const
660 {
661     std::lock_guard<std::mutex> lockCbMap(audioSessionMutex_);
662     return audioSessionCallbackList_.size();
663 }
664 
OnAudioSessionDeactive(int32_t deactiveEvent)665 int32_t AudioPolicyClientStubImpl::OnAudioSessionDeactive(int32_t deactiveEvent)
666 {
667     AUDIO_INFO_LOG("OnAudioSessionDeactive in");
668     std::lock_guard<std::mutex> lockCbMap(audioSessionMutex_);
669     AudioSessionDeactiveEvent newDeactiveEvent;
670     newDeactiveEvent.deactiveReason = static_cast<AudioSessionDeactiveReason>(deactiveEvent);
671     for (auto it = audioSessionCallbackList_.begin(); it != audioSessionCallbackList_.end(); ++it) {
672         (*it)->OnAudioSessionDeactive(newDeactiveEvent);
673     }
674     return SUCCESS;
675 }
676 
AddAudioSessionStateCallback(const std::shared_ptr<AudioSessionStateChangedCallback> & cb)677 int32_t AudioPolicyClientStubImpl::AddAudioSessionStateCallback(
678     const std::shared_ptr<AudioSessionStateChangedCallback> &cb)
679 {
680     AUDIO_INFO_LOG("AddAudioSessionStateCallback in");
681     std::lock_guard<std::mutex> lockCbMap(audioSessionStateMutex_);
682     audioSessionStateCallbackList_.push_back(cb);
683     return SUCCESS;
684 }
685 
RemoveAudioSessionStateCallback()686 int32_t AudioPolicyClientStubImpl::RemoveAudioSessionStateCallback()
687 {
688     AUDIO_INFO_LOG("RemoveAudioSessionStateCallback all in");
689     std::lock_guard<std::mutex> lockCbMap(audioSessionStateMutex_);
690     audioSessionStateCallbackList_.clear();
691     return SUCCESS;
692 }
693 
RemoveAudioSessionStateCallback(const std::shared_ptr<AudioSessionStateChangedCallback> & cb)694 int32_t AudioPolicyClientStubImpl::RemoveAudioSessionStateCallback(
695     const std::shared_ptr<AudioSessionStateChangedCallback> &cb)
696 {
697     AUDIO_INFO_LOG("RemoveAudioSessionStateCallback one in");
698     std::lock_guard<std::mutex> lockCbMap(audioSessionStateMutex_);
699     auto it = find_if(audioSessionStateCallbackList_.begin(), audioSessionStateCallbackList_.end(),
700         [&cb](const std::weak_ptr<AudioSessionStateChangedCallback>& elem) {
701             return elem.lock() == cb;
702         });
703     if (it != audioSessionStateCallbackList_.end()) {
704         audioSessionStateCallbackList_.erase(it);
705         AUDIO_INFO_LOG("RemoveAudioSessionStateCallback remove cb succeed");
706     }
707     return SUCCESS;
708 }
709 
GetAudioSessionStateCallbackSize() const710 size_t AudioPolicyClientStubImpl::GetAudioSessionStateCallbackSize() const
711 {
712     std::lock_guard<std::mutex> lockCbMap(audioSessionStateMutex_);
713     return audioSessionStateCallbackList_.size();
714 }
715 
OnAudioSessionStateChanged(int32_t stateChangeHint)716 int32_t AudioPolicyClientStubImpl::OnAudioSessionStateChanged(int32_t stateChangeHint)
717 {
718     AudioSessionStateChangedEvent stateChangedEvent;
719     stateChangedEvent.stateChangeHint = static_cast<AudioSessionStateChangeHint>(stateChangeHint);
720     AUDIO_INFO_LOG("OnAudioSessionStateChanged in");
721     std::lock_guard<std::mutex> lockCbMap(audioSessionStateMutex_);
722     for (auto it = audioSessionStateCallbackList_.begin(); it != audioSessionStateCallbackList_.end(); ++it) {
723         std::shared_ptr<AudioSessionStateChangedCallback> audioSessionStateChangedCallback = (*it).lock();
724         if (audioSessionStateChangedCallback != nullptr) {
725             audioSessionStateChangedCallback->OnAudioSessionStateChanged(stateChangedEvent);
726         }
727     }
728     return SUCCESS;
729 }
730 
AddAudioSessionDeviceCallback(const std::shared_ptr<AudioSessionCurrentDeviceChangedCallback> & cb)731 int32_t AudioPolicyClientStubImpl::AddAudioSessionDeviceCallback(
732     const std::shared_ptr<AudioSessionCurrentDeviceChangedCallback> &cb)
733 {
734     AUDIO_INFO_LOG("AddAudioSessionDeviceCallback in");
735     std::lock_guard<std::mutex> lockCbMap(audioSessionDeviceMutex_);
736     audioSessionDeviceCallbackList_.push_back(cb);
737     return SUCCESS;
738 }
739 
RemoveAudioSessionDeviceCallback()740 int32_t AudioPolicyClientStubImpl::RemoveAudioSessionDeviceCallback()
741 {
742     AUDIO_INFO_LOG("RemoveAudioSessionDeviceCallback all in");
743     std::lock_guard<std::mutex> lockCbMap(audioSessionDeviceMutex_);
744     audioSessionDeviceCallbackList_.clear();
745     return SUCCESS;
746 }
747 
RemoveAudioSessionDeviceCallback(const std::shared_ptr<AudioSessionCurrentDeviceChangedCallback> & cb)748 int32_t AudioPolicyClientStubImpl::RemoveAudioSessionDeviceCallback(
749     const std::shared_ptr<AudioSessionCurrentDeviceChangedCallback> &cb)
750 {
751     AUDIO_INFO_LOG("RemoveAudioSessionDeviceCallback one in");
752     std::lock_guard<std::mutex> lockCbMap(audioSessionDeviceMutex_);
753     auto it = find_if(audioSessionDeviceCallbackList_.begin(), audioSessionDeviceCallbackList_.end(),
754         [&cb](const std::weak_ptr<AudioSessionCurrentDeviceChangedCallback>& elem) {
755             return elem.lock() == cb;
756         });
757     if (it != audioSessionDeviceCallbackList_.end()) {
758         audioSessionDeviceCallbackList_.erase(it);
759         AUDIO_INFO_LOG("RemoveAudioSessionDeviceCallback remove cb succeed");
760     }
761     return SUCCESS;
762 }
763 
GetAudioSessionDeviceCallbackSize() const764 size_t AudioPolicyClientStubImpl::GetAudioSessionDeviceCallbackSize() const
765 {
766     std::lock_guard<std::mutex> lockCbMap(audioSessionDeviceMutex_);
767     return audioSessionDeviceCallbackList_.size();
768 }
769 
OnAudioSessionCurrentDeviceChanged(const CurrentOutputDeviceChangedEvent & deviceChangedEvent)770 int32_t AudioPolicyClientStubImpl::OnAudioSessionCurrentDeviceChanged(
771     const CurrentOutputDeviceChangedEvent &deviceChangedEvent)
772 {
773     AUDIO_INFO_LOG("OnAudioSessionCurrentDeviceChanged in");
774     std::lock_guard<std::mutex> lockCbMap(audioSessionDeviceMutex_);
775     for (auto it = audioSessionDeviceCallbackList_.begin(); it != audioSessionDeviceCallbackList_.end(); ++it) {
776         std::shared_ptr<AudioSessionCurrentDeviceChangedCallback> deviceChangedCallback = (*it).lock();
777         if (deviceChangedCallback != nullptr) {
778             deviceChangedCallback->OnAudioSessionCurrentDeviceChanged(deviceChangedEvent);
779         }
780     }
781 
782     return SUCCESS;
783 }
784 
AddMicStateChangeCallback(const std::shared_ptr<AudioManagerMicStateChangeCallback> & cb)785 int32_t AudioPolicyClientStubImpl::AddMicStateChangeCallback(
786     const std::shared_ptr<AudioManagerMicStateChangeCallback> &cb)
787 {
788     std::lock_guard<std::mutex> lockCbMap(micStateChangeMutex_);
789     micStateChangeCallbackList_.push_back(cb);
790     return SUCCESS;
791 }
RemoveMicStateChangeCallback()792 int32_t AudioPolicyClientStubImpl::RemoveMicStateChangeCallback()
793 {
794     std::lock_guard<std::mutex> lockCbMap(micStateChangeMutex_);
795     micStateChangeCallbackList_.clear();
796     return SUCCESS;
797 }
798 
GetMicStateChangeCallbackSize() const799 size_t AudioPolicyClientStubImpl::GetMicStateChangeCallbackSize() const
800 {
801     std::lock_guard<std::mutex> lockCbMap(micStateChangeMutex_);
802     return micStateChangeCallbackList_.size();
803 }
804 
HasMicStateChangeCallback()805 bool AudioPolicyClientStubImpl::HasMicStateChangeCallback()
806 {
807     std::lock_guard<std::mutex> lockCbMap(micStateChangeMutex_);
808     if (micStateChangeCallbackList_.empty()) {
809         AUDIO_INFO_LOG("MicStateChangeCallback list is empty.");
810         return false;
811     }
812     return true;
813 }
814 
OnMicStateUpdated(const MicStateChangeEvent & micStateChangeEvent)815 int32_t AudioPolicyClientStubImpl::OnMicStateUpdated(const MicStateChangeEvent &micStateChangeEvent)
816 {
817     std::lock_guard<std::mutex> lockCbMap(micStateChangeMutex_);
818     for (auto it = micStateChangeCallbackList_.begin(); it != micStateChangeCallbackList_.end(); ++it) {
819         (*it)->OnMicStateUpdated(micStateChangeEvent);
820     }
821     return SUCCESS;
822 }
823 
AddPreferredOutputDeviceChangeCallback(const AudioRendererInfo & rendererInfo,const std::shared_ptr<AudioPreferredOutputDeviceChangeCallback> & cb)824 int32_t AudioPolicyClientStubImpl::AddPreferredOutputDeviceChangeCallback(const AudioRendererInfo &rendererInfo,
825     const std::shared_ptr<AudioPreferredOutputDeviceChangeCallback> &cb)
826 {
827     std::lock_guard<std::mutex> lockCbMap(pOutputDeviceChangeMutex_);
828     preferredOutputDeviceCallbackMap_[rendererInfo.streamUsage].push_back(cb);
829     return SUCCESS;
830 }
831 
RemovePreferredOutputDeviceChangeCallback(const std::shared_ptr<AudioPreferredOutputDeviceChangeCallback> & cb)832 int32_t AudioPolicyClientStubImpl::RemovePreferredOutputDeviceChangeCallback(
833     const std::shared_ptr<AudioPreferredOutputDeviceChangeCallback> &cb)
834 {
835     std::lock_guard<std::mutex> lockCbMap(pOutputDeviceChangeMutex_);
836     if (cb == nullptr) {
837         preferredOutputDeviceCallbackMap_.clear();
838         return SUCCESS;
839     }
840     for (auto &it : preferredOutputDeviceCallbackMap_) {
841         auto iter = find(it.second.begin(), it.second.end(), cb);
842         if (iter != it.second.end()) {
843             it.second.erase(iter);
844         }
845     }
846     return SUCCESS;
847 }
848 
GetPreferredOutputDeviceChangeCallbackSize() const849 size_t AudioPolicyClientStubImpl::GetPreferredOutputDeviceChangeCallbackSize() const
850 {
851     std::lock_guard<std::mutex> lockCbMap(pOutputDeviceChangeMutex_);
852     return preferredOutputDeviceCallbackMap_.size();
853 }
854 
OnPreferredOutputDeviceUpdated(const AudioRendererInfo & rendererInfo,const std::vector<std::shared_ptr<AudioDeviceDescriptor>> & desc)855 int32_t AudioPolicyClientStubImpl::OnPreferredOutputDeviceUpdated(const AudioRendererInfo &rendererInfo,
856     const std::vector<std::shared_ptr<AudioDeviceDescriptor>> &desc)
857 {
858     int32_t size = static_cast<int32_t>(desc.size());
859     CHECK_AND_RETURN_RET_LOG(size < PREFERRED_DEVICE_VALID_SIZE, ERR_INVALID_PARAM,
860         "get invalid size : %{public}d", size);
861     std::lock_guard<std::mutex> lockCbMap(pOutputDeviceChangeMutex_);
862     auto it = preferredOutputDeviceCallbackMap_.find(rendererInfo.streamUsage);
863     CHECK_AND_RETURN_RET_LOG(it != preferredOutputDeviceCallbackMap_.end(),
864         ERR_CALLBACK_NOT_REGISTERED, "streamUsage not found");
865     for (auto iter = it->second.begin(); iter != it->second.end(); ++iter) {
866         CHECK_AND_CONTINUE_LOG(iter != it->second.end() && (*iter) != nullptr, "iter is null");
867     (*iter)->OnPreferredOutputDeviceUpdated(desc);
868     }
869     return SUCCESS;
870 }
871 
AddPreferredInputDeviceChangeCallback(const AudioCapturerInfo & capturerInfo,const std::shared_ptr<AudioPreferredInputDeviceChangeCallback> & cb)872 int32_t AudioPolicyClientStubImpl::AddPreferredInputDeviceChangeCallback(const AudioCapturerInfo &capturerInfo,
873     const std::shared_ptr<AudioPreferredInputDeviceChangeCallback> &cb)
874 {
875     std::lock_guard<std::mutex> lockCbMap(pInputDeviceChangeMutex_);
876     preferredInputDeviceCallbackMap_[capturerInfo.sourceType].push_back(cb);
877     return SUCCESS;
878 }
879 
RemovePreferredInputDeviceChangeCallback(const std::shared_ptr<AudioPreferredInputDeviceChangeCallback> & cb)880 int32_t AudioPolicyClientStubImpl::RemovePreferredInputDeviceChangeCallback(
881     const std::shared_ptr<AudioPreferredInputDeviceChangeCallback> &cb)
882 {
883     std::lock_guard<std::mutex> lockCbMap(pInputDeviceChangeMutex_);
884     if (cb == nullptr) {
885         preferredInputDeviceCallbackMap_.clear();
886         return SUCCESS;
887     }
888     for (auto &it : preferredInputDeviceCallbackMap_) {
889         auto iter = find(it.second.begin(), it.second.end(), cb);
890         if (iter != it.second.end()) {
891             it.second.erase(iter);
892         }
893     }
894     return SUCCESS;
895 }
896 
GetPreferredInputDeviceChangeCallbackSize() const897 size_t AudioPolicyClientStubImpl::GetPreferredInputDeviceChangeCallbackSize() const
898 {
899     std::lock_guard<std::mutex> lockCbMap(pInputDeviceChangeMutex_);
900     return preferredInputDeviceCallbackMap_.size();
901 }
902 
OnPreferredInputDeviceUpdated(const AudioCapturerInfo & capturerInfo,const std::vector<std::shared_ptr<AudioDeviceDescriptor>> & desc)903 int32_t AudioPolicyClientStubImpl::OnPreferredInputDeviceUpdated(const AudioCapturerInfo &capturerInfo,
904     const std::vector<std::shared_ptr<AudioDeviceDescriptor>> &desc)
905 {
906     int32_t size = static_cast<int32_t>(desc.size());
907     CHECK_AND_RETURN_RET_LOG(size < PREFERRED_DEVICE_VALID_SIZE, ERR_INVALID_PARAM,
908         "get invalid size : %{public}d", size);
909     std::lock_guard<std::mutex> lockCbMap(pInputDeviceChangeMutex_);
910     auto it = preferredInputDeviceCallbackMap_.find(capturerInfo.sourceType);
911     CHECK_AND_RETURN_RET_LOG(it != preferredInputDeviceCallbackMap_.end(),
912         ERR_CALLBACK_NOT_REGISTERED, "sourceType not found");
913     for (auto iter = it->second.begin(); iter != it->second.end(); ++iter) {
914         CHECK_AND_CONTINUE_LOG(iter != it->second.end() && (*iter) != nullptr, "iter is null");
915         (*iter)->OnPreferredInputDeviceUpdated(desc);
916     }
917     return SUCCESS;
918 }
919 
AddRendererStateChangeCallback(const std::shared_ptr<AudioRendererStateChangeCallback> & cb)920 int32_t AudioPolicyClientStubImpl::AddRendererStateChangeCallback(
921     const std::shared_ptr<AudioRendererStateChangeCallback> &cb)
922 {
923     CHECK_AND_RETURN_RET_LOG(cb, ERR_INVALID_PARAM, "cb is null");
924 
925     std::lock_guard<std::mutex> lockCbMap(rendererStateChangeMutex_);
926     rendererStateChangeCallbackList_.push_back(cb);
927     return SUCCESS;
928 }
929 
RemoveRendererStateChangeCallback(const std::vector<std::shared_ptr<AudioRendererStateChangeCallback>> & callbacks)930 int32_t AudioPolicyClientStubImpl::RemoveRendererStateChangeCallback(
931     const std::vector<std::shared_ptr<AudioRendererStateChangeCallback>> &callbacks)
932 {
933     std::lock_guard<std::mutex> lockCbMap(rendererStateChangeMutex_);
934     for (const auto &cb : callbacks) {
935         rendererStateChangeCallbackList_.erase(
936             std::remove(rendererStateChangeCallbackList_.begin(),
937             rendererStateChangeCallbackList_.end(), cb), rendererStateChangeCallbackList_.end());
938     }
939 
940     return SUCCESS;
941 }
942 
RemoveRendererStateChangeCallback(const std::shared_ptr<AudioRendererStateChangeCallback> & callback)943 int32_t AudioPolicyClientStubImpl::RemoveRendererStateChangeCallback(
944     const std::shared_ptr<AudioRendererStateChangeCallback> &callback)
945 {
946     std::lock_guard<std::mutex> lockCbMap(rendererStateChangeMutex_);
947     rendererStateChangeCallbackList_.erase(
948         std::remove(rendererStateChangeCallbackList_.begin(),
949         rendererStateChangeCallbackList_.end(), callback), rendererStateChangeCallbackList_.end());
950 
951     return SUCCESS;
952 }
953 
GetRendererStateChangeCallbackSize() const954 size_t AudioPolicyClientStubImpl::GetRendererStateChangeCallbackSize() const
955 {
956     std::lock_guard<std::mutex> lockCbMap(rendererStateChangeMutex_);
957     return rendererStateChangeCallbackList_.size();
958 }
959 
AddDeviceChangeWithInfoCallback(const uint32_t sessionId,const std::weak_ptr<DeviceChangeWithInfoCallback> & cb)960 int32_t AudioPolicyClientStubImpl::AddDeviceChangeWithInfoCallback(
961     const uint32_t sessionId, const std::weak_ptr<DeviceChangeWithInfoCallback> &cb)
962 {
963     std::lock_guard<std::mutex> lockCbMap(deviceChangeWithInfoCallbackMutex_);
964     deviceChangeWithInfoCallbackMap_[sessionId] = cb;
965     return SUCCESS;
966 }
967 
RemoveDeviceChangeWithInfoCallback(const uint32_t sessionId)968 int32_t AudioPolicyClientStubImpl::RemoveDeviceChangeWithInfoCallback(const uint32_t sessionId)
969 {
970     std::lock_guard<std::mutex> lockCbMap(deviceChangeWithInfoCallbackMutex_);
971     deviceChangeWithInfoCallbackMap_.erase(sessionId);
972     return SUCCESS;
973 }
974 
GetDeviceChangeWithInfoCallbackkSize() const975 size_t AudioPolicyClientStubImpl::GetDeviceChangeWithInfoCallbackkSize() const
976 {
977     std::lock_guard<std::mutex> lockCbMap(deviceChangeWithInfoCallbackMutex_);
978     return deviceChangeWithInfoCallbackMap_.size();
979 }
980 
OnRendererDeviceChange(uint32_t sessionId,const AudioDeviceDescriptor & deviceInfo,const AudioStreamDeviceChangeReasonExt & reason)981 int32_t AudioPolicyClientStubImpl::OnRendererDeviceChange(uint32_t sessionId,
982     const AudioDeviceDescriptor &deviceInfo, const AudioStreamDeviceChangeReasonExt &reason)
983 {
984     Trace trace("AudioPolicyClientStubImpl::OnRendererDeviceChange");
985     std::shared_ptr<DeviceChangeWithInfoCallback> callback = nullptr;
986     {
987         std::lock_guard<std::mutex> lockCbMap(deviceChangeWithInfoCallbackMutex_);
988         if (deviceChangeWithInfoCallbackMap_.count(sessionId) == 0) {
989             return ERR_OPERATION_FAILED;
990         }
991         callback = deviceChangeWithInfoCallbackMap_.at(sessionId).lock();
992         if (callback == nullptr) {
993             deviceChangeWithInfoCallbackMap_.erase(sessionId);
994             return ERR_CALLBACK_NOT_REGISTERED;
995         }
996     }
997     if (callback != nullptr) {
998         Trace traceCallback("callback->OnDeviceChangeWithInfo sessionid:" + std::to_string(sessionId)
999             + " reason:" + std::to_string(static_cast<int>(reason)));
1000         callback->OnDeviceChangeWithInfo(sessionId, deviceInfo, reason);
1001     }
1002     return SUCCESS;
1003 }
1004 
OnRendererStateChange(const std::vector<std::shared_ptr<AudioRendererChangeInfo>> & audioRendererChangeInfos)1005 int32_t AudioPolicyClientStubImpl::OnRendererStateChange(
1006     const std::vector<std::shared_ptr<AudioRendererChangeInfo>> &audioRendererChangeInfos)
1007 {
1008     int32_t size = static_cast<int32_t>(audioRendererChangeInfos.size());
1009     CHECK_AND_RETURN_RET_LOG(size < STATE_VALID_SIZE, ERR_INVALID_PARAM,
1010         "get invalid size : %{public}d", size);
1011     std::vector<std::shared_ptr<AudioRendererStateChangeCallback>> callbacks;
1012     {
1013         std::lock_guard<std::mutex> lockCbMap(rendererStateChangeMutex_);
1014         callbacks = rendererStateChangeCallbackList_;
1015     }
1016     size_t cBSize = callbacks.size();
1017     size_t infosSize = audioRendererChangeInfos.size();
1018     AUDIO_DEBUG_LOG("cbSize: %{public}zu infoSize: %{public}zu", cBSize, infosSize);
1019 
1020     if (getuid() == RSS_UID) {
1021         AUDIO_INFO_LOG("cbSize: %{public}zu infoSize: %{public}zu", cBSize, infosSize);
1022     }
1023 
1024     Trace trace("AudioPolicyClientStubImpl::OnRendererStateChange");
1025     for (auto &cb : callbacks) {
1026         Trace traceCallback("OnRendererStateChange");
1027         cb->OnRendererStateChange(audioRendererChangeInfos);
1028     }
1029     return SUCCESS;
1030 }
1031 
OnRecreateRendererStreamEvent(uint32_t sessionId,int32_t streamFlag,const AudioStreamDeviceChangeReasonExt & reason)1032 int32_t AudioPolicyClientStubImpl::OnRecreateRendererStreamEvent(uint32_t sessionId, int32_t streamFlag,
1033     const AudioStreamDeviceChangeReasonExt &reason)
1034 {
1035     AUDIO_INFO_LOG("Enter");
1036     std::shared_ptr<DeviceChangeWithInfoCallback> callback = nullptr;
1037     {
1038         std::lock_guard<std::mutex> lockCbMap(deviceChangeWithInfoCallbackMutex_);
1039         if (deviceChangeWithInfoCallbackMap_.count(sessionId) == 0) {
1040             AUDIO_ERR_LOG("No session id %{public}d", sessionId);
1041             return ERR_OPERATION_FAILED;
1042         }
1043         callback = deviceChangeWithInfoCallbackMap_.at(sessionId).lock();
1044     }
1045     if (callback != nullptr) {
1046         callback->OnRecreateStreamEvent(sessionId, streamFlag, reason);
1047     }
1048     return SUCCESS;
1049 }
1050 
OnRecreateCapturerStreamEvent(uint32_t sessionId,int32_t streamFlag,const AudioStreamDeviceChangeReasonExt & reason)1051 int32_t AudioPolicyClientStubImpl::OnRecreateCapturerStreamEvent(uint32_t sessionId, int32_t streamFlag,
1052     const AudioStreamDeviceChangeReasonExt &reason)
1053 {
1054     AUDIO_INFO_LOG("Enter");
1055     std::shared_ptr<DeviceChangeWithInfoCallback> callback = nullptr;
1056     {
1057         std::lock_guard<std::mutex> lockCbMap(deviceChangeWithInfoCallbackMutex_);
1058         if (deviceChangeWithInfoCallbackMap_.count(sessionId) == 0) {
1059             AUDIO_ERR_LOG("No session id %{public}d", sessionId);
1060             return ERR_OPERATION_FAILED;
1061         }
1062         callback = deviceChangeWithInfoCallbackMap_.at(sessionId).lock();
1063     }
1064     if (callback != nullptr) {
1065         callback->OnRecreateStreamEvent(sessionId, streamFlag, reason);
1066     }
1067     return SUCCESS;
1068 }
1069 
AddCapturerStateChangeCallback(const std::shared_ptr<AudioCapturerStateChangeCallback> & cb)1070 int32_t AudioPolicyClientStubImpl::AddCapturerStateChangeCallback(
1071     const std::shared_ptr<AudioCapturerStateChangeCallback> &cb)
1072 {
1073     std::lock_guard<std::mutex> lockCbMap(capturerStateChangeMutex_);
1074     capturerStateChangeCallbackList_.push_back(cb);
1075     return SUCCESS;
1076 }
1077 
RemoveCapturerStateChangeCallback()1078 int32_t AudioPolicyClientStubImpl::RemoveCapturerStateChangeCallback()
1079 {
1080     std::lock_guard<std::mutex> lockCbMap(capturerStateChangeMutex_);
1081     capturerStateChangeCallbackList_.clear();
1082     return SUCCESS;
1083 }
1084 
GetCapturerStateChangeCallbackSize() const1085 size_t AudioPolicyClientStubImpl::GetCapturerStateChangeCallbackSize() const
1086 {
1087     std::lock_guard<std::mutex> lockCbMap(capturerStateChangeMutex_);
1088     return capturerStateChangeCallbackList_.size();
1089 }
1090 
OnCapturerStateChange(const std::vector<std::shared_ptr<AudioCapturerChangeInfo>> & audioCapturerChangeInfos)1091 int32_t AudioPolicyClientStubImpl::OnCapturerStateChange(
1092     const std::vector<std::shared_ptr<AudioCapturerChangeInfo>> &audioCapturerChangeInfos)
1093 {
1094     int32_t size = static_cast<int32_t>(audioCapturerChangeInfos.size());
1095     CHECK_AND_RETURN_RET_LOG(size < STATE_VALID_SIZE, ERR_INVALID_PARAM,
1096         "get invalid size : %{public}d", size);
1097     std::vector<std::shared_ptr<AudioCapturerStateChangeCallback>> tmpCallbackList;
1098     {
1099         std::lock_guard<std::mutex> lockCbMap(capturerStateChangeMutex_);
1100         for (auto it = capturerStateChangeCallbackList_.begin(); it != capturerStateChangeCallbackList_.end(); ++it) {
1101             std::shared_ptr<AudioCapturerStateChangeCallback> capturerStateChangeCallback = (*it).lock();
1102             if (capturerStateChangeCallback != nullptr) {
1103                 tmpCallbackList.emplace_back(capturerStateChangeCallback);
1104             }
1105         }
1106     }
1107     for (auto it = tmpCallbackList.begin(); it != tmpCallbackList.end(); ++it) {
1108         if (*it == nullptr) {
1109             AUDIO_WARNING_LOG("tmpCallbackList is nullptr");
1110             continue;
1111         }
1112         (*it)->OnCapturerStateChange(audioCapturerChangeInfos);
1113     }
1114     return SUCCESS;
1115 }
1116 
AddHeadTrackingDataRequestedChangeCallback(const std::string & macAddress,const std::shared_ptr<HeadTrackingDataRequestedChangeCallback> & cb)1117 int32_t AudioPolicyClientStubImpl::AddHeadTrackingDataRequestedChangeCallback(const std::string &macAddress,
1118     const std::shared_ptr<HeadTrackingDataRequestedChangeCallback> &cb)
1119 {
1120     std::lock_guard<std::mutex> lockCbMap(headTrackingDataRequestedChangeMutex_);
1121     if (!headTrackingDataRequestedChangeCallbackMap_.count(macAddress)) {
1122         AUDIO_INFO_LOG("First registeration for the specified device");
1123         headTrackingDataRequestedChangeCallbackMap_.insert(std::make_pair(macAddress, cb));
1124     } else {
1125         AUDIO_INFO_LOG("Repeated registeration for the specified device, replaced by the new one");
1126         headTrackingDataRequestedChangeCallbackMap_[macAddress] = cb;
1127     }
1128     return SUCCESS;
1129 }
1130 
RemoveHeadTrackingDataRequestedChangeCallback(const std::string & macAddress)1131 int32_t AudioPolicyClientStubImpl::RemoveHeadTrackingDataRequestedChangeCallback(const std::string &macAddress)
1132 {
1133     std::lock_guard<std::mutex> lockCbMap(headTrackingDataRequestedChangeMutex_);
1134     headTrackingDataRequestedChangeCallbackMap_.erase(macAddress);
1135     return SUCCESS;
1136 }
1137 
GetHeadTrackingDataRequestedChangeCallbackSize() const1138 size_t AudioPolicyClientStubImpl::GetHeadTrackingDataRequestedChangeCallbackSize() const
1139 {
1140     std::lock_guard<std::mutex> lockCbMap(headTrackingDataRequestedChangeMutex_);
1141     return headTrackingDataRequestedChangeCallbackMap_.size();
1142 }
1143 
OnHeadTrackingDeviceChange(const std::unordered_map<std::string,bool> & changeInfo)1144 int32_t AudioPolicyClientStubImpl::OnHeadTrackingDeviceChange(const std::unordered_map<std::string, bool> &changeInfo)
1145 {
1146     int32_t size = static_cast<int32_t>(changeInfo.size());
1147     CHECK_AND_RETURN_RET_LOG(size < DEVICE_CHANGE_VALID_SIZE, ERR_INVALID_PARAM,
1148         "get invalid size : %{public}d", size);
1149     std::lock_guard<std::mutex> lockCbMap(headTrackingDataRequestedChangeMutex_);
1150     if (headTrackingDataRequestedChangeCallbackMap_.size() == 0) {
1151         return ERR_INVALID_PARAM;
1152     }
1153     for (const auto &pair : changeInfo) {
1154         if (!headTrackingDataRequestedChangeCallbackMap_.count(pair.first)) {
1155             AUDIO_WARNING_LOG("the specified device has not been registered");
1156             continue;
1157         }
1158         std::shared_ptr<HeadTrackingDataRequestedChangeCallback> headTrackingDataRequestedChangeCallback =
1159             headTrackingDataRequestedChangeCallbackMap_[pair.first];
1160         if (headTrackingDataRequestedChangeCallback != nullptr) {
1161             AUDIO_DEBUG_LOG("head tracking data requested change event of the specified device has been notified");
1162             headTrackingDataRequestedChangeCallback->OnHeadTrackingDataRequestedChange(pair.second);
1163         }
1164     }
1165     return SUCCESS;
1166 }
1167 
AddSpatializationEnabledChangeCallback(const std::shared_ptr<AudioSpatializationEnabledChangeCallback> & cb)1168 int32_t AudioPolicyClientStubImpl::AddSpatializationEnabledChangeCallback(
1169     const std::shared_ptr<AudioSpatializationEnabledChangeCallback> &cb)
1170 {
1171     std::lock_guard<std::mutex> lockCbMap(spatializationEnabledChangeMutex_);
1172     spatializationEnabledChangeCallbackList_.push_back(cb);
1173     return SUCCESS;
1174 }
1175 
RemoveSpatializationEnabledChangeCallback()1176 int32_t AudioPolicyClientStubImpl::RemoveSpatializationEnabledChangeCallback()
1177 {
1178     std::lock_guard<std::mutex> lockCbMap(spatializationEnabledChangeMutex_);
1179     spatializationEnabledChangeCallbackList_.clear();
1180     return SUCCESS;
1181 }
1182 
GetSpatializationEnabledChangeCallbackSize() const1183 size_t AudioPolicyClientStubImpl::GetSpatializationEnabledChangeCallbackSize() const
1184 {
1185     std::lock_guard<std::mutex> lockCbMap(spatializationEnabledChangeMutex_);
1186     return spatializationEnabledChangeCallbackList_.size();
1187 }
1188 
OnSpatializationEnabledChange(bool enabled)1189 int32_t AudioPolicyClientStubImpl::OnSpatializationEnabledChange(bool enabled)
1190 {
1191     std::lock_guard<std::mutex> lockCbMap(spatializationEnabledChangeMutex_);
1192     for (const auto &callback : spatializationEnabledChangeCallbackList_) {
1193         callback->OnSpatializationEnabledChange(enabled);
1194     }
1195     return SUCCESS;
1196 }
1197 
OnSpatializationEnabledChangeForAnyDevice(const std::shared_ptr<AudioDeviceDescriptor> & deviceDescriptor,bool enabled)1198 int32_t AudioPolicyClientStubImpl::OnSpatializationEnabledChangeForAnyDevice(
1199     const std::shared_ptr<AudioDeviceDescriptor> &deviceDescriptor, bool enabled)
1200 {
1201     std::lock_guard<std::mutex> lockCbMap(spatializationEnabledChangeMutex_);
1202     for (const auto &callback : spatializationEnabledChangeCallbackList_) {
1203         callback->OnSpatializationEnabledChangeForAnyDevice(deviceDescriptor, enabled);
1204     }
1205     return SUCCESS;
1206 }
1207 
AddSpatializationEnabledChangeForCurrentDeviceCallback(const std::shared_ptr<AudioSpatializationEnabledChangeForCurrentDeviceCallback> & cb)1208 int32_t AudioPolicyClientStubImpl::AddSpatializationEnabledChangeForCurrentDeviceCallback(
1209     const std::shared_ptr<AudioSpatializationEnabledChangeForCurrentDeviceCallback> &cb)
1210 {
1211     std::lock_guard<std::mutex> lockCbMap(spatializationEnabledChangeForCurrentDeviceMutex_);
1212     spatializationEnabledChangeForCurrentDeviceCallbackList_.push_back(cb);
1213     return SUCCESS;
1214 }
1215 
RemoveSpatializationEnabledChangeForCurrentDeviceCallback()1216 int32_t AudioPolicyClientStubImpl::RemoveSpatializationEnabledChangeForCurrentDeviceCallback()
1217 {
1218     std::lock_guard<std::mutex> lockCbMap(spatializationEnabledChangeForCurrentDeviceMutex_);
1219     spatializationEnabledChangeForCurrentDeviceCallbackList_.clear();
1220     return SUCCESS;
1221 }
1222 
GetSpatializationEnabledChangeForCurrentDeviceCallbackSize() const1223 size_t AudioPolicyClientStubImpl::GetSpatializationEnabledChangeForCurrentDeviceCallbackSize() const
1224 {
1225     std::lock_guard<std::mutex> lockCbMap(spatializationEnabledChangeForCurrentDeviceMutex_);
1226     return spatializationEnabledChangeForCurrentDeviceCallbackList_.size();
1227 }
1228 
OnSpatializationEnabledChangeForCurrentDevice(bool enabled)1229 int32_t AudioPolicyClientStubImpl::OnSpatializationEnabledChangeForCurrentDevice(bool enabled)
1230 {
1231     std::lock_guard<std::mutex> lockCbMap(spatializationEnabledChangeForCurrentDeviceMutex_);
1232     for (const auto &callback : spatializationEnabledChangeForCurrentDeviceCallbackList_) {
1233         callback->OnSpatializationEnabledChangeForCurrentDevice(enabled);
1234     }
1235     return SUCCESS;
1236 }
1237 
AddHeadTrackingEnabledChangeCallback(const std::shared_ptr<AudioHeadTrackingEnabledChangeCallback> & cb)1238 int32_t AudioPolicyClientStubImpl::AddHeadTrackingEnabledChangeCallback(
1239     const std::shared_ptr<AudioHeadTrackingEnabledChangeCallback> &cb)
1240 {
1241     std::lock_guard<std::mutex> lockCbMap(headTrackingEnabledChangeMutex_);
1242     headTrackingEnabledChangeCallbackList_.push_back(cb);
1243     return SUCCESS;
1244 }
1245 
RemoveHeadTrackingEnabledChangeCallback()1246 int32_t AudioPolicyClientStubImpl::RemoveHeadTrackingEnabledChangeCallback()
1247 {
1248     std::lock_guard<std::mutex> lockCbMap(headTrackingEnabledChangeMutex_);
1249     headTrackingEnabledChangeCallbackList_.clear();
1250     return SUCCESS;
1251 }
1252 
GetHeadTrackingEnabledChangeCallbacSize() const1253 size_t AudioPolicyClientStubImpl::GetHeadTrackingEnabledChangeCallbacSize() const
1254 {
1255     std::lock_guard<std::mutex> lockCbMap(headTrackingEnabledChangeMutex_);
1256     return headTrackingEnabledChangeCallbackList_.size();
1257 }
1258 
OnHeadTrackingEnabledChange(bool enabled)1259 int32_t AudioPolicyClientStubImpl::OnHeadTrackingEnabledChange(bool enabled)
1260 {
1261     std::lock_guard<std::mutex> lockCbMap(headTrackingEnabledChangeMutex_);
1262     for (const auto &callback : headTrackingEnabledChangeCallbackList_) {
1263         callback->OnHeadTrackingEnabledChange(enabled);
1264     }
1265     return SUCCESS;
1266 }
1267 
OnHeadTrackingEnabledChangeForAnyDevice(const std::shared_ptr<AudioDeviceDescriptor> & deviceDescriptor,bool enabled)1268 int32_t AudioPolicyClientStubImpl::OnHeadTrackingEnabledChangeForAnyDevice(
1269     const std::shared_ptr<AudioDeviceDescriptor> &deviceDescriptor, bool enabled)
1270 {
1271     std::lock_guard<std::mutex> lockCbMap(headTrackingEnabledChangeMutex_);
1272     for (const auto &callback : headTrackingEnabledChangeCallbackList_) {
1273         callback->OnHeadTrackingEnabledChangeForAnyDevice(deviceDescriptor, enabled);
1274     }
1275     return SUCCESS;
1276 }
1277 
AddNnStateChangeCallback(const std::shared_ptr<AudioNnStateChangeCallback> & cb)1278 int32_t AudioPolicyClientStubImpl::AddNnStateChangeCallback(const std::shared_ptr<AudioNnStateChangeCallback> &cb)
1279 {
1280     std::lock_guard<std::mutex> lockCbMap(nnStateChangeMutex_);
1281     nnStateChangeCallbackList_.push_back(cb);
1282     return SUCCESS;
1283 }
1284 
RemoveNnStateChangeCallback()1285 int32_t AudioPolicyClientStubImpl::RemoveNnStateChangeCallback()
1286 {
1287     std::lock_guard<std::mutex> lockCbMap(nnStateChangeMutex_);
1288     nnStateChangeCallbackList_.clear();
1289     return SUCCESS;
1290 }
1291 
GetNnStateChangeCallbackSize() const1292 size_t AudioPolicyClientStubImpl::GetNnStateChangeCallbackSize() const
1293 {
1294     std::lock_guard<std::mutex> lockCbMap(nnStateChangeMutex_);
1295     return nnStateChangeCallbackList_.size();
1296 }
1297 
OnNnStateChange(int32_t nnState)1298 int32_t AudioPolicyClientStubImpl::OnNnStateChange(int32_t nnState)
1299 {
1300     std::lock_guard<std::mutex> lockCbMap(nnStateChangeMutex_);
1301     for (const auto &callback : nnStateChangeCallbackList_) {
1302         callback->OnNnStateChange(nnState);
1303     }
1304     return SUCCESS;
1305 }
1306 
AddAudioFormatUnsupportedErrorCallback(const std::shared_ptr<AudioFormatUnsupportedErrorCallback> & cb)1307 int32_t AudioPolicyClientStubImpl::AddAudioFormatUnsupportedErrorCallback(
1308     const std::shared_ptr<AudioFormatUnsupportedErrorCallback> &cb)
1309 {
1310     std::lock_guard<std::mutex> lockCbMap(formatUnsupportedErrorMutex_);
1311     AudioFormatUnsupportedErrorCallbackList_.push_back(cb);
1312     return SUCCESS;
1313 }
1314 
RemoveAudioFormatUnsupportedErrorCallback()1315 int32_t AudioPolicyClientStubImpl::RemoveAudioFormatUnsupportedErrorCallback()
1316 {
1317     std::lock_guard<std::mutex> lockCbMap(formatUnsupportedErrorMutex_);
1318     AudioFormatUnsupportedErrorCallbackList_.clear();
1319     return SUCCESS;
1320 }
1321 
GetAudioFormatUnsupportedErrorCallbackSize() const1322 size_t AudioPolicyClientStubImpl::GetAudioFormatUnsupportedErrorCallbackSize() const
1323 {
1324     std::lock_guard<std::mutex> lockCbMap(formatUnsupportedErrorMutex_);
1325     return AudioFormatUnsupportedErrorCallbackList_.size();
1326 }
1327 
OnFormatUnsupportedError(int32_t errorCode)1328 int32_t AudioPolicyClientStubImpl::OnFormatUnsupportedError(int32_t errorCode)
1329 {
1330     std::lock_guard<std::mutex> lockCbMap(formatUnsupportedErrorMutex_);
1331     for (const auto &callback : AudioFormatUnsupportedErrorCallbackList_) {
1332         CHECK_AND_CONTINUE(callback != nullptr);
1333         callback->OnFormatUnsupportedError(static_cast<AudioErrors>(errorCode));
1334     }
1335     return SUCCESS;
1336 }
1337 
GetStreamVolumeChangeCallbackSize() const1338 size_t AudioPolicyClientStubImpl::GetStreamVolumeChangeCallbackSize() const
1339 {
1340     std::lock_guard<std::mutex> lockCbMap(streamVolumeChangeMutex_);
1341     return streamVolumeChangeCallbackList_.size();
1342 }
1343 
GetStreamVolumeChangeCallbackStreamUsages() const1344 std::set<StreamUsage> AudioPolicyClientStubImpl::GetStreamVolumeChangeCallbackStreamUsages() const
1345 {
1346     std::lock_guard<std::mutex> lockCbMap(streamVolumeChangeMutex_);
1347     std::set<StreamUsage> allStreamUsages;
1348     for (auto &[streamUsages, cb] : streamVolumeChangeCallbackList_) {
1349         (void)cb;
1350         allStreamUsages.insert(streamUsages.begin(), streamUsages.end());
1351     }
1352     return allStreamUsages;
1353 }
1354 
AddStreamVolumeChangeCallback(const std::set<StreamUsage> & streamUsages,const std::shared_ptr<StreamVolumeChangeCallback> & cb)1355 int32_t AudioPolicyClientStubImpl::AddStreamVolumeChangeCallback(const std::set<StreamUsage> &streamUsages,
1356     const std::shared_ptr<StreamVolumeChangeCallback> &cb)
1357 {
1358     std::lock_guard<std::mutex> lockCbMap(streamVolumeChangeMutex_);
1359     streamVolumeChangeCallbackList_.emplace_back(streamUsages, cb);
1360     return SUCCESS;
1361 }
1362 
RemoveStreamVolumeChangeCallback(const std::shared_ptr<StreamVolumeChangeCallback> & cb)1363 int32_t AudioPolicyClientStubImpl::RemoveStreamVolumeChangeCallback(
1364     const std::shared_ptr<StreamVolumeChangeCallback> &cb)
1365 {
1366     std::lock_guard<std::mutex> lockCbMap(streamVolumeChangeMutex_);
1367     if (cb == nullptr) {
1368         streamVolumeChangeCallbackList_.clear();
1369         return SUCCESS;
1370     }
1371     auto it = find_if(streamVolumeChangeCallbackList_.begin(), streamVolumeChangeCallbackList_.end(),
1372         [&cb](const std::pair<std::set<StreamUsage>, std::weak_ptr<StreamVolumeChangeCallback>> &elem) {
1373             return elem.second.lock() == cb;
1374         });
1375     if (it != streamVolumeChangeCallbackList_.end()) {
1376         streamVolumeChangeCallbackList_.erase(it);
1377     }
1378     return SUCCESS;
1379 }
1380 
OnStreamVolumeChange(const StreamVolumeEvent & streamVolumeEvent)1381 int32_t AudioPolicyClientStubImpl::OnStreamVolumeChange(const StreamVolumeEvent &streamVolumeEvent)
1382 {
1383     std::lock_guard<std::mutex> lockCbMap(volumeKeyEventMutex_);
1384     for (auto &[streamUsages, cb] : streamVolumeChangeCallbackList_) {
1385         std::shared_ptr<StreamVolumeChangeCallback> streamVolumeChangeCallback = cb.lock();
1386         if (streamVolumeChangeCallback != nullptr && streamUsages.count(streamVolumeEvent.streamUsage)) {
1387             streamVolumeChangeCallback->OnStreamVolumeChange(streamVolumeEvent);
1388         }
1389     }
1390     return SUCCESS;
1391 }
1392 } // namespace AudioStandard
1393 } // namespace OHOS