1 /*
2 * Copyright (c) 2023-2023 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 {
AddVolumeKeyEventCallback(const std::shared_ptr<VolumeKeyEventCallback> & cb)26 int32_t AudioPolicyClientStubImpl::AddVolumeKeyEventCallback(const std::shared_ptr<VolumeKeyEventCallback> &cb)
27 {
28 std::lock_guard<std::mutex> lockCbMap(volumeKeyEventMutex_);
29 volumeKeyEventCallbackList_.push_back(cb);
30 return SUCCESS;
31 }
32
RemoveVolumeKeyEventCallback(const std::shared_ptr<VolumeKeyEventCallback> & cb)33 int32_t AudioPolicyClientStubImpl::RemoveVolumeKeyEventCallback(const std::shared_ptr<VolumeKeyEventCallback> &cb)
34 {
35 std::lock_guard<std::mutex> lockCbMap(volumeKeyEventMutex_);
36 if (cb == nullptr) {
37 volumeKeyEventCallbackList_.clear();
38 return SUCCESS;
39 }
40 auto it = find_if(volumeKeyEventCallbackList_.begin(), volumeKeyEventCallbackList_.end(),
41 [&cb](const std::weak_ptr<VolumeKeyEventCallback>& elem) {
42 return elem.lock() == cb;
43 });
44 if (it != volumeKeyEventCallbackList_.end()) {
45 volumeKeyEventCallbackList_.erase(it);
46 }
47 return SUCCESS;
48 }
49
GetVolumeKeyEventCallbackSize() const50 size_t AudioPolicyClientStubImpl::GetVolumeKeyEventCallbackSize() const
51 {
52 std::lock_guard<std::mutex> lockCbMap(volumeKeyEventMutex_);
53 return volumeKeyEventCallbackList_.size();
54 }
55
OnVolumeKeyEvent(VolumeEvent volumeEvent)56 void AudioPolicyClientStubImpl::OnVolumeKeyEvent(VolumeEvent volumeEvent)
57 {
58 std::lock_guard<std::mutex> lockCbMap(volumeKeyEventMutex_);
59 for (auto it = volumeKeyEventCallbackList_.begin(); it != volumeKeyEventCallbackList_.end(); ++it) {
60 std::shared_ptr<VolumeKeyEventCallback> volumeKeyEventCallback = (*it).lock();
61 if (volumeKeyEventCallback != nullptr) {
62 volumeKeyEventCallback->OnVolumeKeyEvent(volumeEvent);
63 }
64 }
65 }
66
AddFocusInfoChangeCallback(const std::shared_ptr<AudioFocusInfoChangeCallback> & cb)67 int32_t AudioPolicyClientStubImpl::AddFocusInfoChangeCallback(const std::shared_ptr<AudioFocusInfoChangeCallback> &cb)
68 {
69 std::lock_guard<std::mutex> lockCbMap(focusInfoChangeMutex_);
70 focusInfoChangeCallbackList_.push_back(cb);
71 return SUCCESS;
72 }
73
RemoveFocusInfoChangeCallback()74 int32_t AudioPolicyClientStubImpl::RemoveFocusInfoChangeCallback()
75 {
76 std::lock_guard<std::mutex> lockCbMap(focusInfoChangeMutex_);
77 focusInfoChangeCallbackList_.clear();
78 return SUCCESS;
79 }
80
OnAudioFocusInfoChange(const std::list<std::pair<AudioInterrupt,AudioFocuState>> & focusInfoList)81 void AudioPolicyClientStubImpl::OnAudioFocusInfoChange(
82 const std::list<std::pair<AudioInterrupt, AudioFocuState>> &focusInfoList)
83 {
84 std::lock_guard<std::mutex> lockCbMap(focusInfoChangeMutex_);
85 for (auto it = focusInfoChangeCallbackList_.begin(); it != focusInfoChangeCallbackList_.end(); ++it) {
86 (*it)->OnAudioFocusInfoChange(focusInfoList);
87 }
88 }
89
OnAudioFocusRequested(const AudioInterrupt & requestFocus)90 void AudioPolicyClientStubImpl::OnAudioFocusRequested(const AudioInterrupt &requestFocus)
91 {
92 std::lock_guard<std::mutex> lockCbMap(focusInfoChangeMutex_);
93 for (auto it = focusInfoChangeCallbackList_.begin(); it != focusInfoChangeCallbackList_.end(); ++it) {
94 (*it)->OnAudioFocusRequested(requestFocus);
95 }
96 }
97
OnAudioFocusAbandoned(const AudioInterrupt & abandonFocus)98 void AudioPolicyClientStubImpl::OnAudioFocusAbandoned(const AudioInterrupt &abandonFocus)
99 {
100 std::lock_guard<std::mutex> lockCbMap(focusInfoChangeMutex_);
101 for (auto it = focusInfoChangeCallbackList_.begin(); it != focusInfoChangeCallbackList_.end(); ++it) {
102 (*it)->OnAudioFocusAbandoned(abandonFocus);
103 }
104 }
105
GetFocusInfoChangeCallbackSize() const106 size_t AudioPolicyClientStubImpl::GetFocusInfoChangeCallbackSize() const
107 {
108 std::lock_guard<std::mutex> lockCbMap(focusInfoChangeMutex_);
109 return focusInfoChangeCallbackList_.size();
110 }
111
DeviceFilterByFlag(DeviceFlag flag,const std::vector<sptr<AudioDeviceDescriptor>> & desc)112 std::vector<sptr<AudioDeviceDescriptor>> AudioPolicyClientStubImpl::DeviceFilterByFlag(DeviceFlag flag,
113 const std::vector<sptr<AudioDeviceDescriptor>>& desc)
114 {
115 std::vector<sptr<AudioDeviceDescriptor>> descRet;
116 DeviceRole role = DEVICE_ROLE_NONE;
117 switch (flag) {
118 case DeviceFlag::ALL_DEVICES_FLAG:
119 for (sptr<AudioDeviceDescriptor> var : desc) {
120 if (var->networkId_ == LOCAL_NETWORK_ID) {
121 descRet.insert(descRet.end(), var);
122 }
123 }
124 break;
125 case DeviceFlag::ALL_DISTRIBUTED_DEVICES_FLAG:
126 for (sptr<AudioDeviceDescriptor> var : desc) {
127 if (var->networkId_ != LOCAL_NETWORK_ID) {
128 descRet.insert(descRet.end(), var);
129 }
130 }
131 break;
132 case DeviceFlag::ALL_L_D_DEVICES_FLAG:
133 descRet = desc;
134 break;
135 case DeviceFlag::OUTPUT_DEVICES_FLAG:
136 case DeviceFlag::INPUT_DEVICES_FLAG:
137 role = flag == INPUT_DEVICES_FLAG ? INPUT_DEVICE : OUTPUT_DEVICE;
138 for (sptr<AudioDeviceDescriptor> var : desc) {
139 if (var->networkId_ == LOCAL_NETWORK_ID && var->deviceRole_ == role) {
140 descRet.insert(descRet.end(), var);
141 }
142 }
143 break;
144 case DeviceFlag::DISTRIBUTED_OUTPUT_DEVICES_FLAG:
145 case DeviceFlag::DISTRIBUTED_INPUT_DEVICES_FLAG:
146 role = flag == DISTRIBUTED_INPUT_DEVICES_FLAG ? INPUT_DEVICE : OUTPUT_DEVICE;
147 for (sptr<AudioDeviceDescriptor> var : desc) {
148 if (var->networkId_ != LOCAL_NETWORK_ID && var->deviceRole_ == role) {
149 descRet.insert(descRet.end(), var);
150 }
151 }
152 break;
153 default:
154 break;
155 }
156 return descRet;
157 }
158
AddDeviceChangeCallback(const DeviceFlag & flag,const std::shared_ptr<AudioManagerDeviceChangeCallback> & cb)159 int32_t AudioPolicyClientStubImpl::AddDeviceChangeCallback(const DeviceFlag &flag,
160 const std::shared_ptr<AudioManagerDeviceChangeCallback> &cb)
161 {
162 std::lock_guard<std::mutex> lockCbMap(deviceChangeMutex_);
163 deviceChangeCallbackList_.push_back(std::make_pair(flag, cb));
164 return SUCCESS;
165 }
166
RemoveDeviceChangeCallback(DeviceFlag flag,std::shared_ptr<AudioManagerDeviceChangeCallback> & cb)167 int32_t AudioPolicyClientStubImpl::RemoveDeviceChangeCallback(DeviceFlag flag,
168 std::shared_ptr<AudioManagerDeviceChangeCallback> &cb)
169 {
170 std::lock_guard<std::mutex> lockCbMap(deviceChangeMutex_);
171 auto iter = deviceChangeCallbackList_.begin();
172 while (iter != deviceChangeCallbackList_.end()) {
173 if ((iter->first & flag) && (iter->second == cb || cb == nullptr)) {
174 AUDIO_INFO_LOG("remove device change cb flag:%{public}d", flag);
175 iter = deviceChangeCallbackList_.erase(iter);
176 } else {
177 iter++;
178 }
179 }
180 return SUCCESS;
181 }
182
GetDeviceChangeCallbackSize() const183 size_t AudioPolicyClientStubImpl::GetDeviceChangeCallbackSize() const
184 {
185 std::lock_guard<std::mutex> lockCbMap(deviceChangeMutex_);
186 return deviceChangeCallbackList_.size();
187 }
188
OnDeviceChange(const DeviceChangeAction & dca)189 void AudioPolicyClientStubImpl::OnDeviceChange(const DeviceChangeAction &dca)
190 {
191 std::lock_guard<std::mutex> lockCbMap(deviceChangeMutex_);
192 DeviceChangeAction deviceChangeAction;
193 deviceChangeAction.type = dca.type;
194 for (auto it = deviceChangeCallbackList_.begin(); it != deviceChangeCallbackList_.end(); ++it) {
195 deviceChangeAction.flag = it->first;
196 deviceChangeAction.deviceDescriptors = DeviceFilterByFlag(it->first, dca.deviceDescriptors);
197 if (it->second && deviceChangeAction.deviceDescriptors.size() > 0) {
198 it->second->OnDeviceChange(deviceChangeAction);
199 }
200 }
201 }
202
OnMicrophoneBlocked(const MicrophoneBlockedInfo & blockedInfo)203 void AudioPolicyClientStubImpl::OnMicrophoneBlocked(const MicrophoneBlockedInfo &blockedInfo)
204 {
205 std::lock_guard<std::mutex> lockCbMap(microphoneBlockedMutex_);
206 MicrophoneBlockedInfo microphoneBlockedInfo;
207 microphoneBlockedInfo.blockStatus = blockedInfo.blockStatus;
208 for (auto it = microphoneBlockedCallbackList_.begin(); it != microphoneBlockedCallbackList_.end(); ++it) {
209 microphoneBlockedInfo.devices= blockedInfo.devices;
210 if (it->second && microphoneBlockedInfo.devices.size() > 0) {
211 it->second->OnMicrophoneBlocked(microphoneBlockedInfo);
212 }
213 }
214 }
215
AddMicrophoneBlockedCallback(const int32_t clientId,const std::shared_ptr<AudioManagerMicrophoneBlockedCallback> & cb)216 int32_t AudioPolicyClientStubImpl::AddMicrophoneBlockedCallback(const int32_t clientId,
217 const std::shared_ptr<AudioManagerMicrophoneBlockedCallback> &cb)
218 {
219 std::lock_guard<std::mutex> lockCbMap(microphoneBlockedMutex_);
220 microphoneBlockedCallbackList_.push_back(std::make_pair(clientId, cb));
221 AUDIO_INFO_LOG("add mic blocked cb clientId:%{public}d", clientId);
222 return SUCCESS;
223 }
224
RemoveMicrophoneBlockedCallback(const int32_t clientId,const std::shared_ptr<AudioManagerMicrophoneBlockedCallback> & cb)225 int32_t AudioPolicyClientStubImpl::RemoveMicrophoneBlockedCallback(const int32_t clientId,
226 const std::shared_ptr<AudioManagerMicrophoneBlockedCallback> &cb)
227 {
228 std::lock_guard<std::mutex> lockCbMap(microphoneBlockedMutex_);
229 auto iter = microphoneBlockedCallbackList_.begin();
230 while (iter != microphoneBlockedCallbackList_.end()) {
231 if ((iter->first == clientId) && (iter->second == cb || cb == nullptr)) {
232 AUDIO_INFO_LOG("remove mic blocked cb flag:%{public}d", clientId);
233 iter = microphoneBlockedCallbackList_.erase(iter);
234 } else {
235 iter++;
236 }
237 }
238 return SUCCESS;
239 }
240
GetMicrophoneBlockedCallbackSize() const241 size_t AudioPolicyClientStubImpl::GetMicrophoneBlockedCallbackSize() const
242 {
243 std::lock_guard<std::mutex> lockCbMap(microphoneBlockedMutex_);
244 return microphoneBlockedCallbackList_.size();
245 }
246
AddRingerModeCallback(const std::shared_ptr<AudioRingerModeCallback> & cb)247 int32_t AudioPolicyClientStubImpl::AddRingerModeCallback(const std::shared_ptr<AudioRingerModeCallback> &cb)
248 {
249 std::lock_guard<std::mutex> lockCbMap(ringerModeMutex_);
250 ringerModeCallbackList_.push_back(cb);
251 return SUCCESS;
252 }
253
RemoveRingerModeCallback()254 int32_t AudioPolicyClientStubImpl::RemoveRingerModeCallback()
255 {
256 std::lock_guard<std::mutex> lockCbMap(ringerModeMutex_);
257 ringerModeCallbackList_.clear();
258 return SUCCESS;
259 }
260
RemoveRingerModeCallback(const std::shared_ptr<AudioRingerModeCallback> & cb)261 int32_t AudioPolicyClientStubImpl::RemoveRingerModeCallback(const std::shared_ptr<AudioRingerModeCallback> &cb)
262 {
263 std::lock_guard<std::mutex> lockCbMap(ringerModeMutex_);
264 auto iter = ringerModeCallbackList_.begin();
265 while (iter != ringerModeCallbackList_.end()) {
266 if (*iter == cb) {
267 iter = ringerModeCallbackList_.erase(iter);
268 } else {
269 iter++;
270 }
271 }
272 return SUCCESS;
273 }
274
GetRingerModeCallbackSize() const275 size_t AudioPolicyClientStubImpl::GetRingerModeCallbackSize() const
276 {
277 std::lock_guard<std::mutex> lockCbMap(ringerModeMutex_);
278 return ringerModeCallbackList_.size();
279 }
280
OnRingerModeUpdated(const AudioRingerMode & ringerMode)281 void AudioPolicyClientStubImpl::OnRingerModeUpdated(const AudioRingerMode &ringerMode)
282 {
283 std::lock_guard<std::mutex> lockCbMap(ringerModeMutex_);
284 for (auto it = ringerModeCallbackList_.begin(); it != ringerModeCallbackList_.end(); ++it) {
285 (*it)->OnRingerModeUpdated(ringerMode);
286 }
287 }
288
AddAudioSessionCallback(const std::shared_ptr<AudioSessionCallback> & cb)289 int32_t AudioPolicyClientStubImpl::AddAudioSessionCallback(const std::shared_ptr<AudioSessionCallback> &cb)
290 {
291 AUDIO_INFO_LOG("AddAudioSessionCallback in");
292 std::lock_guard<std::mutex> lockCbMap(audioSessionMutex_);
293 audioSessionCallbackList_.push_back(cb);
294 return SUCCESS;
295 }
296
RemoveAudioSessionCallback()297 int32_t AudioPolicyClientStubImpl::RemoveAudioSessionCallback()
298 {
299 AUDIO_INFO_LOG("RemoveAudioSessionCallback all in");
300 std::lock_guard<std::mutex> lockCbMap(audioSessionMutex_);
301 audioSessionCallbackList_.clear();
302 return SUCCESS;
303 }
304
RemoveAudioSessionCallback(const std::shared_ptr<AudioSessionCallback> & cb)305 int32_t AudioPolicyClientStubImpl::RemoveAudioSessionCallback(const std::shared_ptr<AudioSessionCallback> &cb)
306 {
307 AUDIO_INFO_LOG("RemoveAudioSessionCallback one in");
308 std::lock_guard<std::mutex> lockCbMap(audioSessionMutex_);
309 auto iter = audioSessionCallbackList_.begin();
310 while (iter != audioSessionCallbackList_.end()) {
311 if (*iter == cb) {
312 iter = audioSessionCallbackList_.erase(iter);
313 } else {
314 iter++;
315 }
316 }
317 return SUCCESS;
318 }
319
GetAudioSessionCallbackSize() const320 size_t AudioPolicyClientStubImpl::GetAudioSessionCallbackSize() const
321 {
322 std::lock_guard<std::mutex> lockCbMap(audioSessionMutex_);
323 return audioSessionCallbackList_.size();
324 }
325
OnAudioSessionDeactive(const AudioSessionDeactiveEvent & deactiveEvent)326 void AudioPolicyClientStubImpl::OnAudioSessionDeactive(const AudioSessionDeactiveEvent &deactiveEvent)
327 {
328 AUDIO_INFO_LOG("OnAudioSessionDeactive in");
329 std::lock_guard<std::mutex> lockCbMap(audioSessionMutex_);
330 for (auto it = audioSessionCallbackList_.begin(); it != audioSessionCallbackList_.end(); ++it) {
331 (*it)->OnAudioSessionDeactive(deactiveEvent);
332 }
333 }
334
AddMicStateChangeCallback(const std::shared_ptr<AudioManagerMicStateChangeCallback> & cb)335 int32_t AudioPolicyClientStubImpl::AddMicStateChangeCallback(
336 const std::shared_ptr<AudioManagerMicStateChangeCallback> &cb)
337 {
338 std::lock_guard<std::mutex> lockCbMap(micStateChangeMutex_);
339 micStateChangeCallbackList_.push_back(cb);
340 return SUCCESS;
341 }
RemoveMicStateChangeCallback()342 int32_t AudioPolicyClientStubImpl::RemoveMicStateChangeCallback()
343 {
344 std::lock_guard<std::mutex> lockCbMap(micStateChangeMutex_);
345 micStateChangeCallbackList_.clear();
346 return SUCCESS;
347 }
348
GetMicStateChangeCallbackSize() const349 size_t AudioPolicyClientStubImpl::GetMicStateChangeCallbackSize() const
350 {
351 std::lock_guard<std::mutex> lockCbMap(micStateChangeMutex_);
352 return micStateChangeCallbackList_.size();
353 }
354
HasMicStateChangeCallback()355 bool AudioPolicyClientStubImpl::HasMicStateChangeCallback()
356 {
357 std::lock_guard<std::mutex> lockCbMap(micStateChangeMutex_);
358 if (micStateChangeCallbackList_.empty()) {
359 AUDIO_INFO_LOG("MicStateChangeCallback list is empty.");
360 return false;
361 }
362 return true;
363 }
364
OnMicStateUpdated(const MicStateChangeEvent & micStateChangeEvent)365 void AudioPolicyClientStubImpl::OnMicStateUpdated(const MicStateChangeEvent &micStateChangeEvent)
366 {
367 std::lock_guard<std::mutex> lockCbMap(micStateChangeMutex_);
368 for (auto it = micStateChangeCallbackList_.begin(); it != micStateChangeCallbackList_.end(); ++it) {
369 (*it)->OnMicStateUpdated(micStateChangeEvent);
370 }
371 }
372
AddPreferredOutputDeviceChangeCallback(const std::shared_ptr<AudioPreferredOutputDeviceChangeCallback> & cb)373 int32_t AudioPolicyClientStubImpl::AddPreferredOutputDeviceChangeCallback(
374 const std::shared_ptr<AudioPreferredOutputDeviceChangeCallback> &cb)
375 {
376 std::lock_guard<std::mutex> lockCbMap(pOutputDeviceChangeMutex_);
377 preferredOutputDeviceCallbackList_.push_back(cb);
378 return SUCCESS;
379 }
380
RemovePreferredOutputDeviceChangeCallback()381 int32_t AudioPolicyClientStubImpl::RemovePreferredOutputDeviceChangeCallback()
382 {
383 std::lock_guard<std::mutex> lockCbMap(pOutputDeviceChangeMutex_);
384 preferredOutputDeviceCallbackList_.clear();
385 return SUCCESS;
386 }
387
GetPreferredOutputDeviceChangeCallbackSize() const388 size_t AudioPolicyClientStubImpl::GetPreferredOutputDeviceChangeCallbackSize() const
389 {
390 std::lock_guard<std::mutex> lockCbMap(pOutputDeviceChangeMutex_);
391 return preferredOutputDeviceCallbackList_.size();
392 }
393
OnPreferredOutputDeviceUpdated(const std::vector<sptr<AudioDeviceDescriptor>> & desc)394 void AudioPolicyClientStubImpl::OnPreferredOutputDeviceUpdated(const std::vector<sptr<AudioDeviceDescriptor>> &desc)
395 {
396 std::lock_guard<std::mutex> lockCbMap(pOutputDeviceChangeMutex_);
397 for (auto it = preferredOutputDeviceCallbackList_.begin(); it != preferredOutputDeviceCallbackList_.end(); ++it) {
398 (*it)->OnPreferredOutputDeviceUpdated(desc);
399 }
400 }
401
AddPreferredInputDeviceChangeCallback(const std::shared_ptr<AudioPreferredInputDeviceChangeCallback> & cb)402 int32_t AudioPolicyClientStubImpl::AddPreferredInputDeviceChangeCallback(
403 const std::shared_ptr<AudioPreferredInputDeviceChangeCallback> &cb)
404 {
405 std::lock_guard<std::mutex> lockCbMap(pInputDeviceChangeMutex_);
406 preferredInputDeviceCallbackList_.push_back(cb);
407 return SUCCESS;
408 }
409
RemovePreferredInputDeviceChangeCallback()410 int32_t AudioPolicyClientStubImpl::RemovePreferredInputDeviceChangeCallback()
411 {
412 std::lock_guard<std::mutex> lockCbMap(pInputDeviceChangeMutex_);
413 preferredInputDeviceCallbackList_.clear();
414 return SUCCESS;
415 }
416
GetPreferredInputDeviceChangeCallbackSize() const417 size_t AudioPolicyClientStubImpl::GetPreferredInputDeviceChangeCallbackSize() const
418 {
419 std::lock_guard<std::mutex> lockCbMap(pInputDeviceChangeMutex_);
420 return preferredInputDeviceCallbackList_.size();
421 }
422
OnPreferredInputDeviceUpdated(const std::vector<sptr<AudioDeviceDescriptor>> & desc)423 void AudioPolicyClientStubImpl::OnPreferredInputDeviceUpdated(const std::vector<sptr<AudioDeviceDescriptor>> &desc)
424 {
425 std::lock_guard<std::mutex> lockCbMap(pInputDeviceChangeMutex_);
426 for (auto it = preferredInputDeviceCallbackList_.begin(); it != preferredInputDeviceCallbackList_.end(); ++it) {
427 (*it)->OnPreferredInputDeviceUpdated(desc);
428 }
429 }
430
AddRendererStateChangeCallback(const std::shared_ptr<AudioRendererStateChangeCallback> & cb)431 int32_t AudioPolicyClientStubImpl::AddRendererStateChangeCallback(
432 const std::shared_ptr<AudioRendererStateChangeCallback> &cb)
433 {
434 CHECK_AND_RETURN_RET_LOG(cb, ERR_INVALID_PARAM, "cb is null");
435
436 std::lock_guard<std::mutex> lockCbMap(rendererStateChangeMutex_);
437 rendererStateChangeCallbackList_.push_back(cb);
438 return SUCCESS;
439 }
440
RemoveRendererStateChangeCallback(const std::vector<std::shared_ptr<AudioRendererStateChangeCallback>> & callbacks)441 int32_t AudioPolicyClientStubImpl::RemoveRendererStateChangeCallback(
442 const std::vector<std::shared_ptr<AudioRendererStateChangeCallback>> &callbacks)
443 {
444 std::lock_guard<std::mutex> lockCbMap(rendererStateChangeMutex_);
445 for (const auto &cb : callbacks) {
446 rendererStateChangeCallbackList_.erase(
447 std::remove(rendererStateChangeCallbackList_.begin(),
448 rendererStateChangeCallbackList_.end(), cb), rendererStateChangeCallbackList_.end());
449 }
450
451 return SUCCESS;
452 }
453
RemoveRendererStateChangeCallback(const std::shared_ptr<AudioRendererStateChangeCallback> & callback)454 int32_t AudioPolicyClientStubImpl::RemoveRendererStateChangeCallback(
455 const std::shared_ptr<AudioRendererStateChangeCallback> &callback)
456 {
457 std::lock_guard<std::mutex> lockCbMap(rendererStateChangeMutex_);
458 rendererStateChangeCallbackList_.erase(
459 std::remove(rendererStateChangeCallbackList_.begin(),
460 rendererStateChangeCallbackList_.end(), callback), rendererStateChangeCallbackList_.end());
461
462 return SUCCESS;
463 }
464
GetRendererStateChangeCallbackSize() const465 size_t AudioPolicyClientStubImpl::GetRendererStateChangeCallbackSize() const
466 {
467 std::lock_guard<std::mutex> lockCbMap(rendererStateChangeMutex_);
468 return rendererStateChangeCallbackList_.size();
469 }
470
AddDeviceChangeWithInfoCallback(const uint32_t sessionId,const std::weak_ptr<DeviceChangeWithInfoCallback> & cb)471 int32_t AudioPolicyClientStubImpl::AddDeviceChangeWithInfoCallback(
472 const uint32_t sessionId, const std::weak_ptr<DeviceChangeWithInfoCallback> &cb)
473 {
474 std::lock_guard<std::mutex> lockCbMap(deviceChangeWithInfoCallbackMutex_);
475 deviceChangeWithInfoCallbackMap_[sessionId] = cb;
476 return SUCCESS;
477 }
478
RemoveDeviceChangeWithInfoCallback(const uint32_t sessionId)479 int32_t AudioPolicyClientStubImpl::RemoveDeviceChangeWithInfoCallback(const uint32_t sessionId)
480 {
481 std::lock_guard<std::mutex> lockCbMap(deviceChangeWithInfoCallbackMutex_);
482 deviceChangeWithInfoCallbackMap_.erase(sessionId);
483 return SUCCESS;
484 }
485
GetDeviceChangeWithInfoCallbackkSize() const486 size_t AudioPolicyClientStubImpl::GetDeviceChangeWithInfoCallbackkSize() const
487 {
488 std::lock_guard<std::mutex> lockCbMap(deviceChangeWithInfoCallbackMutex_);
489 return deviceChangeWithInfoCallbackMap_.size();
490 }
491
OnRendererDeviceChange(const uint32_t sessionId,const DeviceInfo & deviceInfo,const AudioStreamDeviceChangeReasonExt reason)492 void AudioPolicyClientStubImpl::OnRendererDeviceChange(const uint32_t sessionId,
493 const DeviceInfo &deviceInfo, const AudioStreamDeviceChangeReasonExt reason)
494 {
495 Trace trace("AudioPolicyClientStubImpl::OnRendererDeviceChange");
496 std::shared_ptr<DeviceChangeWithInfoCallback> callback = nullptr;
497 {
498 std::lock_guard<std::mutex> lockCbMap(deviceChangeWithInfoCallbackMutex_);
499 if (deviceChangeWithInfoCallbackMap_.count(sessionId) == 0) {
500 return;
501 }
502 callback = deviceChangeWithInfoCallbackMap_.at(sessionId).lock();
503 if (callback == nullptr) {
504 deviceChangeWithInfoCallbackMap_.erase(sessionId);
505 return;
506 }
507 }
508 if (callback != nullptr) {
509 Trace traceCallback("callback->OnDeviceChangeWithInfo sessionid:" + std::to_string(sessionId)
510 + " reason:" + std::to_string(static_cast<int>(reason)));
511 callback->OnDeviceChangeWithInfo(sessionId, deviceInfo, reason);
512 }
513 }
514
OnRendererStateChange(std::vector<std::unique_ptr<AudioRendererChangeInfo>> & audioRendererChangeInfos)515 void AudioPolicyClientStubImpl::OnRendererStateChange(
516 std::vector<std::unique_ptr<AudioRendererChangeInfo>> &audioRendererChangeInfos)
517 {
518 std::vector<std::shared_ptr<AudioRendererStateChangeCallback>> callbacks;
519 {
520 std::lock_guard<std::mutex> lockCbMap(rendererStateChangeMutex_);
521 callbacks = rendererStateChangeCallbackList_;
522 }
523 size_t cBSize = callbacks.size();
524 size_t infosSize = audioRendererChangeInfos.size();
525 AUDIO_DEBUG_LOG("cbSize: %{public}zu infoSize: %{public}zu", cBSize, infosSize);
526
527 if (getuid() == RSS_UID) {
528 AUDIO_INFO_LOG("cbSize: %{public}zu infoSize: %{public}zu", cBSize, infosSize);
529 }
530
531 Trace trace("AudioPolicyClientStubImpl::OnRendererStateChange");
532 for (auto &cb : callbacks) {
533 Trace traceCallback("OnRendererStateChange");
534 cb->OnRendererStateChange(audioRendererChangeInfos);
535 }
536 }
537
OnRecreateRendererStreamEvent(const uint32_t sessionId,const int32_t streamFlag,const AudioStreamDeviceChangeReasonExt reason)538 void AudioPolicyClientStubImpl::OnRecreateRendererStreamEvent(const uint32_t sessionId, const int32_t streamFlag,
539 const AudioStreamDeviceChangeReasonExt reason)
540 {
541 AUDIO_INFO_LOG("Enter");
542 std::shared_ptr<DeviceChangeWithInfoCallback> callback = nullptr;
543 {
544 std::lock_guard<std::mutex> lockCbMap(deviceChangeWithInfoCallbackMutex_);
545 if (deviceChangeWithInfoCallbackMap_.count(sessionId) == 0) {
546 AUDIO_ERR_LOG("No session id %{public}d", sessionId);
547 return;
548 }
549 callback = deviceChangeWithInfoCallbackMap_.at(sessionId).lock();
550 }
551 if (callback != nullptr) {
552 callback->OnRecreateStreamEvent(sessionId, streamFlag, reason);
553 }
554 }
555
OnRecreateCapturerStreamEvent(const uint32_t sessionId,const int32_t streamFlag,const AudioStreamDeviceChangeReasonExt reason)556 void AudioPolicyClientStubImpl::OnRecreateCapturerStreamEvent(const uint32_t sessionId, const int32_t streamFlag,
557 const AudioStreamDeviceChangeReasonExt reason)
558 {
559 AUDIO_INFO_LOG("Enter");
560 std::shared_ptr<DeviceChangeWithInfoCallback> callback = nullptr;
561 {
562 std::lock_guard<std::mutex> lockCbMap(deviceChangeWithInfoCallbackMutex_);
563 if (deviceChangeWithInfoCallbackMap_.count(sessionId) == 0) {
564 AUDIO_ERR_LOG("No session id %{public}d", sessionId);
565 return;
566 }
567 callback = deviceChangeWithInfoCallbackMap_.at(sessionId).lock();
568 }
569 if (callback != nullptr) {
570 callback->OnRecreateStreamEvent(sessionId, streamFlag, reason);
571 }
572 }
573
AddCapturerStateChangeCallback(const std::shared_ptr<AudioCapturerStateChangeCallback> & cb)574 int32_t AudioPolicyClientStubImpl::AddCapturerStateChangeCallback(
575 const std::shared_ptr<AudioCapturerStateChangeCallback> &cb)
576 {
577 std::lock_guard<std::mutex> lockCbMap(capturerStateChangeMutex_);
578 capturerStateChangeCallbackList_.push_back(cb);
579 return SUCCESS;
580 }
581
RemoveCapturerStateChangeCallback()582 int32_t AudioPolicyClientStubImpl::RemoveCapturerStateChangeCallback()
583 {
584 std::lock_guard<std::mutex> lockCbMap(capturerStateChangeMutex_);
585 capturerStateChangeCallbackList_.clear();
586 return SUCCESS;
587 }
588
GetCapturerStateChangeCallbackSize() const589 size_t AudioPolicyClientStubImpl::GetCapturerStateChangeCallbackSize() const
590 {
591 std::lock_guard<std::mutex> lockCbMap(capturerStateChangeMutex_);
592 return capturerStateChangeCallbackList_.size();
593 }
594
OnCapturerStateChange(std::vector<std::unique_ptr<AudioCapturerChangeInfo>> & audioCapturerChangeInfos)595 void AudioPolicyClientStubImpl::OnCapturerStateChange(
596 std::vector<std::unique_ptr<AudioCapturerChangeInfo>> &audioCapturerChangeInfos)
597 {
598 std::vector<std::shared_ptr<AudioCapturerStateChangeCallback>> tmpCallbackList;
599 {
600 std::lock_guard<std::mutex> lockCbMap(capturerStateChangeMutex_);
601 for (auto it = capturerStateChangeCallbackList_.begin(); it != capturerStateChangeCallbackList_.end(); ++it) {
602 std::shared_ptr<AudioCapturerStateChangeCallback> capturerStateChangeCallback = (*it).lock();
603 if (capturerStateChangeCallback != nullptr) {
604 tmpCallbackList.emplace_back(capturerStateChangeCallback);
605 }
606 }
607 }
608 for (auto it = tmpCallbackList.begin(); it != tmpCallbackList.end(); ++it) {
609 (*it)->OnCapturerStateChange(audioCapturerChangeInfos);
610 }
611 }
612
AddHeadTrackingDataRequestedChangeCallback(const std::string & macAddress,const std::shared_ptr<HeadTrackingDataRequestedChangeCallback> & cb)613 int32_t AudioPolicyClientStubImpl::AddHeadTrackingDataRequestedChangeCallback(const std::string &macAddress,
614 const std::shared_ptr<HeadTrackingDataRequestedChangeCallback> &cb)
615 {
616 std::lock_guard<std::mutex> lockCbMap(headTrackingDataRequestedChangeMutex_);
617 if (!headTrackingDataRequestedChangeCallbackMap_.count(macAddress)) {
618 AUDIO_INFO_LOG("First registeration for the specified device");
619 headTrackingDataRequestedChangeCallbackMap_.insert(std::make_pair(macAddress, cb));
620 } else {
621 AUDIO_INFO_LOG("Repeated registeration for the specified device, replaced by the new one");
622 headTrackingDataRequestedChangeCallbackMap_[macAddress] = cb;
623 }
624 return SUCCESS;
625 }
626
RemoveHeadTrackingDataRequestedChangeCallback(const std::string & macAddress)627 int32_t AudioPolicyClientStubImpl::RemoveHeadTrackingDataRequestedChangeCallback(const std::string &macAddress)
628 {
629 std::lock_guard<std::mutex> lockCbMap(headTrackingDataRequestedChangeMutex_);
630 headTrackingDataRequestedChangeCallbackMap_.erase(macAddress);
631 return SUCCESS;
632 }
633
GetHeadTrackingDataRequestedChangeCallbackSize() const634 size_t AudioPolicyClientStubImpl::GetHeadTrackingDataRequestedChangeCallbackSize() const
635 {
636 std::lock_guard<std::mutex> lockCbMap(headTrackingDataRequestedChangeMutex_);
637 return headTrackingDataRequestedChangeCallbackMap_.size();
638 }
639
OnHeadTrackingDeviceChange(const std::unordered_map<std::string,bool> & changeInfo)640 void AudioPolicyClientStubImpl::OnHeadTrackingDeviceChange(const std::unordered_map<std::string, bool> &changeInfo)
641 {
642 std::lock_guard<std::mutex> lockCbMap(headTrackingDataRequestedChangeMutex_);
643 if (headTrackingDataRequestedChangeCallbackMap_.size() == 0) {
644 return;
645 }
646 for (const auto &pair : changeInfo) {
647 if (!headTrackingDataRequestedChangeCallbackMap_.count(pair.first)) {
648 AUDIO_WARNING_LOG("the specified device has not been registered");
649 continue;
650 }
651 std::shared_ptr<HeadTrackingDataRequestedChangeCallback> headTrackingDataRequestedChangeCallback =
652 headTrackingDataRequestedChangeCallbackMap_[pair.first];
653 if (headTrackingDataRequestedChangeCallback != nullptr) {
654 AUDIO_DEBUG_LOG("head tracking data requested change event of the specified device has been notified");
655 headTrackingDataRequestedChangeCallback->OnHeadTrackingDataRequestedChange(pair.second);
656 }
657 }
658 }
659
AddSpatializationEnabledChangeCallback(const std::shared_ptr<AudioSpatializationEnabledChangeCallback> & cb)660 int32_t AudioPolicyClientStubImpl::AddSpatializationEnabledChangeCallback(
661 const std::shared_ptr<AudioSpatializationEnabledChangeCallback> &cb)
662 {
663 std::lock_guard<std::mutex> lockCbMap(spatializationEnabledChangeMutex_);
664 spatializationEnabledChangeCallbackList_.push_back(cb);
665 return SUCCESS;
666 }
667
RemoveSpatializationEnabledChangeCallback()668 int32_t AudioPolicyClientStubImpl::RemoveSpatializationEnabledChangeCallback()
669 {
670 std::lock_guard<std::mutex> lockCbMap(spatializationEnabledChangeMutex_);
671 spatializationEnabledChangeCallbackList_.clear();
672 return SUCCESS;
673 }
674
GetSpatializationEnabledChangeCallbackSize() const675 size_t AudioPolicyClientStubImpl::GetSpatializationEnabledChangeCallbackSize() const
676 {
677 std::lock_guard<std::mutex> lockCbMap(spatializationEnabledChangeMutex_);
678 return spatializationEnabledChangeCallbackList_.size();
679 }
680
OnSpatializationEnabledChange(const bool & enabled)681 void AudioPolicyClientStubImpl::OnSpatializationEnabledChange(const bool &enabled)
682 {
683 std::lock_guard<std::mutex> lockCbMap(spatializationEnabledChangeMutex_);
684 for (const auto &callback : spatializationEnabledChangeCallbackList_) {
685 callback->OnSpatializationEnabledChange(enabled);
686 }
687 }
688
OnSpatializationEnabledChangeForAnyDevice(const sptr<AudioDeviceDescriptor> & deviceDescriptor,const bool & enabled)689 void AudioPolicyClientStubImpl::OnSpatializationEnabledChangeForAnyDevice(const sptr<AudioDeviceDescriptor>
690 &deviceDescriptor, const bool &enabled)
691 {
692 std::lock_guard<std::mutex> lockCbMap(spatializationEnabledChangeMutex_);
693 for (const auto &callback : spatializationEnabledChangeCallbackList_) {
694 callback->OnSpatializationEnabledChangeForAnyDevice(deviceDescriptor, enabled);
695 }
696 }
697
AddHeadTrackingEnabledChangeCallback(const std::shared_ptr<AudioHeadTrackingEnabledChangeCallback> & cb)698 int32_t AudioPolicyClientStubImpl::AddHeadTrackingEnabledChangeCallback(
699 const std::shared_ptr<AudioHeadTrackingEnabledChangeCallback> &cb)
700 {
701 std::lock_guard<std::mutex> lockCbMap(headTrackingEnabledChangeMutex_);
702 headTrackingEnabledChangeCallbackList_.push_back(cb);
703 return SUCCESS;
704 }
705
RemoveHeadTrackingEnabledChangeCallback()706 int32_t AudioPolicyClientStubImpl::RemoveHeadTrackingEnabledChangeCallback()
707 {
708 std::lock_guard<std::mutex> lockCbMap(headTrackingEnabledChangeMutex_);
709 headTrackingEnabledChangeCallbackList_.clear();
710 return SUCCESS;
711 }
712
GetHeadTrackingEnabledChangeCallbacSize() const713 size_t AudioPolicyClientStubImpl::GetHeadTrackingEnabledChangeCallbacSize() const
714 {
715 std::lock_guard<std::mutex> lockCbMap(headTrackingEnabledChangeMutex_);
716 return headTrackingEnabledChangeCallbackList_.size();
717 }
718
OnHeadTrackingEnabledChange(const bool & enabled)719 void AudioPolicyClientStubImpl::OnHeadTrackingEnabledChange(const bool &enabled)
720 {
721 std::lock_guard<std::mutex> lockCbMap(headTrackingEnabledChangeMutex_);
722 for (const auto &callback : headTrackingEnabledChangeCallbackList_) {
723 callback->OnHeadTrackingEnabledChange(enabled);
724 }
725 }
726
OnHeadTrackingEnabledChangeForAnyDevice(const sptr<AudioDeviceDescriptor> & deviceDescriptor,const bool & enabled)727 void AudioPolicyClientStubImpl::OnHeadTrackingEnabledChangeForAnyDevice(const sptr<AudioDeviceDescriptor>
728 &deviceDescriptor, const bool &enabled)
729 {
730 std::lock_guard<std::mutex> lockCbMap(headTrackingEnabledChangeMutex_);
731 for (const auto &callback : headTrackingEnabledChangeCallbackList_) {
732 callback->OnHeadTrackingEnabledChangeForAnyDevice(deviceDescriptor, enabled);
733 }
734 }
735 } // namespace AudioStandard
736 } // namespace OHOS