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