1 /*
2 * Copyright (C) 2021-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
16 #include "audio_control_manager.h"
17
18 #include "call_control_manager.h"
19 #include "call_state_processor.h"
20 #include "distributed_call_manager.h"
21 #include "telephony_log_wrapper.h"
22 #include "audio_system_manager.h"
23 #include "audio_routing_manager.h"
24 #include "audio_device_info.h"
25 #include "audio_info.h"
26
27 namespace OHOS {
28 namespace Telephony {
29 using namespace AudioStandard;
30 constexpr int32_t DTMF_PLAY_TIME = 30;
31 constexpr int32_t VOICE_TYPE = 0;
32 constexpr int32_t CRS_TYPE = 2;
33
AudioControlManager()34 AudioControlManager::AudioControlManager()
35 : isLocalRingbackNeeded_(false), ring_(nullptr), tone_(nullptr), sound_(nullptr)
36 {}
37
~AudioControlManager()38 AudioControlManager::~AudioControlManager()
39 {
40 DelayedSingleton<AudioProxy>::GetInstance()->UnsetDeviceChangeCallback();
41 DelayedSingleton<AudioProxy>::GetInstance()->UnsetAudioPreferDeviceChangeCallback();
42 }
43
Init()44 void AudioControlManager::Init()
45 {
46 DelayedSingleton<AudioDeviceManager>::GetInstance()->Init();
47 DelayedSingleton<AudioSceneProcessor>::GetInstance()->Init();
48 DelayedSingleton<AudioProxy>::GetInstance()->SetAudioDeviceChangeCallback();
49 DelayedSingleton<AudioProxy>::GetInstance()->SetAudioPreferDeviceChangeCallback();
50 }
51
UpdateForegroundLiveCall()52 void AudioControlManager::UpdateForegroundLiveCall()
53 {
54 int32_t callId = DelayedSingleton<CallStateProcessor>::GetInstance()->GetAudioForegroundLiveCall();
55 if (callId == INVALID_CALLID) {
56 frontCall_ = nullptr;
57 DelayedSingleton<AudioProxy>::GetInstance()->SetMicrophoneMute(false);
58 TELEPHONY_LOGE("callId is invalid");
59 return;
60 }
61
62 sptr<CallBase> liveCall = CallObjectManager::GetOneCallObject(callId);
63 if (liveCall == nullptr) {
64 TELEPHONY_LOGE("liveCall is nullptr");
65 return;
66 }
67 if (liveCall->GetTelCallState() == TelCallState::CALL_STATUS_ACTIVE) {
68 if (frontCall_ == nullptr) {
69 frontCall_ = liveCall;
70 } else {
71 int32_t frontCallId = frontCall_->GetCallID();
72 int32_t liveCallId = liveCall->GetCallID();
73 if (frontCallId != liveCallId) {
74 frontCall_ = liveCall;
75 }
76 }
77 bool frontCallMute = frontCall_->IsMuted();
78 bool currentMute = DelayedSingleton<AudioProxy>::GetInstance()->IsMicrophoneMute();
79 if (frontCallMute != currentMute) {
80 SetMute(frontCallMute);
81 }
82 }
83 }
84
CallStateUpdated(sptr<CallBase> & callObjectPtr,TelCallState priorState,TelCallState nextState)85 void AudioControlManager::CallStateUpdated(
86 sptr<CallBase> &callObjectPtr, TelCallState priorState, TelCallState nextState)
87 {
88 if (callObjectPtr == nullptr) {
89 TELEPHONY_LOGE("call object nullptr");
90 return;
91 }
92 if (callObjectPtr->GetCallType() == CallType::TYPE_VOIP) {
93 TELEPHONY_LOGI("voip call not need control audio");
94 return;
95 }
96 if (totalCalls_.count(callObjectPtr) == 0) {
97 int32_t callId = callObjectPtr->GetCallID();
98 TelCallState callState = callObjectPtr->GetTelCallState();
99 TELEPHONY_LOGI("add new call , call id : %{public}d , call state : %{public}d", callId, callState);
100 totalCalls_.insert(callObjectPtr);
101 }
102 HandleCallStateUpdated(callObjectPtr, priorState, nextState);
103 if (nextState == TelCallState::CALL_STATUS_DISCONNECTED && totalCalls_.count(callObjectPtr) > 0) {
104 totalCalls_.erase(callObjectPtr);
105 }
106 UpdateForegroundLiveCall();
107 }
108
VideoStateUpdated(sptr<CallBase> & callObjectPtr,VideoStateType priorVideoState,VideoStateType nextVideoState)109 void AudioControlManager::VideoStateUpdated(
110 sptr<CallBase> &callObjectPtr, VideoStateType priorVideoState, VideoStateType nextVideoState)
111 {
112 if (callObjectPtr == nullptr) {
113 TELEPHONY_LOGE("call object nullptr");
114 return;
115 }
116 if (callObjectPtr->GetCallType() != CallType::TYPE_IMS) {
117 TELEPHONY_LOGE("other call not need control audio");
118 return;
119 }
120 AudioDevice device = {
121 .deviceType = AudioDeviceType::DEVICE_SPEAKER,
122 .address = { 0 },
123 };
124 if (callObjectPtr->GetCrsType() == CRS_TYPE) {
125 TELEPHONY_LOGI("crs ring tone should be speaker");
126 SetAudioDevice(device);
127 return;
128 }
129 TelCallState telCallState = callObjectPtr->GetTelCallState();
130 AudioDeviceType initDeviceType = GetInitAudioDeviceType();
131 if (!IsVideoCall(priorVideoState) && IsVideoCall(nextVideoState) &&
132 (telCallState != TelCallState::CALL_STATUS_INCOMING && telCallState != TelCallState::CALL_STATUS_WAITING)) {
133 if (callObjectPtr->GetOriginalCallType() == VOICE_TYPE &&
134 (telCallState == TelCallState::CALL_STATUS_DIALING || telCallState == TelCallState::CALL_STATUS_ALERTING)) {
135 device.deviceType = AudioDeviceType::DEVICE_EARPIECE;
136 }
137 if (initDeviceType == AudioDeviceType::DEVICE_WIRED_HEADSET ||
138 initDeviceType == AudioDeviceType::DEVICE_BLUETOOTH_SCO) {
139 device.deviceType = initDeviceType;
140 }
141 TELEPHONY_LOGI("set device type, type: %{public}d", static_cast<int32_t>(device.deviceType));
142 SetAudioDevice(device);
143 } else if (IsVideoCall(priorVideoState) && !IsVideoCall(nextVideoState)) {
144 device.deviceType = AudioDeviceType::DEVICE_EARPIECE;
145 if (initDeviceType == AudioDeviceType::DEVICE_WIRED_HEADSET ||
146 initDeviceType == AudioDeviceType::DEVICE_BLUETOOTH_SCO) {
147 device.deviceType = initDeviceType;
148 }
149 TELEPHONY_LOGI("set device type, type: %{public}d", static_cast<int32_t>(device.deviceType));
150 SetAudioDevice(device);
151 }
152 }
153
UpdateDeviceTypeForVideoCall()154 void AudioControlManager::UpdateDeviceTypeForVideoCall()
155 {
156 sptr<CallBase> foregroundCall = CallObjectManager::GetForegroundLiveCall();
157 if (foregroundCall == nullptr) {
158 TELEPHONY_LOGE("call object nullptr");
159 return;
160 }
161 if (foregroundCall->GetCallType() != CallType::TYPE_IMS) {
162 TELEPHONY_LOGE("other call not need control audio");
163 return;
164 }
165 AudioDevice device = {
166 .deviceType = AudioDeviceType::DEVICE_SPEAKER,
167 .address = { 0 },
168 };
169 AudioDeviceType initDeviceType = GetInitAudioDeviceType();
170 if (IsVideoCall(foregroundCall->GetVideoStateType())) {
171 if (initDeviceType == AudioDeviceType::DEVICE_WIRED_HEADSET ||
172 initDeviceType == AudioDeviceType::DEVICE_BLUETOOTH_SCO) {
173 device.deviceType = initDeviceType;
174 }
175 TELEPHONY_LOGI("set device type, type: %{public}d", static_cast<int32_t>(device.deviceType));
176 SetAudioDevice(device);
177 }
178 }
179
UpdateDeviceTypeForCrs()180 void AudioControlManager::UpdateDeviceTypeForCrs()
181 {
182 sptr<CallBase> incomingCall = CallObjectManager::GetOneCallObject(CallRunningState::CALL_RUNNING_STATE_RINGING);
183 if (incomingCall == nullptr) {
184 return;
185 }
186 if (incomingCall->GetCrsType() == CRS_TYPE) {
187 AudioDevice device = {
188 .deviceType = AudioDeviceType::DEVICE_SPEAKER,
189 .address = { 0 },
190 };
191 TELEPHONY_LOGI("crs ring tone should be speaker");
192 SetAudioDevice(device);
193 }
194 }
195
IncomingCallActivated(sptr<CallBase> & callObjectPtr)196 void AudioControlManager::IncomingCallActivated(sptr<CallBase> &callObjectPtr) {}
197
IncomingCallHungUp(sptr<CallBase> & callObjectPtr,bool isSendSms,std::string content)198 void AudioControlManager::IncomingCallHungUp(sptr<CallBase> &callObjectPtr, bool isSendSms, std::string content)
199 {
200 if (callObjectPtr == nullptr) {
201 TELEPHONY_LOGE("call object ptr nullptr");
202 return;
203 }
204 StopCallTone();
205 }
206
HandleCallStateUpdated(sptr<CallBase> & callObjectPtr,TelCallState priorState,TelCallState nextState)207 void AudioControlManager::HandleCallStateUpdated(
208 sptr<CallBase> &callObjectPtr, TelCallState priorState, TelCallState nextState)
209 {
210 if (nextState == TelCallState::CALL_STATUS_ANSWERED) {
211 TELEPHONY_LOGI("user answered, mute ringer instead of release renderer");
212 if (priorState == TelCallState::CALL_STATUS_INCOMING) {
213 DelayedSingleton<CallStateProcessor>::GetInstance()->DeleteCall(callObjectPtr->GetCallID(), priorState);
214 }
215 MuteRinger();
216 return;
217 }
218 HandleNextState(callObjectPtr, nextState);
219 if (priorState == nextState) {
220 TELEPHONY_LOGI("prior state equals next state");
221 return;
222 }
223 HandlePriorState(callObjectPtr, priorState);
224 }
225
HandleNextState(sptr<CallBase> & callObjectPtr,TelCallState nextState)226 void AudioControlManager::HandleNextState(sptr<CallBase> &callObjectPtr, TelCallState nextState)
227 {
228 AudioEvent event = AudioEvent::UNKNOWN_EVENT;
229 DelayedSingleton<CallStateProcessor>::GetInstance()->AddCall(callObjectPtr->GetCallID(), nextState);
230 switch (nextState) {
231 case TelCallState::CALL_STATUS_DIALING:
232 event = AudioEvent::NEW_DIALING_CALL;
233 audioInterruptState_ = AudioInterruptState::INTERRUPT_STATE_RINGING;
234 break;
235 case TelCallState::CALL_STATUS_ALERTING:
236 event = AudioEvent::NEW_ALERTING_CALL;
237 audioInterruptState_ = AudioInterruptState::INTERRUPT_STATE_RINGING;
238 break;
239 case TelCallState::CALL_STATUS_ACTIVE:
240 HandleNewActiveCall(callObjectPtr);
241 audioInterruptState_ = AudioInterruptState::INTERRUPT_STATE_ACTIVATED;
242 break;
243 case TelCallState::CALL_STATUS_WAITING:
244 case TelCallState::CALL_STATUS_INCOMING:
245 event = AudioEvent::NEW_INCOMING_CALL;
246 audioInterruptState_ = AudioInterruptState::INTERRUPT_STATE_RINGING;
247 break;
248 case TelCallState::CALL_STATUS_DISCONNECTING:
249 case TelCallState::CALL_STATUS_DISCONNECTED:
250 if (isCrsVibrating_) {
251 DelayedSingleton<AudioProxy>::GetInstance()->StopVibrator();
252 isCrsVibrating_ = false;
253 }
254 audioInterruptState_ = AudioInterruptState::INTERRUPT_STATE_DEACTIVATED;
255 break;
256 default:
257 break;
258 }
259 if (event == AudioEvent::UNKNOWN_EVENT) {
260 return;
261 }
262 DelayedSingleton<AudioSceneProcessor>::GetInstance()->ProcessEvent(event);
263 }
264
HandlePriorState(sptr<CallBase> & callObjectPtr,TelCallState priorState)265 void AudioControlManager::HandlePriorState(sptr<CallBase> &callObjectPtr, TelCallState priorState)
266 {
267 AudioEvent event = AudioEvent::UNKNOWN_EVENT;
268 DelayedSingleton<CallStateProcessor>::GetInstance()->DeleteCall(callObjectPtr->GetCallID(), priorState);
269 int32_t stateNumber = DelayedSingleton<CallStateProcessor>::GetInstance()->GetCallNumber(priorState);
270 switch (priorState) {
271 case TelCallState::CALL_STATUS_DIALING:
272 if (stateNumber == EMPTY_VALUE) {
273 StopRingback(); // should stop ringtone while no more alerting calls
274 event = AudioEvent::NO_MORE_DIALING_CALL;
275 }
276 break;
277 case TelCallState::CALL_STATUS_ALERTING:
278 if (stateNumber == EMPTY_VALUE) {
279 StopRingback(); // should stop ringtone while no more alerting calls
280 event = AudioEvent::NO_MORE_ALERTING_CALL;
281 }
282 break;
283 case TelCallState::CALL_STATUS_INCOMING:
284 case TelCallState::CALL_STATUS_WAITING:
285 ProcessAudioWhenCallActive(callObjectPtr);
286 StopRingtone();
287 StopCallTone();
288 event = AudioEvent::NO_MORE_INCOMING_CALL;
289 break;
290 case TelCallState::CALL_STATUS_ACTIVE:
291 if (stateNumber == EMPTY_VALUE) {
292 event = AudioEvent::NO_MORE_ACTIVE_CALL;
293 }
294 StopRingback();
295 break;
296 case TelCallState::CALL_STATUS_HOLDING:
297 if (stateNumber == EMPTY_VALUE) {
298 event = AudioEvent::NO_MORE_HOLDING_CALL;
299 }
300 break;
301 default:
302 break;
303 }
304 if (event == AudioEvent::UNKNOWN_EVENT) {
305 return;
306 }
307 DelayedSingleton<AudioSceneProcessor>::GetInstance()->ProcessEvent(event);
308 }
309
ProcessAudioWhenCallActive(sptr<CallBase> & callObjectPtr)310 void AudioControlManager::ProcessAudioWhenCallActive(sptr<CallBase> &callObjectPtr)
311 {
312 if (callObjectPtr->GetCallRunningState() == CallRunningState::CALL_RUNNING_STATE_ACTIVE) {
313 if (isCrsVibrating_) {
314 DelayedSingleton<AudioProxy>::GetInstance()->StopVibrator();
315 isCrsVibrating_ = false;
316 }
317 StopSoundtone();
318 PlaySoundtone();
319 UpdateDeviceTypeForVideoCall();
320 }
321 }
322
HandleNewActiveCall(sptr<CallBase> & callObjectPtr)323 void AudioControlManager::HandleNewActiveCall(sptr<CallBase> &callObjectPtr)
324 {
325 std::string number = callObjectPtr->GetAccountNumber();
326 if (number.empty()) {
327 TELEPHONY_LOGE("call object account number empty");
328 return;
329 }
330 CallType callType = callObjectPtr->GetCallType();
331 AudioEvent event = AudioEvent::UNKNOWN_EVENT;
332 switch (callType) {
333 case CallType::TYPE_CS:
334 case CallType::TYPE_SATELLITE:
335 event = AudioEvent::NEW_ACTIVE_CS_CALL;
336 break;
337 case CallType::TYPE_IMS:
338 event = AudioEvent::NEW_ACTIVE_IMS_CALL;
339 break;
340 case CallType::TYPE_OTT:
341 event = AudioEvent::NEW_ACTIVE_OTT_CALL;
342 break;
343 default:
344 break;
345 }
346 if (event == AudioEvent::UNKNOWN_EVENT) {
347 return;
348 }
349 DelayedSingleton<AudioSceneProcessor>::GetInstance()->ProcessEvent(event);
350 }
351
352 /**
353 * @param device , audio device
354 * usually called by the ui interaction , in purpose of switching to another audio device
355 */
SetAudioDevice(const AudioDevice & device)356 int32_t AudioControlManager::SetAudioDevice(const AudioDevice &device)
357 {
358 TELEPHONY_LOGI("set audio device, type: %{public}d", static_cast<int32_t>(device.deviceType));
359 AudioDeviceType audioDeviceType = AudioDeviceType::DEVICE_UNKNOWN;
360 switch (device.deviceType) {
361 case AudioDeviceType::DEVICE_SPEAKER:
362 case AudioDeviceType::DEVICE_EARPIECE:
363 case AudioDeviceType::DEVICE_WIRED_HEADSET:
364 audioDeviceType = device.deviceType;
365 break;
366 case AudioDeviceType::DEVICE_DISTRIBUTED_AUTOMOTIVE:
367 case AudioDeviceType::DEVICE_DISTRIBUTED_PHONE:
368 case AudioDeviceType::DEVICE_DISTRIBUTED_PAD:
369 TELEPHONY_LOGI("set audio device, address: %{public}s", device.address);
370 if (DelayedSingleton<DistributedCallManager>::GetInstance()->SwitchDCallDevice(device)) {
371 DelayedSingleton<AudioDeviceManager>::GetInstance()->SetCurrentAudioDevice(device.deviceType);
372 return TELEPHONY_SUCCESS;
373 }
374 return CALL_ERR_AUDIO_SET_AUDIO_DEVICE_FAILED;
375 case AudioDeviceType::DEVICE_BLUETOOTH_SCO: {
376 AudioSystemManager* audioSystemManager = AudioSystemManager::GetInstance();
377 int32_t ret = audioSystemManager->SetCallDeviceActive(ActiveDeviceType::BLUETOOTH_SCO,
378 true, device.address);
379 if (ret != 0) {
380 TELEPHONY_LOGE("SetCallDeviceActive failed");
381 return CALL_ERR_AUDIO_SET_AUDIO_DEVICE_FAILED;
382 }
383 audioDeviceType = device.deviceType;
384 break;
385 }
386 default:
387 break;
388 }
389 if (audioDeviceType != AudioDeviceType::DEVICE_UNKNOWN) {
390 if (DelayedSingleton<DistributedCallManager>::GetInstance()->IsDAudioDeviceConnected()) {
391 DelayedSingleton<DistributedCallManager>::GetInstance()->DisconnectDCallDevice();
392 }
393 if (DelayedSingleton<AudioDeviceManager>::GetInstance()->SwitchDevice(audioDeviceType)) {
394 return TELEPHONY_SUCCESS;
395 }
396 }
397 return CALL_ERR_AUDIO_SET_AUDIO_DEVICE_FAILED;
398 }
399
PlayRingtone()400 bool AudioControlManager::PlayRingtone()
401 {
402 if (!ShouldPlayRingtone()) {
403 TELEPHONY_LOGE("should not play ringtone");
404 return false;
405 }
406 ring_ = std::make_unique<Ring>();
407 if (ring_ == nullptr) {
408 TELEPHONY_LOGE("create ring object failed");
409 return false;
410 }
411 sptr<CallBase> incomingCall = CallObjectManager::GetOneCallObject(CallRunningState::CALL_RUNNING_STATE_RINGING);
412 if (incomingCall == nullptr) {
413 TELEPHONY_LOGE("incomingCall is nullptr");
414 return false;
415 }
416 CallAttributeInfo info;
417 incomingCall->GetCallAttributeBaseInfo(info);
418 AudioStandard::AudioRingerMode ringMode = DelayedSingleton<AudioProxy>::GetInstance()->GetRingerMode();
419 if (incomingCall->GetCrsType() == CRS_TYPE && ringMode != AudioStandard::AudioRingerMode::RINGER_MODE_SILENT) {
420 if (!isCrsVibrating_) {
421 isCrsVibrating_ = (DelayedSingleton<AudioProxy>::GetInstance()->StartVibrator() == TELEPHONY_SUCCESS);
422 }
423 if (ringMode != AudioStandard::AudioRingerMode::RINGER_MODE_VIBRATE) {
424 if (PlaySoundtone()) {
425 TELEPHONY_LOGI("play soundtone success");
426 return true;
427 }
428 return false;
429 }
430 }
431 if (ring_->Play(info.accountId) != TELEPHONY_SUCCESS) {
432 TELEPHONY_LOGE("play ringtone failed");
433 return false;
434 }
435 TELEPHONY_LOGI("play ringtone success");
436 return true;
437 }
438
PlaySoundtone()439 bool AudioControlManager::PlaySoundtone()
440 {
441 if (soundState_ == SoundState::SOUNDING) {
442 TELEPHONY_LOGE("should not play soundTone");
443 return false;
444 }
445 if (sound_ == nullptr) {
446 sound_ = std::make_unique<Sound>();
447 if (sound_ == nullptr) {
448 TELEPHONY_LOGE("create sound object failed");
449 return false;
450 }
451 }
452 if (sound_->Play() != TELEPHONY_SUCCESS) {
453 TELEPHONY_LOGE("play soundtone failed");
454 return false;
455 }
456 TELEPHONY_LOGI("play soundtone success");
457 return true;
458 }
459
StopSoundtone()460 bool AudioControlManager::StopSoundtone()
461 {
462 if (soundState_ == SoundState::STOPPED) {
463 TELEPHONY_LOGI("soundtone already stopped");
464 return true;
465 }
466 if (sound_ == nullptr) {
467 TELEPHONY_LOGE("sound_ is nullptr");
468 return false;
469 }
470 if (sound_->Stop() != TELEPHONY_SUCCESS) {
471 TELEPHONY_LOGE("stop soundtone failed");
472 return false;
473 }
474 sound_->ReleaseRenderer();
475 TELEPHONY_LOGI("stop soundtone success");
476 return true;
477 }
478
StopRingtone()479 bool AudioControlManager::StopRingtone()
480 {
481 if (ringState_ == RingState::STOPPED) {
482 TELEPHONY_LOGI("ringtone already stopped");
483 return true;
484 }
485 if (ring_ == nullptr) {
486 TELEPHONY_LOGE("ring_ is nullptr");
487 return false;
488 }
489 if (ring_->Stop() != TELEPHONY_SUCCESS) {
490 TELEPHONY_LOGE("stop ringtone failed");
491 return false;
492 }
493 ring_->ReleaseRenderer();
494 TELEPHONY_LOGI("stop ringtone success");
495 return true;
496 }
497
498 /**
499 * while audio state changed , maybe need to reinitialize the audio device
500 * in order to get the initialization status of audio device , need to consider varieties of audio conditions
501 */
GetInitAudioDeviceType() const502 AudioDeviceType AudioControlManager::GetInitAudioDeviceType() const
503 {
504 if (audioInterruptState_ == AudioInterruptState::INTERRUPT_STATE_DEACTIVATED) {
505 return AudioDeviceType::DEVICE_DISABLE;
506 } else {
507 /**
508 * Init audio device type according to the priority in different call state:
509 * In voice call state, bluetooth sco > wired headset > earpiece > speaker
510 * In video call state, bluetooth sco > wired headset > speaker > earpiece
511 */
512 if (AudioDeviceManager::IsBtScoConnected()) {
513 return AudioDeviceType::DEVICE_BLUETOOTH_SCO;
514 }
515 if (AudioDeviceManager::IsWiredHeadsetConnected()) {
516 return AudioDeviceType::DEVICE_WIRED_HEADSET;
517 }
518 sptr<CallBase> liveCall = CallObjectManager::GetForegroundLiveCall();
519 if (liveCall != nullptr && liveCall->GetVideoStateType() == VideoStateType::TYPE_VIDEO) {
520 TELEPHONY_LOGI("current video call speaker is active");
521 return AudioDeviceType::DEVICE_SPEAKER;
522 }
523 if (AudioDeviceManager::IsEarpieceAvailable()) {
524 return AudioDeviceType::DEVICE_EARPIECE;
525 }
526 return AudioDeviceType::DEVICE_SPEAKER;
527 }
528 }
529
530 /**
531 * @param isMute , mute state
532 * usually called by the ui interaction , mute or unmute microphone
533 */
SetMute(bool isMute)534 int32_t AudioControlManager::SetMute(bool isMute)
535 {
536 bool hasCall = DelayedSingleton<CallControlManager>::GetInstance()->HasCall();
537 if (!hasCall) {
538 TELEPHONY_LOGE("no call exists, set mute failed");
539 return CALL_ERR_AUDIO_SETTING_MUTE_FAILED;
540 }
541 bool enabled = false;
542 if ((DelayedSingleton<CallControlManager>::GetInstance()->HasEmergency(enabled) == TELEPHONY_SUCCESS) && enabled) {
543 isMute = false;
544 }
545 if (!DelayedSingleton<AudioProxy>::GetInstance()->SetMicrophoneMute(isMute)) {
546 TELEPHONY_LOGE("set mute failed");
547 return CALL_ERR_AUDIO_SETTING_MUTE_FAILED;
548 }
549 DelayedSingleton<AudioDeviceManager>::GetInstance()->ReportAudioDeviceInfo();
550 if (frontCall_ == nullptr) {
551 TELEPHONY_LOGE("frontCall_ is nullptr");
552 return TELEPHONY_ERR_LOCAL_PTR_NULL;
553 }
554 frontCall_->SetMicPhoneState(isMute);
555 TELEPHONY_LOGI("SetMute success callId:%{public}d, mute:%{public}d", frontCall_->GetCallID(), isMute);
556 return TELEPHONY_SUCCESS;
557 }
558
MuteRinger()559 int32_t AudioControlManager::MuteRinger()
560 {
561 sptr<CallBase> incomingCall = CallObjectManager::GetOneCallObject(CallRunningState::CALL_RUNNING_STATE_RINGING);
562 if (incomingCall != nullptr) {
563 if (incomingCall->GetCrsType() == CRS_TYPE) {
564 TELEPHONY_LOGI("Mute network ring tone.");
565 MuteNetWorkRingTone();
566 }
567 }
568 if (ringState_ == RingState::STOPPED) {
569 TELEPHONY_LOGI("ring already stopped");
570 return TELEPHONY_SUCCESS;
571 }
572 if (ring_ == nullptr) {
573 TELEPHONY_LOGE("ring is nullptr");
574 return CALL_ERR_AUDIO_SETTING_MUTE_FAILED;
575 }
576 if (ring_->SetMute() != TELEPHONY_SUCCESS) {
577 TELEPHONY_LOGE("SetMute fail");
578 return CALL_ERR_AUDIO_SETTING_MUTE_FAILED;
579 }
580 TELEPHONY_LOGI("mute ring success");
581 return TELEPHONY_SUCCESS;
582 }
583
PlayCallEndedTone(TelCallState priorState,TelCallState nextState,CallEndedType type)584 void AudioControlManager::PlayCallEndedTone(TelCallState priorState, TelCallState nextState, CallEndedType type)
585 {
586 if (nextState != TelCallState::CALL_STATUS_DISCONNECTED) {
587 return;
588 }
589 if (priorState == TelCallState::CALL_STATUS_ACTIVE || priorState == TelCallState::CALL_STATUS_DIALING ||
590 priorState == TelCallState::CALL_STATUS_HOLDING) {
591 switch (type) {
592 case CallEndedType::PHONE_IS_BUSY:
593 PlayCallTone(ToneDescriptor::TONE_ENGAGED);
594 break;
595 case CallEndedType::CALL_ENDED_NORMALLY:
596 PlayCallTone(ToneDescriptor::TONE_FINISHED);
597 break;
598 case CallEndedType::UNKNOWN:
599 PlayCallTone(ToneDescriptor::TONE_UNKNOWN);
600 break;
601 case CallEndedType::INVALID_NUMBER:
602 PlayCallTone(ToneDescriptor::TONE_INVALID_NUMBER);
603 break;
604 default:
605 break;
606 }
607 }
608 }
609
GetCallList()610 std::set<sptr<CallBase>> AudioControlManager::GetCallList()
611 {
612 std::lock_guard<std::mutex> lock(mutex_);
613 return totalCalls_;
614 }
615
GetCurrentActiveCall()616 sptr<CallBase> AudioControlManager::GetCurrentActiveCall()
617 {
618 int32_t callId = DelayedSingleton<CallStateProcessor>::GetInstance()->GetCurrentActiveCall();
619 if (callId != INVALID_CALLID) {
620 return GetCallBase(callId);
621 }
622 return nullptr;
623 }
624
GetCallBase(int32_t callId)625 sptr<CallBase> AudioControlManager::GetCallBase(int32_t callId)
626 {
627 sptr<CallBase> callBase = nullptr;
628 std::lock_guard<std::mutex> lock(mutex_);
629 for (auto &call : totalCalls_) {
630 if (call->GetCallID() == callId) {
631 callBase = call;
632 break;
633 }
634 }
635 return callBase;
636 }
637
IsEmergencyCallExists() const638 bool AudioControlManager::IsEmergencyCallExists() const
639 {
640 for (auto call : totalCalls_) {
641 if (call->GetEmergencyState()) {
642 return true;
643 }
644 }
645 return false;
646 }
647
GetAudioInterruptState()648 AudioInterruptState AudioControlManager::GetAudioInterruptState()
649 {
650 return audioInterruptState_;
651 }
652
SetVolumeAudible()653 void AudioControlManager::SetVolumeAudible()
654 {
655 DelayedSingleton<AudioProxy>::GetInstance()->SetVolumeAudible();
656 }
657
SetRingState(RingState state)658 void AudioControlManager::SetRingState(RingState state)
659 {
660 ringState_ = state;
661 }
662
SetSoundState(SoundState state)663 void AudioControlManager::SetSoundState(SoundState state)
664 {
665 soundState_ = state;
666 }
667
SetToneState(ToneState state)668 void AudioControlManager::SetToneState(ToneState state)
669 {
670 toneState_ = state;
671 }
672
SetLocalRingbackNeeded(bool isNeeded)673 void AudioControlManager::SetLocalRingbackNeeded(bool isNeeded)
674 {
675 if (isLocalRingbackNeeded_ && !isNeeded) {
676 StopRingback();
677 }
678 isLocalRingbackNeeded_ = isNeeded;
679 }
680
IsNumberAllowed(const std::string & phoneNum)681 bool AudioControlManager::IsNumberAllowed(const std::string &phoneNum)
682 {
683 // check whether the phone number is allowed or not , should not ring if number is not allowed
684 return true;
685 }
686
ShouldPlayRingtone() const687 bool AudioControlManager::ShouldPlayRingtone() const
688 {
689 auto processor = DelayedSingleton<CallStateProcessor>::GetInstance();
690 int32_t alertingCallNum = processor->GetCallNumber(TelCallState::CALL_STATUS_ALERTING);
691 int32_t incomingCallNum = processor->GetCallNumber(TelCallState::CALL_STATUS_INCOMING);
692 if (incomingCallNum == EMPTY_VALUE || alertingCallNum > EMPTY_VALUE || ringState_ == RingState::RINGING) {
693 return false;
694 }
695 return true;
696 }
697
IsAudioActivated() const698 bool AudioControlManager::IsAudioActivated() const
699 {
700 return audioInterruptState_ == AudioInterruptState::INTERRUPT_STATE_ACTIVATED ||
701 audioInterruptState_ == AudioInterruptState::INTERRUPT_STATE_RINGING;
702 }
703
PlayCallTone(ToneDescriptor type)704 int32_t AudioControlManager::PlayCallTone(ToneDescriptor type)
705 {
706 if (toneState_ == ToneState::TONEING) {
707 TELEPHONY_LOGE("should not play callTone");
708 return CALL_ERR_AUDIO_TONE_PLAY_FAILED;
709 }
710 toneState_ = ToneState::TONEING;
711 tone_ = std::make_unique<Tone>(type);
712 if (tone_ == nullptr) {
713 TELEPHONY_LOGE("create tone failed");
714 return TELEPHONY_ERR_LOCAL_PTR_NULL;
715 }
716 if (tone_->Play() != TELEPHONY_SUCCESS) {
717 TELEPHONY_LOGE("play calltone failed");
718 return CALL_ERR_AUDIO_TONE_PLAY_FAILED;
719 }
720 TELEPHONY_LOGI("play calltone success");
721 return TELEPHONY_SUCCESS;
722 }
723
StopCallTone()724 int32_t AudioControlManager::StopCallTone()
725 {
726 if (toneState_ == ToneState::STOPPED) {
727 TELEPHONY_LOGI("tone is already stopped");
728 return TELEPHONY_SUCCESS;
729 }
730 if (tone_ == nullptr) {
731 TELEPHONY_LOGE("tone_ is nullptr");
732 return TELEPHONY_ERR_LOCAL_PTR_NULL;
733 }
734 if (tone_->Stop() != TELEPHONY_SUCCESS) {
735 TELEPHONY_LOGE("stop calltone failed");
736 return CALL_ERR_AUDIO_TONE_STOP_FAILED;
737 }
738 tone_->ReleaseRenderer();
739 toneState_ = ToneState::STOPPED;
740 TELEPHONY_LOGI("stop call tone success");
741 return TELEPHONY_SUCCESS;
742 }
743
IsTonePlaying() const744 bool AudioControlManager::IsTonePlaying() const
745 {
746 return toneState_ == ToneState::TONEING;
747 }
748
IsCurrentRinging() const749 bool AudioControlManager::IsCurrentRinging() const
750 {
751 return ringState_ == RingState::RINGING;
752 }
753
PlayRingback()754 int32_t AudioControlManager::PlayRingback()
755 {
756 if (!isLocalRingbackNeeded_) {
757 return CALL_ERR_AUDIO_TONE_PLAY_FAILED;
758 }
759 return PlayCallTone(ToneDescriptor::TONE_RINGBACK);
760 }
761
StopRingback()762 int32_t AudioControlManager::StopRingback()
763 {
764 return StopCallTone();
765 }
766
PlayWaitingTone()767 int32_t AudioControlManager::PlayWaitingTone()
768 {
769 return PlayCallTone(ToneDescriptor::TONE_WAITING);
770 }
771
StopWaitingTone()772 int32_t AudioControlManager::StopWaitingTone()
773 {
774 return StopCallTone();
775 }
776
PlayDtmfTone(char str)777 int32_t AudioControlManager::PlayDtmfTone(char str)
778 {
779 ToneDescriptor dtmfTone = Tone::ConvertDigitToTone(str);
780 return PlayCallTone(dtmfTone);
781 }
782
StopDtmfTone()783 int32_t AudioControlManager::StopDtmfTone()
784 {
785 return StopCallTone();
786 }
787
OnPostDialNextChar(char str)788 int32_t AudioControlManager::OnPostDialNextChar(char str)
789 {
790 int32_t result = PlayDtmfTone(str);
791 if (result != TELEPHONY_SUCCESS) {
792 return result;
793 }
794 std::this_thread::sleep_for(std::chrono::milliseconds(DTMF_PLAY_TIME));
795 result = StopDtmfTone();
796 if (result != TELEPHONY_SUCCESS) {
797 return result;
798 }
799 return TELEPHONY_SUCCESS;
800 }
801
NewCallCreated(sptr<CallBase> & callObjectPtr)802 void AudioControlManager::NewCallCreated(sptr<CallBase> &callObjectPtr) {}
803
CallDestroyed(const DisconnectedDetails & details)804 void AudioControlManager::CallDestroyed(const DisconnectedDetails &details) {}
805
IsSoundPlaying()806 bool AudioControlManager::IsSoundPlaying()
807 {
808 return soundState_ == SoundState::SOUNDING;
809 }
810
MuteNetWorkRingTone()811 void AudioControlManager::MuteNetWorkRingTone()
812 {
813 bool result =
814 DelayedSingleton<AudioProxy>::GetInstance()->SetAudioScene(AudioStandard::AudioScene::AUDIO_SCENE_DEFAULT);
815 TELEPHONY_LOGI("Set volume mute, result: %{public}d", result);
816 if (isCrsVibrating_) {
817 DelayedSingleton<AudioProxy>::GetInstance()->StopVibrator();
818 isCrsVibrating_ = false;
819 }
820 }
821
IsVideoCall(VideoStateType videoState)822 bool AudioControlManager::IsVideoCall(VideoStateType videoState)
823 {
824 return videoState == VideoStateType::TYPE_SEND_ONLY || videoState == VideoStateType::TYPE_RECEIVE_ONLY ||
825 videoState == VideoStateType::TYPE_VIDEO;
826 }
827 } // namespace Telephony
828 } // namespace OHOS