1 /*
2 * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "audio_errors.h"
17 #include "audio_manager_proxy.h"
18 #include "audio_stream.h"
19 #include "audio_policy_manager.h"
20 #include "audio_volume_key_event_callback_stub.h"
21
22 #include "iservice_registry.h"
23 #include "media_log.h"
24 #include "system_ability_definition.h"
25
26 #include "audio_system_manager.h"
27
28 namespace OHOS {
29 namespace AudioStandard {
30 using namespace std;
31 static sptr<IStandardAudioService> g_sProxy = nullptr;
32 const map<pair<ContentType, StreamUsage>, AudioStreamType> AudioSystemManager::streamTypeMap_
33 = AudioSystemManager::CreateStreamMap();
34
AudioSystemManager()35 AudioSystemManager::AudioSystemManager()
36 {
37 MEDIA_DEBUG_LOG("AudioSystemManager start");
38 init();
39 }
40
~AudioSystemManager()41 AudioSystemManager::~AudioSystemManager()
42 {
43 MEDIA_DEBUG_LOG("AudioSystemManager::~AudioSystemManager");
44 if (cbClientId_ != -1) {
45 UnsetRingerModeCallback(cbClientId_);
46 }
47
48 if (volumeChangeClientPid_ != -1) {
49 MEDIA_DEBUG_LOG("AudioSystemManager::~AudioSystemManager UnregisterVolumeKeyEventCallback");
50 (void)UnregisterVolumeKeyEventCallback(volumeChangeClientPid_);
51 }
52 }
53
GetInstance()54 AudioSystemManager *AudioSystemManager::GetInstance()
55 {
56 static AudioSystemManager audioManager;
57 return &audioManager;
58 }
59
GetCallingPid()60 uint32_t AudioSystemManager::GetCallingPid()
61 {
62 return getpid();
63 }
64
CreateStreamMap()65 map<pair<ContentType, StreamUsage>, AudioStreamType> AudioSystemManager::CreateStreamMap()
66 {
67 map<pair<ContentType, StreamUsage>, AudioStreamType> streamMap;
68
69 streamMap[make_pair(CONTENT_TYPE_UNKNOWN, STREAM_USAGE_UNKNOWN)] = AudioStreamType::STREAM_MUSIC;
70 streamMap[make_pair(CONTENT_TYPE_UNKNOWN, STREAM_USAGE_MEDIA)] = AudioStreamType::STREAM_MUSIC;
71 streamMap[make_pair(CONTENT_TYPE_UNKNOWN, STREAM_USAGE_VOICE_COMMUNICATION)] = AudioStreamType::STREAM_MUSIC;
72 streamMap[make_pair(CONTENT_TYPE_UNKNOWN, STREAM_USAGE_VOICE_ASSISTANT)] = AudioStreamType::STREAM_MUSIC;
73 streamMap[make_pair(CONTENT_TYPE_UNKNOWN, STREAM_USAGE_NOTIFICATION_RINGTONE)] = AudioStreamType::STREAM_MUSIC;
74
75 streamMap[make_pair(CONTENT_TYPE_SPEECH, STREAM_USAGE_UNKNOWN)] = AudioStreamType::STREAM_MUSIC;
76 streamMap[make_pair(CONTENT_TYPE_SPEECH, STREAM_USAGE_MEDIA)] = AudioStreamType::STREAM_VOICE_ASSISTANT;
77 streamMap[make_pair(CONTENT_TYPE_SPEECH, STREAM_USAGE_VOICE_COMMUNICATION)] = AudioStreamType::STREAM_VOICE_CALL;
78 streamMap[make_pair(CONTENT_TYPE_SPEECH, STREAM_USAGE_VOICE_ASSISTANT)] = AudioStreamType::STREAM_VOICE_ASSISTANT;
79 streamMap[make_pair(CONTENT_TYPE_SPEECH, STREAM_USAGE_NOTIFICATION_RINGTONE)] = AudioStreamType::STREAM_MUSIC;
80
81 streamMap[make_pair(CONTENT_TYPE_MUSIC, STREAM_USAGE_UNKNOWN)] = AudioStreamType::STREAM_MUSIC;
82 streamMap[make_pair(CONTENT_TYPE_MUSIC, STREAM_USAGE_MEDIA)] = AudioStreamType::STREAM_MUSIC;
83 streamMap[make_pair(CONTENT_TYPE_MUSIC, STREAM_USAGE_VOICE_COMMUNICATION)] = AudioStreamType::STREAM_MUSIC;
84 streamMap[make_pair(CONTENT_TYPE_MUSIC, STREAM_USAGE_VOICE_ASSISTANT)] = AudioStreamType::STREAM_VOICE_ASSISTANT;
85 streamMap[make_pair(CONTENT_TYPE_MUSIC, STREAM_USAGE_NOTIFICATION_RINGTONE)] = AudioStreamType::STREAM_RING;
86
87 streamMap[make_pair(CONTENT_TYPE_MOVIE, STREAM_USAGE_UNKNOWN)] = AudioStreamType::STREAM_MEDIA;
88 streamMap[make_pair(CONTENT_TYPE_MOVIE, STREAM_USAGE_MEDIA)] = AudioStreamType::STREAM_MEDIA;
89 streamMap[make_pair(CONTENT_TYPE_MOVIE, STREAM_USAGE_VOICE_COMMUNICATION)] = AudioStreamType::STREAM_MUSIC;
90 streamMap[make_pair(CONTENT_TYPE_MOVIE, STREAM_USAGE_VOICE_ASSISTANT)] = AudioStreamType::STREAM_MUSIC;
91 streamMap[make_pair(CONTENT_TYPE_MOVIE, STREAM_USAGE_NOTIFICATION_RINGTONE)] = AudioStreamType::STREAM_MUSIC;
92
93 streamMap[make_pair(CONTENT_TYPE_SONIFICATION, STREAM_USAGE_UNKNOWN)] = AudioStreamType::STREAM_NOTIFICATION;
94 streamMap[make_pair(CONTENT_TYPE_SONIFICATION, STREAM_USAGE_MEDIA)] = AudioStreamType::STREAM_NOTIFICATION;
95 streamMap[make_pair(CONTENT_TYPE_SONIFICATION, STREAM_USAGE_VOICE_COMMUNICATION)] = AudioStreamType::STREAM_MUSIC;
96 streamMap[make_pair(CONTENT_TYPE_SONIFICATION, STREAM_USAGE_VOICE_ASSISTANT)] = AudioStreamType::STREAM_MUSIC;
97 streamMap[make_pair(CONTENT_TYPE_SONIFICATION, STREAM_USAGE_NOTIFICATION_RINGTONE)] = AudioStreamType::STREAM_MUSIC;
98
99 streamMap[make_pair(CONTENT_TYPE_RINGTONE, STREAM_USAGE_UNKNOWN)] = AudioStreamType::STREAM_RING;
100 streamMap[make_pair(CONTENT_TYPE_RINGTONE, STREAM_USAGE_MEDIA)] = AudioStreamType::STREAM_RING;
101 streamMap[make_pair(CONTENT_TYPE_RINGTONE, STREAM_USAGE_VOICE_COMMUNICATION)] = AudioStreamType::STREAM_MUSIC;
102 streamMap[make_pair(CONTENT_TYPE_RINGTONE, STREAM_USAGE_VOICE_ASSISTANT)] = AudioStreamType::STREAM_MUSIC;
103 streamMap[make_pair(CONTENT_TYPE_RINGTONE, STREAM_USAGE_NOTIFICATION_RINGTONE)] = AudioStreamType::STREAM_RING;
104
105 return streamMap;
106 }
107
GetStreamType(ContentType contentType,StreamUsage streamUsage)108 AudioStreamType AudioSystemManager::GetStreamType(ContentType contentType, StreamUsage streamUsage)
109 {
110 AudioStreamType streamType = AudioStreamType::STREAM_MUSIC;
111 auto pos = streamTypeMap_.find(make_pair(contentType, streamUsage));
112 if (pos != streamTypeMap_.end()) {
113 streamType = pos->second;
114 }
115
116 if (streamType == AudioStreamType::STREAM_MEDIA) {
117 streamType = AudioStreamType::STREAM_MUSIC;
118 }
119
120 return streamType;
121 }
122
123
init()124 void AudioSystemManager::init()
125 {
126 auto samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
127 if (samgr == nullptr) {
128 MEDIA_ERR_LOG("AudioSystemManager::init failed");
129 return;
130 }
131
132 sptr<IRemoteObject> object = samgr->GetSystemAbility(AUDIO_DISTRIBUTED_SERVICE_ID);
133 if (object == nullptr) {
134 MEDIA_DEBUG_LOG("AudioSystemManager::object is NULL.");
135 return;
136 }
137 g_sProxy = iface_cast<IStandardAudioService>(object);
138 if (g_sProxy == nullptr) {
139 MEDIA_DEBUG_LOG("AudioSystemManager::init g_sProxy is NULL.");
140 } else {
141 MEDIA_DEBUG_LOG("AudioSystemManager::init g_sProxy is assigned.");
142 }
143 }
144
SetRingerMode(AudioRingerMode ringMode) const145 bool AudioSystemManager::SetRingerMode(AudioRingerMode ringMode) const
146 {
147 /* Call Audio Policy SetRingerMode */
148 return AudioPolicyManager::GetInstance().SetRingerMode(ringMode);
149 }
150
GetRingerMode() const151 AudioRingerMode AudioSystemManager::GetRingerMode() const
152 {
153 /* Call Audio Policy GetRingerMode */
154 return (AudioPolicyManager::GetInstance().GetRingerMode());
155 }
156
SetAudioScene(const AudioScene & scene)157 int32_t AudioSystemManager::SetAudioScene(const AudioScene &scene)
158 {
159 MEDIA_DEBUG_LOG("SetAudioScene audioScene_=%{public}d done", scene);
160 return AudioPolicyManager::GetInstance().SetAudioScene(scene);
161 }
162
GetAudioScene() const163 AudioScene AudioSystemManager::GetAudioScene() const
164 {
165 return AudioPolicyManager::GetInstance().GetAudioScene();
166 }
167
SetDeviceActive(ActiveDeviceType deviceType,bool flag) const168 int32_t AudioSystemManager::SetDeviceActive(ActiveDeviceType deviceType, bool flag) const
169 {
170 switch (deviceType) {
171 case SPEAKER:
172 case BLUETOOTH_SCO:
173 break;
174 default:
175 MEDIA_ERR_LOG("SetDeviceActive device=%{public}d not supported", deviceType);
176 return ERR_NOT_SUPPORTED;
177 }
178
179 /* Call Audio Policy SetDeviceActive */
180 return (AudioPolicyManager::GetInstance().SetDeviceActive(static_cast<InternalDeviceType>(deviceType), flag));
181 }
182
IsDeviceActive(ActiveDeviceType deviceType) const183 bool AudioSystemManager::IsDeviceActive(ActiveDeviceType deviceType) const
184 {
185 switch (deviceType) {
186 case SPEAKER:
187 case BLUETOOTH_SCO:
188 break;
189 default:
190 MEDIA_ERR_LOG("IsDeviceActive device=%{public}d not supported", deviceType);
191 return false;
192 }
193
194 /* Call Audio Policy IsDeviceActive */
195 return (AudioPolicyManager::GetInstance().IsDeviceActive(static_cast<InternalDeviceType>(deviceType)));
196 }
197
IsStreamActive(AudioSystemManager::AudioVolumeType volumeType) const198 bool AudioSystemManager::IsStreamActive(AudioSystemManager::AudioVolumeType volumeType) const
199 {
200 switch (volumeType) {
201 case STREAM_MUSIC:
202 case STREAM_RING:
203 case STREAM_VOICE_CALL:
204 case STREAM_VOICE_ASSISTANT:
205 break;
206 default:
207 MEDIA_ERR_LOG("IsStreamActive volumeType=%{public}d not supported", volumeType);
208 return false;
209 }
210
211 AudioStreamType StreamVolType = (AudioStreamType)volumeType;
212 return AudioPolicyManager::GetInstance().IsStreamActive(StreamVolType);
213 }
214
GetAudioParameter(const std::string key) const215 const std::string AudioSystemManager::GetAudioParameter(const std::string key) const
216 {
217 CHECK_AND_RETURN_RET_LOG(g_sProxy != nullptr, "", "GetAudioParameter::Audio service unavailable");
218 return g_sProxy->GetAudioParameter(key);
219 }
220
SetAudioParameter(const std::string & key,const std::string & value) const221 void AudioSystemManager::SetAudioParameter(const std::string &key, const std::string &value) const
222 {
223 CHECK_AND_RETURN_LOG(g_sProxy != nullptr, "SetAudioParameter::Audio service unavailable");
224 g_sProxy->SetAudioParameter(key, value);
225 }
226
RetrieveCookie(int32_t & size) const227 const char *AudioSystemManager::RetrieveCookie(int32_t &size) const
228 {
229 CHECK_AND_RETURN_RET_LOG(g_sProxy != nullptr, nullptr, "RetrieveCookie::Audio service unavailable");
230 return g_sProxy->RetrieveCookie(size);
231 }
232
SetVolume(AudioSystemManager::AudioVolumeType volumeType,int32_t volume) const233 int32_t AudioSystemManager::SetVolume(AudioSystemManager::AudioVolumeType volumeType, int32_t volume) const
234 {
235 /* Validate and return INVALID_PARAMS error */
236 if ((volume < MIN_VOLUME_LEVEL) || (volume > MAX_VOLUME_LEVEL)) {
237 MEDIA_ERR_LOG("Invalid Volume Input!");
238 return ERR_INVALID_PARAM;
239 }
240
241 switch (volumeType) {
242 case STREAM_MUSIC:
243 case STREAM_RING:
244 case STREAM_NOTIFICATION:
245 case STREAM_VOICE_CALL:
246 case STREAM_VOICE_ASSISTANT:
247 break;
248 default:
249 MEDIA_ERR_LOG("SetVolume volumeType=%{public}d not supported", volumeType);
250 return ERR_NOT_SUPPORTED;
251 }
252
253 /* Call Audio Policy SetStreamVolume */
254 AudioStreamType StreamVolType = (AudioStreamType)volumeType;
255 float volumeToHdi = MapVolumeToHDI(volume);
256 return AudioPolicyManager::GetInstance().SetStreamVolume(StreamVolType, volumeToHdi);
257 }
258
GetVolume(AudioSystemManager::AudioVolumeType volumeType) const259 int32_t AudioSystemManager::GetVolume(AudioSystemManager::AudioVolumeType volumeType) const
260 {
261 switch (volumeType) {
262 case STREAM_MUSIC:
263 case STREAM_RING:
264 case STREAM_NOTIFICATION:
265 case STREAM_VOICE_CALL:
266 case STREAM_VOICE_ASSISTANT:
267 break;
268 default:
269 MEDIA_ERR_LOG("GetVolume volumeType=%{public}d not supported", volumeType);
270 return (float)ERR_NOT_SUPPORTED;
271 }
272
273 /* Call Audio Policy SetStreamMute */
274 AudioStreamType StreamVolType = (AudioStreamType)volumeType;
275 float volumeFromHdi = AudioPolicyManager::GetInstance().GetStreamVolume(StreamVolType);
276
277 return MapVolumeFromHDI(volumeFromHdi);
278 }
279
MapVolumeToHDI(int32_t volume)280 float AudioSystemManager::MapVolumeToHDI(int32_t volume)
281 {
282 float value = (float)volume / MAX_VOLUME_LEVEL;
283 float roundValue = (int)(value * CONST_FACTOR);
284
285 return (float)roundValue / CONST_FACTOR;
286 }
287
MapVolumeFromHDI(float volume)288 int32_t AudioSystemManager::MapVolumeFromHDI(float volume)
289 {
290 float value = (float)volume * MAX_VOLUME_LEVEL;
291 return nearbyint(value);
292 }
293
GetMaxVolume(AudioSystemManager::AudioVolumeType volumeType) const294 int32_t AudioSystemManager::GetMaxVolume(AudioSystemManager::AudioVolumeType volumeType) const
295 {
296 CHECK_AND_RETURN_RET_LOG(g_sProxy != nullptr, ERR_OPERATION_FAILED, "GetMaxVolume::Audio service unavailable");
297 return g_sProxy->GetMaxVolume(volumeType);
298 }
299
GetMinVolume(AudioSystemManager::AudioVolumeType volumeType) const300 int32_t AudioSystemManager::GetMinVolume(AudioSystemManager::AudioVolumeType volumeType) const
301 {
302 CHECK_AND_RETURN_RET_LOG(g_sProxy != nullptr, ERR_OPERATION_FAILED, "GetMinVolume::Audio service unavailable");
303 return g_sProxy->GetMinVolume(volumeType);
304 }
305
SetMute(AudioSystemManager::AudioVolumeType volumeType,bool mute) const306 int32_t AudioSystemManager::SetMute(AudioSystemManager::AudioVolumeType volumeType, bool mute) const
307 {
308 switch (volumeType) {
309 case STREAM_MUSIC:
310 case STREAM_RING:
311 case STREAM_NOTIFICATION:
312 case STREAM_VOICE_CALL:
313 case STREAM_VOICE_ASSISTANT:
314 break;
315 default:
316 MEDIA_ERR_LOG("SetMute volumeType=%{public}d not supported", volumeType);
317 return ERR_NOT_SUPPORTED;
318 }
319
320 /* Call Audio Policy SetStreamMute */
321 AudioStreamType StreamVolType = (AudioStreamType)volumeType;
322 return AudioPolicyManager::GetInstance().SetStreamMute(StreamVolType, mute);
323 }
324
IsStreamMute(AudioSystemManager::AudioVolumeType volumeType) const325 bool AudioSystemManager::IsStreamMute(AudioSystemManager::AudioVolumeType volumeType) const
326 {
327 MEDIA_DEBUG_LOG("AudioSystemManager::GetMute Client");
328
329 switch (volumeType) {
330 case STREAM_MUSIC:
331 case STREAM_RING:
332 case STREAM_NOTIFICATION:
333 case STREAM_VOICE_CALL:
334 case STREAM_VOICE_ASSISTANT:
335 break;
336 default:
337 MEDIA_ERR_LOG("IsStreamMute volumeType=%{public}d not supported", volumeType);
338 return false;
339 }
340
341 /* Call Audio Policy SetStreamVolume */
342 AudioStreamType StreamVolType = (AudioStreamType)volumeType;
343 return AudioPolicyManager::GetInstance().GetStreamMute(StreamVolType);
344 }
345
SetDeviceChangeCallback(const std::shared_ptr<AudioManagerDeviceChangeCallback> & callback)346 int32_t AudioSystemManager::SetDeviceChangeCallback(const std::shared_ptr<AudioManagerDeviceChangeCallback> &callback)
347 {
348 MEDIA_INFO_LOG("Entered AudioSystemManager::%{public}s", __func__);
349 if (callback == nullptr) {
350 MEDIA_ERR_LOG("SetDeviceChangeCallback: callback is nullptr");
351 return ERR_INVALID_PARAM;
352 }
353
354 int32_t clientId = GetCallingPid();
355 return AudioPolicyManager::GetInstance().SetDeviceChangeCallback(clientId, callback);
356 }
357
UnsetDeviceChangeCallback()358 int32_t AudioSystemManager::UnsetDeviceChangeCallback()
359 {
360 MEDIA_INFO_LOG("Entered AudioSystemManager::%{public}s", __func__);
361 int32_t clientId = GetCallingPid();
362 return AudioPolicyManager::GetInstance().UnsetDeviceChangeCallback(clientId);
363 }
364
SetRingerModeCallback(const int32_t clientId,const std::shared_ptr<AudioRingerModeCallback> & callback)365 int32_t AudioSystemManager::SetRingerModeCallback(const int32_t clientId,
366 const std::shared_ptr<AudioRingerModeCallback> &callback)
367 {
368 if (callback == nullptr) {
369 MEDIA_ERR_LOG("AudioSystemManager: callback is nullptr");
370 return ERR_INVALID_PARAM;
371 }
372
373 cbClientId_ = clientId;
374
375 return AudioPolicyManager::GetInstance().SetRingerModeCallback(clientId, callback);
376 }
377
UnsetRingerModeCallback(const int32_t clientId) const378 int32_t AudioSystemManager::UnsetRingerModeCallback(const int32_t clientId) const
379 {
380 return AudioPolicyManager::GetInstance().UnsetRingerModeCallback(clientId);
381 }
382
SetMicrophoneMute(bool isMute) const383 int32_t AudioSystemManager::SetMicrophoneMute(bool isMute) const
384 {
385 CHECK_AND_RETURN_RET_LOG(g_sProxy != nullptr, ERR_OPERATION_FAILED, "SetMicrophoneMute::Audio service unavailable");
386 return g_sProxy->SetMicrophoneMute(isMute);
387 }
388
IsMicrophoneMute() const389 bool AudioSystemManager::IsMicrophoneMute() const
390 {
391 CHECK_AND_RETURN_RET_LOG(g_sProxy != nullptr, ERR_OPERATION_FAILED, "IsMicrophoneMute::Audio service unavailable");
392 return g_sProxy->IsMicrophoneMute();
393 }
394
GetDevices(DeviceFlag deviceFlag)395 std::vector<sptr<AudioDeviceDescriptor>> AudioSystemManager::GetDevices(DeviceFlag deviceFlag)
396 {
397 return AudioPolicyManager::GetInstance().GetDevices(deviceFlag);
398 }
399
RegisterVolumeKeyEventCallback(const int32_t clientPid,const std::shared_ptr<VolumeKeyEventCallback> & callback)400 int32_t AudioSystemManager::RegisterVolumeKeyEventCallback(const int32_t clientPid,
401 const std::shared_ptr<VolumeKeyEventCallback> &callback)
402 {
403 MEDIA_DEBUG_LOG("AudioSystemManager RegisterVolumeKeyEventCallback");
404
405 if (callback == nullptr) {
406 MEDIA_ERR_LOG("AudioSystemManager::RegisterVolumeKeyEventCallbackcallback is nullptr");
407 return ERR_INVALID_PARAM;
408 }
409 volumeChangeClientPid_ = clientPid;
410
411 return AudioPolicyManager::GetInstance().SetVolumeKeyEventCallback(clientPid, callback);
412 }
413
UnregisterVolumeKeyEventCallback(const int32_t clientPid)414 int32_t AudioSystemManager::UnregisterVolumeKeyEventCallback(const int32_t clientPid)
415 {
416 MEDIA_DEBUG_LOG("AudioSystemManager::UnregisterVolumeKeyEventCallback");
417 int32_t ret = AudioPolicyManager::GetInstance().UnsetVolumeKeyEventCallback(clientPid);
418 if (!ret) {
419 MEDIA_DEBUG_LOG("AudioSystemManager::UnregisterVolumeKeyEventCallback success");
420 volumeChangeClientPid_ = -1;
421 }
422
423 return ret;
424 }
425
426 // Below stub implementation is added to handle compilation error in call manager
427 // Once call manager adapt to new interrupt implementation, this will be removed
SetAudioManagerCallback(const AudioSystemManager::AudioVolumeType streamType,const std::shared_ptr<AudioManagerCallback> & callback)428 int32_t AudioSystemManager::SetAudioManagerCallback(const AudioSystemManager::AudioVolumeType streamType,
429 const std::shared_ptr<AudioManagerCallback> &callback)
430 {
431 MEDIA_DEBUG_LOG("AudioSystemManager SetAudioManagerCallback stub implementation");
432 return SUCCESS;
433 }
434
UnsetAudioManagerCallback(const AudioSystemManager::AudioVolumeType streamType) const435 int32_t AudioSystemManager::UnsetAudioManagerCallback(const AudioSystemManager::AudioVolumeType streamType) const
436 {
437 MEDIA_DEBUG_LOG("AudioSystemManager UnsetAudioManagerCallback stub implementation");
438 return SUCCESS;
439 }
440
ActivateAudioInterrupt(const AudioInterrupt & audioInterrupt)441 int32_t AudioSystemManager::ActivateAudioInterrupt(const AudioInterrupt &audioInterrupt)
442 {
443 MEDIA_DEBUG_LOG("AudioSystemManager ActivateAudioInterrupt stub implementation");
444 return SUCCESS;
445 }
446
DeactivateAudioInterrupt(const AudioInterrupt & audioInterrupt) const447 int32_t AudioSystemManager::DeactivateAudioInterrupt(const AudioInterrupt &audioInterrupt) const
448 {
449 MEDIA_DEBUG_LOG("AudioSystemManager DeactivateAudioInterrupt stub implementation");
450 return SUCCESS;
451 }
452
SetAudioManagerInterruptCallback(const std::shared_ptr<AudioManagerCallback> & callback)453 int32_t AudioSystemManager::SetAudioManagerInterruptCallback(const std::shared_ptr<AudioManagerCallback> &callback)
454 {
455 uint32_t clientID = GetCallingPid();
456 MEDIA_INFO_LOG("AudioSystemManager:: SetAudioManagerInterruptCallback client id: %{public}d", clientID);
457 if (callback == nullptr) {
458 MEDIA_ERR_LOG("AudioSystemManager::callback is null");
459 return ERR_INVALID_PARAM;
460 }
461
462 if (audioInterruptCallback_ != nullptr) {
463 MEDIA_DEBUG_LOG("AudioSystemManager reset existing callback object");
464 AudioPolicyManager::GetInstance().UnsetAudioManagerInterruptCallback(clientID);
465 audioInterruptCallback_.reset();
466 audioInterruptCallback_ = nullptr;
467 }
468
469 audioInterruptCallback_ = std::make_shared<AudioManagerInterruptCallbackImpl>();
470 if (audioInterruptCallback_ == nullptr) {
471 MEDIA_ERR_LOG("AudioSystemManager::Failed to allocate memory for audioInterruptCallback");
472 return ERROR;
473 }
474
475 int32_t ret = AudioPolicyManager::GetInstance().SetAudioManagerInterruptCallback(clientID, audioInterruptCallback_);
476 if (ret != SUCCESS) {
477 MEDIA_ERR_LOG("AudioSystemManager::Failed set callback");
478 return ERROR;
479 }
480
481 std::shared_ptr<AudioManagerInterruptCallbackImpl> cbInterrupt =
482 std::static_pointer_cast<AudioManagerInterruptCallbackImpl>(audioInterruptCallback_);
483 cbInterrupt->SaveCallback(callback);
484
485 return SUCCESS;
486 }
487
UnsetAudioManagerInterruptCallback()488 int32_t AudioSystemManager::UnsetAudioManagerInterruptCallback()
489 {
490 uint32_t clientID = GetCallingPid();
491 MEDIA_INFO_LOG("AudioSystemManager:: UnsetAudioManagerInterruptCallback client id: %{public}d", clientID);
492
493 int32_t ret = AudioPolicyManager::GetInstance().UnsetAudioManagerInterruptCallback(clientID);
494 if (audioInterruptCallback_ != nullptr) {
495 audioInterruptCallback_.reset();
496 audioInterruptCallback_ = nullptr;
497 }
498
499 return ret;
500 }
501
RequestAudioFocus(const AudioInterrupt & audioInterrupt)502 int32_t AudioSystemManager::RequestAudioFocus(const AudioInterrupt &audioInterrupt)
503 {
504 uint32_t clientID = GetCallingPid();
505 MEDIA_INFO_LOG("AudioSystemManager:: RequestAudioFocus client id: %{public}d", clientID);
506 CHECK_AND_RETURN_RET_LOG(audioInterrupt.contentType >= CONTENT_TYPE_UNKNOWN
507 && audioInterrupt.contentType <= CONTENT_TYPE_RINGTONE, ERR_INVALID_PARAM,
508 "Invalid content type");
509 CHECK_AND_RETURN_RET_LOG(audioInterrupt.streamUsage >= STREAM_USAGE_UNKNOWN
510 && audioInterrupt.streamUsage <= STREAM_USAGE_NOTIFICATION_RINGTONE,
511 ERR_INVALID_PARAM, "Invalid stream usage");
512 CHECK_AND_RETURN_RET_LOG(audioInterrupt.streamType >= AudioStreamType::STREAM_VOICE_CALL
513 && audioInterrupt.streamType <= AudioStreamType::STREAM_ACCESSIBILITY,
514 ERR_INVALID_PARAM, "Invalid stream type");
515 return AudioPolicyManager::GetInstance().RequestAudioFocus(clientID, audioInterrupt);
516 }
517
AbandonAudioFocus(const AudioInterrupt & audioInterrupt)518 int32_t AudioSystemManager::AbandonAudioFocus(const AudioInterrupt &audioInterrupt)
519 {
520 uint32_t clientID = GetCallingPid();
521 MEDIA_INFO_LOG("AudioSystemManager:: AbandonAudioFocus client id: %{public}d", clientID);
522 CHECK_AND_RETURN_RET_LOG(audioInterrupt.contentType >= CONTENT_TYPE_UNKNOWN
523 && audioInterrupt.contentType <= CONTENT_TYPE_RINGTONE, ERR_INVALID_PARAM,
524 "Invalid content type");
525 CHECK_AND_RETURN_RET_LOG(audioInterrupt.streamUsage >= STREAM_USAGE_UNKNOWN
526 && audioInterrupt.streamUsage <= STREAM_USAGE_NOTIFICATION_RINGTONE,
527 ERR_INVALID_PARAM, "Invalid stream usage");
528 CHECK_AND_RETURN_RET_LOG(audioInterrupt.streamType >= AudioStreamType::STREAM_VOICE_CALL
529 && audioInterrupt.streamType <= AudioStreamType::STREAM_ACCESSIBILITY,
530 ERR_INVALID_PARAM, "Invalid stream type");
531 return AudioPolicyManager::GetInstance().AbandonAudioFocus(clientID, audioInterrupt);
532 }
533
AudioManagerInterruptCallbackImpl()534 AudioManagerInterruptCallbackImpl::AudioManagerInterruptCallbackImpl()
535 {
536 MEDIA_INFO_LOG("AudioManagerInterruptCallbackImpl constructor");
537 }
538
~AudioManagerInterruptCallbackImpl()539 AudioManagerInterruptCallbackImpl::~AudioManagerInterruptCallbackImpl()
540 {
541 MEDIA_DEBUG_LOG("AudioManagerInterruptCallbackImpl: instance destroy");
542 }
543
SaveCallback(const std::weak_ptr<AudioManagerCallback> & callback)544 void AudioManagerInterruptCallbackImpl::SaveCallback(const std::weak_ptr<AudioManagerCallback> &callback)
545 {
546 callback_ = callback;
547 }
548
OnInterrupt(const InterruptEventInternal & interruptEvent)549 void AudioManagerInterruptCallbackImpl::OnInterrupt(const InterruptEventInternal &interruptEvent)
550 {
551 cb_ = callback_.lock();
552 if (cb_ != nullptr) {
553 InterruptAction interruptAction = {};
554 interruptAction.actionType = (interruptEvent.eventType == INTERRUPT_TYPE_BEGIN)
555 ? TYPE_INTERRUPT : TYPE_ACTIVATED;
556 interruptAction.interruptType = interruptEvent.eventType;
557 interruptAction.interruptHint = interruptEvent.hintType;
558 interruptAction.activated = (interruptEvent.eventType == INTERRUPT_TYPE_BEGIN) ? false : true;
559 cb_->OnInterrupt(interruptAction);
560 MEDIA_DEBUG_LOG("AudioManagerInterruptCallbackImpl: OnInterrupt : Notify event to app complete");
561 } else {
562 MEDIA_ERR_LOG("AudioManagerInterruptCallbackImpl: callback is null");
563 }
564
565 return;
566 }
567 } // namespace AudioStandard
568 } // namespace OHOS
569