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_control_manager.h"
17 #include "audio_player.h"
18 #include "call_state_processor.h"
19
20 #include "telephony_log_wrapper.h"
21
22 #include "call_control_manager.h"
23
24 namespace OHOS {
25 namespace Telephony {
AudioControlManager()26 AudioControlManager::AudioControlManager()
27 : isTonePlaying_(false), isLocalRingbackNeeded_(false), ring_(nullptr), tone_(nullptr)
28 {}
29
~AudioControlManager()30 AudioControlManager::~AudioControlManager() {}
31
Init()32 void AudioControlManager::Init()
33 {
34 DelayedSingleton<AudioDeviceManager>::GetInstance()->Init();
35 DelayedSingleton<AudioSceneProcessor>::GetInstance()->Init();
36 }
37
CallStateUpdated(sptr<CallBase> & callObjectPtr,TelCallState priorState,TelCallState nextState)38 void AudioControlManager::CallStateUpdated(
39 sptr<CallBase> &callObjectPtr, TelCallState priorState, TelCallState nextState)
40 {
41 if (callObjectPtr == nullptr) {
42 TELEPHONY_LOGE("call object nullptr");
43 return;
44 }
45 if (totalCalls_.count(callObjectPtr) == 0) {
46 int32_t callId = callObjectPtr->GetCallID();
47 TelCallState callState = callObjectPtr->GetTelCallState();
48 TELEPHONY_LOGI("add new call , call id : %{public}d , call state : %{public}d", callId, callState);
49 totalCalls_.insert(callObjectPtr);
50 }
51 HandleCallStateUpdated(callObjectPtr, priorState, nextState);
52 if (nextState == TelCallState::CALL_STATUS_DISCONNECTED && totalCalls_.count(callObjectPtr) > 0) {
53 totalCalls_.erase(callObjectPtr);
54 }
55 }
56
IncomingCallActivated(sptr<CallBase> & callObjectPtr)57 void AudioControlManager::IncomingCallActivated(sptr<CallBase> &callObjectPtr)
58 {
59 if (callObjectPtr == nullptr) {
60 TELEPHONY_LOGE("call object ptr nullptr");
61 return;
62 }
63 StopRingtone();
64 DelayedSingleton<AudioProxy>::GetInstance()->SetMicrophoneMute(false); // unmute microphone
65 }
66
IncomingCallHungUp(sptr<CallBase> & callObjectPtr,bool isSendSms,std::string content)67 void AudioControlManager::IncomingCallHungUp(sptr<CallBase> &callObjectPtr, bool isSendSms, std::string content)
68 {
69 if (callObjectPtr == nullptr) {
70 TELEPHONY_LOGE("call object ptr nullptr");
71 return;
72 }
73 StopRingtone();
74 DelayedSingleton<AudioProxy>::GetInstance()->SetMicrophoneMute(false); // unmute microphone
75 }
76
HandleCallStateUpdated(sptr<CallBase> & callObjectPtr,TelCallState priorState,TelCallState nextState)77 void AudioControlManager::HandleCallStateUpdated(
78 sptr<CallBase> &callObjectPtr, TelCallState priorState, TelCallState nextState)
79 {
80 HandleNextState(callObjectPtr, nextState);
81 if (priorState == nextState) {
82 TELEPHONY_LOGI("prior state equals next state");
83 return;
84 }
85 HandlePriorState(callObjectPtr, priorState);
86 }
87
HandleNextState(sptr<CallBase> & callObjectPtr,TelCallState nextState)88 void AudioControlManager::HandleNextState(sptr<CallBase> &callObjectPtr, TelCallState nextState)
89 {
90 AudioEvent event = AudioEvent::UNKNOWN_EVENT;
91 std::string number = callObjectPtr->GetAccountNumber();
92 DelayedSingleton<CallStateProcessor>::GetInstance()->AddCall(number, nextState);
93 switch (nextState) {
94 case TelCallState::CALL_STATUS_DIALING:
95 event = AudioEvent::NEW_DIALING_CALL;
96 SetAudioInterruptState(AudioInterruptState::INTERRUPT_STATE_RINGING);
97 break;
98 case TelCallState::CALL_STATUS_ALERTING:
99 event = AudioEvent::NEW_ALERTING_CALL;
100 SetAudioInterruptState(AudioInterruptState::INTERRUPT_STATE_RINGING);
101 break;
102 case TelCallState::CALL_STATUS_ACTIVE:
103 HandleNewActiveCall(callObjectPtr);
104 SetAudioInterruptState(AudioInterruptState::INTERRUPT_STATE_ACTIVATED);
105 break;
106 case TelCallState::CALL_STATUS_INCOMING:
107 event = AudioEvent::NEW_INCOMING_CALL;
108 SetAudioInterruptState(AudioInterruptState::INTERRUPT_STATE_RINGING);
109 break;
110 case TelCallState::CALL_STATUS_DISCONNECTED:
111 SetAudioInterruptState(AudioInterruptState::INTERRUPT_STATE_DEACTIVATED);
112 break;
113 default:
114 break;
115 }
116 if (event == AudioEvent::UNKNOWN_EVENT) {
117 return;
118 }
119 DelayedSingleton<AudioSceneProcessor>::GetInstance()->ProcessEvent(event);
120 }
121
HandlePriorState(sptr<CallBase> & callObjectPtr,TelCallState priorState)122 void AudioControlManager::HandlePriorState(sptr<CallBase> &callObjectPtr, TelCallState priorState)
123 {
124 AudioEvent event = AudioEvent::UNKNOWN_EVENT;
125 std::string number = callObjectPtr->GetAccountNumber();
126 DelayedSingleton<CallStateProcessor>::GetInstance()->DeleteCall(number, priorState);
127 int32_t stateNumber = DelayedSingleton<CallStateProcessor>::GetInstance()->GetCallNumber(priorState);
128 switch (priorState) {
129 case TelCallState::CALL_STATUS_DIALING:
130 if (stateNumber == EMPTY_VALUE) {
131 StopRingback(); // should stop ringtone while no more alerting calls
132 event = AudioEvent::NO_MORE_DIALING_CALL;
133 }
134 break;
135 case TelCallState::CALL_STATUS_ALERTING:
136 if (stateNumber == EMPTY_VALUE) {
137 StopRingback(); // should stop ringtone while no more alerting calls
138 event = AudioEvent::NO_MORE_ALERTING_CALL;
139 }
140 break;
141 case TelCallState::CALL_STATUS_INCOMING:
142 if (stateNumber == EMPTY_VALUE) {
143 bool result = StopRingtone(); // should stop ringtone while no more incoming calls
144 if (result && callObjectPtr->GetCallRunningState() != CallRunningState::CALL_RUNNING_STATE_ACTIVE) {
145 AudioPlayer::ReleaseRenderer();
146 }
147 event = AudioEvent::NO_MORE_INCOMING_CALL;
148 }
149 break;
150 case TelCallState::CALL_STATUS_ACTIVE:
151 if (stateNumber == EMPTY_VALUE) {
152 // unmute microphone while no more active call
153 DelayedSingleton<AudioProxy>::GetInstance()->SetMicrophoneMute(false);
154 event = AudioEvent::NO_MORE_ACTIVE_CALL;
155 }
156 break;
157 default:
158 break;
159 }
160 if (event == AudioEvent::UNKNOWN_EVENT) {
161 return;
162 }
163 DelayedSingleton<AudioSceneProcessor>::GetInstance()->ProcessEvent(event);
164 }
165
HandleNewActiveCall(sptr<CallBase> & callObjectPtr)166 void AudioControlManager::HandleNewActiveCall(sptr<CallBase> &callObjectPtr)
167 {
168 std::string number = callObjectPtr->GetAccountNumber();
169 if (number.empty()) {
170 TELEPHONY_LOGE("call object account number empty");
171 return;
172 }
173 CallType callType = callObjectPtr->GetCallType();
174 AudioEvent event = AudioEvent::UNKNOWN_EVENT;
175 switch (callType) {
176 case CallType::TYPE_CS:
177 event = AudioEvent::NEW_ACTIVE_CS_CALL;
178 break;
179 case CallType::TYPE_IMS:
180 event = AudioEvent::NEW_ACTIVE_IMS_CALL;
181 break;
182 case CallType::TYPE_OTT:
183 event = AudioEvent::NEW_ACTIVE_OTT_CALL;
184 break;
185 default:
186 break;
187 }
188 if (event == AudioEvent::UNKNOWN_EVENT) {
189 return;
190 }
191 DelayedSingleton<AudioSceneProcessor>::GetInstance()->ProcessEvent(event);
192 }
193
194 /**
195 * @param device , audio device
196 * usually called by the ui interaction , in purpose of switching to another audio device
197 */
SetAudioDevice(AudioDevice device)198 int32_t AudioControlManager::SetAudioDevice(AudioDevice device)
199 {
200 AudioDevice audioDevice = AudioDevice::DEVICE_UNKNOWN;
201 switch (device) {
202 case AudioDevice::DEVICE_SPEAKER:
203 case AudioDevice::DEVICE_EARPIECE:
204 case AudioDevice::DEVICE_WIRED_HEADSET:
205 case AudioDevice::DEVICE_BLUETOOTH_SCO:
206 audioDevice = device;
207 break;
208 default:
209 break;
210 }
211 if (audioDevice != AudioDevice::DEVICE_UNKNOWN &&
212 DelayedSingleton<AudioDeviceManager>::GetInstance()->SwitchDevice(audioDevice)) {
213 return TELEPHONY_SUCCESS;
214 }
215 return CALL_ERR_AUDIO_SET_AUDIO_DEVICE_FAILED;
216 }
217
PlayRingtone()218 bool AudioControlManager::PlayRingtone()
219 {
220 if (!ShouldPlayRingtone()) {
221 TELEPHONY_LOGE("should not play ringtone");
222 return false;
223 }
224 ring_ = std::make_unique<Ring>();
225 if (ring_ == nullptr) {
226 TELEPHONY_LOGE("create ring object failed");
227 return false;
228 }
229 if (ring_->Play() != TELEPHONY_SUCCESS) {
230 TELEPHONY_LOGE("play ringtone failed");
231 return false;
232 }
233 TELEPHONY_LOGI("play ringtone success");
234 return true;
235 }
236
PlayRingtone(const std::string & phoneNum)237 bool AudioControlManager::PlayRingtone(const std::string &phoneNum)
238 {
239 if (!IsNumberAllowed(phoneNum)) {
240 TELEPHONY_LOGE("number is not allowed");
241 return false;
242 }
243 if (!ShouldPlayRingtone()) {
244 TELEPHONY_LOGE("should not play ringtone");
245 return false;
246 }
247 ring_ = std::make_unique<Ring>();
248 if (ring_ == nullptr) {
249 TELEPHONY_LOGE("create ring failed");
250 return false;
251 }
252 if (ring_->Play() != TELEPHONY_SUCCESS) {
253 TELEPHONY_LOGE("play ringtone failed");
254 return false;
255 }
256 TELEPHONY_LOGI("play ringtone success");
257 return true;
258 }
259
PlayRingtone(const std::string & phoneNum,const std::string & ringtonePath)260 bool AudioControlManager::PlayRingtone(const std::string &phoneNum, const std::string &ringtonePath)
261 {
262 if (!IsNumberAllowed(phoneNum)) {
263 TELEPHONY_LOGE("number is not allowed");
264 return false;
265 }
266 if (!ShouldPlayRingtone()) {
267 TELEPHONY_LOGE("should not play ringtone");
268 return false;
269 }
270 ring_ = std::make_unique<Ring>(ringtonePath);
271 if (ring_ == nullptr) {
272 TELEPHONY_LOGE("create ring failed");
273 return false;
274 }
275 if (ring_->Play() == TELEPHONY_SUCCESS) {
276 TELEPHONY_LOGE("play ringtone failed");
277 return false;
278 }
279 TELEPHONY_LOGI("play ringtone success");
280 return true;
281 }
282
StopRingtone()283 bool AudioControlManager::StopRingtone()
284 {
285 if (ringState_ == RingState::STOPPED) {
286 TELEPHONY_LOGI("ringtone already stopped");
287 return true;
288 }
289 if (ring_ != nullptr && ring_->Stop() == TELEPHONY_SUCCESS) {
290 TELEPHONY_LOGI("stop ringtone success");
291 ring_ = nullptr;
292 return true;
293 }
294 TELEPHONY_LOGE("stop ringtone failed");
295 return false;
296 }
297
SetAudioInterruptState(AudioInterruptState state)298 void AudioControlManager::SetAudioInterruptState(AudioInterruptState state)
299 {
300 TELEPHONY_LOGI("set audio interrupt state : %{public}d", state);
301 audioInterruptState_ = state;
302 /**
303 * send audio interrupt event to audio device manager , maybe need to reinitialize the audio device
304 */
305 AudioEvent event = AudioEvent::UNKNOWN_EVENT;
306 switch (audioInterruptState_) {
307 case AudioInterruptState::INTERRUPT_STATE_ACTIVATED:
308 event = AudioEvent::AUDIO_ACTIVATED;
309 break;
310 case AudioInterruptState::INTERRUPT_STATE_DEACTIVATED:
311 event = AudioEvent::AUDIO_DEACTIVATED;
312 break;
313 case AudioInterruptState::INTERRUPT_STATE_RINGING:
314 event = AudioEvent::AUDIO_RINGING;
315 break;
316 default:
317 break;
318 }
319 if (event == AudioEvent::UNKNOWN_EVENT) {
320 return;
321 }
322 DelayedSingleton<AudioDeviceManager>::GetInstance()->ProcessEvent(event);
323 }
324
325 /**
326 * while audio state changed , maybe need to reinitialize the audio device
327 * in order to get the initialization status of audio device , need to consider varieties of audio conditions
328 */
GetInitAudioDevice() const329 AudioDevice AudioControlManager::GetInitAudioDevice() const
330 {
331 if (audioInterruptState_ == AudioInterruptState::INTERRUPT_STATE_DEACTIVATED) {
332 return AudioDevice::DEVICE_DISABLE;
333 } else {
334 // interrupted or ringing , priority : bt sco , wired headset , speaker
335 if (AudioDeviceManager::IsBtScoConnected()) {
336 return AudioDevice::DEVICE_BLUETOOTH_SCO;
337 }
338 if (AudioDeviceManager::IsWiredHeadsetConnected()) {
339 return AudioDevice::DEVICE_WIRED_HEADSET;
340 }
341 if (audioInterruptState_ == AudioInterruptState::INTERRUPT_STATE_RINGING) {
342 return AudioDevice::DEVICE_SPEAKER;
343 }
344 if (GetCurrentActiveCall() != nullptr && GetCurrentActiveCall()->IsSpeakerphoneOn()) {
345 TELEPHONY_LOGI("current call speaker is active");
346 return AudioDevice::DEVICE_SPEAKER;
347 }
348 if (AudioDeviceManager::IsEarpieceAvailable()) {
349 return AudioDevice::DEVICE_EARPIECE;
350 }
351 return AudioDevice::DEVICE_SPEAKER;
352 }
353 }
354
355 /**
356 * @param isMute , mute state
357 * usually called by the ui interaction , mute or unmute microphone
358 */
SetMute(bool isMute)359 int32_t AudioControlManager::SetMute(bool isMute)
360 {
361 bool hasCall = DelayedSingleton<CallControlManager>::GetInstance()->HasCall();
362 if (!hasCall) {
363 TELEPHONY_LOGE("no call exists, set mute failed");
364 return CALL_ERR_AUDIO_SETTING_MUTE_FAILED;
365 }
366 bool enabled = false;
367 if ((DelayedSingleton<CallControlManager>::GetInstance()->HasEmergency(enabled) == TELEPHONY_SUCCESS) && enabled) {
368 isMute = false;
369 }
370 if (DelayedSingleton<AudioProxy>::GetInstance()->SetMicrophoneMute(isMute)) {
371 TELEPHONY_LOGI("set mute success , current mute state : %{public}d", isMute);
372 return TELEPHONY_SUCCESS;
373 }
374 TELEPHONY_LOGE("set mute failed");
375 return CALL_ERR_AUDIO_SETTING_MUTE_FAILED;
376 }
377
MuteRinger()378 int32_t AudioControlManager::MuteRinger()
379 {
380 if (ringState_ == RingState::STOPPED) {
381 TELEPHONY_LOGI("ring already stopped");
382 return TELEPHONY_SUCCESS;
383 }
384 if (ring_ != nullptr && ring_->Stop() == TELEPHONY_SUCCESS) {
385 TELEPHONY_LOGI("mute ring success");
386 return TELEPHONY_SUCCESS;
387 }
388 TELEPHONY_LOGE("mute ring failed");
389 return CALL_ERR_AUDIO_SETTING_MUTE_FAILED;
390 }
391
PlayCallEndedTone(TelCallState priorState,TelCallState nextState,CallEndedType type)392 void AudioControlManager::PlayCallEndedTone(TelCallState priorState, TelCallState nextState, CallEndedType type)
393 {
394 if (nextState != TelCallState::CALL_STATUS_DISCONNECTED) {
395 return;
396 }
397 if (priorState == TelCallState::CALL_STATUS_ACTIVE || priorState == TelCallState::CALL_STATUS_DIALING ||
398 priorState == TelCallState::CALL_STATUS_HOLDING) {
399 switch (type) {
400 case CallEndedType::PHONE_IS_BUSY:
401 PlayCallTone(ToneDescriptor::TONE_ENGAGED);
402 break;
403 case CallEndedType::CALL_ENDED_NORMALLY:
404 PlayCallTone(ToneDescriptor::TONE_FINISHED);
405 break;
406 case CallEndedType::UNKNOWN:
407 PlayCallTone(ToneDescriptor::TONE_UNKNOWN);
408 break;
409 case CallEndedType::INVALID_NUMBER:
410 PlayCallTone(ToneDescriptor::TONE_INVALID_NUMBER);
411 break;
412 default:
413 break;
414 }
415 }
416 }
417
GetCallList()418 std::set<sptr<CallBase>> AudioControlManager::GetCallList()
419 {
420 return totalCalls_;
421 }
422
GetCurrentActiveCall() const423 sptr<CallBase> AudioControlManager::GetCurrentActiveCall() const
424 {
425 std::string number = DelayedSingleton<CallStateProcessor>::GetInstance()->GetCurrentActiveCall();
426 if (!number.empty()) {
427 return GetCallBase(number);
428 }
429 return nullptr;
430 }
431
GetCallBase(const std::string & phoneNum) const432 sptr<CallBase> AudioControlManager::GetCallBase(const std::string &phoneNum) const
433 {
434 sptr<CallBase> callBase = nullptr;
435 if (phoneNum.empty()) {
436 return callBase;
437 }
438 for (auto &call : totalCalls_) {
439 if (call->GetAccountNumber() == phoneNum) {
440 callBase = call;
441 break;
442 }
443 }
444 return callBase;
445 }
446
IsEmergencyCallExists() const447 bool AudioControlManager::IsEmergencyCallExists() const
448 {
449 for (auto call : totalCalls_) {
450 if (call->GetEmergencyState()) {
451 return true;
452 }
453 }
454 return false;
455 }
456
GetAudioInterruptState()457 AudioInterruptState AudioControlManager::GetAudioInterruptState()
458 {
459 return audioInterruptState_;
460 }
461
SetVolumeAudible()462 void AudioControlManager::SetVolumeAudible()
463 {
464 DelayedSingleton<AudioProxy>::GetInstance()->SetVolumeAudible();
465 }
466
SetRingState(RingState state)467 void AudioControlManager::SetRingState(RingState state)
468 {
469 ringState_ = state;
470 }
471
SetLocalRingbackNeeded(bool isNeeded)472 void AudioControlManager::SetLocalRingbackNeeded(bool isNeeded)
473 {
474 isLocalRingbackNeeded_ = isNeeded;
475 }
476
IsNumberAllowed(const std::string & phoneNum)477 bool AudioControlManager::IsNumberAllowed(const std::string &phoneNum)
478 {
479 // check whether the phone number is allowed or not , should not ring if number is not allowed
480 return true;
481 }
482
ShouldPlayRingtone() const483 bool AudioControlManager::ShouldPlayRingtone() const
484 {
485 auto processor = DelayedSingleton<CallStateProcessor>::GetInstance();
486 int32_t alertingCallNum = processor->GetCallNumber(TelCallState::CALL_STATUS_ALERTING);
487 int32_t incomingCallNum = processor->GetCallNumber(TelCallState::CALL_STATUS_INCOMING);
488 if (incomingCallNum != EXIST_ONLY_ONE_CALL || alertingCallNum > EMPTY_VALUE ||
489 ringState_ == RingState::RINGING) {
490 return false;
491 }
492 return true;
493 }
494
IsAudioActivated() const495 bool AudioControlManager::IsAudioActivated() const
496 {
497 return audioInterruptState_ == AudioInterruptState::INTERRUPT_STATE_ACTIVATED ||
498 audioInterruptState_ == AudioInterruptState::INTERRUPT_STATE_RINGING;
499 }
500
PlayCallTone(ToneDescriptor type)501 int32_t AudioControlManager::PlayCallTone(ToneDescriptor type)
502 {
503 tone_ = std::make_unique<Tone>(type);
504 if (tone_ == nullptr) {
505 TELEPHONY_LOGE("create tone failed");
506 return TELEPHONY_ERR_LOCAL_PTR_NULL;
507 }
508 if (tone_->Play() == TELEPHONY_SUCCESS) {
509 isTonePlaying_ = true;
510 return TELEPHONY_SUCCESS;
511 }
512 TELEPHONY_LOGE("play call tone failed");
513 return CALL_ERR_AUDIO_TONE_PLAY_FAILED;
514 }
515
StopCallTone()516 int32_t AudioControlManager::StopCallTone()
517 {
518 if (!isTonePlaying_) {
519 TELEPHONY_LOGI("tone is already stopped");
520 return TELEPHONY_SUCCESS;
521 }
522 if (tone_ != nullptr && tone_->Stop() == TELEPHONY_SUCCESS) {
523 isTonePlaying_ = false;
524 tone_ = nullptr;
525 return TELEPHONY_SUCCESS;
526 }
527 TELEPHONY_LOGE("stop call tone failed");
528 return CALL_ERR_AUDIO_TONE_STOP_FAILED;
529 }
530
IsTonePlaying() const531 bool AudioControlManager::IsTonePlaying() const
532 {
533 return isTonePlaying_;
534 }
535
IsCurrentRinging() const536 bool AudioControlManager::IsCurrentRinging() const
537 {
538 return ringState_ == RingState::RINGING;
539 }
540
PlayRingback()541 int32_t AudioControlManager::PlayRingback()
542 {
543 if (!isLocalRingbackNeeded_) {
544 return CALL_ERR_AUDIO_TONE_PLAY_FAILED;
545 }
546 return PlayCallTone(ToneDescriptor::TONE_RINGBACK);
547 }
548
StopRingback()549 int32_t AudioControlManager::StopRingback()
550 {
551 return StopCallTone();
552 }
553
PlayWaitingTone()554 int32_t AudioControlManager::PlayWaitingTone()
555 {
556 return PlayCallTone(ToneDescriptor::TONE_WAITING);
557 }
558
StopWaitingTone()559 int32_t AudioControlManager::StopWaitingTone()
560 {
561 return StopCallTone();
562 }
563
NewCallCreated(sptr<CallBase> & callObjectPtr)564 void AudioControlManager::NewCallCreated(sptr<CallBase> &callObjectPtr) {}
565
CallDestroyed(const DisconnectedDetails & details)566 void AudioControlManager::CallDestroyed(const DisconnectedDetails &details) {}
567 } // namespace Telephony
568 } // namespace OHOS