1/* 2 * Copyright (C) 2021-2024 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_control_manager.h" 17 18#include "call_ability_report_proxy.h" 19#include "call_control_manager.h" 20#include "call_dialog.h" 21#include "call_state_processor.h" 22#include "common_type.h" 23#include "distributed_call_manager.h" 24#include "telephony_log_wrapper.h" 25#include "audio_system_manager.h" 26#include "audio_routing_manager.h" 27#include "audio_device_info.h" 28#include "audio_info.h" 29#include "voip_call_connection.h" 30#include "settings_datashare_helper.h" 31#include "distributed_communication_manager.h" 32#include "os_account_manager.h" 33 34namespace OHOS { 35namespace Telephony { 36using namespace AudioStandard; 37constexpr int32_t DTMF_PLAY_TIME = 30; 38constexpr int32_t VOICE_TYPE = 0; 39constexpr int32_t CRS_TYPE = 2; 40constexpr int32_t CALL_ENDED_PLAY_TIME = 300; 41 42AudioControlManager::AudioControlManager() 43 : isLocalRingbackNeeded_(false), ring_(nullptr), tone_(nullptr), sound_(nullptr) 44{} 45 46AudioControlManager::~AudioControlManager() 47{ 48 DelayedSingleton<AudioProxy>::GetInstance()->UnsetDeviceChangeCallback(); 49 DelayedSingleton<AudioProxy>::GetInstance()->UnsetAudioPreferDeviceChangeCallback(); 50 DelayedSingleton<AudioProxy>::GetInstance()->UnsetAudioMicStateChangeCallback(); 51} 52 53void AudioControlManager::Init() 54{ 55 DelayedSingleton<AudioDeviceManager>::GetInstance()->Init(); 56 DelayedSingleton<AudioSceneProcessor>::GetInstance()->Init(); 57} 58 59void AudioControlManager::UpdateForegroundLiveCall() 60{ 61 int32_t callId = DelayedSingleton<CallStateProcessor>::GetInstance()->GetAudioForegroundLiveCall(); 62 if (callId == INVALID_CALLID) { 63 frontCall_ = nullptr; 64 DelayedSingleton<AudioProxy>::GetInstance()->SetMicrophoneMute(false); 65 TELEPHONY_LOGE("callId is invalid"); 66 return; 67 } 68 69 sptr<CallBase> liveCall = CallObjectManager::GetOneCallObject(callId); 70 if (liveCall == nullptr) { 71 TELEPHONY_LOGE("liveCall is nullptr"); 72 return; 73 } 74 if (liveCall->GetTelCallState() == TelCallState::CALL_STATUS_ACTIVE || 75 liveCall->GetTelCallState() == TelCallState::CALL_STATUS_DIALING || 76 liveCall->GetTelCallState() == TelCallState::CALL_STATUS_ALERTING) { 77 if (frontCall_ == nullptr) { 78 frontCall_ = liveCall; 79 } else { 80 int32_t frontCallId = frontCall_->GetCallID(); 81 int32_t liveCallId = liveCall->GetCallID(); 82 if (frontCallId != liveCallId) { 83 frontCall_ = liveCall; 84 } 85 } 86 bool frontCallMute = frontCall_->IsMuted(); 87 bool currentMute = DelayedSingleton<AudioProxy>::GetInstance()->IsMicrophoneMute(); 88 if (frontCallMute != currentMute) { 89 SetMute(frontCallMute); 90 } 91 } 92} 93 94void AudioControlManager::HandleCallStateUpdatedForVoip( 95 sptr<CallBase> &callObjectPtr, TelCallState priorState, TelCallState nextState) 96{ 97 TELEPHONY_LOGI("control audio for voip start, callId:%{public}d, priorState:%{public}d, nextState:%{public}d", 98 callObjectPtr->GetCallID(), priorState, nextState); 99 if (priorState == TelCallState::CALL_STATUS_INCOMING && nextState == TelCallState::CALL_STATUS_INCOMING) { 100 if (DelayedSingleton<CallObjectManager>::GetInstance()->GetVoipCallNum() == 1) { 101 AudioDevice device = { 102 .deviceType = AudioDeviceType::DEVICE_EARPIECE, 103 .address = { 0 }, 104 }; 105 if (DelayedSingleton<AudioProxy>::GetInstance()->GetPreferredOutputAudioDevice(device) == 106 TELEPHONY_SUCCESS) { 107 DelayedSingleton<AudioDeviceManager>::GetInstance()->SetCurrentAudioDevice(device.deviceType); 108 TELEPHONY_LOGI("control audio for voip finish, callId:%{public}d", callObjectPtr->GetCallID()); 109 } else { 110 TELEPHONY_LOGE("current audio device nullptr when control audio for voip"); 111 } 112 } 113 } 114} 115 116void AudioControlManager::CallStateUpdated( 117 sptr<CallBase> &callObjectPtr, TelCallState priorState, TelCallState nextState) 118{ 119 if (callObjectPtr == nullptr) { 120 TELEPHONY_LOGE("call object nullptr"); 121 return; 122 } 123 if (callObjectPtr->GetCallType() == CallType::TYPE_VOIP) { 124 HandleCallStateUpdatedForVoip(callObjectPtr, priorState, nextState); 125 return; 126 } 127 std::lock_guard<std::mutex> lock(mutex_); 128 if (totalCalls_.count(callObjectPtr) == 0) { 129 int32_t callId = callObjectPtr->GetCallID(); 130 TelCallState callState = callObjectPtr->GetTelCallState(); 131 TELEPHONY_LOGI("add new call, callid:%{public}d , callstate:%{public}d", callId, callState); 132 totalCalls_.insert(callObjectPtr); 133 } 134 HandleCallStateUpdated(callObjectPtr, priorState, nextState); 135 if (nextState == TelCallState::CALL_STATUS_DISCONNECTED && totalCalls_.count(callObjectPtr) > 0) { 136 totalCalls_.erase(callObjectPtr); 137 } 138 UpdateForegroundLiveCall(); 139} 140 141void AudioControlManager::VideoStateUpdated( 142 sptr<CallBase> &callObjectPtr, VideoStateType priorVideoState, VideoStateType nextVideoState) 143{ 144 if (callObjectPtr == nullptr) { 145 TELEPHONY_LOGE("call object nullptr"); 146 return; 147 } 148 if (callObjectPtr->GetCallType() != CallType::TYPE_IMS) { 149 TELEPHONY_LOGE("other call not need control audio"); 150 return; 151 } 152 if (DelayedSingleton<DistributedCommunicationManager>::GetInstance()->IsAudioOnSink()) { 153 TELEPHONY_LOGI("audio is on sink, not need control audio"); 154 return; 155 } 156 AudioDevice device = { 157 .deviceType = AudioDeviceType::DEVICE_SPEAKER, 158 .address = { 0 }, 159 }; 160 AudioDeviceType initDeviceType = GetInitAudioDeviceType(); 161 if (callObjectPtr->GetCrsType() == CRS_TYPE) { 162 AudioStandard::AudioRingerMode ringMode = DelayedSingleton<AudioProxy>::GetInstance()->GetRingerMode(); 163 if (ringMode != AudioStandard::AudioRingerMode::RINGER_MODE_NORMAL) { 164 if (initDeviceType == AudioDeviceType::DEVICE_WIRED_HEADSET || 165 initDeviceType == AudioDeviceType::DEVICE_BLUETOOTH_SCO) { 166 device.deviceType = initDeviceType; 167 } 168 } 169 TELEPHONY_LOGI("crs ring tone should be speaker"); 170 SetAudioDevice(device); 171 return; 172 } 173 CheckTypeAndSetAudioDevice(callObjectPtr, priorVideoState, nextVideoState, initDeviceType, device); 174} 175 176void AudioControlManager::CheckTypeAndSetAudioDevice(sptr<CallBase> &callObjectPtr, VideoStateType priorVideoState, 177 VideoStateType nextVideoState, AudioDeviceType &initDeviceType, AudioDevice &device) 178{ 179 TelCallState telCallState = callObjectPtr->GetTelCallState(); 180 if (!IsVideoCall(priorVideoState) && IsVideoCall(nextVideoState) && 181 (telCallState != TelCallState::CALL_STATUS_INCOMING && telCallState != TelCallState::CALL_STATUS_WAITING)) { 182 if (callObjectPtr->GetOriginalCallType() == VOICE_TYPE && 183 (telCallState == TelCallState::CALL_STATUS_DIALING || telCallState == TelCallState::CALL_STATUS_ALERTING)) { 184 TELEPHONY_LOGI("before modify set device to EARPIECE, now not set"); 185 return; 186 } 187 if (initDeviceType == AudioDeviceType::DEVICE_WIRED_HEADSET || 188 initDeviceType == AudioDeviceType::DEVICE_BLUETOOTH_SCO || 189 initDeviceType == AudioDeviceType::DEVICE_DISTRIBUTED_AUTOMOTIVE) { 190 device.deviceType = initDeviceType; 191 } 192 TELEPHONY_LOGI("set device type, type: %{public}d", static_cast<int32_t>(device.deviceType)); 193 SetAudioDevice(device); 194 } else if (!isSetAudioDeviceByUser_ && IsVideoCall(priorVideoState) && !IsVideoCall(nextVideoState)) { 195 device.deviceType = AudioDeviceType::DEVICE_EARPIECE; 196 if (initDeviceType == AudioDeviceType::DEVICE_WIRED_HEADSET || 197 initDeviceType == AudioDeviceType::DEVICE_BLUETOOTH_SCO || 198 initDeviceType == AudioDeviceType::DEVICE_DISTRIBUTED_AUTOMOTIVE) { 199 device.deviceType = initDeviceType; 200 } 201 TELEPHONY_LOGI("set device type, type: %{public}d", static_cast<int32_t>(device.deviceType)); 202 SetAudioDevice(device); 203 } 204} 205 206void AudioControlManager::UpdateDeviceTypeForVideoOrSatelliteCall() 207{ 208 sptr<CallBase> foregroundCall = CallObjectManager::GetForegroundCall(); 209 if (foregroundCall == nullptr) { 210 TELEPHONY_LOGE("call object nullptr"); 211 return; 212 } 213 if (foregroundCall->GetCallType() != CallType::TYPE_IMS || 214 foregroundCall->GetCallType() != CallType::TYPE_SATELLITE) { 215 TELEPHONY_LOGE("other call not need control audio"); 216 return; 217 } 218 AudioDevice device = { 219 .deviceType = AudioDeviceType::DEVICE_SPEAKER, 220 .address = { 0 }, 221 }; 222 AudioDeviceType initDeviceType = GetInitAudioDeviceType(); 223 if (IsVideoCall(foregroundCall->GetVideoStateType()) || 224 foregroundCall->GetCallType() == CallType::TYPE_SATELLITE) { 225 if (initDeviceType == AudioDeviceType::DEVICE_WIRED_HEADSET || 226 initDeviceType == AudioDeviceType::DEVICE_BLUETOOTH_SCO || 227 initDeviceType == AudioDeviceType::DEVICE_DISTRIBUTED_AUTOMOTIVE) { 228 device.deviceType = initDeviceType; 229 } 230 TELEPHONY_LOGI("set device type, type: %{public}d", static_cast<int32_t>(device.deviceType)); 231 SetAudioDevice(device); 232 } 233} 234 235void AudioControlManager::UpdateDeviceTypeForCrs() 236{ 237 sptr<CallBase> incomingCall = CallObjectManager::GetOneCallObject(CallRunningState::CALL_RUNNING_STATE_RINGING); 238 if (incomingCall == nullptr || incomingCall->IsAnsweredCall()) { 239 return; 240 } 241 if (incomingCall->GetCrsType() == CRS_TYPE) { 242 AudioDevice device = { 243 .deviceType = AudioDeviceType::DEVICE_SPEAKER, 244 .address = { 0 }, 245 }; 246 AudioStandard::AudioRingerMode ringMode = DelayedSingleton<AudioProxy>::GetInstance()->GetRingerMode(); 247 if (ringMode != AudioStandard::AudioRingerMode::RINGER_MODE_NORMAL) { 248 AudioDeviceType initDeviceType = GetInitAudioDeviceType(); 249 if (initDeviceType == AudioDeviceType::DEVICE_WIRED_HEADSET || 250 initDeviceType == AudioDeviceType::DEVICE_BLUETOOTH_SCO) { 251 device.deviceType = initDeviceType; 252 } 253 } 254 TELEPHONY_LOGI("crs ring tone should be speaker"); 255 SetAudioDevice(device); 256 } 257} 258 259void AudioControlManager::UpdateDeviceTypeForVideoDialing() 260{ 261 sptr<CallBase> dialingCall = CallObjectManager::GetOneCallObject(CallRunningState::CALL_RUNNING_STATE_DIALING); 262 if (dialingCall == nullptr) { 263 return; 264 } 265 if (dialingCall->GetVideoStateType() == VideoStateType::TYPE_VIDEO) { 266 AudioDevice device = { 267 .deviceType = AudioDeviceType::DEVICE_SPEAKER, 268 .address = { 0 }, 269 }; 270 AudioDeviceType initDeviceType = GetInitAudioDeviceType(); 271 if (initDeviceType == AudioDeviceType::DEVICE_WIRED_HEADSET || 272 initDeviceType == AudioDeviceType::DEVICE_BLUETOOTH_SCO) { 273 device.deviceType = initDeviceType; 274 } 275 TELEPHONY_LOGI("dialing video call should be speaker"); 276 SetAudioDevice(device); 277 } 278} 279 280void AudioControlManager::IncomingCallActivated(sptr<CallBase> &callObjectPtr) {} 281 282void AudioControlManager::IncomingCallHungUp(sptr<CallBase> &callObjectPtr, bool isSendSms, std::string content) 283{ 284 if (callObjectPtr == nullptr) { 285 TELEPHONY_LOGE("call object ptr nullptr"); 286 return; 287 } 288 StopWaitingTone(); 289} 290 291void AudioControlManager::HandleCallStateUpdated( 292 sptr<CallBase> &callObjectPtr, TelCallState priorState, TelCallState nextState) 293{ 294 if (callObjectPtr == nullptr) { 295 TELEPHONY_LOGE("call object is nullptr"); 296 return; 297 } 298 if (nextState == TelCallState::CALL_STATUS_ANSWERED) { 299 TELEPHONY_LOGI("user answered, mute ringer instead of release renderer"); 300 if (priorState == TelCallState::CALL_STATUS_INCOMING) { 301 DelayedSingleton<CallStateProcessor>::GetInstance()->DeleteCall(callObjectPtr->GetCallID(), priorState); 302 } 303 MuteRinger(); 304 return; 305 } 306 HandleNextState(callObjectPtr, nextState); 307 if (priorState == nextState) { 308 TELEPHONY_LOGI("prior state equals next state"); 309 return; 310 } 311 HandlePriorState(callObjectPtr, priorState); 312} 313 314void AudioControlManager::HandleNextState(sptr<CallBase> &callObjectPtr, TelCallState nextState) 315{ 316 AudioEvent event = AudioEvent::UNKNOWN_EVENT; 317 DelayedSingleton<CallStateProcessor>::GetInstance()->AddCall(callObjectPtr->GetCallID(), nextState); 318 switch (nextState) { 319 case TelCallState::CALL_STATUS_DIALING: 320 event = AudioEvent::NEW_DIALING_CALL; 321 audioInterruptState_ = AudioInterruptState::INTERRUPT_STATE_RINGING; 322 break; 323 case TelCallState::CALL_STATUS_ALERTING: 324 event = AudioEvent::NEW_ALERTING_CALL; 325 audioInterruptState_ = AudioInterruptState::INTERRUPT_STATE_RINGING; 326 break; 327 case TelCallState::CALL_STATUS_ACTIVE: 328 HandleNewActiveCall(callObjectPtr); 329 audioInterruptState_ = AudioInterruptState::INTERRUPT_STATE_ACTIVATED; 330 break; 331 case TelCallState::CALL_STATUS_WAITING: 332 case TelCallState::CALL_STATUS_INCOMING: 333 event = AudioEvent::NEW_INCOMING_CALL; 334 audioInterruptState_ = AudioInterruptState::INTERRUPT_STATE_RINGING; 335 break; 336 case TelCallState::CALL_STATUS_DISCONNECTING: 337 case TelCallState::CALL_STATUS_DISCONNECTED: 338 if (isCrsVibrating_) { 339 DelayedSingleton<AudioProxy>::GetInstance()->StopVibrator(); 340 isCrsVibrating_ = false; 341 } 342 audioInterruptState_ = AudioInterruptState::INTERRUPT_STATE_DEACTIVATED; 343 break; 344 default: 345 break; 346 } 347 if (event == AudioEvent::UNKNOWN_EVENT) { 348 return; 349 } 350 DelayedSingleton<AudioSceneProcessor>::GetInstance()->ProcessEvent(event); 351} 352 353void AudioControlManager::HandlePriorState(sptr<CallBase> &callObjectPtr, TelCallState priorState) 354{ 355 AudioEvent event = AudioEvent::UNKNOWN_EVENT; 356 DelayedSingleton<CallStateProcessor>::GetInstance()->DeleteCall(callObjectPtr->GetCallID(), priorState); 357 int32_t stateNumber = DelayedSingleton<CallStateProcessor>::GetInstance()->GetCallNumber(priorState); 358 switch (priorState) { 359 case TelCallState::CALL_STATUS_DIALING: 360 if (stateNumber == EMPTY_VALUE) { 361 event = AudioEvent::NO_MORE_DIALING_CALL; 362 } 363 break; 364 case TelCallState::CALL_STATUS_ALERTING: 365 if (stateNumber == EMPTY_VALUE) { 366 event = AudioEvent::NO_MORE_ALERTING_CALL; 367 } 368 break; 369 case TelCallState::CALL_STATUS_INCOMING: 370 case TelCallState::CALL_STATUS_WAITING: 371 ProcessAudioWhenCallActive(callObjectPtr); 372 event = AudioEvent::NO_MORE_INCOMING_CALL; 373 break; 374 case TelCallState::CALL_STATUS_ACTIVE: 375 if (stateNumber == EMPTY_VALUE) { 376 event = AudioEvent::NO_MORE_ACTIVE_CALL; 377 } 378 break; 379 case TelCallState::CALL_STATUS_HOLDING: 380 if (stateNumber == EMPTY_VALUE) { 381 event = AudioEvent::NO_MORE_HOLDING_CALL; 382 } 383 break; 384 default: 385 break; 386 } 387 if (event == AudioEvent::UNKNOWN_EVENT) { 388 return; 389 } 390 DelayedSingleton<AudioSceneProcessor>::GetInstance()->ProcessEvent(event); 391} 392 393void AudioControlManager::ProcessAudioWhenCallActive(sptr<CallBase> &callObjectPtr) 394{ 395 if (callObjectPtr->GetCallRunningState() == CallRunningState::CALL_RUNNING_STATE_ACTIVE) { 396 if (isCrsVibrating_) { 397 DelayedSingleton<AudioProxy>::GetInstance()->StopVibrator(); 398 isCrsVibrating_ = false; 399 } 400 int ringCallCount = CallObjectManager::GetCallNumByRunningState(CallRunningState::CALL_RUNNING_STATE_RINGING); 401 if ((CallObjectManager::GetCurrentCallNum() - ringCallCount) < MIN_MULITY_CALL_COUNT) { 402 StopSoundtone(); 403 PlaySoundtone(); 404 } 405 UpdateDeviceTypeForVideoOrSatelliteCall(); 406 } 407} 408 409void AudioControlManager::HandleNewActiveCall(sptr<CallBase> &callObjectPtr) 410{ 411 CallType callType = callObjectPtr->GetCallType(); 412 AudioEvent event = AudioEvent::UNKNOWN_EVENT; 413 switch (callType) { 414 case CallType::TYPE_CS: 415 case CallType::TYPE_SATELLITE: 416 event = AudioEvent::NEW_ACTIVE_CS_CALL; 417 break; 418 case CallType::TYPE_IMS: 419 event = AudioEvent::NEW_ACTIVE_IMS_CALL; 420 break; 421 case CallType::TYPE_OTT: 422 event = AudioEvent::NEW_ACTIVE_OTT_CALL; 423 break; 424 default: 425 break; 426 } 427 if (event == AudioEvent::UNKNOWN_EVENT) { 428 return; 429 } 430 DelayedSingleton<AudioSceneProcessor>::GetInstance()->ProcessEvent(event); 431} 432 433/** 434 * @param device , audio device 435 * usually called by the ui interaction , in purpose of switching to another audio device 436 */ 437int32_t AudioControlManager::SetAudioDevice(const AudioDevice &device) 438{ 439 return SetAudioDevice(device, false); 440} 441 442/** 443 * @param device , audio device 444 * @param isByUser , call from callui or not 445 * usually called by the ui interaction , in purpose of switching to another audio device 446 */ 447int32_t AudioControlManager::SetAudioDevice(const AudioDevice &device, bool isByUser) 448{ 449 TELEPHONY_LOGI("set audio device, type: %{public}d", static_cast<int32_t>(device.deviceType)); 450 AudioDeviceType audioDeviceType = AudioDeviceType::DEVICE_UNKNOWN; 451 if (CallObjectManager::HasSatelliteCallExist() && device.deviceType == AudioDeviceType::DEVICE_EARPIECE) { 452 DelayedSingleton<CallDialog>::GetInstance()->DialogConnectExtension("SATELLITE_CALL_NOT_SUPPORT_EARPIECE"); 453 return CALL_ERR_AUDIO_SET_AUDIO_DEVICE_FAILED; 454 } 455 isSetAudioDeviceByUser_ = isByUser; 456 switch (device.deviceType) { 457 case AudioDeviceType::DEVICE_SPEAKER: 458 case AudioDeviceType::DEVICE_EARPIECE: 459 case AudioDeviceType::DEVICE_WIRED_HEADSET: 460 audioDeviceType = device.deviceType; 461 break; 462 case AudioDeviceType::DEVICE_DISTRIBUTED_AUTOMOTIVE: 463 case AudioDeviceType::DEVICE_DISTRIBUTED_PHONE: 464 case AudioDeviceType::DEVICE_DISTRIBUTED_PAD: 465 case AudioDeviceType::DEVICE_DISTRIBUTED_PC: 466 return HandleDistributeAudioDevice(device); 467 case AudioDeviceType::DEVICE_BLUETOOTH_SCO: { 468 std::string address = device.address; 469 std::unique_ptr<AudioStandard::AudioDeviceDescriptor> activeBluetoothDevice = 470 AudioStandard::AudioRoutingManager::GetInstance()->GetActiveBluetoothDevice(); 471 if (address.empty() && activeBluetoothDevice != nullptr && !activeBluetoothDevice->macAddress_.empty()) { 472 address = activeBluetoothDevice->macAddress_; 473 } 474 AudioSystemManager* audioSystemManager = AudioSystemManager::GetInstance(); 475 int32_t ret = audioSystemManager->SetCallDeviceActive(ActiveDeviceType::BLUETOOTH_SCO, 476 true, address); 477 if (ret != 0) { 478 TELEPHONY_LOGE("SetCallDeviceActive failed"); 479 return CALL_ERR_AUDIO_SET_AUDIO_DEVICE_FAILED; 480 } 481 audioDeviceType = device.deviceType; 482 break; 483 } 484 default: 485 break; 486 } 487 return SwitchAudioDevice(audioDeviceType); 488} 489 490int32_t AudioControlManager::SwitchAudioDevice(AudioDeviceType audioDeviceType) 491{ 492 if (audioDeviceType != AudioDeviceType::DEVICE_UNKNOWN) { 493 if (DelayedSingleton<DistributedCommunicationManager>::GetInstance()->IsAudioOnSink()) { 494 DelayedSingleton<DistributedCommunicationManager>::GetInstance()->SwitchToSourceDevice(); 495 } 496 if (DelayedSingleton<DistributedCallManager>::GetInstance()->IsDCallDeviceSwitchedOn()) { 497 DelayedSingleton<DistributedCallManager>::GetInstance()->SwitchOffDCallDeviceSync(); 498 } 499 if (DelayedSingleton<AudioDeviceManager>::GetInstance()->SwitchDevice(audioDeviceType)) { 500 return TELEPHONY_SUCCESS; 501 } 502 } 503 return CALL_ERR_AUDIO_SET_AUDIO_DEVICE_FAILED; 504} 505 506int32_t AudioControlManager::HandleDistributeAudioDevice(const AudioDevice &device) 507{ 508 if (DelayedSingleton<DistributedCommunicationManager>::GetInstance()->IsDistributedDev(device)) { 509 if (DelayedSingleton<DistributedCommunicationManager>::GetInstance()->SwitchToSinkDevice(device)) { 510 return TELEPHONY_SUCCESS; 511 } 512 return CALL_ERR_AUDIO_SET_AUDIO_DEVICE_FAILED; 513 } 514 if (!DelayedSingleton<DistributedCallManager>::GetInstance()->IsDCallDeviceSwitchedOn()) { 515 if (DelayedSingleton<DistributedCallManager>::GetInstance()->SwitchOnDCallDeviceSync(device)) { 516 return TELEPHONY_SUCCESS; 517 } 518 return CALL_ERR_AUDIO_SET_AUDIO_DEVICE_FAILED; 519 } 520 return TELEPHONY_SUCCESS; 521} 522 523bool AudioControlManager::PlayRingtone() 524{ 525 if (!ShouldPlayRingtone()) { 526 TELEPHONY_LOGE("should not play ringtone"); 527 return false; 528 } 529 ring_ = std::make_unique<Ring>(); 530 if (ring_ == nullptr) { 531 TELEPHONY_LOGE("create ring object failed"); 532 return false; 533 } 534 sptr<CallBase> incomingCall = CallObjectManager::GetOneCallObject(CallRunningState::CALL_RUNNING_STATE_RINGING); 535 if (incomingCall == nullptr) { 536 TELEPHONY_LOGE("incomingCall is nullptr"); 537 return false; 538 } 539 CallAttributeInfo info; 540 incomingCall->GetCallAttributeBaseInfo(info); 541 AudioStandard::AudioRingerMode ringMode = DelayedSingleton<AudioProxy>::GetInstance()->GetRingerMode(); 542 if (incomingCall->GetCrsType() == CRS_TYPE) { 543 if (!isCrsVibrating_ && (ringMode != AudioStandard::AudioRingerMode::RINGER_MODE_SILENT)) { 544 if (ringMode == AudioStandard::AudioRingerMode::RINGER_MODE_VIBRATE || IsRingingVibrateModeOn()) { 545 isCrsVibrating_ = (DelayedSingleton<AudioProxy>::GetInstance()->StartVibrator() == TELEPHONY_SUCCESS); 546 } 547 } 548 if ((ringMode == AudioStandard::AudioRingerMode::RINGER_MODE_NORMAL) || IsBtOrWireHeadPlugin()) { 549 if (PlaySoundtone()) { 550 TELEPHONY_LOGI("play soundtone success"); 551 return true; 552 } 553 return false; 554 } 555 TELEPHONY_LOGI("type_crs but not play ringtone"); 556 return false; 557 } 558 if (ring_->Play(info.accountId) != TELEPHONY_SUCCESS) { 559 TELEPHONY_LOGE("play ringtone failed"); 560 return false; 561 } 562 TELEPHONY_LOGI("play ringtone success"); 563 return true; 564} 565 566bool AudioControlManager::IsDistributeCallSinkStatus() 567{ 568 std::string dcStatus = ""; 569 auto settingHelper = SettingsDataShareHelper::GetInstance(); 570 if (settingHelper != nullptr) { 571 OHOS::Uri settingUri(SettingsDataShareHelper::SETTINGS_DATASHARE_URI); 572 settingHelper->Query(settingUri, "distributed_modem_state", dcStatus); 573 } 574 TELEPHONY_LOGI("distributed communication modem status: %{public}s", dcStatus.c_str()); 575 if (dcStatus != "1_sink") { 576 return false; 577 } 578 return true; 579} 580 581bool AudioControlManager::PlaySoundtone() 582{ 583 if (IsDistributeCallSinkStatus()) { 584 TELEPHONY_LOGI("distribute call sink status, no need to play sound tone"); 585 return true; 586 } 587 if (soundState_ == SoundState::SOUNDING) { 588 TELEPHONY_LOGE("should not play soundTone"); 589 return false; 590 } 591 if (sound_ == nullptr) { 592 sound_ = std::make_unique<Sound>(); 593 if (sound_ == nullptr) { 594 TELEPHONY_LOGE("create sound object failed"); 595 return false; 596 } 597 } 598 if (sound_->Play() != TELEPHONY_SUCCESS) { 599 TELEPHONY_LOGE("play soundtone failed"); 600 return false; 601 } 602 TELEPHONY_LOGI("play soundtone success"); 603 return true; 604} 605 606bool AudioControlManager::StopSoundtone() 607{ 608 if (soundState_ == SoundState::STOPPED) { 609 TELEPHONY_LOGI("soundtone already stopped"); 610 return true; 611 } 612 if (sound_ == nullptr) { 613 TELEPHONY_LOGE("sound_ is nullptr"); 614 return false; 615 } 616 if (sound_->Stop() != TELEPHONY_SUCCESS) { 617 TELEPHONY_LOGE("stop soundtone failed"); 618 return false; 619 } 620 sound_->ReleaseRenderer(); 621 TELEPHONY_LOGI("stop soundtone success"); 622 return true; 623} 624 625bool AudioControlManager::StopRingtone() 626{ 627 if (ringState_ == RingState::STOPPED) { 628 TELEPHONY_LOGI("ringtone already stopped"); 629 return true; 630 } 631 if (ring_ == nullptr) { 632 TELEPHONY_LOGE("ring_ is nullptr"); 633 return false; 634 } 635 if (ring_->Stop() != TELEPHONY_SUCCESS) { 636 TELEPHONY_LOGE("stop ringtone failed"); 637 return false; 638 } 639 ring_->ReleaseRenderer(); 640 TELEPHONY_LOGI("stop ringtone success"); 641 return true; 642} 643 644/** 645 * while audio state changed , maybe need to reinitialize the audio device 646 * in order to get the initialization status of audio device , need to consider varieties of audio conditions 647 */ 648AudioDeviceType AudioControlManager::GetInitAudioDeviceType() const 649{ 650 if (audioInterruptState_ == AudioInterruptState::INTERRUPT_STATE_DEACTIVATED) { 651 return AudioDeviceType::DEVICE_DISABLE; 652 } else { 653 if (DelayedSingleton<DistributedCommunicationManager>::GetInstance()->IsConnected()) { 654 AudioDevice device = { 655 .deviceType = AudioDeviceType::DEVICE_UNKNOWN, 656 }; 657 (void)DelayedSingleton<AudioProxy>::GetInstance()->GetPreferredOutputAudioDevice(device); 658 return device.deviceType; 659 } 660 661 /** 662 * Init audio device type according to the priority in different call state: 663 * In voice call state, bluetooth sco > wired headset > earpiece > speaker 664 * In video call state, bluetooth sco > wired headset > speaker > earpiece 665 */ 666 if (AudioDeviceManager::IsDistributedCallConnected()) { 667 return AudioDeviceType::DEVICE_DISTRIBUTED_AUTOMOTIVE; 668 } 669 if (AudioDeviceManager::IsBtActived()) { 670 return AudioDeviceType::DEVICE_BLUETOOTH_SCO; 671 } 672 if (AudioDeviceManager::IsWiredHeadsetConnected()) { 673 return AudioDeviceType::DEVICE_WIRED_HEADSET; 674 } 675 sptr<CallBase> liveCall = CallObjectManager::GetForegroundCall(); 676 if (liveCall != nullptr && (liveCall->GetVideoStateType() == VideoStateType::TYPE_VIDEO || 677 liveCall->GetCallType() == CallType::TYPE_SATELLITE)) { 678 TELEPHONY_LOGI("current video or satellite call speaker is active"); 679 return AudioDeviceType::DEVICE_SPEAKER; 680 } 681 if (AudioDeviceManager::IsEarpieceAvailable()) { 682 return AudioDeviceType::DEVICE_EARPIECE; 683 } 684 return AudioDeviceType::DEVICE_SPEAKER; 685 } 686} 687 688/** 689 * @param isMute , mute state 690 * usually called by the ui interaction , mute or unmute microphone 691 */ 692int32_t AudioControlManager::SetMute(bool isMute) 693{ 694 bool hasCall = DelayedSingleton<CallControlManager>::GetInstance()->HasCall(); 695 if (!hasCall) { 696 TELEPHONY_LOGE("no call exists, set mute failed"); 697 return CALL_ERR_AUDIO_SETTING_MUTE_FAILED; 698 } 699 bool enabled = false; 700 if ((DelayedSingleton<CallControlManager>::GetInstance()->HasEmergency(enabled) == TELEPHONY_SUCCESS) && enabled) { 701 isMute = false; 702 } 703 if (!DelayedSingleton<AudioProxy>::GetInstance()->SetMicrophoneMute(isMute)) { 704 TELEPHONY_LOGE("set mute failed"); 705 return CALL_ERR_AUDIO_SETTING_MUTE_FAILED; 706 } 707 DelayedSingleton<AudioDeviceManager>::GetInstance()->ReportAudioDeviceInfo(); 708 if (frontCall_ == nullptr) { 709 TELEPHONY_LOGE("frontCall_ is nullptr"); 710 return TELEPHONY_ERR_LOCAL_PTR_NULL; 711 } 712 bool muted = DelayedSingleton<AudioProxy>::GetInstance()->IsMicrophoneMute(); 713 frontCall_->SetMicPhoneState(muted); 714 TELEPHONY_LOGI("SetMute success callId:%{public}d, mute:%{public}d", frontCall_->GetCallID(), muted); 715 return TELEPHONY_SUCCESS; 716} 717 718int32_t AudioControlManager::MuteRinger() 719{ 720 sptr<CallBase> incomingCall = CallObjectManager::GetOneCallObject(CallRunningState::CALL_RUNNING_STATE_RINGING); 721 if (incomingCall != nullptr) { 722 if (incomingCall->GetCrsType() == CRS_TYPE) { 723 TELEPHONY_LOGI("Mute network ring tone."); 724 MuteNetWorkRingTone(); 725 } 726 } 727 SendMuteRingEvent(); 728 if (ringState_ == RingState::STOPPED) { 729 TELEPHONY_LOGI("ring already stopped"); 730 return TELEPHONY_SUCCESS; 731 } 732 if (ring_ == nullptr) { 733 TELEPHONY_LOGE("ring is nullptr"); 734 return CALL_ERR_AUDIO_SETTING_MUTE_FAILED; 735 } 736 if (ring_->SetMute() != TELEPHONY_SUCCESS) { 737 TELEPHONY_LOGE("SetMute fail"); 738 return CALL_ERR_AUDIO_SETTING_MUTE_FAILED; 739 } 740 TELEPHONY_LOGI("mute ring success"); 741 return TELEPHONY_SUCCESS; 742} 743 744void AudioControlManager::SendMuteRingEvent() 745{ 746 CallEventInfo eventInfo; 747 eventInfo.eventId = CallAbilityEventId::EVENT_MUTE_RING; 748 DelayedSingleton<CallAbilityReportProxy>::GetInstance()->CallEventUpdated(eventInfo); 749} 750 751void AudioControlManager::PlayCallEndedTone(CallEndedType type) 752{ 753 int32_t state; 754 DelayedSingleton<CallControlManager>::GetInstance()->GetVoIPCallState(state); 755 if (state != static_cast<int32_t>(CallStateToApp::CALL_STATE_IDLE)) { 756 TELEPHONY_LOGI("not play callEndTone when has voip call"); 757 return; 758 } 759 AudioStandard::AudioRingerMode ringMode = DelayedSingleton<AudioProxy>::GetInstance()->GetRingerMode(); 760 if (ringMode != AudioStandard::AudioRingerMode::RINGER_MODE_NORMAL) { 761 TELEPHONY_LOGE("ringer mode is not normal"); 762 return; 763 } 764 switch (type) { 765 case CallEndedType::PHONE_IS_BUSY: 766 PlayCallTone(ToneDescriptor::TONE_ENGAGED); 767 break; 768 case CallEndedType::CALL_ENDED_NORMALLY: 769 if (toneState_ == ToneState::TONEING) { 770 StopCallTone(); 771 } 772 TELEPHONY_LOGI("play call ended tone"); 773 if (PlayCallTone(ToneDescriptor::TONE_FINISHED) != TELEPHONY_SUCCESS) { 774 TELEPHONY_LOGE("play call ended tone failed"); 775 return; 776 } 777 toneState_ = ToneState::CALLENDED; 778 std::this_thread::sleep_for(std::chrono::milliseconds(CALL_ENDED_PLAY_TIME)); 779 toneState_ = ToneState::TONEING; 780 if (StopCallTone() != TELEPHONY_SUCCESS) { 781 TELEPHONY_LOGE("stop call ended tone failed"); 782 return; 783 } 784 break; 785 case CallEndedType::UNKNOWN: 786 PlayCallTone(ToneDescriptor::TONE_UNKNOWN); 787 break; 788 case CallEndedType::INVALID_NUMBER: 789 PlayCallTone(ToneDescriptor::TONE_INVALID_NUMBER); 790 break; 791 default: 792 break; 793 } 794} 795 796std::set<sptr<CallBase>> AudioControlManager::GetCallList() 797{ 798 std::lock_guard<std::mutex> lock(mutex_); 799 return totalCalls_; 800} 801 802sptr<CallBase> AudioControlManager::GetCurrentActiveCall() 803{ 804 int32_t callId = DelayedSingleton<CallStateProcessor>::GetInstance()->GetCurrentActiveCall(); 805 if (callId != INVALID_CALLID) { 806 return GetCallBase(callId); 807 } 808 return nullptr; 809} 810 811sptr<CallBase> AudioControlManager::GetCallBase(int32_t callId) 812{ 813 sptr<CallBase> callBase = nullptr; 814 std::lock_guard<std::mutex> lock(mutex_); 815 for (auto &call : totalCalls_) { 816 if (call->GetCallID() == callId) { 817 callBase = call; 818 break; 819 } 820 } 821 return callBase; 822} 823 824bool AudioControlManager::IsEmergencyCallExists() 825{ 826 std::lock_guard<std::mutex> lock(mutex_); 827 for (auto call : totalCalls_) { 828 if (call->GetEmergencyState()) { 829 return true; 830 } 831 } 832 return false; 833} 834 835AudioInterruptState AudioControlManager::GetAudioInterruptState() 836{ 837 return audioInterruptState_; 838} 839 840void AudioControlManager::SetVolumeAudible() 841{ 842 DelayedSingleton<AudioProxy>::GetInstance()->SetVolumeAudible(); 843} 844 845void AudioControlManager::SetRingState(RingState state) 846{ 847 ringState_ = state; 848} 849 850void AudioControlManager::SetSoundState(SoundState state) 851{ 852 soundState_ = state; 853} 854 855void AudioControlManager::SetToneState(ToneState state) 856{ 857 std::lock_guard<std::recursive_mutex> lock(toneStateLock_); 858 toneState_ = state; 859} 860 861void AudioControlManager::SetLocalRingbackNeeded(bool isNeeded) 862{ 863 if (isLocalRingbackNeeded_ && !isNeeded) { 864 StopRingback(); 865 } 866 isLocalRingbackNeeded_ = isNeeded; 867} 868 869bool AudioControlManager::IsNumberAllowed(const std::string &phoneNum) 870{ 871 // check whether the phone number is allowed or not , should not ring if number is not allowed 872 return true; 873} 874 875bool AudioControlManager::ShouldPlayRingtone() const 876{ 877 auto processor = DelayedSingleton<CallStateProcessor>::GetInstance(); 878 int32_t alertingCallNum = processor->GetCallNumber(TelCallState::CALL_STATUS_ALERTING); 879 int32_t incomingCallNum = processor->GetCallNumber(TelCallState::CALL_STATUS_INCOMING); 880 if (incomingCallNum == EMPTY_VALUE || alertingCallNum > EMPTY_VALUE || ringState_ == RingState::RINGING 881 || (soundState_ == SoundState::SOUNDING && CallObjectManager::HasIncomingCallCrsType())) { 882 return false; 883 } 884 return true; 885} 886 887bool AudioControlManager::IsAudioActivated() const 888{ 889 return audioInterruptState_ == AudioInterruptState::INTERRUPT_STATE_ACTIVATED || 890 audioInterruptState_ == AudioInterruptState::INTERRUPT_STATE_RINGING; 891} 892 893int32_t AudioControlManager::PlayCallTone(ToneDescriptor type) 894{ 895 std::lock_guard<std::recursive_mutex> lock(toneStateLock_); 896 if (toneState_ == ToneState::TONEING) { 897 TELEPHONY_LOGE("callTone is already playing"); 898 return CALL_ERR_AUDIO_TONE_PLAY_FAILED; 899 } 900 toneState_ = ToneState::TONEING; 901 tone_ = std::make_unique<Tone>(type); 902 if (tone_ == nullptr) { 903 TELEPHONY_LOGE("create tone failed"); 904 return TELEPHONY_ERR_LOCAL_PTR_NULL; 905 } 906 if (tone_->Play() != TELEPHONY_SUCCESS) { 907 TELEPHONY_LOGE("play calltone failed"); 908 return CALL_ERR_AUDIO_TONE_PLAY_FAILED; 909 } 910 TELEPHONY_LOGI("play calltone success"); 911 return TELEPHONY_SUCCESS; 912} 913 914int32_t AudioControlManager::StopCallTone() 915{ 916 std::lock_guard<std::recursive_mutex> lock(toneStateLock_); 917 if (toneState_ == ToneState::STOPPED) { 918 TELEPHONY_LOGI("tone is already stopped"); 919 return TELEPHONY_SUCCESS; 920 } 921 if (toneState_ == ToneState::CALLENDED) { 922 TELEPHONY_LOGE("call ended tone is running"); 923 return CALL_ERR_AUDIO_TONE_STOP_FAILED; 924 } 925 if (tone_ == nullptr) { 926 TELEPHONY_LOGE("tone_ is nullptr"); 927 return TELEPHONY_ERR_LOCAL_PTR_NULL; 928 } 929 if (tone_->Stop() != TELEPHONY_SUCCESS) { 930 TELEPHONY_LOGE("stop calltone failed"); 931 return CALL_ERR_AUDIO_TONE_STOP_FAILED; 932 } 933 tone_->ReleaseRenderer(); 934 toneState_ = ToneState::STOPPED; 935 TELEPHONY_LOGI("stop call tone success"); 936 return TELEPHONY_SUCCESS; 937} 938 939bool AudioControlManager::IsTonePlaying() 940{ 941 std::lock_guard<std::recursive_mutex> lock(toneStateLock_); 942 return toneState_ == ToneState::TONEING; 943} 944 945bool AudioControlManager::IsCurrentRinging() const 946{ 947 return ringState_ == RingState::RINGING; 948} 949 950int32_t AudioControlManager::PlayRingback() 951{ 952 if (!isLocalRingbackNeeded_) { 953 return CALL_ERR_AUDIO_TONE_PLAY_FAILED; 954 } 955 return PlayCallTone(ToneDescriptor::TONE_RINGBACK); 956} 957 958int32_t AudioControlManager::StopRingback() 959{ 960 return StopCallTone(); 961} 962 963int32_t AudioControlManager::PlayWaitingTone() 964{ 965 return PlayCallTone(ToneDescriptor::TONE_WAITING); 966} 967 968int32_t AudioControlManager::StopWaitingTone() 969{ 970 if (tone_ != nullptr && tone_->getCurrentToneType() == ToneDescriptor::TONE_WAITING) { 971 return StopCallTone(); 972 } 973 return TELEPHONY_SUCCESS; 974} 975 976int32_t AudioControlManager::PlayDtmfTone(char str) 977{ 978 ToneDescriptor dtmfTone = Tone::ConvertDigitToTone(str); 979 std::unique_ptr<Tone> tone = std::make_unique<Tone>(dtmfTone); 980 if (tone == nullptr) { 981 TELEPHONY_LOGE("create dtmf tone failed"); 982 return TELEPHONY_ERR_LOCAL_PTR_NULL; 983 } 984 if (tone->Play() != TELEPHONY_SUCCESS) { 985 TELEPHONY_LOGE("play dtmftone failed"); 986 return CALL_ERR_AUDIO_TONE_PLAY_FAILED; 987 } 988 TELEPHONY_LOGI("play dtmftone success"); 989 std::this_thread::sleep_for(std::chrono::milliseconds(DTMF_PLAY_TIME)); 990 if (tone->Stop() != TELEPHONY_SUCCESS) { 991 TELEPHONY_LOGE("stop dtmftone failed"); 992 return CALL_ERR_AUDIO_TONE_STOP_FAILED; 993 } 994 tone->ReleaseRenderer(); 995 TELEPHONY_LOGI("stop dtmf tone success"); 996 return TELEPHONY_SUCCESS; 997} 998 999int32_t AudioControlManager::StopDtmfTone() 1000{ 1001 return StopCallTone(); 1002} 1003 1004int32_t AudioControlManager::OnPostDialNextChar(char str) 1005{ 1006 int32_t result = PlayDtmfTone(str); 1007 if (result != TELEPHONY_SUCCESS) { 1008 return result; 1009 } 1010 return TELEPHONY_SUCCESS; 1011} 1012 1013void AudioControlManager::NewCallCreated(sptr<CallBase> &callObjectPtr) {} 1014 1015void AudioControlManager::CallDestroyed(const DisconnectedDetails &details) {} 1016 1017bool AudioControlManager::IsSoundPlaying() 1018{ 1019 return soundState_ == SoundState::SOUNDING; 1020} 1021 1022void AudioControlManager::MuteNetWorkRingTone() 1023{ 1024 bool result = 1025 DelayedSingleton<AudioProxy>::GetInstance()->SetAudioScene(AudioStandard::AudioScene::AUDIO_SCENE_DEFAULT); 1026 TELEPHONY_LOGI("Set volume mute, result: %{public}d", result); 1027 if (isCrsVibrating_) { 1028 DelayedSingleton<AudioProxy>::GetInstance()->StopVibrator(); 1029 isCrsVibrating_ = false; 1030 } 1031} 1032 1033bool AudioControlManager::IsVideoCall(VideoStateType videoState) 1034{ 1035 return videoState == VideoStateType::TYPE_SEND_ONLY || videoState == VideoStateType::TYPE_RECEIVE_ONLY || 1036 videoState == VideoStateType::TYPE_VIDEO; 1037} 1038 1039bool AudioControlManager::IsBtOrWireHeadPlugin() 1040{ 1041 return AudioDeviceManager::IsBtActived() || AudioDeviceManager::IsWiredHeadsetConnected(); 1042} 1043 1044bool AudioControlManager::IsRingingVibrateModeOn() 1045{ 1046 auto datashareHelper = SettingsDataShareHelper::GetInstance(); 1047 std::string ringingVibrateModeEnable {"1"}; 1048 std::vector<int> activedOsAccountIds; 1049 OHOS::AccountSA::OsAccountManager::QueryActiveOsAccountIds(activedOsAccountIds); 1050 if (activedOsAccountIds.empty()) { 1051 TELEPHONY_LOGW("activedOsAccountIds is empty"); 1052 return false; 1053 } 1054 int userId = activedOsAccountIds[0]; 1055 OHOS::Uri uri( 1056 "datashare:///com.ohos.settingsdata/entry/settingsdata/USER_SETTINGSDATA_" 1057 + std::to_string(userId) + "?Proxy=true"); 1058 int resp = datashareHelper->Query(uri, "hw_vibrate_when_ringing", ringingVibrateModeEnable); 1059 if (resp == TELEPHONY_SUCCESS && ringingVibrateModeEnable == "1") { 1060 TELEPHONY_LOGI("RingingVibrateModeOpen:true"); 1061 return true; 1062 } 1063 return false; 1064} 1065} // namespace Telephony 1066} // namespace OHOS